18 from __future__
import division
21 from blessings
import Terminal
24 from scipy.optimize
import minimize_scalar
as minimize_scalar
25 import FoamGeometryConstruction_Periodic
33 with open(
'input.json')
as data_file:
35 data = json.load(data_file)
38 filenameBox=filename+
"Box" 40 filenameBoxAscii = filenameBox+
"-ascii" 42 filenameBoxStruts = filenameBox+
"Struts" 44 filenameDescriptors =
"descriptors.out" 46 filenameParameters =
"parameters.out" 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.")
64 "binvox -e -d {0:d} -rotz -rotx -rotz -rotz -t vtk ".format(vx)
65 +filenameBox+
".ply >binvox.out" 67 with open(
'binvox.out')
as data_file:
68 for line
in data_file:
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
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.")
92 "binvox -e -d {0:d} -rotz -rotx -rotz -rotz -t vtk ".format(vx)
93 +filenameBox+
".ply >binvox.out" 95 filenameIn = filenameBox+
".vtk" 96 filenameOut = filenameBoxAscii+
".vtk" 98 spacing=[dx/vx,dy/vy,dz/vz]
99 vtkconv.main(filenameIn,filenameOut,origin,spacing)
100 f=open(
"foamreconstr.in",
"w")
107 f.write(
"{0:f}\n".format(dedge))
108 f.write(
"{0:f}\n".format(1-strutContent*(1-porosity)))
121 f.write(filenameBoxStruts+
"\n")
122 f.write(filenameBoxAscii+
".vtk\n")
123 f.write(filename+
".gnu\n")
125 f.write(filenameDescriptors+
"\n")
126 f.write(filenameParameters+
"\n")
128 os.system(
"./foamreconstr/foamreconstr")
129 f=open(filenameDescriptors,
"r") 130 eps=float(f.readline()) 131 fs=float(f.readline()) 133 f=open(filenameParameters,"r") 134 dedge=float(f.readline()) 136 resid=((eps-porosity)/porosity)**2 137 print "dimension: {0:4d}, porosity: {1:f}".format(vx,eps)+\
138 ", strut content: {0:f}".format(fs)
147 ts = datetime.datetime.now()
151 "Create periodic RVE of foam" +
154 FoamGeometryConstruction_Periodic.main(
155 MU,SIGMA,NumOfCells,filename,packing,
156 alternativePackingAlgorithm,tesselation,visualizeTesselation,geometry,
157 statistics,hypermesh,deleteFiles,dx,dy,dz
159 if moveToPeriodicBox:
163 "Convert .geo to .stl" +
166 os.system(
"gmsh -n -2 -format stl "+filename+
".geo >gmsh.out")
168 os.remove(
"gmsh.out")
172 "Move to periodic box" +
175 filenameIn = filename+
".stl" 176 filenameOut = filenameBox+
".stl" 181 filenameIn,filenameOut,xmin,ymin,zmin,dx,dy,dz,renderBox
184 os.remove(filenameIn)
188 "Convert .stl to .ply" +
191 os.system(
"meshconv "+filenameBox+
".stl -c ply")
193 os.remove(filenameBox+
'.stl')
202 "Optimizing porosity" +
206 porOpt,bracket=[100,120],method=
'Brent',tol=1e-2
210 print 'box size: {0:d}'.format(vx)
215 "Creating and saving optimal foam" +
220 os.remove(
"binvox.out")
221 os.remove(filename+
".ply")
225 "Convert binary .vtk to ascii .vtk" +
228 filenameIn = filenameBox+
".vtk" 229 filenameOut = filenameBoxAscii+
".vtk" 231 spacing=[dx/vx,dy/vy,dz/vz]
232 vtkconv.main(filenameIn,filenameOut,origin,spacing)
234 os.remove(filenameIn)
238 "Optimizing porosity and strut content" +
242 porfsOpt,bracket=[150,200],method=
'Brent',tol=1e-2
249 print 'optimal box size: {0:d}'.format(vx)
254 "Creating and saving optimal foam" +
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.")
262 "binvox -e -d {0:d}".format(vx)+
" -rotz -rotx -rotz -rotz " 263 +
"-t vtk "+filenameBox+
".ply >binvox.out" 265 filenameIn = filenameBox+
".vtk" 266 filenameOut = filenameBoxAscii+
".vtk" 268 spacing=[dx/vx,dy/vy,dz/vz]
269 vtkconv.main(filenameIn,filenameOut,origin,spacing)
270 f=open(
"foamreconstr.in",
"w")
277 f.write(
"{0:f}\n".format(dedge))
278 f.write(
"{0:f}\n".format(1-strutContent*(1-porosity)))
291 f.write(filenameBoxStruts+
"\n")
292 f.write(filenameBoxAscii+
".vtk\n")
293 f.write(filename+
".gnu\n")
295 f.write(filenameDescriptors+
"\n")
296 f.write(filenameParameters+
"\n")
298 os.system(
"./foamreconstr/foamreconstr")
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()
308 print "Foam created in: ",te
310 if __name__ ==
'__main__':
def porfsOpt(x)
Objective function.
def porOpt(vx)
Objective function.