PhreeqcUsers Discussion Forum

Conceptual Models => Incorporation PHREEQC in programming languages => Topic started by: freiburgermsu on November 06, 2021, 04:27:52 AM

Title: PHREEQpy in unix
Post by: freiburgermsu on November 06, 2021, 04:27:52 AM
Dear PHREEQCusers,

I am developing ROSSpy (https://pypi.org/project/ROSSpy/) as an application of PHREEQpy for simulating the geochemical reactive transport of reverse osmosis desalination. ROSSpy executes flawlessly in windows, however, the package yields an error that the database cannot load in unix systems (https://github.com/freiburgermsu/ROSSpy/blob/main/examples/other/test_function/executing_rosspy_test.ipynb). This error occurs despite parameterizing DATABASE as the first line of the PQI file (https://github.com/freiburgermsu/ROSSpy/blob/main/examples/other/test_function/rosspy_test-2/input.pqi) and despite providing the database as an argument to the imported PHREEQpy object (https://github.com/freiburgermsu/ROSSpy/blob/main/rosspy/core.py#L719-L721).

The PHREEQpy object is also differentially defined based upon the operating system (https://github.com/freiburgermsu/ROSSpy/blob/main/rosspy/core.py#L47-L50), which suggests that the PHREEQpy object should behave identically in both operating sysytems. What could be the source of this error with different operating systems? How can it be resolved?

I am optimistic that some of you may be able to help clarify and resolve the issue. I can elaborate any details of the error or the corresponding code at anyone's request.

Thank you for your support :)
  Andrew
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 09, 2021, 11:59:48 PM
The DATABASE keyword is disabled for input files in Linux. Here is what my guru says:
================================

I think he needs to make sure the call to phreeqc.load_database() see line 720:

https://github.com/freiburgermsu/ROSSpy/blob/main/examples/other/test_function/executing_rosspy_test.ipynb

is called with the correct path of the pitzer database.  He mentions in the post that the PQI file is using the DATABASE keyword but iphreeqc ignores it, it requires the DB to be loaded using LoadDatabase or in phreeqpy's case load_database().
Title: Re: PHREEQpy in unix
Post by: freiburgermsu on November 11, 2021, 06:15:48 PM
Dear David,

I printed the path in the Notebook (https://github.com/freiburgermsu/ROSSpy/blob/main/examples/other/test_function/executing_rosspy_test.ipynb) and verified that database path
Code: [Select]
/Users/Andrew/Documents/Research/University of Victoria Civil Engineering/ROSSpy/rosspy/databases/pitzer.datis correct via the "Go" function of the Finder, which accepts a path and leads to that location. The acceptance of the path by "Go" suggests that the syntax of the path is correct, although, perhaps some syntactic detail is implicitly understood by Mac OS yet might not be sufficient for PHREEQpy?

I appreciate your guidance :)
   Andrew
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 11, 2021, 06:37:38 PM
Try a quoted string "/users/..." instead of a variable. And do an ls from Python with the same string just to make sure Python can see the file. If it works, you can modify as necessary.
Title: Re: PHREEQpy in unix
Post by: freiburgermsu on November 11, 2021, 06:49:02 PM
I hard-coded the path to the load_database() function and received the same error. What is the syntax for "Is from Python"?
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 11, 2021, 08:43:27 PM
I have no clue, other than you have the wrong path. Here is the example from PhreeqPy website.

Code: [Select]
# create IPhreeqc object
phreeqc = phreeqc_mod.IPhreeqc()
# load database
phreeqc.load_database(r"phreeqc.dat")
# create initial conditions
initial_conditions = make_initial_conditions()
phreeqc.run_string(initial_conditions)
# get components
components = phreeqc.get_component_list()
# create selected output and run it
selected_output = make_selected_output(components)
phreeqc.run_string(selected_output)
# run initial conditions
phc_string = "RUN_CELLS; -cells 0-1\n"
phreeqc.run_string(phc_string)
conc = get_selected_output(phreeqc)
Really? You are the Python programmer.
Title: Re: PHREEQpy in unix
Post by: freiburgermsu on November 12, 2021, 12:29:14 AM
Dear David,

I verified that the path is correct by opening and printing the database file in the Notebook environment (https://github.com/freiburgermsu/ROSSpy/blob/main/examples/other/test_function/executing_rosspy_test.ipynb). I also tried loading the database as a string via the "load_database_string()" function (https://github.com/freiburgermsu/ROSSpy/blob/main/rosspy/core.py#L767-L777), although, this was also not effective. I am not sure how our logic differs from the example, although, I will keep investigating. A potential workaround may be developing a docker image for non-windows operating systems, however, I would definitely prefer to not alienate non-windows users.

Do you mean the "is" keyword of Python
Code: [Select]
self.parameters['database_path'] is str
that generates a boolean from comparing the object class and the object type? I printed the type of the self.parameters['database_path'] object in the Notebook, which revealed that the path object is a string as we would expect.

Thank you for your efforts :)
    Andrew
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 12, 2021, 04:09:43 PM
Try changing to the directory and opening just "pitzer.dat".

Also, check the return code from load_database.
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 12, 2021, 04:52:38 PM
Another possibility is the special characters are causing a problem. Use the string "SOLUTION_MASTER_SPECIES" with load_database_string to see if you get an error message aboult missing species definitions.

Are  you looking at error messages? There should be one with the load_database calls. Use GetErrorString and related methods to look at the error messages.
Title: Re: PHREEQpy in unix
Post by: freiburgermsu on November 12, 2021, 06:15:52 PM
Dear David,

The get_error_string() function in the Notebook console (https://github.com/freiburgermsu/ROSSpy/blob/main/examples/other/test_function/executing_rosspy_test.ipynb) reveals that numerous syntactic errors exist in the database file, which presumably cause loading the database to fail. I suspected that the database structure may differ between the Windows and Unix systems, however, the exact same set of database errors exist when the pitzer.dat database from the current PHREEQC for Mac (phreeqc-3.5.0-14000.dmg) is passed through load_database(). Does the Unix edition of PHREEQpy expect a different database syntax that is not used in the default databases?

Passing "SOLUTION_MASTER_SPECIES" to the load_database_string() function revealed only one error: "ERROR H2O not defined.".

Passing just "pitzer.dat" to load_database() also failed, and the return object is "None".

Thank you for your support :)
    Andrew
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 12, 2021, 06:25:48 PM
The response from only "SOLUTION_MASTER_SPECIES" is what I would expect. It read the string, but PHREEQC requires a minimum set of species, including H2O, H+, OH-, O2, and H2. It processed the string and determined these species were missing.

My latest guess is that the problem is the special characters in pitzer.dat or possibly the end-of-line characters. The syntax of the database is exactly the same.
Title: Re: PHREEQpy in unix
Post by: freiburgermsu on November 12, 2021, 07:46:32 PM
Dear David,

I investigated some of the printed ERRORs relative to the referenced section in the database: e.g.
Code: [Select]
ERROR: Equation has no equal sign.
log_k-4.362
corresponds with this line in the database
Code: [Select]
log_k   -4.362
and
Code: [Select]
ERROR: Unknown option.
ERROR: -Vm 46.1 # 136.14 / 2.95
corresponds with this line in the database
Code: [Select]
-Vm 46.1 # 136.14 / 2.95
.
These database sections seem to both lack special characters and end-of-line denotations, which suggests that perhaps the error has other origins.

I will explore whether I can access the PHREEQpy source code and understand how the operating system changes the database interpretation.

Thank you :)
   Andrew
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 12, 2021, 08:30:42 PM
Ah, I think you have PHREEQC version 2. Let me check on how to update to version 3.
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 13, 2021, 01:56:39 AM
You do not have the latest version of IPhreeqc; you are trying to read a version 3 database with version 2.  You could use a version 2 pitzer.dat, but I think you definitely want features of version 3. Because it is choking on -Vm identifiers (molar volumes), you have a version that is too old to calculate density and solution volume.

