[petsc-users] Reading Vectors from a PETSc Vec

Matthew Knepley knepley at gmail.com
Mon Nov 14 18:53:44 CST 2022


On Mon, Nov 14, 2022 at 7:50 PM Mohammad Ali Yaqteen <mhyaqteen at sju.ac.kr>
wrote:

> 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:
>
> PetscInt    ei[tdof];
> PetscScalar eveci[tdof];
>
> for (int i = 0; i < tdof; i++)
>         ei[i] = i;
>

If you are running in serial, just use VecGetArrayRead().

  Thanks,

    Matt


> if (nconv>0)
>     {
>         eval.setZero(nconv);
>         evec.setZero(KS.rows(),nconv);
>         PetscCall(PetscPrintf(PETSC_COMM_WORLD,
>              "           k          ||Ax-kx||/||kx||\n"
>              "   ----------------- ------------------\n"));
>
>         for (int i = 0; i < nconv; i++)
>         {
>             PetscCall(EPSGetEigenpair(eps,i,&kr,&ki,xr,xi));
>             PetscCall(EPSComputeError(eps,i,EPS_ERROR_RELATIVE,&error1));
>
>             #if defined(PETSC_USE_COMPLEX)
>                 re = PetscRealPart(kr);
>                 im = PetscImaginaryPart(kr);
>             #else
>                 re = kr;
>                 im = ki;
>             #endif
>             if (im!=0.0) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %9f%+9fi
> %12g\n",(double)re,(double)im,(double)error1));
>             else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"   %12f
> %12g\n",(double)re,(double)error1));
>             eval(i) = re;
>             VecGetValues(xr, tdof, ei, eveci);
>             for (int j = 0; j < tdof; j++)
>             {
>                 evec(j, i) = eveci[j];
>             }
>         }
>         PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
>     }
>
> Thank you
> Ali
> ------------------------------
> *From:* Matthew Knepley <knepley at gmail.com>
> *Sent:* Tuesday, November 15, 2022 9:43 AM
> *To:* Mohammad Ali Yaqteen <mhyaqteen at sju.ac.kr>
> *Cc:* petsc-users at mcs.anl.gov <petsc-users at mcs.anl.gov>
> *Subject:* Re: [petsc-users] Reading Vectors from a PETSc Vec
>
> On Mon, Nov 14, 2022 at 7:39 PM Mohammad Ali Yaqteen <mhyaqteen at sju.ac.kr>
> wrote:
>
> 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.
>
>
> By who? It does not give the wrong output.
>
> You do not show where in the code you define tdof and ei[].
>
> If not this, then what function should be used to read the contents of a
> vector?
>
>
> I think it would be simpler for you to use VecGetArrayRead(), unless you
> want values from other processes.
>
>   Thanks,
>
>     Matt
>
>
> for (int i = 0; i < nconv; i++)
>         {
>             PetscCall(EPSGetEigenpair(eps,i,&kr,&ki,xr,xi));
>             PetscCall(EPSComputeError(eps,i,EPS_ERROR_RELATIVE,&error1));
>
>             #if defined(PETSC_USE_COMPLEX)
>                 re = PetscRealPart(kr);
>                 im = PetscImaginaryPart(kr);
>             #else
>                 re = kr;
>                 im = ki;
>             #endif
>             if (im!=0.0) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %9f%+9fi
> %12g\n",(double)re,(double)im,(double)error1));
>             else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"   %12f
> %12g\n",(double)re,(double)error1));
>             eval(i) = re;
>             VecGetValues(xr, tdof, ei, eveci);
>             for (int j = 0; j < tdof; j++)
>             {
>                 evec(j, i) = eveci[j];
>             }
>         }
>         PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
>
> Thank you
> Ali
>
>
>
> --
> What most experimenters take for granted before they begin their
> experiments is infinitely more interesting than any results to which their
> experiments lead.
> -- Norbert Wiener
>
> https://www.cse.buffalo.edu/~knepley/
> <http://www.cse.buffalo.edu/~knepley/>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20221114/1414877a/attachment-0001.html>


More information about the petsc-users mailing list