PhreeqcUsers Discussion Forum

Registrations currently disabled due to excessive spam. Please email phreeqcusers at gmail.com to request an account.
Welcome Guest
 

  • Forum Home
  • Login
  • Register

  • PhreeqcUsers Discussion Forum »
  • Conceptual Models »
  • Program coupling »
  • Initializing from YAML
« previous next »
  • Print
Pages: [1]   Go Down

Author Topic: Initializing from YAML  (Read 1226 times)

Portega

  • Contributor
  • Posts: 5
Initializing from YAML
« on: 02/08/24 06:00 »
Hi All,

I'm failing epically to initialize the phreeqcrm class from a YAML file. My initial guess is that I'm writing the YAML incorrectly, but I can't figure out where.

I have pasted the function I'm using to initialize from YAML and the YAML file. Any help would be appreciated.

Code: [Select]
def initialize_phreeqcrm_yaml(yamlfile, nxyz):

    #initialize phreeqc from yaml file
    phreeqc_rm = phreeqcrm.PhreeqcRM(nxyz, 1)
    status = phreeqc_rm.InitializeYAML(yamlfile)
    status = phreeqc_rm.SetScreenOn(True)
    sout_columns = phreeqc_rm.GetSelectedOutputHeadings()

    soutdf = pd.DataFrame(columns = sout_columns)
    return phreeqc_rm, soutdf, sout_columns

Code: [Select]
- key: SetComponentH2O
  tf: false
- key: UseSolutionDensityVolume
  tf: false
- key: SetFilePrefix
  prefix: test01\_phreeqc
- key: OpenFiles
- key: SetUnitsSolution
  option: 2
- key: SetPorosity
  por: !!python/object/apply:numpy.core.multiarray._reconstruct
    args:
    - &id001 !!python/name:numpy.ndarray ''
    - !!python/tuple
      - 0
    - !!binary |
      Yg==
    state: !!python/tuple
    - 1
    - !!python/tuple
      - 50
    - !!python/object/apply:numpy.dtype
      args:
      - f8
      - false
      - true
      state: !!python/tuple
      - 3
      - <
      - null
      - null
      - null
      - -1
      - -1
      - 0
    - false
    - !!binary |
      AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8A
      AAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAA
      AAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAA
      AAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAA
      AADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAA
      APA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA
      8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADw
      Pw==
- key: SetPrintChemistryMask
  cell_mask: !!python/object/apply:numpy.core.multiarray._reconstruct
    args:
    - *id001
    - !!python/tuple
      - 0
    - !!binary |
      Yg==
    state: !!python/tuple
    - 1
    - !!python/tuple
      - 50
    - &id002 !!python/object/apply:numpy.dtype
      args:
      - i4
      - false
      - true
      state: !!python/tuple
      - 3
      - <
      - null
      - null
      - null
      - -1
      - -1
      - 0
    - false
    - !!binary |
      AQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAAB
      AAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEA
      AAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAA
      AAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAA=
- key: SetPrintChemistryOn
  workers: false
  initial_phreeqc: true
  utility: false
- key: LoadDatabase
  database: database\pht3d_datab.dat
- key: RunFile
  workers: true
  initial_phreeqc: true
  utility: true
  chemistry_name: test01\phinp.dat
- key: RunString
  workers: true
  initial_phreeqc: false
  utility: true
  input_string: DELETE; -all
- key: SetTime
  time: 0.0
- key: SetTimeStep
  time_step: 0.0
- key: SetGridCellCount
  count: 50
- key: InitialPhreeqc2Module
  ic: !!python/object/apply:numpy.core.multiarray._reconstruct
    args:
    - *id001
    - !!python/tuple
      - 0
    - !!binary |
      Yg==
    state: !!python/tuple
    - 1
    - !!python/tuple
      - 350
    - *id002
    - false
    - !!binary |
      AQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAAB
      AAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEA
      AAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAA
      AAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAA
      AQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAAB
      AAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEA
      AAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAA
      AP//////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////
      //////////////////////////////////////////8=
- key: RunCells


Cheers,
P
Logged

dlparkhurst

  • Global Moderator
  • *****
  • Posts: 4074
Re: Initializing from YAML
« Reply #1 on: 02/08/24 09:31 »
Looks like you are using YAML features that I have never tried, and honestly, know nothing about.

