-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidationScript.py
164 lines (139 loc) · 5.19 KB
/
ValidationScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import numpy as np
import csv
import sys
'''
Validation considering following problem parameters:
%%%% KVALID , MVALID, SVALID %%%%
Domain = 2x2 km
Mesh = 50x50 elements
T = 2.6s
dt = 0.002s
I = 1.0
freq = 2Hz
vel = 1.0km/s (only background)
Single shot = (x=1.5, y=1.5)
No absorbing boundaries
%%%% KVALID2 , MVALID2, SVALID2 %%%% TODO
Domain = 2x2 km
Mesh = 80x80 elements
T = 2.6s
dt = 0.002s
I = 1.0
freq = 2Hz
vel1 = 1.0km/s
vel2 = 3.0km/s (single centered square 0.3x0.3 km)
Four shots = (x=0.0, y=0.0), (x=2.0, y=0.0), (x=0.0, y=2.0), (x=2.0, y=2.0)
No absorbing boundaries
'''
class Valid:
def __init__(self):
self.tol = 10**(-7)
def fill(self, nn, nsteps, nshots):
self.nn = int(nn)
self.nsteps = int(nsteps)
self.nshots = int(nshots)
self.raw_stiffness = np.zeros((self.nn*self.nn))
self.raw_mass = np.zeros((self.nn*self.nn))
self.raw_solution = np.zeros((self.nn*self.nsteps*self.nshots))
def update(self):
self.solution = np.zeros((self.nn,self.nsteps,self.nshots))
index = 0
for s in range(self.nshots):
for t in range(self.nsteps):
for n in range(self.nn):
self.solution[n,t,s] = self.raw_solution[index]
index += 1
self.stiffness = np.zeros((self.nn,self.nn))
index = 0
for j in range(self.nn):
for i in range(self.nn):
self.stiffness[i,j] = self.raw_stiffness[index]
index += 1
self.mass = np.zeros((self.nn,self.nn))
index = 0
for j in range(self.nn):
for i in range(self.nn):
self.mass[i,j] = self.raw_mass[index]
index += 1
def run(self, kvalid: str, mvalid: str, svalid: str):
self.val_stiffness(example=kvalid)
self.val_mass(example=mvalid)
self.val_solution(example=svalid)
def val_stiffness(self, example: str):
with open(f'./validationData/{example}.npy', 'rb') as f:
kvalid = np.load(f,allow_pickle=True)
result = Valid.least_squares(kvalid, self.stiffness)
if result < self.tol:
print("The stiffness matrix was validated with success.\n")
else:
print(f"The stiffness matrix is not correct. Error value: {result}\n")
return
def val_mass(self, example: str):
with open(f'./validationData/{example}.npy', 'rb') as f:
mvalid = np.load(f,allow_pickle=True)
result = Valid.least_squares(mvalid, self.mass)
if result < self.tol:
print("The mass matrix was validated with success.\n")
else:
print(f"The mass matrix is not correct. Error value: {result}\n")
return
def val_solution(self, example: str):
with open(f'./validationData/{example}.npy', 'rb') as f:
svalid = np.load(f,allow_pickle=True)
result = Valid.least_squares(svalid, self.solution, time=True)
if result < self.tol*100*self.nn*self.nshots*self.nsteps:
print("The solution was validated with success.")
else:
print(f"The solution is not correct. Error value: {result}")
return
@staticmethod
def least_squares(matA, matB, time=False):
value = 0
if time is False:
for j in range(matA.shape[1]):
for i in range(matA.shape[0]):
value += (matA[i,j]-matB[i,j])**2
return value
if time is True:
for k in range(matA.shape[2]):
for j in range(matA.shape[1]):
for i in range(matA.shape[0]):
value += (matA[i,j,k]-matB[i,j,k])**2
return value
@staticmethod
def parse_csv_file(file_path, model):
'''
Index:
Row 0 - Problem configuration
Row 1 - Solution field (FULL)
Row 2 - Stiffness matrix
Row 3 - Mass matrix
'''
row_counter = 0
with open(file_path, 'r') as file:
reader = csv.reader(file)
for row in reader:
if row_counter == 0:
model.fill(row[0],row[8],row[10])
if row_counter == 1:
for i in range(model.nshots*model.nsteps*model.nn):
model.raw_solution[i] = float(row[i])
if row_counter == 2:
for i in range(model.nn*model.nn):
model.raw_stiffness[i] = float(row[i])
if row_counter == 3:
for i in range(model.nn*model.nn):
model.raw_mass[i] = float(row[i])
else:
pass
row_counter += 1
model.update()
def main():
print("\nVALIDATING DATA\n ... \n")
valid = Valid()
csv.field_size_limit(sys.maxsize)
Valid.parse_csv_file(file_path="./Output/valid.csv", model=valid)
valid.run("kvalid","mvalid","svalid")
print("\n ... \n\nDone. ")
if __name__ == "__main__":
main()