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)