Peano
Loading...
Searching...
No Matches
petscReader.py
Go to the documentation of this file.
1#read an xml file containing a petsc object
2import numpy as np
3
4def getMatDimensions(fName):
5 output = 0
6 with open(fName) as f:
7 for _ in range(3): #skip the first two lines
8 l = f.readline()
9 while l:
10 output += 1
11 l = f.readline()
12 return output
13
14#advances an index
15def readUntil(line, character, index=0):
16 # print(line)
17 output = line[index:].index(character)
18 return output + index
19
20#get the pair (col, entry), chop the line
21#and then return all three
23 # print(line)
24 colNumber = line[ readUntil(line,'(') + 1: readUntil(line,',') ]
25 #convert to float
26 colNumber = float(colNumber)
27 # print(colNumber)
28 #chop
29
30 val = line[ readUntil(line, ',')+1 : readUntil(line, ')') ]
31 val = float(val)
32
33 #chop the line
34 line = line[ readUntil(line, ')') + 1: ]
35 # print(line)
36
37 return int(colNumber), val, line
38
39def convertLineToVector(line, dim):
40 #chop off first part
41 line = line[readUntil(line,'('):]
42
43 output = np.zeros((dim))
44 while ')' in line:
45 col, val, line = getFirstColNumberAndEntryFromLine(line)
46 output[col] = val
47
48 return output
49
50
51def populateMatrix(fName):
52 dim = getMatDimensions(fName)
53 output = np.zeros((dim,dim))
54 with open(fName) as f:
55 for _ in range(3): #skip the first two lines
56 l = f.readline()
57
58 row = 0
59 while l:
60 if l == "\n":
61 break
62 output[row,:] = convertLineToVector(l, dim)
63 row += 1
64 l = f.readline() # read the line
65 return output
66
67def populateRHS(fName, solDim=None):
68 dim = solDim if solDim else getMatDimensions(fName)
69 vec = np.zeros((dim))
70 with open(fName) as f:
71 for _ in range(3): #skip the first two lines
72 l = f.readline()
73
74 row = 0
75 while l and row < dim:
76 if l == '\n':
77 break
78 vec[row] = float(l)
79 row += 1
80 l=f.readline()
81 return vec
82
83
85 return np.linalg.matrix_rank(mat)
86
87if __name__ == "__main__":
88
89 #hardcoding file names!
90
91 dim = getMatDimensions("mat.xml")
92
93 try:
94 mat = populateMatrix("mat.xml")
95 except:
96 print("no mat.xml file available")
97 pass
98 try:
99 rhs = populateRHS("rhs.xml")
100 except:
101 print("no rhs.xml available")
102 pass
103
104 try:
105 numCells = 27 * 27
106 solDim = 4 * numCells
107 # sol = populateRHS("solution.xml", solDim)
108 sol = populateRHS("solution.xml")
109 except:
110 print("didn't read in solution")
111 pass
112
113 print(f'shape of this matrix is {mat.shape}')
114 print("getting rank of this matrix...")
115
116 # print(f"rank of this matrix is {getRankOfMatrix(mat)}")
117
118 # print("getting eigenvalues and vectors")
119 # eigs, vecs = np.linalg.eig(mat)
120 # print(f'smallest eigenvalue is {eigs[np.argmin(np.abs(eigs))]}')
getMatDimensions(fName)
Definition petscReader.py:4
getRankOfMatrix(mat)
readUntil(line, character, index=0)
populateMatrix(fName)
convertLineToVector(line, dim)
getFirstColNumberAndEntryFromLine(line)