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
19class IPhreeqcPhast;
20
21class cxxStorageBin;
22//class cxxNameDouble;
23#include "NameDouble.h"
24class cxxSolution;
25class PHRQ_io;
26#include <vector>
27#include <list>
28#include <set>
29#include <map>
30#include <mutex>
31#include <string>
32#include "RMVARS.h"
33
34#include "irm_dll_export.h"
35
36#if SWIG
37#define IRM_DLL_EXPORT
38#endif
39
40class PHRQ_io;
41class IPhreeqc;
42//class BMI_Var;
49class IRM_DLL_EXPORT PhreeqcRMStop : public std::exception
50{
51public:
52 const char *what() const throw () {return "Failure in PhreeqcRM\n";}
53};
54//class LetItThrow : public std::logic_error {
55//public:
56// LetItThrow(std::string error_string) : std::logic_error(error_string.c_str()) { };
57//};
60#include "IrmResult.h"
61enum {
123} /* MPI_METHOD */;
124
125
126template<typename T>
128{
129public:
131 {
132 const std::lock_guard<std::mutex> lock(StaticIndexer<T>::_InstancesLock);
135 }
136
138 {
139 const std::lock_guard<std::mutex> lock(StaticIndexer<T>::_InstancesLock);
140 auto search = StaticIndexer<T>::_Instances.find(this->_Index);
141 assert(search != StaticIndexer<T>::_Instances.end());
142 if (search != StaticIndexer<T>::_Instances.end())
143 {
144 StaticIndexer<T>::_Instances.erase(search);
145 }
146 }
147
148 static T* GetInstance(int id)
149 {
150 const std::lock_guard<std::mutex> lock(_InstancesLock);
151 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
152 if (search != StaticIndexer<T>::_Instances.end())
153 {
154 return search->second;
155 }
156 return nullptr;
157 }
158
159 template<typename Derived>
160 static Derived* GetInstance(int id)
161 {
162 const std::lock_guard<std::mutex> lock(_InstancesLock);
163 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
164 if (search != StaticIndexer<T>::_Instances.end())
165 {
166 if (Derived* derived = dynamic_cast<Derived*>(search->second))
167 {
168 return derived;
169 }
170 }
171 return nullptr;
172 }
173
174 static IRM_RESULT Destroy(int id)
175 {
176 auto search = StaticIndexer<T>::_Instances.find(size_t(id));
177 if (search != StaticIndexer<T>::_Instances.end())
178 {
179 assert(dynamic_cast<T*>(search->second));
180 delete search->second;
181 return IRM_OK;
182 }
183 return IRM_BADINSTANCE;
184 }
185
186 //template<typename Derived>
187 //static IRM_RESULT Destroy(int id)
188 //{
189 // auto search = StaticIndexer<T>::_Instances.find(size_t(id));
190 // if (search != StaticIndexer<T>::_Instances.end())
191 // {
192 // assert(dynamic_cast<T*>(search->second));
193 // if (Derived* derived = dynamic_cast<Derived*>(search->second))
194 // {
195 // delete derived;
196 // return IRM_OK;
197 // }
198 // }
199 // return IRM_BADINSTANCE;
200 //}
201
202 static void DestroyAll()
203 {
204 std::list<T*> items;
205 for (auto pair : StaticIndexer<T>::_Instances)
206 {
207 assert(dynamic_cast<T*>(pair.second));
208 items.push_back(pair.second);
209 }
210 for (auto item : items)
211 {
212 delete item;
213 }
214 }
215
216 //template<typename Derived>
217 //static void DestroyAll()
218 //{
219 // const std::lock_guard<std::mutex> lock(_InstancesLock);
220 // std::list<Derived*> derived_items;
221 // for (auto pair : StaticIndexer<T>::_Instances)
222 // {
223 // assert(dynamic_cast<T*>(pair.second));
224 // if (Derived* derived = dynamic_cast<Derived*>(pair.second))
225 // {
226 // derived_items.push_back(derived);
227 // }
228 // }
229 // for (auto item : derived_items)
230 // {
231 // delete item;
232 // }
233 //}
234
236 {
237 return (int)this->_Index;
238 }
239
240protected:
241 size_t _Index;
242
243 static std::mutex _InstancesLock;
244 static std::map<size_t, T*> _Instances;
245 static size_t _InstancesIndex;
246};
247
248// static members of StaticIndexer
249
250template<typename T>
252
253template<typename T>
254std::map<size_t, T*> StaticIndexer<T>::_Instances;
255
256template<typename T>
258
259
260
268class IRM_DLL_EXPORT PhreeqcRM : public StaticIndexer<PhreeqcRM>
269{
270public:
272 static int CreateReactionModule(int nxyz, MP_TYPE nthreads);
274private:
275 virtual void AddOutputVars(std::string option, std::string def);
276 virtual void ClearBMISelectedOutput();
277 virtual void GenerateAutoOutputVars();
278 virtual void UpdateBMI(RMVARS v_enum);
279public:
280#ifdef USE_YAML
308 static int GetGridCellCountYAML(const char* YAML_file);
309#endif // #ifdef USE_YAML
310
373 PhreeqcRM(int nxyz, MP_TYPE thread_count_or_communicator, PHRQ_io * io=NULL, bool delay_construct=false);
374 virtual ~PhreeqcRM(void);
437 IPhreeqc * Concentrations2Utility(const std::vector< double > &c,
438 const std::vector< double > &tc, const std::vector< double > &p_atm);
474 IRM_RESULT CreateMapping(const std::vector< int > &grid2chem);
508 void DecodeError(int result);
530 IRM_RESULT DumpModule(bool dump_on, bool append = false);
553 void ErrorHandler(int result, const std::string &e_string);
570 void ErrorMessage(const std::string &error_string, bool prepend = true);
678 const std::vector < std::vector <int> > & GetBackwardMapping(void) {return this->backward_mapping;}
680 void GetBackwardMappingSWIG(std::vector<int>& nback_output, std::vector<int>& cellnumbers_output);
704 int GetChemistryCellCount(void) const {return this->count_chemistry;}
724 int GetComponentCount(void) const {return (int) this->components.size();}
749const std::vector< std::string > & GetComponents(void) const {return this->components;}
750
791 IRM_RESULT GetConcentrations(std::vector< double > &c_output);
857 std::string GetDatabaseFileName(void) {return this->database_file_name;}
884 IRM_RESULT GetDensityCalculated(std::vector< double > & d_output);
885 IRM_RESULT GetDensity(std::vector< double >& d_output);
921 const std::vector < int> & GetEndCell(void) {return this->end_cell;}
951const std::vector< std::string > & GetEquilibriumPhases(void) const { return this->EquilibriumPhasesList; }
979int GetEquilibriumPhasesCount(void) const { return (int) this->EquilibriumPhasesList.size(); }
980
981
1002 int GetErrorHandlerMode(void) {return this->error_handler_mode;}
1021 std::string GetErrorString(void);
1022
1058const std::vector< std::string > & GetExchangeNames(void) const { return this->ExchangeNamesList; }
1093const std::vector< std::string > & GetExchangeSpecies(void) const { return this->ExchangeSpeciesNamesList; }
1125int GetExchangeSpeciesCount(void) const { return (int) this->ExchangeSpeciesNamesList.size(); }
1126
1127
1145 std::string GetFilePrefix(void) {return this->file_prefix;}
1166const std::vector < int > & GetForwardMapping(void) {return this->forward_mapping_root;}
1167
1197const std::vector< std::string > & GetGasComponents(void) const { return this->GasComponentsList; }
1225int GetGasComponentsCount(void) const { return (int) this->GasComponentsList.size(); }
1226
1259IRM_RESULT GetGasCompMoles(std::vector< double >& gas_moles_output);
1260
1294IRM_RESULT GetGasCompPhi(std::vector< double >& gas_phi);
1295
1328IRM_RESULT GetGasCompPressures(std::vector< double >& gas_pressure);
1329
1361IRM_RESULT GetGasPhaseVolume(std::vector< double >& gas_volume_output);
1362
1389 const std::vector < double > & GetGfw(void) {return this->gfw;}
1411 int GetGridCellCount(void) {return this->nxyz;}
1438IPhreeqc * GetIPhreeqcPointer(int i);
1465IRM_RESULT GetIthConcentration(int i, std::vector< double >& c_output);
1495IRM_RESULT GetIthSpeciesConcentration(int i, std::vector< double >& c_output);
1496
1526const std::vector< std::string > & GetKineticReactions(void) const { return this->KineticReactionsList; }
1554int GetKineticReactionsCount(void) const { return (int) this->KineticReactionsList.size(); }
1555
1580 int GetMpiMyself(void) const {return this->mpi_myself;}
1606 int GetMpiTasks(void) const {return this->mpi_tasks;}
1684 bool GetPartitionUZSolids(void) const {return this->partition_uz_solids;}
1685#ifdef USE_RV
1706 std::vector< double > & GetPoreVolume(void) {return this->pore_volume;}
1707#endif
1727 const std::vector< double >& GetPorosity(void);
1749 const std::vector< double > & GetPressure(void);
1770 const std::vector< int > & GetPrintChemistryMask (void) {return this->print_chem_mask_root;}
1799 const std::vector <bool> & GetPrintChemistryOn(void) {return this->print_chemistry_on;}
1823 bool GetRebalanceByCell(void) const {return this->rebalance_by_cell;}
1848 double GetRebalanceFraction(void) const {return this->rebalance_fraction;}
1884IRM_RESULT GetSaturationCalculated(std::vector< double > & sat_output);
1885IRM_RESULT GetSaturation(std::vector< double >& sat_output);
1925 IRM_RESULT GetSelectedOutput(std::vector< double > &s_output);
2052 IRM_RESULT GetSelectedOutputHeading(int icol, std::string &heading);
2097 IRM_RESULT GetSelectedOutputHeadings(std::vector< std::string >& headings);
2126 bool GetSelectedOutputOn(void) {return this->selected_output_on;}
2175
2202int GetSICount(void) const { return (int) this->SINamesList.size(); }
2236const std::vector< std::string > & GetSINames(void) const { return this->SINamesList; }
2237
2273const std::vector< std::string > & GetSolidSolutionComponents(void) const { return this->SolidSolutionComponentsList; }
2306int GetSolidSolutionComponentsCount(void) const { return (int) this->SolidSolutionComponentsList.size(); }
2307
2344const std::vector< std::string > & GetSolidSolutionNames(void) const { return this->SolidSolutionNamesList; }
2345
2366 const std::vector< double > & GetSolutionVolume(void);
2412 IRM_RESULT GetSpeciesConcentrations(std::vector< double > & species_conc_output);
2444 int GetSpeciesCount(void) {return (int) this->species_names.size();}
2479 const std::vector< double > & GetSpeciesD25(void) {return this->species_d_25;}
2521 IRM_RESULT GetSpeciesLog10Gammas(std::vector< double > & species_log10gammas);
2522
2563IRM_RESULT GetSpeciesLog10Molalities(std::vector< double >& species_log10molalities);
2564
2598 const std::vector< std::string > & GetSpeciesNames(void) {return this->species_names;}
2634 bool GetSpeciesSaveOn(void) {return this->species_save_on;}
2635
2685 const std::vector<cxxNameDouble> & GetSpeciesStoichiometry(void) {return this->species_stoichiometry;}
2687 void GetSpeciesStoichiometrySWIG(std::vector<std::string>& species_output,
2688 std::vector<int>& nelt_output, std::vector<std::string>& elts_output, std::vector<double>& coef_output);
2721 const std::vector< double > & GetSpeciesZ(void) {return this->species_z;}
2756const std::vector < int> & GetStartCell(void) {return this->start_cell;}
2757
2794const std::vector< std::string > & GetSurfaceNames(void) const { return this->SurfaceNamesList; }
2795
2831const std::vector< std::string > & GetSurfaceSpecies(void) const { return this->SurfaceSpeciesNamesList; }
2832
2865int GetSurfaceSpeciesCount(void) const { return (int) this->SurfaceSpeciesNamesList.size(); }
2866
2867
2905const std::vector< std::string > & GetSurfaceTypes(void) const { return this->SurfaceTypesList; }
2906
2927 //const std::vector< double > & GetTemperature(void) {return this->tempc;}
2928 const std::vector< double > & GetTemperature(void);
2929 //int PhreeqcRM::GetVarItemsize(const std::string name);
2930 //int PhreeqcRM::GetVarNbytes(const std::string name);
2931
2952 int GetThreadCount() {return this->nthreads;}
2975 double GetTime(void) const {return this->time;}
3000 double GetTimeConversion(void) {return this->time_conversion;}
3024 double GetTimeStep(void) {return this->time_step;}
3049 int GetUnitsExchange(void) {return this->units_Exchange;}
3074 int GetUnitsGasPhase(void) {return this->units_GasPhase;}
3099 int GetUnitsKinetics(void) {return this->units_Kinetics;}
3124 int GetUnitsPPassemblage(void) {return this->units_PPassemblage;}
3179 int GetUnitsSolution(void) {return this->units_Solution;}
3204 int GetUnitsSSassemblage(void) {return this->units_SSassemblage;}
3229 int GetUnitsSurface(void) {return this->units_Surface;}
3249 const std::vector<double>& GetViscosity();
3250
3251
3274 const std::vector<IPhreeqcPhast *> & GetWorkers() {return this->workers;}
3275#ifdef USE_YAML
3395IRM_RESULT InitializeYAML(std::string yamlfile);
3396#endif
3424IRM_RESULT InitialPhreeqc2Concentrations(std::vector < double > & destination_c,
3425 const std::vector < int > & boundary_solution1);
3465 std::vector < double > & destination_c,
3466 const std::vector < int > & boundary_solution1,
3467 const std::vector < int > & boundary_solution2,
3468 const std::vector < double > & fraction1);
3498IRM_RESULT InitialEquilibriumPhases2Module(const std::vector < int >& equilibrium_phases);
3528IRM_RESULT InitialExchanges2Module(const std::vector < int >& exchanges);
3557IRM_RESULT InitialGasPhases2Module(const std::vector < int >& gas_phases);
3586IRM_RESULT InitialKinetics2Module(const std::vector < int >& kinetics);
3627IRM_RESULT InitialPhreeqc2Module(const std::vector < int >& initial_conditions1);
3691 const std::vector < int >& initial_conditions1,
3692 const std::vector < int >& initial_conditions2,
3693 const std::vector < double >& fraction1);
3723IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector < double >& destination_c,
3724 const std::vector < int >& boundary_solution1);
3764IRM_RESULT InitialPhreeqc2SpeciesConcentrations(std::vector < double >& destination_c,
3765 const std::vector < int >& boundary_solution1,
3766 const std::vector < int >& boundary_solution2,
3767 const std::vector < double >& fraction1);
3795 const std::vector< int >& cell_numbers);
3796
3825IRM_RESULT InitialSolidSolutions2Module(const std::vector < int >& solid_solutions);
3855IRM_RESULT InitialSolutions2Module(const std::vector < int >& solutions);
3884IRM_RESULT InitialSurfaces2Module(const std::vector < int >& surfaces);
3885
3902 IRM_RESULT LoadDatabase(const std::string &database);
3921 void LogMessage(const std::string &str);
4049 void OutputMessage(const std::string &str);
4070 IRM_RESULT ReturnHandler(IRM_RESULT result, const std::string& e_string);
4071
4072
4130 IRM_RESULT RunFile(bool workers, bool initial_phreeqc, bool utility, const std::string & chemistry_name);
4157 IRM_RESULT RunString(bool workers, bool initial_phreeqc, bool utility, const std::string & input_string);
4180 void ScreenMessage(const std::string &str);
4247 IRM_RESULT SetConcentrations(const std::vector< double > &c);
4311 IRM_RESULT SetDensityUser(const std::vector< double > &density);
4312 IRM_RESULT SetDensity(const std::vector< double >& density);
4332 IRM_RESULT SetDumpFileName(const std::string & dump_name);
4392 IRM_RESULT SetFilePrefix(const std::string & prefix);
4393
4429IRM_RESULT SetGasCompMoles(const std::vector< double >& gas_moles);
4468IRM_RESULT SetGasPhaseVolume(const std::vector< double >& gas_volume);
4495IRM_RESULT SetIthConcentration(int i, std::vector< double >& c);
4525IRM_RESULT SetIthSpeciesConcentration(int i, std::vector< double >& c);
4526
4622 IRM_RESULT SetMpiWorkerCallbackC(int (*fcn)(int *method, void * cookie));
4698 IRM_RESULT SetMpiWorkerCallbackFortran(int (*fcn)(int *method));
4808 IRM_RESULT SetPorosity(const std::vector< double > &por);
4809
4830 IRM_RESULT SetPressure(const std::vector< double > &p);
4857 IRM_RESULT SetPrintChemistryMask(const std::vector<int> & cell_mask);
4892 IRM_RESULT SetPrintChemistryOn(bool workers, bool initial_phreeqc, bool utility);
4978 IRM_RESULT SetRepresentativeVolume(const std::vector< double > &rv);
5008 IRM_RESULT SetSaturationUser(const std::vector< double > &sat);
5009 IRM_RESULT SetSaturation(const std::vector< double >& sat);
5124 IRM_RESULT SetTemperature(const std::vector< double > &t);
5142 IRM_RESULT SetTime(double time);
5161 IRM_RESULT SetTimeConversion(double conv_factor);
5181 IRM_RESULT SetTimeStep(double time_step);
5494IRM_RESULT SpeciesConcentrations2Module(const std::vector< double > & species_conc);
5495
5630 void WarningMessage(const std::string &warnstr);
5631public:
5632// Utilities
5633 static std::string Char2TrimString(const char * str, size_t l = 0);
5634 static bool FileExists(const std::string &name);
5635 static void FileRename(const std::string &temp_name, const std::string &name,
5636 const std::string &backup_name);
5637 static IRM_RESULT Int2IrmResult(int r, bool positive_ok);
5638private:
5639 IRM_RESULT CellInitialize(
5640 int i,
5641 int n_user_new,
5642 const int *initial_conditions1,
5643 const int *initial_conditions2,
5644 double *fraction1,
5645 std::set<std::string> &error_set);
5646 IRM_RESULT CheckCells();
5647 int CheckSelectedOutput();
5648 //void Collapse2Nchem(double *d_in, double *d_out);
5649 //void Collapse2Nchem(int *i_in, int *i_out);
5650 IPhreeqc * Concentrations2UtilityH2O(const std::vector< double > &c_in,
5651 const std::vector< double > &t_in, const std::vector< double > &p_in);
5652 IPhreeqc * Concentrations2UtilityNoH2O(const std::vector< double > &c_in,
5653 const std::vector< double > &t_in, const std::vector< double > &p_in);
5654 void Concentrations2Solutions(int n, std::vector< double > &c);
5655 void Concentrations2SolutionsH2O(int n, std::vector< double > &c);
5656 void Concentrations2SolutionsNoH2O(int n, std::vector< double > &c);
5657 void cxxSolution2concentration(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5658 void cxxSolution2concentrationH2O(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5659 void cxxSolution2concentrationNoH2O(cxxSolution * cxxsoln_ptr, std::vector< double > & d, double v, double dens);
5660 void GatherNchem(std::vector< double > &source, std::vector< double > &destination);
5661 cxxStorageBin & Get_phreeqc_bin(void) {return *this->phreeqc_bin;}
5662 IRM_RESULT HandleErrorsInternal(std::vector< int > & r);
5663 void PartitionUZ(int n, int iphrq, int ihst, double new_frac);
5664 void RebalanceLoad(void);
5665 void RebalanceLoadPerCell(void);
5666 IRM_RESULT RunCellsThread(int i);
5667 IRM_RESULT RunFileThread(int n);
5668 IRM_RESULT RunStringThread(int n, std::string & input);
5669 IRM_RESULT RunCellsThreadNoPrint(int n);
5670 void Scale_solids(int n, int iphrq, double frac);
5671 void ScatterNchem(double *d_array);
5672 void ScatterNchem(int *i_array);
5673 void ScatterNchem(std::vector< double > &source, std::vector< double > &destination);
5674 void ScatterNchem(std::vector< int > &source, std::vector< int > &destination);
5675 IRM_RESULT SetChemistryFileName(const char * prefix = NULL);
5676 IRM_RESULT SetDatabaseFileName(const char * db = NULL);
5677 void SetEndCells(void);
5678 void SetEndCellsHeterogeneous(void);
5679 double TimeStandardTask(void);
5680 IRM_RESULT TransferCells(cxxStorageBin &t_bin, int old, int nnew);
5681 IRM_RESULT TransferCellsUZ(std::ostringstream &raw_stream, int old, int nnew);
5682
5683private:
5684 //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);
5685 IRM_RESULT SetGeneric(const std::vector< double > &source, std::vector< double > &destination_root, std::vector< double > &destination_worker, int mpiMethod, const std::string &name);
5686public:
5687
5688protected:
5689
5690#if defined(_MSC_VER)
5691/* disable warning C4251: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' */
5692#pragma warning(disable:4251)
5693#endif
5695 bool component_h2o; // true: use H2O, excess H, excess O, and charge;
5696 // false total H, total O, and charge
5697 std::string database_file_name;
5698 std::string chemistry_file_name;
5699 std::string dump_file_name;
5700 std::string file_prefix;
5701 cxxStorageBin * phreeqc_bin;
5702 int mpi_myself;
5703 int mpi_tasks;
5704 std::vector <std::string> components; // list of components to be transported
5705 std::vector <double> gfw; // gram formula weights converting mass to moles (1 for each component)
5706 double gfw_water; // gfw of water
5707 bool partition_uz_solids;
5708 int nxyz; // number of nodes
5709 int count_chemistry; // number of cells for chemistry
5710 double time; // time from transport, sec
5711 double time_step; // time step from transport, sec
5712 double time_conversion; // time conversion factor, multiply to convert to preferred time unit for output
5713 std::vector <double> CurrentConcentrations;
5714 std::vector <double> CurrentSpeciesConcentrations;
5715 std::vector <double> IthCurrentConcentrations;
5716 std::vector <double> IthCurrentSpeciesConcentrations;
5717 std::set<int> IthConcentrationSet;
5718 std::set<int> IthSpeciesConcentrationSet;
5719 std::vector <double> old_saturation_root; // saturation fraction from previous step
5720 std::vector <double> old_saturation_worker;
5721 std::vector< double > saturation_root; // nxyz saturation fraction
5722 std::vector< double > saturation_worker; // nchem on workers saturation fraction
5723 std::vector< double > pressure_root; // nxyz on root current pressure
5724 std::vector< double > pressure_worker; // nchem on workers current pressure
5725 std::vector< double > rv_root; // nxyz on root representative volume
5726 std::vector< double > rv_worker; // nchem on workers representative volume
5727 std::vector< double > porosity_root; // nxyz porosity
5728 std::vector< double > porosity_worker; // nchem on workers porosity
5729 std::vector< double > tempc_root; // nxyz on root temperature Celsius
5730 std::vector< double > tempc_worker; // nchem on workers temperature Celsius
5731 std::vector< double > density_root; // nxyz density
5732 std::vector< double > density_worker; // nchem on workers density
5733 std::vector< double > viscosity_root; // nxyz viscosity
5734 std::vector< double > viscosity_worker; // nchem on workers viscosity
5735 std::vector< double > solution_volume_root; // nxyz on root solution volume
5736 std::vector< double > solution_volume_worker; // nchem on workers solution_volume
5737 std::vector< int > print_chem_mask_root; // nxyz print flags for output file
5738 std::vector< int > print_chem_mask_worker; // nchem print flags for output file
5739 bool rebalance_by_cell; // rebalance method 0 std, 1 by_cell
5740 double rebalance_fraction; // parameter for rebalancing process load for parallel
5741 int units_Solution; // 1 mg/L, 2 mol/L, 3 kg/kgs
5742 int units_PPassemblage; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5743 int units_Exchange; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5744 int units_Surface; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5745 int units_GasPhase; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5746 int units_SSassemblage; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5747 int units_Kinetics; // 0, mol/L cell; 1, mol/L water; 2 mol/L rock
5748 std::vector <int> forward_mapping_root; // mapping from nxyz cells to count_chem chemistry cells
5749 std::vector <std::vector <int> > backward_mapping; // mapping from count_chem chemistry cells to nxyz cells
5750 bool use_solution_density_volume;
5751 bool worker_waiting;
5752 // print flags
5753 std::vector< bool > print_chemistry_on; // print flag for chemistry output file
5754 bool selected_output_on; // create selected output
5755
5756 int error_count;
5757 int error_handler_mode; // 0, return code; 1, throw; 2 exit;
5758 bool need_error_check;
5759 std::string phreeqcrm_error_string;
5760
5761 std::map<std::string, int> method_map;
5762 // threading
5763 int nthreads;
5764 std::vector<IPhreeqcPhast *> workers;
5765 //std::vector< int > start_cell;
5766 //std::vector< int > end_cell;
5767 PHRQ_io *phreeqcrm_io;
5768 bool delete_phreeqcrm_io;
5769
5770 // mpi
5771#ifdef USE_MPI
5772 MPI_Comm phreeqcrm_comm; // MPI communicator
5773#endif
5774 int (*mpi_worker_callback_fortran) (int *method);
5775 int (*mpi_worker_callback_c) (int *method, void *cookie);
5776 void *mpi_worker_callback_cookie;
5777
5778 // mcd
5779 bool species_save_on;
5780 std::vector <std::string> species_names;
5781 std::vector <double> species_z;
5782 std::vector <double> species_d_25;
5783 std::vector <cxxNameDouble> species_stoichiometry;
5784 std::map<int, int> s_num2rm_species_num;
5785 std::vector< double > standard_task_vector; // root only
5786
5787 std::vector< int > start_cell;
5788 std::vector< int > end_cell;
5789 // reactant lists
5790 std::vector <std::string> ExchangeSpeciesNamesList;
5791 std::vector <std::string> ExchangeNamesList;
5792 std::vector <std::string> SurfaceSpeciesNamesList;
5793 std::vector <std::string> SurfaceTypesList;
5794 std::vector <std::string> SurfaceNamesList;
5795
5796 std::vector <std::string> EquilibriumPhasesList;
5797 std::vector <std::string> GasComponentsList;
5798 std::vector <std::string> KineticReactionsList;
5799 std::vector <std::string> SolidSolutionComponentsList;
5800 std::vector <std::string> SolidSolutionNamesList;
5801 std::vector <std::string> SINamesList;
5802 std::set <std::string> ElementRedoxSet;
5803
5804protected:
5805 static const int default_nxyz = 10;
5806 static const MP_TYPE default_data_for_parallel_processing;
5807 struct Initializer {
5808 int nxyz_arg;
5809 MP_TYPE data_for_parallel_processing;
5810 PHRQ_io *io;
5811
5812 Initializer()
5813 : nxyz_arg(default_nxyz) , data_for_parallel_processing(default_data_for_parallel_processing), io(NULL) {}
5814
5815 Initializer(int nxyz_arg, MP_TYPE data_for_parallel_processing, PHRQ_io *io)
5816 : nxyz_arg(nxyz_arg) , data_for_parallel_processing(data_for_parallel_processing), io(io) {}
5817
5818 } initializer;
5819
5820 virtual void Construct(Initializer initializer);
5821
5823private:
5824 //friend class RM_interface;
5825 friend class BMIPhreeqcRM;
5826 friend class VarManager;
5827
5828#if defined(_MSC_VER)
5829/* reset warning C4251 */
5830#pragma warning(default:4251)
5831#endif
5832
5833};
5834#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:78
@ METHOD_SETCONCENTRATIONS
Definition PhreeqcRM.h:90
@ METHOD_STATEDELETE
Definition PhreeqcRM.h:121
@ METHOD_RUNCELLS
Definition PhreeqcRM.h:86
@ METHOD_CONSTRUCT
Definition PhreeqcRM.h:62
@ METHOD_SETSELECTEDOUTPUTON
Definition PhreeqcRM.h:105
@ METHOD_SETTEMPERATURE
Definition PhreeqcRM.h:107
@ METHOD_SETCOMPONENTH2O
Definition PhreeqcRM.h:89
@ METHOD_GETSATURATIONCALCULATED
Definition PhreeqcRM.h:74
@ METHOD_GETSPECIESCONCENTRATIONS
Definition PhreeqcRM.h:77
@ METHOD_SETUNITSGASPHASE
Definition PhreeqcRM.h:112
@ METHOD_SETSPECIESSAVEON
Definition PhreeqcRM.h:106
@ METHOD_FINDCOMPONENTS
Definition PhreeqcRM.h:65
@ METHOD_CREATEMAPPING
Definition PhreeqcRM.h:63
@ METHOD_SETPOROSITY
Definition PhreeqcRM.h:97
@ METHOD_GETSPECIESLOG10MOLALITIES
Definition PhreeqcRM.h:79
@ METHOD_GETPRESSURE
Definition PhreeqcRM.h:73
@ METHOD_RUNSTRING
Definition PhreeqcRM.h:88
@ METHOD_GETCONCENTRATIONS
Definition PhreeqcRM.h:66
@ METHOD_SETPRINTCHEMISTRYON
Definition PhreeqcRM.h:99
@ METHOD_GETGASCOMPPHI
Definition PhreeqcRM.h:71
@ METHOD_GETGASCOMPPRESSURES
Definition PhreeqcRM.h:70
@ METHOD_SETUNITSKINETICS
Definition PhreeqcRM.h:113
@ METHOD_SETFILEPREFIX
Definition PhreeqcRM.h:93
@ METHOD_INITIALPHREEQCCELL2MODULE
Definition PhreeqcRM.h:83
@ METHOD_SETSATURATIONUSER
Definition PhreeqcRM.h:104
@ METHOD_GETGASCOMPMOLES
Definition PhreeqcRM.h:69
@ METHOD_INITIALPHREEQC2MODULE
Definition PhreeqcRM.h:82
@ METHOD_SETREBALANCEFRACTION
Definition PhreeqcRM.h:102
@ METHOD_GETTEMPERATURE
Definition PhreeqcRM.h:80
@ METHOD_SETUNITSEXCHANGE
Definition PhreeqcRM.h:111
@ METHOD_RUNFILE
Definition PhreeqcRM.h:87
@ METHOD_SETPARTITIONUZSOLIDS
Definition PhreeqcRM.h:96
@ METHOD_SETTIMESTEP
Definition PhreeqcRM.h:110
@ METHOD_GETERRORSTRING
Definition PhreeqcRM.h:68
@ METHOD_GETVISCOSITY
Definition PhreeqcRM.h:81
@ METHOD_SETUNITSPPASSEMBLAGE
Definition PhreeqcRM.h:114
@ METHOD_GETSOLUTIONVOLUME
Definition PhreeqcRM.h:76
@ METHOD_SETGASCOMPMOLES
Definition PhreeqcRM.h:94
@ METHOD_SETTIMECONVERSION
Definition PhreeqcRM.h:109
@ METHOD_STATESAVE
Definition PhreeqcRM.h:119
@ METHOD_SETGASPHASEVOLUME
Definition PhreeqcRM.h:95
@ METHOD_GETGASPHASEVOLUME
Definition PhreeqcRM.h:72
@ METHOD_SETUNITSSOLUTION
Definition PhreeqcRM.h:115
@ METHOD_SETREPRESENTATIVEVOLUME
Definition PhreeqcRM.h:103
@ METHOD_SETTIME
Definition PhreeqcRM.h:108
@ METHOD_MPIWORKERBREAK
Definition PhreeqcRM.h:85
@ METHOD_GETDENSITYCALCULATED
Definition PhreeqcRM.h:67
@ METHOD_SETERRORHANDLERMODE
Definition PhreeqcRM.h:92
@ METHOD_SETREBALANCEBYCELL
Definition PhreeqcRM.h:101
@ METHOD_SETUNITSSURFACE
Definition PhreeqcRM.h:117
@ METHOD_SETUNITSSSASSEMBLAGE
Definition PhreeqcRM.h:116
@ METHOD_LOADDATABASE
Definition PhreeqcRM.h:84
@ METHOD_SETPRINTCHEMISTRYMASK
Definition PhreeqcRM.h:100
@ METHOD_USESOLUTIONDENSITYVOLUME
Definition PhreeqcRM.h:122
@ METHOD_GETSELECTEDOUTPUT
Definition PhreeqcRM.h:75
@ METHOD_SETDENSITYUSER
Definition PhreeqcRM.h:91
@ METHOD_SETPRESSURE
Definition PhreeqcRM.h:98
@ METHOD_DUMPMODULE
Definition PhreeqcRM.h:64
@ METHOD_SPECIESCONCENTRATIONS2MODULE
Definition PhreeqcRM.h:118
@ METHOD_STATEAPPLY
Definition PhreeqcRM.h:120
#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:50
const char * what() const
Definition PhreeqcRM.h:52
Geochemical reaction module.
Definition PhreeqcRM.h:269
IRM_RESULT CloseFiles(void)
const std::vector< double > & GetGfw(void)
Definition PhreeqcRM.h:1389
IRM_RESULT GetSaturation(std::vector< double > &sat_output)
int GetExchangeSpeciesCount(void) const
Definition PhreeqcRM.h:1125
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:2306
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:1770
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:1823
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:3099
const std::vector< std::string > & GetKineticReactions(void) const
Definition PhreeqcRM.h:1526
int GetUnitsSSassemblage(void)
Definition PhreeqcRM.h:3204
int GetChemistryCellCount(void) const
Definition PhreeqcRM.h:704
IRM_RESULT SetUnitsSSassemblage(int option)
const std::vector< double > & GetSpeciesD25(void)
Definition PhreeqcRM.h:2479
IRM_RESULT SetPorosity(const std::vector< double > &por)
int GetCurrentSelectedOutputUserNumber(void)
const std::vector< std::string > & GetGasComponents(void) const
Definition PhreeqcRM.h:1197
IRM_RESULT InitialGasPhases2Module(const std::vector< int > &gas_phases)
const std::vector< cxxNameDouble > & GetSpeciesStoichiometry(void)
Definition PhreeqcRM.h:2685
const std::vector< std::string > & GetExchangeNames(void) const
Definition PhreeqcRM.h:1058
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:2634
const std::vector< std::string > & GetExchangeSpecies(void) const
Definition PhreeqcRM.h:1093
IRM_RESULT SetSelectedOutputOn(bool tf)
const std::vector< std::string > & GetSpeciesNames(void)
Definition PhreeqcRM.h:2598
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:3124
IRM_RESULT SetUnitsPPassemblage(int option)
const std::vector< int > & GetEndCell(void)
Definition PhreeqcRM.h:921
int GetThreadCount()
Definition PhreeqcRM.h:2952
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:724
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:2273
std::string GetDatabaseFileName(void)
Definition PhreeqcRM.h:857
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:2794
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:1684
IRM_RESULT GetGasCompPhi(std::vector< double > &gas_phi)
int GetMpiMyself(void) const
Definition PhreeqcRM.h:1580
const std::vector< std::string > & GetSolidSolutionNames(void) const
Definition PhreeqcRM.h:2344
const std::vector< std::string > & GetComponents(void) const
Definition PhreeqcRM.h:749
IRM_RESULT InitialSolutions2Module(const std::vector< int > &solutions)
double GetTime(void) const
Definition PhreeqcRM.h:2975
int GetEquilibriumPhasesCount(void) const
Definition PhreeqcRM.h:979
int GetMpiTasks(void) const
Definition PhreeqcRM.h:1606
IRM_RESULT SetGasPhaseVolume(const std::vector< double > &gas_volume)
IRM_RESULT StateApply(int istate)
int GetSpeciesCount(void)
Definition PhreeqcRM.h:2444
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:1166
int GetSelectedOutputColumnCount(void)
IRM_RESULT InitializeYAML(std::string yamlfile)
IRM_RESULT SetUnitsExchange(int option)
std::string GetErrorString(void)
int GetGridCellCount(void)
Definition PhreeqcRM.h:1411
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:2236
void ErrorMessage(const std::string &error_string, bool prepend=true)
const std::vector< int > & GetStartCell(void)
Definition PhreeqcRM.h:2756
int GetErrorHandlerMode(void)
Definition PhreeqcRM.h:1002
bool GetSelectedOutputOn(void)
Definition PhreeqcRM.h:2126
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:1554
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:678
IRM_RESULT SetUnitsSolution(int option)
IRM_RESULT InitialSurfaces2Module(const std::vector< int > &surfaces)
double GetTimeStep(void)
Definition PhreeqcRM.h:3024
int GetUnitsExchange(void)
Definition PhreeqcRM.h:3049
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:3274
IRM_RESULT SetDensityUser(const std::vector< double > &density)
IRM_RESULT CreateMapping(const std::vector< int > &grid2chem)
std::string GetFilePrefix(void)
Definition PhreeqcRM.h:1145
const std::vector< double > & GetSpeciesZ(void)
Definition PhreeqcRM.h:2721
IRM_RESULT SetCurrentSelectedOutputUserNumber(int n_user)
int MpiAbort()
int GetSICount(void) const
Definition PhreeqcRM.h:2202
IRM_RESULT SetUnitsSurface(int option)
double GetTimeConversion(void)
Definition PhreeqcRM.h:3000
IRM_RESULT GetDensity(std::vector< double > &d_output)
double GetRebalanceFraction(void) const
Definition PhreeqcRM.h:1848
void WarningMessage(const std::string &warnstr)
const std::vector< double > & GetSolutionVolume(void)
IRM_RESULT SetErrorHandlerMode(int mode)
int GetGasComponentsCount(void) const
Definition PhreeqcRM.h:1225
IRM_RESULT SetSaturationUser(const std::vector< double > &sat)
const std::vector< std::string > & GetSurfaceTypes(void) const
Definition PhreeqcRM.h:2905
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:951
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:1799
const std::vector< std::string > & GetSurfaceSpecies(void) const
Definition PhreeqcRM.h:2831
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:3229
int GetSurfaceSpeciesCount(void) const
Definition PhreeqcRM.h:2865
int GetUnitsSolution(void)
Definition PhreeqcRM.h:3179
IRM_RESULT SetDensity(const std::vector< double > &density)
int GetUnitsGasPhase(void)
Definition PhreeqcRM.h:3074
IRM_RESULT SetUnitsGasPhase(int option)
Definition PhreeqcRM.h:128
static std::map< size_t, T * > _Instances
Definition PhreeqcRM.h:244
static IRM_RESULT Destroy(int id)
Definition PhreeqcRM.h:174
static Derived * GetInstance(int id)
Definition PhreeqcRM.h:160
static size_t _InstancesIndex
Definition PhreeqcRM.h:245
size_t _Index
Definition PhreeqcRM.h:241
StaticIndexer(T *self)
Definition PhreeqcRM.h:130
int GetIndex()
Definition PhreeqcRM.h:235
~StaticIndexer()
Definition PhreeqcRM.h:137
static void DestroyAll()
Definition PhreeqcRM.h:202
static std::mutex _InstancesLock
Definition PhreeqcRM.h:243
static T * GetInstance(int id)
Definition PhreeqcRM.h:148