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