MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
ElasticModulus.py
1 
25 
26 
49 
50 import os
51 from os import getcwd, system
52 from os.path import abspath, dirname, join
53 from jinja2 import Template
54 import subprocess
55 
56 from modena import ForwardMappingModel,BackwardMappingModel, CFunction, ModenaFireTask, Strategy
57 
58 
59 from fireworks.utilities.fw_utilities import explicit_serialize
60 
61 # ----------------------- Convenience variables ----------------------------- #
62 MODULE_DIR = dirname(abspath(__file__))
63 EXECUTION_DIR = getcwd()
64 
65 TEMPLATE_FoamElasticModulus = Template(\
66 """
67 {{ s['point']['Mu'] }}
68 {{ s['point']['Sigma'] }}
69 {{ s['point']['strut_content'] }}
70 {{ s['point']['rho'] }}
71 """, trim_blocks=True, lstrip_blocks=True)
72 
73 # ----------------------- Exact simulation wrapper -------------------------- #
74 @explicit_serialize
75 
78 class MechanicalPropertiesExactSim(ModenaFireTask):
79 
80  INPUT_FILE = "Inputs.in"
81  OUTPUT_FILE = "Outputs.in"
82 
83  def task(self, fw_spec):
84 
85  # Write input for detailed model
86  TEMPLATE_FoamElasticModulus.stream(s=self).dump(self.INPUT_FILE)
87 
88  # Execute detailed model
89  run_command = join(MODULE_DIR, "AbaqusSimulation.py")
90 
91  ret = subprocess.check_call(run_command, shell=True)
92  # This call enables backward mapping capabilities (not needed in this example)
93  self.handleReturnCode(ret)
94 
95  # Analyse output
96  with open(self.OUTPUT_FILE, 'r') as FILE: self['point']['E'] = float(FILE.readline())
97 
98 
99 
100 # ----------------------- Surrogate Functions ------------------------------- #
101 f_backward = CFunction(
102  Ccode= '''
103 #include "modena.h"
104 #include "math.h"
105 void two_tank_flowRate
106 (
107  const modena_model_t* model,
108  const double* inputs,
109  double *outputs
110 )
111 {
112  {% block variables %}{% endblock %}
113 
114 
115  const double C1 = parameters[0];
116  const double C2 = parameters[1];
117 
118  const double E_PU = 2400;
119  const double rho_PU = 1200;
120 
121  outputs[0] = E_PU*(C1*pow(strut_content*rho/rho_PU,2) + C2*(1-strut_content)*(rho/rho_PU));
122 }
123 ''',
124  # These are global bounds for the function
125  inputs={
126  'rho': { 'min': 0, 'max': 1e5},
127  'strut_content': { 'min': 0, 'max': 1e5},
128  'Mu': { 'min': 0, 'max': 1e5},
129  'Sigma': { 'min': 0, 'max': 1e5},
130  },
131  outputs={
132  'E': { 'min': 9e99, 'max': -9e99, 'argPos': 0 },
133  },
134  parameters={
135  'C1': { 'min': 0.0, 'max': 10.0, 'argPos': 0 },
136  'C2': { 'min': 0.0, 'max': 10.0, 'argPos': 1 },
137  },
138 )
139 
140 
141 # Strut content is a assumed to be a function of foam density.
142 f_ludwigshafen = CFunction(
143  Ccode='''
144 #include "modena.h"
145 #include "math.h"
146 void ElasticModulus
147 (
148  const modena_model_t* model,
149  const double* inputs,
150  double *outputs
151 )
152 {
153  {% block variables %}{% endblock %}
154 
155  const double C1 = parameters[0];
156  const double C2 = parameters[1];
157  const double E_PU = 2400;
158  const double rho_PU = 1200;
159 
160  outputs[0] = E_PU*(C1*pow(strut_content*rho/rho_PU,2) + C2*(1-strut_content)*(rho/rho_PU));
161 }
162 ''',
163  # These are global bounds for the function
164  inputs={
165  'rho': { 'min': 0, 'max': 1e5},
166  'strut_content': { 'min': 0, 'max': 1e5},
167  'Mu': { 'min': 0, 'max': 1e5},
168  'Sigma': { 'min': 0, 'max': 1e5},
169  },
170  outputs={
171  'E': { 'min': 0, 'max': 1, 'argPos': 0 },
172  },
173  parameters={
174  'C1': { 'min': -9e99, 'max': +9e99, 'argPos': 0 },
175  'C2': { 'min': -9e99, 'max': +9e99, 'argPos': 1 },
176  },
177 )
178 
179 
180 # -------------------------- Surrogate Models ------------------------------- #
181 m_forward = ForwardMappingModel(
182  _id='FoamElasticModulus',
183  surrogateFunction=f_ludwigshafen,
184  substituteModels= [ ],
185  parameters=[0, 1],# TODO: Use real values from Ludwigshafen presentation
186 )
187 
188 
189 m_backward = BackwardMappingModel(
190  _id= 'FoamElasticModulus_TEST',
191  surrogateFunction= f_backward,
192  exactTask=MechanicalPropertiesExactSim(),
193  substituteModels= [],
194  initialisationStrategy= Strategy.InitialData(
195  initialData={
196  'rho': [30.2,60,120,240,30.2,60,120,240,30.2,30.2,30.2,30.2,30.2,30.2,30.2],
197  'strut_content': [0.4,0.4,0.4,0.4,0.8,0.8,0.8,0.8,0.85,0.85,0.85,0.85,0.85,0.85,0.85],
198  'Mu': [0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39],
199  'Sigma': [0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.01,0.05,0.1,0.15,0.2,0.25,0.30],
200  'E': [8.16,22.85,52.89,124.73,6.53,13.06,30.36,65.63,4.97,5.48,5.42,4.52,4.61,4.42,4.79],
201  }
202  ),
203  outOfBoundsStrategy= Strategy.ExtendSpaceStochasticSampling(
204  nNewPoints= 4
205  ),
206  parameterFittingStrategy= Strategy.NonLinFitWithErrorContol(
207  testDataPercentage= 0.2,
208  maxError= 9e99,
209  improveErrorStrategy= Strategy.StochasticSampling(
210  nNewPoints= 2
211  ),
212  maxIterations= 5 # Currently not used
213  ),
214 )
215