10#include <tclap/CmdLine.h>
29 std::string
const& val_name,
33 std::size_t start(line.find(val_name));
34 std::size_t end(std::string::npos);
37 ERR(
"Value not found.");
40 value = line.substr(start + 1, std::string::npos);
43 start = value.find(
"\"");
44 value = value.substr(start + 1, std::string::npos);
45 end = value.find(
"\"");
49 start = value.find_first_not_of(
" ");
50 value = value.substr(start + 1, std::string::npos);
52 end = value.find_first_of(
" ");
54 value = value.substr(0, end);
60std::string
getName(std::string
const& line)
67 auto start = line.find(
"ZONETYPE=");
68 if (start == std::string::npos)
71 "A required 'ZONETYPE=' substring is not available in the ZONE "
72 "description: '{:s}'.",
75 start += std::size(
"ZONETYPE=") - 1;
77 auto end = line.find(
',', start);
78 if (end == std::string::npos)
81 "Expected the 'ZONETYPE=type' to be followed by a comma in the "
82 "ZONE description '{:s}'. The zone type starts at position {:d}.",
87 ERR(
"ZONETYPE string is empty in ZONE description '{:s}'.", line);
89 return std::string_view(&line[start], end - start);
93std::pair<std::size_t, std::size_t>
getDimensions(std::string
const& line)
95 std::pair<std::size_t, std::size_t> dims;
96 std::stringstream start(
getValue(line,
"I=",
false));
98 std::stringstream end(
getValue(line,
"J=",
false));
106 std::size_t
const start = var.find_first_not_of(
"\"");
107 var = var.substr(start, std::string::npos);
108 std::size_t
const end = var.find_first_of(
"\"");
109 return var.substr(0, end);
115 std::string
const var_str(
"VARIABLES");
116 std::size_t start(line.find(var_str));
117 std::string all_vars =
118 line.substr(start + var_str.length(), std::string::npos);
119 start = all_vars.find(
"=");
120 all_vars = all_vars.substr(start + 1, std::string::npos);
123 std::vector<std::string> variables;
125 while (end != std::string::npos)
127 end = all_vars.find_first_of(
" ");
128 std::string var = all_vars.substr(0, end);
130 all_vars = all_vars.substr(end + 1, std::string::npos);
139 std::size_t
const& current,
140 std::size_t
const& total)
142 if (current != total)
144 ERR(
"Data rows found do not fit specified dimensions for section "
155 std::string
const& name,
156 std::size_t
const& current,
157 std::size_t
const& total)
169 std::vector<std::vector<double>>& scalars,
170 std::size_t& val_count)
173 scalars.reserve(n_scalars);
174 for (std::size_t i = 0; i < n_scalars; ++i)
176 scalars.emplace_back(0);
183 std::string
const& file_name,
184 std::size_t& write_count,
185 std::size_t& val_count,
186 std::size_t& val_total)
188 if (write_count == 0 || val_total != 0)
190 std::size_t
const delim_pos(file_name.find_last_of(
"."));
191 std::string
const base_name(file_name.substr(0, delim_pos + 1));
192 std::string
const extension(
193 file_name.substr(delim_pos, std::string::npos));
197 INFO(
"Writing section #{}", write_count);
199 out.open(base_name + std::to_string(write_count++) + extension);
205 std::size_t& write_count,
206 std::vector<std::string>
const& vec_names,
207 std::vector<std::vector<double>>
const& scalars,
208 std::pair<std::size_t, std::size_t>
const& dims)
211 for (std::size_t i = 0; i < vec_names.size(); ++i)
213 if (vec_names[i] ==
"x" || vec_names[i] ==
"X")
215 cellsize = scalars[i][1] - scalars[i][0];
222 ERR(
"Cell size not found. Aborting...");
228 origin, cellsize, -9999};
230 std::unique_ptr<MeshLib::Mesh> mesh(
237 for (std::size_t i = 1; i < vec_names.size(); ++i)
243 ERR(
"Error creating array '{:s}'.", vec_names[i]);
246 prop->reserve(scalars[i].size());
247 std::copy(scalars[i].cbegin(), scalars[i].cend(),
248 std::back_inserter(*prop));
251 std::size_t
const delim_pos(file_name.find_last_of(
"."));
252 std::string
const base_name(file_name.substr(0, delim_pos + 1));
253 std::string
const extension(file_name.substr(delim_pos, std::string::npos));
255 INFO(
"Writing section #{}", write_count);
257 vtu.
writeToFile(base_name + std::to_string(write_count++) + extension);
264 while (std::getline(in, line))
266 if ((line.find(
"TITLE") != std::string::npos) ||
267 (line.find(
"VARIABLES") != std::string::npos) ||
268 (line.find(
"ZONE") != std::string::npos))
276int splitFile(std::ifstream& in, std::string
const& file_name)
281 std::size_t val_count(0);
282 std::size_t val_total(0);
283 std::size_t write_count(0);
284 while (std::getline(in, line))
286 if (line.find(
"TITLE") != std::string::npos)
297 if (line.find(
"VARIABLES") != std::string::npos)
308 if (line.find(
"ZONE") != std::string::npos)
318 std::pair<std::size_t, std::size_t> dims =
getDimensions(line);
319 val_total = dims.first * dims.second;
331 INFO(
"Writing time step #{}", write_count);
333 INFO(
"Finished split.");
343 std::pair<std::size_t, std::size_t> dims(0, 0);
344 std::vector<std::string> var_names;
345 std::vector<std::vector<double>> scalars;
346 std::size_t val_count(0);
347 std::size_t val_total(0);
348 std::size_t write_count(0);
349 while (std::getline(in, line))
351 if (line.find(
"GEOMETRY") != std::string::npos)
360 if (line.find(
"TITLE") != std::string::npos)
374 if (line.find(
"VARIABLES") != std::string::npos)
390 if (line.find(
"ZONE") != std::string::npos)
403 if (
auto const zonetype =
getZonetype(line); zonetype !=
"ORDERED")
405 ERR(
"Given zonetype '{:s}' is not supported. Only 'ORDERED' "
406 "zonetype data can be converted.",
411 val_total = dims.first * dims.second;
417 std::stringstream iss(line);
419 std::size_t
const n_scalars(scalars.size());
422 if (i > n_scalars - 1)
424 ERR(
"Too much data for existing scalar arrays");
427 scalars[i++].push_back(x);
431 ERR(
"Not enough data for existing scalar arrays");
441 INFO(
"Finished conversion.");
456int main(
int argc,
char* argv[])
460 "OpenGeoSys-6 software, version " +
463 "Copyright (c) 2012-2024, OpenGeoSys Community "
464 "(http://www.opengeosys.org)",
466 TCLAP::SwitchArg split_arg(
"s",
"split",
467 "split time steps into separate files");
469 TCLAP::SwitchArg convert_arg(
"c",
"convert",
470 "convert TecPlot data into OGS meshes");
471 cmd.add(convert_arg);
472 TCLAP::ValueArg<std::string> output_arg(
473 "o",
"output-file",
"output mesh file",
false,
"",
"string");
475 TCLAP::ValueArg<std::string> input_arg(
476 "i",
"input-file",
"TecPlot input file",
true,
"",
"string");
478 cmd.parse(argc, argv);
482 if (!input_arg.isSet())
484 ERR(
"No input file given. Please specify TecPlot (*.plt) file");
488 if (convert_arg.getValue() && !output_arg.isSet())
490 ERR(
"No output file given. Please specify OGS mesh (*.vtu) file");
494 std::ifstream in(input_arg.getValue().c_str());
497 ERR(
"Could not open file {:s}.", input_arg.getValue());
501 if (!convert_arg.isSet() && !split_arg.isSet())
503 INFO(
"Nothing to do. Use -s to split or -c to convert.");
507 std::string
const filename =
508 (output_arg.isSet()) ? output_arg.getValue() : input_arg.getValue();
510 if (split_arg.getValue())
514 else if (convert_arg.getValue())
Definition of the Point class.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition of the Mesh class.
Implementation of the VtuInterface class.
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
void trim(std::string &str, char ch)
GITINFOLIB_EXPORT const std::string ogs_version