OGS
SWMMConverter.cpp File Reference
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 32 of file SWMMConverter.cpp.

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}
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-2026, 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: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)
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:56
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:28
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 19 of file SWMMConverter.cpp.

20{
21 GeoLib::GEOObjects geo_objects;
23 geo_objects, true))
24 return -1;
25
26 GeoLib::IO::BoostXmlGmlInterface xml(geo_objects);
27 xml.export_name = BaseLib::extractBaseNameWithoutExtension(input_file);
28 BaseLib::IO::writeStringToFile(xml.writeToString(), output_file);
29 return 0;
30}
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:46
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:34
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 51 of file SWMMConverter.cpp.

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 if (MeshLib::IO::writeMeshToFile(mesh, output_file, {}, false,
69 vtkXMLWriter::Ascii) != 0)
70 {
71 return -1;
72 }
73 return 0;
74 }
75
76 std::string const basename = BaseLib::dropFileExtension(output_file);
77 std::string const extension = BaseLib::getFileExtension(output_file);
78 std::size_t const n_time_steps(swmm->getNumberOfTimeSteps());
79 INFO("Number of simulation time steps: {:d}", n_time_steps);
80 for (std::size_t i = 0; i < n_time_steps; i++)
81 {
82 if (node_args)
84
85 if (link_args)
87
88 std::string name(basename + std::to_string(i) + extension);
89 if (MeshLib::IO::writeMeshToFile(mesh, name, {}, false,
90 vtkXMLWriter::Ascii) != 0)
91 {
92 return -1;
93 }
94 }
95 return 0;
96}
int addObjectsToMesh(FileIO::SwmmInterface &swmm, MeshLib::Mesh &mesh, FileIO::SwmmObject const type, std::size_t const timestep)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > output_variable_names, bool const use_compression, int const data_mode)

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

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