MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
run.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
18 from __future__ import division
19 import os
20 import sys
21 from blessings import Terminal
22 import json
23 import datetime
24 from scipy.optimize import minimize_scalar as minimize_scalar
25 import FoamGeometryConstruction_Periodic
26 import periodicBox
27 import vtkconv
28 
29 dx=dy=dz=1
30 
31 dedge=6
32 
33 with open('input.json') as data_file:
34 
35  data = json.load(data_file)
36 locals().update(data) # Creates variables from dictionary
37 
38 filenameBox=filename+"Box"
39 
40 filenameBoxAscii = filenameBox+"-ascii"
41 
42 filenameBoxStruts = filenameBox+"Struts"
43 
44 filenameDescriptors = "descriptors.out"
45 
46 filenameParameters = "parameters.out"
47 
48 term = Terminal()
49 
55 def porOpt(vx):
56  vx=int(vx)
57  vy=vx
58  vz=vx
59  if os.path.isfile(filenameBox+'.vtk'):
60  os.remove(filenameBox+'.vtk')
61  if not os.path.isfile(filenameBox+'.ply'):
62  raise SystemError(".ply file is missing. Nothing to binarize.")
63  os.system(
64  "binvox -e -d {0:d} -rotz -rotx -rotz -rotz -t vtk ".format(vx)
65  +filenameBox+".ply >binvox.out"
66  )
67  with open('binvox.out') as data_file:
68  for line in data_file:
69  if "counted" in line:
70  solidVoxel,totalVoxel=\
71  [int(s) for s in line.split() if s.isdigit()]
72  eps=1-solidVoxel/totalVoxel
73  print "dimension: {0:4d}, porosity: {1:f}".format(vx,eps)
74  return (eps-porosity)**2
75 
82 def porfsOpt(x):
83  global dedge
84  vx=int(x)
85  vy=vx
86  vz=vx
87  if os.path.isfile(filenameBox+'.vtk'):
88  os.remove(filenameBox+'.vtk')
89  if not os.path.isfile(filenameBox+'.ply'):
90  raise SystemError(".ply file is missing. Nothing to binarize.")
91  os.system(
92  "binvox -e -d {0:d} -rotz -rotx -rotz -rotz -t vtk ".format(vx)
93  +filenameBox+".ply >binvox.out"
94  )
95  filenameIn = filenameBox+".vtk"
96  filenameOut = filenameBoxAscii+".vtk"
97  origin=[dx,dy,dz]
98  spacing=[dx/vx,dy/vy,dz/vz]
99  vtkconv.main(filenameIn,filenameOut,origin,spacing)
100  f=open("foamreconstr.in","w")
101  f.write("0\n")
102  f.write("1\n")
103  f.write("0\n")
104  f.write("0\n")
105  f.write("0\n")
106  f.write("0\n")
107  f.write("{0:f}\n".format(dedge))
108  f.write("{0:f}\n".format(1-strutContent*(1-porosity)))
109  f.write("0\n")
110  f.write("1\n")
111  f.write("0\n")
112  f.write("0\n")
113  f.write("0\n")
114  f.write("0\n")
115  f.write("0\n")
116  f.write("0\n")
117  f.write("1\n")
118  f.write("0\n")
119  f.write("1\n")
120  f.write("0\n")
121  f.write(filenameBoxStruts+"\n")
122  f.write(filenameBoxAscii+".vtk\n")
123  f.write(filename+".gnu\n")
124  f.write("name\n")
125  f.write(filenameDescriptors+"\n")
126  f.write(filenameParameters+"\n")
127  f.close()
128  os.system("./foamreconstr/foamreconstr")
129  f=open(filenameDescriptors,"r")
130  eps=float(f.readline())
131  fs=float(f.readline())
132  f.close()
133  f=open(filenameParameters,"r")
134  dedge=float(f.readline())
135  f.close()
136  resid=((eps-porosity)/porosity)**2
137  print "dimension: {0:4d}, porosity: {1:f}".format(vx,eps)+\
138  ", strut content: {0:f}".format(fs)
139  return resid
140 
141 
146 def main():
147  ts = datetime.datetime.now()
148 
149  print(
150  term.yellow +
151  "Create periodic RVE of foam" +
152  term.normal
153  )
154  FoamGeometryConstruction_Periodic.main(
155  MU,SIGMA,NumOfCells,filename,packing,
156  alternativePackingAlgorithm,tesselation,visualizeTesselation,geometry,
157  statistics,hypermesh,deleteFiles,dx,dy,dz
158  )
159  if moveToPeriodicBox:
160 
161  print(
162  term.yellow +
163  "Convert .geo to .stl" +
164  term.normal
165  )
166  os.system("gmsh -n -2 -format stl "+filename+".geo >gmsh.out")
167  if deleteFiles:
168  os.remove("gmsh.out")
169 
170  print(
171  term.yellow +
172  "Move to periodic box" +
173  term.normal
174  )
175  filenameIn = filename+".stl"
176  filenameOut = filenameBox+".stl"
177  xmin=dx
178  ymin=dy
179  zmin=dz
180  periodicBox.main(
181  filenameIn,filenameOut,xmin,ymin,zmin,dx,dy,dz,renderBox
182  )
183  if deleteFiles:
184  os.remove(filenameIn)
185 
186  print(
187  term.yellow +
188  "Convert .stl to .ply" +
189  term.normal
190  )
191  os.system("meshconv "+filenameBox+".stl -c ply")
192  if deleteFiles:
193  os.remove(filenameBox+'.stl')
194  if binarizeBox:
195 
196  if strutContent==0:
197  # Find the size of box, which would give desired porosity
198  # This method is not optimal, since the solver doesn't know that the
199  # function takes only integer arguments
200  print(
201  term.yellow +
202  "Optimizing porosity" +
203  term.normal
204  )
205  res=minimize_scalar(
206  porOpt,bracket=[100,120],method='Brent',tol=1e-2
207  )
208  vx=res.x
209  vx=int(vx)
210  print 'box size: {0:d}'.format(vx)
211  vy=vx
212  vz=vx
213  print(
214  term.yellow +
215  "Creating and saving optimal foam" +
216  term.normal
217  )
218  porOpt(vx) # Call it with the optimized box size
219  if deleteFiles:
220  os.remove("binvox.out")
221  os.remove(filename+".ply")
222 
223  print(
224  term.yellow +
225  "Convert binary .vtk to ascii .vtk" +
226  term.normal
227  )
228  filenameIn = filenameBox+".vtk"
229  filenameOut = filenameBoxAscii+".vtk"
230  origin=[dx,dy,dz]
231  spacing=[dx/vx,dy/vy,dz/vz]
232  vtkconv.main(filenameIn,filenameOut,origin,spacing)
233  if deleteFiles:
234  os.remove(filenameIn)
235  else:
236  print(
237  term.yellow +
238  "Optimizing porosity and strut content" +
239  term.normal
240  )
241  res=minimize_scalar(
242  porfsOpt,bracket=[150,200],method='Brent',tol=1e-2
243  )
244  # res=minimize_scalar(
245  # porfsOpt,bounds=[200,250],method='bounded',tol=2e0
246  # )
247  vx=res.x
248  vx=int(vx)
249  print 'optimal box size: {0:d}'.format(vx)
250  vy=vx
251  vz=vx
252  print(
253  term.yellow +
254  "Creating and saving optimal foam" +
255  term.normal
256  )
257  if os.path.isfile(filenameBox+'.vtk'):
258  os.remove(filenameBox+'.vtk')
259  if not os.path.isfile(filenameBox+'.ply'):
260  raise SystemError(".ply file is missing. Nothing to binarize.")
261  os.system(
262  "binvox -e -d {0:d}".format(vx)+" -rotz -rotx -rotz -rotz "
263  +"-t vtk "+filenameBox+".ply >binvox.out"
264  )
265  filenameIn = filenameBox+".vtk"
266  filenameOut = filenameBoxAscii+".vtk"
267  origin=[dx,dy,dz]
268  spacing=[dx/vx,dy/vy,dz/vz]
269  vtkconv.main(filenameIn,filenameOut,origin,spacing)
270  f=open("foamreconstr.in","w")
271  f.write("0\n")
272  f.write("1\n")
273  f.write("0\n")
274  f.write("0\n")
275  f.write("1\n")
276  f.write("0\n")
277  f.write("{0:f}\n".format(dedge))
278  f.write("{0:f}\n".format(1-strutContent*(1-porosity)))
279  f.write("0\n")
280  f.write("1\n")
281  f.write("0\n")
282  f.write("0\n")
283  f.write("0\n")
284  f.write("0\n")
285  f.write("0\n")
286  f.write("0\n")
287  f.write("1\n")
288  f.write("0\n")
289  f.write("1\n")
290  f.write("0\n")
291  f.write(filenameBoxStruts+"\n")
292  f.write(filenameBoxAscii+".vtk\n")
293  f.write(filename+".gnu\n")
294  f.write("name\n")
295  f.write(filenameDescriptors+"\n")
296  f.write(filenameParameters+"\n")
297  f.close()
298  os.system("./foamreconstr/foamreconstr")
299  if deleteFiles:
300  os.remove(filenameBoxAscii+".vtk")
301  os.remove(filenameDescriptors)
302  os.remove(filenameParameters)
303  os.remove("binvox.out")
304  os.remove(filenameBox+".vtk")
305  os.remove("foamreconstr.in")
306  tf = datetime.datetime.now()
307  te = tf - ts
308  print "Foam created in: ",te
309 
310 if __name__ == '__main__':
311  main()
def main()
Main function.
Definition: run.py:146
def porfsOpt(x)
Objective function.
Definition: run.py:82
def porOpt(vx)
Objective function.
Definition: run.py:55