-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
210 lines (164 loc) · 6.73 KB
/
cli.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
#!/usr/bin/env python
from core import Solver
from benchmark import converge_150
import hdf5
import sys, os
from optparse import OptionParser
from scipy import misc
# Pretty self explanatory really . . .
parser = OptionParser()
parser.add_option( "-f", "--h5file", dest="filename",
help="hdf5 file to operate on. If given image files, it will create an h5 file in-place with the same filename other than the extension." )
parser.add_option( "-o", "--overwrite", dest="force",
action="store_true", default=False,
help="Run the simulation even if results exist in the h5." )
parser.add_option( "-d", "--h5dir", dest="directory",
help="gives a directory (recursive) to converge all files ending '.h5'. If given image files, it will create an h5 file in-place with the same filename other than the extension." )
parser.add_option( "-s", "--solver", dest="solver",
default="default",
help="Sets the matrix solver. Options are: spsolve (default), nobi, splu, ruge, bicgstab, trilinos" )
parser.add_option( "-v", "--verbose", dest="verb",
default=2,
help="set the outputting of the solver (0-3) 2 is default.")
parser.add_option( "-c", "--converge", type=float,
dest="converge", default = 1e-8,
help="Use the iterative solver(default). If convergence criteria is unspecified the default is 1e-8" )
parser.add_option( "-m", "--monolithic",
default = False,
dest="mono",
action="store_true",
help="Use the direct monolithic solve function" )
parser.add_option( "--random", dest="rand",
default=0, type=int,
help="Give a seed to randomize the order the files are converged in." )
parser.add_option( "-r", "--reverse", dest="reverse",
action="store_true", default=False,
help="Reverse the order the files are solved in." )
# Add the three simulation direction options
for sim_dir in ["x", "y", "z"]:
parser.add_option( "-%s" % sim_dir,"--%s_sim" % sim_dir,
default = False,
dest= "%s" % sim_dir,
action="store_true",
help="Converge and save a simulation in the %s direction" % sim_dir )
parser.add_option( "-t","--test",
default = False,
dest="test",
action="store_true",
help="Test all 255 configurations")
parser.add_option( "-b","--big",
default = False,
dest="bigmode",
action="store_true",
help="Enable Big Mode")
parser.add_option( "-n","--nobiot",
default = False,
dest="nobi",
action="store_true",
help="Disable Biot Number Acceleration")
(options, args) = parser.parse_args()
if options.test:
from unittest_validate import test_all
test_all(sol_method=options.solver)
sys.exit()
# One or the other or both . . . cmon guys
if options.filename == None and options.directory == None and not options.test:
parser.print_help()
raise ValueError("You must specify either a directory (-d) or file (-f).")
# you _can_ specify both a -d and -f!
targets = []
# Populate list of targets from -f
if options.filename != None:
targets.append(options.filename)
# Populate list of targets from -d
if options.directory != None:
for path, fold, fils in os.walk(options.directory):
for f in fils:
# skip svn folders.
if not ".svn" in path:
targets.append( os.path.join(path, f) )
targets.sort()
if options.rand:
import random
random.seed(options.rand)
random.shuffle(targets)
if options.reverse:
targets.reverse()
# Print the targets
print "The following files will be operated on:"
for f in targets:
print "\t", f
# Populate the various pressure drops we will use
print "Using %s solver." % options.solver
# For each file targeted . . .
for f in targets:
# Determine where to save (this accommodates h5's and image format)
# Saving to h5's
save_path = os.path.splitext(f)[0] + ".h5"
if hdf5.isHDF5File(f):
print "Loading", f, "as hdf5 file . . ."
# open the h5 and get the dimensionality
h5 = hdf5.openFile(f)
ndim = len(h5.root.geometry.S.shape)
h5.close()
else:
# Default to trying an image . . .
print f, "is not and HDF5 file . . . trying image?"
S = 1. * ( misc.imread(f, flatten=True) < (255/2.) )
# Appropriate image transformation to make x/y convention meaningful
S = S[::-1].T
ndim = 2
# Make a h5 file for the results
hdf5.write_S(save_path, S)
# Get the domain dimensionality
print "Dimension:", ndim
# Populate the pressure drops based on user input
# Done here and not earlier b/c we need to know the dimensionality
dPs = []
# Have dp/dx
if options.x and ndim == 2:
dPs.append((1,0))
elif options.x and ndim == 3:
dPs.append((1,0,0))
# have dp/dy
if options.y and ndim == 2:
dPs.append((0,1))
elif options.y and ndim == 3:
dPs.append((0,1,0))
# dp/dz (3d only)
if options.z and ndim == 3:
dPs.append((0,0,1))
if options.x and ndim == 4:
dPs.append((1,0,0,0))
if options.y and ndim == 4:
dPs.append((0,1,0,0))
if options.z and ndim == 4:
dPs.append((0,0,1,0))
# For each pressure drop
for dP in dPs:
# Print which dP were using currently
print "Doing %s-sim" % str(dP)
# Check to see if a simulation has been done
if hdf5.has_dP_sim(save_path, dP) and options.force:
print "\tSimulation Detected! Results will be overwritten!"
elif hdf5.has_dP_sim(save_path, dP) and not options.force:
print "\tSimulation Detected! Skipping!"
continue
# If bigmode, just pass the filename instead of the solid
if options.bigmode:
S = save_path
else:
S = hdf5.get_S(save_path)
# Setup solver, printing full debug info, using the solver specified
sol = Solver(S, dP, printing=int(options.verb), sol_method=options.solver)
# setup is now implicit
# sol.setup()
#if we want to solve monolithically . . . or use the iterative solver
if options.mono:
sol.monolithic_solve()
else:
sol.converge(options.converge)
# Save dis shit.
print "Saving . . ."
hdf5.write_solver_to_h5(save_path, sol)
del sol