dafoam_matgetvalues.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 Read a PETSc matrix and print the value(s) at given row and column(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 printMatValues(mat, rowI, colI, transposed, diffTol = 1e-30):
14 
15  # read the Jac mat
16  jacMat = PETSc.Mat().create(PETSc.COMM_WORLD)
17  viewer = PETSc.Viewer().createBinary(mat, comm=PETSc.COMM_WORLD)
18  jacMat.load(viewer)
19 
20  if transposed == "1":
21  print("Getting transposed values")
22  jacMat.transpose()
23  elif transposed == "0":
24  pass
25  else:
26  print("Error!!!transposed should be either 0 or 1")
27  exit()
28 
29  rowVals = jacMat.getRow(int(rowI))
30  nCols = len(rowVals[0])
31 
32  for i in range(nCols):
33  if (int(colI) == -1) or (int(rowVals[0][i]) == int(colI)):
34  if abs(rowVals[1][i]) > diffTol:
35  print("%16d %20.16e" % (rowVals[0][i], rowVals[1][i]))
36 
37 
38 if __name__ == "__main__":
39  print("\nUsage: python dafoam_matgetvalues.py matName rowI colI transposed")
40  print("Example python dafoam_matgetvalues.py dRdWT.bin 100 25 0")
41  print("NOTE: if colI=-1, print all columns\n")
42  printMatValues(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
dafoam_matgetvalues.printMatValues
def printMatValues(mat, rowI, colI, transposed, diffTol=1e-30)
Definition: dafoam_matgetvalues.py:13