-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_dcdm.py
204 lines (178 loc) · 8.51 KB
/
test_dcdm.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os, sys, time
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
sys.path.append('lib')
from lib.read_data import *
import struct
import numpy as np
import scipy.sparse as spa
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow as tf
from tensorflow.sparse import sparse_dense_matmul as sp_mult
def get_predefined_model(N, name_model):
if name_model == "from64":
fil_num=16
input_rhs = keras.Input(shape=(dim, dim, dim, 1))
first_layer = layers.Conv3D(fil_num, (3, 3, 3), activation='linear', padding='same')(input_rhs)
la = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(first_layer)
lb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(la)
la = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(lb) + la
lb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(la)
apa = layers.AveragePooling3D((2, 2,2), padding='same')(lb)
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
upa = layers.UpSampling3D((2, 2,2))(apa) + lb
upb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upa)
upa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upb) + upa
upb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upa)
upa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upb) + upa
last_layer = layers.Dense(1, activation='linear')(upa)
model = keras.Model(input_rhs, last_layer)
return model
elif name_model == "from128":
fil_num=16
input_rhs = keras.Input(shape=(N, N, N, 1))
first_layer = layers.Conv3D(fil_num, (3, 3, 3), activation='linear', padding='same')(input_rhs)
la = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(first_layer)
lb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(la)
la = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(lb) + la
lb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(la)
la = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(lb) + la
lb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(la)
apa = layers.AveragePooling3D((2, 2,2), padding='same')(lb) #7
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
apb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apa)
apa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(apb) + apa
upa = layers.UpSampling3D((2, 2,2))(apa) + lb
upb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upa)
upa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upb) + upa
upb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upa)
upa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upb) + upa
upb = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upa)
upa = layers.Conv3D(fil_num, (3, 3, 3), activation='relu', padding='same')(upb) + upa
last_layer = layers.Dense(1, activation='linear')(upa)
model = keras.Model(input_rhs, last_layer)
return model
def dcdm(A, b, x_init, model_predict, max_it=100, tol=1e-10, verbose=True):
dim2 =len(b)
start_time = time.time()
res_arr = []
time_arr = []
p0 = tf.zeros_like(b)
p1 = tf.zeros_like(b)
Ap0 = tf.zeros_like(b)
Ap1 = tf.zeros_like(b)
alpha0 = 1.0
alpha1 = 1.0
r = b - tf.reshape(sp_mult(A, tf.reshape(x_init, (dim2, 1))), [-1])
norm_r = tf.norm(r).numpy()
res_arr = [norm_r]
tol = norm_r*tol
if verbose:
print("Initial residual =",norm_r)
if norm_r < tol:
print("DCDM converged in 0 iterations to residual ",norm_r)
return x_init, res_arr
x_sol = x_init
time_arr = [time.time()-start_time]
for i in range(max_it):
r_normalized = r / norm_r
q = model_predict(r_normalized)
# q = r_normalized
q = q - p1 * tf.tensordot(q, Ap1, 1) / alpha1 - p0 * tf.tensordot(q, Ap0, 1) / alpha0
Ap0 = Ap1
Ap1 = tf.reshape(sp_mult(A, tf.reshape(q, (dim2, 1))), [-1])
p0 = p1
p1 = q
alpha0 = alpha1
alpha1 = tf.tensordot(p1, Ap1, 1)
beta = tf.tensordot(p1, r, 1)
x_sol = x_sol + p1 * beta/alpha1
r = b - tf.reshape(sp_mult(A, tf.reshape(x_sol, (dim2, 1))), [-1])
norm_r = tf.norm(r).numpy()
res_arr = res_arr + [norm_r]
time_arr = time_arr + [time.time()-start_time]
if verbose:
print(i+1, norm_r)
if norm_r < tol:
print("DCDM converged in ", i+1, " iterations to residual ", norm_r)
for i in reversed(range(len(time_arr))):
time_arr[i] -= time_arr[0]
return x_sol, res_arr, time_arr
print("DCDM converged in ", max_it, "(maximum iteration) iterations to residual ",norm_r)
for i in reversed(range(len(time_arr))):
time_arr[i] -= time_arr[0]
return x_sol, res_arr, time_arr
gpus = tf.config.list_physical_devices('GPU')
print(gpus)
N = 128
N2 = N**3
dim = N+2
dim2 = dim**3
frames = [45]
max_it = 100
tol = 1e-6
# scene = f"waterflow_ball_N{N}_200_3D"
# scene = f"smoke_solid_N{N}_200_3D"
scene = "mantaflow"
data_dir = f"data/{scene}"
model_dir = f"../dataset_mlpcg/trained_models/model_N{N}_from128_F32"
json_file = open(model_dir + '/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = get_predefined_model(dim, 'from128')
model.load_weights(model_dir + "/model.h5")
def model_predict(r):
if compressed:
rr = np.zeros(N2)
rr[fluid_cells] = r.numpy()
rr = tf.convert_to_tensor(rr, tf.float32)
else:
rr = tf.cast(r, tf.float32)
r = tf.reshape(rr, [1, N,N,N])
r = tf.pad(r, [[0,0],[1,1], [1,1], [1,1]])
x = model(r,training=False)
# x = r
x = x[0, 1:-1, 1:-1, 1:-1]
x = tf.reshape(x, [-1])
if compressed:
x = tf.convert_to_tensor(x.numpy()[fluid_cells], tf.float64)
else:
x = tf.cast(x, tf.float64)
return x
output_file = f"tests/{scene}.txt"
for frame in frames:
A = spa.load_npz(f"{data_dir}/A_{frame}.npz")
b = np.load(f"{data_dir}/div_v_star_{frame}.npy")
flags = np.load(f"{data_dir}/flags_{frame}.npy")
flags = np.where(flags==2, SOLID, FLUID)
fluid_cells = np.argwhere(flags == FLUID).ravel()
# b = load_vector(f"{data_dir}/div_v_star_{frame}.bin")
# A = readA_sparse(f"{data_dir}/A_{frame}.bin",'d', shape=None)
# flags = read_flags(f"{data_dir}/flags_{frame}.bin")
# fluid_cells = np.where(flags==FLUID)[0]
# compressed = len(b) < N2
compressed = False
b = tf.convert_to_tensor(b, dtype=tf.float64)
b /= tf.norm(b)
A = A.tocoo().astype(np.float64)
A = tf.sparse.SparseTensor(np.array([A.row, A.col]).T, A.data, A.shape)
x_sol, res_arr, time_arr = dcdm(A, b, tf.zeros_like(b), model_predict, max_it,tol, False)
t0=time.time()
x_sol, res_arr, time_arr = dcdm(A, b, tf.zeros_like(b), model_predict, 600,tol, True)
time_dcdm = time.time() - t0
print("DCDM took ",time_dcdm, " secs")
with open(output_file, 'w') as f:
for res, time in zip(res_arr, time_arr):
f.write(f"{res:^8.4}, {time:>8.4}\n")