OGS
SWMMConverter.cpp File Reference

Detailed Description

Definition in file SWMMConverter.cpp.

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 38 of file SWMMConverter.cpp.

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}
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 162 of file SWMMConverter.cpp.

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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
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)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
GITINFOLIB_EXPORT const std::string ogs_version

References ERR(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), 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 114 of file SWMMConverter.cpp.

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}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
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 25 of file SWMMConverter.cpp.

26{
27 GeoLib::GEOObjects geo_objects;
29 geo_objects, true))
30 return -1;
31
32 GeoLib::IO::BoostXmlGmlInterface xml(geo_objects);
33 xml.export_name = BaseLib::extractBaseNameWithoutExtension(input_file);
34 BaseLib::IO::writeStringToFile(xml.writeToString(), output_file);
35 return 0;
36}
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 57 of file SWMMConverter.cpp.

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}
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 98 of file SWMMConverter.cpp.

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