Squashed 'examples/com/' changes from 3ed6cb65..af8d7af7

af8d7af7 Closes https://github.com/usgs-coupled/iphreeqccom/issues/6

git-subtree-dir: examples/com
git-subtree-split: af8d7af7fb6d37dfdbc4d825350f183f0073d14c
This commit is contained in:
Darth Vader 2024-08-22 21:43:27 +00:00
parent ce6d37c593
commit e9b2c29221

View File

@ -45,7 +45,7 @@ class CoupledModel(object):
processes) processes)
self.reaction_model.make_initial_state() self.reaction_model.make_initial_state()
init_conc = dict([(name, [value] * ncells) for name, value in init_conc = dict([(name, [value] * ncells) for name, value in
self.reaction_model.init_conc.items()]) list(self.reaction_model.init_conc.items())])
self.advection_model = AdvectionModel(init_conc, self.advection_model = AdvectionModel(init_conc,
self.reaction_model.inflow_conc) self.reaction_model.inflow_conc)
self.component_names = self.reaction_model.component_names self.component_names = self.reaction_model.component_names
@ -56,7 +56,7 @@ class CoupledModel(object):
def run(self): def run(self):
"""Go over all time steps (shifts). """Go over all time steps (shifts).
""" """
for shift in xrange(self.nshifts): for shift in range(self.nshifts):
self.advection_model.advect() self.advection_model.advect()
self.advection_model.save_results(self.results) self.advection_model.save_results(self.results)
self.reaction_model.modify(self.advection_model.conc) self.reaction_model.modify(self.advection_model.conc)
@ -155,7 +155,7 @@ class ReactionModel(object):
self.initial_conditions) self.initial_conditions)
self.calculators = [root_calculator] self.calculators = [root_calculator]
self.cell_ranges = [(0, root_ncells)] self.cell_ranges = [(0, root_ncells)]
for process in xrange(self.processes - 1): for process in range(self.processes - 1):
self.calculators.append(PhreeqcCalculatorProxy(slave_ncells, self.calculators.append(PhreeqcCalculatorProxy(slave_ncells,
self.initial_conditions)) self.initial_conditions))
self.cell_ranges.append((current_cell, self.cell_ranges.append((current_cell,
@ -193,7 +193,7 @@ class ReactionModel(object):
self.conc[name] = [] self.conc[name] = []
for cell_range, calculator in zip(self.cell_ranges, self.calculators): for cell_range, calculator in zip(self.cell_ranges, self.calculators):
current_conc = dict([(name, value[cell_range[0]:cell_range[1]]) for current_conc = dict([(name, value[cell_range[0]:cell_range[1]]) for
name, value in new_conc.items()]) name, value in list(new_conc.items())])
calculator.modify(current_conc) calculator.modify(current_conc)
for calculator in self.calculators: for calculator in self.calculators:
conc = calculator.get_modified() conc = calculator.get_modified()
@ -251,7 +251,7 @@ class PhreeqcCalculator(object):
code += self.make_selected_output(self.components) code += self.make_selected_output(self.components)
self.phreeqc.RunString(code) self.phreeqc.RunString(code)
self.conc = self.get_selected_output() self.conc = self.get_selected_output()
all_names = self.conc.keys() all_names = list(self.conc.keys())
self.component_names = [name for name in all_names if name not in self.component_names = [name for name in all_names if name not in
('cb', 'H', 'O')] ('cb', 'H', 'O')]
code = '' code = ''
@ -270,7 +270,7 @@ class PhreeqcCalculator(object):
end = self.ncells + 1 end = self.ncells + 1
conc.update(new_conc) conc.update(new_conc)
modify = [] modify = []
for index, cell in enumerate(xrange(1, end)): for index, cell in enumerate(range(1, end)):
modify.append("SOLUTION_MODIFY %d" % cell) modify.append("SOLUTION_MODIFY %d" % cell)
modify.append("\t-cb %e" % conc['cb'][index]) modify.append("\t-cb %e" % conc['cb'][index])
modify.append("\t-total_h %s" % conc['H'][index]) modify.append("\t-total_h %s" % conc['H'][index])
@ -375,7 +375,7 @@ class PhreeqcCalculatorProxy(object):
def process_worker(ncells, initial_conditions, in_queue, out_queue): def process_worker(ncells, initial_conditions, in_queue, out_queue):
"""This runs in another process. """This runs in another process.
""" """
print 'Started process with ID', os.getpid() print('Started process with ID', os.getpid())
calculator = PhreeqcCalculator(ncells, initial_conditions) calculator = PhreeqcCalculator(ncells, initial_conditions)
out_queue.put((calculator.inflow_conc, calculator.init_conc, out_queue.put((calculator.inflow_conc, calculator.init_conc,
calculator.component_names)) calculator.component_names))
@ -393,7 +393,7 @@ def plot(ncells, outflow, specie_names):
""" """
colors = {'Ca': 'r', 'Cl': 'b', 'K': 'g', 'N': 'y', 'Na': 'm'} colors = {'Ca': 'r', 'Cl': 'b', 'K': 'g', 'N': 'y', 'Na': 'm'}
x = [i / float(ncells) for i in x = [i / float(ncells) for i in
xrange(1, len(outflow[specie_names[0]]) + 1)] range(1, len(outflow[specie_names[0]]) + 1)]
args = [] args = []
for name in specie_names: for name in specie_names:
args.extend([x, outflow[name], colors[name]]) args.extend([x, outflow[name], colors[name]])
@ -410,15 +410,9 @@ def measure_time(func, *args, **kwargs):
"""Convenience function to measure run times. """Convenience function to measure run times.
""" """
import sys import sys
if sys.platform == 'win32': start = time.perf_counter()
# time.clock is more accurate on Windows
timer_func = time.clock
else:
# but behaves differently on other platforms
timer_func = time.time
start = timer_func()
result = func(*args, **kwargs) result = func(*args, **kwargs)
return result, time.clock() - start return result, time.perf_counter() - start
if __name__ == '__main__': if __name__ == '__main__':
@ -460,12 +454,12 @@ if __name__ == '__main__':
model.run() model.run()
return model, model.results return model, model.results
(model, outflow), run_time = measure_time(run) (model, outflow), run_time = measure_time(run)
print 'Statistics' print('Statistics')
print '==========' print('==========')
print 'number of cells: ', ncells print('number of cells: ', ncells)
print 'number of shifts: ', nshifts print('number of shifts: ', nshifts)
print 'number of processes:', processes print('number of processes:', processes)
print 'run_time: ', run_time print('run_time: ', run_time)
plot(ncells, outflow, model.component_names) plot(ncells, outflow, model.component_names)
main(ncells=400, nshifts=1200, processes=2) main(ncells=400, nshifts=1200, processes=2)