OGS
SWMMConverter.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
13#include "BaseLib/FileTools.h"
14#include "BaseLib/Logging.h"
15#include "BaseLib/MPI.h"
16#include "BaseLib/StringTools.h"
18#include "GeoLib/GEOObjects.h"
20#include "InfoLib/GitInfo.h"
22#include "MeshLib/Mesh.h"
23#include "MeshLib/Properties.h"
24
25int writeGeoOutput(std::string input_file, std::string output_file)
26{
27 GeoLib::GEOObjects geo_objects;
29 geo_objects, true))
30 return -1;
31
32 GeoLib::IO::BoostXmlGmlInterface xml(geo_objects);
35 return 0;
36}
37
39 MeshLib::Mesh& mesh,
40 FileIO::SwmmObject const type,
41 std::size_t const timestep)
42{
43 std::size_t const n_node_params(swmm.getNumberOfParameters(type));
44 for (std::size_t j = 0; j < n_node_params; ++j)
45 {
46 std::string const vec_name(swmm.getArrayName(type, j));
47 if (vec_name.empty())
48 return -2;
49 std::vector<double> data_vec =
50 swmm.getArrayAtTimeStep(type, timestep, j);
51 if (!swmm.addResultsToMesh(mesh, type, vec_name, data_vec))
52 return -3;
53 }
54 return 0;
55}
56
57int writeMeshOutput(std::string const& input_file,
58 std::string const& output_file,
59 bool const node_args,
60 bool const link_args)
61{
62 std::unique_ptr<FileIO::SwmmInterface> swmm =
64 if (swmm == nullptr)
65 return -1;
66
67 MeshLib::Mesh& mesh = swmm->getMesh();
68
69 bool const no_output_file = !swmm->existsSwmmOutputFile();
70 if (!(node_args || link_args) || no_output_file)
71 {
72 if (no_output_file)
73 INFO("No output file found.");
74 MeshLib::IO::VtuInterface vtkIO(&mesh, 0, false);
75 vtkIO.writeToFile(output_file);
76 return 0;
77 }
78
79 std::string const basename = BaseLib::dropFileExtension(output_file);
80 std::string const extension = BaseLib::getFileExtension(output_file);
81 std::size_t const n_time_steps(swmm->getNumberOfTimeSteps());
82 INFO("Number of simulation time steps: {:d}", n_time_steps);
83 for (std::size_t i = 0; i < n_time_steps; i++)
84 {
85 if (node_args)
87
88 if (link_args)
90
91 MeshLib::IO::VtuInterface vtkio(&mesh, 0, false);
92 std::string name(basename + std::to_string(i) + extension);
93 vtkio.writeToFile(name);
94 }
95 return 0;
96}
97
99 FileIO::SwmmObject const type,
100 std::string const& base,
101 std::string const& ext)
102{
103 std::size_t n_objects = swmm.getNumberOfObjects(type);
104 std::string const& type_str(swmm.swmmObjectTypeToString(type));
105 for (std::size_t i = 0; i < n_objects; ++i)
106 {
107 std::string const obj_name = swmm.getName(type, i);
108 std::string const obj_file_name =
109 std::string(base + "_" + type_str + "_" + obj_name + ext);
110 swmm.writeCsvForObject(obj_file_name, type, i);
111 }
112}
113
114int writeCsvOutput(std::string input_file,
115 std::string output_file,
116 bool const node_args,
117 bool const link_args,
118 bool const catchment_args,
119 bool const system_args)
120{
121 std::unique_ptr<FileIO::SwmmInterface> swmm =
123 if (swmm == nullptr)
124 return -1;
125
126 if (!swmm->existsSwmmOutputFile())
127 {
128 INFO("No output file found, skipping data conversion to CSV.");
129 return -1;
130 }
131
132 if (!(node_args || link_args || catchment_args || system_args))
133 {
134 INFO("No data category selected. Nothing to write.");
135 return 0;
136 }
137
138 std::string const basename = BaseLib::dropFileExtension(output_file);
139 std::string const extension = BaseLib::getFileExtension(output_file);
140
141 if (node_args)
143 extension);
144
145 if (link_args)
147 extension);
148
149 if (catchment_args)
151 basename, extension);
152
153 if (system_args)
154 {
155 std::string const obj_file_name =
156 std::string(basename + "_system" + extension);
157 swmm->writeCsvForObject(obj_file_name, FileIO::SwmmObject::SYSTEM, 0);
158 }
159 return 0;
160}
161
162int main(int argc, char* argv[])
163{
164 TCLAP::CmdLine cmd(
165 "Read files for the Storm Water Management Model (SWMM) and converts "
166 "them into OGS data structures.\n\n"
167 "OpenGeoSys-6 software, version " +
169 ".\n"
170 "Copyright (c) 2012-2025, OpenGeoSys Community "
171 "(http://www.opengeosys.org)",
173 TCLAP::ValueArg<std::string> mesh_output_arg(
174 "m", "mesh", "Output (.vtu). Mesh output file ", false, "",
175 "OUTPUT_FILE");
176 cmd.add(mesh_output_arg);
177 TCLAP::ValueArg<std::string> geo_output_arg(
178 "g", "geo", "Output (.gml). Geometry output file ", false, "",
179 "OUTPUT_FILE");
180 cmd.add(geo_output_arg);
181 TCLAP::ValueArg<std::string> csv_output_arg(
182 "c", "csv", "Output (.csv). csv output file", false, "", "OUTPUT_FILE");
183 cmd.add(csv_output_arg);
184 auto log_level_arg = BaseLib::makeLogLevelArg();
185 cmd.add(log_level_arg);
186 TCLAP::ValueArg<std::string> swmm_input_arg(
187 "i", "input", "Input (.inp). SWMM input file", true, "", "INPUT_FILE");
188 cmd.add(swmm_input_arg);
189 TCLAP::SwitchArg add_nodes_arg(
190 "", "node_vars", "Read node variables and add to output mesh");
191 cmd.add(add_nodes_arg);
192 TCLAP::SwitchArg add_links_arg(
193 "", "link_vars", "Read link variables and add to output mesh");
194 cmd.add(add_links_arg);
195 TCLAP::SwitchArg add_subcatchments_arg(
196 "", "subcatchment_vars",
197 "Read subcatchment variables and write to CSV-file");
198 cmd.add(add_subcatchments_arg);
199 TCLAP::SwitchArg add_system_arg(
200 "", "system_vars", "Read system variables and write to CSV-file");
201 cmd.add(add_system_arg);
202 cmd.parse(argc, argv);
203
204 BaseLib::MPI::Setup mpi_setup(argc, argv);
205 BaseLib::initOGSLogger(log_level_arg.getValue());
206
207 if (!(geo_output_arg.isSet() || mesh_output_arg.isSet() ||
208 csv_output_arg.isSet()))
209 {
210 ERR("No output format given. Please specify OGS geometry or mesh "
211 "output file.");
212 return -1;
213 }
214
215 if ((add_subcatchments_arg.getValue() || add_system_arg.getValue()) &&
216 !csv_output_arg.isSet())
217 {
218 ERR("Please specify csv output file for exporting subcatchment or "
219 "system parameters.");
220 return -1;
221 }
222
223 if (geo_output_arg.isSet())
224 writeGeoOutput(swmm_input_arg.getValue(), geo_output_arg.getValue());
225
226 if (mesh_output_arg.isSet())
227 writeMeshOutput(swmm_input_arg.getValue(), mesh_output_arg.getValue(),
228 add_nodes_arg.getValue(), add_links_arg.getValue());
229
230 if (csv_output_arg.isSet())
231 writeCsvOutput(swmm_input_arg.getValue(),
232 csv_output_arg.getValue(),
233 add_nodes_arg.getValue(),
234 add_links_arg.getValue(),
235 add_subcatchments_arg.getValue(),
236 add_system_arg.getValue());
237
238 return 0;
239}
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:36
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
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
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
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