I wrote to Mike Mueller, who runs the PhreeqPy site, but you may want to bug him as well, that, if possible, update to  the newest IPhreeqc from https://www.usgs.gov/software/phreeqc-version-3.

Here are directions from Scott to make a shared object of the latest IPhreeqc:

========================================

 He needs to build iphreeqc as a shared library:

Get a recent version of cmake

in a terminal:

$ curl -L -O https://water.usgs.gov/water-resources/software/PHREEQC/iphreeqc-3.7.1-15876.tar.gz
$ tar xvzf iphreeqc-3.7.1-15876
$ cd iphreeqc-3.7.1-15876
$ mkdir _cmake
$ cd _cmake
$ cmake -DBUILD_SHARED_LIBS:BOOL=ON ..
$ cmake --build . --config relwithdebuginfo -j 8

then copy the shared object (something like libiphreeqc.3.6.3.dylib) to  the site-packages/phreeqpy/iphreeqc/ directory
as libiphreeqc.0.dylib

On my linux machine its located at:
~/.local/lib/python3.9/site-packages/phreeqpy/iphreeqc/libiphreeqc.0.dylib


Title: Re: PHREEQpy in unix
Post by: hydrocomputing on November 13, 2021, 09:47:16 AM
Hi Andrew and David,

This Mike, the developer of PhreeqPy. The thread title talks about Unix but the example is for MacOS. For most part Unix (mainly Linux) and MacOS are close enough, but there are some differences such as case sensitivity of file system paths. On Unix paths are case-sensitiv. On MacOS paths case-preserving but not case-sensitive, just like on Windows.

Looks like there are some serious problems reading the database. One reason could the encoding of the database file. In PhreeqPy I assume UTF-8 encoding of all files on Unix and MacOS. Therefore, please try to convert your database file to UTF-8. Useful editors such as Notepad++ (https://notepad-plus-plus.org/) make this easy (Encoding --> Convert to UTF-8).

HTH,
Mike
Title: Re: PHREEQpy in unix
Post by: dlparkhurst on November 13, 2021, 03:30:25 PM
The latest version of PHREEQC programs (https://www.usgs.gov/software/phreeqc-version-3) should have all files encoded as UTF-8 (although one source-code file, print.cpp, is ansi). The database files should be UTF-8.
Title: Re: PHREEQpy in unix
Post by: freiburgermsu on November 13, 2021, 11:48:19 PM
Dear David and Mike,

I will follow the directions from Scott for building a newer version of the PHREEQC library. How will this process update the PHREEQC to version 3 in the PHREEQpy module of PyPI? My goal is that PHREEQpy is updated to version 3, which will enable by extension ROSSpy to be accessible to MacOS researchers.

The pitzer.dat database, which caused the problem that is articulated in the Notebook, was already encoded as UTF-8 according to NotePad++. The same database errors also occurred with the most recent database files, which suggests that the database files are not causing the errors.

Mike, I am willing to work with you towards updating PHREEQpy to version 3, as you need.

Thank you for your support :)
    Andrew
Title: Re: PHREEQpy in unix
Post by: freiburgermsu on November 14, 2021, 04:54:32 AM
Mike, I just pushed the most recent version of ROSSpy (0.0.8) to PyPI for your convenience to
Code: [Select]
pip install rosspythe current package contents.

Thank you :)
    Andrew