I suggest that you first use the yamlphreeqcrm class to write the YAML file and make sure that that YAML file works. You could then try replacing, say the porosity definition in the file, with the binary format you are trying to use.

PhreeqcRM loads the YAML file and processes the file in the method InitializeYAML in the file PhreeqcRM.cpp. It sequentially goes through the nodes of the YAML file and runs the corresponding C++ method. For SetPorosity, it expects to extract a vector of length nxyz.

Here is an example of the use of yamlphreeqcrm that is found in the file WriteYAMLFile_py.py in the swig/python directory:

Code: [Select]
import numpy as np
import yamlphreeqcrm

from constants import FilePaths

def WriteYAMLFile_py():
# Create YAMLPhreeqcRM document
yrm = yamlphreeqcrm.YAMLPhreeqcRM()
# Number of cells
nxyz = 40;
# Set GridCellCount
yrm.YAMLSetGridCellCount(nxyz)
# Set ThreadCount
yrm.YAMLThreadCount(3)
# Set some properties
yrm.YAMLSetErrorHandlerMode(1)
yrm.YAMLSetComponentH2O(False)
yrm.YAMLSetRebalanceFraction(0.5)
yrm.YAMLSetRebalanceByCell(True)
yrm.YAMLUseSolutionDensityVolume(False)
yrm.YAMLSetPartitionUZSolids(False)
# Open files
yrm.YAMLSetFilePrefix("AdvectBMI_py");
yrm.YAMLOpenFiles();
    # Set concentration units
yrm.YAMLSetUnitsSolution(2)           # 1, mg/L; 2, mol/L; 3, kg/kgs
yrm.YAMLSetUnitsPPassemblage(1)       # 0, mol/L cell; 1, mol/L water; 2 mol/L rock
yrm.YAMLSetUnitsExchange(1)           # 0, mol/L cell; 1, mol/L water; 2 mol/L rock
yrm.YAMLSetUnitsSurface(1)            # 0, mol/L cell; 1, mol/L water; 2 mol/L rock
yrm.YAMLSetUnitsGasPhase(1)           # 0, mol/L cell; 1, mol/L water; 2 mol/L rock
yrm.YAMLSetUnitsSSassemblage(1)       # 0, mol/L cell; 1, mol/L water; 2 mol/L rock
yrm.YAMLSetUnitsKinetics(1)           # 0, mol/L cell; 1, mol/L water; 2 mol/L rock
# Set conversion from seconds to user units (days) Only affects one print statement
time_conversion = 1.0 / 86400.0
yrm.YAMLSetTimeConversion(time_conversion)
# Set representative volume
rv = [1] * nxyz
yrm.YAMLSetRepresentativeVolume(rv)
# Set initial density
density = [1.0] * nxyz
yrm.YAMLSetDensityUser(density)
    # Set initial porosity
