dafoam_vecgetvalues.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 Read a PETSc vector and print the value(s) at given row(s)
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 printVecValues(vecName, rowI, diffTol=1e-30):
14 
15  # read the vector
16  vec1 = PETSc.Vec().create(comm=PETSc.COMM_WORLD)
17  viewer = PETSc.Viewer().createBinary(vecName, comm=PETSc.COMM_WORLD)
18  vec1.load(viewer)
19 
20  rowI = int(rowI)
21 
22  vecSize = vec1.getSize()
23 
24  if rowI == -1:
25  for i in range(vecSize):
26  if abs(vec1.getValue(i)) > diffTol:
27  print("%12d %16.14e" % (i, vec1.getValue(i)))
28  else:
29  print("%12d %16.14e" % (rowI, vec1.getValue(rowI)))
30 
31 
32 if __name__ == "__main__":
33  print("\nUsage: python dafoam_vecgetvalues.py vecName rowI")
34  print("Example python dafoam_vecgetvalues.py dFdW.bin 100")
35  print("NOTE: if rowI=-1, print all elements\n")
36  printVecValues(sys.argv[1], sys.argv[2])
37 
dafoam_vecgetvalues.printVecValues
def printVecValues(vecName, rowI, diffTol=1e-30)
Definition: dafoam_vecgetvalues.py:13