-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathphreeqpython.py
405 lines (302 loc) · 14.5 KB
/
phreeqpython.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
""" Phreeqpython module """
import os
import gzip
from pathlib import Path
from .viphreeqc import VIPhreeqc
from .solution import Solution
from .gas import Gas
from .equilibriumphase import EquilibriumPhase
from .utility import convert_units
import warnings
class PhreeqPython(object):
""" PhreeqPython Class to interact with the VIPHREEQC module """
def __init__(self, database=None, database_directory = None, from_file=None,
debug=False):
# Create VIPhreeqc Instance
self.ip = VIPhreeqc()
self.ip.debug = debug
self.chain = False
self.chain_buffer = ""
# Load Vitens.dat database. The VIPhreeqc module is unable to handle relative paths
if not database:
database = "vitens.dat"
if not database_directory:
database_directory = Path(os.path.dirname(__file__) + "/database")
database_path = database_directory / database
if not database_directory.exists():
raise FileNotFoundError("Database directory does not exist.")
if not database_path.exists():
raise FileNotFoundError("Database file not found")
self.ip.load_database(database_path)
if from_file:
dump = gzip.open(from_file,"rb")
try:
inputstr = dump.read().decode('utf-8') + "END"
print(inputstr)
self.ip.run_string(inputstr)
solutions = self.ip.get_solution_list()
# force IPHREEQC to calculate all the solution properties
for solution_number in solutions:
self.change_solution(solution_number,{'Na':0})
self.solution_counter = solutions[-1]
# precalculte all solutions
finally:
dump.close()
else:
self.buffer = False
self.solution_counter = -1
self.gas_counter = -1
self.phase_counter = -1
def add_equilibrium_phase(self, components=[], to_si=[], amount=[]):
self.phase_counter += 1
inputstr = "EQUILIBRIUM_PHASE {}\n".format(self.phase_counter)
components = [components] if not isinstance(components, list) else components
to_si = [to_si] if not isinstance(to_si, list) else to_si
amount = [amount] if not isinstance(amount, list) else amount
if(len(to_si) < len(components)):
to_si.extend([0 for i in range(len(components)-len(to_si))])
if(len(amount) < len(components)):
amount.extend([10 for i in range(len(components)-len(amount))])
for num in range(len(components)):
inputstr += "{} {} {}\n".format(components[num], to_si[num], amount[num])
inputstr += "SAVE EQUILIBRIUM_PHASE {}\n".format(self.phase_counter)
self.ip.run_string(inputstr)
return EquilibriumPhase(self, self.phase_counter)
def add_gas(self, components=None, pressure=1.0, volume=1.0, fixed_pressure=True, fixed_volume=False, equilibrate_with=False):
""" add a gas phase to the VIPhreeqc stack """
if fixed_pressure and fixed_volume:
raise ValueError("Cannot create a gas with a fixed pressure and a fixed volume!")
if equilibrate_with is not False and not fixed_volume:
raise ValueError("Only gas phases with a fixed_volume can be created in equilibrium with a solution")
self.gas_counter += 1
inputstr = "GAS_PHASE {}\n".format(self.gas_counter)
if fixed_pressure:
inputstr += "-fixed_pressure \n"
if fixed_volume:
inputstr += "-fixed_volume \n"
inputstr += "-volume {}\n".format(volume)
inputstr += "-pressure {}\n".format(pressure)
for gas, pressure in components.items():
inputstr += "{} {} \n".format(gas, pressure)
if equilibrate_with:
if isinstance(equilibrate_with, Solution):
inputstr += "-equilibrate {}\n".format(equilibrate_with.number)
else:
inputstr += "-equilibrate {}\n".format(equilibrate_with)
inputstr += "SAVE GAS_PHASE "+str(self.gas_counter) + "\n"
inputstr += "END \n"
self.ip.run_string(inputstr)
return Gas(self, self.gas_counter)
def add_solution_raw(self, composition=None):
warnings.warn("add_solution_raw is deprecated, use add_solution and add_solution_simple instead", DeprecationWarning)
return self.add_solution(composition)
def add_solution(self, composition=None, extraneous=None):
""" add a solution to the VIPhreeqc Stack, allowing more control over the
created solution """
self.solution_counter += 1
inputstr = "SOLUTION "+str(self.solution_counter) + "\n"
if len(composition) > 0:
for key, value in composition.items():
inputstr += " "+key+" "+str(value) + "\n"
if not self.chain:
inputstr += "SAVE SOLUTION "+str(self.solution_counter) + "\n"
inputstr += "END \n"
self.ip.run_string(inputstr)
else:
self.chain_buffer += inputstr
return Solution(self, self.solution_counter, extraneous=extraneous)
def add_solution_simple(self, composition=None, temperature=25, units='mmol'):
""" add a solution to the VIPhreeqc Stack and add all individual components
in a reaction step
"""
self.solution_counter += 1
inputstr = "SOLUTION "+str(self.solution_counter) + "\n"
inputstr += "-temp "+str(temperature) + "\n"
if len(composition) > 0:
inputstr += "REACTION 1 \n"
for species, amount in composition.items():
moles = convert_units(species, amount, units, 'mmol')
inputstr += species + " " + str(moles) + "\n"
inputstr += "1 mmol \n"
if not self.chain:
inputstr += "SAVE SOLUTION "+str(self.solution_counter) + "\n"
inputstr += "END \n"
self.ip.run_string(inputstr)
else:
self.chain_buffer += inputstr
return Solution(self, self.solution_counter)
def add_master_species(self, element, master_species, alkalinity=0, gfw=1, egfw=""):
""" add a master species to the VIPhreeqc Instance """
inputstr = "SOLUTION_MASTER_SPECIES; {} {} {} {} {} \n".format(element, master_species, alkalinity, gfw, egfw)
self.buffer = inputstr
def add_species(self, reaction, log_k=0, delta_h=0, egfw=None):
""" add a solution species to the VIPhreeqc Instance """
inputstr = self.buffer if self.buffer else ""
self.buffer = False
inputstr += "SOLUTION_SPECIES \n {} \n".format(reaction)
inputstr += "log_k {}".format(log_k)
self.ip.run_string(inputstr)
def change_solution(self, solution_number, elements, create_new=False):
""" change solution composition by adding/removing elements """
inputstr = ""
if not self.chain:
inputstr += "USE SOLUTION "+str(solution_number)+"\n"
inputstr += "REACTION 1 \n"
for element, change in elements.items():
inputstr += element + " " + str(change) + "\n"
inputstr += "1 mol \n"
if create_new:
self.solution_counter += 1
solution_number = self.solution_counter
if not self.chain:
inputstr += "SAVE SOLUTION "+str(solution_number) + "\n"
inputstr += "END"
self.ip.run_string(inputstr)
else:
self.chain_buffer += inputstr
return Solution(self, solution_number)
def equalize_solution(self, solution_number, phases, to_si, in_phase=[10], with_element=[None]):
""" saturate or desaturate (equalize) a solution with one or more phases """
if not isinstance(phases, list):
phases = [phases]
if not isinstance(to_si, list):
to_si = [to_si]
if not isinstance(in_phase, list):
in_phase = [in_phase]
if not isinstance(with_element, list):
with_element = [with_element]
else:
# fix default inputs
if len(to_si) < len(phases):
to_si.extend([0 for i in range(len(phases)-len(to_si))])
if len(in_phase) < len(phases):
in_phase.extend([10 for i in range(len(phases)-len(in_phase))])
if len(with_element) < len(phases):
with_element.extend([None for i in range(len(phases)-len(with_element))])
inputstr = ""
if not self.chain:
inputstr += "USE SOLUTION "+str(solution_number)+"\n"
inputstr += "EQUILIBRIUM PHASES 1 \n"
for num in range(len(phases)):
if with_element[num]:
inputstr += phases[num] + " " + str(to_si[num]) + " " + with_element[num] + " " + str(in_phase[num]) + "\n"
else:
inputstr += phases[num] + " " + str(to_si[num]) + " " + str(in_phase[num]) + "\n"
if not self.chain:
inputstr += "SAVE SOLUTION "+str(solution_number) + "\n"
inputstr += "END"
self.ip.run_string(inputstr)
else:
self.chain_buffer += inputstr
return Solution(self, solution_number)
def mix_solutions(self, solutions):
""" Create a mixture from two other solutions """
# add a solution to the VIPhreeqc Stack
self.solution_counter += 1
# mix two or more solutions to obtain a new solution
inputstr = "MIX 1 \n"
# set of phreeqpython IDs
pp_ids = set()
extraneous = {}
def merge_extraneous(n, extraneous, fraction):
for k, v in n.items():
if isinstance(v, dict):
extraneous[k] = {} if k not in extraneous else extraneous[k]
merge_extraneous(v, extraneous[k], fraction)
else:
extraneous[k] = extraneous.get(k, 0) + v * fraction
for solution, fraction in solutions.items():
merge_extraneous(solution.extraneous, extraneous, fraction)
if isinstance(solution, Solution):
pp_ids.add(solution.pp.ip.id_)
inputstr += str(solution.number) + " " + str(fraction) + "\n"
else:
inputstr += str(solution) + " " + str(fraction) + "\n"
inputstr += "SAVE SOLUTION "+str(self.solution_counter) + "\n"
inputstr += "END \n"
if len(pp_ids) > 1:
raise ValueError('Cannot Mix solutions belonging to seperate PhreeqPython instances!')
self.ip.run_string(inputstr)
return Solution(self, self.solution_counter, extraneous=extraneous)
def interact_solution_gas(self, solution_number, gas_number):
""" Interact solution with gas phase """
inputstr = "USE SOLUTION " + str(solution_number) + "\n"
inputstr += "USE GAS_PHASE " + str(gas_number) + "\n"
inputstr += "SAVE GAS_PHASE " + str(gas_number) + "\n"
inputstr += "SAVE SOLUTION " + str(solution_number) + "\n"
inputstr += "END"
self.ip.run_string(inputstr)
def interact_solution_phase(self, solution_number, phase_number):
""" Interact solution with equilibrium phase """
inputstr = "USE SOLUTION " + str(solution_number) + "\n"
inputstr += "USE EQUILIBRIUM_PHASE " + str(phase_number) + "\n"
inputstr += "SAVE EQUILIBRIUM_PHASE " + str(phase_number) + "\n"
inputstr += "SAVE SOLUTION " + str(solution_number) + "\n"
inputstr += "END"
self.ip.run_string(inputstr)
def change_solution_temperature(self, solution_number, temperature):
""" change temperature """
inputstr = "USE SOLUTION " + str(solution_number) + "\n"
inputstr += "REACTION_TEMPERATURE 1 \n"
inputstr += str(temperature) + "\n"
inputstr += "SAVE SOLUTION "+str(solution_number) + "\n"
self.ip.run_string(inputstr)
return Solution(self, self.solution_counter)
def copy_solution(self, solution_number):
""" Copy a solution to create a new one """
# add a solution to the VIPhreeqc Stack
self.solution_counter += 1
# mix two or more solutions to obtain a new solution
inputstr = "COPY SOLUTION " + str(solution_number) + " " + str(self.solution_counter) + "\n"
inputstr += "END\n"
self.ip.run_string(inputstr)
return Solution(self, self.solution_counter)
def copy_gas(self, gas_number):
""" Copy a solution to create a new one """
# add a solution to the VIPhreeqc Stack
self.gas_counter += 1
# mix two or more solutions to obtain a new solution
inputstr = "COPY GAS_PHASE " + str(gas_number) + " " + str(self.gas_counter) + "\n"
inputstr += "END\n"
self.ip.run_string(inputstr)
return Gas(self, self.gas_counter)
def empty_solution(self):
return self.add_solution({})
def remove_solutions(self, solution_number_list):
""" Remove solutions from VIPhreeqc memory """
inputstr = "DELETE \n"
inputstr += "-solution " + ' '.join(map(str, solution_number_list))
self.ip.run_string(inputstr)
def remove_gases(self, gas_number_list):
""" Remove solutions from VIPhreeqc memory """
inputstr = "DELETE \n"
inputstr += "-gas_phase " + ' '.join(map(str, gas_number_list))
self.ip.run_string(inputstr)
def get_solution(self, number):
return Solution(self, number)
def dump_solutions(self, solution_number_list = None, filename='dump.gz'):
""" Dump solutions to raw file for transmission to another VIPhreeqc instance """
if not solution_number_list:
solution_number_list = []
inputstr = "DUMP \n"
inputstr += "-file "+filename + "\n"
inputstr += "-solution " + ' '.join(map(str, solution_number_list)) + "\n"
inputstr += "END"
self.ip.set_dump_string_on()
self.ip.run_string(inputstr)
dump = self.ip.get_dump_string()
# write to file
dumpfile = gzip.open(filename,'w')
dumpfile.write(dump)
dumpfile.close()
self.ip.set_dump_string_off()
def start_chain(self, number):
self.chain = True
self.chain_buffer = "USE SOLUTION "+str(number) + "\n"
def end(self):
self.chain = False
inputstr = "SAVE SOLUTION "+str(self.solution_counter) + "\n"
inputstr += "END \n"
self.ip.run_string(self.chain_buffer + inputstr)
def get_solution_list(self):
return self.ip.get_solution_list()