[petsc-users] Setting entries of symmetric matrix

Florian Lindner mailinglists at xgm.de
Fri Sep 12 02:56:35 CDT 2014


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.

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;
}


More information about the petsc-users mailing list