OGS
PhreeqcInstancePool.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <IPhreeqc.h>
7
8#include "BaseLib/Error.h"
9#include "BaseLib/Logging.h"
10
11namespace ChemistryLib
12{
13namespace PhreeqcIOData
14{
15int PhreeqcInstancePool::createInstance(std::string const& database)
16{
17 int const id = CreateIPhreeqc();
18 if (id < 0)
19 {
21 "Failed to create PHREEQC instance (IPQ_RESULT error code = {}).",
22 id);
23 }
24 if (LoadDatabase(id, database.c_str()) != IPQ_OK)
25 {
26 OutputErrorString(id);
28 "Failed to load thermodynamic database '{:s}' for PHREEQC "
29 "instance {}.",
30 database, id);
31 }
32 return id;
33}
34
36{
37 SetSelectedOutputFileOn(id, 0);
38 if (SetSelectedOutputStringOn(id, 1) != IPQ_OK)
39 {
40 OGS_FATAL("Failed to enable string output for PHREEQC instance {}.",
41 id);
42 }
43 SetDumpFileOn(id, 0);
44 if (SetDumpStringOn(id, 1) != IPQ_OK)
45 {
46 OGS_FATAL("Failed to enable dump string for PHREEQC instance {}.", id);
47 }
48}
49
50PhreeqcInstancePool::PhreeqcInstancePool(std::string const& database,
51 int const num_instances)
52{
53 if (num_instances < 1)
54 {
55 OGS_FATAL("PhreeqcInstancePool requires at least 1 instance, got {}.",
56 num_instances);
57 }
58
59 instance_ids_.reserve(num_instances);
60 for (int i = 0; i < num_instances; ++i)
61 {
62 int const id = createInstance(database);
64 instance_ids_.push_back(id);
65 }
66
67 INFO("Created {} PHREEQC instances for parallel chemistry execution.",
68 num_instances);
69}
70
72{
73 for (int const id : instance_ids_)
74 {
75 DestroyIPhreeqc(id);
76 }
77}
78
80{
81 if (thread_id < 0 || thread_id >= static_cast<int>(instance_ids_.size()))
82 {
84 "Thread ID {} is out of range [0, {}). This indicates a bug in "
85 "the parallel chemistry implementation.",
86 thread_id, instance_ids_.size());
87 }
88 return instance_ids_[thread_id];
89}
90
91} // namespace PhreeqcIOData
92} // namespace ChemistryLib
#define OGS_FATAL(...)
Definition Error.h:10
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
PhreeqcInstancePool(std::string const &database, int num_instances)
static int createInstance(std::string const &database)