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

39 {
40  std::size_t const n_node_params(swmm.getNumberOfParameters(type));
41  for (std::size_t j = 0; j < n_node_params; ++j)
42  {
43  std::string const vec_name(swmm.getArrayName(type, j));
44  if (vec_name.empty())
45  return -2;
46  std::vector<double> data_vec =
47  swmm.getArrayAtTimeStep(type, timestep, j);
48  if (!swmm.addResultsToMesh(mesh, type, vec_name, data_vec))
49  return -3;
50  }
51  return 0;
52 }
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 159 of file SWMMConverter.cpp.

160 {
161  TCLAP::CmdLine cmd(
162  "Read files for the Storm Water Management Model (SWMM) and converts "
163  "them into OGS data structures.\n\n"
164  "OpenGeoSys-6 software, version " +
166  ".\n"
167  "Copyright (c) 2012-2021, OpenGeoSys Community "
168  "(http://www.opengeosys.org)",
170  TCLAP::ValueArg<std::string> mesh_output_arg(
171  "m", "mesh", "mesh output file (*.vtu)", false, "", "mesh output file");
172  cmd.add(mesh_output_arg);
173  TCLAP::ValueArg<std::string> geo_output_arg(
174  "g", "geo", "geometry output file (*.gml)", false, "",
175  "geometry output file");
176  cmd.add(geo_output_arg);
177  TCLAP::ValueArg<std::string> csv_output_arg(
178  "c", "csv", "csv output file (*.csv)", false, "", "CSV output file");
179  cmd.add(csv_output_arg);
180  TCLAP::ValueArg<std::string> swmm_input_arg(
181  "i", "input", "SWMM input file (*.inp)", 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  if (!(geo_output_arg.isSet() || mesh_output_arg.isSet() ||
199  csv_output_arg.isSet()))
200  {
201  ERR("No output format given. Please specify OGS geometry or mesh "
202  "output file.");
203  return -1;
204  }
205 
206  if ((add_subcatchments_arg.getValue() || add_system_arg.getValue()) &&
207  !csv_output_arg.isSet())
208  {
209  ERR("Please specify csv output file for exporting subcatchment or "
210  "system parameters.");
211  return -1;
212  }
213 
214  if (geo_output_arg.isSet())
215  writeGeoOutput(swmm_input_arg.getValue(), geo_output_arg.getValue());
216 
217  if (mesh_output_arg.isSet())
218  writeMeshOutput(swmm_input_arg.getValue(), mesh_output_arg.getValue(),
219  add_nodes_arg.getValue(), add_links_arg.getValue());
220 
221  if (csv_output_arg.isSet())
222  writeCsvOutput(swmm_input_arg.getValue(),
223  csv_output_arg.getValue(),
224  add_nodes_arg.getValue(),
225  add_links_arg.getValue(),
226  add_subcatchments_arg.getValue(),
227  add_system_arg.getValue());
228 
229  return 0;
230 }
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
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 111 of file SWMMConverter.cpp.

117 {
118  std::unique_ptr<FileIO::SwmmInterface> swmm =
119  FileIO::SwmmInterface::create(input_file);
120  if (swmm == nullptr)
121  return -1;
122 
123  if (!swmm->existsSwmmOutputFile())
124  {
125  INFO("No output file found, skipping data conversion to CSV.");
126  return -1;
127  }
128 
129  if (!(node_args || link_args || catchment_args || system_args))
130  {
131  INFO("No data category selected. Nothing to write.");
132  return 0;
133  }
134 
135  std::string const basename = BaseLib::dropFileExtension(output_file);
136  std::string const extension = BaseLib::getFileExtension(output_file);
137 
138  if (node_args)
140  extension);
141 
142  if (link_args)
144  extension);
145 
146  if (catchment_args)
148  basename, extension);
149 
150  if (system_args)
151  {
152  std::string const obj_file_name =
153  std::string(basename + "_system" + extension);
154  swmm->writeCsvForObject(obj_file_name, FileIO::SwmmObject::SYSTEM, 0);
155  }
156  return 0;
157 }
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
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)
Definition: FileTools.cpp:186
std::string dropFileExtension(std::string const &filename)
Definition: FileTools.cpp:169

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

23 {
24  GeoLib::GEOObjects geo_objects;
26  geo_objects, true))
27  return -1;
28 
29  GeoLib::IO::BoostXmlGmlInterface xml(geo_objects);
30  xml.export_name = BaseLib::extractBaseNameWithoutExtension(input_file);
31  BaseLib::IO::writeStringToFile(xml.writeToString(), output_file);
32  return 0;
33 }
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:61
int writeStringToFile(std::string content, std::filesystem::path const &file_path)
Definition: Writer.cpp:45
std::string extractBaseNameWithoutExtension(std::string const &pathname)
Definition: FileTools.cpp:180

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

58 {
59  std::unique_ptr<FileIO::SwmmInterface> swmm =
61  if (swmm == nullptr)
62  return -1;
63 
64  MeshLib::Mesh& mesh = swmm->getMesh();
65 
66  bool const no_output_file = !swmm->existsSwmmOutputFile();
67  if (!(node_args || link_args) || no_output_file)
68  {
69  if (no_output_file)
70  INFO("No output file found.");
71  MeshLib::IO::VtuInterface vtkIO(&mesh, 0, false);
72  vtkIO.writeToFile(output_file);
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  MeshLib::IO::VtuInterface vtkio(&mesh, 0, false);
89  std::string name(basename + std::to_string(i) + extension);
90  vtkio.writeToFile(name);
91  }
92  return 0;
93 }
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....
Definition: VtuInterface.h:38

References addObjectsToMesh(), FileIO::SwmmInterface::create(), BaseLib::dropFileExtension(), BaseLib::getFileExtension(), INFO(), FileIO::LINK, MaterialPropertyLib::name, 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 95 of file SWMMConverter.cpp.

99 {
100  std::size_t n_objects = swmm.getNumberOfObjects(type);
101  std::string const& type_str(swmm.swmmObjectTypeToString(type));
102  for (std::size_t i = 0; i < n_objects; ++i)
103  {
104  std::string const obj_name = swmm.getName(type, i);
105  std::string const obj_file_name =
106  std::string(base + "_" + type_str + "_" + obj_name + ext);
107  swmm.writeCsvForObject(obj_file_name, type, i);
108  }
109 }
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().