-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcapsim.py
329 lines (261 loc) · 12.4 KB
/
capsim.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
#CapSim is software to be used for modeling contaminant transport in a
#cap of contaminated sediment.
#
#This file is used to pull together all of the input/output GUIs and the
#mathematical solvers used in the simulation.
#
########################################################################
# #
# ###### ## ###### ###### ####### # # #
# # # # # # # # ## ## #
# # # # # # # # # # # # #
# # ###### ###### ###### # # # # # #
# # # # # # # # # # #
# # # # # # # # # #
# ###### # # # ###### ####### # # #
# #
#Version 3.5 #
# #
#Developed by Xiaolong Shen, David Lampert and Danny Reible #
#Department of Civil, Architectural and Environmental Engineering #
#The University of Texas at Austin #
# #
#Last Updated: 6/20/2017 by XS #
# #
########################################################################
import cPickle as pickle, sys, os, warnings
import _winreg as wreg
import tkFileDialog as tkfd
from Tkinter import Tk
if sys.path[0][-3:] == 'zip':
os.chdir(sys.path[0][:-12])
path = sys.path[0][:-12]
warnings.filterwarnings('ignore')
else: path = sys.path[0]
#Load the information of registry,
CapSimReg = wreg.ConnectRegistry(None, wreg.HKEY_CURRENT_USER)
# Check the existence of the key 'CapSim' in Registry
try:
CapSimKey = wreg.OpenKey(CapSimReg, r'Software\CapSim')
Filepath = wreg.QueryValueEx(CapSimKey, 'FilePath')[0]
CapSimKey.Close()
except:
rootinit = Tk()
w = rootinit.winfo_screenwidth()
h = rootinit.winfo_screenheight()
rootsize = tuple(int(_) for _ in rootinit.geometry().split('+')[0].split('x'))
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
rootinit.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
# Create a window for users to input file directory and then create a key in Registry containing the directory information
UserPath = os.environ['USERPROFILE']
Filepath = tkfd.askdirectory(initialdir=UserPath+r'\Documents', parent = rootinit, title='To initialize CapSim, please select the directory for input and output files')
if Filepath is not '':
CapSimKey = wreg.CreateKey(CapSimReg, r'Software\CapSim')
wreg.SetValueEx(CapSimKey,r'FilePath',0,wreg.REG_SZ,Filepath)
# Create the folders under the input, output and database directory
try: os.chdir(Filepath)
except: os.mkdir(Filepath)
try: os.chdir(Filepath+r'/database')
except: os.mkdir(Filepath+r'/database')
try: os.chdir(Filepath+r'/input_cpsm_files')
except: os.mkdir(Filepath+r'/input_cpsm_files')
try: os.chdir(Filepath+r'/output')
except: os.mkdir(Filepath+r'/output')
try: os.chdir(Filepath+r'/batch_files')
except: os.mkdir(Filepath+r'/batch_files')
rootinit.destroy()
sys.path.append(path)
sys.path.append(path + r'/input_windows')
sys.path.append(path + r'/database')
sys.path.append(path + r'/solvers')
sys.path.append(path + r'/postprocess')
# Load the functions for GUI and solver
from capsim_object_types import System
from mainmenu import show_mainmenu
from updatedfile import get_updatedfile, get_updateddatabase
from help import show_help
from systemunits import get_systemunits
from chemicalproperties import get_chemicalproperties
from matrixproperties import get_matrixproperties
from systemproperties import get_systemproperties
from layerproperties import get_layerproperties
from sorptionproperties import get_sorptionproperties
from reactionproperties import get_reactionproperties
from reactioncoefficients import get_reactioncoefficients
from layerconditions import get_layerconditions
from solidlayerconditions import get_solidlayerconditions
from solveroptions import get_solveroptions
from inputoptions import get_inputoptions
from summary import get_summary
from batch import make_batch
from database import edit_database, make_database
from soliddatabase import edit_soliddatabase, make_soliddatabase
from solver import solve_system, solve_batch
from postprocess import postprocess_data, postprocess_batch
from graphprocess import graphprocess_data
# The current version and the previous versions
version = '3.5'
previous_version = ['CapSim 3.0', 'CapSim 3.1', 'CapSim 3.2', 'CapSim 3.2a', 'CapSim 3.2b', 'CapSim 3.3', '3.3', '3.4', '3.5', '3.6']
fonttype = 'Arial 10'
formulatype = 'Calibri 11'
while(1):
# Start the CapSim loop and create the 'system' instance
system = System(version, fonttype, formulatype)
# Load the chemical and solid material database, create a new database file if no database file is available, update the database file if it is from the older version
try:
data = open(Filepath + r'/database/capsim3_chemicaldatabase', 'r')
database = pickle.load(data)
data.close()
except:
try:
data = open(Filepath + r'/database/capsim_chemicaldatabase', 'r')
database = pickle.load(data)
data.close()
database = get_updateddatabase(database)
pickle.dump(database, open(Filepath + r'/database/capsim3_chemicaldatabase', 'w'))
except:
data = make_database(Filepath)
database = pickle.load(data)
data.close()
try: materialdata = open(Filepath + r'/database/capsim_soliddatabase', 'r')
except: materialdata = make_soliddatabase(Filepath)
materials = pickle.load(materialdata)
materialdata.close()
option, filename = show_mainmenu(system)
# Create a new input file using the input window classes
if option == 0:
step = 1
while system is not None:
if step == 1 : system, step = get_systemunits(system, step)
if step == 2 : system, step = get_chemicalproperties(system, database, step)
if step == 3 : system, step = get_matrixproperties(system, materials, step)
if step == 4 : system, step = get_sorptionproperties(system, step)
if step == 5 : system, step = get_layerproperties(system, step)
if step == 6 : system, step = get_reactionproperties(system, step)
if step == 7 : system, step = get_reactioncoefficients(system, step)
if step == 8 : system, step = get_systemproperties(system, step)
if step == 9 : system, step = get_layerconditions(system, step)
if step == 10: system, step = get_solidlayerconditions(system, step)
if step == 11: system, step = get_solveroptions(system, step)
if step == 12: system, step = get_inputoptions(system, step)
if step == 13:
while(1):
#show the summary window
system = get_summary(system, database, materials)
#run the simulation
if system is not None:
output, main = solve_system(system)
#postprocess
if output is not None:
main = postprocess_data(system, output)
if main == 1: break
else: break
#Loads an existing cpsm file
if option == 1:
cpsmfile = open(filename, 'r')
system = pickle.load(cpsmfile)
cpsmfile.close()
#Update the loaded input file if it is from a previous version
if system.version != version and previous_version.count(system.version) == 0:
system = get_updatedfile(system, version, fonttype)
system.version = version
# updates from version 3.3
try: a = system.outputsteps
except: system.outputsteps = 100
try: a = system.timeoption
except: system.timeoption = 'Crank-Nicolson'
# updates from version 3.4
try: a = system.matrices[0].components[0].mfraction
except:
for matrix in system.matrices:
for component in matrix.components:
component.mfraction = round(component.rho*component.fraction/matrix.rho, 6)
print(component.mfraction)
# updates from version 3.5
try: a = system.BCs[system.chemicals[0].name].tau
except:
system.taucoefs = {}
system.taucoefs['Q'] = 1.
system.taucoefs['V'] = 100.
system.taucoefs['h'] = 1.
system.taucoefs['DOC'] = 0.
system.taucoefs['Qevap'] = 0.
system.taucoefs['Decay'] = 'None'
system.taucoefs['Evap'] = 'None'
for chemical in system.chemicals:
system.BCs[chemical.name].tau = 0
system.BCs[chemical.name].kdecay = 0
system.BCs[chemical.name].kevap = 0
if system.adv == 'Tidal oscillation': system.adv = 'Period oscillation'
if system.topBCtype == 'CSTR water body': system.topBCtype = 'Finite mixed water column'
if system.bio == 'Bioturbation': system.bio = 'Uniform'
try: a = system.biomix
except:
system.biomix = 0
system.depsteps = 0
system.averageoption = 'Instaneous'
try: a = system.depgrid
except:
system.depoption = 'Time step size'
system.depgrid = system.Vdep * system.delt
try: a = system.sigma
except:
system.sigma = 10
while(1):
#show the summary window
system = get_summary(system, database, materials)
#run the simulation
if system is not None:
output, main = solve_system(system)
#postprocess
if output is not None:
main = postprocess_data(system, output)
if main == 1: break
else: break
if option == 2:
#run a batch file
systems, type, main = make_batch(system)
if main != 1:
outputs, main = solve_batch(systems, type)
if outputs is not None:
main = postprocess_batch(type, systems, outputs)
if main == 1: break
if option == 3:
#Load an existing output file
while(1):
step = 1
if system is not None:
system, step = get_systemunits(system, step)
if step == 2:
main = graphprocess_data(system)
else:
main = 1
if main == 1: break
else: break
if option == 4:
#Edit the chemical database
while(1):
data = open(Filepath + r'/database/capsim3_chemicaldatabase', 'r')
database = pickle.load(data)
data.close()
database, main = edit_database(database, system)
pickle.dump(database, open(Filepath + r'/database/capsim3_chemicaldatabase', 'w'))
if main == 1: break
if option == 5:
#Edit the solid material database
while(1):
data = open(Filepath + r'/database/capsim_soliddatabase', 'r')
database = pickle.load(data)
data.close()
database, main = edit_soliddatabase(database, system)
pickle.dump(database, open(Filepath + r'/database/capsim_soliddatabase', 'w'))
if main == 1: break
#if option == 6:
#show the help
# main = 0
# while main == 0: main = show_help(system)
sys.exit()