Last updated: December 21, 2020
This is a model (developed by Ted Eary of Enchemica Llc) that illustrate how GoldSim can be used to carry out mixing and geochemical calculations.
Linking of GoldSim to the geochemical software PHREEQC offers opportunities to account for the effects of complex aqueous chemical processes in water balance models. There are two approaches for making the linkage based on implementation of GoldSim’s External element that has an interface to Dynamic Link Libraries (DLLs) developed in software code outside of GoldSim. These approaches are:
- C++ DLL
- Python DLL
The two models in this addition to the model library are meant to provides examples of setting up and using these linkage approaches.
C++ DLL Approach
A description and example of the C++ approach has been in the GoldSim model for a few years. That model has been slightly revised to use time-varying water chemistry and less erratic water flows. It has also been designed to allow a direct comparison to the Python DLL approach so that both approaches can be evaluated as each has advantages and disadvantages.
The C++ approach involves sequential steps of sending aqueous concentrations from GoldSim to PHREEQC through the External element interface, running PHREEQC in a separate asynchronous process using the batch version of PHREEQC, writing the PHREEQC results to a text file, reading the results from the text file, and finally sending the results back to GoldSim via the External element interface. The advantages of this approach are that it has been in use for many years and found to be robust and flexible and allows review of intermediate output files to find errors in inputs to PHREEQC. The primary disadvantages are slow run times due to the intermediate file input-output steps occurring during each time step. Also, alteration of the C++ code is needed to return results other than concentrations and any changes to the C++ code, such as changing the number of species, require re-compilation to create a new DLL.
Python DLL Approach
In recent years, new releases of the PHREEQC software have included the object-oriented IPHREEQC modules that can be directly used by other software such as Python. This capability has been incorporated into the PhreeqPython wrapper around the VIPhreeqc extension of the IPHREEQC modules (v. 3.3.7) developed by Vitens. Also, GoldSim has developed an article called GSPy: GoldSim/Python Interface for Scripting Support and associated DLL (GSPy.pyd) that provide definitions of external functions in pure Python that can be called by GoldSim during a simulation. This combination of software provides the means to link GoldSim to the IPHREEQC modules through Python, resulting in efficient incorporation of complex aqueous geochemical calculations in GoldSim models of water quality. The GSPy-Python approach has the advantages that alteration of the Python code needs no re-compilation, implementation of the PhreeqPython functions is relatively simple, and run times are shorter because there are no intermediate file input-output steps in the calculation sequence. The primary disadvantages are that debugging of errors in the PHREEQC setup is more difficult, the setup requires installation of Python, and not all of the Keyword options in PHREEQC are available for representing aqueous geochemical processes.
For more specific instructions on setting up the linkage between GoldSim and Python for this model, see the Readme.docx that is included in the attached zip file (link below).
The examples models simulate mixing of an acidic and an alkaline water flowing into a pond. Both the flow rates of the two streams and the water chemistries vary with time. Chemical equilibrium is assumed to be instantaneously achieved in the pond where the two streams mix. The C++ and the GSPy-Python versions of the model are exactly the same except that the External element that provides the interface to PHREEQC implements the software-specific DLL. Both models also contain a TimeSeries element that records the concentrations returned from each External element. These recorded concentrations are compared to each other to identify differences in results between the two approaches. These example models can be adapted to simulate more complex systems that involve mixing of solutions and the consequent effects of reactive processes on water chemistry.
Considerations for Implementation
To implement either of the approaches used in these models, the user should have a good working knowledge of PHREEQC and GoldSim’s Contaminant Transport (CT) module. Specific to the C++ approach, the user should have knowledge of C++ and be familiar with compilation procedures to create DLLs because the code will probably need to be changed to make it applicable to each model application. For the GSPy-Python approach, the user will need to follow the installation steps for the GSPy GoldSim/Python interface as described here. It will also be very useful to have some knowledge of Python because the Python code will likely need to be changed in a few places to make it applicable to each model application.
Compared to the C++ linkage method, the Python linkage approach has been found to be faster. The following table shows results of informal tests of runtimes for the example models (731-day simulation, 1-day timestep).
|Run||GSPy DLL Run time (seconds)||C++ DLL Run time (seconds)*|
Hardware: Intel® Xeon® CPU E3-1505M v5 @ 2.80GHz; 32 GB RAM; MS Win Pro 10-64-bit
* PHREEQC 3.6.2 (Win 10 batch version)
consider the points discussed in this article.
Download the Model File:
Version Update (by Ted Eary):
Removed the modification of the ionic-charge balancing in phreeqcLinkFunctions.cpp, so that the use of pH for charge balancing defined in the phreeqc template file (phreeqc_input_template.txt) gets used as the default. That default is to use pH. No changes are needed to phreeqc_input_template.txt.
Added in the following comment to the phreeqcLinkFunctions.cpp to make it clear that selection of an ionic charge-balancing approach needs to be determined by the user; something that is not always obvious. Below is the chunk of code where changes were made.
Please sign in to leave a comment.