por = [0.2] * nxyz
yrm.YAMLSetPorosity(por)
# Set initial saturation
sat = [1] * nxyz
yrm.YAMLSetSaturationUser(sat)   
# Set cells to print chemistry when print chemistry is turned on
print_chemistry_mask = [0] * nxyz
for i in range(nxyz // 2):
print_chemistry_mask[i] = 1
yrm.YAMLSetPrintChemistryMask(print_chemistry_mask) 
# Demonstation of mapping, two equivalent rows by symmetry
    # zero-based indexing
grid2chem = [-1] * nxyz
for i in range(nxyz // 2):
grid2chem[i] = i
grid2chem[i + nxyz // 2] = i
yrm.YAMLCreateMapping(grid2chem)
# Set printing of chemistry file
yrm.YAMLSetPrintChemistryOn(False, True, False) # workers, initial_phreeqc, utility
# Load database
yrm.YAMLLoadDatabase("phreeqc.dat")   
    # Run file to define solutions and reactants for initial conditions, selected output
workers = True             # Worker instances do the reaction calculations for transport
initial_phreeqc = True     # InitialPhreeqc instance accumulates initial and boundary conditions
utility = True             # Utility instance is available for processing
yrm.YAMLRunFile(workers, initial_phreeqc, utility, "advect.pqi")
# Clear contents of workers and utility
initial_phreeqc = False
input = "DELETE; -all"
yrm.YAMLRunString(workers, initial_phreeqc, utility, input)
yrm.YAMLAddOutputVars("AddOutputVars", "true")
# Determine number of components to transport
yrm.YAMLFindComponents()
# set array of initial conditions
ic1 = [-1]*(nxyz*7)
ic2 = [-1]*(nxyz*7)
f1 = [1]*(nxyz*7)
for i in range(nxyz):
ic1[i] = 1;                # Solution 1
ic1[nxyz + i] = -1;        # Equilibrium phases none
ic1[2 * nxyz + i] = 1;     # Exchange 1
ic1[3 * nxyz + i] = -1;    # Surface none
ic1[4 * nxyz + i] = -1;    # Gas phase none
ic1[5 * nxyz + i] = -1;    # Solid solutions none
ic1[6 * nxyz + i] = -1;    # Kinetics none
yrm.YAMLInitialPhreeqc2Module_mix(ic1, ic2, f1)   
# No mixing is defined, so the following is equivalent
#yrm.YAMLInitialPhreeqc2Module(ic1)

# alternative for setting initial conditions
# cell number in first argument (-1 indicates last solution, 40 in this case)
# in advect.pqi and any reactants with the same number--
# Equilibrium phases, exchange, surface, gas phase, solid solution, and (or) kinetics--
# will be written to cells 18 and 19 (0 based)
module_cells = [18, 19]
yrm.YAMLInitialPhreeqcCell2Module(-1, module_cells)
# Initial equilibration of cells
time_step = 0.0    # no kinetics
yrm.YAMLSetTimeStep(time_step)
time = 0.0
yrm.YAMLSetTime(time)
yrm.YAMLRunCells()
time_step = 86400.0
yrm.YAMLSetTimeStep(time_step)   
# Write YAML file
yrm.WriteYAMLDoc(FilePaths.YAML)
print("Done.")

if __name__ == '__main__':
    WriteYAMLFile_py()
           

Logged

Portega

  • Contributor
  • Posts: 5
Re: Initializing from YAML
« Reply #2 on: 02/08/24 13:15 »
Thank you, David!

Actually, I'm using the same function to build this YAML file. I'm getting those weird binaries when I give the YAML function a NumPy array coming from an attribute like:

Code: [Select]
phreeqcrm_yaml.YAMLInitialPhreeqc2Module(self.ic1_flatten)

where ic1_flatten is a 1D array with length 7*nxyz.

The YAML file is written correctly if I do:

Code: [Select]
phreeqcrm_yaml.YAMLInitialPhreeqc2Module([1] * (7*nxyz))

Thank you again!
Logged

Portega

  • Contributor
  • Posts: 5
Re: Initializing from YAML
« Reply #3 on: 07/08/24 03:56 »
I managed to write the numpy arrays as lists by adding a representer to yaml

See https://stackoverflow.com/questions/75508283/dump-numpy-array-to-yaml-as-regular-list

Code: [Select]
def ndarray_representer(dumper: yaml.Dumper, array: np.ndarray) -> yaml.Node:
    return dumper.represent_list(array.tolist())

yaml.add_representer(np.ndarray, ndarray_representer)

Cheers,
P
Logged

dlparkhurst

  • Global Moderator
  • *****
  • Posts: 4074
Re: Initializing from YAML
« Reply #4 on: 07/08/24 06:52 »
Yeah, my Python is not the best. I'll try to make some modifications so that the methods work with various types--lists, tuples, and numpy arrays. Right now my guess is that only lists are handled correctly, but I'll have to check.
Logged

dlparkhurst

  • Global Moderator
  • *****
  • Posts: 4074
Re: Initializing from YAML
« Reply #5 on: 08/08/24 17:56 »
I have sent a message to Portega with a potential fix. If anyone else would like to be a beta tester, send me a message.

We are working on pip and conda 3.8.0 versions for Python, which should be available soon.
Logged

  • Print
Pages: [1]   Go Up
« previous next »
  • PhreeqcUsers Discussion Forum »
  • Conceptual Models »
  • Program coupling »
  • Initializing from YAML
 

  • SMF 2.0.19 | SMF © 2021, Simple Machines | Terms and Policies
  • XHTML
  • RSS
  • WAP2