PhreeqcRM
PhreeqcRM.h
Go to the documentation of this file.
1
11#if !defined(PHREEQCRM_H_INCLUDED)
12#define PHREEQCRM_H_INCLUDED
13#ifdef USE_MPI
14#include "mpi.h"
15#define MP_TYPE MPI_Comm
16#else
17#define MP_TYPE int
18#endif
19
20// forward declarations
21class cxxNameDouble;
22class cxxSolution;
23class cxxStorageBin;
24class IPhreeqc;
25class IPhreeqcPhast;
26class PHRQ_io;
27
28#include "NameDouble.h"
29#include <vector>
30#include <list>
31#include <set>
32#include <map>
33#include <mutex>
34#include <string>
35#include <memory>
36#include "RMVARS.h"
37
38#include "irm_dll_export.h"
39
40#if SWIG
41#define IRM_DLL_EXPORT
42#endif
43
44//class BMI_Var;
51class IRM_DLL_EXPORT PhreeqcRMStop : public std::exception
52{
53public:
54 const char *what() const throw () {return "Failure in PhreeqcRM\n";}
55};
56//class LetItThrow : public std::logic_error {
57//public:
58// LetItThrow(std::string error_string) : std::logic_error(error_string.c_str()) { };
59//};
62#include "IrmResult.h"
63enum {
125} /* MPI_METHOD */;
126
127
128template<typename T>
130{
131public:
133 {
134 const std::lock_guard<std::mutex> lock(StaticIndexer<T>::_InstancesLock);
137 }
138
140 {
141 const std::lock_guard<std::mutex> lock(StaticIndexer<T>::_InstancesLock);
142 auto search = StaticIndexer<T>::_Instances.find(this->_Index);
143 assert(search != StaticIndexer<T>::_Instances.end());
144 if (search != StaticIndexer<T>::_Instances.end())
145 {
146 StaticIndexer<T>::_Instances.erase(search);
147 }
148 }
149
150 static T* GetInstance(int id)
151 {
152 const std::lock_guard<std::mutex> lock(_InstancesLock);
153 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
154 if (search != StaticIndexer<T>::_Instances.end())
155 {
156 return search->second;
157 }
158 return nullptr;
159 }
160
161 template<typename Derived>
162 static Derived* GetInstance(int id)
163 {
164 const std::lock_guard<std::mutex> lock(_InstancesLock);
165 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
166 if (search != StaticIndexer<T>::_Instances.end())
167 {
168 if (Derived* derived = dynamic_cast<Derived*>(search->second))
169 {
170 return derived;
171 }
172 }
173 return nullptr;
174 }
175
176 static IRM_RESULT Destroy(int id)
177 {
178 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
179 if (search != StaticIndexer<T>::_Instances.end())
180 {
181 assert(dynamic_cast<T*>(search->second));
182 delete search->second;
183 return IRM_OK;
184 }
185 return IRM_BADINSTANCE;
186 }
187
188 //template<typename Derived>
189 //static IRM_RESULT Destroy(int id)
190 //{
191 // auto search = StaticIndexer<T>::_Instances.find(size_t(id));
192 // if (search != StaticIndexer<T>::_Instances.end())
193 // {
194 // assert(dynamic_cast<T*>(search->second));
195 // if (Derived* derived = dynamic_cast<Derived*>(search->second))
196 // {
197 // delete derived;
198 // return IRM_OK;
199 // }
200 // }
201 // return IRM_BADINSTANCE;
202 //}
203
204 static void DestroyAll()
205 {
206 std::list<T*> items;
207 for (auto pair : StaticIndexer<T>::_Instances)
208 {
209 assert(dynamic_cast<T*>(pair.second));
210 items.push_back(pair.second);
211 }
212 for (auto item : items)
213 {
214 delete item;
215 }
216 }
217
218 //template<typename Derived>
219 //static void DestroyAll()
220 //{
221 // const std::lock_guard<std::mutex> lock(_InstancesLock);
222 // std::list<Derived*> derived_items;
223 // for (auto pair : StaticIndexer<T>::_Instances)
224 // {
225 // assert(dynamic_cast<T*>(pair.second));
226 // if (Derived* derived = dynamic_cast<Derived*>(pair.second))
227 // {
228 // derived_items.push_back(derived);
229 // }
230 // }
231 // for (auto item : derived_items)
232 // {
233 // delete item;
234 // }
235 //}
236
238 {
239 return (int)this->_Index;
240 }
241
242protected:
243 size_t _Index;
244
245 static std::mutex _InstancesLock;
246 static std::map<size_t, T*> _Instances;
247 static size_t _InstancesIndex;
248};
249
250// static members of StaticIndexer
251
252template<typename T>
254
255template<typename T>
256std::map<size_t, T*> StaticIndexer<T>::_Instances;
257
258template<typename T>
260
261
262
270class IRM_DLL_EXPORT PhreeqcRM : public StaticIndexer<PhreeqcRM>
271{
272public:
274 static int CreateReactionModule(int nxyz, MP_TYPE nthreads);
276private:
277 virtual void AddOutputVars(std::string option, std::string def);
278 virtual void ClearBMISelectedOutput();
279 virtual void GenerateAutoOutputVars();
280 virtual void UpdateBMI(RMVARS v_enum);
281public:
282#ifdef USE_YAML
310 static int GetGridCellCountYAML(const char* YAML_file);
311#endif // #ifdef USE_YAML
312
375 PhreeqcRM(int nxyz, MP_TYPE thread_count_or_communicator, PHRQ_io * io=NULL, bool delay_construct=false);
376 virtual ~PhreeqcRM(void);
439 IPhreeqc * Concentrations2Utility(const std::vector< double > &c,
440 const std::vector< double > &tc, const std::vector< double > &p_atm);
476 IRM_RESULT CreateMapping(const std::vector< int > &grid2chem);
510 void DecodeError(int result);
532 IRM_RESULT DumpModule(bool dump_on, bool append = false);
555 void ErrorHandler(int result, const std::string &e_string);
572 void ErrorMessage(const std::string &error_string, bool prepend = true);
680 const std::vector < std::vector <int> > & GetBackwardMapping(void) {return this->backward_mapping;}
682 void GetBackwardMappingSWIG(std::vector<int>& nback_output, std::vector<int>& cellnumbers_output);
706 int GetChemistryCellCount(void) const {return this->count_chemistry;}
726 int GetComponentCount(void) const {return (int) this->components.size();}
751const std::vector< std::string > & GetComponents(void) const {return this->components;}
752
793 IRM_RESULT GetConcentrations(std::vector< double > &c_output);
859 std::string GetDatabaseFileName(void) {return this->database_file_name;}
886 IRM_RESULT GetDensityCalculated(std::vector< double > & d_output);
887 IRM_RESULT GetDensity(std::vector< double >& d_output);
923 const std::vector < int> & GetEndCell(void) {return this->end_cell;}
953const std::vector< std::string > & GetEquilibriumPhases(void) const { return this->EquilibriumPhasesList; }
981int GetEquilibriumPhasesCount(void) const { return (int) this->EquilibriumPhasesList.size(); }
982
983
1004 int GetErrorHandlerMode(void) {return this->error_handler_mode;}
1023 std::string GetErrorString(void);
1024
1060const std::vector< std::string > & GetExchangeNames(void) const { return this->ExchangeNamesList; }
1095const std::vector< std::string > & GetExchangeSpecies(void) const { return this->ExchangeSpeciesNamesList; }
1127int GetExchangeSpeciesCount(void) const { return (int) this->ExchangeSpeciesNamesList.size(); }
1128
1129
1147 std::string GetFilePrefix(void) {return this->file_prefix;}
1168const std::vector < int > & GetForwardMapping(void) {return this->forward_mapping_root;}
1169
1199const std::vector< std::string > & GetGasComponents(void) const { return this->GasComponentsList; }
1227int GetGasComponentsCount(void) const { return (int) this->GasComponentsList.size(); }
1228
1261IRM_RESULT GetGasCompMoles(std::vector< double >& gas_moles_output);
1262
1296IRM_RESULT GetGasCompPhi(std::vector< double >& gas_phi);
1297
1330IRM_RESULT GetGasCompPressures(std::vector< double >& gas_pressure);
1331
1363IRM_RESULT GetGasPhaseVolume(std::vector< double >& gas_volume_output);
1364
1391 const std::vector < double > & GetGfw(void) {return this->gfw;}
1413 int GetGridCellCount(void) {return this->nxyz;}
1440IPhreeqc * GetIPhreeqcPointer(int i);
1467IRM_RESULT GetIthConcentration(int i, std::vector< double >& c_output);
1497IRM_RESULT GetIthSpeciesConcentration(int i, std::vector< double >& c_output);
1498
1528const std::vector< std::string > & GetKineticReactions(void) const { return this->KineticReactionsList; }
1556int GetKineticReactionsCount(void) const { return (int) this->KineticReactionsList.size(); }
1557
1582 int GetMpiMyself(void) const {return this->mpi_myself;}
1608 int GetMpiTasks(void) const {return this->mpi_tasks;}
1686 bool GetPartitionUZSolids(void) const {return this->partition_uz_solids;}
1687#ifdef USE_RV
1708 std::vector< double > & GetPoreVolume(void) {return this->pore_volume;}
1709#endif
1729 const std::vector< double >& GetPorosity(void);
1751 const std::vector< double > & GetPressure(void);
1772 const std::vector< int > & GetPrintChemistryMask (void) {return this->print_chem_mask_root;}
1801 const std::vector <bool> & GetPrintChemistryOn(void) {return this->print_chemistry_on;}
1825 bool GetRebalanceByCell(void) const {return this->rebalance_by_cell;}
1850 double GetRebalanceFraction(void) const {return this->rebalance_fraction;}
1886IRM_RESULT GetSaturationCalculated(std::vector< double > & sat_output);
1887IRM_RESULT GetSaturation(std::vector< double >& sat_output);
1927 IRM_RESULT GetSelectedOutput(std::vector< double > &s_output);
2054 IRM_RESULT GetSelectedOutputHeading(int icol, std::string &heading);
2099 IRM_RESULT GetSelectedOutputHeadings(std::vector< std::string >& headings);
2128 bool GetSelectedOutputOn(void) {return this->selected_output_on;}
2177
2204int GetSICount(void) const { return (int) this->SINamesList.size(); }
2238const std::vector< std::string > & GetSINames(void) const { return this->SINamesList; }
2239
2275const std::vector< std::string > & GetSolidSolutionComponents(void) const { return this->SolidSolutionComponentsList; }
2308int GetSolidSolutionComponentsCount(void) const { return (int) this->SolidSolutionComponentsList.size(); }
2309
2346const std::vector< std::string > & GetSolidSolutionNames(void) const { return this->SolidSolutionNamesList; }
2347
2368 const std::vector< double > & GetSolutionVolume(void);
2414 IRM_RESULT GetSpeciesConcentrations(std::vector< double > & species_conc_output);
2446 int GetSpeciesCount(void) {return (int) this->species_names.size();}
2481 const std::vector< double > & GetSpeciesD25(void) {return this->species_d_25;}
2523 IRM_RESULT GetSpeciesLog10Gammas(std::vector< double > & species_log10gammas);
2524
2565IRM_RESULT GetSpeciesLog10Molalities(std::vector< double >& species_log10molalities);
2566
2600 const std::vector< std::string > & GetSpeciesNames(void) {return this->species_names;}
2636 bool GetSpeciesSaveOn(void) {return this->species_save_on;}
2637
2687 const std::vector<cxxNameDouble> & GetSpeciesStoichiometry(void) {return this->species_stoichiometry;}
2689 void GetSpeciesStoichiometrySWIG(std::vector<std::string>& species_output,
2690 std::vector<int>& nelt_output, std::vector<std::string>& elts_output, std::vector<double>& coef_output);
2723 const std::vector< double > & GetSpeciesZ(void) {return this->species_z;}
2758const std::vector < int> & GetStartCell(void) {return this->start_cell;}
2759
2796const std::vector< std::string > & GetSurfaceNames(void) const { return this->SurfaceNamesList; }
2797
2833const std::vector< std::string > & GetSurfaceSpecies(void) const { return this->SurfaceSpeciesNamesList; }
2834
2867int GetSurfaceSpeciesCount(void) const { return (int) this->SurfaceSpeciesNamesList.size(); }
2868
2869
2907const std::vector< std::string > & GetSurfaceTypes(void) const { return this->SurfaceTypesList; }
2908
2929 //const std::vector< double > & GetTemperature(void) {return this->tempc;}
2930 const std::vector< double > & GetTemperature(void);
2931 //int PhreeqcRM::GetVarItemsize(const std::string name);
2932 //int PhreeqcRM::GetVarNbytes(const std::string name);
2933
2954 int GetThreadCount() {return this->nthreads;}
2977 double GetTime(void) const {return this->time;}
3002 double GetTimeConversion(void) {return this->time_conversion;}
3026 double GetTimeStep(void) {return this->time_step;}
3051 int GetUnitsExchange(void) {return this->units_Exchange;}
3076 int GetUnitsGasPhase(void) {return this->units_GasPhase;}
3101 int GetUnitsKinetics(void) {return this->units_Kinetics;}
3126 int GetUnitsPPassemblage(void) {return this->units_PPassemblage;}
3181 int GetUnitsSolution(void) {return this->units_Solution;}
3206 int GetUnitsSSassemblage(void) {return this->units_SSassemblage;}
3231 int GetUnitsSurface(void) {return this->units_Surface;}
3251 const std::vector<double>& GetViscosity();
3252
3253
3276 const std::vector<IPhreeqcPhast *> & GetWorkers() {return this->workers;}
3277#ifdef USE_YAML
3397IRM_RESULT InitializeYAML(std::string yamlfile);
3398#endif
3426IRM_RESULT InitialPhreeqc2Concentrations(std::vector < double > & destination_c,
3427 const std::vector < int > & boundary_solution1);
3467 std::vector < double > & destination_c,
3468 const std::vector < int > & boundary_solution1,
3469 const std::vector < int > & boundary_solution2,
3470 const std::vector < double > & fraction1);
3500IRM_RESULT InitialEquilibriumPhases2Module(const std::vector < int >& equilibrium_phases);
3530IRM_RESULT InitialExchanges2Module(const std::vector < int >& exchanges);
3559IRM_RESULT InitialGasPhases2Module(const std::vector < int >& gas_phases);
3588IRM_RESULT InitialKinetics2Module(const std::vector < int >& kinetics);
3629IRM_RESULT InitialPhreeqc2Module(const std::vector < int >& initial_conditions1);
3693 const std::vector < int >& initial_conditions1,
3694 const std::vector < int >& initial_conditions2,
3695 const std::vector < double >& fraction1);
3725IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector < double >& destination_c,
3726 const std::vector < int >& boundary_solution1);
3766IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector < double >& destination_c,
3767 const std::vector < int >& boundary_solution1,
3768 const std::vector < int >& boundary_solution2,
3769 const std::vector < double >& fraction1);
3797 const std::vector< int >& cell_numbers);
3798
3827IRM_RESULT InitialSolidSolutions2Module(const std::vector < int >& solid_solutions);
3857IRM_RESULT InitialSolutions2Module(const std::vector < int >& solutions);
3886IRM_RESULT InitialSurfaces2Module(const std::vector < int >& surfaces);
3887
3904 virtual IRM_RESULT LoadDatabase(const std::string &database);
3923 void LogMessage(const std::string &str);
4051 void OutputMessage(const std::string &str);
4072 IRM_RESULT ReturnHandler(IRM_RESULT result, const std::string& e_string);
4073
4074
4132 IRM_RESULT RunFile(bool workers, bool initial_phreeqc, bool utility, const std::string & chemistry_name);
4159 IRM_RESULT RunString(bool workers, bool initial_phreeqc, bool utility, const std::string & input_string);
4182 void ScreenMessage(const std::string &str);
4249 IRM_RESULT SetConcentrations(const std::vector< double > &c);
4313 IRM_RESULT SetDensityUser(const std::vector< double > &density);
4314 IRM_RESULT SetDensity(const std::vector< double >& density);
4334 IRM_RESULT SetDumpFileName(const std::string & dump_name);
4394 IRM_RESULT SetFilePrefix(const std::string & prefix);
4395
4431IRM_RESULT SetGasCompMoles(const std::vector< double >& gas_moles);
4470IRM_RESULT SetGasPhaseVolume(const std::vector< double >& gas_volume);
4497IRM_RESULT SetIthConcentration(int i, std::vector< double >& c);
4527IRM_RESULT SetIthSpeciesConcentration(int i, std::vector< double >& c);
4528
4624 IRM_RESULT SetMpiWorkerCallbackC(int (*fcn)(int *method, void * cookie));
4700 IRM_RESULT SetMpiWorkerCallbackFortran(int (*fcn)(int *method));
4810 IRM_RESULT SetPorosity(const std::vector< double > &por);
4811
4832 IRM_RESULT SetPressure(const std::vector< double > &p);
4859 IRM_RESULT SetPrintChemistryMask(const std::vector<int> & cell_mask);
4894 IRM_RESULT SetPrintChemistryOn(bool workers, bool initial_phreeqc, bool utility);
4980 IRM_RESULT SetRepresentativeVolume(const std::vector< double > &rv);
5010 IRM_RESULT SetSaturationUser(const std::vector< double > &sat);
5011 IRM_RESULT SetSaturation(const std::vector< double >& sat);
5126 IRM_RESULT SetTemperature(const std::vector< double > &t);
5144 IRM_RESULT SetTime(double time);
5163 IRM_RESULT SetTimeConversion(double conv_factor);
5183 IRM_RESULT SetTimeStep(double time_step);
5496IRM_RESULT SpeciesConcentrations2Module(const std::vector< double > & species_conc);
5497
5632 void WarningMessage(const std::string &warnstr);
5633public:
5634// Utilities
5635 static std::string Char2TrimString(const char * str, size_t l = 0);
5636 static bool FileExists(const std::string &name);
5637 static void FileRename(const std::string &temp_name, const std::string &name,
5638 const std::string &backup_name);
5639 static IRM_RESULT Int2IrmResult(int r, bool positive_ok);
5640private:
5641 IRM_RESULT CellInitialize(
5642 int i,
5643 int n_user_new,
5644 const int *initial_conditions1,
5645 const int *initial_conditions2,
5646 double *fraction1,
5647 std::set<std::string> &error_set);
5648 IRM_RESULT CheckCells();
5649 int CheckSelectedOutput();
5650 //void Collapse2Nchem(double *d_in, double *d_out);
5651 //void Collapse2Nchem(int *i_in, int *i_out);
5652 IPhreeqc * Concentrations2UtilityH2O(const std::vector< double > &c_in,
5653 const std::vector< double > &t_in, const std::vector< double > &p_in);
5654 IPhreeqc * Concentrations2UtilityNoH2O(const std::vector< double > &c_in,
5655 const std::vector< double > &t_in, const std::vector< double > &p_in);
5656 void Concentrations2Solutions(int n, std::vector< double > &c);
5657 void Concentrations2SolutionsH2O(int n, std::vector< double > &c);
5658 void Concentrations2SolutionsNoH2O(int n, std::vector< double > &c);
5659 void cxxSolution2concentration(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5660 void cxxSolution2concentrationH2O(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5661 void cxxSolution2concentrationNoH2O(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5662 void GatherNchem(std::vector< double > &source, std::vector< double > &destination);
5663 cxxStorageBin & Get_phreeqc_bin(void) {return *this->phreeqc_bin;}
5664 IRM_RESULT HandleErrorsInternal(std::vector< int > & r);
5665 void PartitionUZ(int n, int iphrq, int ihst, double new_frac);
5666 void RebalanceLoad(void);
5667 void RebalanceLoadPerCell(void);
5668 IRM_RESULT RunCellsThread(int i);
5669 IRM_RESULT RunFileThread(int n);
5670 IRM_RESULT RunStringThread(int n, std::string & input);
5671 IRM_RESULT RunCellsThreadNoPrint(int n);
5672 void Scale_solids(int n, int iphrq, double frac);
5673 void ScatterNchem(double *d_array);
5674 void ScatterNchem(int *i_array);
5675 void ScatterNchem(std::vector< double > &source, std::vector< double > &destination);
5676 void ScatterNchem(std::vector< int > &source, std::vector< int > &destination);
5677 IRM_RESULT SetChemistryFileName(const char * prefix = NULL);
5678 IRM_RESULT SetDatabaseFileName(const char * db = NULL);
5679 void SetEndCells(void);
5680 void SetEndCellsHeterogeneous(void);
5681 double TimeStandardTask(void);
5682 IRM_RESULT TransferCells(cxxStorageBin &t_bin, int old, int nnew);
5683 IRM_RESULT TransferCellsUZ(std::ostringstream &raw_stream, int old, int nnew);
5684
5685private:
5686 //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);
5687 IRM_RESULT SetGeneric(const std::vector< double > &source, std::vector< double > &destination_root, std::vector< double > &destination_worker, int mpiMethod, const std::string &name);
5688public:
5689
5690protected:
5691
5692#if defined(_MSC_VER)
5693/* disable warning C4251: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' */
5694#pragma warning(disable:4251)
5695#endif
5697 bool component_h2o; // true: use H2O, excess H, excess O, and charge;
5698 // false total H, total O, and charge
5699 std::string database_file_name;
5700 std::string chemistry_file_name;
5701 std::string dump_file_name;
5702 std::string file_prefix;
5703 cxxStorageBin * phreeqc_bin;
5704 int mpi_myself;
5705 int mpi_tasks;
5706 std::vector <std::string> components; // list of components to be transported
5707 std::vector <double> gfw; // gram formula weights converting mass to moles (1 for each component)
5708 double gfw_water; // gfw of water
5709 bool partition_uz_solids;
5710 int nxyz; // number of nodes
5711 int count_chemistry; // number of cells for chemistry
5712 double time; // time from transport, sec
5713 double time_step; // time step from transport, sec
5714 double time_conversion; // time conversion factor, multiply to convert to preferred time unit for output
5715 std::vector <double> CurrentConcentrations;
5716 std::vector <double> CurrentSpeciesConcentrations;
5717 std::vector <double> IthCurrentConcentrations;
5718 std::vector <double> IthCurrentSpeciesConcentrations;
5719 std::set<int> IthConcentrationSet;
5720 std::set<int> IthSpeciesConcentrationSet;
5721 std::vector <double> old_saturation_root; // saturation fraction from previous step
5722 std::vector <double> old_saturation_worker;
5723 std::vector< double > saturation_root; // nxyz saturation fraction
5724 std::vector< double > saturation_worker; // nchem on workers saturation fraction
5725 std::vector< double > pressure_root; // nxyz on root current pressure
5726 std::vector< double > pressure_worker; // nchem on workers current pressure
5727 std::vector< double > rv_root; // nxyz on root representative volume
5728 std::vector< double > rv_worker; // nchem on workers representative volume
5729 std::vector< double > porosity_root; // nxyz porosity
5730 std::vector< double > porosity_worker; // nchem on workers porosity
5731 std::vector< double > tempc_root; // nxyz on root temperature Celsius
5732 std::vector< double > tempc_worker; // nchem on workers temperature Celsius
5733 std::vector< double > density_root; // nxyz density
5734 std::vector< double > density_worker; // nchem on workers density
5735 std::vector< double > viscosity_root; // nxyz viscosity
5736 std::vector< double > viscosity_worker; // nchem on workers viscosity
5737 std::vector< double > solution_volume_root; // nxyz on root solution volume
5738 std::vector< double > solution_volume_worker; // nchem on workers solution_volume
5739 std::vector< int > print_chem_mask_root; // nxyz print flags for output file
5740 std::vector< int > print_chem_mask_worker; // nchem print flags for output file
5741 bool rebalance_by_cell; // rebalance method 0 std, 1 by_cell
5742 double rebalance_fraction; // parameter for rebalancing process load for parallel
5743 int units_Solution; // 1 mg/L, 2 mol/L, 3 kg/kgs
5744 int units_PPassemblage; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5745 int units_Exchange; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5746 int units_Surface; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5747 int units_GasPhase; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5748 int units_SSassemblage; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5749 int units_Kinetics; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5750 std::vector <int> forward_mapping_root; // mapping from nxyz cells to count_chem chemistry cells
5751 std::vector <std::vector <int> > backward_mapping; // mapping from count_chem chemistry cells to nxyz cells
5752 bool use_solution_density_volume;
5753 bool worker_waiting;
5754 // print flags
5755 std::vector< bool > print_chemistry_on; // print flag for chemistry output file
5756 bool selected_output_on; // create selected output
5757
5758 int error_count;
5759 int error_handler_mode; // 0, return code; 1, throw; 2 exit;
5760 bool need_error_check;
5761 std::string phreeqcrm_error_string;
5762
5763 std::map<std::string, int> method_map;
5764 // threading
5765 int nthreads;
5766 std::vector<IPhreeqcPhast *> workers;
5767 //std::vector< int > start_cell;
5768 //std::vector< int > end_cell;
5769 PHRQ_io *phreeqcrm_io;
5770 bool delete_phreeqcrm_io;
5771
5772 // mpi
5773#ifdef USE_MPI
5774 MPI_Comm phreeqcrm_comm; // MPI communicator
5775#endif
5776 int (*mpi_worker_callback_fortran) (int *method);
5777 int (*mpi_worker_callback_c) (int *method, void *cookie);
5778 void *mpi_worker_callback_cookie;
5779
5780 // mcd
5781 bool species_save_on;
5782 std::vector <std::string> species_names;
5783 std::vector <double> species_z;
5784 std::vector <double> species_d_25;
5785 std::vector <cxxNameDouble> species_stoichiometry;
5786 std::map<int, int> s_num2rm_species_num;
5787 std::vector< double > standard_task_vector; // root only
5788
5789 std::vector< int > start_cell;
5790 std::vector< int > end_cell;
5791 // reactant lists
5792 std::vector <std::string> ExchangeSpeciesNamesList;
5793 std::vector <std::string> ExchangeNamesList;
5794 std::vector <std::string> SurfaceSpeciesNamesList;
5795 std::vector <std::string> SurfaceTypesList;
5796 std::vector <std::string> SurfaceNamesList;
5797
5798 std::vector <std::string> EquilibriumPhasesList;
5799 std::vector <std::string> GasComponentsList;
5800 std::vector <std::string> KineticReactionsList;
5801 std::vector <std::string> SolidSolutionComponentsList;
5802 std::vector <std::string> SolidSolutionNamesList;
5803 std::vector <std::string> SINamesList;
5804 std::set <std::string> ElementRedoxSet;
5805
5806protected:
5807 static const int default_nxyz = 10;
5808 static const MP_TYPE default_data_for_parallel_processing;
5809
5810 class Initializer; // Forward declaration
5811 std::unique_ptr<Initializer> initializer;
5812
5813 virtual void Construct();
5814
5815 void set_data_for_parallel_processing(int value);
5816 void set_io(PHRQ_io* value);
5817 void set_nxyz(int value);
5818
5820private:
5821 //friend class RM_interface;
5822 friend class BMIPhreeqcRM;
5823 friend class VarManager;
5824
5825#if defined(_MSC_VER)
5826/* reset warning C4251 */
5827#pragma warning(default:4251)
5828#endif
5829
5830};
5831#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:80
@ METHOD_SETCONCENTRATIONS
Definition PhreeqcRM.h:92
@ METHOD_STATEDELETE
Definition PhreeqcRM.h:123
@ METHOD_RUNCELLS
Definition PhreeqcRM.h:88
@ METHOD_CONSTRUCT
Definition PhreeqcRM.h:64
@ METHOD_SETSELECTEDOUTPUTON
Definition PhreeqcRM.h:107
@ METHOD_SETTEMPERATURE
Definition PhreeqcRM.h:109
@ METHOD_SETCOMPONENTH2O
Definition PhreeqcRM.h:91
@ METHOD_GETSATURATIONCALCULATED
Definition PhreeqcRM.h:76
@ METHOD_GETSPECIESCONCENTRATIONS
Definition PhreeqcRM.h:79
@ METHOD_SETUNITSGASPHASE
Definition PhreeqcRM.h:114
@ METHOD_SETSPECIESSAVEON
Definition PhreeqcRM.h:108
@ METHOD_FINDCOMPONENTS
Definition PhreeqcRM.h:67
@ METHOD_CREATEMAPPING
Definition PhreeqcRM.h:65
@ METHOD_SETPOROSITY
Definition PhreeqcRM.h:99
@ METHOD_GETSPECIESLOG10MOLALITIES
Definition PhreeqcRM.h:81
@ METHOD_GETPRESSURE
Definition PhreeqcRM.h:75
@ METHOD_RUNSTRING
Definition PhreeqcRM.h:90
@ METHOD_GETCONCENTRATIONS
Definition PhreeqcRM.h:68
@ METHOD_SETPRINTCHEMISTRYON
Definition PhreeqcRM.h:101
@ METHOD_GETGASCOMPPHI
Definition PhreeqcRM.h:73
@ METHOD_GETGASCOMPPRESSURES
Definition PhreeqcRM.h:72
@ METHOD_SETUNITSKINETICS
Definition PhreeqcRM.h:115
@ METHOD_SETFILEPREFIX
Definition PhreeqcRM.h:95
@ METHOD_INITIALPHREEQCCELL2MODULE
Definition PhreeqcRM.h:85
@ METHOD_SETSATURATIONUSER
Definition PhreeqcRM.h:106
@ METHOD_GETGASCOMPMOLES
Definition PhreeqcRM.h:71
@ METHOD_INITIALPHREEQC2MODULE
Definition PhreeqcRM.h:84
@ METHOD_SETREBALANCEFRACTION
Definition PhreeqcRM.h:104
@ METHOD_GETTEMPERATURE
Definition PhreeqcRM.h:82
@ METHOD_SETUNITSEXCHANGE
Definition PhreeqcRM.h:113
@ METHOD_RUNFILE
Definition PhreeqcRM.h:89
@ METHOD_SETPARTITIONUZSOLIDS
Definition PhreeqcRM.h:98
@ METHOD_SETTIMESTEP
Definition PhreeqcRM.h:112
@ METHOD_GETERRORSTRING
Definition PhreeqcRM.h:70
@ METHOD_GETVISCOSITY
Definition PhreeqcRM.h:83
@ METHOD_SETUNITSPPASSEMBLAGE
Definition PhreeqcRM.h:116
@ METHOD_GETSOLUTIONVOLUME
Definition PhreeqcRM.h:78
@ METHOD_SETGASCOMPMOLES
Definition PhreeqcRM.h:96
@ METHOD_SETTIMECONVERSION
Definition PhreeqcRM.h:111
@ METHOD_STATESAVE
Definition PhreeqcRM.h:121
@ METHOD_SETGASPHASEVOLUME
Definition PhreeqcRM.h:97
@ METHOD_GETGASPHASEVOLUME
Definition PhreeqcRM.h:74
@ METHOD_SETUNITSSOLUTION
Definition PhreeqcRM.h:117
@ METHOD_SETREPRESENTATIVEVOLUME
Definition PhreeqcRM.h:105
@ METHOD_SETTIME
Definition PhreeqcRM.h:110
@ METHOD_MPIWORKERBREAK
Definition PhreeqcRM.h:87
@ METHOD_GETDENSITYCALCULATED
Definition PhreeqcRM.h:69
@ METHOD_SETERRORHANDLERMODE
Definition PhreeqcRM.h:94
@ METHOD_SETREBALANCEBYCELL
Definition PhreeqcRM.h:103
@ METHOD_SETUNITSSURFACE
Definition PhreeqcRM.h:119
@ METHOD_SETUNITSSSASSEMBLAGE
Definition PhreeqcRM.h:118
@ METHOD_LOADDATABASE
Definition PhreeqcRM.h:86
@ METHOD_SETPRINTCHEMISTRYMASK
Definition PhreeqcRM.h:102
@ METHOD_USESOLUTIONDENSITYVOLUME
Definition PhreeqcRM.h:124
@ METHOD_GETSELECTEDOUTPUT
Definition PhreeqcRM.h:77
@ METHOD_SETDENSITYUSER
Definition PhreeqcRM.h:93
@ METHOD_SETPRESSURE
Definition PhreeqcRM.h:100
@ METHOD_DUMPMODULE
Definition PhreeqcRM.h:66
@ METHOD_SPECIESCONCENTRATIONS2MODULE
Definition PhreeqcRM.h:120
@ METHOD_STATEAPPLY
Definition PhreeqcRM.h:122
#define MP_TYPE
Definition PhreeqcRM.h:17
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:52
const char * what() const
Definition PhreeqcRM.h:54
Geochemical reaction module.
Definition PhreeqcRM.h:271
IRM_RESULT CloseFiles(void)
const std::vector< double > & GetGfw(void)
Definition PhreeqcRM.h:1391
IRM_RESULT GetSaturation(std::vector< double > &sat_output)
int GetExchangeSpeciesCount(void) const
Definition PhreeqcRM.h:1127
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:2308
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:1772
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:1825
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:3101
const std::vector< std::string > & GetKineticReactions(void) const
Definition PhreeqcRM.h:1528
int GetUnitsSSassemblage(void)
Definition PhreeqcRM.h:3206
int GetChemistryCellCount(void) const
Definition PhreeqcRM.h:706
IRM_RESULT SetUnitsSSassemblage(int option)
const std::vector< double > & GetSpeciesD25(void)
Definition PhreeqcRM.h:2481
IRM_RESULT SetPorosity(const std::vector< double > &por)
int GetCurrentSelectedOutputUserNumber(void)
const std::vector< std::string > & GetGasComponents(void) const
Definition PhreeqcRM.h:1199
IRM_RESULT InitialGasPhases2Module(const std::vector< int > &gas_phases)
const std::vector< cxxNameDouble > & GetSpeciesStoichiometry(void)
Definition PhreeqcRM.h:2687
const std::vector< std::string > & GetExchangeNames(void) const
Definition PhreeqcRM.h:1060
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:2636
const std::vector< std::string > & GetExchangeSpecies(void) const
Definition PhreeqcRM.h:1095
IRM_RESULT SetSelectedOutputOn(bool tf)
const std::vector< std::string > & GetSpeciesNames(void)
Definition PhreeqcRM.h:2600
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:3126
IRM_RESULT SetUnitsPPassemblage(int option)
const std::vector< int > & GetEndCell(void)
Definition PhreeqcRM.h:923
int GetThreadCount()
Definition PhreeqcRM.h:2954
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:726
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:2275
std::string GetDatabaseFileName(void)
Definition PhreeqcRM.h:859
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:2796
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:1686
IRM_RESULT GetGasCompPhi(std::vector< double > &gas_phi)
int GetMpiMyself(void) const
Definition PhreeqcRM.h:1582
const std::vector< std::string > & GetSolidSolutionNames(void) const
Definition PhreeqcRM.h:2346
const std::vector< std::string > & GetComponents(void) const
Definition PhreeqcRM.h:751
IRM_RESULT InitialSolutions2Module(const std::vector< int > &solutions)
double GetTime(void) const
Definition PhreeqcRM.h:2977
int GetEquilibriumPhasesCount(void) const
Definition PhreeqcRM.h:981
int GetMpiTasks(void) const
Definition PhreeqcRM.h:1608
IRM_RESULT SetGasPhaseVolume(const std::vector< double > &gas_volume)
IRM_RESULT StateApply(int istate)
int GetSpeciesCount(void)
Definition PhreeqcRM.h:2446
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:1168
int GetSelectedOutputColumnCount(void)
IRM_RESULT InitializeYAML(std::string yamlfile)
IRM_RESULT SetUnitsExchange(int option)
std::string GetErrorString(void)
int GetGridCellCount(void)
Definition PhreeqcRM.h:1413
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:2238
void ErrorMessage(const std::string &error_string, bool prepend=true)
const std::vector< int > & GetStartCell(void)
Definition PhreeqcRM.h:2758
int GetErrorHandlerMode(void)
Definition PhreeqcRM.h:1004
bool GetSelectedOutputOn(void)
Definition PhreeqcRM.h:2128
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:1556
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:680
IRM_RESULT SetUnitsSolution(int option)
IRM_RESULT InitialSurfaces2Module(const std::vector< int > &surfaces)
double GetTimeStep(void)
Definition PhreeqcRM.h:3026
int GetUnitsExchange(void)
Definition PhreeqcRM.h:3051
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:3276
IRM_RESULT SetDensityUser(const std::vector< double > &density)
IRM_RESULT CreateMapping(const std::vector< int > &grid2chem)
std::string GetFilePrefix(void)
Definition PhreeqcRM.h:1147
const std::vector< double > & GetSpeciesZ(void)
Definition PhreeqcRM.h:2723
IRM_RESULT SetCurrentSelectedOutputUserNumber(int n_user)
int MpiAbort()
int GetSICount(void) const
Definition PhreeqcRM.h:2204
IRM_RESULT SetUnitsSurface(int option)
double GetTimeConversion(void)
Definition PhreeqcRM.h:3002
IRM_RESULT GetDensity(std::vector< double > &d_output)
double GetRebalanceFraction(void) const
Definition PhreeqcRM.h:1850
void WarningMessage(const std::string &warnstr)
const std::vector< double > & GetSolutionVolume(void)
virtual IRM_RESULT LoadDatabase(const std::string &database)
IRM_RESULT SetErrorHandlerMode(int mode)
int GetGasComponentsCount(void) const
Definition PhreeqcRM.h:1227
IRM_RESULT SetSaturationUser(const std::vector< double > &sat)
const std::vector< std::string > & GetSurfaceTypes(void) const
Definition PhreeqcRM.h:2907
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:953
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:1801
const std::vector< std::string > & GetSurfaceSpecies(void) const
Definition PhreeqcRM.h:2833
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:3231
int GetSurfaceSpeciesCount(void) const
Definition PhreeqcRM.h:2867
int GetUnitsSolution(void)
Definition PhreeqcRM.h:3181
IRM_RESULT SetDensity(const std::vector< double > &density)
int GetUnitsGasPhase(void)
Definition PhreeqcRM.h:3076
IRM_RESULT SetUnitsGasPhase(int option)
Definition PhreeqcRM.h:130
static std::map< size_t, T * > _Instances
Definition PhreeqcRM.h:246
static IRM_RESULT Destroy(int id)
Definition PhreeqcRM.h:176
static Derived * GetInstance(int id)
Definition PhreeqcRM.h:162
static size_t _InstancesIndex
Definition PhreeqcRM.h:247
size_t _Index
Definition PhreeqcRM.h:243
StaticIndexer(T *self)
Definition PhreeqcRM.h:132
int GetIndex()
Definition PhreeqcRM.h:237
~StaticIndexer()
Definition PhreeqcRM.h:139
static void DestroyAll()
Definition PhreeqcRM.h:204
static std::mutex _InstancesLock
Definition PhreeqcRM.h:245
static T * GetInstance(int id)
Definition PhreeqcRM.h:150