[petsc-users] Repeated Solves

Adam Byrd adam1.byrd at gmail.com
Wed Jul 20 13:58:36 CDT 2011


I'm attempting to integrate PETSc into an existing simulation and I have
successfully gotten PETSc to handle the first matrix inversion, but I'm
having trouble setting it up properly for subsequent inversions. It
currently does the inversions without complaining, but gives incorrect
output. I believe I need to 'reset' the matrix being inverted. I am
inverting the same matrix each time, but the values and nonzero structure
change. I create and set up the matrix, ksp, and pc once, then call a
routine that fills the matrix and calls KSPSetOperators() before using
MatMatSolve again. The routine is called multiple times.

I am including the actual code, but here is the outline of what I'm
currently trying:

//Setup hamiltonian

ierr = MatCreate(PETSC_COMM_WORLD, &hamiltonian);CHKERRQ(ierr);

ierr = MatSetType(hamiltonian, MATAIJ);CHKERRQ(ierr);

ierr = MatSetSizes(hamiltonian, PETSC_DECIDE, PETSC_DECIDE, rows,
columns);CHKERRQ(ierr);

ierr = MatAssemblyBegin(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);

ierr = MatAssemblyEnd(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);


//Setup PETSc Solver

ierr = KSPCreate(PETSC_COMM_WORLD, &ksp);CHKERRQ(ierr);

ierr = KSPSetOperators(ksp, hamiltonian, hamiltonian,
SAME_NONZERO_PATTERN);CHKERRQ(ierr);

ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);

ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);

ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);

ierr = test_invert(hamiltonian, inverseHamiltonian, identityMat, pc, ksp,
...);CHKERRQ(ierr);



test_invert (looped part):


//Several MatSetValue() calls

//Setup PETSc Hamiltonian

ierr = MatAssemblyBegin(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);

ierr = MatAssemblyEnd(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);

 //Setup PETSc Solver

ierr = KSPSetOperators(ksp, hamiltonian, hamiltonian,
DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);

ierr = KSPSetUp(ksp);CHKERRQ(ierr);

 //Factor and solve

ierr = PCFactorGetMatrix(pc, &hamiltonian);CHKERRQ(ierr);

ierr = MatMatSolve(hamiltonian, identityMat,
inverseHamiltonian);CHKERRQ(ierr);



What needs to be done to the matrix to clear it out and use it again?
MatZeroEntries doesn't want to work on an unfactored matrix, plus the
nonzero structure changes. Do I need to reset the inverseHamiltonian as
well?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20110720/eb5587e9/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: petsccntor.zip
Type: application/zip
Size: 76411 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20110720/eb5587e9/attachment-0001.zip>


More information about the petsc-users mailing list