MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
Solubility.py
1 
31 
32 
66 
67 import os
68 from modena import *
69 from modena import ForwardMappingModel,BackwardMappingModel,SurrogateModel,\
70  CFunction,IndexSet
71 import modena.Strategy as Strategy
72 from fireworks.user_objects.firetasks.script_task import FireTaskBase, ScriptTask
73 from fireworks import Firework, Workflow, FWAction
74 from fireworks.utilities.fw_utilities import explicit_serialize
75 from blessings import Terminal
76 from jinja2 import Template
77 
78 # Create terminal for colour output
79 term = Terminal()
80 
81 species = IndexSet(
82  name= 'solubility_pol_species',
83  names= [ 'Air', 'CO2', 'CyP', 'O2', 'N2' ]
84 )
85 system = IndexSet(
86  name= 'solubility_num_of_components',
87  names= [ '2', '3' ]
88 )
89 
90 @explicit_serialize
91 
103 class SolubilityExactSim(ModenaFireTask):
104  def task(self, fw_spec):
105  # Write input for detailed model
106  ff = open('in.txt', 'w')
107  Tstr = str(self['point']['T'])
108  ff.write('%s \n' %(Tstr))
109  if (self['indices']['B']=='2'):
110  ff.write('2 \n') #number of components in system
111  if (self['indices']['A']=='CO2'):
112  ff.write('co2 \n')
113  elif (self['indices']['A']=='Air'):
114  ff.write('air \n')
115  elif (self['indices']['A']=='CyP'):
116  ff.write('cyclopentane \n')
117  elif (self['indices']['A']=='O2'):
118  ff.write('o2 \n')
119  elif (self['indices']['A']=='N2'):
120  ff.write('n2 \n')
121  #pass molar liquid composition
122  x1l_str = str(self['point']['xl1'])
123  x2l_str = str(self['point']['xl2'])
124  ff.write('%s \n' %(x1l_str))
125  ff.write('%s \n' %(x2l_str))
126  elif (self['indices']['B']=='3'):
127  ff.write('3 \n') #number of components in system
128  if (self['indices']['A']=='CO2'):
129  ff.write('co2 \n')
130  elif (self['indices']['A']=='Air'):
131  ff.write('air \n')
132  elif (self['indices']['A']=='CyP'):
133  ff.write('cyclopentane \n')
134  elif (self['indices']['A']=='O2'):
135  ff.write('o2 \n')
136  elif (self['indices']['A']=='N2'):
137  ff.write('n2 \n')
138  #pass molar liquid composition
139  x1l_str = str(self['point']['xl1'])
140  x2l_str = str(self['point']['xl2'])
141  x3l_str = str(self['point']['xl3'])
142  ff.write('%s \n' %(x1l_str))
143  ff.write('%s \n' %(x2l_str))
144  ff.write('%s \n' %(x3l_str))
145  ff.close()
146 
147  #create output file for detailed code
148  fff = open('out.txt', 'w+')
149  fff.close()
150 
151  # Execute the detailed model
152  # path to **this** file + /src/...
153  # will break if distributed computing
154  ret = os.system(os.path.dirname(os.path.abspath(__file__))+\
155  '/src/pcsaft')
156  # This call enables backward mapping capabilities
157  self.handleReturnCode(ret)
158  # Analyse output
159  # os.getcwd() returns the path to the "launcher" directory
160  try:
161  FILE = open(os.getcwd()+'/out.txt','r') except IOError:
162  raise IOError("File not found")
163  self['point']['H'] = float(FILE.readline())
164  FILE.close()
165 
166 
172 Ccode2='''
173 #include "modena.h"
174 #include "math.h"
175 
176 void surroSolubility2
177 (
178 const modena_model_t* model,
179 const double* inputs,
180 double *outputs
181 )
182 {
183 {% block variables %}{% endblock %}
184 
185 const double P0 = parameters[0];
186 const double P1 = parameters[1];
187 const double P2 = parameters[2];
188 
189 const double term1 = P1*(1/T - 1/P2);
190 const double term2 = exp(term1);
191 
192 outputs[0] = P0*term2;
193 
194 outputs[0] = P0 + T*P1 + P2*T*T;
195 }
196 '''
197 Ccode3='''
198 #include "modena.h"
199 #include "math.h"
200 
201 void surroSolubility3
202 (
203 const modena_model_t* model,
204 const double* inputs,
205 double *outputs
206 )
207 {
208 {% block variables %}{% endblock %}
209 
210 const double P0 = parameters[0];
211 const double P1 = parameters[1];
212 const double P2 = parameters[2];
213 const double P3 = parameters[3];
214 
215 const double term1 = P1*(1/T - 1/P2);
216 const double term2 = exp(term1);
217 
218 outputs[0] = P0*term2;
219 
220 outputs[0] = P0 + T*P1 + P2*T*T;
221 
222 outputs[0] = P0+P1*exp(-pow((T-P2),2)/P3);
223 }
224 '''
225 outputs={
226  'H': { 'min': 9e99, 'max': -9e99, 'argPos': 0 }
227 }
228 parameters={
229  'param0': { 'min': 1e-12, 'max': 1E1, 'argPos': 0 }, #check if boundaries are reasonable!!!
230  'param1': { 'min': -1e-1, 'max': 1e-1, 'argPos': 1 },
231  'param2': { 'min': -1e-1, 'max': 1e-1, 'argPos': 2 },
232 }
233 parameters4={
234  'param0': { 'min': -9e9, 'max': 9e9+2*1.42885440e-04, 'argPos': 0 }, #check if boundaries are reasonable!!!
235  'param1': { 'min': -9e9, 'max': 9e9+2*1.22132172e+00, 'argPos': 1 },
236  'param2': { 'min': -9e9, 'max': 9e9+2*-7.97789449e+02, 'argPos': 2 },
237  'param3': { 'min': -9e9, 'max': 9e9+2*1.33835999e+05, 'argPos': 3 },
238 }
239 indices={
240  'A': species,
241  'B': system
242 }
243 inputs2={
244  'T': { 'min': 200.0, 'max': 550.0}, #check if boundaries reasonable, from this range, the random values for the DOE are chosen!
245  'xl1': { 'min': 0.0, 'max': 1.0 },
246  'xl2': { 'min': 0.0, 'max': 1.0 },
247 }
248 inputs3={
249  'T': { 'min': 200.0, 'max': 550.0}, #check if boundaries reasonable, from this range, the random values for the DOE are chosen!
250  'xl1': { 'min': 0.0, 'max': 1.0 },
251  'xl2': { 'min': 0.0, 'max': 1.0 },
252  'xl3': { 'min': 0.0, 'max': 1.0 },
253 }
254 f2 = CFunction(Ccode=Ccode2,
255  inputs=inputs2,
256  outputs=outputs,
257  parameters=parameters,
258  indices=indices
259 )
260 f3 = CFunction(Ccode=Ccode3,
261  inputs=inputs3,
262  outputs=outputs,
263  parameters=parameters4,
264  indices=indices
265 )
266 
267 outOfBoundsStrategy=Strategy.ExtendSpaceStochasticSampling(
268  nNewPoints=4
269 )
270 parameterFittingStrategy=Strategy.NonLinFitWithErrorContol(
271  testDataPercentage=0.2,
272  maxError=0.1,
273  improveErrorStrategy=Strategy.StochasticSampling(
274  nNewPoints=2
275  ),
276  maxIterations=5 # Currently not used
277 )
278 m_solubilityCO2PU = BackwardMappingModel(
279  _id='Solubility[A=CO2,B=2]',
280  surrogateFunction=f2,
281  exactTask=SolubilityExactSim(),
282  substituteModels=[],
283  initialisationStrategy=Strategy.InitialPoints(
284  initialPoints={
285  'T': [290, 320, 350, 380],
286  'xl1': [1.1e-3, 1.0e-3, 1.0e-3, 1.0e-4],
287  'xl2': [0.9989, 0.999, 0.999, 0.9999],
288  },
289  ),
290  outOfBoundsStrategy=outOfBoundsStrategy,
291  parameterFittingStrategy=parameterFittingStrategy
292 )
293 m_solubilityAirPU = BackwardMappingModel(
294  _id='Solubility[A=Air,B=2]',
295  surrogateFunction=f2,
296  exactTask=SolubilityExactSim(),
297  substituteModels=[],
298  initialisationStrategy=Strategy.InitialPoints(
299  initialPoints={
300  'T': [290, 320, 350, 380],
301  'xl1': [1.1e-3, 1.0e-3, 1.0e-3, 1.0e-4],
302  'xl2': [0.9989, 0.999, 0.999, 0.9999],
303  },
304  ),
305  outOfBoundsStrategy=outOfBoundsStrategy,
306  parameterFittingStrategy=parameterFittingStrategy
307 )
308 m_solubilityCyclopentanePU = BackwardMappingModel(
309  _id='Solubility[A=CyP,B=2]',
310  surrogateFunction=f2,
311  exactTask=SolubilityExactSim(),
312  substituteModels=[],
313  initialisationStrategy=Strategy.InitialPoints(
314  initialPoints={
315  'T': [290, 320, 350, 380],
316  'xl1': [1.1e-3, 1.0e-3, 1.0e-3, 1.0e-4],
317  'xl2': [0.9989, 0.999, 0.999, 0.9999],
318  },
319  ),
320  outOfBoundsStrategy=outOfBoundsStrategy,
321  parameterFittingStrategy=parameterFittingStrategy
322 )
323 m_solubilityO2PU = BackwardMappingModel(
324  _id='Solubility[A=O2,B=2]',
325  surrogateFunction=f2,
326  exactTask=SolubilityExactSim(),
327  substituteModels=[],
328  initialisationStrategy=Strategy.InitialPoints(
329  initialPoints={
330  'T': [290, 320, 350, 380],
331  'xl1': [1.1e-3, 1.0e-3, 1.0e-3, 1.0e-4],
332  'xl2': [0.9989, 0.999, 0.999, 0.9999],
333  },
334  ),
335  outOfBoundsStrategy=outOfBoundsStrategy,
336  parameterFittingStrategy=parameterFittingStrategy
337 )
338 m_solubilityN2PU = BackwardMappingModel(
339  _id='Solubility[A=N2,B=2]',
340  surrogateFunction=f2,
341  exactTask=SolubilityExactSim(),
342  substituteModels=[],
343  initialisationStrategy=Strategy.InitialPoints(
344  initialPoints={
345  'T': [290, 320, 350, 380],
346  'xl1': [1.1e-3, 1.0e-3, 1.0e-3, 1.0e-4],
347  'xl2': [0.9989, 0.999, 0.999, 0.9999],
348  },
349  ),
350  outOfBoundsStrategy=outOfBoundsStrategy,
351  parameterFittingStrategy=parameterFittingStrategy
352 )
353 # 3 component models
354 m_solubilityCO2 = BackwardMappingModel(
355  _id='Solubility[A=CO2,B=3]',
356  surrogateFunction=f3,
357  exactTask=SolubilityExactSim(),
358  substituteModels=[],
359  initialisationStrategy=Strategy.InitialPoints(
360  initialPoints={
361  'T': [290, 350, 450, 550],
362  'xl1': [0.9e-4, 1.1e-4, 1e-4, 1e-4],
363  'xl2': [0.51, 0.49, 0.5, 0.5],
364  'xl3': [0.49, 0.51, 0.5, 0.5],
365  },
366  ),
367  outOfBoundsStrategy=outOfBoundsStrategy,
368  parameterFittingStrategy=parameterFittingStrategy
369 )
370 m_solubilityAir = BackwardMappingModel(
371  _id='Solubility[A=Air,B=3]',
372  surrogateFunction=f3,
373  exactTask=SolubilityExactSim(),
374  substituteModels=[],
375  initialisationStrategy=Strategy.InitialPoints(
376  initialPoints={
377  'T': [290, 350, 450, 550],
378  'xl1': [0.9e-4, 1.1e-4, 1e-4, 1e-4],
379  'xl2': [0.51, 0.49, 0.5, 0.5],
380  'xl3': [0.49, 0.51, 0.5, 0.5],
381  },
382  ),
383  outOfBoundsStrategy=outOfBoundsStrategy,
384  parameterFittingStrategy=parameterFittingStrategy
385 )
386 m_solubilityCyclopentane = BackwardMappingModel(
387  _id='Solubility[A=CyP,B=3]',
388  surrogateFunction=f3,
389  exactTask=SolubilityExactSim(),
390  substituteModels=[],
391  initialisationStrategy=Strategy.InitialPoints(
392  initialPoints={
393  'T': [290, 350, 450, 550],
394  'xl1': [0.9e-4, 1.1e-4, 1e-4, 1e-4],
395  'xl2': [0.51, 0.49, 0.5, 0.5],
396  'xl3': [0.49, 0.51, 0.5, 0.5],
397  },
398  ),
399  outOfBoundsStrategy=outOfBoundsStrategy,
400  parameterFittingStrategy=parameterFittingStrategy
401 )
402 # below are experimental solubility models
403 # they are needed, because we want substitute model for bubble growth model
404 inputsExp={
405  'T': { 'min': 200.0, 'max': 550.0}
406 }
407 CcodeR11Baser='''
408 #include "modena.h"
409 #include "math.h"
410 
411 void surroSolubilityR11Baser
412 (
413 const modena_model_t* model,
414 const double* inputs,
415 double *outputs
416 )
417 {
418 {% block variables %}{% endblock %}
419 
420 const double P0 = parameters[0];
421 const double P1 = parameters[1];
422 const double P2 = parameters[2];
423 const double P3 = parameters[3];
424 
425 outputs[0] = (P0 + P1*exp(-pow(T-P2,2)/(2*P3*P3)))*1100.0/137.37e-3/101e3;
426 }
427 '''
428 parameters4={
429  'param0': { 'min': 1e-12, 'max': 1E1, 'argPos': 0 },
430  'param1': { 'min': -1e-1, 'max': 1e-1, 'argPos': 1 },
431  'param2': { 'min': -1e-1, 'max': 1e-1, 'argPos': 2 },
432  'param3': { 'min': -1e-1, 'max': 1e-1, 'argPos': 3 },
433 }
434 fR11Baser = CFunction(Ccode=CcodeR11Baser,
435  inputs=inputsExp,
436  outputs=outputs,
437  parameters=parameters4
438 )
439 
440 parR11Baser = [1e-7, 4.2934, 203.3556, 40.016]
441 m_solubilityR11Baser = ForwardMappingModel(
442  _id='SolubilityR11Baser',
443  surrogateFunction=fR11Baser,
444  substituteModels=[],
445  parameters=parR11Baser,
446  inputs=inputsExp,
447  outputs=outputs,
448 )
449 CcodeCO2Baser='''
450 #include "modena.h"
451 #include "math.h"
452 
453 void surroSolubilityCO2Baser
454 (
455 const modena_model_t* model,
456 const double* inputs,
457 double *outputs
458 )
459 {
460 {% block variables %}{% endblock %}
461 
462 const double P0 = parameters[0];
463 
464 outputs[0] = P0;
465 }
466 '''
467 parameters1={
468  'param0': { 'min': 1e-12, 'max': 1E1, 'argPos': 0 },
469 }
470 if 'argPos' in inputsExp['T']: #added by previous CFunction
471  inputsExp['T'].pop('argPos')
472 fCO2Baser = CFunction(Ccode=CcodeCO2Baser,
473  inputs=inputsExp,
474  outputs=outputs,
475  parameters=parameters1
476 )
477 
478 parCO2Baser = [1.1e-4]
479 m_solubilityCO2Baser = ForwardMappingModel(
480  _id='SolubilityCO2Baser',
481  surrogateFunction=fCO2Baser,
482  substituteModels=[],
483  parameters=parCO2Baser,
484  inputs=inputsExp,
485  outputs=outputs,
486 )
487 CcodePentGupta='''
488 #include "modena.h"
489 #include "math.h"
490 
491 void surroSolubilityPentGupta
492 (
493 const modena_model_t* model,
494 const double* inputs,
495 double *outputs
496 )
497 {
498 {% block variables %}{% endblock %}
499 
500 const double P0 = parameters[0];
501 const double P1 = parameters[1];
502 const double P2 = parameters[2];
503 const double P3 = parameters[3];
504 const double P4 = parameters[4];
505 
506 outputs[0] = P0/(exp((P1-P2*T)/(P3-T))-P4)*1100.0/72.15e-3/101e3;
507 }
508 '''
509 parameters5={
510  'param0': { 'min': 1e-12, 'max': 1E1, 'argPos': 0 },
511  'param1': { 'min': -1e-1, 'max': 1e-1, 'argPos': 1 },
512  'param2': { 'min': -1e-1, 'max': 1e-1, 'argPos': 2 },
513  'param3': { 'min': -1e-1, 'max': 1e-1, 'argPos': 3 },
514  'param4': { 'min': -1e-1, 'max': 1e-1, 'argPos': 4 },
515 }
516 if 'argPos' in inputsExp['T']: #added by previous CFunction
517  inputsExp['T'].pop('argPos')
518 fPentGupta = CFunction(Ccode=CcodePentGupta,
519  inputs=inputsExp,
520  outputs=outputs,
521  parameters=parameters5
522 )
523 
524 parPentGupta = [-3.3e-4, 2.09e4, 67.5, 8.69e4, 1.01]
525 m_solubilityPentGupta = ForwardMappingModel(
526  _id='SolubilityPentGupta',
527  surrogateFunction=fPentGupta,
528  substituteModels=[],
529  parameters=parPentGupta,
530  inputs=inputsExp,
531  outputs=outputs,
532 )
533 CcodePentWinkler='''
534 #include "modena.h"
535 #include "math.h"
536 
537 void surroSolubilityPentWinkler
538 (
539 const modena_model_t* model,
540 const double* inputs,
541 double *outputs
542 )
543 {
544 {% block variables %}{% endblock %}
545 
546 const double P0 = parameters[0];
547 const double P1 = parameters[1];
548 const double P2 = parameters[2];
549 const double P3 = parameters[3];
550 
551 outputs[0] = (P0 + P1*exp(-pow(T-P2,2)/(2*P3*P3)))*1100.0/72.15e-3/101e3;
552 }
553 '''
554 if 'argPos' in inputsExp['T']: #added by previous CFunction
555  inputsExp['T'].pop('argPos')
556 fPentWinkler = CFunction(Ccode=CcodePentWinkler,
557  inputs=inputsExp,
558  outputs=outputs,
559  parameters=parameters5
560 )
561 
562 parPentWinkler = [0.0064,0.0551,298.0,17.8]
563 m_solubilityPentWinkler = ForwardMappingModel(
564  _id='SolubilityPentWinkler',
565  surrogateFunction=fPentWinkler,
566  substituteModels=[],
567  parameters=parPentWinkler,
568  inputs=inputsExp,
569  outputs=outputs,
570 )
571 
This FireTask controls the execution of the detailed model of the Solubility model.
Definition: Solubility.py:102