MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
foamConductivity.py
Go to the documentation of this file.
1 
31 
32 
58 
59 import os
60 from modena import *
61 import modena.Strategy as Strategy
62 from fireworks.utilities.fw_utilities import explicit_serialize
63 from jinja2 import Template
64 import json
65 import polymerConductivity
66 import gasConductivity
67 import gasMixtureConductivity
68 import json
69 
70 json.encoder.FLOAT_REPR = lambda o: format(o, '.12g')
71 
72 @explicit_serialize
73 
90 class FoamConductivityExactTask(ModenaFireTask):
91 
95  def task(self, fw_spec):
96  eps = self['point']['eps']
97  dcell = self['point']['dcell']
98  fstrut = self['point']['fstrut']
99  temp = self['point']['T']
100  xCO2 = self['point']['x[CO2]']
101  xCyP = self['point']['x[CyP]']
102  xO2 = self['point']['x[O2]']
103  xN2 = self['point']['x[N2]']
104  # Write input
105  inputs={"upperBoundary": {"temperature": temp+1,"emittance": 0.9}}
106  inputs["lowerBoundary"]={"temperature": temp-1,"emittance": 0.9}
107  inputs["gasComposition"]={
108  "O2": xO2,
109  "N2": xN2,
110  "CO2": xCO2,
111  "Cyclopentane": xCyP
112  }
113  inputs["gasDensity"]=1.2
114  inputs["solidDensity"]=1.1e3
115  inputs["sourceOfProperty"]={
116  "porosity": "DirectInput",
117  "cellSize": "DirectInput",
118  "gasComposition": "DirectInput",
119  "strutContent": "DirectInput",
120  "wallThickness": "DirectInput"
121  }
122  inputs["porosity"]=eps
123  inputs["cellSize"]=dcell
124  inputs["morphologyInput"]="strutContent"
125  inputs["wallThickness"]=0.5e-6
126  inputs["strutContent"]=fstrut
127  inputs["strutSize"]=1e-6
128  inputs["foamThickness"]=3e-2
129  inputs["spatialDiscretization"]=200
130  inputs["useWallThicknessDistribution"]=True
131  inputs["wallThicknessStandardDeviation"]=0.2
132  inputs["numberOfGrayBoxes"]=10
133  inputs["numericalEffectiveConductivity"]=False
134  # inputs["structureName"]=
135  inputs["testMode"]=False
136  with open('foamConductivity.json','w') as f:
137  json.dump(inputs, f, indent=4)
138  os.mkdir('inputs')
139  os.rename('foamConductivity.json','inputs/foamConductivity.json')
140  # Execute the detailed model
141  # path to **this** file + /src/...
142  # will break if distributed computing
143  ret = os.system(os.path.dirname(os.path.abspath(__file__))+'/src/kfoam')
144  # This call enables backward mapping capabilities
145  self.handleReturnCode(ret)
146  # Analyse output
147  # os.getcwd() returns the path to the "launcher" directory
148  try:
149  FILE = open(os.getcwd()+'/foamConductivity.out','r') except IOError:
150  raise IOError("File not found")
151 
152  self['point']['kfoam'] = float(FILE.readline())
153 
154  # os.remove('foamConductivity.json')
155  # os.remove('foamConductivity.out')
156 
157 
161 f_foamConductivity = CFunction(
162  Ccode='''
163 #include "modena.h"
164 #include "math.h"
165 
166 void tcfoam_SM
167 (
168  const modena_model_t* model,
169  const double* inputs,
170  double *outputs
171 )
172 {
173  {% block variables %}{% endblock %}
174 
175  const double alpha = parameters[1];
176  const double beta = parameters[0];
177 
178  const double sigma=5.67e-8;
179 
180  double fs,Xs,Xw,X,kappa,kr;
181  double kfoam;
182  double kgas=gasMixtureConductivity;
183  double kpol=polymer_thermal_conductivity;
184 
185  fs=alpha*fstrut;
186  Xs=(1+4*kgas/(kgas+kpol))/3.0;
187  Xw=2*(1+kgas/(2*kpol))/3.0;
188  X=(1-fs)*Xw+fs*Xs;
189  kappa=4.09*sqrt(1-eps)/dcell;
190  kr=16*sigma*pow(T,3)/(3*kappa);
191  kfoam = (kgas*eps+kpol*X*(1-eps))/(eps+(1-eps)*X)+beta*kr;
192 
193  outputs[0] = kfoam;
194 }
195 ''',
196  # These are global bounds for the function
197  inputs={
198  'eps': {'min': 0, 'max': 0.995},
199  'dcell': {'min': 0, 'max': 1e-1},
200  'fstrut': {'min': 0, 'max': 1},
201  'gasMixtureConductivity': {'min': 0, 'max': 1e-1},
202  'polymer_thermal_conductivity': {'min': 0, 'max': 1e0},
203  'T': {'min': 273, 'max': 550},
204  'x': {'index': gasConductivity.species, 'min': 0, 'max': 1},
205  },
206  outputs={
207  'kfoam': {'min': 0, 'max': 1e0, 'argPos': 0},
208  },
209  parameters={
210  'param1': {'min': -1e9, 'max': 1e9 + 2, 'argPos': 0},
211  'param2': {'min': -1e9, 'max': 1e9 + 2, 'argPos': 1},
212  },
213 )
214 
215 try: #initialization by initModels
216  with open("./inputs/init_foamConductivity.json") as fl:
217  foaming_ini=json.load(fl)
218 except IOError: #automatic initialization
219  with open("../inputs/init_foamConductivity.json") as fl:
220  foaming_ini=json.load(fl)
221 
222 
226 m_foamConductivity = BackwardMappingModel(
227  _id='foamConductivity',
228  surrogateFunction=f_foamConductivity,
229  exactTask=FoamConductivityExactTask(),
230  substituteModels=[
231  gasMixtureConductivity.m_gasMixtureConductivity,\
232  polymerConductivity.m_polymer_thermal_conductivity\
233  ],
234  initialisationStrategy=Strategy.InitialPoints(
235  initialPoints=foaming_ini,
236  ),
237  outOfBoundsStrategy=Strategy.ExtendSpaceStochasticSampling(
238  nNewPoints=4
239  ),
240  parameterFittingStrategy=Strategy.NonLinFitWithErrorContol(
241  testDataPercentage=0.2,
242  maxError=0.01,
243  improveErrorStrategy=Strategy.StochasticSampling(
244  nNewPoints=2
245  ),
246  maxIterations=5 # Currently not used
247  ),
248 )
249 
250 
254 m_simulation = Strategy.BackwardMappingScriptTask(
255  script=os.path.dirname(os.path.abspath(__file__))+'/src/kfoam' +
256  ' && cp foamConductivity.out ../results/foamConductivity' +
257  ' && cp hahtf.out ../results/foamConductivity'
258 )
259 
Recipe for Foam conductivity application.