3 Compare values in two different vectors and identify the max relative/absolute error
9 petsc4py.init(sys.argv)
10 from petsc4py
import PETSc
19 vec1 = PETSc.Vec().create(comm=PETSc.COMM_WORLD)
20 viewer = PETSc.Viewer().createBinary(vecName1, comm=PETSc.COMM_WORLD)
24 vec2 = PETSc.Vec().create(comm=PETSc.COMM_WORLD)
25 viewer = PETSc.Viewer().createBinary(vecName2, comm=PETSc.COMM_WORLD)
29 vecDiff = PETSc.Vec().create(comm=PETSc.COMM_WORLD)
30 viewer = PETSc.Viewer().createBinary(vecName1, comm=PETSc.COMM_WORLD)
33 Istart, Iend = vec1.getOwnershipRange()
35 vecDiff.axpy(-1.0, vec2)
43 for i
in range(Istart, Iend):
44 valDiff = abs(vecDiff[i])
47 valError = valDiff / (valRef + diffTol)
51 print(
"mode not supported! Options are: abs or rel")
52 l2norm = l2norm + valDiff ** 2
53 if abs(valError) > diffTol:
54 if abs(valError) > maxDiff:
57 maxVal1 = vec1.getValue(i)
58 maxVal2 = vec2.getValue(i)
62 print(
"L2Norm: %20.16e" % l2norm)
63 print(
"MaxDiff: %20.16e" % maxDiff)
64 print(
"MaxVal1: %20.16e" % maxVal1)
65 print(
"MaxVal2: %20.16e" % maxVal2)
66 print(
"MaxrowI: %d" % maxRowI)
69 print(
"Two vectors are exactly same with tolerance: %e" % diffTol)
73 if __name__ ==
"__main__":
74 print(
"\nUsage: python dafoam_vecreldiff.py vecName1 vecName2 mode")
75 print(
"Example python dafoam_vecreldiff.py dFdW1.bin dFdW2.bin abs\n")