OGS
CreateLookupTable.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 "CreateLookupTable.h"
5
6#include <algorithm>
7#include <boost/algorithm/string.hpp>
8#include <fstream>
9#include <map>
10
11#include "BaseLib/Algorithm.h"
12#include "BaseLib/Error.h"
13#include "BaseLib/FileTools.h"
14#include "LookupTable.h"
16
17namespace
18{
22std::vector<std::size_t> getIndexVector(std::vector<double> const& data,
23 double const value)
24{
25 std::vector<std::size_t> idx_vec;
26
27 for (auto it = data.begin();
28 (it = std::find(it, data.end(), value)) != data.end();
29 ++it)
30 {
31 idx_vec.push_back(std::distance(data.begin(), it));
32 }
33
34 if (idx_vec.empty())
35 {
36 OGS_FATAL("No matching element {} is found in the vector.", value);
37 }
38
39 return idx_vec;
40}
41
42} // namespace
43
44namespace ProcessLib
45{
46namespace ComponentTransport
47{
48std::unique_ptr<LookupTable> createLookupTable(
49 std::optional<std::string> const tabular_file,
50 std::filesystem::path const& project_directory,
51 std::vector<std::vector<std::reference_wrapper<ProcessVariable>>> const&
52 process_variables)
53{
54 if (!tabular_file)
55 {
56 return nullptr;
57 }
58
59 auto const path_to_tabular_file =
60 BaseLib::joinPaths(project_directory.string(), *tabular_file);
61
62 if (!BaseLib::IsFileExisting(path_to_tabular_file))
63 {
65 "Not found the tabular file with the specified file path: {:s}",
66 path_to_tabular_file);
67 }
68
69 INFO("Found the tabular file: {:s}", path_to_tabular_file);
70
71 std::ifstream in(path_to_tabular_file);
72 if (!in)
73 {
74 OGS_FATAL("Couldn't open the tabular file: {:s}.",
75 path_to_tabular_file);
76 }
77
78 // read field names
79 std::string line;
80 std::getline(in, line);
81 std::vector<std::string> field_names;
82 boost::split(field_names, line, boost::is_any_of("\t "));
83
84 // categorize entry fields
85 std::vector<std::string> input_field_names;
86 std::copy_if(field_names.begin(), field_names.end(),
87 std::back_inserter(input_field_names),
88 [](auto const& field_name) -> bool
89 { return field_name.find("_new") == std::string::npos; });
90
91 // read table entries
92 std::map<std::string, std::vector<double>> tabular_data;
93 while (std::getline(in, line))
94 {
95 std::vector<std::string> field_data;
96 boost::split(field_data, line, boost::is_any_of("\t "));
97
98 assert(field_data.size() == field_names.size());
99 for (std::size_t field_id = 0; field_id < field_data.size(); ++field_id)
100 {
101 tabular_data[field_names[field_id]].push_back(
102 std::stod(field_data[field_id]));
103 }
104 }
105 in.close();
106
107 std::vector<Field> input_fields;
108 input_fields.reserve(input_field_names.size());
109 for (auto const& field_name : input_field_names)
110 {
111 auto pv = std::find_if(process_variables.begin(),
112 process_variables.end(),
113 [&field_name](auto const& v) -> bool
114 {
115 return v[0].get().getName() == field_name ||
116 v[0].get().getName() + "_prev" ==
117 field_name;
118 });
119
120 if (pv == process_variables.end())
121 {
122 OGS_FATAL(
123 "Not found field name {:s} in the group of process variables "
124 "defined in the project file.",
125 field_name);
126 }
127
128 auto const process_id =
129 static_cast<int>(std::distance(process_variables.cbegin(), pv));
130
131 auto seed_points = tabular_data[field_name];
132 BaseLib::makeVectorUnique(seed_points);
133
134 std::vector<std::vector<std::size_t>> point_id_groups;
135 for (auto const seed_point : seed_points)
136 {
137 auto const point_id_group =
138 getIndexVector(tabular_data[field_name], seed_point);
139 point_id_groups.push_back(point_id_group);
140 }
141
142 input_fields.emplace_back(point_id_groups, seed_points, field_name,
143 process_id);
144 }
145
146 return std::make_unique<LookupTable>(std::move(input_fields),
147 std::move(tabular_data));
148}
149
150} // namespace ComponentTransport
151} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:19
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:23
std::string joinPaths(std::string const &pathA, std::string const &pathB)
void makeVectorUnique(std::vector< T > &v)
Definition Algorithm.h:173
std::unique_ptr< LookupTable > createLookupTable(std::optional< std::string > const tabular_file, std::filesystem::path const &project_directory, std::vector< std::vector< std::reference_wrapper< ProcessVariable > > > const &process_variables)
std::vector< std::size_t > getIndexVector(std::vector< double > const &data, double const value)