dafoam_vecdiff.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 Compare values in two different vectors and identify the max relative/absolute error
4 """
5 import os, sys
6 import argparse
7 import petsc4py
8 
9 petsc4py.init(sys.argv)
10 from petsc4py import PETSc
11 
12 
13 def evalVecDiff(vecName1, vecName2, mode, diffTol=1e-30):
14 
15  # the relative error is computed as
16  # (vec1-vec2)/vec1
17 
18  # read vec 1
19  vec1 = PETSc.Vec().create(comm=PETSc.COMM_WORLD)
20  viewer = PETSc.Viewer().createBinary(vecName1, comm=PETSc.COMM_WORLD)
21  vec1.load(viewer)
22 
23  # read vec 2
24  vec2 = PETSc.Vec().create(comm=PETSc.COMM_WORLD)
25  viewer = PETSc.Viewer().createBinary(vecName2, comm=PETSc.COMM_WORLD)
26  vec2.load(viewer)
27 
28  # read vec Diff
29  vecDiff = PETSc.Vec().create(comm=PETSc.COMM_WORLD)
30  viewer = PETSc.Viewer().createBinary(vecName1, comm=PETSc.COMM_WORLD)
31  vecDiff.load(viewer)
32 
33  Istart, Iend = vec1.getOwnershipRange()
34 
35  vecDiff.axpy(-1.0, vec2)
36 
37  maxDiff = -1.0e30
38  maxVal1 = -1.0e30
39  maxVal2 = -1.0e30
40  maxRowI = -1.0e30
41  l2norm = 0.0
42  foundDiff = 0
43  for i in range(Istart, Iend):
44  valDiff = abs(vecDiff[i])
45  valRef = abs(vec1[i])
46  if mode == "rel":
47  valError = valDiff / (valRef + diffTol)
48  elif mode == "abs":
49  valError = valDiff
50  else:
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:
55  maxDiff = valError
56  maxRowI = i
57  maxVal1 = vec1.getValue(i)
58  maxVal2 = vec2.getValue(i)
59  foundDiff = 1
60 
61  if foundDiff == 1:
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)
67  return maxDiff
68  else:
69  print("Two vectors are exactly same with tolerance: %e" % diffTol)
70  return 0.0, 0.0
71 
72 
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")
76  evalVecDiff(sys.argv[1], sys.argv[2], sys.argv[3])
dafoam_vecdiff.evalVecDiff
def evalVecDiff(vecName1, vecName2, mode, diffTol=1e-30)
Definition: dafoam_vecdiff.py:13