On Tue, Feb 2, 2010 at 10:23 AM, Umut Tabak <span dir="ltr">&lt;<a href="mailto:u.tabak@tudelft.nl">u.tabak@tudelft.nl</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Dear all,<br>
<br>
I was trying to solve two linear systems in the same ksp context while setting the operators with KSPSetOperators. But I am getting a Memory corruption error. What  I try to do is to set two linear operators and rhs vectors and get the solutions, say<br>

<br>
for solA, xa,<br>
<br>
(Ka-lambs*Ma)*xa = -lambs*KsaT*modeS (I extract lambs from a vector of eigenvalues and modeS from a matrix of eigenvectors)<br>
<br>
and for solS, xs<br>
<br>
(Ks-lamba*Ms)*xs = -Ksa*modeP (similarly lamba is extracted from a vector and the modeP also from a matrix of vectors as columns)<br>
<br>
I am using MatDuplicate functions to update the operator matrices at each iteration, there is not a problem in the first solve, at least no errors, but on the second one I am getting the Memory corruption error mentioned above. Also one more question is on where to use KSPSetFromOptions on the iterations. If I use KSPSetFromOptions on the same iteration twice(before solve)I get the same error from this function(this was not also reasonable to me). Section of the code is below(a bit crappy however should give the idea on what I tried to do). I am sure this is a simple problem but could not find the reason.<br>
</blockquote><div><br>0) ALWAYS send the entire error message<br><br>1) ALWAYS check all the return values from the PETSc functions<br><br>2) When you get memory corruption, run with valgrind (as it says in the error message)<br>
<br>3) Solving twice does not produce problems.<br><br>   Matt<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
  KSP ksp;<br>
  KSPCreate(PETSC_COMM_WORLD, &amp;ksp);<br>
  for(int j=0; j!=nR; j++)<br>
  {<br>
    //KSPCreate(PETSC_COMM_WORLD, &amp;kspA);<br>
    // duplicate the matrix<br>
    MatDuplicate(Ka, MAT_COPY_VALUES, &amp;OpA);<br>
    //<br>
    MatAXPY(OpA, -eSolverS.getRoot(j), Ma, SAME_NONZERO_PATTERN);<br>
    MatGetColumnVector(eigVecsStr, modeS, j);<br>
    // create the rhs for the operation<br>
    MatMult(KsaT, modeS, rhsA);<br>
    VecScale(rhsA, -eSolverS.getRoot(j));<br>
    cout &lt;&lt; &quot;Solving the linear system for pressure side corrections&quot; &lt;&lt; endl;<br>
    KSPSetOperators(ksp, OpA, OpA, DIFFERENT_NONZERO_PATTERN);<br>
    //KSPGetPC(kspA, &amp;pcA);<br>
    //PCSetType(pcA, PCJACOBI);<br>
    //KSPSetTolerances(ksp, 1e-7, PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);<br>
    //KSPSetFromOptions(kspA);<br>
    // solve<br>
    KSPSolve(ksp,rhsA,solA);<br>
    corA.assignSubVector(solA, 1, ra, j);<br>
    //<br>
    MatDuplicate(Ks, MAT_COPY_VALUES, &amp;OpS);<br>
    MatAXPY(OpS, -eSolverA.getRoot(j), Ms, SAME_NONZERO_PATTERN);<br>
    MatGetColumnVector(eigVecsAco, modeA, j);<br>
    // create the rhs for the operation<br>
    MatMult(Ksa, modeA, rhsS);<br>
    VecScale(rhsS, -1.0);<br>
    cout &lt;&lt; &quot;Solving the linear system for structure side corrections&quot; &lt;&lt; endl;<br>
    //KSPCreate(PETSC_COMM_WORLD, &amp;kspS);<br>
    KSPSetOperators(ksp, OpS, OpS, DIFFERENT_NONZERO_PATTERN);<br>
    //KSPGetPC(kspS, &amp;pcS);<br>
    //PCSetType(pcS, PCJACOBI);<br>
    //KSPSetTolerances(kspS, 1e-7, PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);<br>
    //KSPSetFromOptions(kspS);<br>
    // solve<br>
    KSPSolve(ksp,rhsS,solS);<br>
    //int r,m;<br>
    //VecGetSize(rhsS, &amp;r);<br>
    //VecGetSize(solS, &amp;m);<br>
    //cout &lt;&lt; r &lt;&lt; &quot; &quot; &lt;&lt; m &lt;&lt; endl;<br>
    corS.assignSubVector(solS, 1, rs, j);<br>
    cout &lt;&lt; &quot;Solved  the linear system for structure corrections&quot; &lt;&lt; endl;<br>
  }<br>
  corA.finalize(); corS.finalize();<br>
<br>
Best,<br><font color="#888888">
Umut<br>
<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener<br>