OGS
SWMMConverter.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#ifdef USE_PETSC
13#include <mpi.h>
14#endif
15
17#include "BaseLib/FileTools.h"
18#include "BaseLib/StringTools.h"
19#include "GeoLib/GEOObjects.h"
21#include "InfoLib/GitInfo.h"
23#include "MeshLib/Mesh.h"
24#include "MeshLib/Properties.h"
25
26int writeGeoOutput(std::string input_file, std::string output_file)
27{
28 GeoLib::GEOObjects geo_objects;
30 geo_objects, true))
31 return -1;
32
33 GeoLib::IO::BoostXmlGmlInterface xml(geo_objects);
36 return 0;
37}
38
40 MeshLib::Mesh& mesh,
41 FileIO::SwmmObject const type,
42 std::size_t const timestep)
43{
44 std::size_t const n_node_params(swmm.getNumberOfParameters(type));
45 for (std::size_t j = 0; j < n_node_params; ++j)
46 {
47 std::string const vec_name(swmm.getArrayName(type, j));
48 if (vec_name.empty())
49 return -2;
50 std::vector<double> data_vec =
51 swmm.getArrayAtTimeStep(type, timestep, j);
52 if (!swmm.addResultsToMesh(mesh, type, vec_name, data_vec))
53 return -3;
54 }
55 return 0;
56}
57
58int writeMeshOutput(std::string const& input_file,
59 std::string const& output_file,
60 bool const node_args,
61 bool const link_args)
62{
63 std::unique_ptr<FileIO::SwmmInterface> swmm =
65 if (swmm == nullptr)
66 return -1;
67
68 MeshLib::Mesh& mesh = swmm->getMesh();
69
70 bool const no_output_file = !swmm->existsSwmmOutputFile();
71 if (!(node_args || link_args) || no_output_file)
72 {
73 if (no_output_file)
74 INFO("No output file found.");
75 MeshLib::IO::VtuInterface vtkIO(&mesh, 0, false);
76 vtkIO.writeToFile(output_file);
77 return 0;
78 }
79
80 std::string const basename = BaseLib::dropFileExtension(output_file);
81 std::string const extension = BaseLib::getFileExtension(output_file);
82 std::size_t const n_time_steps(swmm->getNumberOfTimeSteps());
83 INFO("Number of simulation time steps: {:d}", n_time_steps);
84 for (std::size_t i = 0; i < n_time_steps; i++)
85 {
86 if (node_args)
88
89 if (link_args)
91
92 MeshLib::IO::VtuInterface vtkio(&mesh, 0, false);
93 std::string name(basename + std::to_string(i) + extension);
94 vtkio.writeToFile(name);
95 }
96 return 0;
97}
98
100 FileIO::SwmmObject const type,
101 std::string const& base,
102 std::string const& ext)
103{
104 std::size_t n_objects = swmm.getNumberOfObjects(type);
105 std::string const& type_str(swmm.swmmObjectTypeToString(type));
106 for (std::size_t i = 0; i < n_objects; ++i)
107 {
108 std::string const obj_name = swmm.getName(type, i);
109 std::string const obj_file_name =
110 std::string(base + "_" + type_str + "_" + obj_name + ext);
111 swmm.writeCsvForObject(obj_file_name, type, i);
112 }
113}
114
115int writeCsvOutput(std::string input_file,
116 std::string output_file,
117 bool const node_args,
118 bool const link_args,
119 bool const catchment_args,
120 bool const system_args)
121{
122 std::unique_ptr<FileIO::SwmmInterface> swmm =
124 if (swmm == nullptr)
125 return -1;
126
127 if (!swmm->existsSwmmOutputFile())
128 {
129 INFO("No output file found, skipping data conversion to CSV.");
130 return -1;
131 }
132
133 if (!(node_args || link_args || catchment_args || system_args))
134 {
135 INFO("No data category selected. Nothing to write.");
136 return 0;
137 }
138
139 std::string const basename = BaseLib::dropFileExtension(output_file);
140 std::string const extension = BaseLib::getFileExtension(output_file);
141
142 if (node_args)
144 extension);
145
146 if (link_args)
148 extension);
149
150 if (catchment_args)
152 basename, extension);
153
154 if (system_args)
155 {
156 std::string const obj_file_name =
157 std::string(basename + "_system" + extension);
158 swmm->writeCsvForObject(obj_file_name, FileIO::SwmmObject::SYSTEM, 0);
159 }
160 return 0;
161}
162
163int main(int argc, char* argv[])
164{
165 TCLAP::CmdLine cmd(
166 "Read files for the Storm Water Management Model (SWMM) and converts "
167 "them into OGS data structures.\n\n"
168 "OpenGeoSys-6 software, version " +
170 ".\n"
171 "Copyright (c) 2012-2024, OpenGeoSys Community "
172 "(http://www.opengeosys.org)",
174 TCLAP::ValueArg<std::string> mesh_output_arg(
175 "m", "mesh", "mesh output file (*.vtu)", false, "", "mesh output file");
176 cmd.add(mesh_output_arg);
177 TCLAP::ValueArg<std::string> geo_output_arg(
178 "g", "geo", "geometry output file (*.gml)", false, "",
179 "geometry output file");
180 cmd.add(geo_output_arg);
181 TCLAP::ValueArg<std::string> csv_output_arg(
182 "c", "csv", "csv output file (*.csv)", false, "", "CSV output file");
183 cmd.add(csv_output_arg);
184 TCLAP::ValueArg<std::string> swmm_input_arg(
185 "i", "input", "SWMM input file (*.inp)", true, "", "input file");
186 cmd.add(swmm_input_arg);
187 TCLAP::SwitchArg add_nodes_arg(
188 "", "node_vars", "Read node variables and add to output mesh");
189 cmd.add(add_nodes_arg);
190 TCLAP::SwitchArg add_links_arg(
191 "", "link_vars", "Read link variables and add to output mesh");
192 cmd.add(add_links_arg);
193 TCLAP::SwitchArg add_subcatchments_arg(
194 "", "subcatchment_vars",
195 "Read subcatchment variables and write to CSV-file");
196 cmd.add(add_subcatchments_arg);
197 TCLAP::SwitchArg add_system_arg(
198 "", "system_vars", "Read system variables and write to CSV-file");
199 cmd.add(add_system_arg);
200 cmd.parse(argc, argv);
201
202#ifdef USE_PETSC
203 MPI_Init(&argc, &argv);
204#endif
205
206 if (!(geo_output_arg.isSet() || mesh_output_arg.isSet() ||
207 csv_output_arg.isSet()))
208 {
209 ERR("No output format given. Please specify OGS geometry or mesh "
210 "output file.");
211#ifdef USE_PETSC
212 MPI_Finalize();
213#endif
214 return -1;
215 }
216
217 if ((add_subcatchments_arg.getValue() || add_system_arg.getValue()) &&
218 !csv_output_arg.isSet())
219 {
220 ERR("Please specify csv output file for exporting subcatchment or "
221 "system parameters.");
222#ifdef USE_PETSC
223 MPI_Finalize();
224#endif
225 return -1;
226 }
227
228 if (geo_output_arg.isSet())
229 writeGeoOutput(swmm_input_arg.getValue(), geo_output_arg.getValue());
230
231 if (mesh_output_arg.isSet())
232 writeMeshOutput(swmm_input_arg.getValue(), mesh_output_arg.getValue(),
233 add_nodes_arg.getValue(), add_links_arg.getValue());
234
235 if (csv_output_arg.isSet())
236 writeCsvOutput(swmm_input_arg.getValue(),
237 csv_output_arg.getValue(),
238 add_nodes_arg.getValue(),
239 add_links_arg.getValue(),
240 add_subcatchments_arg.getValue(),
241 add_system_arg.getValue());
242
243#ifdef USE_PETSC
244 MPI_Finalize();
245#endif
246 return 0;
247}
Definition of the BoostXmlGmlInterface class.
Filename manipulation routines.
Definition of the GEOObjects class.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the class Properties that implements a container of properties.
Definition of the Mesh class.
int writeCsvOutput(std::string input_file, std::string output_file, bool const node_args, bool const link_args, bool const catchment_args, bool const system_args)
void writeObjectsOfSwmmTypeToCsv(FileIO::SwmmInterface &swmm, FileIO::SwmmObject const type, std::string const &base, std::string const &ext)
int main(int argc, char *argv[])
int writeGeoOutput(std::string input_file, std::string output_file)
int addObjectsToMesh(FileIO::SwmmInterface &swmm, MeshLib::Mesh &mesh, FileIO::SwmmObject const type, std::size_t const timestep)
int writeMeshOutput(std::string const &input_file, std::string const &output_file, bool const node_args, bool const link_args)
Definition of string helper functions.
Implementation of the VtuInterface class.
std::string writeToString()
Writes the object to a string.
Definition Writer.cpp:31
std::size_t getNumberOfObjects(SwmmObject obj_type) const
Returns the number of objects of the given type.
bool writeCsvForObject(std::string const &file_name, SwmmObject obj_type, std::size_t obj_idx) const
Write a CSV file for one object of the given type for all time steps.
std::string getName(SwmmObject obj_type, std::size_t idx) const
Returns the Name for the indexed object of the given type (or an empty string if an error occurred).
static std::unique_ptr< SwmmInterface > create(std::string const &file_name)
std::vector< double > getArrayAtTimeStep(SwmmObject obj_type, std::size_t time_step, std::size_t var_idx) const
Returns an array for a given variable at all nodes/links from a SWMM output file for a given time ste...
static bool addResultsToMesh(MeshLib::Mesh &mesh, SwmmObject const type, std::string const &vec_name, std::vector< double > const &data)
static bool convertSwmmInputToGeometry(std::string const &inp_file_name, GeoLib::GEOObjects &geo_objects, bool add_subcatchments)
Reading a SWMM input file and conversion into OGS geometry.
static std::string swmmObjectTypeToString(SwmmObject const obj_type)
Returns a string with the name of the object type.
std::size_t getNumberOfParameters(SwmmObject obj_type) const
Returns the number of parameters (incl. pollutants) of the given type.
std::string getArrayName(SwmmObject obj_type, std::size_t var_idx) const
Returns the name of the data array for the given object type and parameter index.
Container class for geometric objects.
Definition GEOObjects.h:57
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:45
std::string getFileExtension(const std::string &path)
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::string dropFileExtension(std::string const &filename)
SwmmObject
SWMM object types.
GITINFOLIB_EXPORT const std::string ogs_version