> On 2 Jul 2024, at 10:59 AM, maitri ksh <maitri....@gmail.com> wrote:
> 
> A variable 'arecomplex' which is by default set to false inside 
> PetscBinaryRead() overrides 'complex', true in PetscBinaryRead(file, 
> "complex", true) thus giving real valued output unless one manually changes 
> arecomplex=true  inside the function.

No, you do not need to edit this function.
I’m not a MATLAB expert, but I guess there is a difference between “complex", 
true (what you are using) and 'complex', true (what I told you to use).

Thanks,
Pierre

> On Mon, Jul 1, 2024 at 5:40 PM Pierre Jolivet <pie...@joliv.et 
> <mailto:pie...@joliv.et>> wrote:
>> 
>> 
>>> On 1 Jul 2024, at 4:37 PM, maitri ksh <maitri....@gmail.com 
>>> <mailto:maitri....@gmail.com>> wrote:
>>> 
>>> This Message Is From an External Sender
>>> This message came from outside your organization.
>>> I need to export complex vectors data from PETSc and read them in MATLAB. 
>>> However, I am encountering some issues with the data format and 
>>> interpretation in MATLAB.  
>>> 
>>> code-snippet of the vector data export section:
>>> // Assemble the vectors before exporting
>>> ierr = VecAssemblyBegin(f); CHKERRQ(ierr);
>>> ierr = VecAssemblyEnd(f); CHKERRQ(ierr);
>>> 
>>> PetscViewer viewerf;
>>> // Save the complex vectors to binary files
>>> ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD, "output_f.dat", 
>>> FILE_MODE_WRITE, &viewerf); CHKERRQ(ierr);
>>> ierr = VecView(f, viewerf); CHKERRQ(ierr);
>>> ierr = PetscViewerDestroy(&viewerf); CHKERRQ(ierr);
>>> 
>>> // Create vectors to store the magnitudes
>>> Vec f_magnitude;
>>> ierr = VecDuplicate(f, &f_magnitude); CHKERRQ(ierr);
>>> 
>>> // Get local portion of the vectors
>>> const PetscScalar *f_array;
>>> PetscScalar *f_magnitude_array;
>>> PetscInt n_local;
>>> 
>>> ierr = VecGetLocalSize(f, &n_local); CHKERRQ(ierr);
>>> ierr = VecGetArrayRead(f, &f_array); CHKERRQ(ierr);
>>> ierr = VecGetArray(f_magnitude, &f_magnitude_array); CHKERRQ(ierr);
>>> 
>>> // Compute the magnitude for each element
>>> for (int i = 0; i < n_local; i++) {
>>>     f_magnitude_array[i] = PetscAbsScalar(f_array[i]);
>>> }
>>> 
>>> // Restore arrays
>>> ierr = VecRestoreArrayRead(f, &f_array); CHKERRQ(ierr);
>>> ierr = VecRestoreArray(f_magnitude, &f_magnitude_array); CHKERRQ(ierr);
>>> 
>>> // Assemble the magnitude vectors
>>> ierr = VecAssemblyBegin(f_magnitude); CHKERRQ(ierr);
>>> ierr = VecAssemblyEnd(f_magnitude); CHKERRQ(ierr);
>>> 
>>> // Save the magnitude vectors to binary files
>>> PetscViewer viewerfmag;
>>> ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD, "output_f_mag.dat", 
>>> FILE_MODE_WRITE, &viewerfmag); CHKERRQ(ierr);
>>> ierr = VecView(f_magnitude, viewerfmag); CHKERRQ(ierr);
>>> ierr = PetscViewerDestroy(&viewerfmag); CHKERRQ(ierr);
>>> 
>>> In MATLAB, I am using petscBinaryRead to read the data. The complex vectors 
>>> are read, but only the real part is available. The magnitude vectors are 
>>> however read as alternating zero and non-zero elements. What went wrong?
>>> How can I export the data correctly to MATLAB-accessible format? (I have 
>>> not configured PETSc with Matlab as I was encountering library conflict 
>>> issues)
>> 
>> 
>> How are you reading complex-valued binary files in MATLAB?
>> It is not the same as real-valued files, in particular, you must add the 
>> parameters 'complex', true to the PetscBinaryRead() call, type help 
>> PetscBinaryRead in the MATLAB console.
>> 
>> Thanks,
>> Pierre
>> 

Reply via email to