[petsc-users] Reading Vectors from a PETSc Vec

Junchao Zhang junchao.zhang at gmail.com
Mon Nov 14 22:48:17 CST 2022


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

> I am sorry for the trouble but I don’t understand its usage. Like I want
> to read Vec xr. I am giving it to a pointer array. How would I access the
> values of the vector? Could you please briefly explain?
>
Suppose you have a vector x with global size 10, and you run with 2 MPI
ranks, then the code sketch would be

const PetscScalar *a;
VecGetArrayRead(x, &a);
 // code reading a[].  On rank 0, a[0]~[a4] contains x[0]~x[4]; on rank 1,
a[0]~[a4] contains x[5]~x[9]
VecRestoreArrayRead(x, &a);

See manuals at
https://petsc.org/release/docs/manualpages/Vec/VecGetArrayRead/,
https://petsc.org/release/docs/manualpages/Vec/VecGetLocalSize/, and also
examples there.


>
>
> *From:* Matthew Knepley <knepley at gmail.com>
> *Sent:* Tuesday, November 15, 2022 9:54 AM
> *To:* Mohammad Ali Yaqteen <mhyaqteen at sju.ac.kr>
> *Cc:* petsc-users at mcs.anl.gov
> *Subject:* Re: [petsc-users] Reading Vectors from a PETSc Vec
>
>
>
> 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/62194f0d/attachment.html>


More information about the petsc-users mailing list