PHREEQC has an embedded Basic interpreter (David Gillespie, Synaptics, Inc., San Jose, Calif., written commun., 1997; distributed with the Linux operating system, Free Software Foundation, Inc.). Basic is a computer language with statements on numbered lines. The statements are much like the formulas entered in a spreadsheet cell, but Basic allows, in addition, the conditional statements and looping operations of a programming language. Variables can be defined at will, given a value, and used in subsequent lines. Variable names must start with a letter, which can be followed by any number of letters and numbers, and the variable's name must be different from the general and PHREEQC Basic functions. Names ending with a “$” are for strings. Thus,

10 A = 1.246 20 A$ = 'A equals 1.246'

In Basic you can use the operators “+”, “-”, “*”, “/”, and “=”, just as in written equations. A single variable is used on the left side of the equals sign. Expressions in parentheses, “(expression)”, are evaluated first, and then used in the more general expression. Exponentiation is done with the ^ sign: 2^2 = 4. The standard Basic and special PHREEQC Basic functions are listed in tables 7 and 8, respectively.

Basic programs are executed in line number order, regardless of the order used for writing the lines (but, for good programming, keep the number order intact). Basic variables, functions, and statements are case insensitive. Initially, a numeric variable is zero, and a string is empty, “”. The scope of variables is limited to the program unit where they are defined (RATES, USER_GRAPH, USER_PRINT, USER_PUNCH, or CALCULATE_VALUES data block). Numeric data can be transferred between program units with the functions PUT and GET (see table 8). However, in multithreaded and multiprocessor applications (for instance, PHAST, Parkhurst and others, 2010), PUT and GET may not work correctly.

Basic in PHREEQC is quite powerful, and it could be used for other purposes than manipulating variables in PHREEQC. For example, the following input file (illustrated in figure 3) plots the sine function from 0 to 360 degrees:

SOLUTION 1 REACTION; H2O 0; 0 in 21 USER_GRAPH -axis_titles 'ANGLE, IN DEGREES' 'SINE(ANGLE)' -axis_scale x_axis 0 360 90 10 pi = 2 * arctan(1e20) 20 i = pi * (step_no - 1) / 10 30 graph_x i * 180 / pi 40 graph_y sin(i) END

Originally, the Basic interpreter was a unique feature of PHREEQC version 2, aimed at calculating rates for kinetic geochemical processes. Rate expressions for kinetic reactions can have various forms, and they tend to be redefined or updated frequently as more data become available. In a PHREEQC input file, the rates can be adapted easily by the user as necessary. Because rate expressions often depend on conditions (for example, the rate expression may be different for mineral dissolution and precipitation), the conditional “if” statement of Basic can be necessary. Special Basic functions (table 8) have been written to retrieve geochemical quantities that frequently are used in kinetic rate expressions, such as molalities, activities, saturation indices, and moles of reactants.

PHREEQC calculations generate a large number of geochemical quantities, possibly distributed in space and time as well. Rather than storing or writing all of these quantities for a run, small Basic programs in the data blocks USER_GRAPH, USER_PRINT and USER_PUNCH can be used to print selected items or to calculate and graph specific numbers such as sums of species or concentrations in milligrams per liter. Also, the implementation of isotopes as individual chemical components relies heavily on Basic programs in the CALCULATE_VALUES data block, where Basic is used to calculate specific isotopic ratios, such as of carbon-13 in various bicarbonate species. Functions defined in CALCULATE_VALUES data blocks can be used in any Basic program within PHREEQC.