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 <iostream>
7
8namespace ChemistryLib
9{
10namespace PhreeqcIOData
11{
12void Dump::print(std::ostream& os, std::size_t const num_chemical_systems) const
13{
14 os << "DUMP"
15 << "\n";
16 os << "-file " << dump_file << "\n";
17 os << "-append false"
18 << "\n";
19 os << "-solution 1-" << num_chemical_systems << "\n";
20 os << "END"
21 << "\n";
22}
23
24void Dump::readDumpFile(std::istream& in,
25 std::size_t const num_chemical_systems)
26{
28 aqueous_solutions_prev.reserve(num_chemical_systems);
29
30 std::string line;
31 std::string aqueous_solution_prev;
32 std::size_t chemical_system_id = 0;
33 while (std::getline(in, line))
34 {
35 if (line.find("USE reaction_pressure none") != std::string::npos)
36 {
37 break;
38 }
39
40 if (line.find("SOLUTION_RAW") != std::string::npos)
41 {
42 aqueous_solution_prev =
43 "SOLUTION_RAW " +
44 std::to_string(num_chemical_systems + chemical_system_id + 1) +
45 "\n";
46 continue;
47 }
48
49 aqueous_solution_prev += line + "\n";
50
51 if (line.find("-gammas") != std::string::npos)
52 {
53 aqueous_solutions_prev.push_back(aqueous_solution_prev);
54 aqueous_solution_prev.clear();
55 ++chemical_system_id;
56 }
57 }
58}
59
60void Dump::readDumpFromString(std::string_view dump_content,
61 std::size_t const num_chemical_systems)
62{
64 aqueous_solutions_prev.reserve(num_chemical_systems);
65
66 std::string_view line;
67 std::string aqueous_solution_prev;
68 std::size_t chemical_system_id = 0;
69 std::size_t pos = 0;
70
71 while (pos < dump_content.size())
72 {
73 // Extract next line
74 if (const auto newline_pos = dump_content.find('\n', pos);
75 newline_pos == std::string_view::npos)
76 {
77 line = dump_content.substr(pos);
78 pos = dump_content.size();
79 }
80 else
81 {
82 line = dump_content.substr(pos, newline_pos - pos);
83 pos = newline_pos + 1;
84 }
85
86 // Remove trailing \r if present (Windows line endings)
87 if (!line.empty() && line.back() == '\r')
88 {
89 line.remove_suffix(1);
90 }
91
92 if (line.find("USE reaction_pressure none") != std::string::npos)
93 {
94 break;
95 }
96
97 if (line.find("SOLUTION_RAW") != std::string::npos)
98 {
99 aqueous_solution_prev =
100 "SOLUTION_RAW " +
101 std::to_string(num_chemical_systems + chemical_system_id + 1) +
102 "\n";
103 continue;
104 }
105
106 aqueous_solution_prev += line;
107 aqueous_solution_prev += "\n";
108
109 if (line.find("-gammas") != std::string::npos)
110 {
111 aqueous_solutions_prev.push_back(aqueous_solution_prev);
112 aqueous_solution_prev.clear();
113 ++chemical_system_id;
114 }
115 }
116}
117} // namespace PhreeqcIOData
118} // namespace ChemistryLib
std::string const dump_file
Definition Dump.h:46
void readDumpFromString(std::string_view dump_content, std::size_t const num_chemical_systems)
Definition Dump.cpp:60
void readDumpFile(std::istream &in, std::size_t const num_chemical_systems)
Definition Dump.cpp:24
void print(std::ostream &os, std::size_t const num_chemical_systems) const
Definition Dump.cpp:12
std::vector< std::string > aqueous_solutions_prev
Definition Dump.h:47