-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathTestBuilder.py
233 lines (195 loc) · 11.1 KB
/
TestBuilder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# ***************************************************************************
# * *
# * Copyright (c) 2016 - Qingfeng Xia <qingfeng.xia iesensor.com> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
from __future__ import print_function, absolute_import
import sys
import os
import tempfile
import platform
import os.path
PACKAGE_PARENT = '../..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
import FoamCaseBuilder
from FoamCaseBuilder.utility import runFoamApplication, runFoamCommand, getFoamDir, createRawFoamFile
from FoamCaseBuilder.utility import ParsedParameterFile, BoundaryDict
from FoamCaseBuilder.BasicBuilder import getDefaultSolverSettings, BasicBuilder
from FoamCaseBuilder.ThermalBuilder import getDefaultHeatTransferSolverSettings, ThermalBuilder
script_path = os.path.dirname(os.path.abspath( __file__ ))
case_dir = '/tmp' # to put test file
mesh_file = script_path + os.path.sep +'TestCase.unv' #script path may not writable
if not os.path.exists(mesh_file):
mesh_file_url = "https://www.iesensor.com/download/TestCase.unv"
print('using curl to download mesh file first, it will fail if curl is abscent')
print('you need to run this script twice to get the case built. \n reason unknown, could be mesh download not finished during building')
runFoamApplication(['curl -o {} {}'.format(mesh_file, mesh_file_url)])
def test_runFoamApplication():
print("getFoamDir() = ", getFoamDir())
runFoamApplication(["icoFoam", '-help'])
def test_dictFileLoad(case):
#PyFoam 0.66 can parse dict file for OF>=3.0, with "#include file"
#case= getFoamDir() + "/tutorials/incompressible/simpleFoam/pipeCyclic"
file="0/U"
f = ParsedParameterFile(case + os.path.sep + file)
print(f['boundaryField'])
print(f['internalField'])
def test_boundaryDictLoad(case):
#PyFoam 0.66 can parse dict file for OF>=3.0, with "#include file"
f = BoundaryDict(case)
print(f.patches())
print(f[f.patches()[0]])
def test_createRawFoamFile(case):
_constant_dir_created = False
if not os.path.exists(case + os.path.sep + "constant"):
_constant_dir_created = True
os.mkdir(case + os.path.sep + "constant")
lines = ['transportModel Newtonian;','\n', 'nu [ 0 2 -1 0 0 0 0 ] 1e-06;']
createRawFoamFile(case, "constant", "transportProperties", lines)
file = case + os.path.sep + "constant" + os.path.sep + "transportProperties" #PyFoam 0.6.6 can not parse this dict for OpenFOAM 3.0+
f = ParsedParameterFile(file)
print(f['nu'])
os.remove(file)
if _constant_dir_created:
os.rmdir(case + os.path.sep + "constant")
def test_basic_builder(using_laminar_model = True):
"""assuming mesh has generated with patches name same as bc_names list
This is only a test, it will not generate accurate result due to low mesh quality
PyFoam/examples/CaseBuilder/lesSnappy.pfcb
"""
using_pressure_inlet = False # only for compressible fluid case, not implemented yet
# case setup is fine for kEpsilon, but will diverge!
solver_settings = getDefaultSolverSettings()
case= case_dir + os.path.sep + "TestCaseCLI"
#zipped_template_file is deprecated
#default to setup in ~/.bashrc: source '/opt/openfoam40/etc/bashrc'
#but FOAM_DIR can be setup before run any command
#setFoamDir('/home/qingfeng/OpenFOAM/OpenFOAM-2.1.x/')
#setFoamVersion((2,1,0))
#print(FOAM_SETTINGS['FOAM_VERSION'])
#tutorial_path = "tutorials/incompressible/simpleFoam/pipeCyclic"
tutorial_path = None # build up case from scratch
template_path = tutorial_path
#pipe diameter is 20mm
if using_laminar_model:
turbulenceSettings={'name': "laminar"}
inletVelocity = (0, 0, 1)
kinematicViscosity = 1
else:
#"Intensity&LengthScale","unspecific","Intensity&HydraulicDiameter"
turbulenceSettings={'name': "realizableKE", 'specification':"Intensity&HydraulicDiameter",
'intensityValue': 0.05, 'lengthValue': 0.01}
solver_settings['turbulenceModel'] = "realizableKE"
# 'kEpsilon' case setup is fine, but divergent is hard for he bad mesh quality
inletVelocity = (0, 0, 0.1)
kinematicViscosity = 0.000001
transientSettings = {"startTime":0.0, "endTime":1.0, "timeStep":0.001, "writeInterval":100}
# compressible flow only, currently not supported yet!
inlet={'name': "Inlet", 'type': "inlet", 'subtype': "totalPressure", 'value': 10000,
'turbulenceSettings': turbulenceSettings}
inlet={'name': "Inlet", 'type': "inlet", 'subtype': "uniformVelocity", 'value': inletVelocity,
'turbulenceSettings': turbulenceSettings}
#inlet={'name': "Inlet", 'type': "inlet", 'subtype': "volumetricFlowRate", 'value': 0.0001, 'turbulenceSettings': turbulenceSettings}
outlet={'name': "Outlet", 'type': "outlet", 'subtype': "staticPressure", 'value': 0.0}
#outlet={'name': "Outlet", 'type': "outlet", 'subtype': "outFlow", 'value': 0.0}
case_builder = BasicBuilder(case, solver_settings, template_path)
case_builder.createCase() # clear case, clear data
case_builder.setupMesh(mesh_file, 0.001)
case_builder.turbulenceProperties = turbulenceSettings
#print case_builder.fluidProperties
case_builder.fluidProperties = {'name':'oneLiquid', 'kinematicViscosity': kinematicViscosity}
#higher viscosity may help converge in coarse mesh
case_builder.boundaryConditions = [inlet, outlet]
case_builder.internalFields = {'U': inletVelocity}
case_builder.build()
case_builder.setupRelaxationFactors(0.1, 0.1, 0.3) # changed the default setting, not necessary
case_builder.setupPressureReference(0.0, 0) # pRefValue, pRefValue
msg = case_builder.check()
if msg:
print('Error: case setup check failed with message\n, {}, \n please check dict files'.format(msg))
#fvScheme file: divSchemes, fvSolution file has turbulence model specific var setting up
case_builder.setupRelaxationFactors(0.1, 0.1) # reduce for the coarse 3D mesh
# todo: residual plot
cmdline = "simpleFoam -case {} > log.{}".format(case, case_builder._solverName) # foamJob <solver> & foamLog
print("please run the command in new terminal: \n"+ cmdline)
#lauch command outside please, it takes several minutes to converge
runFoamApplication(cmdline)
#pyFoamPlotWatcher.py
cmdline = "paraFoam -case {}".format(case)
print("view result in command with: \n"+ cmdline)
return case
def test_heat_transfer_builder(compressible=True):
# getTestMesh
#from ThermalBuilder import getDefaultHeatTransferSolverSettings
solver_settings = getDefaultHeatTransferSolverSettings()
solver_settings['compressible'] = compressible
case= case_dir + os.path.sep + "TestCaseHT"
#template_path = "tutorials/heatTransfer/buoyantBoussinesqSimpleFoam/hotRoom/"
template_path = None
using_laminar = False
if using_laminar: # buoyantBoussinesqSimpleFoam solver does not support laminar flow
turbulenceSettings={'name': 'laminar'}
solver_settings['turbulenceModel'] = 'laminar'
inletVelocity = (0, 0, 0.02)
inletPressure = 10100
else:
turbulenceSettings={'name': solver_settings['turbulenceModel'], 'specification':"intensity&HydraulicDiameter",
'intensityValue': 0.05, 'lengthValue': 0.01}
inletVelocity = (0, 0, 1.0)
inletPressure = 11000
inlet={'name': "Inlet", 'type': "inlet", 'subtype': "totalPressure", 'value': inletPressure,
'turbulenceSettings': turbulenceSettings,
'thermalSettings': {'subtype':'fixedValue', 'temperature':320}}
inlet={'name': "Inlet", 'type': "inlet", 'subtype': "uniformVelocity", 'value': inletVelocity,
'turbulenceSettings': turbulenceSettings,
'thermalSettings': {'subtype':'fixedValue', 'temperature':320}}
outlet={'name': "Outlet", 'type': "outlet", 'subtype': "staticPressure", 'value': 1e5,
#turbulenceSettings is only necessary for inlet
'thermalSettings': {'subtype':'fixedValue', 'temperature':300}} # subtype will be overwritten
wall={'name': "defaultFaces", 'type': "wall",'subtype': "fixed", # faces without allocated boundary condition setting
#'thermalSettings': {'subtype':'heatFlux', 'heatFlux':100}}
'thermalSettings': {'subtype':'fixedGradient', 'heatFlux':10000}}
#from ThermalBuilder import ThermalBuilder
case_builder = ThermalBuilder(case, solver_settings, template_path)
case_builder.createCase()
case_builder.setupMesh(mesh_file, 0.001)
case_builder.turbulenceProperties = turbulenceSettings
#print case_builder.fluidProperties
case_builder.fluidProperties = {'name':'air'}
#higher viscosity may help converge
case_builder.boundaryConditions = [inlet, outlet, wall]
case_builder.internalFields = {'U': inletVelocity, 'T': 300, 'p': 100000}
case_builder.build()
msg = case_builder.check()
if msg:
print('Error: case setup check failed with message\n, {}, \n please check dict files'.format(msg))
case_builder.summarize()
return case
if __name__ == '__main__':
if platform.system() == 'Windows':
print("Test on Windows is not support, mainly because case path not translated, try python on Windows Linux System")
else:
#test_runFoamApplication()
case = test_basic_builder(using_laminar_model = True)
test_dictFileLoad(case )
test_boundaryDictLoad(case )
test_createRawFoamFile(case )
#test_basic_builder(using_laminar_model = False)
#test_heat_transfer_builder()