OGS
SWMMInterface.h
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#pragma once
5
6#include <map>
7#include <memory>
8#include <string>
9#include <vector>
10
11
12#include "GeoLib/Station.h"
13
14namespace GeoLib {
15 class GEOObjects;
16 class Point;
17 class Polyline;
18}
19
20namespace MeshLib {
21 class Mesh;
22 class Node;
23 class Element;
24}
25
26namespace FileIO
27{
28
30enum class SwmmObject
31{
33 NODE = 1,
34 LINK = 2,
36};
37
55class SwmmInterface final
56{
57public:
60 static std::unique_ptr<SwmmInterface> create(std::string const& file_name);
61
64 static bool addResultsToMesh(MeshLib::Mesh &mesh, SwmmObject const type,
65 std::string const& vec_name, std::vector<double> const& data);
66
68 MeshLib::Mesh& getMesh() const { return *_mesh; }
69
71 std::string getArrayName(SwmmObject obj_type, std::size_t var_idx) const;
72
74 std::vector<std::string> getNames(SwmmObject obj_type) const;
75
77 std::string getName(SwmmObject obj_type, std::size_t idx) const;
78
80 std::size_t getNumberOfObjects(SwmmObject obj_type) const;
81
83 std::size_t getNumberOfParameters(SwmmObject obj_type) const;
84
86 std::size_t getNumberOfTimeSteps() const;
87
89 std::vector<double> getArrayAtTimeStep(SwmmObject obj_type, std::size_t time_step, std::size_t var_idx) const;
90
92 std::vector<double> getArrayForObject(SwmmObject obj_type, std::size_t obj_idx, std::size_t var_idx) const;
93
95 bool getNodeCoordinateVectors(std::vector<double> &x, std::vector<double> &y, std::vector<double> &z) const;
96
98 bool getLinkPointIds(std::vector<std::size_t> &inlets, std::vector<std::size_t> &outlets) const;
99
101 bool writeCsvForTimestep(std::string const& file_name, SwmmObject obj_type, std::size_t time_step) const;
102
104 bool writeCsvForObject(std::string const& file_name, SwmmObject obj_type, std::size_t obj_idx) const;
105
107 static bool isSwmmInputFile(std::string const& inp_file_name);
108
110 bool existsSwmmOutputFile() const;
111
113 static std::string swmmObjectTypeToString(SwmmObject const obj_type);
114
116 static bool convertSwmmInputToGeometry(std::string const& inp_file_name,
117 GeoLib::GEOObjects &geo_objects, bool add_subcatchments);
118
121
122private:
124 SwmmInterface(std::string const& swmm_base_name);
125
128
131 template <typename T>
132 static bool readCoordinates(std::ifstream &in, std::vector<T*> &points, std::vector<std::string> &names);
133
135 bool readNodeData(std::ifstream &in, std::vector<MeshLib::Node*> &nodes,
136 std::map<std::string, std::size_t> const& name_id_map,
137 std::vector<double> &max_depth, bool read_max_depth);
138
140 bool readLineElements(std::ifstream &in, std::vector<MeshLib::Element*> &elements,
141 std::vector<MeshLib::Node*> const& nodes, std::map<std::string, std::size_t> const& name_id_map);
142
144 bool readSubcatchments(std::ifstream &in, std::map< std::string, std::size_t> const& name_id_map);
145
147 bool readPollutants(std::ifstream &in);
148
150 std::string getArrayName(SwmmObject obj_type, std::size_t var_idx, std::size_t n_pollutants) const;
151
153 bool addRainGaugeTimeSeriesLocations(std::ifstream &in);
154
156 bool matchSubcatchmentsWithPolygons(std::vector<GeoLib::Polyline*> const& lines, std::vector<std::string> const& names);
157
159 std::vector<std::string> getSubcatchmentNameMap() const;
160
162 static bool addPointElevation(std::ifstream &in,
163 std::vector<GeoLib::Point*> &points, std::map<std::string,
164 std::size_t> const& name_id_map);
165
167 static bool readLinksAsPolylines(std::ifstream &in,
168 std::vector<GeoLib::Polyline*> &lines, std::vector<std::string> &line_names,
169 std::vector<GeoLib::Point*> const& points, std::map<std::string, std::size_t> const& point_names);
170
173 static bool readPolygons(std::ifstream &in, std::vector<GeoLib::Polyline*> &lines,
174 std::vector<std::string> &line_names, std::vector<GeoLib::Point*> &points,
175 std::vector<std::string> &pnt_names);
176
178 static bool isSectionFinished(std::string const& str);
179
181 static bool isCommentLine(std::string const& str);
182
186 {
187 std::string name;
188 std::size_t rain_gauge;
189 std::size_t outlet;
190 double area;
192 };
193
194 //variables
196 std::string const _base_name;
198 std::vector<std::string> _id_nodename_map;
200 std::vector<std::string> _id_linkname_map;
202 std::vector<std::string> _pollutant_names;
204 std::vector<Subcatchment> _subcatchments;
206 std::vector<GeoLib::Point*> _subcatchment_points;
208 std::vector< std::pair<GeoLib::Station, std::string> > _rain_gauges;
210 std::unique_ptr<MeshLib::Mesh> _mesh;
211};
212
213} // namespace FileIO
std::unique_ptr< MeshLib::Mesh > _mesh
Mesh generated from SWMM input (+ optional output data)
std::vector< std::string > _id_nodename_map
Vector storing the names of all nodes/junctions.
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.
bool readPollutants(std::ifstream &in)
Reads pollutant names and parameters.
bool readSubcatchments(std::ifstream &in, std::map< std::string, std::size_t > const &name_id_map)
Reads subcatchment information.
std::vector< std::string > getSubcatchmentNameMap() const
Creates a temporary string vector containing all subcatchment names in correct order.
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 bool isCommentLine(std::string const &str)
Checks if the given line string is a comment line.
std::string const _base_name
All files for a given SWMM simulation have the same base name.
bool getNodeCoordinateVectors(std::vector< double > &x, std::vector< double > &y, std::vector< double > &z) const
Writes the node coordinates into double vectors for writing of CSV files.
bool getLinkPointIds(std::vector< std::size_t > &inlets, std::vector< std::size_t > &outlets) const
Writes the inlet- and outlet IDs for links into vectors for writing of CSV files.
bool matchSubcatchmentsWithPolygons(std::vector< GeoLib::Polyline * > const &lines, std::vector< std::string > const &names)
Matches existing subcatchment names with subsequently read polylines marking the outlines of said sub...
SwmmInterface(std::string const &swmm_base_name)
Constructor.
static bool readPolygons(std::ifstream &in, std::vector< GeoLib::Polyline * > &lines, std::vector< std::string > &line_names, std::vector< GeoLib::Point * > &points, std::vector< std::string > &pnt_names)
bool addRainGaugeTimeSeriesLocations(std::ifstream &in)
Reads the location of external rain gauge time series files.
std::vector< GeoLib::Point * > _subcatchment_points
Separate node vector containing points for defining subcatchment outlines.
static bool isSwmmInputFile(std::string const &inp_file_name)
Checks if file is a SWMM input file.
bool readLineElements(std::ifstream &in, std::vector< MeshLib::Element * > &elements, std::vector< MeshLib::Node * > const &nodes, std::map< std::string, std::size_t > const &name_id_map)
Reads links/conduits and returns them as a vector of OGS line elements.
bool writeCsvForTimestep(std::string const &file_name, SwmmObject obj_type, std::size_t time_step) const
Write a CSV file for all object of the given type at one time step.
std::vector< std::pair< GeoLib::Station, std::string > > _rain_gauges
Vector containing rain gauge information as well the position of external time series files.
static bool readLinksAsPolylines(std::ifstream &in, std::vector< GeoLib::Polyline * > &lines, std::vector< std::string > &line_names, std::vector< GeoLib::Point * > const &points, std::map< std::string, std::size_t > const &point_names)
During geometry conversion, this reads links (conduits/pumps/weirs) and converts them into polylines.
std::vector< std::string > _id_linkname_map
Vector storing the names of all links/conduits.
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...
std::size_t getNumberOfTimeSteps() const
Returns the number of time steps for the simulation results.
static bool readCoordinates(std::ifstream &in, std::vector< T * > &points, std::vector< std::string > &names)
static bool addResultsToMesh(MeshLib::Mesh &mesh, SwmmObject const type, std::string const &vec_name, std::vector< double > const &data)
bool existsSwmmOutputFile() const
Checks if a SWMM output file exists for the current input.
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.
bool readSwmmInputToLineMesh()
Reading a SWMM input file and creating an OGS line mesh. This is automatically called when the object...
std::size_t getNumberOfParameters(SwmmObject obj_type) const
Returns the number of parameters (incl. pollutants) of the given type.
static bool addPointElevation(std::ifstream &in, std::vector< GeoLib::Point * > &points, std::map< std::string, std::size_t > const &name_id_map)
During geometry conversion, this adds elevation values to the existing point vector.
MeshLib::Mesh & getMesh() const
Returns the mesh generated from SWMM file content.
bool readNodeData(std::ifstream &in, std::vector< MeshLib::Node * > &nodes, std::map< std::string, std::size_t > const &name_id_map, std::vector< double > &max_depth, bool read_max_depth)
Reads input information associated with nodes (elevation, depth, etc.)
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.
std::vector< Subcatchment > _subcatchments
Vector storing information about all subcatchments.
static bool isSectionFinished(std::string const &str)
Checks if the given line string is empty. Empty strings mark the end of sections in a SWMM input file...
std::vector< double > getArrayForObject(SwmmObject obj_type, std::size_t obj_idx, std::size_t var_idx) const
Returns an array for a given variable for one specific object from a SWMM output file for all time st...
std::vector< std::string > getNames(SwmmObject obj_type) const
Get all the object names for a given object type.
std::vector< std::string > _pollutant_names
Vector storing the names of all pollutants.
Container class for geometric objects.
Definition GEOObjects.h:46
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition Polyline.h:29
SwmmObject
SWMM object types.