<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'>
<p><span><span class="Apple-converted-space">Thank you but I am still confused, I still get the same error message when calling KSPDestroy(). My code looks something like this:</span></span></p>
<p><br /></p>
<p><span>ierr = MatShift(M,-E);<br /></span><span>ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);<br /></span><span>ierr = KSPSetOperators(ksp,M,M);<br /></span><span>ierr = KSPSetType(ksp,KSPPREONLY);<br /></span><span>ierr = KSPGetPC(ksp,&pc);<br /></span><span>ierr = PCSetType(pc,PCCHOLESKY);<br /></span><span>ierr = KSPSetUp(ksp);<br /></span><span>ierr = PCFactorGetMatrix(pc,&B);<br /></span><span>ierr = MatGetInertia(B,&nneg,&nzero,&npos);<br /></span><span>ierr = PetscPrintf(PETSC_COMM_WORLD,"nneg: %D<span class="Apple-converted-space">    </span>nzero: %D<span class="Apple-converted-space">    </span>npos: %D <span class="Apple-converted-space">  </span>\n",nneg,nzero,npos);<br /></span><span></span><span></span><span><span class="Apple-converted-space"></span>ierr = MatDestroy(&B);<br /></span><span></span><span>ierr = MatDestroy(&M);<br /></span><span>ierr = KSPDestroy(&ksp);</span><span></span></p>
<p><span>where I've defined the matrix M beforehand. What am I doing wrong?</span></p>
<p><span>Thanks again,</span></p>
<p><span>Best regards,</span></p>
<p><span>Perceval,</span></p>
<p><br /></p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">
<div class="pre" style="margin: 0; padding: 0; font-family: monospace">If you obtained the PC object with KSPGetPC() then you do not have to destroy it, only the KSP object. KSPGetPC() only gives a pointer to the internal object, which is managed by the KSP.<br />Jose<br /><br /><br />
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">El 24 mar 2020, a las 17:18, Perceval Desforges <<a href="mailto:perceval.desforges@polytechnique.edu">perceval.desforges@polytechnique.edu</a>> escribió:<br /><br />Thank you very much, this seems to work well.<br /><br />I have another question linked to this which is probably a bit basic so I apologize.<br /><br />At the end of my program, I want to destroy all the petsc objects. I start by destroying the matrixes, then the ksp and pc objects similarly to how it's done in the examples. However I get an error when attempting to destroy the KSP and PC objects :<br /><br />[0]PETSC ERROR: Invalid argument<br /><br />[0]PETSC ERROR: Wrong type of object: Parameter # 1<br /><br />I've tried switching the order around, but I still get the same errors. <br /><br />And if I don't destroy these objects I get a memory leak, which I'd like to avoid.<br /><br />My question is I don't really understand when and how I'm supposed to destroy the KSP and PC objects? <br /><br />Thanks again,<br /><br />Best regards,<br /><br />Perceval,<br /><br /><br /><br /><br /><br />
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">You can do this directly in PETSc. Create a KSP object with PREONLY and PCCHOLESKY (or just a PC object). Then call KSPSetUp (or PCSetUp) and extract the factored matrix with PCFactorGetMatrix(). Then call MatGetInertia() on the factored matrix. Repeat this for each value of E.<br /><br />I guess it can be even shorter if you call MatCholeskyFactor() directly.<br /><br />Jose<br /><br /><br />
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">El 24 mar 2020, a las 11:07, Perceval Desforges <<a href="mailto:perceval.desforges@polytechnique.edu">perceval.desforges@polytechnique.edu</a>> escribió:<br /><br />Dear petsc developers,<br /><br />I am interested in calculating the inertias of matrixes. Specifically, for a certain matrix A, and for different real numbers E, I want to calculate the inertias of (A - E * I), in order to get the number of eigenvalues less than E. <br /><br />In order to do this I have been setting up a slepc EPS object with spectrum slicing, and using EPSKrylovSchurGetInertias. I realize this is a bit convoluted, and was wondering if there is a better way to do this?<br /><br />Best regards,<br /><br />Perceval,<br /><br />P.S. my last email seems to not have been sent (I couldn't find it in the archives) so I am trying again...<br /><br /></blockquote>
</blockquote>
<br /><br /></blockquote>
</div>
</blockquote>
<p><br /></p>

</body></html>