-
Notifications
You must be signed in to change notification settings - Fork 0
/
KCNODE.py
227 lines (164 loc) · 8.14 KB
/
KCNODE.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from torchdyn.core import NeuralODE
import torch
class BaselineNN(torch.nn.Module):
def __init__(self, n_hidden=8):
super(BaselineNN, self).__init__()
self.layer_1 = torch.nn.Linear(6, n_hidden)
self.layer_3 = torch.nn.Linear(n_hidden, n_hidden)
self.layer_out = torch.nn.Linear(n_hidden, 5)
def forward(self, x):
molar_flows = x[:, :-2]
temperature = x[:, -2:-1]
total_pressure = x[:, -1:]
partial_pressure = molar_flows[:, :-1] / molar_flows.sum(axis=1, keepdim=True) * total_pressure
x = torch.tanh(self.layer_1(torch.hstack([partial_pressure, temperature])))
x = torch.exp(self.layer_3(x))
x = self.layer_out(x)
return torch.hstack([x, torch.zeros_like(x[:, :3])])
class KineticConstrainedNN(torch.nn.Module):
def __init__(self, n_hidden=8):
super(KineticConstrainedNN, self).__init__()
self.layer_1 = torch.nn.Linear(5, n_hidden)
self.layer_T = torch.nn.Linear(1, 2)
self.layer_out = torch.nn.Linear(n_hidden, 2)
self.coef_ = torch.tensor([[-1, 1, 0], # the stoichiometric matrix
[-1, 1,-3],
[ 1,-1,-1],
[ 1,-1, 1],
[ 0, 0, 1],
[ 0, 0, 0]]).T.float()
def forward(self, x):
molar_flows = x[:, :-2]
temperature = x[:, -2:-1]
total_pressure = x[:, -1:]
partial_pressure = molar_flows[:, :-1] / molar_flows.sum(axis=1, keepdim=True) * total_pressure
p_CO2 = partial_pressure[:, 0:1]
p_H2 = partial_pressure[:, 1:2]
p_CO = partial_pressure[:, 2:3]
p_H2O = partial_pressure[:, 3:4]
x = torch.tanh(self.layer_1(partial_pressure))
x = torch.sigmoid(self.layer_out(x))
xt = torch.exp(10*self.layer_T(temperature))
T = 1 / (temperature*8.31/10000 + 1 / (273.15+300))
K_eq = torch.exp(3.933 - 4076/(T - 39.64))
xt = torch.hstack([xt[:, :1], xt[:, :1] / K_eq, xt[:, 1:]])
x1 = p_CO2 * p_H2 * x[:, 0:1]
x2 = p_CO * p_H2O * x[:, 0:1]
x3 = p_CO * p_H2 * x[:, 1:2]
x = torch.hstack([x1, x2, x3])
x = x * xt
x = torch.linalg.multi_dot([x, self.coef_])
return torch.hstack([x, torch.zeros_like(x[:, :2])])
class MyLinear(torch.nn.Linear):
'''
To make parameters related to activation energy be positive
'''
def __init__(self, in_features, out_features):
super().__init__(in_features, out_features)
def forward(self, input):
return torch.nn.functional.linear(input, -torch.abs(self.weight), self.bias)
class KineticConstrainedFTNN(torch.nn.Module):
def __init__(self, n_hidden=8):
super(KineticConstrainedFTNN, self).__init__()
self.layer_1 = torch.nn.Linear(6, n_hidden)
self.layer_T = MyLinear(1, 5)
self.layer_out = torch.nn.Linear(n_hidden, 12)
self.amount_an = 15
self.amount_en = self.amount_an - 1
self.amount_oh = 7
self.create_matrix()
def create_matrix(self):
self.coef_ = torch.tensor([[ 0, 0, 0], # N2
[-1, 1,-1], # CO2
[ 1,-1, 0], # CO
[-1, 1,-4], # H2
[ 1,-1, 2], # H2O
])
self.coef_ = torch.vstack([self.coef_, torch.zeros([self.amount_an + self.amount_en + self.amount_oh, 3])])
self.coef_[5, 2] = 1
M11 = torch.zeros((5, self.amount_an))
M11[2] = -1 # CO
M11[3] = -(2 + 1 / torch.arange(1, self.amount_an+1)) # H2
M11[4] = +1 # H2O
M12 = torch.zeros((5, self.amount_en))
M12[2] = -1 # CO
M12[3] = -2 # H2
M12[4] = +1 # H2O
M13 = torch.zeros((5, self.amount_oh))
M13[2] = -1 # CO
M13[3] = -2 # H2
M13[4] = +(1 - 1 / torch.arange(1, self.amount_oh+1)) # H2O
M1 = torch.hstack([M11, M12, M13])
M21 = torch.eye(self.amount_an) * 1 / torch.arange(1, self.amount_an+1)
M22 = torch.zeros((self.amount_an, self.amount_en))
M23 = torch.zeros((self.amount_an, self.amount_oh))
M2 = torch.hstack([M21, M22, M23])
M31 = torch.zeros((self.amount_en, self.amount_an))
M32 = torch.eye(self.amount_en) * 1 / torch.arange(2, self.amount_en+2)
M33 = torch.zeros((self.amount_en, self.amount_oh))
M3 = torch.hstack([M31, M32, M33])
M41 = torch.zeros((self.amount_oh, self.amount_an))
M42 = torch.zeros((self.amount_oh, self.amount_en))
M43 = torch.eye(self.amount_oh) * 1 / torch.arange(1, self.amount_oh+1)
M4 = torch.hstack([M41, M42, M43])
M = torch.vstack([M1, M2, M3, M4])
self.coef_ = torch.hstack([self.coef_, M]).T.float()
def forward(self, x):
molar_flows = x[:, 3:]
total_pressure = x[:, 0:1]
temperature = x[:, 1:2]
tos = x[:, 2:3]
partial_pressure = molar_flows[:, 1:5] / molar_flows.sum(axis=1, keepdim=True) * total_pressure
p_CO2 = partial_pressure[:, 0:1]
p_CO = partial_pressure[:, 1:2]
p_H2 = partial_pressure[:, 2:3]
p_H2O = partial_pressure[:, 3:4]
x = torch.hstack([temperature, tos, partial_pressure])
x = torch.tanh(self.layer_1(x))
x = torch.sigmoid(self.layer_out(x))
alpha1 = x[:, 5:6]
alpha2 = x[:, 6:7]
fraction = x[:, 7:8]
alpha_en = x[:, 8:9]
frac_en2 = x[:, 9:10]
alpha_oh = x[:, 10:11]
frac_oh1 = x[:, 11:12]
xt = torch.exp(self.layer_T(temperature))
T = 1 / (temperature*8.31/10000 + 1 / (273.15+280))
K_eq = torch.exp(3.933 - 4076/(T - 39.64))
xt = torch.hstack([xt[:, 0:1], xt[:, 0:1] / K_eq, xt[:, 1:5]])
rwgs_f = p_CO2 * p_H2 * x[:, 0:1]
rwgs_b = p_CO * p_H2O * x[:, 0:1]
methanation = p_CO2 * p_H2 * x[:, 1:2]
ft_an = p_CO * p_H2 * x[:, 2:3]
ft_en = p_CO * p_H2 * x[:, 3:4]
ft_oh = p_CO * p_H2 * x[:, 4:5]
x = torch.hstack([rwgs_f, rwgs_b, methanation, ft_an, ft_en, ft_oh])
rates = x * xt
rate_ft_an = rates[:, -3:-2]
rate_ft_en = rates[:, -2:-1]
rate_ft_oh = rates[:, -1:]
carbon_number = torch.arange(1, self.amount_an + 1)
rate_an = fraction * carbon_number * rate_ft_an * torch.pow(alpha1, carbon_number - 1) + (1-fraction) * carbon_number * rate_ft_an * torch.pow(alpha2, carbon_number - 1)
carbon_number = torch.arange(2, self.amount_en + 2)
rate_en = carbon_number * rate_ft_en * torch.pow(alpha_en, carbon_number - 1)
rate_en[:, 0:1] *= frac_en2
carbon_number = torch.arange(1, self.amount_oh + 1)
rate_oh = carbon_number * rate_ft_oh * torch.pow(alpha_oh, carbon_number - 1)
rate_oh[:, 0:1] *= frac_oh1
x = torch.hstack([rates[:, :3], rate_an, rate_en, rate_oh])
x = torch.linalg.multi_dot([x, self.coef_])
out = torch.hstack([torch.zeros_like(x[:, :3]), x])
return out
def get_baseline_model(solver='dopri5'):
model_nn = BaselineNN()
return NeuralODE(model_nn, sensitivity='autograd',
solver=solver, order=1, return_t_eval=False)
def get_KCNODE_methanation_model(solver='dopri5'):
model_nn = KineticConstrainedNN()
return NeuralODE(model_nn, sensitivity='autograd',
solver=solver, order=1, return_t_eval=False)
def get_KCNODE_FT_model(solver='dopri5'):
model_nn = KineticConstrainedFTNN()
return NeuralODE(model_nn, sensitivity='autograd',
solver=solver, order=1, return_t_eval=False)