[petsc-users] using real and complex together

Sam Guo sam.guo at cd-adapco.com
Mon Sep 28 14:42:30 CDT 2020


Hi Stefano and PETSc dev team,
   I want to try your suggestion to always load complex version of PETSc
but if my input matrix A is real, I want to create shell matrix to
matrix-vector and factorization using real only.
   I still need to understand how MatRealPart works. Does it just zero out
the image numerical values or does it delete the image memory? If my input
matrix A is real, how do I create a shell matrix to matrix -vector
multiplication y=A*x where A is real, PestcScalar = complex, x and y are
Vec? I notice there is a VecRealPart but it seems it just zeros the image
numerical values. It seems I still have to create a PetscReal pointer  to
copy the real part of PetacScalar pointers like following. Can you comment
on it?

Thanks,
Sam

PetscScalar *px = nullptr;
VecGetArrayRead(x, &px);
PetscScalar *py = nullptr;
VecGetArray(y, &py);
int localSize = 0;
VecGetLocalSize(x, &localSize);
std::vector<PetasReal> realX(localSize); // I am using c++ to call PETSc

//retrieve real part
for(int i = 0; i < localSize; i++)  realX[i] = PetscRealPart(px[i]);

// do real matrix-vector multiplication
// realY=A*realX
// here where realY is std::vector<PetscReal>

//put real part back to py
  for(int i = 0; i < localSize; i++)  pv[i] = realY[i];
VecRestoreArray(y,&py);

On Tue, May 26, 2020 at 1:49 PM Sam Guo <sam.guo at cd-adapco.com> wrote:

> Thanks
>
> On Tuesday, May 26, 2020, Stefano Zampini <stefano.zampini at gmail.com>
> wrote:
>
>> All the solvers/matrices/vectors works for PetscScalar types (i.e. in
>> your case complex)
>> If you need to solve for the real part only, you can duplicate the matrix
>> and call MatRealPart to zero out the imaginary part. But the solve will
>> always run in the complex space
>> You should not be worried about doubling the memory for a matrix (i.e.
>> real and imaginary part)
>>
>>
>> On May 26, 2020, at 11:28 PM, Sam Guo <sam.guo at cd-adapco.com> wrote:
>>
>> complex version is needed since matrix sometimes is real and sometimes is
>> complex. I want to solve real matrix without allocating memory for
>> imaginary part((except eigen pairs).
>>
>> On Tuesday, May 26, 2020, Zhang, Hong <hzhang at mcs.anl.gov> wrote:
>>
>>> You can build PETSc with complex version, and declare some variables as
>>> 'PETSC_REAL'.
>>> Hong
>>>
>>> ------------------------------
>>> *From:* petsc-users <petsc-users-bounces at mcs.anl.gov> on behalf of Sam
>>> Guo <sam.guo at cd-adapco.com>
>>> *Sent:* Tuesday, May 26, 2020 1:00 PM
>>> *To:* PETSc <petsc-users at mcs.anl.gov>
>>> *Subject:* [petsc-users] using real and complex together
>>>
>>> Dear PETSc dev team,
>>>    Can I use both real and complex versions together?
>>>
>>> Thanks,
>>> Sam
>>>
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20200928/89f40440/attachment.html>


More information about the petsc-users mailing list