This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Get pointer to Petsc matrix?

+4 votes

In the C++ interface of fenics; How do I get the pointer to the underlying Petsc object of a matrix so that I can directly use the functionality of Petsc? (I need direct access to the matrix entries, so that I can change some values.)

asked Jul 10, 2014 by BB FEniCS Novice (710 points)

2 Answers

+2 votes
 
Best answer
// Create PETSc-based matrix
PETScMatrix A;

// Access underlying PETSc object
Mat mat = A.mat();
answered Jul 10, 2014 by Garth N. Wells FEniCS Expert (35,930 points)
selected Jul 10, 2014 by BB
+1 vote

You can use a dynamic pointer cast to reach the base vector/matrix

For example:

std::dynamic_pointer_cast<PETScVector>(x.vector())

I find this useful if I have a Function which has a vector that was created by an internal factory. I can check if PETSc is in use, and then do the dynamic cast to do anything PETSc specific (like apply("")).

answered Jul 15, 2014 by Charles FEniCS User (4,220 points)
...