forked from deepflameCFD/deepflame-dev
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathinference.py
More file actions
191 lines (147 loc) · 5.77 KB
/
inference.py
File metadata and controls
191 lines (147 loc) · 5.77 KB
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
import torch
import numpy as np
import time
import os
import cantera as ct
device_main = "cuda:0"
device_list = [0] #range(torch.cuda.device_count())
torch.set_printoptions(precision=10)
class NN_MLP(torch.nn.Module):
def __init__(self, layer_info):
super(NN_MLP, self).__init__()
self.net = torch.nn.Sequential()
n = len(layer_info) - 1
for i in range(n - 1):
self.net.add_module('linear_layer_%d' %(i), torch.nn.Linear(layer_info[i], layer_info[i + 1]))
self.net.add_module('gelu_layer_%d' %(i), torch.nn.GELU())
#if i <= 2:
# self.net.add_module('batch_norm_%d' %(i), torch.nn.BatchNorm1d(layer_info[i + 1]))
self.net.add_module('linear_layer_%d' %(n - 1), torch.nn.Linear(layer_info[n - 1], layer_info[n]))
def forward(self, x):
return self.net(x)
try:
#load variables from constant/CanteraTorchProperties
path_r = r"./constant/CanteraTorchProperties"
with open(path_r, "r") as f:
data = f.read()
i = data.index('torchModel')
a = data.index('"',i)
b = data.index('"',a+1)
modelName = data[a+1:b]
i = data.index('frozenTemperature')
a = data.index(';', i)
b = data.rfind(' ',i+1,a)
frozenTemperature = float(data[b+1:a])
i = data.index('inferenceDeltaTime')
a = data.index(';', i)
b = data.rfind(' ',i+1,a)
delta_t = float(data[b+1:a])
i = data.index('CanteraMechanismFile')
a = data.index('"',i)
b = data.index('"',a+1)
mechanismName = data[a+1:b]
i = data.index('GPU')
a = data.index(';', i)
b = data.rfind(' ',i+1,a)
switch_GPU = data[b+1:a]
#read mechanism species number
gas = ct.Solution(mechanismName)
n_species = gas.n_species
#load OpenFOAM switch
switch_on = ["true", "True", "on", "yes", "y", "t", "any"]
switch_off = ["false", "False", "off", "no", "n", "f", "none"]
if switch_GPU in switch_on:
device = torch.device(device_main)
device_ids = device_list
elif switch_GPU in switch_off:
device = torch.device("cpu")
device_ids = [0]
else:
print("invalid setting!")
os._exit(0)
lamda = 0.1
dim = 9
state_dict = torch.load(modelName,map_location='cpu')
Xmu0 = state_dict['data_in_mean']
Xstd0 = state_dict['data_in_std']
Ymu0 = state_dict['data_target_mean']
Ystd0 = state_dict['data_target_std']
Xmu0 = torch.tensor(Xmu0).unsqueeze(0).to(device=device)
Xstd0 = torch.tensor(Xstd0).unsqueeze(0).to(device=device)
Ymu0 = torch.tensor(Ymu0).unsqueeze(0).to(device=device)
Ystd0 = torch.tensor(Ystd0).unsqueeze(0).to(device=device)
Xmu1 = Xmu0
Xstd1 = Xstd0
Ymu1 = Ymu0
Ystd1 = Ystd0
Xmu2 = Xmu0
Xstd2 = Xstd0
Ymu2 = Ymu0
Ystd2 = Ystd0
"""
#load model
layers = [n_species +2, 1600, 800, 400, 1]
model0list = []
for i in range(n_species-1):
model0list.append(NN_MLP(layers))
for i in range(n_species-1):
model0list[i].load_state_dict(state_dict[f'net{i}'])
for i in range(n_species-1):
model0list[i].eval()
model0list[i].to(device=device)
if len(device_ids) > 1:
for i in range(n_species-1):
model0list[i] = torch.nn.DataParallel(model0list[i], device_ids=device_ids)
"""
#load model
layers = [2+n_species]+[400]*4+[ n_species-1]
# layers = [2+n_species]+[800,400,200,100]+[n_species-1]
model = NN_MLP(layers)
model.load_state_dict(state_dict['net'])
model.eval()
model.to(device=device)
if len(device_ids) > 1:
model = torch.nn.DataParallel(model, device_ids=device_ids)
except Exception as e:
print(e.args)
def inference(vec0):
'''
use model to inference
'''
vec0 = np.abs(np.reshape(vec0, (-1, 3+n_species))) # T, P, Yi(7), Rho
vec0[:,1] *= 101325
# vec0[:,1] *= 0
# vec0[:,1] += 101325
mask = vec0[:,0] > frozenTemperature
vec0_input = vec0[mask, :]
print(f'real inference points number: {vec0_input.shape[0]}')
try:
with torch.no_grad():
input0_ = torch.from_numpy(vec0_input).double().to(device=device) #cast ndarray to torch tensor
# pre_processing
rho0 = input0_[:, -1].unsqueeze(1)
input0_Y = input0_[:, 2:-1].clone()
input0_bct = input0_[:, 0:-1]
input0_bct[:, 2:] = (input0_bct[:, 2:]**(lamda) - 1) / lamda #BCT
input0_normalized = (input0_bct - Xmu0) / Xstd0 #DimXmu0 = 9, DimXstd0 = 9, input0_bct =
# input0_normalized[:, -1] = 0 #set Y_AR to 0
input0_normalized = input0_normalized.float()
input0_normalized = input0_normalized.to(device=device)
#inference
output0_normalized = []
#for i in range(n_species-1):
# output0_normalized.append(model0list[i](input0_normalized))
#output0_normalized = torch.cat(output0_normalized, dim=1)
output0_normalized = model(input0_normalized)
# post_processing
output0_bct = output0_normalized * Ystd0 + Ymu0 + input0_bct[:, 2:-1]
output0_Y = input0_Y.clone()
output0_Y[:, :-1] = (lamda * output0_bct + 1)**(1 / lamda)
output0_Y[:, :-1] = output0_Y[:, :-1] / torch.sum(input=output0_Y[:, :-1], dim=1, keepdim=True) * (1 - output0_Y[:, -1:])
output0 = (output0_Y - input0_Y) * rho0 / delta_t
output0 = output0.cpu().numpy()
result = np.zeros((vec0.shape[0], n_species))
result[mask, :] = output0
return result
except Exception as e:
print(e.args)