OGS
CreateAnchors.cpp
Go to the documentation of this file.
1
10#include <spdlog/spdlog.h>
11#include <tclap/CmdLine.h>
12#include <vtkXMLUnstructuredGridReader.h>
13#include <vtkXMLUnstructuredGridWriter.h>
14
15#include <fstream>
16#include <nlohmann/json.hpp>
17
18#include "BaseLib/FileTools.h"
19#include "BaseLib/Logging.h"
21#include "InfoLib/GitInfo.h"
22
23vtkSmartPointer<vtkUnstructuredGrid> readGrid(std::string const& input_filename)
24{
25 if (!BaseLib::IsFileExisting(input_filename))
26 {
27 OGS_FATAL("'{:s}' file does not exist.", input_filename);
28 }
29
30 vtkNew<vtkXMLUnstructuredGridReader> reader;
31 reader->SetFileName(input_filename.c_str());
32 reader->Update();
33
34 if (!reader->GetOutput())
35 {
36 OGS_FATAL("Could not open file '{}'", input_filename);
37 }
38 if (reader->GetOutput()->GetNumberOfCells() == 0)
39 {
40 OGS_FATAL("Mesh file '{}' contains no cells.", input_filename);
41 }
42 return reader->GetOutput();
43}
44
45void writeGrid(vtkUnstructuredGrid* grid, std::string const& output_filename)
46{
47 vtkNew<vtkXMLUnstructuredGridWriter> writer;
48 writer->SetFileName(output_filename.c_str());
49 writer->SetInputData(grid);
50 writer->Write();
51}
52
53Eigen::MatrixX3d readJSON(TCLAP::ValueArg<std::string> const& input_filename)
54{
55 using json = nlohmann::json;
56 std::ifstream f(input_filename.getValue());
57 if (!f)
58 {
59 OGS_FATAL("Could not open file '{:s}'", input_filename.getValue());
60 }
61 json const data = json::parse(f);
62
63 auto const number_of_anchors = data["anchor_start_points"].size();
64 if (number_of_anchors == 0)
65 {
66 OGS_FATAL("No anchors found in the json.");
67 }
68 if (number_of_anchors != data["anchor_end_points"].size())
69 {
71 "Number of anchor start points does not match the number of end "
72 "points");
73 }
74
75 Eigen::MatrixX3d realcoords(2 * number_of_anchors, 3);
76
77 auto get_coordinates = [&data](const char* key, std::size_t const i)
78 {
79 const auto& v = data[key][i].get_ref<json::array_t const&>();
80 if (v.size() != 3)
81 {
83 "Expected a vector of length three for {:s} {}. Got vector of "
84 "size {}.",
85 key, i, v.size());
86 }
87 return Eigen::RowVector3d{v[0].get<double>(), v[1].get<double>(),
88 v[2].get<double>()};
89 };
90
91 for (std::size_t i = 0; i < number_of_anchors; i++)
92 {
93 realcoords.row(2 * i).noalias() =
94 get_coordinates("anchor_start_points", i);
95 realcoords.row(2 * i + 1).noalias() =
96 get_coordinates("anchor_end_points", i);
97 }
98
99 return realcoords;
100}
101
102int main(int argc, char** argv)
103{
104 // parse cmdline -----------------------------------------------------------
105
106 TCLAP::CmdLine cmd(
107 "Computes natual coordinates from given real coordinates\n\n"
108
109 "OpenGeoSys-6 software, version " +
111 ".\n"
112 "Copyright (c) 2012-2025, OpenGeoSys Community "
113 "(http://www.opengeosys.org)",
115
116 TCLAP::ValueArg<std::string> log_level_arg(
117 "l", "log-level",
118 "the verbosity of logging messages: none, error, warn, info, debug, "
119 "all",
120 false,
121#ifdef NDEBUG
122 "info",
123#else
124 "all",
125#endif
126 "LOG_LEVEL", cmd);
127
128 TCLAP::ValueArg<std::string> input_filename_arg(
129 "i", "input", "Input bulk mesh", true, "", "VTU_FILE", cmd);
130
131 TCLAP::ValueArg<std::string> output_filename_arg(
132 "o", "output", "Anchor mesh", true, "", "VTU_FILE", cmd);
133
134 TCLAP::ValueArg<std::string> json_filename_arg(
135 "f", "json-file", "JSON file containing anchor start and end points",
136 true, "", "JSON_FILE", cmd);
137
138 TCLAP::ValueArg<double> tolerance_arg(
139 "", "tolerance", "Tolerance/Search length", false,
140 std::numeric_limits<double>::epsilon(), "float", cmd);
141
142 TCLAP::ValueArg<unsigned> max_iter_arg(
143 "", "max-iter",
144 "maximum number of iterations of the internal root-finding algorithm",
145 false, 5, "int", cmd);
146
147 cmd.parse(argc, argv);
148
149 BaseLib::setConsoleLogLevel(log_level_arg.getValue());
150 spdlog::set_pattern("%^%l:%$ %v");
151 spdlog::set_error_handler(
152 [](const std::string& msg)
153 {
154 std::cerr << "spdlog error: " << msg << std::endl;
155 OGS_FATAL("spdlog logger error occurred.");
156 });
157
158 auto const realcoords = readJSON(json_filename_arg);
159 auto const bulk_mesh = readGrid(input_filename_arg.getValue());
160
161 // Compute natural coordinates
162 namespace AU = ApplicationUtils;
163 AU::ComputeNaturalCoordsResult const result = AU::computeNaturalCoords(
164 bulk_mesh, realcoords, tolerance_arg.getValue(),
165 max_iter_arg.getValue());
166 // Write output
167 auto const output_mesh = AU::toVTKGrid(result);
168 writeGrid(output_mesh, output_filename_arg.getValue());
169
170 if (!result.success)
171 {
172 OGS_FATAL("Failed to compute natural coordinates.");
173 }
174
175 return EXIT_SUCCESS;
176}
int main(int argc, char **argv)
Eigen::MatrixX3d readJSON(TCLAP::ValueArg< std::string > const &input_filename)
vtkSmartPointer< vtkUnstructuredGrid > readGrid(std::string const &input_filename)
void writeGrid(vtkUnstructuredGrid *grid, std::string const &output_filename)
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
Git information.
void setConsoleLogLevel(std::string const &level_string)
Definition Logging.cpp:37
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:50
GITINFOLIB_EXPORT const std::string ogs_version