MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
flowRate_idealGas.py
1 
31 
32 
39 
40 import os
41 import modena
42 from modena import ForwardMappingModel, BackwardMappingModel, SurrogateModel, CFunction, ModenaFireTask
43 import modena.Strategy as Strategy
44 from fireworks import Firework, Workflow, FWAction
45 from fireworks.utilities.fw_utilities import explicit_serialize
46 from blessings import Terminal
47 from jinja2 import Template
48 import idealGas
49 
50 
51 # ********************************* Class ********************************** #
52 @explicit_serialize
53 
57 class FlowRateExactSim(ModenaFireTask):
58 
59  def task(self, fw_spec):
60  # Write input
61 
62  # See http://jinja.pocoo.org/docs/dev/templates/
63  Template('''
64 {{ s['point']['D'] }}
65 {{ s['point']['rho0'] }}
66 {{ s['point']['p0'] }}
67 {{ s['point']['p1Byp0'] }}
68  '''.strip()).stream(s=self).dump('in.txt')
69 
70  # Execute the application
71  # In this simple example, this call stands for a complex microscopic
72  # code - such as full 3D CFD simulation.
73  # Source code in src/flowRateExact.C
74  ret = os.system(os.path.dirname(os.path.abspath(__file__))+'/src/flowRateExact')
75 
76  # This enables backward mapping capabilities (not needed in this example)
77  self.handleReturnCode(ret)
78 
79  # Analyse output
80  f = open('out.txt', 'r') self['point']['flowRate'] = float(f.readline())
81  f.close()
82 
83 
84 f = CFunction(
85  Ccode= '''
86 #include "modena.h"
87 #include "math.h"
88 
89 void two_tank_flowRate
90 (
91  const modena_model_t* model,
92  const double* inputs,
93  double *outputs
94 )
95 {
96  {% block variables %}{% endblock %}
97 
98  const double D = inputs[0];
99  const double rho0 = inputs[1];
100  const double p0 = inputs[2];
101  const double p1 = p0*inputs[3];
102 
103  const double P0 = parameters[0];
104  const double P1 = parameters[1];
105 
106  outputs[0] = M_PI*pow(D, 2.0)*P1*sqrt(P0*rho0*p0);
107 }
108 ''',
109  # These are global bounds for the function
110  inputs={
111  'D': { 'min': 0, 'max': 9e99 },
112  'T0': { 'min': 0, 'max': 9e99 },
113  'p0': { 'min': 0, 'max': 9e99 },
114  'p1Byp0': { 'min': 0, 'max': 1.0 },
115  },
116  outputs={
117  'flowRate': { 'min': 9e99, 'max': -9e99, 'argPos': 0 },
118  },
119  parameters={
120  'param0': { 'min': 0.0, 'max': 10.0, 'argPos': 0 },
121  'param1': { 'min': 0.0, 'max': 10.0, 'argPos': 1 },
122  },
123 )
124 
125 m = BackwardMappingModel(
126  _id= 'flowRate',
127  surrogateFunction= f,
128  exactTask= FlowRateExactSim(),
129  substituteModels= [ idealGas.m ],
130  initialisationStrategy= Strategy.InitialPoints(
131  initialPoints=
132  {
133  'D': [0.01, 0.01, 0.01, 0.01],
134  'T0': [300, 300, 300, 300],
135  'p0': [2.8e5, 3.2e5, 2.8e5, 3.2e5],
136  'p1Byp0': [0.03, 0.03, 0.04, 0.04],
137  },
138  ),
139  outOfBoundsStrategy= Strategy.ExtendSpaceStochasticSampling(
140  nNewPoints= 4
141  ),
142  parameterFittingStrategy= Strategy.NonLinFitWithErrorContol(
143  testDataPercentage= 0.2,
144  maxError= 0.05,
145  improveErrorStrategy= Strategy.StochasticSampling(
146  nNewPoints= 2
147  ),
148  maxIterations= 5 # Currently not used
149  ),
150 )
151