[petsc-users] Setting entries of symmetric matrix

Matthew Knepley knepley at gmail.com
Fri Sep 12 07:49:37 CDT 2014


On Fri, Sep 12, 2014 at 2:56 AM, Florian Lindner <mailinglists at xgm.de>
wrote:

> Hello,
>
> I have a matrix that have the option set MAT_SYMMETRY_ETERNAL and set some
> values in the upper triangular. When reading values I was expecting that
> Petsc makes it a symmetric matrix, but the lower triangular is empty like
> it was initialized.
>

This is not the point of the functions. The mechanics of symmetry are
handled by the matrix type, SBAIJ. This
option is an optimization hint. It says that the matrix will respond that
it is symmetric if asked, which may be
much much cheaper than checking whether it is actually symmetric.

   Matt


> Thanks,
> Florian
>
> Example code:
>
>
> #include <iostream>
> #include "petscmat.h"
> #include "petscviewer.h"
>
> using namespace std;
>
> // Compiling with: mpic++ -g3 -Wall -I ~/software/petsc/include -I
> ~/software/petsc/arch-linux2-c-debug/include -L
> ~/software/petsc/arch-linux2-c-debug/lib -lpetsc test.cpp
>
> int main(int argc, char **args)
> {
>   PetscInitialize(&argc, &args, "", NULL);
>
>   PetscErrorCode ierr = 0;
>   int N = 4;
>   Mat matrix;
>
>   // Create dense matrix, but code should work for sparse, too (I hope)
>   // dense is more convenient to MatView.
>   ierr = MatCreateDense(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, N,
> N, NULL, &matrix); CHKERRQ(ierr);
>   ierr = MatSetUp(matrix); CHKERRQ(ierr);
>   ierr = MatSetOption(matrix, MAT_SYMMETRY_ETERNAL, PETSC_TRUE);
> CHKERRQ(ierr);
>
>   MatSetValue(matrix, 1, 1, 1, INSERT_VALUES);
>   MatSetValue(matrix, 1, 2, 2, INSERT_VALUES);
>   MatSetValue(matrix, 1, 3, 3, INSERT_VALUES);
>   MatSetValue(matrix, 2, 3, 4, INSERT_VALUES);
>
>   ierr = MatAssemblyBegin(matrix, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
>   ierr = MatAssemblyEnd(matrix, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
>
>   const PetscScalar *vals;
>   ierr = MatGetRow(matrix, 2, NULL, NULL, &vals);
>   cout << "Vals = " << vals[0] << " " << vals[1] << " " << vals[2] << " "
> << vals[3] << endl;
>   // prints:   Vals = 0 0 0 4
>   // excepted: Vals = 0 2 0 4
>   ierr = MatRestoreRow(matrix, 2, NULL, NULL, &vals);
>
>   ierr = MatView(matrix, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr);
>
>   PetscFinalize();
>   return 0;
> }
>



-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140912/ac275b51/attachment.html>


More information about the petsc-users mailing list