GWBplugin – Getting started with Python

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.


Using the plug-in

Using the GWB plug-in feature:

  1. Including GWBplugin.py in your Python script.
  2. GWBplugin Python wrapper class overview.
  3. Initializing the GWB application you wish to use.
  4. Configure and execute your calculations.
  5. Retrieving the results.
  6. Cleaning up.

Including GWBplugin.py in your Python script.

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.

# GWBplugin.py

ANULL = -999999

class GWBplugin:
	Name = "GWBplugin"
	def __init__(self):
	def initialize (self, app_name, file_name = None, cmds = None):
	def exec_cmd (self, uline):
	def results (self, value, units = None, ix = 0, jy = 0):
	def destroy (self):

Initializing the GWB application you wish to use.

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.

Syntax

def initialize (self, app_name, file_name = None, cmds = None):

Parameters

app_name
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.

Return value

Non-zero on success and zero on failure.

Remarks

None.

Examples

myPlugin.exec_cmd("3 mmol H+")
myPlugin.exec_cmd("2 mmol Ca++")
myPlugin.exec_cmd("5 mmolar Cl-")
myPlugin.exec_cmd("go")

Retrieving the results.

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 ...

python "/Program Files/GWB/src/GWBplugin_Python_Example1.py"

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".

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.