MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
SurfaceTension.py
Go to the documentation of this file.
1 
31 
32 
69 
70 import os
71 from modena import *
72 from fireworks.utilities.fw_utilities import explicit_serialize
73 from jinja2 import Template
74 
75 
85 blowing_agents = IndexSet(
86  name= 'blowing_agents',
87  names= [ 'AIR', 'CO2']
88 )
89 
90 
100 monomers = IndexSet(
101  name = 'monomers',
102  names = ['PU', 'THF', 'HEXANE']
103 )
104 
105 
115 surfactant = IndexSet(
116  name = 'surfactant',
117  names = ['surfactant','no_surfactant']
118 )
119 
120 
121 
122 # ********************************* Class ********************************** #
123 @explicit_serialize
124 
149 class SurfaceTensionExactSim(ModenaFireTask):
150 
151  def task(self, fw_spec):
152 
153  # Write input for detailed model
154  self.generate_inputfile()
155 
156  # Execute detailed model
157  run_command = os.path.dirname(os.path.abspath(__file__))+'/src/PCSAFT_SurfaceTension -snes_monitor_short -ksp_monitor_short \
158  -nx 800 -rc 9.0 -box 300 -erel 1e-08 -init_pert 0 \
159  -snes_type newtonls -snes_converged_reason \
160  -snes_atol 1e-07 -snes_rtol 1e-07 -snes_stol 1e-07 -snes_max_it 20 \
161  -ksp_max_it 15 -ksp_gmres_restart 50 \
162  -snes_linesearch_type l2 -snes_linesearch_damping 0.3 -snes_linesearch_monitor \
163  -snes_max_fail 1 -snes_max_linear_solve_fail 100 \
164  -ksp_gmres_cgs_refinement_type refine_always \
165  -snes_ksp_ew -snes_ksp_ew_version 1 -snes_ksp_ew_rtol0 0.5 -snes_ksp_ew_rtolmax 0.9 -snes_ksp_ew_threshold 0.1 \
166  -jac 0 -pc_type none > log'
167 
168 
169  ret = os.system(run_command)
170  # This call enables backward mapping capabilities (not needed in this example)
171  self.handleReturnCode(ret)
172 
173  # Analyse output
174  self.analyse_output()
175 
176 
180  def generate_inputfile(self):
181  with open('in.txt','w') as f:
182  f.write("{}\n".format(self['point']['T']))
183  if self['indices']['C']=='no_surfactant':
184  ncomp=2
185  f.write("{}\n".format(ncomp))
186  f.write("{}\n".format(self['indices']['A'].lower()))
187  f.write("{}\n".format(self['indices']['B'].lower()))
188  else:
189  ncomp=3
190  f.write("{}\n".format(ncomp))
191  f.write("{}\n".format(self['indices']['A'].lower()))
192  f.write("{}\n".format(self['indices']['B'].lower()))
193  f.write("{}\n".format(self['indices']['C'].lower()))
194  for i in range(ncomp):
195  f.write("{}\n".format(0.0))
196  with open('out.txt','w+') as FILE:
197  pass
198 
199 
203  def analyse_output(self):
204  with open('out.txt', 'r') as FILE: self['point']['ST'] = float(FILE.readline())
205 
206 
227 f = CFunction(
228  Ccode= r'''
229 #include "modena.h"
230 #include "math.h"
231 
232 void surroSurfaceTension
233 (
234 const modena_model_t* model,
235 const double* inputs,
236 double *outputs
237 )
238 {
239 {% block variables %}{% endblock %}
240 
241 const double P0 = parameters[0];
242 const double P1 = parameters[1];
243 const double P2 = parameters[2];
244 
245 outputs[0] = P0 + T*P1 + P2*T*T;
246 }
247 ''',
248  inputs={
249  'T': { 'min': 270.0, 'max': 550.0 },
250  },
251  outputs={
252  'ST': { 'min': 9e99, 'max': -9e99, 'argPos': 0 },
253  },
254  parameters={
255  'param0': { 'min': -1E10, 'max': 1E10, 'argPos': 0 },
256  'param1': { 'min': -1E10, 'max': 1E10, 'argPos': 1 },
257  'param2': { 'min': -1E10, 'max': 1E10, 'argPos': 2 },
258  },
259  species = {
260  'A' : blowing_agents,
261  'B' : monomers,
262  'C' : surfactant,
263  }
264 )
265 
266 
282 m = BackwardMappingModel(
283  _id= 'SurfaceTension[A=AIR,B=PU,C=surfactant]',
284  surrogateFunction= f,
285  exactTask= SurfaceTensionExactSim(),
286  substituteModels= [ ],
287  initialisationStrategy= Strategy.InitialPoints(
288  initialPoints=
289  {
290  'T': [290.0, 300.0, 350.0, 500.0],
291  },
292  ),
293  outOfBoundsStrategy= Strategy.ExtendSpaceStochasticSampling(
294  nNewPoints= 4
295  ),
296  parameterFittingStrategy= Strategy.NonLinFitWithErrorContol(
297  testDataPercentage= 0.2,
298  maxError= 1e-0,
299  improveErrorStrategy= Strategy.StochasticSampling(
300  nNewPoints= 2
301  ),
302  maxIterations= 5 # Currently not used
303  ),
304 )
305 
306 
319 m2 = BackwardMappingModel(
320  _id= 'SurfaceTension[A=AIR,B=PU,C=no_surfactant]',
321  surrogateFunction= f,
322  exactTask= SurfaceTensionExactSim(),
323  substituteModels= [ ],
324  initialisationStrategy= Strategy.InitialPoints(
325  initialPoints=
326  {
327  'T': [290.0, 300.0, 350.0, 500.0],
328  },
329  ),
330  outOfBoundsStrategy= Strategy.ExtendSpaceStochasticSampling(
331  nNewPoints= 4
332  ),
333  parameterFittingStrategy= Strategy.NonLinFitWithErrorContol(
334  testDataPercentage= 0.2,
335  maxError= 1e-0,
336  improveErrorStrategy= Strategy.StochasticSampling(
337  nNewPoints= 2
338  ),
339  maxIterations= 5 # Currently not used
340  ),
341 )
342 
Recipe for PCSAFT Surface Tension Application.