-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulateFullModel.py
414 lines (351 loc) · 17.5 KB
/
simulateFullModel.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
############################################################################
#
# Imperial College London, United Kingdom
# Multifunctional Nanomaterials Laboratory
#
# Project: ERASE
# Year: 2021
# Python: Python 3.7
# Authors: Ashwin Kumar Rajagopalan (AK)
#
# Purpose:
# Simulates the sensor chamber as a CSTR incorporating kinetic effects
#
# Last modified:
# - 2021-03-20, AK: Bug fix for flow rate calculator
# - 2021-02-19, AK: Add relative tolerances for ode solver
# - 2021-02-03, AK: Add total volume and void fraction
# - 2021-02-02, AK: Add flow rate to output
# - 2021-01-30, AK: Add constant pressure model
# - 2021-01-27, AK: Add volSorbent and volGas to inputs
# - 2021-01-25, AK: Change the time interval definition
# - 2021-01-21, AK: Cosmetic changes
# - 2021-01-20, AK: Change to output time and plot function
# - 2021-01-20, AK: Cosmetic changes
# - 2021-01-19, AK: Initial creation
#
# Input arguments:
#
#
# Output arguments:
#
#
############################################################################
def simulateFullModel(**kwargs):
import numpy as np
from scipy.integrate import solve_ivp
from simulateSensorArray import simulateSensorArray
import auxiliaryFunctions
# Plot flag
plotFlag = False
# Get the commit ID of the current repository
gitCommitID = auxiliaryFunctions.getCommitID()
# Get the current date and time for saving purposes
currentDT = auxiliaryFunctions.getCurrentDateTime()
# Model flag (constant pressure or constant flow rate)
# Default is constant pressure
if 'modelConstF' in kwargs:
modelConstF = kwargs["modelConstF"]
else:
modelConstF = False
# Sensor ID
if 'sensorID' in kwargs:
sensorID = np.array([kwargs["sensorID"]])
else:
sensorID = np.array([6])
# Kinetic rate constants [/s]
if 'rateConstant' in kwargs:
rateConstant = np.array(kwargs["rateConstant"])
else:
rateConstant = np.array([.01,.01,.01])
# Feed flow rate [m3/s]
if 'flowIn' in kwargs:
flowIn = np.array(kwargs["flowIn"])
else:
flowIn = np.array([5e-7])
# Feed Mole Fraction [-]
if 'feedMoleFrac' in kwargs:
feedMoleFrac = np.array(kwargs["feedMoleFrac"])
else:
feedMoleFrac = np.array([1.,0.,0.])
# Initial Gas Mole Fraction [-]
if 'initMoleFrac' in kwargs:
initMoleFrac = np.array(kwargs["initMoleFrac"])
else:
# Equilibrium process
initMoleFrac = np.array([0.,0.,1.])
# Time span for integration [tuple with t0 and tf]
if 'timeInt' in kwargs:
timeInt = kwargs["timeInt"]
else:
timeInt = (0.0,2000)
# Time step for output printing
if 'timeStep' in kwargs:
timeStep = kwargs["timeStep"]
else:
timeStep = 5.
# Volume of sorbent material [m3]
if 'volSorbent' in kwargs:
volSorbent = kwargs["volSorbent"]
else:
volSorbent = 5e-7
# Volume of gas chamber (dead volume) [m3]
if 'volGas' in kwargs:
volGas = kwargs["volGas"]
else:
volGas = 5e-7
# Total volume of the system [m3]
if 'volTotal' in kwargs:
volTotal = kwargs["volTotal"]
if 'voidFrac' in kwargs:
voidFrac = kwargs["voidFrac"]
else:
raise Exception("You should provide void fraction if you provide total volume!")
volGas = voidFrac * volTotal # Volume of gas chamber (dead volume) [m3]
volSorbent = (1-voidFrac) * volTotal # Volume of solid sorbent [m3]
if (len(feedMoleFrac) != len(initMoleFrac)
or len(feedMoleFrac) != len(rateConstant)):
raise Exception("The dimensions of the mole fraction or rate constant and the number of gases in the adsorbent is not consistent!")
else:
numberOfGases = len(feedMoleFrac)
# Total pressure of the gas [Pa]
if 'pressureTotal' in kwargs:
pressureTotal = np.array(kwargs["pressureTotal"]);
else:
pressureTotal = np.array([1.e5]);
# Temperature of the gas [K]
# Can be a vector of temperatures
if 'temperature' in kwargs:
temperature = np.array(kwargs["temperature"]);
else:
temperature = np.array([298.15]);
# Compute the initial sensor loading [mol/m3] @ initMoleFrac
sensorLoadingPerGasVol, adsorbentDensity, molecularWeight = simulateSensorArray(sensorID, pressureTotal,
temperature, np.array([initMoleFrac]),
fullModel = True)
# Prepare tuple of input parameters for the ode solver
inputParameters = (sensorID, adsorbentDensity, rateConstant, numberOfGases, flowIn,
feedMoleFrac, initMoleFrac, pressureTotal, temperature, volSorbent, volGas)
# Solve the system of ordinary differential equations
# Stiff solver used for the problem: BDF or Radau
# The output is print out every 5 s
# Solves the model assuming constant flow rate at the inlet and outlet
if modelConstF:
# Prepare initial conditions vector
initialConditions = np.zeros([2*numberOfGases])
initialConditions[0:numberOfGases-1] = initMoleFrac[0:numberOfGases-1] # Gas mole fraction
initialConditions[numberOfGases-1:2*numberOfGases-1] = sensorLoadingPerGasVol # Initial Loading
initialConditions[2*numberOfGases-1] = pressureTotal # Outlet pressure the same as inlet pressure
outputSol = solve_ivp(solveSorptionEquationConstF, timeInt, initialConditions,
method='Radau', t_eval = np.arange(timeInt[0],timeInt[1],timeStep),
rtol = 1e-6, args = inputParameters)
# Flow out vector in output
flowOutVec = flowIn * np.ones(len(outputSol.t)) # Constant flow rate
# Parse out the output matrix and add flow rate
resultMat = np.row_stack((outputSol.y,flowOutVec))
# Solves the model assuming constant/negligible pressure across the sensor
else:
# Prepare initial conditions vector
initialConditions = np.zeros([2*numberOfGases-1])
initialConditions[0:numberOfGases-1] = initMoleFrac[0:numberOfGases-1] # Gas mole fraction
initialConditions[numberOfGases-1:2*numberOfGases-1] = sensorLoadingPerGasVol # Initial Loading
outputSol = solve_ivp(solveSorptionEquationConstP, timeInt, initialConditions,
method='Radau', t_eval = np.arange(timeInt[0],timeInt[1],timeStep),
rtol = 1e-6, args = inputParameters)
# Presure vector in output
pressureVec = pressureTotal * np.ones(len(outputSol.t)) # Constant pressure
# This is needed to make sure constant pressure model functions well
# If the time of integration and the time step is equal then the code
# will fail because of the flow rate determination step which requires
# a gradient - very first step flow out and in are considered to be the
# same if only one element present
if len(outputSol.t) == 1 :
flowOut = flowIn
else:
# Compute the outlet flow rate
dqdt = np.gradient(outputSol.y[numberOfGases-1:2*numberOfGases-1,:],
outputSol.t, axis=1) # Compute gradient of loading
sum_dqdt = np.sum(dqdt, axis=0) # Time resolved sum of gradient
flowOut = flowIn - ((volSorbent*(8.314*temperature)/pressureTotal)*(sum_dqdt))
# Parse out the output matrix and add flow rate
resultMat = np.row_stack((outputSol.y,pressureVec,flowOut))
# Parse out the time
timeSim = outputSol.t
# Compute the time resolved sensor response
sensorFingerPrint = np.zeros([len(timeSim)])
for ii in range(len(timeSim)):
loadingTemp = resultMat[numberOfGases-1:2*numberOfGases-1,ii]
sensorFingerPrint[ii] = np.dot(loadingTemp,molecularWeight)/adsorbentDensity
# Call the plotting function
if plotFlag:
plotFullModelResult(timeSim, resultMat, sensorFingerPrint, inputParameters,
gitCommitID, currentDT, modelConstF)
# Return time and the output matrix
return timeSim, resultMat, sensorFingerPrint, inputParameters
# func: solveSorptionEquationConstF - Constant flow rate model
# Solves the system of ODEs to evaluate the gas composition, loadings, and pressure
def solveSorptionEquationConstF(t, f, *inputParameters):
import numpy as np
from simulateSensorArray import simulateSensorArray
# Gas constant
Rg = 8.314; # [J/mol K]
# Unpack the tuple of input parameters used to solve equations
sensorID, _ , rateConstant, numberOfGases, flowIn, feedMoleFrac, _ , pressureTotal, temperature, volSorbent, volGas = inputParameters
# Initialize the derivatives to zero
df = np.zeros([2*numberOfGases])
# Compute the equilbirium loading at the current gas composition
currentGasComposition = np.concatenate((f[0:numberOfGases-1],
np.array([1.-np.sum(f[0:numberOfGases-1])])))
sensorLoadingPerGasVol, _ , _ = simulateSensorArray(sensorID, f[2*numberOfGases-1],
temperature, np.array([currentGasComposition]),
fullModel = True)
# Linear driving force model (derivative of solid phase loadings)
df[numberOfGases-1:2*numberOfGases-1] = np.multiply(rateConstant,(sensorLoadingPerGasVol-f[numberOfGases-1:2*numberOfGases-1]))
# Total mass balance
# Assumes constant flow rate, so pressure evalauted
term1 = 1/volGas
term2 = ((flowIn*pressureTotal) - (flowIn*f[2*numberOfGases-1])
- ((volSorbent*(Rg*temperature))*(np.sum(df[numberOfGases-1:2*numberOfGases-1]))))
df[2*numberOfGases-1] = term1*term2
# Component mass balance
term1 = 1/volGas
for ii in range(numberOfGases-1):
term2 = (flowIn*(pressureTotal*feedMoleFrac[ii] - f[2*numberOfGases-1]*f[ii])
- (volSorbent*(Rg*temperature))*df[ii+numberOfGases-1])
df[ii] = (term1*term2 - f[ii]*df[2*numberOfGases-1])/f[2*numberOfGases-1]
# Return the derivatives for the solver
return df
# func: solveSorptionEquationConstP - Constant pressure model
# Solves the system of ODEs to evaluate the gas composition and loadings
def solveSorptionEquationConstP(t, f, *inputParameters):
import numpy as np
from simulateSensorArray import simulateSensorArray
# Gas constant
Rg = 8.314; # [J/mol K]
# Unpack the tuple of input parameters used to solve equations
sensorID, _ , rateConstant, numberOfGases, flowIn, feedMoleFrac, _ , pressureTotal, temperature, volSorbent, volGas = inputParameters
# Initialize the derivatives to zero
df = np.zeros([2*numberOfGases-1])
# Compute the equilbirium loading at the current gas composition
currentGasComposition = np.concatenate((f[0:numberOfGases-1],
np.array([1.-np.sum(f[0:numberOfGases-1])])))
sensorLoadingPerGasVol, _ , _ = simulateSensorArray(sensorID, pressureTotal,
temperature, np.array([currentGasComposition]),
fullModel = True)
# Linear driving force model (derivative of solid phase loadings)
df[numberOfGases-1:2*numberOfGases-1] = np.multiply(rateConstant,(sensorLoadingPerGasVol-f[numberOfGases-1:2*numberOfGases-1]))
# Total mass balance
# Assumes constant pressure, so flow rate evalauted
flowOut = flowIn - ((volSorbent*(Rg*temperature)/pressureTotal)*(np.sum(df[numberOfGases-1:2*numberOfGases-1])))
# Component mass balance
term1 = 1/volGas
for ii in range(numberOfGases-1):
term2 = ((flowIn*feedMoleFrac[ii] - flowOut*f[ii])
- (volSorbent*(Rg*temperature)/pressureTotal)*df[ii+numberOfGases-1])
df[ii] = term1*term2
# Return the derivatives for the solver
return df
# func: plotFullModelResult
# Plots the model output for the conditions simulated locally
def plotFullModelResult(timeSim, resultMat, sensorFingerPrint, inputParameters,
gitCommitID, currentDT, modelConstF):
import numpy as np
import os
import matplotlib.pyplot as plt
# Save settings
saveFlag = False
saveFileExtension = ".png"
# Unpack the tuple of input parameters used to solve equations
sensorID, _ , _ , _ , flowIn, _ , _ , _ , temperature, _ , _ = inputParameters
os.chdir("plotFunctions")
if resultMat.shape[0] == 7:
# Plot the solid phase compositions
plt.style.use('doubleColumn.mplstyle') # Custom matplotlib style file
fig = plt.figure
ax = plt.subplot(1,3,1)
ax.plot(timeSim, resultMat[2,:],
linewidth=1.5,color='r')
ax.set(xlabel='$t$ [s]',
ylabel='$q_1$ [mol m$^{\mathregular{-3}}$]',
xlim = [timeSim[0], timeSim[-1]], ylim = [0, 1.1*np.max(resultMat[2,:])])
ax = plt.subplot(1,3,2)
ax.plot(timeSim, resultMat[3,:],
linewidth=1.5,color='b')
ax.set(xlabel='$t$ [s]',
ylabel='$q_2$ [mol m$^{\mathregular{-3}}$]',
xlim = [timeSim[0], timeSim[-1]], ylim = [0, 1.1*np.max(resultMat[3,:])])
ax = plt.subplot(1,3,3)
ax.plot(timeSim, resultMat[4,:],
linewidth=1.5,color='g')
ax.set(xlabel='$t$ [s]',
ylabel='$q_3$ [mol m$^{\mathregular{-3}}$]',
xlim = [timeSim[0], timeSim[-1]], ylim = [0, 1.1*np.max(resultMat[4,:])])
# Save the figure
if saveFlag:
# FileName: solidLoadingFM_<sensorID>_<currentDateTime>_<gitCommitID>
saveFileName = "solidLoadingFM_" + str(sensorID) + "_" + currentDT + "_" + gitCommitID + saveFileExtension
savePath = os.path.join('..','simulationFigures',saveFileName.replace('[','').replace(']',''))
# Check if inputResources directory exists or not. If not, create the folder
if not os.path.exists(os.path.join('..','simulationFigures')):
os.mkdir(os.path.join('..','simulationFigures'))
plt.savefig (savePath)
plt.show()
# Plot the pressure drop, the flow rate, and the mole fraction
plt.style.use('doubleColumn.mplstyle') # Custom matplotlib style file
fig = plt.figure
ax = plt.subplot(1,3,1)
ax.plot(timeSim, resultMat[5,:],
linewidth=1.5,color='r')
ax.set_xlabel('$t$ [s]')
ax.set_ylabel('$P$ [Pa]', color='r')
ax.tick_params(axis='y', labelcolor='r')
ax.set(xlim = [timeSim[0], timeSim[-1]],
ylim = [0, 1.1*np.max(resultMat[5,:])])
ax2 = plt.twinx()
ax2.plot(timeSim, resultMat[6,:],
linewidth=1.5,color='b')
ax2.set_ylabel('$F$ [m$^{\mathregular{3}}$ s$^{\mathregular{-1}}$]', color='b')
ax2.tick_params(axis='y', labelcolor='b')
ax2.set(xlim = [timeSim[0], timeSim[-1]],
ylim = [0, 1.1*np.max(resultMat[6,:])])
ax = plt.subplot(1,3,2)
ax.plot(timeSim, resultMat[5,:]*resultMat[6,:]/(temperature*8.314),
linewidth=1.5,color='k')
ax.plot(timeSim, resultMat[5,:]*resultMat[6,:]*resultMat[0,:]/(temperature*8.314),
linewidth=1.5,color='r')
ax.plot(timeSim, resultMat[5,:]*resultMat[6,:]*resultMat[1,:]/(temperature*8.314),
linewidth=1.5,color='b')
ax.plot(timeSim, resultMat[5,:]*resultMat[6,:]*(1-resultMat[0,:]-resultMat[1,:])/(temperature*8.314),
linewidth=1.5,color='g')
ax.set(xlabel='$t$ [s]',
ylabel='$Q$ [mol s$^{\mathregular{-1}}$]',
xlim = [timeSim[0], timeSim[-1]], ylim = [0, 1.1*np.max(resultMat[5,:])*np.max(resultMat[6,:])
/temperature/8.314])
ax = plt.subplot(1,3,3)
ax.plot(timeSim, resultMat[0,:],linewidth=1.5,color='r')
ax.plot(timeSim, resultMat[1,:],linewidth=1.5,color='b')
ax.plot(timeSim, 1-resultMat[0,:]-resultMat[1,:],linewidth=1.5,color='g')
ax.set(xlabel='$t$ [s]',
ylabel='$y$ [-]',
xlim = [timeSim[0], timeSim[-1]], ylim = [0, 1.])
plt.show()
# Plot the sensor finger print
plt.style.use('singleColumn.mplstyle') # Custom matplotlib style file
fig = plt.figure
ax = plt.subplot(1,1,1)
ax.plot(timeSim, sensorFingerPrint,
linewidth=1.5,color='k')
ax.set(xlabel='$t$ [s]',
ylabel='$m_i$ [g kg$^{\mathregular{-1}}$]',
xlim = [timeSim[0], timeSim[-1]], ylim = [0, 1.1*np.max(sensorFingerPrint)])
# Save the figure
if saveFlag:
# FileName: SensorFingerPrintFM_<sensorID>_<currentDateTime>_<gitCommitID>
saveFileName = "SensorFingerPrintFM_" + str(sensorID) + "_" + currentDT + "_" + gitCommitID + saveFileExtension
savePath = os.path.join('..','simulationFigures',saveFileName.replace('[','').replace(']',''))
# Check if inputResources directory exists or not. If not, create the folder
if not os.path.exists(os.path.join('..','simulationFigures')):
os.mkdir(os.path.join('..','simulationFigures'))
plt.savefig (savePath)
plt.show()
os.chdir("..")