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