MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
prep_init_foamConductivity.py
1 #!/usr/bin/env python
2 import sys
3 import os
4 import json
5 from numpy import loadtxt
6 from modena import SurrogateModel
7 def my_help():
8  "print usage of the module"
9  print('Usage: prep_init_foamConductivity v')
10  print('v: integer')
11  print("0 - try to initialize with points so that foamConductivity "+
12  "does not go out of range")
13  print('1 - initialize with points from foamAging.json')
14  print('2 - initialize with points from foamAging.json except for gas '
15  +'composition - foamConductivity should not go out of range in '
16  +'foamAging simulation')
17  return
18 
23 def no_refitting():
24  ini={
25  "eps": [0.9,0.0,0.96,0.99,0.7,0.5],
26  "dcell": [200e-6,0.0,300e-6,100e-6,1e-2,200e-6],
27  "fstrut": [0.0,1.0,0.7,0.6,0.0,0.9],
28  "T": [280,549,300,350,330,300],
29  "x[CO2]": [1,0,0,0,1,0],
30  "x[CyP]": [0,1,0,0,0,1],
31  "x[O2]": [0,0,1,0,0,0],
32  "x[N2]": [0,0,0,1,0,0]
33  }
34  with open("./inputs/init_foamConductivity.json","w") as fl:
35  json.dump(ini,fl,indent=4,sort_keys=True)
36  return
37 def setIP(a0):
38  "creates a list with small perturbation around the input value"
39  a=[]
40  for i in xrange(4):
41  a.append(a0)
42  upar=1-1e-4
43  opar=1+1e-4
44  a[0]=a[0]*upar
45  if a0==0:
46  a[1]=1e-4
47  else:
48  a[1]=a[1]*opar
49  return a
50 def setIP_fractions(a0):
51  "creates a list with small perturbation around the input value"
52  a=[]
53  for i in xrange(4):
54  a.append(a0)
55  upar=1-1e-4
56  opar=1+1e-4
57  if a0>1:
58  a0=1.0
59  a[0]=a[0]*upar
60  if a0==0:
61  a[1]=1e-4
62  else:
63  a[1]=a[1]*opar
64  if a[1]>1:
65  a[1]=1.0
66  return a
67 def get_rho(inputs, results):
68  if inputs['sourceOfProperty']['foamDensity']=='DirectInput':
69  rho0=inputs['morphology']['foamDensity']
70  elif inputs['sourceOfProperty']['foamDensity']=='BubbleGrowth':
71  with open(results+"/bubbleGrowth/after_foaming.txt") as fl2:
72  rho0,dcell,xCyP,xCO2=loadtxt(fl2,skiprows=1,unpack=True)
73  elif inputs['sourceOfProperty']['foamDensity']=='Qmom0D':
74  with open(results+"/CFD0D/after_foaming.txt") as fl2:
75  rho0,dcell,var,xCyP,xCO2=loadtxt(fl2,skiprows=1,unpack=True)
76  elif inputs['sourceOfProperty']['foamDensity']=='Qmom3D':
77  with open(results+"/CFD3D/after_foaming.txt") as fl2:
78  rho0,dcell,xCyP,xCO2=loadtxt(fl2,skiprows=1,unpack=True)
79  else:
80  raise Exception("unknown source for foam density")
81  return rho0
82 def get_dcell(inputs, results):
83  if inputs['sourceOfProperty']['cellSize']=='DirectInput':
84  dcell0=inputs['morphology']['cellSize']
85  elif inputs['sourceOfProperty']['cellSize']=='BubbleGrowth':
86  with open(results+"/bubbleGrowth/after_foaming.txt") as fl2:
87  rho,dcell0,xCyP,xCO2=loadtxt(fl2,skiprows=1,unpack=True)
88  elif inputs['sourceOfProperty']['cellSize']=='Qmom0D':
89  with open(results+"/CFD0D/after_foaming.txt") as fl2:
90  rho,dcell0,var,xCyP,xCO2=loadtxt(fl2,skiprows=1,unpack=True)
91  elif inputs['sourceOfProperty']['cellSize']=='Qmom3D':
92  with open(results+"/CFD3D/after_foaming.txt") as fl2:
93  rho,dcell0,xCyP,xCO2=loadtxt(fl2,skiprows=1,unpack=True)
94  else:
95  raise Exception("unknown source for cell size")
96  return dcell0
97 def get_gas_comp(inputs, results):
98  if inputs['sourceOfProperty']['gasComposition']=='DirectInput':
99  xO20=inputs['foamCondition']['initialComposition']['O2']
100  xN20=inputs['foamCondition']['initialComposition']['N2']
101  xCO20=inputs['foamCondition']['initialComposition']['CO2']
102  xCyP0=inputs['foamCondition']['initialComposition']['Cyclopentane']
103  elif inputs['sourceOfProperty']['gasComposition']=='BubbleGrowth':
104  with open(results+"/bubbleGrowth/after_foaming.txt") as fl2:
105  rho,dcell,xCyP0,xCO20=loadtxt(fl2,skiprows=1,unpack=True)
106  xO20=xN20=0
107  elif inputs['sourceOfProperty']['gasComposition']=='Qmom0D':
108  with open(results+"/CFD0D/after_foaming.txt") as fl2:
109  rho,dcell,var,xCyP0,xCO20=loadtxt(fl2,skiprows=1,unpack=True)
110  xO20=xN20=0
111  elif inputs['sourceOfProperty']['gasComposition']=='Qmom3D':
112  with open(results+"/CFD3D/after_foaming.txt") as fl2:
113  rho,dcell,xCyP0,xCO20=loadtxt(fl2,skiprows=1,unpack=True)
114  xO20=xN20=0
115  else:
116  raise Exception("unknown source for gas composition")
117  return xO20,xN20,xCO20,xCyP0
118 def get_fstrut(inputs, results):
119  if inputs['sourceOfProperty']['strutContent']=='DirectInput':
120  fstrut0=inputs['morphology']['strutContent']
121  elif inputs['sourceOfProperty']['strutContent']=='StrutContent':
122  inputs = {'rho': rho0}
123  model=SurrogateModel.load('strutContent')
124  outputs=model.callModel(inputs)
125  fstrut0=outputs['fs']
126  else:
127  raise Exception("unknown source for strut content")
128  return fstrut0
129 def foamAging(flag):
130  "create initial points from foamAging.json"
131  if os.getcwd().split(os.path.sep)[-1] != 'foamAging':
132  raise Exception("you can initialize for foamAging only from foamAging "
133  +"folder")
134  results='../foamExpansion/results/'
135  with open("./inputs/foamAging.json") as fl:
136  inputs=json.load(fl)
137  T0=inputs['foamCondition']['conductivityTemperature']
138  rhop=inputs['physicalProperties']['polymerDensity']
139  rho0=get_rho(inputs, results)
140  dcell0=get_dcell(inputs, results)
141  xO20,xN20,xCO20,xCyP0=get_gas_comp(inputs, results)
142  fstrut0=get_fstrut(inputs, results)
143  s=xO20+xN20+xCO20+xCyP0
144  xO20=xO20/s
145  xN20=xN20/s
146  xCO20=xCO20/s
147  xCyP0=xCyP0/s
148  eps0=1-rho0/rhop
149  if flag==1:
150  ini = {
151  'eps': setIP(eps0),
152  'dcell': setIP(dcell0),
153  'fstrut': setIP(fstrut0),
154  'T': setIP(T0),
155  'x[CO2]': setIP_fractions(xCO20),
156  'x[CyP]': setIP_fractions(xCyP0),
157  'x[O2]': setIP_fractions(xO20),
158  'x[N2]': setIP_fractions(xN20),
159  }
160  elif flag==2:
161  ini = {
162  'eps': setIP(eps0),
163  'dcell': setIP(dcell0),
164  'fstrut': setIP(fstrut0),
165  'T': setIP(T0),
166  'x[CO2]': [1,0,0,0],
167  'x[CyP]': [0,1,0,0],
168  'x[O2]': [0,0,1,0],
169  'x[N2]': [0,0,0,1]
170  }
171  with open("./inputs/init_foamConductivity.json","w") as fl:
172  json.dump(ini,fl,indent=4,sort_keys=True)
173  return
174 if __name__=='__main__':
175  n=len(sys.argv)
176  if n==2:
177  if sys.argv[1]=='0':
178  no_refitting()
179  elif sys.argv[1]=='1':
180  foamAging(1)
181  elif sys.argv[1]=='2':
182  foamAging(2)
183  else:
184  my_help()
185  else:
186  my_help()
MoDeNa Module definition of the Foam aging model.
Definition: __init__.py:1