PhreeqcRM
PhreeqcRM.h
Go to the documentation of this file.
1
9#if !defined(PHREEQCRM_H_INCLUDED)
10#define PHREEQCRM_H_INCLUDED
11#ifdef USE_MPI
12#include "mpi.h"
13#define MP_TYPE MPI_Comm
14#else
15#define MP_TYPE int
16#endif
17class IPhreeqcPhast;
18
19class cxxStorageBin;
20//class cxxNameDouble;
21#include "NameDouble.h"
22class cxxSolution;
23class PHRQ_io;
24#include <vector>
25#include <list>
26#include <set>
27#include <map>
28#include <mutex>
29#include <string>
30#include "RMVARS.h"
31
32#include "irm_dll_export.h"
33
34#if SWIG
35#define IRM_DLL_EXPORT
36#endif
37
38class PHRQ_io;
39class IPhreeqc;
40//class BMI_Var;
47class IRM_DLL_EXPORT PhreeqcRMStop : public std::exception
48{
49public:
50 const char *what() const throw () {return "Failure in PhreeqcRM\n";}
51};
52//class LetItThrow : public std::logic_error {
53//public:
54// LetItThrow(std::string error_string) : std::logic_error(error_string.c_str()) { };
55//};
58#include "IrmResult.h"
59enum {
121} /* MPI_METHOD */;
122
123
124template<typename T>
126{
127public:
129 {
130 const std::lock_guard<std::mutex> lock(StaticIndexer<T>::_InstancesLock);
133 }
134
136 {
137 const std::lock_guard<std::mutex> lock(StaticIndexer<T>::_InstancesLock);
138 auto search = StaticIndexer<T>::_Instances.find(this->_Index);
139 assert(search != StaticIndexer<T>::_Instances.end());
140 if (search != StaticIndexer<T>::_Instances.end())
141 {
142 StaticIndexer<T>::_Instances.erase(search);
143 }
144 }
145
146 static T* GetInstance(int id)
147 {
148 const std::lock_guard<std::mutex> lock(_InstancesLock);
149 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
150 if (search != StaticIndexer<T>::_Instances.end())
151 {
152 return search->second;
153 }
154 return nullptr;
155 }
156
157 template<typename Derived>
158 static Derived* GetInstance(int id)
159 {
160 const std::lock_guard<std::mutex> lock(_InstancesLock);
161 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
162 if (search != StaticIndexer<T>::_Instances.end())
163 {
164 if (Derived* derived = dynamic_cast<Derived*>(search->second))
165 {
166 return derived;
167 }
168 }
169 return nullptr;
170 }
171
172 static IRM_RESULT Destroy(int id)
173 {
174 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
175 if (search != StaticIndexer<T>::_Instances.end())
176 {
177 assert(dynamic_cast<T*>(search->second));
178 delete search->second;
179 return IRM_OK;
180 }
181 return IRM_BADINSTANCE;
182 }
183
184 //template<typename Derived>
185 //static IRM_RESULT Destroy(int id)
186 //{
187 // auto search = StaticIndexer<T>::_Instances.find(size_t(id));
188 // if (search != StaticIndexer<T>::_Instances.end())
189 // {
190 // assert(dynamic_cast<T*>(search->second));
191 // if (Derived* derived = dynamic_cast<Derived*>(search->second))
192 // {
193 // delete derived;
194 // return IRM_OK;
195 // }
196 // }
197 // return IRM_BADINSTANCE;
198 //}
199
200 static void DestroyAll()
201 {
202 std::list<T*> items;
203 for (auto pair : StaticIndexer<T>::_Instances)
204 {
205 assert(dynamic_cast<T*>(pair.second));
206 items.push_back(pair.second);
207 }
208 for (auto item : items)
209 {
210 delete item;
211 }
212 }
213
214 //template<typename Derived>
215 //static void DestroyAll()
216 //{
217 // const std::lock_guard<std::mutex> lock(_InstancesLock);
218 // std::list<Derived*> derived_items;
219 // for (auto pair : StaticIndexer<T>::_Instances)
220 // {
221 // assert(dynamic_cast<T*>(pair.second));
222 // if (Derived* derived = dynamic_cast<Derived*>(pair.second))
223 // {
224 // derived_items.push_back(derived);
225 // }
226 // }
227 // for (auto item : derived_items)
228 // {
229 // delete item;
230 // }
231 //}
232
234 {
235 return (int)this->_Index;
236 }
237
238protected:
239 size_t _Index;
240
241 static std::mutex _InstancesLock;
242 static std::map<size_t, T*> _Instances;
243 static size_t _InstancesIndex;
244};
245
246// static members of StaticIndexer
247
248template<typename T>
250
251template<typename T>
252std::map<size_t, T*> StaticIndexer<T>::_Instances;
253
254template<typename T>
256
257
258
266class IRM_DLL_EXPORT PhreeqcRM : public StaticIndexer<PhreeqcRM>
267{
268public:
270 static int CreateReactionModule(int nxyz, MP_TYPE nthreads);
272private:
273 virtual void AddOutputVars(std::string option, std::string def);
274 virtual void ClearBMISelectedOutput();
275 virtual void GenerateAutoOutputVars();
276 virtual void UpdateBMI(RMVARS v_enum);
277public:
278#ifdef USE_YAML
306 static int GetGridCellCountYAML(const char* YAML_file);
307#endif // #ifdef USE_YAML
308
371 PhreeqcRM(int nxyz, MP_TYPE thread_count_or_communicator, PHRQ_io * io=NULL, bool delay_construct=false);
372 virtual ~PhreeqcRM(void);
435 IPhreeqc * Concentrations2Utility(const std::vector< double > &c,
436 const std::vector< double > &tc, const std::vector< double > &p_atm);
472 IRM_RESULT CreateMapping(const std::vector< int > &grid2chem);
506 void DecodeError(int result);
528 IRM_RESULT DumpModule(bool dump_on, bool append = false);
551 void ErrorHandler(int result, const std::string &e_string);
568 void ErrorMessage(const std::string &error_string, bool prepend = true);
676 const std::vector < std::vector <int> > & GetBackwardMapping(void) {return this->backward_mapping;}
678 void GetBackwardMappingSWIG(std::vector<int>& nback_output, std::vector<int>& cellnumbers_output);
702 int GetChemistryCellCount(void) const {return this->count_chemistry;}
722 int GetComponentCount(void) const {return (int) this->components.size();}
747const std::vector< std::string > & GetComponents(void) const {return this->components;}
748
789 IRM_RESULT GetConcentrations(std::vector< double > &c_output);
855 std::string GetDatabaseFileName(void) {return this->database_file_name;}
882 IRM_RESULT GetDensityCalculated(std::vector< double > & d_output);
883 IRM_RESULT GetDensity(std::vector< double >& d_output);
919 const std::vector < int> & GetEndCell(void) {return this->end_cell;}
949const std::vector< std::string > & GetEquilibriumPhases(void) const { return this->EquilibriumPhasesList; }
977int GetEquilibriumPhasesCount(void) const { return (int) this->EquilibriumPhasesList.size(); }
978
979
1000 int GetErrorHandlerMode(void) {return this->error_handler_mode;}
1019 std::string GetErrorString(void);
1020
1056const std::vector< std::string > & GetExchangeNames(void) const { return this->ExchangeNamesList; }
1091const std::vector< std::string > & GetExchangeSpecies(void) const { return this->ExchangeSpeciesNamesList; }
1123int GetExchangeSpeciesCount(void) const { return (int) this->ExchangeSpeciesNamesList.size(); }
1124
1125
1143 std::string GetFilePrefix(void) {return this->file_prefix;}
1164const std::vector < int > & GetForwardMapping(void) {return this->forward_mapping_root;}
1165
1195const std::vector< std::string > & GetGasComponents(void) const { return this->GasComponentsList; }
1223int GetGasComponentsCount(void) const { return (int) this->GasComponentsList.size(); }
1224
1257IRM_RESULT GetGasCompMoles(std::vector< double >& gas_moles_output);
1258
1292IRM_RESULT GetGasCompPhi(std::vector< double >& gas_phi);
1293
1326IRM_RESULT GetGasCompPressures(std::vector< double >& gas_pressure);
1327
1359IRM_RESULT GetGasPhaseVolume(std::vector< double >& gas_volume_output);
1360
1387 const std::vector < double > & GetGfw(void) {return this->gfw;}
1409 int GetGridCellCount(void) {return this->nxyz;}
1436IPhreeqc * GetIPhreeqcPointer(int i);
1463IRM_RESULT GetIthConcentration(int i, std::vector< double >& c_output);
1493IRM_RESULT GetIthSpeciesConcentration(int i, std::vector< double >& c_output);
1494
1524const std::vector< std::string > & GetKineticReactions(void) const { return this->KineticReactionsList; }
1552int GetKineticReactionsCount(void) const { return (int) this->KineticReactionsList.size(); }
1553
1578 int GetMpiMyself(void) const {return this->mpi_myself;}
1604 int GetMpiTasks(void) const {return this->mpi_tasks;}
1682 bool GetPartitionUZSolids(void) const {return this->partition_uz_solids;}
1683#ifdef USE_RV
1704 std::vector< double > & GetPoreVolume(void) {return this->pore_volume;}
1705#endif
1725 const std::vector< double >& GetPorosity(void);
1747 const std::vector< double > & GetPressure(void);
1768 const std::vector< int > & GetPrintChemistryMask (void) {return this->print_chem_mask_root;}
1797 const std::vector <bool> & GetPrintChemistryOn(void) {return this->print_chemistry_on;}
1821 bool GetRebalanceByCell(void) const {return this->rebalance_by_cell;}
1846 double GetRebalanceFraction(void) const {return this->rebalance_fraction;}
1882IRM_RESULT GetSaturationCalculated(std::vector< double > & sat_output);
1883IRM_RESULT GetSaturation(std::vector< double >& sat_output);
1923 IRM_RESULT GetSelectedOutput(std::vector< double > &s_output);
2050 IRM_RESULT GetSelectedOutputHeading(int icol, std::string &heading);
2095 IRM_RESULT GetSelectedOutputHeadings(std::vector< std::string >& headings);
2124 bool GetSelectedOutputOn(void) {return this->selected_output_on;}
2173
2200int GetSICount(void) const { return (int) this->SINamesList.size(); }
2234const std::vector< std::string > & GetSINames(void) const { return this->SINamesList; }
2235
2271const std::vector< std::string > & GetSolidSolutionComponents(void) const { return this->SolidSolutionComponentsList; }
2304int GetSolidSolutionComponentsCount(void) const { return (int) this->SolidSolutionComponentsList.size(); }
2305
2342const std::vector< std::string > & GetSolidSolutionNames(void) const { return this->SolidSolutionNamesList; }
2343
2364 const std::vector< double > & GetSolutionVolume(void);
2410 IRM_RESULT GetSpeciesConcentrations(std::vector< double > & species_conc_output);
2442 int GetSpeciesCount(void) {return (int) this->species_names.size();}
2477 const std::vector< double > & GetSpeciesD25(void) {return this->species_d_25;}
2519 IRM_RESULT GetSpeciesLog10Gammas(std::vector< double > & species_log10gammas);
2520
2561IRM_RESULT GetSpeciesLog10Molalities(std::vector< double >& species_log10molalities);
2562
2596 const std::vector< std::string > & GetSpeciesNames(void) {return this->species_names;}
2632 bool GetSpeciesSaveOn(void) {return this->species_save_on;}
2633
2683 const std::vector<cxxNameDouble> & GetSpeciesStoichiometry(void) {return this->species_stoichiometry;}
2685 void GetSpeciesStoichiometrySWIG(std::vector<std::string>& species_output,
2686 std::vector<int>& nelt_output, std::vector<std::string>& elts_output, std::vector<double>& coef_output);
2719 const std::vector< double > & GetSpeciesZ(void) {return this->species_z;}
2754const std::vector < int> & GetStartCell(void) {return this->start_cell;}
2755
2792const std::vector< std::string > & GetSurfaceNames(void) const { return this->SurfaceNamesList; }
2793
2829const std::vector< std::string > & GetSurfaceSpecies(void) const { return this->SurfaceSpeciesNamesList; }
2830
2863int GetSurfaceSpeciesCount(void) const { return (int) this->SurfaceSpeciesNamesList.size(); }
2864
2865
2903const std::vector< std::string > & GetSurfaceTypes(void) const { return this->SurfaceTypesList; }
2904
2925 //const std::vector< double > & GetTemperature(void) {return this->tempc;}
2926 const std::vector< double > & GetTemperature(void);
2927 //int PhreeqcRM::GetVarItemsize(const std::string name);
2928 //int PhreeqcRM::GetVarNbytes(const std::string name);
2929
2950 int GetThreadCount() {return this->nthreads;}
2973 double GetTime(void) const {return this->time;}
2998 double GetTimeConversion(void) {return this->time_conversion;}
3022 double GetTimeStep(void) {return this->time_step;}
3047 int GetUnitsExchange(void) {return this->units_Exchange;}
3072 int GetUnitsGasPhase(void) {return this->units_GasPhase;}
3097 int GetUnitsKinetics(void) {return this->units_Kinetics;}
3122 int GetUnitsPPassemblage(void) {return this->units_PPassemblage;}
3177 int GetUnitsSolution(void) {return this->units_Solution;}
3202 int GetUnitsSSassemblage(void) {return this->units_SSassemblage;}
3227 int GetUnitsSurface(void) {return this->units_Surface;}
3247 const std::vector<double>& GetViscosity();
3248
3249
3272 const std::vector<IPhreeqcPhast *> & GetWorkers() {return this->workers;}
3273#ifdef USE_YAML
3393IRM_RESULT InitializeYAML(std::string yamlfile);
3394#endif
3422IRM_RESULT InitialPhreeqc2Concentrations(std::vector < double > & destination_c,
3423 const std::vector < int > & boundary_solution1);
3463 std::vector < double > & destination_c,
3464 const std::vector < int > & boundary_solution1,
3465 const std::vector < int > & boundary_solution2,
3466 const std::vector < double > & fraction1);
3496IRM_RESULT InitialEquilibriumPhases2Module(const std::vector < int >& equilibrium_phases);
3526IRM_RESULT InitialExchanges2Module(const std::vector < int >& exchanges);
3555IRM_RESULT InitialGasPhases2Module(const std::vector < int >& gas_phases);
3584IRM_RESULT InitialKinetics2Module(const std::vector < int >& kinetics);
3625IRM_RESULT InitialPhreeqc2Module(const std::vector < int >& initial_conditions1);
3689 const std::vector < int >& initial_conditions1,
3690 const std::vector < int >& initial_conditions2,
3691 const std::vector < double >& fraction1);
3721IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector < double >& destination_c,
3722 const std::vector < int >& boundary_solution1);
3762IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector < double >& destination_c,
3763 const std::vector < int >& boundary_solution1,
3764 const std::vector < int >& boundary_solution2,
3765 const std::vector < double >& fraction1);
3793 const std::vector< int >& cell_numbers);
3794
3823IRM_RESULT InitialSolidSolutions2Module(const std::vector < int >& solid_solutions);
3853IRM_RESULT InitialSolutions2Module(const std::vector < int >& solutions);
3882IRM_RESULT InitialSurfaces2Module(const std::vector < int >& surfaces);
3883
3900 IRM_RESULT LoadDatabase(const std::string &database);
3919 void LogMessage(const std::string &str);
4047 void OutputMessage(const std::string &str);
4068 IRM_RESULT ReturnHandler(IRM_RESULT result, const std::string& e_string);
4069
4070
4128 IRM_RESULT RunFile(bool workers, bool initial_phreeqc, bool utility, const std::string & chemistry_name);
4155 IRM_RESULT RunString(bool workers, bool initial_phreeqc, bool utility, const std::string & input_string);
4178 void ScreenMessage(const std::string &str);
4245 IRM_RESULT SetConcentrations(const std::vector< double > &c);
4309 IRM_RESULT SetDensityUser(const std::vector< double > &density);
4310 IRM_RESULT SetDensity(const std::vector< double >& density);
4330 IRM_RESULT SetDumpFileName(const std::string & dump_name);
4390 IRM_RESULT SetFilePrefix(const std::string & prefix);
4391
4427IRM_RESULT SetGasCompMoles(const std::vector< double >& gas_moles);
4466IRM_RESULT SetGasPhaseVolume(const std::vector< double >& gas_volume);
4493IRM_RESULT SetIthConcentration(int i, std::vector< double >& c);
4523IRM_RESULT SetIthSpeciesConcentration(int i, std::vector< double >& c);
4524
4620 IRM_RESULT SetMpiWorkerCallbackC(int (*fcn)(int *method, void * cookie));
4696 IRM_RESULT SetMpiWorkerCallbackFortran(int (*fcn)(int *method));
4806 IRM_RESULT SetPorosity(const std::vector< double > &por);
4807
4828 IRM_RESULT SetPressure(const std::vector< double > &p);
4855 IRM_RESULT SetPrintChemistryMask(const std::vector<int> & cell_mask);
4890 IRM_RESULT SetPrintChemistryOn(bool workers, bool initial_phreeqc, bool utility);
4976 IRM_RESULT SetRepresentativeVolume(const std::vector< double > &rv);
5006 IRM_RESULT SetSaturationUser(const std::vector< double > &sat);
5007 IRM_RESULT SetSaturation(const std::vector< double >& sat);
5122 IRM_RESULT SetTemperature(const std::vector< double > &t);
5140 IRM_RESULT SetTime(double time);
5159 IRM_RESULT SetTimeConversion(double conv_factor);
5179 IRM_RESULT SetTimeStep(double time_step);
5492IRM_RESULT SpeciesConcentrations2Module(const std::vector< double > & species_conc);
5493
5628 void WarningMessage(const std::string &warnstr);
5629public:
5630// Utilities
5631 static std::string Char2TrimString(const char * str, size_t l = 0);
5632 static bool FileExists(const std::string &name);
5633 static void FileRename(const std::string &temp_name, const std::string &name,
5634 const std::string &backup_name);
5635 static IRM_RESULT Int2IrmResult(int r, bool positive_ok);
5636private:
5637 IRM_RESULT CellInitialize(
5638 int i,
5639 int n_user_new,
5640 const int *initial_conditions1,
5641 const int *initial_conditions2,
5642 double *fraction1,
5643 std::set<std::string> &error_set);
5644 IRM_RESULT CheckCells();
5645 int CheckSelectedOutput();
5646 //void Collapse2Nchem(double *d_in, double *d_out);
5647 //void Collapse2Nchem(int *i_in, int *i_out);
5648 IPhreeqc * Concentrations2UtilityH2O(const std::vector< double > &c_in,
5649 const std::vector< double > &t_in, const std::vector< double > &p_in);
5650 IPhreeqc * Concentrations2UtilityNoH2O(const std::vector< double > &c_in,
5651 const std::vector< double > &t_in, const std::vector< double > &p_in);
5652 void Concentrations2Solutions(int n, std::vector< double > &c);
5653 void Concentrations2SolutionsH2O(int n, std::vector< double > &c);
5654 void Concentrations2SolutionsNoH2O(int n, std::vector< double > &c);
5655 void cxxSolution2concentration(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5656 void cxxSolution2concentrationH2O(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5657 void cxxSolution2concentrationNoH2O(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5658 void GatherNchem(std::vector< double > &source, std::vector< double > &destination);
5659 cxxStorageBin & Get_phreeqc_bin(void) {return *this->phreeqc_bin;}
5660 IRM_RESULT HandleErrorsInternal(std::vector< int > & r);
5661 void PartitionUZ(int n, int iphrq, int ihst, double new_frac);
5662 void RebalanceLoad(void);
5663 void RebalanceLoadPerCell(void);
5664 IRM_RESULT RunCellsThread(int i);
5665 IRM_RESULT RunFileThread(int n);
5666 IRM_RESULT RunStringThread(int n, std::string & input);
5667 IRM_RESULT RunCellsThreadNoPrint(int n);
5668 void Scale_solids(int n, int iphrq, double frac);
5669 void ScatterNchem(double *d_array);
5670 void ScatterNchem(int *i_array);
5671 void ScatterNchem(std::vector< double > &source, std::vector< double > &destination);
5672 void ScatterNchem(std::vector< int > &source, std::vector< int > &destination);
5673 IRM_RESULT SetChemistryFileName(const char * prefix = NULL);
5674 IRM_RESULT SetDatabaseFileName(const char * db = NULL);
5675 void SetEndCells(void);
5676 void SetEndCellsHeterogeneous(void);
5677 double TimeStandardTask(void);
5678 IRM_RESULT TransferCells(cxxStorageBin &t_bin, int old, int nnew);
5679 IRM_RESULT TransferCellsUZ(std::ostringstream &raw_stream, int old, int nnew);
5680
5681private:
5682 //IRM_RESULT SetGeneric(std::vector< double > &destination, int newSize, const std::vector< double > &origin, int mpiMethod, const std::string &name, const double newValue = 0.0);
5683 IRM_RESULT SetGeneric(const std::vector< double > &source, std::vector< double > &destination_root, std::vector< double > &destination_worker, int mpiMethod, const std::string &name);
5684public:
5685
5686protected:
5687
5688#if defined(_MSC_VER)
5689/* disable warning C4251: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' */
5690#pragma warning(disable:4251)
5691#endif
5693 bool component_h2o; // true: use H2O, excess H, excess O, and charge;
5694 // false total H, total O, and charge
5695 std::string database_file_name;
5696 std::string chemistry_file_name;
5697 std::string dump_file_name;
5698 std::string file_prefix;
5699 cxxStorageBin * phreeqc_bin;
5700 int mpi_myself;
5701 int mpi_tasks;
5702 std::vector <std::string> components; // list of components to be transported
5703 std::vector <double> gfw; // gram formula weights converting mass to moles (1 for each component)
5704 double gfw_water; // gfw of water
5705 bool partition_uz_solids;
5706 int nxyz; // number of nodes
5707 int count_chemistry; // number of cells for chemistry
5708 double time; // time from transport, sec
5709 double time_step; // time step from transport, sec
5710 double time_conversion; // time conversion factor, multiply to convert to preferred time unit for output
5711 std::vector <double> CurrentConcentrations;
5712 std::vector <double> CurrentSpeciesConcentrations;
5713 std::vector <double> IthCurrentConcentrations;
5714 std::vector <double> IthCurrentSpeciesConcentrations;
5715 std::set<int> IthConcentrationSet;
5716 std::set<int> IthSpeciesConcentrationSet;
5717 std::vector <double> old_saturation_root; // saturation fraction from previous step
5718 std::vector <double> old_saturation_worker;
5719 std::vector< double > saturation_root; // nxyz saturation fraction
5720 std::vector< double > saturation_worker; // nchem on workers saturation fraction
5721 std::vector< double > pressure_root; // nxyz on root current pressure
5722 std::vector< double > pressure_worker; // nchem on workers current pressure
5723 std::vector< double > rv_root; // nxyz on root representative volume
5724 std::vector< double > rv_worker; // nchem on workers representative volume
5725 std::vector< double > porosity_root; // nxyz porosity
5726 std::vector< double > porosity_worker; // nchem on workers porosity
5727 std::vector< double > tempc_root; // nxyz on root temperature Celsius
5728 std::vector< double > tempc_worker; // nchem on workers temperature Celsius
5729 std::vector< double > density_root; // nxyz density
5730 std::vector< double > density_worker; // nchem on workers density
5731 std::vector< double > viscosity_root; // nxyz viscosity
5732 std::vector< double > viscosity_worker; // nchem on workers viscosity
5733 std::vector< double > solution_volume_root; // nxyz on root solution volume
5734 std::vector< double > solution_volume_worker; // nchem on workers solution_volume
5735 std::vector< int > print_chem_mask_root; // nxyz print flags for output file
5736 std::vector< int > print_chem_mask_worker; // nchem print flags for output file
5737 bool rebalance_by_cell; // rebalance method 0 std, 1 by_cell
5738 double rebalance_fraction; // parameter for rebalancing process load for parallel
5739 int units_Solution; // 1 mg/L, 2 mol/L, 3 kg/kgs
5740 int units_PPassemblage; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5741 int units_Exchange; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5742 int units_Surface; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5743 int units_GasPhase; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5744 int units_SSassemblage; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5745 int units_Kinetics; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5746 std::vector <int> forward_mapping_root; // mapping from nxyz cells to count_chem chemistry cells
5747 std::vector <std::vector <int> > backward_mapping; // mapping from count_chem chemistry cells to nxyz cells
5748 bool use_solution_density_volume;
5749 bool worker_waiting;
5750 // print flags
5751 std::vector< bool > print_chemistry_on; // print flag for chemistry output file
5752 bool selected_output_on; // create selected output
5753
5754 int error_count;
5755 int error_handler_mode; // 0, return code; 1, throw; 2 exit;
5756 bool need_error_check;
5757 std::string phreeqcrm_error_string;
5758
5759 std::map<std::string, int> method_map;
5760 // threading
5761 int nthreads;
5762 std::vector<IPhreeqcPhast *> workers;
5763 //std::vector< int > start_cell;
5764 //std::vector< int > end_cell;
5765 PHRQ_io *phreeqcrm_io;
5766 bool delete_phreeqcrm_io;
5767
5768 // mpi
5769#ifdef USE_MPI
5770 MPI_Comm phreeqcrm_comm; // MPI communicator
5771#endif
5772 int (*mpi_worker_callback_fortran) (int *method);
5773 int (*mpi_worker_callback_c) (int *method, void *cookie);
5774 void *mpi_worker_callback_cookie;
5775
5776 // mcd
5777 bool species_save_on;
5778 std::vector <std::string> species_names;
5779 std::vector <double> species_z;
5780 std::vector <double> species_d_25;
5781 std::vector <cxxNameDouble> species_stoichiometry;
5782 std::map<int, int> s_num2rm_species_num;
5783 std::vector< double > standard_task_vector; // root only
5784
5785 std::vector< int > start_cell;
5786 std::vector< int > end_cell;
5787 // reactant lists
5788 std::vector <std::string> ExchangeSpeciesNamesList;
5789 std::vector <std::string> ExchangeNamesList;
5790 std::vector <std::string> SurfaceSpeciesNamesList;
5791 std::vector <std::string> SurfaceTypesList;
5792 std::vector <std::string> SurfaceNamesList;
5793
5794 std::vector <std::string> EquilibriumPhasesList;
5795 std::vector <std::string> GasComponentsList;
5796 std::vector <std::string> KineticReactionsList;
5797 std::vector <std::string> SolidSolutionComponentsList;
5798 std::vector <std::string> SolidSolutionNamesList;
5799 std::vector <std::string> SINamesList;
5800 std::set <std::string> ElementRedoxSet;
5801
5802protected:
5803 static const int default_nxyz = 10;
5804 static const MP_TYPE default_data_for_parallel_processing;
5805 struct Initializer {
5806 int nxyz_arg;
5807 MP_TYPE data_for_parallel_processing;
5808 PHRQ_io *io;
5809
5810 Initializer()
5811 : nxyz_arg(default_nxyz) , data_for_parallel_processing(default_data_for_parallel_processing), io(NULL) {}
5812
5813 Initializer(int nxyz_arg, MP_TYPE data_for_parallel_processing, PHRQ_io *io)
5814 : nxyz_arg(nxyz_arg) , data_for_parallel_processing(data_for_parallel_processing), io(io) {}
5815
5816 } initializer;
5817
5818 virtual void Construct(Initializer initializer);
5819
5821private:
5822 //friend class RM_interface;
5823 friend class BMIPhreeqcRM;
5824 friend class VarManager;
5825
5826#if defined(_MSC_VER)
5827/* reset warning C4251 */
5828#pragma warning(default:4251)
5829#endif
5830
5831};
5832#endif // !defined(PHREEQCRM_H_INCLUDED)
Enumeration used to return error codes.
IRM_RESULT
Enumeration for PhreeqcRM function return codes.
Definition IrmResult.h:8
@ IRM_BADINSTANCE
Definition IrmResult.h:15
@ IRM_OK
Definition IrmResult.h:9
@ METHOD_GETSPECIESLOG10GAMMAS
Definition PhreeqcRM.h:76
@ METHOD_SETCONCENTRATIONS
Definition PhreeqcRM.h:88
@ METHOD_STATEDELETE
Definition PhreeqcRM.h:119
@ METHOD_RUNCELLS
Definition PhreeqcRM.h:84
@ METHOD_CONSTRUCT
Definition PhreeqcRM.h:60
@ METHOD_SETSELECTEDOUTPUTON
Definition PhreeqcRM.h:103
@ METHOD_SETTEMPERATURE
Definition PhreeqcRM.h:105
@ METHOD_SETCOMPONENTH2O
Definition PhreeqcRM.h:87
@ METHOD_GETSATURATIONCALCULATED
Definition PhreeqcRM.h:72
@ METHOD_GETSPECIESCONCENTRATIONS
Definition PhreeqcRM.h:75
@ METHOD_SETUNITSGASPHASE
Definition PhreeqcRM.h:110
@ METHOD_SETSPECIESSAVEON
Definition PhreeqcRM.h:104
@ METHOD_FINDCOMPONENTS
Definition PhreeqcRM.h:63
@ METHOD_CREATEMAPPING
Definition PhreeqcRM.h:61
@ METHOD_SETPOROSITY
Definition PhreeqcRM.h:95
@ METHOD_GETSPECIESLOG10MOLALITIES
Definition PhreeqcRM.h:77
@ METHOD_GETPRESSURE
Definition PhreeqcRM.h:71
@ METHOD_RUNSTRING
Definition PhreeqcRM.h:86
@ METHOD_GETCONCENTRATIONS
Definition PhreeqcRM.h:64
@ METHOD_SETPRINTCHEMISTRYON
Definition PhreeqcRM.h:97
@ METHOD_GETGASCOMPPHI
Definition PhreeqcRM.h:69
@ METHOD_GETGASCOMPPRESSURES
Definition PhreeqcRM.h:68
@ METHOD_SETUNITSKINETICS
Definition PhreeqcRM.h:111
@ METHOD_SETFILEPREFIX
Definition PhreeqcRM.h:91
@ METHOD_INITIALPHREEQCCELL2MODULE
Definition PhreeqcRM.h:81
@ METHOD_SETSATURATIONUSER
Definition PhreeqcRM.h:102
@ METHOD_GETGASCOMPMOLES
Definition PhreeqcRM.h:67
@ METHOD_INITIALPHREEQC2MODULE
Definition PhreeqcRM.h:80
@ METHOD_SETREBALANCEFRACTION
Definition PhreeqcRM.h:100
@ METHOD_GETTEMPERATURE
Definition PhreeqcRM.h:78
@ METHOD_SETUNITSEXCHANGE
Definition PhreeqcRM.h:109
@ METHOD_RUNFILE
Definition PhreeqcRM.h:85
@ METHOD_SETPARTITIONUZSOLIDS
Definition PhreeqcRM.h:94
@ METHOD_SETTIMESTEP
Definition PhreeqcRM.h:108
@ METHOD_GETERRORSTRING
Definition PhreeqcRM.h:66
@ METHOD_GETVISCOSITY
Definition PhreeqcRM.h:79
@ METHOD_SETUNITSPPASSEMBLAGE
Definition PhreeqcRM.h:112
@ METHOD_GETSOLUTIONVOLUME
Definition PhreeqcRM.h:74
@ METHOD_SETGASCOMPMOLES
Definition PhreeqcRM.h:92
@ METHOD_SETTIMECONVERSION
Definition PhreeqcRM.h:107
@ METHOD_STATESAVE
Definition PhreeqcRM.h:117
@ METHOD_SETGASPHASEVOLUME
Definition PhreeqcRM.h:93
@ METHOD_GETGASPHASEVOLUME
Definition PhreeqcRM.h:70
@ METHOD_SETUNITSSOLUTION
Definition PhreeqcRM.h:113
@ METHOD_SETREPRESENTATIVEVOLUME
Definition PhreeqcRM.h:101
@ METHOD_SETTIME
Definition PhreeqcRM.h:106
@ METHOD_MPIWORKERBREAK
Definition PhreeqcRM.h:83
@ METHOD_GETDENSITYCALCULATED
Definition PhreeqcRM.h:65
@ METHOD_SETERRORHANDLERMODE
Definition PhreeqcRM.h:90
@ METHOD_SETREBALANCEBYCELL
Definition PhreeqcRM.h:99
@ METHOD_SETUNITSSURFACE
Definition PhreeqcRM.h:115
@ METHOD_SETUNITSSSASSEMBLAGE
Definition PhreeqcRM.h:114
@ METHOD_LOADDATABASE
Definition PhreeqcRM.h:82
@ METHOD_SETPRINTCHEMISTRYMASK
Definition PhreeqcRM.h:98
@ METHOD_USESOLUTIONDENSITYVOLUME
Definition PhreeqcRM.h:120
@ METHOD_GETSELECTEDOUTPUT
Definition PhreeqcRM.h:73
@ METHOD_SETDENSITYUSER
Definition PhreeqcRM.h:89
@ METHOD_SETPRESSURE
Definition PhreeqcRM.h:96
@ METHOD_DUMPMODULE
Definition PhreeqcRM.h:62
@ METHOD_SPECIESCONCENTRATIONS2MODULE
Definition PhreeqcRM.h:116
@ METHOD_STATEAPPLY
Definition PhreeqcRM.h:118
#define MP_TYPE
Definition PhreeqcRM.h:15
Basic Model Interface implementation of the geochemical reaction module PhreeqcRM.
Definition BMIPhreeqcRM.h:44
This class is derived from std::exception and is thrown when an unrecoverable error has occurred.
Definition PhreeqcRM.h:48
const char * what() const
Definition PhreeqcRM.h:50
Geochemical reaction module.
Definition PhreeqcRM.h:267
IRM_RESULT CloseFiles(void)
const std::vector< double > & GetGfw(void)
Definition PhreeqcRM.h:1387
IRM_RESULT GetSaturation(std::vector< double > &sat_output)
int GetExchangeSpeciesCount(void) const
Definition PhreeqcRM.h:1123
IRM_RESULT SetNthSelectedOutput(int n)
IRM_RESULT RunCells(void)
IRM_RESULT SetUnitsKinetics(int option)
static IRM_RESULT Int2IrmResult(int r, bool positive_ok)
IRM_RESULT SetScreenOn(bool tf)
IRM_RESULT GetIthSpeciesConcentration(int i, std::vector< double > &c_output)
int GetSolidSolutionComponentsCount(void) const
Definition PhreeqcRM.h:2304
IRM_RESULT SetMpiWorkerCallbackFortran(int(*fcn)(int *method))
IRM_RESULT SetPrintChemistryOn(bool workers, bool initial_phreeqc, bool utility)
static IRM_RESULT DestroyReactionModule(int n)
IRM_RESULT InitialSolidSolutions2Module(const std::vector< int > &solid_solutions)
const std::vector< int > & GetPrintChemistryMask(void)
Definition PhreeqcRM.h:1768
IRM_RESULT SetFilePrefix(const std::string &prefix)
IRM_RESULT InitialEquilibriumPhases2Module(const std::vector< int > &equilibrium_phases)
IRM_RESULT GetSelectedOutputHeadings(std::vector< std::string > &headings)
IRM_RESULT GetDensityCalculated(std::vector< double > &d_output)
int GetSelectedOutputCount(void)
IRM_RESULT GetSaturationCalculated(std::vector< double > &sat_output)
void LogMessage(const std::string &str)
bool GetRebalanceByCell(void) const
Definition PhreeqcRM.h:1821
IPhreeqc * Concentrations2Utility(const std::vector< double > &c, const std::vector< double > &tc, const std::vector< double > &p_atm)
const std::vector< double > & GetTemperature(void)
int GetUnitsKinetics(void)
Definition PhreeqcRM.h:3097
const std::vector< std::string > & GetKineticReactions(void) const
Definition PhreeqcRM.h:1524
int GetUnitsSSassemblage(void)
Definition PhreeqcRM.h:3202
int GetChemistryCellCount(void) const
Definition PhreeqcRM.h:702
IRM_RESULT SetUnitsSSassemblage(int option)
const std::vector< double > & GetSpeciesD25(void)
Definition PhreeqcRM.h:2477
IRM_RESULT SetPorosity(const std::vector< double > &por)
int GetCurrentSelectedOutputUserNumber(void)
const std::vector< std::string > & GetGasComponents(void) const
Definition PhreeqcRM.h:1195
IRM_RESULT InitialGasPhases2Module(const std::vector< int > &gas_phases)
const std::vector< cxxNameDouble > & GetSpeciesStoichiometry(void)
Definition PhreeqcRM.h:2683
const std::vector< std::string > & GetExchangeNames(void) const
Definition PhreeqcRM.h:1056
IRM_RESULT SetDumpFileName(const std::string &dump_name)
IRM_RESULT SetErrorOn(bool tf)
void ScreenMessage(const std::string &str)
static int GetGridCellCountYAML(const char *YAML_file)
bool GetSpeciesSaveOn(void)
Definition PhreeqcRM.h:2632
const std::vector< std::string > & GetExchangeSpecies(void) const
Definition PhreeqcRM.h:1091
IRM_RESULT SetSelectedOutputOn(bool tf)
const std::vector< std::string > & GetSpeciesNames(void)
Definition PhreeqcRM.h:2596
IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector< double > &destination_c, const std::vector< int > &boundary_solution1)
IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector< double > &destination_c, const std::vector< int > &boundary_solution1, const std::vector< int > &boundary_solution2, const std::vector< double > &fraction1)
int GetUnitsPPassemblage(void)
Definition PhreeqcRM.h:3122
IRM_RESULT SetUnitsPPassemblage(int option)
const std::vector< int > & GetEndCell(void)
Definition PhreeqcRM.h:919
int GetThreadCount()
Definition PhreeqcRM.h:2950
IRM_RESULT InitialPhreeqc2Module(const std::vector< int > &initial_conditions1, const std::vector< int > &initial_conditions2, const std::vector< double > &fraction1)
IRM_RESULT StateSave(int istate)
const std::vector< double > & GetPorosity(void)
IRM_RESULT SetTime(double time)
int GetComponentCount(void) const
Definition PhreeqcRM.h:722
IRM_RESULT RunString(bool workers, bool initial_phreeqc, bool utility, const std::string &input_string)
IRM_RESULT SetPartitionUZSolids(bool tf)
const std::vector< std::string > & GetSolidSolutionComponents(void) const
Definition PhreeqcRM.h:2271
std::string GetDatabaseFileName(void)
Definition PhreeqcRM.h:855
IRM_RESULT RunFile(bool workers, bool initial_phreeqc, bool utility, const std::string &chemistry_name)
PhreeqcRM(int nxyz, int thread_count_or_communicator, PHRQ_io *io=NULL, bool delay_construct=false)
IRM_RESULT GetGasPhaseVolume(std::vector< double > &gas_volume_output)
IRM_RESULT InitialPhreeqc2Concentrations(std::vector< double > &destination_c, const std::vector< int > &boundary_solution1, const std::vector< int > &boundary_solution2, const std::vector< double > &fraction1)
IRM_RESULT GetSpeciesConcentrations(std::vector< double > &species_conc_output)
const std::vector< std::string > & GetSurfaceNames(void) const
Definition PhreeqcRM.h:2792
static std::string Char2TrimString(const char *str, size_t l=0)
IRM_RESULT GetSpeciesLog10Gammas(std::vector< double > &species_log10gammas)
IRM_RESULT SetIthSpeciesConcentration(int i, std::vector< double > &c)
IRM_RESULT SetPrintChemistryMask(const std::vector< int > &cell_mask)
IRM_RESULT GetConcentrations(std::vector< double > &c_output)
IRM_RESULT SpeciesConcentrations2Module(const std::vector< double > &species_conc)
int GetSelectedOutputRowCount(void)
IRM_RESULT GetSelectedOutputHeading(int icol, std::string &heading)
void DecodeError(int result)
bool GetPartitionUZSolids(void) const
Definition PhreeqcRM.h:1682
IRM_RESULT GetGasCompPhi(std::vector< double > &gas_phi)
int GetMpiMyself(void) const
Definition PhreeqcRM.h:1578
const std::vector< std::string > & GetSolidSolutionNames(void) const
Definition PhreeqcRM.h:2342
const std::vector< std::string > & GetComponents(void) const
Definition PhreeqcRM.h:747
IRM_RESULT InitialSolutions2Module(const std::vector< int > &solutions)
double GetTime(void) const
Definition PhreeqcRM.h:2973
int GetEquilibriumPhasesCount(void) const
Definition PhreeqcRM.h:977
int GetMpiTasks(void) const
Definition PhreeqcRM.h:1604
IRM_RESULT SetGasPhaseVolume(const std::vector< double > &gas_volume)
IRM_RESULT StateApply(int istate)
int GetSpeciesCount(void)
Definition PhreeqcRM.h:2442
IRM_RESULT SetMpiWorkerCallbackCookie(void *cookie)
static void FileRename(const std::string &temp_name, const std::string &name, const std::string &backup_name)
const std::vector< double > & GetPressure(void)
int FindComponents()
int GetNthSelectedOutputUserNumber(int n)
const std::vector< int > & GetForwardMapping(void)
Definition PhreeqcRM.h:1164
int GetSelectedOutputColumnCount(void)
IRM_RESULT InitializeYAML(std::string yamlfile)
IRM_RESULT SetUnitsExchange(int option)
std::string GetErrorString(void)
int GetGridCellCount(void)
Definition PhreeqcRM.h:1409
IRM_RESULT GetSpeciesLog10Molalities(std::vector< double > &species_log10molalities)
IRM_RESULT ReturnHandler(IRM_RESULT result, const std::string &e_string)
IRM_RESULT InitialPhreeqc2Concentrations(std::vector< double > &destination_c, const std::vector< int > &boundary_solution1)
const std::vector< std::string > & GetSINames(void) const
Definition PhreeqcRM.h:2234
void ErrorMessage(const std::string &error_string, bool prepend=true)
const std::vector< int > & GetStartCell(void)
Definition PhreeqcRM.h:2754
int GetErrorHandlerMode(void)
Definition PhreeqcRM.h:1000
bool GetSelectedOutputOn(void)
Definition PhreeqcRM.h:2124
static int CreateReactionModule(int nxyz, int nthreads)
void OutputMessage(const std::string &str)
IRM_RESULT SetSpeciesSaveOn(bool save_on)
virtual ~PhreeqcRM(void)
IRM_RESULT OpenFiles(void)
IRM_RESULT SetRebalanceFraction(double f)
int GetKineticReactionsCount(void) const
Definition PhreeqcRM.h:1552
IRM_RESULT LoadDatabase(const std::string &database)
IRM_RESULT SetSaturation(const std::vector< double > &sat)
IRM_RESULT SetMpiWorkerCallbackC(int(*fcn)(int *method, void *cookie))
IRM_RESULT SetTemperature(const std::vector< double > &t)
IPhreeqc * GetIPhreeqcPointer(int i)
IRM_RESULT InitialPhreeqc2Module(const std::vector< int > &initial_conditions1)
IRM_RESULT SetPressure(const std::vector< double > &p)
const std::vector< std::vector< int > > & GetBackwardMapping(void)
Definition PhreeqcRM.h:676
IRM_RESULT SetUnitsSolution(int option)
IRM_RESULT InitialSurfaces2Module(const std::vector< int > &surfaces)
double GetTimeStep(void)
Definition PhreeqcRM.h:3022
int GetUnitsExchange(void)
Definition PhreeqcRM.h:3047
static void CleanupReactionModuleInstances(void)
IRM_RESULT SetIthConcentration(int i, std::vector< double > &c)
IRM_RESULT SetTimeStep(double time_step)
const std::vector< IPhreeqcPhast * > & GetWorkers()
Definition PhreeqcRM.h:3272
IRM_RESULT SetDensityUser(const std::vector< double > &density)
IRM_RESULT CreateMapping(const std::vector< int > &grid2chem)
std::string GetFilePrefix(void)
Definition PhreeqcRM.h:1143
const std::vector< double > & GetSpeciesZ(void)
Definition PhreeqcRM.h:2719
IRM_RESULT SetCurrentSelectedOutputUserNumber(int n_user)
int MpiAbort()
int GetSICount(void) const
Definition PhreeqcRM.h:2200
IRM_RESULT SetUnitsSurface(int option)
double GetTimeConversion(void)
Definition PhreeqcRM.h:2998
IRM_RESULT GetDensity(std::vector< double > &d_output)
double GetRebalanceFraction(void) const
Definition PhreeqcRM.h:1846
void WarningMessage(const std::string &warnstr)
const std::vector< double > & GetSolutionVolume(void)
IRM_RESULT SetErrorHandlerMode(int mode)
int GetGasComponentsCount(void) const
Definition PhreeqcRM.h:1223
IRM_RESULT SetSaturationUser(const std::vector< double > &sat)
const std::vector< std::string > & GetSurfaceTypes(void) const
Definition PhreeqcRM.h:2903
IRM_RESULT InitialKinetics2Module(const std::vector< int > &kinetics)
IRM_RESULT MpiWorker()
IRM_RESULT SetGasCompMoles(const std::vector< double > &gas_moles)
IRM_RESULT GetSelectedOutput(std::vector< double > &s_output)
const std::vector< std::string > & GetEquilibriumPhases(void) const
Definition PhreeqcRM.h:949
IRM_RESULT GetGasCompMoles(std::vector< double > &gas_moles_output)
IRM_RESULT SetRebalanceByCell(bool tf)
IRM_RESULT InitialExchanges2Module(const std::vector< int > &exchanges)
IRM_RESULT SetComponentH2O(bool tf)
IRM_RESULT InitialPhreeqcCell2Module(int n, const std::vector< int > &cell_numbers)
IRM_RESULT SetTimeConversion(double conv_factor)
IRM_RESULT StateDelete(int istate)
IRM_RESULT SetRepresentativeVolume(const std::vector< double > &rv)
void UseSolutionDensityVolume(bool tf)
const std::vector< double > & GetViscosity()
const std::vector< bool > & GetPrintChemistryOn(void)
Definition PhreeqcRM.h:1797
const std::vector< std::string > & GetSurfaceSpecies(void) const
Definition PhreeqcRM.h:2829
static bool FileExists(const std::string &name)
IRM_RESULT GetGasCompPressures(std::vector< double > &gas_pressure)
IRM_RESULT SetConcentrations(const std::vector< double > &c)
IRM_RESULT GetIthConcentration(int i, std::vector< double > &c_output)
IRM_RESULT MpiWorkerBreak()
IRM_RESULT DumpModule(bool dump_on, bool append=false)
void ErrorHandler(int result, const std::string &e_string)
int GetUnitsSurface(void)
Definition PhreeqcRM.h:3227
int GetSurfaceSpeciesCount(void) const
Definition PhreeqcRM.h:2863
int GetUnitsSolution(void)
Definition PhreeqcRM.h:3177
IRM_RESULT SetDensity(const std::vector< double > &density)
int GetUnitsGasPhase(void)
Definition PhreeqcRM.h:3072
IRM_RESULT SetUnitsGasPhase(int option)
Definition PhreeqcRM.h:126
static std::map< size_t, T * > _Instances
Definition PhreeqcRM.h:242
static IRM_RESULT Destroy(int id)
Definition PhreeqcRM.h:172
static Derived * GetInstance(int id)
Definition PhreeqcRM.h:158
static size_t _InstancesIndex
Definition PhreeqcRM.h:243
size_t _Index
Definition PhreeqcRM.h:239
StaticIndexer(T *self)
Definition PhreeqcRM.h:128
int GetIndex()
Definition PhreeqcRM.h:233
~StaticIndexer()
Definition PhreeqcRM.h:135
static void DestroyAll()
Definition PhreeqcRM.h:200
static std::mutex _InstancesLock
Definition PhreeqcRM.h:241
static T * GetInstance(int id)
Definition PhreeqcRM.h:146