3 Compare values in two different Petsc matrices and identify the max relative/absolute error
10 petsc4py.init(sys.argv)
11 from petsc4py
import PETSc
19 jacMat1 = PETSc.Mat().create(PETSc.COMM_WORLD)
20 viewer = PETSc.Viewer().createBinary(mat1, comm=PETSc.COMM_WORLD)
24 jacMat2 = PETSc.Mat().create(PETSc.COMM_WORLD)
25 viewer = PETSc.Viewer().createBinary(mat2, comm=PETSc.COMM_WORLD)
28 Istart, Iend = jacMat1.getOwnershipRange()
31 jacMatDiff = PETSc.Mat().create(PETSc.COMM_WORLD)
32 viewer = PETSc.Viewer().createBinary(mat1, comm=PETSc.COMM_WORLD)
33 jacMatDiff.load(viewer)
34 jacMatDiff.axpy(-1.0, jacMat2, structure=jacMatDiff.Structure.DIFFERENT_NONZERO_PATTERN)
43 for i
in range(Istart, Iend):
44 rowVals = jacMatDiff.getRow(i)
45 nCols = len(rowVals[0])
46 for j
in range(nCols):
48 valDiff = abs(rowVals[1][j])
49 valRef = abs(jacMat1[i, colI])
51 valError = valDiff / (valRef + diffTol)
55 print(
"mode not supported! Options are: abs or rel")
56 l2norm = l2norm + valDiff ** 2
57 if abs(valError) > diffTol:
58 if abs(valError) > maxDiff:
63 maxVal1 = jacMat1.getValue(i, colI)
64 maxVal2 = jacMat2.getValue(i, colI)
67 print(
"L2Norm: %20.16e" % l2norm)
68 print(
"MaxDiff: %20.16e" % maxDiff)
69 print(
"MaxVal1: %20.16e" % maxVal1)
70 print(
"MaxVal2: %20.16e" % maxVal2)
71 print(
"MaxrowI: %d" % maxRowI)
72 print(
"MaxcolI: %d" % maxColI)
75 print(
"Two matrices are exactly same with tolerance: %e" % diffTol)
79 if __name__ ==
"__main__":
80 print(
"\nUsage: python dafoam_matreldiff.py matName1 matName2 mode")
81 print(
"Example python dafoam_matreldiff.py dRdWT1.bin dRdWT2.bin abs\n")