Actual source code: PetscReadBinaryTrajectory.m
petsc-3.10.5 2019-03-28
Report Typos and Errors

      1: function [varargout] = PetscBinaryReadTrajectory(inarg)
      2: %
      3: %   [varargout] = PetscBinaryReadTrajectory(inarg)
      4: %
      5: %  Read in the trajectory information saved in a folder of PETSc binary file
      6: %  Emit as Matlab struct
      7: %
      8: %  Examples: A = PetscBinaryReadTrajectory('myfolder'); read from myfolder.
      9: %            A = PetscBinaryReadTrajectory(); read from folder 'SA-data' or 'Visualization-data' if they exist, SA-data has the priority.
     10: %

     12: if nargin < 1
     13:   if exist('SA-data','dir')
     14:     inarg = 'SA-data';
     15:   elseif exist('Visualization-data','dir')
     16:     inarg = 'Visualization-data';
     17:   else
     18:     error('Can not find the folder of trajectory files!');
     19:   end
     20: end

     22: indices = 'int32';
     23: precision = 'float64';
     24: maxsteps = 10000;

     26: t = zeros(1,maxsteps);

     28: foundnames = 0;
     29: fullname = fullfile(inarg,'variablenames');
     30: if exist(fullname,'file') == 2
     31:   fd = PetscOpenFile(fullname);
     32:   n     = read(fd,1,indices);
     33:   sizes = read(fd,n,indices);
     34:   names = {'  '};
     35:   for i=1:n,
     36:     names{i} = deblank(char(read(fd,sizes(i),'uchar')))';
     37:   end
     38:   foundnames = 1;
     39: end

     41: for stepnum=1:maxsteps
     42:   filename = sprintf('SA-%06d.bin',stepnum-1);
     43:   fullname = fullfile(inarg,filename);
     44:   if exist(fullname,'file') ~= 2
     45:     steps = stepnum-1;
     46:     break;
     47:   end
     48:   fd = PetscOpenFile(fullname);
     49:   header = double(read(fd,1,indices));

     51:   if isempty(header)
     52:     steps = stepnum-1;
     53:     break;
     54:   end

     56:   if  header == 1211214 % Petsc Vec Object
     57:     %% Read state vector
     58:     m = double(read(fd,1,indices));
     59:     if (stepnum == 1)
     60:       x = zeros(m,maxsteps);
     61:     end
     62:     v = read(fd,m,precision);
     63:     x(:,stepnum) = v;

     65:     %% Read time
     66:     t(stepnum) = read(fd,1,precision);
     67:   end
     68:   % close the reader if we opened it
     69:   close(fd);
     70: end

     72: if steps > 1
     73:   varargout{1} = t(1:steps);
     74:   varargout{2} = x(:,1:steps);
     75:   if foundnames == 1
     76:     varargout{3} = names;
     77:   end
     78: end