[petsc-users] problem in calling KSPSetComputeRHS and KSPSolve multiple times to solve different RHS.

Bishesh Khanal bisheshkh at gmail.com
Fri Sep 13 06:21:28 CDT 2013

Dear all,
I'm trying to reuse an operator for the linear system multiple times with
different RHS.
But it seems that the RHS is not changed in the KSP object. Here is the
code to illustrate what I'm doing:

Vec mX[3], mB[3];   //Solving Ax=b for 3 different RHS.
ierr = KSPSetComputeOperators(mKsp,computeMatrix,this);CHKERRQ(ierr);
ierr = KSPSetDM(mKsp,mDa);CHKERRQ(ierr);
ierr = KSPSetFromOptions(mKsp);CHKERRQ(ierr);

for(int i = 0; i<3; ++i) {
        mBComponent = i;   //sets different component for RHS.
        ierr = KSPSetComputeRHS(mKsp,computeRhs,this);CHKERRQ(ierr);
        ierr = KSPSetDM(mKsp,mDa);CHKERRQ(ierr);
        ierr = KSPSolve(mKsp,NULL,NULL);CHKERRQ(ierr);
        ierr = KSPGetSolution(mKsp,&mX[i]);CHKERRQ(ierr);
        ierr = KSPGetRhs(mKsp,&mB[i]);CHKERRQ(ierr);

What I expect is to have three different solutions in mX array for the
different Rhs computed by computeRhs function. However, the result when I
see using VecView is that the Rhs mB[0], mB[1] and mB[2] are all identical
(which should not have been the case). And mX also has three identical
vectors as solution.
What am I doing wrong here ?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20130913/8004d813/attachment.html>

More information about the petsc-users mailing list