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