Peano 4
Loading...
Searching...
No Matches
TopologyParser.cpp
Go to the documentation of this file.
1#include "TopologyParser.h"
2#include <iostream>
4
6 const char *bathymetry_file_path, const char *displacement_file_path,
7 const char *nc_bathymetry_x_key, const char *nc_bathymetry_y_key, const char *nc_bathymetry_bathymetry_key,
8 const char *nc_displacement_x_key, const char *nc_displacement_y_key, const char *nc_displacement_displacement_key,
9 double i_uq_displacement_origin_x, double i_uq_displacement_origin_y
10):
11uq_displacement_origin_x_(i_uq_displacement_origin_x),
12uq_displacement_origin_y_(i_uq_displacement_origin_y),
13nc_bathymetry_x_key_(nc_bathymetry_x_key),
14nc_bathymetry_y_key_(nc_bathymetry_y_key),
15nc_bathymetry_bathymetry_key_(nc_bathymetry_bathymetry_key),
16nc_displacement_x_key_(nc_displacement_x_key),
17nc_displacement_y_key_(nc_displacement_y_key),
18nc_displacement_displacement_key_(nc_displacement_displacement_key)
19{
20
21 l_nX_ = applications::exahype2::swe::DomainSize(0);
22 l_nY_ = applications::exahype2::swe::DomainSize(1);
23 this->parse_bathymetry_file(bathymetry_file_path);
24 this->parse_displacement_file(displacement_file_path);
25
26}
27
29 const char *bathymetry_file_path,
30 double width_x, double width_y,
31 const char *nc_bathymetry_x_key, const char *nc_bathymetry_y_key, const char *nc_bathymetry_bathymetry_key
32):
33l_nX_(width_x),
34l_nY_(width_y),
35nc_bathymetry_x_key_(nc_bathymetry_x_key),
36nc_bathymetry_y_key_(nc_bathymetry_y_key),
37nc_bathymetry_bathymetry_key_(nc_bathymetry_bathymetry_key)
38{
39 this->parse_bathymetry_file(bathymetry_file_path);
40}
41
42
44 delete this->bathymetry_;
45 delete this->displacement_;
46}
47
49 int ncid;
50 // Open bathymetry netCDF file
51 if ((ncid = NetCDFReader::openCdf(bathymetry_file_path)) == -1)
52 {
53 std::cout << "Scenario could not be initialized: Bathymetry file could not be opened at: "<< bathymetry_file_path << std::endl;
54 return;
55 }
56
57 // Read x-dimension of bathymetry
58 if ((x_bathymetry_ = NetCDFReader::getDimension(ncid, nc_bathymetry_x_key_)) == 0)
59 {
60 std::cout << "Scenario could not be initialized: Dimension of the x-Bathymetry could not be read." << std::endl;
61 return;
62 }
63
64 // Read y-dimension of bathymetry
65 if ((y_bathymetry_ = NetCDFReader::getDimension(ncid, nc_bathymetry_y_key_)) == 0)
66 {
67 std::cout << "Scenario could not be initialized: Dimension of the y-Bathymetry could not be read." << std::endl;
68 return;
69 }
70
71 bathymetry_ = new double[x_bathymetry_*y_bathymetry_];
72 // Read bathymetry
73 if (NetCDFReader::readVariable2D(ncid, nc_bathymetry_bathymetry_key_, bathymetry_) == -1)
74 {
75 std::cout << "Scenario could not be initialized: Bathymetry could not be read." << std::endl;
76 return;
77 }
78
79 // Get max and min x
80 {
81 double tmp[x_bathymetry_]{0};
82 if (NetCDFReader::readVariable1D(ncid, nc_bathymetry_x_key_, tmp) == -1)
83 {
84 std::cout << "Scenario could not be initialized: Bathymetry x indices could not be read." << std::endl;
85 return;
86 }
87 min_x_bathymetry_ = tmp[0];
88 max_x_bathymetry_ = tmp[x_bathymetry_ - 1];
89 }
90
91 // Get max and min y
92 {
93 double tmp[y_bathymetry_]{0};
94 if (NetCDFReader::readVariable1D(ncid, nc_bathymetry_y_key_, tmp) == -1)
95 {
96 std::cout << "Scenario could not be initialized: Bathymetry y indices could not be read." << std::endl;
97 return;
98 }
99 min_y_bathymetry_ = tmp[0];
100 max_y_bathymetry_ = tmp[y_bathymetry_ - 1];
101 }
102
103 // Close bathymetry netCDF file
104 if (NetCDFReader::closeCdf(ncid) == -1)
105 {
106 std::cout << "Scenario could not be initialized: Bathymetry file could not be closed." << std::endl;
107 return;
108 }
109}
110
112 // Read displacement netCDF file
113 int ncid;
114 if ((ncid = NetCDFReader::openCdf(displacement_file_path)) == -1)
115 {
116 std::cout << "Scenario could not be initialized: Displacement file could not be opened at: "<< displacement_file_path << std::endl;
117 return;
118 }
119
120 // Read x-dimension of displacement
121 if ((x_displacement_ = NetCDFReader::getDimension(ncid, nc_displacement_x_key_)) == 0)
122 {
123 std::cout << "Scenario could not be initialized: Dimension of the x-displacement could not be read." << std::endl;
124 return;
125 }
126
127 // Read y-dimension of displacement
128 if ((y_displacement_ = NetCDFReader::getDimension(ncid, nc_displacement_y_key_)) == 0)
129 {
130 std::cout << "Scenario could not be initialized: Dimension of the y-displacement could not be read." << std::endl;
131 return;
132 }
133
134 // Get max and min x
135 {
136 double tmp[x_displacement_]{0};
137 if (NetCDFReader::readVariable1D(ncid, nc_displacement_x_key_, tmp) == -1)
138 {
139 std::cout << "Scenario could not be initialized: Displacement x indices could not be read." << std::endl;
140 return;
141 }
142 min_x_displacement_ = tmp[0];
143 max_x_displacement_ = tmp[x_displacement_ - 1];
144 }
145
146 // Get max and min y
147 {
148 double tmp[y_displacement_]{0};
149 if (NetCDFReader::readVariable1D(ncid, nc_displacement_y_key_, tmp) == -1)
150 {
151 std::cout << "Scenario could not be initialized: Displacement y indices could not be read." << std::endl;
152 return;
153 }
154 min_y_displacement_ = tmp[0];
155 max_y_displacement_ = tmp[y_displacement_ - 1];
156 }
157
158 displacement_ = new double[x_displacement_*y_displacement_];
159
160 // Read displacement
161 if (NetCDFReader::readVariable2D(ncid, nc_displacement_displacement_key_, displacement_) == -1)
162 {
163 std::cout << "Scenario could not be initialized: Displacement could not be read." << std::endl;
164 return;
165 }
166
167 // Close displacement netCDF file
168 if (NetCDFReader::closeCdf(ncid) == -1)
169 {
170 std::cout << "Scenario could not be initialized: Displacement file could not be closed." << std::endl;
171 return;
172 }
173}
174
176 auto x_cdf = transformIndexSimulationToCDFRange(x, l_nX_, min_x_bathymetry_, max_x_bathymetry_);
177 auto y_cdf = transformIndexSimulationToCDFRange(y, l_nY_, min_y_bathymetry_, max_y_bathymetry_);
178 // No defined bathymetry
179 if (tarch::la::greater(x_cdf, max_x_bathymetry_) || tarch::la::smaller(x_cdf, min_x_bathymetry_) || tarch::la::greater(y_cdf, max_y_bathymetry_) || tarch::la::smaller(y_cdf, min_y_bathymetry_))
180 return 20.f;
181 return bathymetry_[transformIndexCDFRangeToArray(x_cdf, y_cdf, min_x_bathymetry_, min_y_bathymetry_, max_x_bathymetry_, max_y_bathymetry_, x_bathymetry_, y_bathymetry_)];
182}
183
185 auto x_cdf = transformIndexSimulationToCDFRange(x, l_nX_, min_x_bathymetry_, max_x_bathymetry_);
186 auto y_cdf = transformIndexSimulationToCDFRange(y, l_nY_, min_y_bathymetry_, max_y_bathymetry_);
187
188 // Has the origin of the displacement in x-dimension been put elsewhere than specified in the file?
189 if(!isinf(uq_displacement_origin_x_))
190 // project coordinates into opposite direction of new origin so that displacement gets shifted towards new origin
191 x_cdf -= uq_displacement_origin_x_ - ((max_x_displacement_ + min_x_displacement_) / 2);
192
193 // Has the origin of the displacement in y-dimension been put elsewhere than specified in the file?
194 if(!isinf(uq_displacement_origin_y_))
195 // project coordinates into opposite direction of new origin so that displacement gets shifted towards new origin
196 y_cdf -= uq_displacement_origin_y_ - ((max_y_displacement_ + min_y_displacement_) / 2);
197
198 // Is displacement defined
199 if (tarch::la::smallerEquals(x_cdf, max_x_displacement_) && tarch::la::greaterEquals(x_cdf, min_x_displacement_) && tarch::la::smallerEquals(y_cdf, max_y_displacement_) && tarch::la::greaterEquals(y_cdf, min_y_displacement_))
200 return displacement_[transformIndexCDFRangeToArray(x_cdf, y_cdf, min_x_displacement_, min_y_displacement_, max_x_displacement_, max_y_displacement_, x_displacement_, y_displacement_)];
201 return 0.0;
202}
203
204
205
206
207
208
TopologyParser(const char *bathymetry_file_path, const char *displacement_file_path, const char *nc_bathymetry_x_key="x", const char *nc_bathymetry_y_key="y", const char *nc_bathymetry_bathymetry_key="z", const char *nc_displacement_x_key="x", const char *nc_displacement_y_key="y", const char *nc_displacement_displacement_key="z", double i_uq_displacement_origin_x=INFINITY, double i_uq_displacement_origin_y=INFINITY)
void parse_displacement_file(const char *displacement_file_path)
void parse_bathymetry_file(const char *bathymetry_file_path)
int transformIndexCDFRangeToArray(double i_x, double i_y, double i_min_x, double i_min_y, double i_max_x, double i_max_y, size_t i_xDim, size_t i_yDim)
Transforms coordinate pair from netCDF plane to 1D row-wise array index.
double transformIndexSimulationToCDFRange(double i_z, double i_nZ, double i_min_z_cdf, double i_max_z_cdf)
Transforms coordinate from simulation plane to netCDF plane.
int readVariable2D(int ncid, const char *key, double **variable, size_t rows, size_t cols)
Read contents of a 2D double variable from netCDF file into 2D array.
int closeCdf(int ncid)
Closes netCDF file.
size_t getDimension(int ncid, const char *key)
Get the dimension of a variable.
int readVariable1D(int ncid, const char *key, double *variable)
Read contents of a 1D double variable from netCDF file.
int openCdf(String filename)
Opens a netCDF file, returns ncid.
static bool smaller(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE) InlineMethod
Smaller operator for floating point values.
bool greater(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
bool greaterEquals(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
bool smallerEquals(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE)