OGS
SWMMConverter.cpp File Reference

Detailed Description

Definition in file SWMMConverter.cpp.

#include <tclap/CmdLine.h>
#include <mpi.h>
#include "Applications/FileIO/SWMM/SWMMInterface.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/StringTools.h"
#include "GeoLib/GEOObjects.h"
#include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Properties.h"
Include dependency graph for SWMMConverter.cpp:

Go to the source code of this file.

Functions

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)
 
void writeObjectsOfSwmmTypeToCsv (FileIO::SwmmInterface &swmm, FileIO::SwmmObject const type, std::string const &base, std::string const &ext)
 
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)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ addObjectsToMesh()

int addObjectsToMesh ( FileIO::SwmmInterface & swmm,
MeshLib::Mesh & mesh,
FileIO::SwmmObject const type,
std::size_t const timestep )

Definition at line 39 of file SWMMConverter.cpp.

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}
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)
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.

References FileIO::SwmmInterface::addResultsToMesh(), FileIO::SwmmInterface::getArrayAtTimeStep(), FileIO::SwmmInterface::getArrayName(), and FileIO::SwmmInterface::getNumberOfParameters().

Referenced by writeMeshOutput().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 163 of file SWMMConverter.cpp.

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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
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)
int writeGeoOutput(std::string input_file, std::string output_file)
int writeMeshOutput(std::string const &input_file, std::string const &output_file, bool const node_args, bool const link_args)
GITINFOLIB_EXPORT const std::string ogs_version

References ERR(), GitInfoLib::GitInfo::ogs_version, writeCsvOutput(), writeGeoOutput(), and writeMeshOutput().

◆ writeCsvOutput()

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 )

Definition at line 115 of file SWMMConverter.cpp.

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}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void writeObjectsOfSwmmTypeToCsv(FileIO::SwmmInterface &swmm, FileIO::SwmmObject const type, std::string const &base, std::string const &ext)
static std::unique_ptr< SwmmInterface > create(std::string const &file_name)
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)

References FileIO::SwmmInterface::create(), BaseLib::dropFileExtension(), BaseLib::getFileExtension(), INFO(), FileIO::LINK, FileIO::NODE, FileIO::SUBCATCHMENT, FileIO::SYSTEM, and writeObjectsOfSwmmTypeToCsv().

Referenced by main().

◆ writeGeoOutput()

int writeGeoOutput ( std::string input_file,
std::string output_file )

Definition at line 26 of file SWMMConverter.cpp.

27{
28 GeoLib::GEOObjects geo_objects;
30 geo_objects, true))
31 return -1;
32
33 GeoLib::IO::BoostXmlGmlInterface xml(geo_objects);
34 xml.export_name = BaseLib::extractBaseNameWithoutExtension(input_file);
35 BaseLib::IO::writeStringToFile(xml.writeToString(), output_file);
36 return 0;
37}
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.
Container class for geometric objects.
Definition GEOObjects.h:57
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:45
std::string extractBaseNameWithoutExtension(std::string const &pathname)

References FileIO::SwmmInterface::convertSwmmInputToGeometry(), BaseLib::IO::XMLInterface::export_name, BaseLib::extractBaseNameWithoutExtension(), BaseLib::IO::writeStringToFile(), and BaseLib::IO::Writer::writeToString().

Referenced by main().

◆ writeMeshOutput()

int writeMeshOutput ( std::string const & input_file,
std::string const & output_file,
bool const node_args,
bool const link_args )

Definition at line 58 of file SWMMConverter.cpp.

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}
int addObjectsToMesh(FileIO::SwmmInterface &swmm, MeshLib::Mesh &mesh, FileIO::SwmmObject const type, std::size_t const timestep)
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....

References addObjectsToMesh(), FileIO::SwmmInterface::create(), BaseLib::dropFileExtension(), BaseLib::getFileExtension(), INFO(), FileIO::LINK, FileIO::NODE, and MeshLib::IO::VtuInterface::writeToFile().

Referenced by main().

◆ writeObjectsOfSwmmTypeToCsv()

void writeObjectsOfSwmmTypeToCsv ( FileIO::SwmmInterface & swmm,
FileIO::SwmmObject const type,
std::string const & base,
std::string const & ext )

Definition at line 99 of file SWMMConverter.cpp.

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}
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::string swmmObjectTypeToString(SwmmObject const obj_type)
Returns a string with the name of the object type.

References FileIO::SwmmInterface::getName(), FileIO::SwmmInterface::getNumberOfObjects(), FileIO::SwmmInterface::swmmObjectTypeToString(), and FileIO::SwmmInterface::writeCsvForObject().

Referenced by writeCsvOutput().