-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_matrix.py
191 lines (141 loc) · 5.81 KB
/
state_matrix.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import numpy as np
from thermo_utils import *
import threading
import torch
def gen_state_matrix(mat_d, dt):
''' state translation: for b-state [A, B, C, D, E, F]
A: x + 1
B: x - 1
C: y + 1
D: y - 1
E: z + 1
F: z - 1
'''
X_GRID = mat_d.shape[0]
Y_GRID = mat_d.shape[1]
Z_GRID = mat_d.shape[2]
a_state = np.zeros((X_GRID, Y_GRID, Z_GRID))
b_state = np.zeros((X_GRID, Y_GRID, Z_GRID, 6))
for i in range(X_GRID):
for j in range(Y_GRID):
for k in range(Z_GRID):
curr_p = mat_d[i, j, k]
a_sum = 0
b_const = (dt/(curr_p[4] * curr_p[5]))
# ---------- Calculate X points ----------
A = curr_p[2] * curr_p[1] * 4 # dz * dy
if i + 1 < X_GRID: # Get point for (1, 0, 0)
target = mat_d[i+1, j, k]
a_sum += 1/((curr_p[0] + target[0]) * A)
b_state[i, j, k, 0] = b_const/((curr_p[0] + target[0]) * A)
if i - 1 >= 0: # Get point for (-1, 0, 0)
target = mat_d[i-1, j, k]
a_sum += 1/((curr_p[0] + target[0]) * A)
b_state[i, j, k, 1] = b_const/((curr_p[0] + target[0]) * A)
# ---------- Calculate Y points ----------
A = curr_p[2] * curr_p[0] * 4 # dz * dx
if j + 1 < Y_GRID: # Get point for (0, 1, 0)
target = mat_d[i, j+1, k]
a_sum += 1/((curr_p[1] + target[1]) * A)
b_state[i, j, k, 2] = b_const/((curr_p[1] + target[1]) * A)
if j - 1 >= 0: # Get point for (0, -1, 0)
target = mat_d[i, j-1, k]
a_sum += 1/((curr_p[1] + target[1]) * A)
b_state[i, j, k, 3] = b_const/((curr_p[1] + target[1]) * A)
# ---------- Calculate Z points ----------
A = curr_p[0] * curr_p[1] * 4 # dx * dy
if k + 1 < Z_GRID: # Get point for (0, 0, 1)
target = mat_d[i, j, k+1]
a_sum += 1/((curr_p[2] + target[2]) * A)
b_state[i, j, k, 4] = b_const/((curr_p[2] + target[2]) * A)
if k - 1 >= 0: # Get point for (0, 0, -1)
target = mat_d[i, j, k-1]
a_sum += 1/((curr_p[2] + target[2]) * A)
b_state[i, j, k, 5] = b_const/((curr_p[2] + target[2]))/A
a_state[i, j, k] = (1 - (dt/(curr_p[4] * curr_p[5])) * a_sum)
return a_state, b_state
def get_hstate_thermo(mat_d, dt):
X_GRID = mat_d.shape[0]
Y_GRID = mat_d.shape[1]
h_state = torch.zeros((X_GRID, Y_GRID))
mass = (8*mat_d[:, :, 0, 0]*mat_d[:, :, 0, 1]*mat_d[:, :, 0, 2])*mat_d[:,:,0,5]
h_state = dt/(mass * mat_d[:, :, 0, 4])
return h_state
def transition_state(mat_t, comp_mat, a_state, b_state, out):
X_GRID = mat_t.shape[0]
Y_GRID = mat_t.shape[1]
Z_GRID = mat_t.shape[2]
''' Previous way of calculating the comp_mat
'''
# ---------- Calculate X points ----------
comp_mat[:X_GRID-1, :, :, 0] = mat_t[1:, :, :]
comp_mat[1:, :, :, 1] = mat_t[:X_GRID-1, :, :]
# ---------- Calculate Y points ----------
comp_mat[:, :Y_GRID-1, :, 2] = mat_t[:, 1:, :]
comp_mat[:, 1:, :, 3] = mat_t[:, :Y_GRID-1, :]
# ---------- Calculate Z points ----------
comp_mat[:, :, :Z_GRID-1, 4] = mat_t[:, :, 1:]
comp_mat[:, :, 1:, 5] = mat_t[:, :, :Z_GRID-1]
''' Previous way(s) of getting the resut
'''
# sum_mat = np.sum(comp_mat*b_state, axis=3)
# return pyfma.fma(mat_t, a_state, sum_mat)
# return ne.evaluate('a*b+c', local_dict={'a': mat_t, 'b': a_state, 'c': np.sum(comp_mat*b_state, axis=3)})
out[:] = mat_t * a_state + torch.sum(comp_mat*b_state, axis=3)
# '''New approach to calculation uses multi-threaded approach to speed up calculations'''
# par_comp_mat(comp_mat, mat_t, func=eq)
# par_fmadd(mat_t, a_state, comp_mat, b_state, 10, out)
def set_mat_temps(mask, initial, new):
return torch.where(mask > 0, initial, new)
def fmadd(a, b, c, d, out):
out[:] = a*b+torch.sum(c*d, axis=3)
def eq(a, b):
a[:] = b[:]
def par_fmadd(a, b, c, d, numCores, out, func=fmadd):
threads = []
pl = a.shape[0]//numCores
for i in range(numCores - 1):
th = threading.Thread(target=func, args=(
a[i*pl:(i+1)*pl, :, :],
b[i*pl:(i+1)*pl, :, :],
c[i*pl:(i+1)*pl, :, :, :],
d[i*pl:(i+1)*pl, :, :, :],
out[i*pl:(i+1)*pl, :, :]))
th.start()
threads.append(th)
for th in threads:
th.join()
def par_comp_mat(comp_mat, mat_t, func=eq):
X_GRID = mat_t.shape[0]
Y_GRID = mat_t.shape[1]
Z_GRID = mat_t.shape[2]
threads = []
# ---------- Calculate X points ----------
th0 = threading.Thread(target=func, args=(
comp_mat[:X_GRID-1, :, :, 0], mat_t[1:, :, :] ))
th1 = threading.Thread(target=func, args=(
comp_mat[1:, :, :, 1], mat_t[:X_GRID-1, :, :]))
th0.start()
th1.start()
threads.append(th0)
threads.append(th1)
# ---------- Calculate X points ----------
th2 = threading.Thread(target=func, args=(
comp_mat[:, :Y_GRID-1, :, 2], mat_t[:, 1:, :]))
th3 = threading.Thread(target=func, args=(
comp_mat[:, 1:, :, 3], mat_t[:, :Y_GRID-1, :]))
th2.start()
th3.start()
threads.append(th2)
threads.append(th3)
# ---------- Calculate X points ----------
th4 = threading.Thread(target=func, args=(
comp_mat[:, :, :Z_GRID-1, 4], mat_t[:, :, 1:]))
th5 = threading.Thread(target=func, args=(
comp_mat[:, :, 1:, 5], mat_t[:, :, :Z_GRID-1]))
th4.start()
th5.start()
threads.append(th4)
threads.append(th5)
for th in threads:
th.join()