Date: 2021-03-03T22:28:22+00:00

Actual source code: generatePetscTestFiles.m
petsc-3.14.5 2021-03-03
Report Typos and Errors

      1: % This MATLAB script generates test matrices for PETSc
      2: % using PETSc-MATLAB IO functions and the function laplacian.m from
      3: % http://www.mathworks.com/matlabcentral/fileexchange/27279-laplacian-in-1d-2d-or-3d

      6: clear all;
      7: nx=3; ny=4; n=nx*ny; % any sizes can be used
      8: % Create a nx-times-ny 2D negative Laplacian with h=1 and Dirichlet BC
      9: [~,~,A]=laplacian([nx,ny],{'DD' 'DD'});
     10: % Alternatevely, a nx-times-ny-times-nz 3D negative Laplacian with h=1 and Dirichlet BC
     11: %nz=2; n=nx*ny*nz; [~,~,A]=laplacian([nx,ny,nz],{'DD' 'DD' 'DD'});

     13: x=ones(n,1); b=A*x; %  use VecSet(x,1.0) in PETSc for x

     15: % this is the default
     16: PetscBinaryWrite('spd-real-int32-float64',A,b, 'indices','int32','precision','float64');
     17: [At,bt] = PetscBinaryRead('spd-real-int32-float64', 'indices','int32','precision','float64');
     18: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     19:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     20:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for spd-real-int32-float64');
     21: end

     23: % int64 only appears in the header
     24: PetscBinaryWrite('spd-real-int64-float64',A, b,'indices','int64','precision','float64');
     25: [At,bt] = PetscBinaryRead('spd-real-int64-float64','indices','int64','precision','float64');
     26: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     27:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     28:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for spd-real-int64-float64');
     29: end

     31: % MATLAB does not yet support single-presision sparce matrices
     32: % but all the entries in A, b, and x are actually integers
     33: PetscBinaryWrite('spd-real-int32-float32',A,b, 'indices','int32','precision','float32');
     34: [At,bt] = PetscBinaryRead('spd-real-int32-float32', 'indices','int32','precision','float32');
     35: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     36:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     37:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for spd-real-int32-float32');
     38: end

     40: % int64 only appears in the header
     41: PetscBinaryWrite('spd-real-int64-float32',A,b,'indices','int64','precision','float32');
     42: [At,bt] = PetscBinaryRead('spd-real-int64-float32','indices','int64','precision','float32');
     43: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     44:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     45:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for spd-real-int64-float32');
     46: end
     47:
     48: % Now, we swap the (1,1) and (1,2) entries in A to make it nonsymmetric
     49: tmp=A(1,1); A(1,1)=A(1,2); A(1,2)=tmp; clear tmp;
     50: % the solution x does not change and can be reused. Check:
     51: if max(abs(b-A*x))  ~= 0
     52:  error('PETSc:generatePetscTestFiles:WrongAns',...
     53:  '%s','The nonsymmetric matrix A is wrong');
     54: end

     56: % We need to again write all possible formats
     57: PetscBinaryWrite('ns-real-int32-float64',A,b,'indices','int32','precision','float64');
     58: [At,bt] = PetscBinaryRead('ns-real-int32-float64','indices','int32','precision','float64');
     59: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     60:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     61:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for ns-real-int32-float64');
     62: end
     63: PetscBinaryWrite('ns-real-int64-float64',A,b,'indices','int64','precision','float64');
     64: [At,bt] = PetscBinaryRead('ns-real-int64-float64','indices','int64','precision','float64');
     65: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     66:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     67:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for ns-real-int64-float64');
     68: end
     69: PetscBinaryWrite('ns-real-int32-float32',A,b,'indices','int32','precision','float32');
     70: [At,bt] = PetscBinaryRead('ns-real-int32-float32','indices','int32','precision','float32');
     71: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     72:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     73:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for ns-real-int32-float32');
     74: end
     75: PetscBinaryWrite('ns-real-int64-float32',A,b,'indices','int64','precision','float32');
     76: [At,bt] = PetscBinaryRead('ns-real-int64-float32','indices','int64','precision','float32');
     77: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     78:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     79:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for ns-real-int64-float32');
     80: end

     82: % Finally, we make A, b, and x complex, still integers
     83: A(1,1)=A(1,1)+1i; A(2,1)=A(2,1)-1i;
     84: x=1i.*ones(n,1); b=A*x;  % use VecSet(x,PETSC_i) in PETSc for x

     86: % We need to again write all possible formats
     87: PetscBinaryWrite('nh-complex-int32-float64',A,b,'indices','int32','precision','float64');
     88: [At,bt] = PetscBinaryRead('nh-complex-int32-float64','indices','int32','precision','float64','complex',true);
     89: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     90:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     91:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for nh-complex-int32-float64');
     92: end
     93: PetscBinaryWrite('nh-complex-int64-float64',A,b,'indices','int64','precision','float64');
     94: [At,bt] = PetscBinaryRead('nh-complex-int64-float64','indices','int64','precision','float64','complex',true);
     95: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
     96:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
     97:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for nh-complex-int64-float64');
     98: end
     99: PetscBinaryWrite('nh-complex-int32-float32',A,b,'indices','int32','precision','float32');
    100: [At,bt] = PetscBinaryRead('nh-complex-int32-float32','indices','int32','precision','float32','complex',true);
    101: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
    102:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
    103:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for nh-complex-int32-float32');
    104: end
    105: PetscBinaryWrite('nh-complex-int64-float32',A,b,'indices','int64','precision','float32');
    106: [At,bt] = PetscBinaryRead('nh-complex-int64-float32','indices','int64','precision','float32','complex',true);
    107: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
    108:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
    109:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for nh-complex-int64-float32');
    110: end

    112: % Make A Hermitian, without changing b and x
    113: tmp=A(1,1); A(1,1)=A(1,2); A(1,2)=tmp; clear tmp;
    114: % the solution x does not change and can be reused. Check:
    115: if max(max(abs(b-A*x)),max(max(abs(A-A'))))  ~= 0
    116:  error('PETSc:generatePetscTestFiles:WrongAns',...
    117:  '%s','The HPD matrix A is wrong');
    118: end

    120: % We need to again write all possible formats
    121: PetscBinaryWrite('hpd-complex-int32-float64',A,b,'indices','int32','precision','float64');
    122: [At,bt] = PetscBinaryRead('hpd-complex-int32-float64','indices','int32','precision','float64','complex',true);
    123: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
    124:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
    125:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for hpd-complex-int32-float64');
    126: end
    127: PetscBinaryWrite('hpd-complex-int64-float64',A,b,'indices','int64','precision','float64');
    128: [At,bt] = PetscBinaryRead('hpd-complex-int64-float64','indices','int64','precision','float64','complex',true);
    129: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
    130:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
    131:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for hpd-complex-int64-float64');
    132: end
    133: PetscBinaryWrite('hpd-complex-int32-float32',A,b,'indices','int32','precision','float32');
    134: [At,bt] = PetscBinaryRead('hpd-complex-int32-float32','indices','int32','precision','float32','complex',true);
    135: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
    136:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
    137:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for hpd-complex-int32-float32');
    138: end
    139: PetscBinaryWrite('hpd-complex-int64-float32',A,b,'indices','int64','precision','float32');
    140: [At,bt] = PetscBinaryRead('hpd-complex-int64-float32','indices','int64','precision','float32','complex',true);
    141: if max(max(max(abs(At-A))),max(abs(bt-b))) ~= 0
    142:  error('PETSc:generatePetscTestFiles:IncompatibleIO',...
    143:  '%s','Error in PetscBinaryWrite or/and PetscBinaryRead for hpd-complex-int64-float32');
    144: end