[petsc-users] save vector to bin-file at each time step

Barry Smith bsmith at mcs.anl.gov
Wed Dec 14 08:32:58 CST 2011


On Dec 14, 2011, at 8:00 AM, robert wrote:

> Hello, 
> 
> I am quite new to petsc and therefore my question might be quite trivial.
> 
> I have a 1D finite difference code and want to store several vectors in a binary:
> 
>     ierr = PetscViewerCreate(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
>     ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"out.bin",FILE_MODE_APPEND,&viewer); CHKERRQ(ierr);
>     ierr = PetscObjectSetName((PetscObject)u,"u");CHKERRQ(ierr);
>     ierr = PetscObjectSetName((PetscObject)rhs,"rhs");CHKERRQ(ierr);
>     ierr = VecView(u,viewer); CHKERRQ(ierr);
>     ierr = VecView(rhs,viewer);CHKERRQ(ierr);
> 
> However, when I read the file with matlab (PetscBinaryRead.m) I only get the first vector.

   1) Did you call PetscViewerDestroy() before the end of the program? If not called all the data may not be flushed into the file.

   2) How are you reading the vectors into Matlab? For example   [u,rhs] = PetscBinaryRead('filename') will read in two vectors.  If you want to call PetscBinaryRead() repeatedly to read more information than read the help for PetscBinaryRead() carefully, you cannot simply call u = PetscBinaryRead('filename'); rhs = PetscBinaryRead('filename') and hope to get the second vector 

  For example: fd = PetscOpenFile('filename');
                u = PetscBinaryRead(fd);   % get first vector
                rhs = PetscBinaryRead(fd);  % get second vector

  Barry


> 
> In further application I would like to use something similar but putting out the solution vector every timestep.
> 
> Could someone just provide me some sample code?
> 
> Thanks, 
> Robert
> 
> 



More information about the petsc-users mailing list