OGS
Dump.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
4#include "Dump.h"
5
6#include <sstream>
7
8#include "BaseLib/Logging.h"
9
10namespace
11{
15std::vector<std::string> parseDumpContent(
16 std::istream& in,
17 std::size_t const num_chemical_systems,
18 std::size_t const start_id = 0)
19{
20 std::vector<std::string> results;
21 std::string line;
22 std::string current;
23 std::size_t id = start_id;
24
25 while (std::getline(in, line))
26 {
27 // Strip trailing \r for robustness when the stream was produced on a
28 // platform with CRLF line endings.
29 if (!line.empty() && line.back() == '\r')
30 {
31 line.pop_back();
32 }
33 if (line.find("USE reaction_pressure none") != std::string::npos)
34 {
35 break;
36 }
37 if (line.find("SOLUTION_RAW") != std::string::npos)
38 {
39 current = "SOLUTION_RAW " +
40 std::to_string(num_chemical_systems + id + 1) + "\n";
41 continue;
42 }
43 current += line;
44 current += "\n";
45 if (line.find("-gammas") != std::string::npos)
46 {
47 results.push_back(std::move(current));
48 current.clear();
49 ++id;
50 }
51 }
52 return results;
53}
54} // namespace
55
56namespace ChemistryLib
57{
58namespace PhreeqcIOData
59{
60void Dump::print(std::ostream& os, std::size_t const num_chemical_systems) const
61{
62 os << "DUMP"
63 << "\n";
64 os << "-file " << dump_file << "\n";
65 os << "-append false"
66 << "\n";
67 os << "-solution 1-" << num_chemical_systems << "\n";
68 os << "END"
69 << "\n";
70}
71
72void Dump::readDumpFile(std::istream& in,
73 std::size_t const num_chemical_systems)
74{
75 aqueous_solutions_prev = parseDumpContent(in, num_chemical_systems);
76}
77
78void Dump::readDumpFromString(std::string_view const dump_content,
79 std::size_t const num_chemical_systems)
80{
81 std::istringstream in{std::string(dump_content)};
82 readDumpFile(in, num_chemical_systems);
83}
84
85void Dump::readDumpFromStringForSystem(std::string_view const dump_content,
86 std::size_t const chemical_system_id,
87 std::size_t const num_chemical_systems)
88{
89 if (aqueous_solutions_prev.size() < num_chemical_systems)
90 {
91 aqueous_solutions_prev.resize(num_chemical_systems);
92 }
93
94 std::istringstream in{std::string(dump_content)};
95 auto parsed =
96 parseDumpContent(in, num_chemical_systems, chemical_system_id);
97 if (parsed.empty())
98 {
99 WARN("No SOLUTION_RAW found in dump for chemical system {}.",
100 chemical_system_id);
101 return;
102 }
103 aqueous_solutions_prev[chemical_system_id] = std::move(parsed[0]);
104}
105} // namespace PhreeqcIOData
106} // namespace ChemistryLib
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
std::vector< std::string > parseDumpContent(std::istream &in, std::size_t const num_chemical_systems, std::size_t const start_id=0)
Definition Dump.cpp:15
void readDumpFromString(std::string_view dump_content, std::size_t const num_chemical_systems)
Definition Dump.cpp:78
void readDumpFile(std::istream &in, std::size_t const num_chemical_systems)
Definition Dump.cpp:72
void readDumpFromStringForSystem(std::string_view dump_content, std::size_t const chemical_system_id, std::size_t const num_chemical_systems)
Definition Dump.cpp:85
void print(std::ostream &os, std::size_t const num_chemical_systems) const
Definition Dump.cpp:60
std::vector< std::string > aqueous_solutions_prev
Definition Dump.h:56