The GWB plug-in feature is implemented as a Dynamic-Link Library (DLL). For Python we provide a GWBplugin wrapper class contained in the Python script file GWBplugin.py which handles dealing with the C data type conversion and calling the DLL. This page answers common queries about how to use the Python version of the plug-in feature. Since Python is a dynamically typed language there are some minor differences with its results function compared to statically typed languages.
Some Python installations (e.g. Microsoft Store versions or embeddable or ARM versions from Python.org) may have security limitations that prevent them from working properly with The GWB. We support the full Windows installer from Python.org's download pages. You should always install the 32-bit or 64-bit Python that matches your GWB installation.
To include GWBplugin.py in your Python script you just need to import the class.
from GWBplugin import *
If you didn't select "Set user PATH and PYTHONPATH environment variables" when installing GWB you will first need to append the "src" folder of the GWB installation to sys.path in Python and then you can import the class.
import sys
sys.path.append('/program files/gwb/src')
from GWBplugin import *
GWBplugin Python wrapper class overview.
This is a synopsis of the Python wrapper class provided in GWBplugin.py in the "src" directory of the GWB installation folder.
Within your code you must first create a "GWBplugin" object. Next, use the "initialize" function to start the GWB application of interest by passing the application name, a optional file name for the GWB application to write output to, and any command-line type arguments.
A string containing the GWB application name you wish to use. Valid options are rxn, spece8, react, x1t, and x2t.
file_name [optional]
A string containing the name of the file you want the GWB application to write its output to. Can be None or an empty string if you do not want the output to be written to a file.
cmds [optional]
A string containing command-line options you could normally pass to the application when running it from the command-line. Can be None or an empty string for defaults.
Command-line options:
-cd
Change the working directory to the directory containing the input script specified with the -i option.
-nocd
Do not change the working directory.
-i <input_script>
Read initial input commands from the specified file.
-gtd <gtdata_dir>
Set directory to search for thermodynamic datasets.
-cond <cond_data>
Set the dataset for calculating electrical conductivity.
-d <thermo_data>
Set the thermodynamic dataset.
-s <surf_data>
Set a dataset of surface sorption reactions.
-iso <isotope_data>
Set a dataset of isotope fractionation factors.
Return value
Non-zero on success and zero on failure.
Remarks
For this function to succeed you must have your GWB installation folder added to the PATH environment variable so all the required DLLs can be found.
initialize must be done before trying to use any of the other functions.
Examples
from GWBplugin import *
...
myPlugin = GWBplugin()
...
# plug-in SpecE8 with no output written and no options
success = myPlugin.initialize("spece8")
...
# plug-in React with output written to output.txt and no options
success = myPlugin.initialize("react","output.txt")
...
# plug-in X1t with no output written, no working directory change,
# and read input from pb_contam.x1t
success = myPlugin.initialize("x1t","","-nocd -i \"c:/program files/gwb/script/pb_contam.x1t\"")
Configure and execute your calculations.
The "exec_cmd" function can be used to transmit commands to the plug-in. Each application has a chapter in the reference manual of the documentation on what commands are available. You use these commands to configure the application and then send a "go" command to trigger the calculations.
Syntax
def exec_cmd (self, uline):
Parameters
uline
A string containing the command you wish to send to the GWB application.
Retrieving the results using the GWB plug-in feature can be accomplished using the "results" function. The keywords, default units, and return types are the same as those listed in the Report Command chapter of the reference manual in the documentation. To use the results commands you provide the desired report command and keywords, optional desired units, and the node location of choice (X1t and X2t only).
Syntax
# results function
def results (self, value, units = None, ix = 0, jy = 0):
Parameters
value
String containing the report command keyword and arguments.
units [optional]
String containing the units you would like the results returned in. Can be None or an empty string if you want default units.
ix [optional]
X node position.
jy [optional]
Y node position.
Return value
Array containing the requested results.
Remarks
Even when requesting a single value it is still returned as an array.
If the command fails for any reason, for example if the requested data doesn't exist or the specified unit conversion failed, the data will be filled with ANULL (-999999.0).
ix is only used when running X1t and X2t. Otherwise it is ignored.
jy is only used when running X2t. Otherwise it is ignored.
Examples
# get aqueous species names
Species = myPlugin.results("species")
# get aqueous species concentrations in mg/kg
Conc = myPlugin.results("concentration aqueous","mg/kg")
# get pH at node 3,5
pH = myPlugin.results("pH","",3,5)[0]
Cleaning up.
The "destroy" function can be used to free up the underlying memory associated with the GWBplugin object.
Syntax
define destroy (self):
Parameters
None.
Return value
None.
Remarks
None.
Examples
myPlugin.destroy()
Quickstart tutorials and examples
Step-by-step instructions for running GWBplugin Example1 on the command line with Python.
For this tutorial you will need to have Python for Windows installed which can be obtained from the main Python website. You should install the bit version of Python that matches your GWB installation. Typically this will be the 64-bit version. GWBplugin has been most recently tested with Python version 3.10.2, but most should work. During installation you will want to make sure that "Add Python to environment variables" is selected.
The first thing you need to do is to open the command prompt ...
cmd.exe
You are now ready to run the example with Python ...
Which should produce output similar to the following ...
Beginning run.
Finished run.
concentration of Cl- in molal is 0.05
concentration of Cl- in mg/kg is 1770
There are 4 aqueous species.
Cl- = 1770 mg/kg
H+ = 1.139e-05 mg/kg
HCl = 1.234e-11 mg/kg
OH- = 0.02039 mg/kg
Congratulations on plugging into the GWB!
Python plug-in support
Why does it say "FileNotFoundError: Could not find module 'gwbplugin' (or one of its dependencies)" when I try to run my program?
In order to locate the GWB DLLs the GWBplugin class uses the GWB_BIN_PATH environment variable that is set during the GWB installation. If the variable is not set correctly you can either manually set it or re-run the GWB installer.
Why does it say "ModuleNotFoundError: No module named 'GWBplugin'" when I try to run my program?
In order for Python to locate the GWBplugin.py wrapper it uses the PYTHONPATH environment variable. If the variable is not set correctly you can either manually set it or re-run the GWB installer being sure to select "Set user PATH and PYTHONPATH environment variables".
How can I check if my PATH Environment variables are set correctly?
You can check by entering the following commands in Python's Command line:
import os,sys
print(sys.path)
This should return the path to the src folder where the GWB is installed. It may look something like this C:\\Program Files\\Gwb\\src
print(os.listdir('C:\\Program Files\\Gwb\\Src'))
The returned list should contain "GWBplugin.py".
print(os.environ['GWB_BIN_PATH'])
This should return the path of the GWB install folder. It may look something like this: C:\Program Files\Gwb
print(os.listdir(os.environ['GWB_BIN_PATH']))
The returned list should contain "gwbplugin.dll".
Why does my Activation Utility keep coming up when I try to call the GWB Plugin?
Please check that you are using the full Windows Python installer instead of the Microsoft Store version or the embeddable or ARM version from Python.org. See note at the top of page regarding the supported Python installation.
Why does it say that %1 is not a valid Win32 application?
You need to be using the same bit version for both Python and GWB. 32-bit Python can only load 32-bit DLLs and 64-bit Python can only load 64-bit DLLs.
Where are the GWBplugin files that I need to import located?
The GWBplugin Python wrapper class file (GWBplugin.py) is installed to the "src" folder in the GWB directory.
For answers to additional questions please check the plug-in section of the reference manual in the documentation, our main support FAQ, or contact us.