<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
I am using Eigen library to which I have to write these vector values from PETSc Vec. tdof is the length of the vector that I need and ei is the number of value in as an index:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0">
PetscInt    ei[tdof];<br class="ContentPasted0">
PetscScalar eveci[tdof];<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0 ContentPasted1">
for (int i = 0; i < tdof; i++)<br class="ContentPasted1">
        ei[i] = i;<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0 ContentPasted1">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0 ContentPasted1 ContentPasted2">
if (nconv>0)
<div class="ContentPasted2">    {</div>
<div class="ContentPasted2">        eval.setZero(nconv);</div>
<div class="ContentPasted2">        evec.setZero(KS.rows(),nconv);</div>
<div class="ContentPasted2">        PetscCall(PetscPrintf(PETSC_COMM_WORLD,</div>
<div class="ContentPasted2">             "           k          ||Ax-kx||/||kx||\n"</div>
<div class="ContentPasted2">             "   ----------------- ------------------\n"));</div>
<div><br class="ContentPasted2">
</div>
<div class="ContentPasted2">        for (int i = 0; i < nconv; i++)</div>
<div class="ContentPasted2">        {</div>
<div class="ContentPasted2">            PetscCall(EPSGetEigenpair(eps,i,&kr,&ki,xr,xi));</div>
<div class="ContentPasted2">            PetscCall(EPSComputeError(eps,i,EPS_ERROR_RELATIVE,&error1));</div>
<div><br class="ContentPasted2">
</div>
<div class="ContentPasted2">            #if defined(PETSC_USE_COMPLEX)</div>
<div class="ContentPasted2">                re = PetscRealPart(kr);</div>
<div class="ContentPasted2">                im = PetscImaginaryPart(kr);</div>
<div class="ContentPasted2">            #else</div>
<div class="ContentPasted2">                re = kr;</div>
<div class="ContentPasted2">                im = ki;</div>
<div class="ContentPasted2">            #endif</div>
<div class="ContentPasted2">            if (im!=0.0) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %9f%+9fi %12g\n",(double)re,(double)im,(double)error1));</div>
<div class="ContentPasted2">            else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12g\n",(double)re,(double)error1));</div>
<div class="ContentPasted2">            eval(i) = re;</div>
<div class="ContentPasted2">            VecGetValues(xr, tdof, ei, eveci);</div>
<div class="ContentPasted2">            for (int j = 0; j < tdof; j++)</div>
<div class="ContentPasted2">            {</div>
<div class="ContentPasted2">                evec(j, i) = eveci[j];</div>
<div class="ContentPasted2">            }</div>
<div class="ContentPasted2">        }</div>
<div class="ContentPasted2">        PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));</div>
    }<br>
</div>
<div id="signature_bookmark"></div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)" class="elementToProof">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)" class="elementToProof">
Thank you </div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)" class="elementToProof">
Ali</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Matthew Knepley <knepley@gmail.com><br>
<b>Sent:</b> Tuesday, November 15, 2022 9:43 AM<br>
<b>To:</b> Mohammad Ali Yaqteen <mhyaqteen@sju.ac.kr><br>
<b>Cc:</b> petsc-users@mcs.anl.gov <petsc-users@mcs.anl.gov><br>
<b>Subject:</b> Re: [petsc-users] Reading Vectors from a PETSc Vec</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr">On Mon, Nov 14, 2022 at 7:39 PM Mohammad Ali Yaqteen <<a href="mailto:mhyaqteen@sju.ac.kr" data-auth="NotApplicable">mhyaqteen@sju.ac.kr</a>> wrote:<br>
</div>
<div class="x_gmail_quote">
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div class="x_msg-4839079817109792065">
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
I am using the following procedure to read from Vec, but it keeps giving me the same values! I was told that using VecGetValues gives wrog output.
</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>By who? It does not give the wrong output.</div>
<div><br>
</div>
<div>You do not show where in the code you define tdof and ei[].</div>
<div><br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div class="x_msg-4839079817109792065">
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
If not this, then what function should be used to read the contents of a vector?</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>I think it would be simpler for you to use VecGetArrayRead(), unless you want values from other processes.</div>
<div><br>
</div>
<div>  Thanks,</div>
<div><br>
</div>
<div>    Matt</div>
<div> </div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div class="x_msg-4839079817109792065">
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
for (int i = 0; i < nconv; i++)
<div>        {</div>
<div>            PetscCall(EPSGetEigenpair(eps,i,&kr,&ki,xr,xi));</div>
<div>            PetscCall(EPSComputeError(eps,i,EPS_ERROR_RELATIVE,&error1));</div>
<div><br>
</div>
<div>            #if defined(PETSC_USE_COMPLEX)</div>
<div>                re = PetscRealPart(kr);</div>
<div>                im = PetscImaginaryPart(kr);</div>
<div>            #else</div>
<div>                re = kr;</div>
<div>                im = ki;</div>
<div>            #endif</div>
<div>            if (im!=0.0) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %9f%+9fi %12g\n",(double)re,(double)im,(double)error1));</div>
<div>            else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12g\n",(double)re,(double)error1));</div>
<div>            eval(i) = re;</div>
<div>            VecGetValues(xr, tdof, ei, eveci);</div>
<div>            for (int j = 0; j < tdof; j++)</div>
<div>            {</div>
<div>                evec(j, i) = eveci[j];</div>
<div>            }</div>
<div>        }</div>
        PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Thank you</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Ali</div>
</div>
</div>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr" class="x_gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>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</div>
<div><br>
</div>
<div><a href="http://www.cse.buffalo.edu/~knepley/" data-auth="NotApplicable">https://www.cse.buffalo.edu/~knepley/</a><br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>