Debian Codegraph @

src/atlas/projection/detail/VariableResolutionProjection.cc

raw
1 /**
2 * (C) Crown copyright 2021, Met Office
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 */
7
8 #include <cmath>
9 #include <functional>
10 #include <sstream>
11
12 #include "eckit/config/Parametrisation.h"
13 #include "eckit/utils/Hash.h"
14
15 #include "atlas/functionspace/StructuredColumns.h"
16 #include "atlas/grid.h"
17 #include "atlas/grid/Grid.h"
18 #include "atlas/projection/detail/ProjectionFactory.h"
19 #include "atlas/projection/detail/VariableResolutionProjection.h"
20 #include "atlas/runtime/Exception.h"
21 #include "atlas/util/Config.h"
22 #include "atlas/util/Constants.h"
23
24 /**
25 * Projection for LAM stretching
26 *
27 * The theory used in this code can be found under:
28 * Lateral boundary conditions for limited area models
29 * Terry Davies 2014
30 * https://doi.org/10.1002/qj.2127
31 *
32 * Some equations are under:
33 * The benefits of the Met Office variable resolution NWP model for forecasting convection
34 * Tang et al. 2013
35 * https://doi.org/10.1002/met.1300
36 *
37 * Generally: From a original regular grid the point in the middle of the grid are unchanged,
38 * based on the boundary of the new internal regular grid.
39 * The grid length is then stretched at a constant stretching (or inflation) factor r,
40 * that is Δsi+1 = rΔsi until all the points are mapped in the new grid.
41 * The domain is then extended to a further uniform coarse resolution region (rim) in the outer domain,
42 *
43 * The origin of the xy-system is at (lon,lat) = (0,0)
44 *
45 */
46
47 #ifdef __NVCOMPILER
48 #define PREVENT_OPT volatile
49 #else
50 #define PREVENT_OPT
51 #endif
52
53 namespace atlas {
54 namespace projection {
55 namespace detail {
56
57 static double new_ratio(int n_stretched, double var_ratio) {
58 /**
59 * compute ratio,
60 * change stretching factor so that high and low grids
61 * retain original sizes
62 */
63 ///< correction used to change from double to integer
64 constexpr float epstest = std::numeric_limits<float>::epsilon();
65
66 ///< number of variable (stretched) grid points in one side
67 PREVENT_OPT int var_ints = (n_stretched + epstest) / 2.;
68 double var_ints_f = n_stretched / 2.;
69 double logr = std::log(var_ratio);
70 double log_ratio = (var_ints_f - 0.5) * logr;
71
72 return std::exp(log_ratio / var_ints);
73 };
74
75
76 ///< specification parameters
77 template <typename Rotation>
78 typename VariableResolutionProjectionT<Rotation>::Spec VariableResolutionProjectionT<Rotation>::spec() const {
79 Spec proj_st;
80 proj_st.set("type", static_type());
81 proj_st.set("outer.dx", delta_outer); ///< resolution of the external regular grid (rim)
82 proj_st.set("inner.dx", delta_inner); ///< resolution of the regional model (regular grid)
83 proj_st.set("progression", var_ratio_); ///< power used for the stretching
84 proj_st.set("inner.xmin", x_reg_start_); ///< xstart of the internal regional grid
85 proj_st.set("inner.ymin", y_reg_start_); ///< ystart of the internal regional grid
86 proj_st.set("inner.xend", x_reg_end_); ///< xend of the regular part of stretched internal grid
87 proj_st.set("inner.yend", y_reg_end_); ///< yend of the regular part of stretched internal grid
88 proj_st.set("outer.xmin", startx_); ///< original domain startx
89 proj_st.set("outer.xend", endx_); ///< original domain endx
90 proj_st.set("outer.ymin", starty_); ///< original domain starty
91 proj_st.set("outer.yend", endy_); ///< original domain endy
92 proj_st.set("rim_widthx", rim_widthx_); ///< xsize of the rim
93 proj_st.set("rim_widthy", rim_widthy_); ///< ysize of the rim
94 rotation_.spec(proj_st);
95 return proj_st;
96 }
97
98
99 ///< constructors
100 template <typename Rotation>
101 VariableResolutionProjectionT<Rotation>::VariableResolutionProjectionT(const eckit::Parametrisation& proj_st):
102 ProjectionImpl(), rotation_(proj_st) {
103 proj_st.get("outer.dx", delta_outer = 0.); ///< resolution of the external regular grid (rim)
104 proj_st.get("inner.dx", delta_inner = 0.); ///< resolution of the regional model (regular grid)
105 proj_st.get("progression", var_ratio_ = 0.); ///< power used for the stretching
106 proj_st.get("inner.xmin", x_reg_start_ = 0.); ///< xstart of the internal regional grid
107 proj_st.get("inner.ymin", y_reg_start_ = 0.); ///< ystart of the internal regional grid
108 proj_st.get("inner.xend", x_reg_end_ = 0.); ///< xend of the regular part of stretched internal grid
109 proj_st.get("inner.yend", y_reg_end_ = 0.); ///< yend of the regular part of stretched internal grid
110 proj_st.get("outer.xmin", startx_ = 0.); ///< original domain startx
111 proj_st.get("outer.xend", endx_ = 0.); ///< original domain endx
112 proj_st.get("outer.ymin", starty_ = 0.); ///< original domain starty
113 proj_st.get("outer.yend", endy_ = 0.); ///< original domain endy
114
115 // To get the rim_widthx_ we can specify either:
116 // - outer.nx
117 // - outer.xwidth
118 // - outer.width
119 // The value for rim_widthy_ is copied from rim_widthx_ and can be overwritten with
120 // - outer.ny
121 // - outer.ywidth
122 if (proj_st.has("outer.nx")) {
123 long outer_nx;
124 proj_st.get("outer.nx", outer_nx);
125 rim_widthx_ = delta_outer * outer_nx;
126 rim_widthy_ = rim_widthx_;
127 }
128 else if (proj_st.has("outer.width")) {
129 proj_st.get("outer.width", rim_widthx_);
130 rim_widthy_ = rim_widthx_;
131 }
132 else if (proj_st.has("outer.xwidth")) {
133 proj_st.get("outer.xwidth", rim_widthx_);
134 rim_widthy_ = rim_widthx_;
135 }
136 if (proj_st.has("outer.ny")) {
137 long outer_ny;
138 proj_st.get("outer.ny", outer_ny);
139 rim_widthy_ = delta_outer * outer_ny;
140 }
141 else if (proj_st.has("outer.ywidth")) {
142 proj_st.get("outer.ywidth", rim_widthy_);
143 }
144
145 constexpr float epsilon = std::numeric_limits<float>::epsilon(); ///< value used to check if the values are equal
146 constexpr float epstest =
147 std::numeric_limits<float>::epsilon(); ///< correction used to change from double to intege
148
149 ///< original domain size includes the points for the rim
150 deltax_all = (endx_ - startx_);
151 deltay_all = (endy_ - starty_);
152
153
154 nx_stretched = 0;
155 ny_stretched = 0;
156 nx_rim = 0;
157 ny_rim = 0;
158
159 if (var_ratio_ == 1) {
160 lam_hires_size = deltax_all;
161 phi_hires_size = deltay_all;
162 lambda_start = x_reg_start_;
163 phi_start = y_reg_start_;
164 }
165 else {
166 lam_hires_size = x_reg_end_ - x_reg_start_;
167 phi_hires_size = y_reg_end_ - y_reg_start_;
168
169 /**
170 * check for start of the new regular LAM grid x and y
171 * in the middle of the previous regular grid
172 */
173
174 ///< distance end of the grid and internal regular grid
175 add_xf_ = (deltax_all + epstest - lam_hires_size) / 2.;
176 add_yf_ = (deltay_all + epstest - phi_hires_size) / 2.;
177 /**
178 * Compute the number of points for different part of the grid
179 * internal regular grid high resolution
180 * stretched grid
181 * external regular grid low resolution
182 * +1 otherwise I have the intervals not the points
183 */
184 nx_rim = rim_widthx_ / delta_outer;
185 ny_rim = rim_widthy_ / delta_outer;
186 nx_stretched = ((deltax_all + epstest - lam_hires_size) / delta_inner) - nx_rim;
187 ny_stretched = ((deltay_all + epstest - phi_hires_size) / delta_inner) - ny_rim;
188 lambda_start = x_reg_start_;
189 phi_start = y_reg_start_;
190 /**
191 * check if stretched grid is in the middle
192 * of the previous regular grid
193 */
194 check_x = startx_ + add_xf_ - lambda_start;
195 check_y = starty_ + add_yf_ - phi_start;
196 check_st = nx_stretched - ny_stretched;
197 checkvalue(epsilon, check_x);
198 checkvalue(epsilon, check_y);
199 checkvalue(epsilon, check_st);
200 }
201
202 int nx_ = (deltax_all + epstest) / delta_inner + 1;
203 int ny_ = (deltay_all + epstest) / delta_inner + 1;
204
205 int nx_inner = (lam_hires_size + epstest) / delta_inner + 1;
206 int ny_inner = (phi_hires_size + epstest) / delta_inner + 1;
207
208 ATLAS_ASSERT((nx_ - 1) - nx_rim - (nx_inner - 1) == nx_stretched);
209 ATLAS_ASSERT((ny_ - 1) - ny_rim - (ny_inner - 1) == ny_stretched);
210
211 new_ratio_[0] = var_ratio_;
212 new_ratio_[1] = var_ratio_;
213 if (var_ratio_ != 1) {
214 new_ratio_[0] = new_ratio(nx_stretched, var_ratio_);
215 new_ratio_[1] = new_ratio(ny_stretched, var_ratio_);
216 }
217 }
218
219
220 template <typename Rotation>
221 void VariableResolutionProjectionT<Rotation>::checkvalue(const double& epsilon, const double& value_check) const {
222 //err_message = "USER defined limits not in the middle of the area " + str;
223 if (value_check > epsilon || value_check < (-1. * epsilon)) {
224 std::string err_message;
225 std::string str = std::to_string(value_check);
226 throw_Exception("USER defined limits not in the middle of the area " + str, Here());
227 }
228 }
229
230 /**
231 * General stretch from a point in regular grid to the
232 * a correspective point in a variable grid
233 */
234
235 template <typename Rotation>
236 double VariableResolutionProjectionT<Rotation>::general_stretch(const double crd, const bool L_long,
237 const int n_stretched) const {
238 constexpr float epstest =
239 std::numeric_limits<float>::epsilon(); ///< correction used to change from double to integer
240 constexpr double epsrem =
241 0.1 * std::numeric_limits<double>::epsilon() /
242 std::numeric_limits<float>::epsilon(); ///< correction used to part the find a part of an integer
243
244 double inner_size; ///< number of new internal regular grid in double
245 double inner_start; ///< start of the regular grid
246 double inner_end; ///< end of the regular grid
247 double point = crd; ///< starting point
248
249 auto normalised = [L_long](double p) {
250 if (L_long) {
251 p = (p < 180) ? p + 360.0 : p;
252 }
253 return p;
254 };
255
256 /*
257 * regular grids
258 */
259 if (var_ratio_ == 1) {
260 return normalised(point);
261 }
262
263 /*
264 * SECTION 1
265 * INTERNAL REGULAR GRID the point is mapped to the same point
266 */
267
268 if (L_long) {
269 inner_start = x_reg_start_;
270 inner_size = lam_hires_size;
271 }
272 else {
273 inner_start = y_reg_start_;
274 inner_size = phi_hires_size;
275 }
276 inner_end = inner_start + inner_size;
277
278 if ((point >= inner_start) && (point <= inner_end)) {
279 return normalised(point);
280 }
281
282 /* SECTION 2
283 * Start variable res 'STRETCHED'
284 * The point is mapped in a stretched grid related to
285 * distance from the regular grid and stretch internal grid: delta_dist
286 */
287
288 double distance_to_inner = 0.; ///< distance from point to reg. grid
289 double delta_add = 0.; ///< additional part in stretch different from internal high resolution
290 int n_high = 0; ///< number of points, from point to reg. grid
291 int n_high_st = 0; ///< number of stretched points, from point to reg grid
292 int n_high_rim = 0; ///< number of rim points, from point to reg grid
293 double p_rem = 0.; ///< remaining part in stretch if distance_to_inner not multiple of delta_high_
294 double p_rem_low = 0.; ///< remaining part in rim if distance_to_inner not multiple of delta_high_
295 double new_ratio = new_ratio_[L_long ? 1 : 0];
296
297 if (point < inner_start) {
298 distance_to_inner = inner_start - point;
299 }
300 else if (point > inner_end) {
301 distance_to_inner = point - inner_end;
302 }
303
304 /*
305 * number of high resolution points intervals, that are not
306 * in the internal regular grid on one side,
307 * this work just for the part of the stretch not the rim
308 */
309
310 ///< always the lowest integer
311 n_high = (distance_to_inner + epstest) / delta_inner;
312
313 ///< only for the stretched part take out the rim part
314 if (n_high > n_stretched / 2.) {
315 n_high_st = (n_stretched / 2.);
316 n_high_rim = n_high - n_high_st;
317 p_rem = 0;
318 p_rem_low = std::fmod((distance_to_inner + epsrem), delta_inner);
319 }
320 else {
321 n_high_st = n_high;
322 n_high_rim = 0;
323 ///< part remaining, use modulo
324 p_rem = std::fmod((distance_to_inner + epsrem), delta_inner);
325 p_rem_low = 0.;
326 }
327
328 ///< computation of the new stretched delta integer part
329 double delta = delta_inner;
330 ///< initialization if is not using the cycle (first interval)
331 double delta_last = delta;
332 double deltacheck = 0;
333 /*
334 * using difference in delta for stretch
335 * The point stretched is not in the regular grid and took out the points for the rim
336 */
337 for (int i = 0; i < n_high_st; i += 1) {
338 delta_last = delta * new_ratio;
339 delta_add = delta_last - delta_inner;
340 delta = delta_last;
341 deltacheck += delta_add;
342 }
343 ///< recomputation of point for every interval
344 if (point > inner_start) {
345 /*
346 * after the end of the internal high resolution grid
347 * no delta_add in the last points as they are rim
348 */
349 point += deltacheck;
350 }
351 else {
352 /*
353 * before the begin of the internal high resolution grid
354 * no delta_add in the first points as they are rim
355 */
356 point -= deltacheck;
357 }
358
359 ///< SECTION 3 last part of stretch adding the remaing non integer part with the same ratio as in the stretching
360 double delta_r = p_rem * std::pow(new_ratio, (n_high_st + 1));
361 double delta_addr = delta_r - p_rem;
362
363 if (point > inner_start) {
364 point += delta_addr;
365 }
366 else {
367 point -= delta_addr;
368 }
369 ///< SECTION 4 rim area
370 if (n_high > n_stretched / 2.) {
371 double delta_l_h_ = 0;
372 for (int i = 0; i < n_high_rim; i += 1) {
373 delta_l_h_ += (delta_outer - delta_inner);
374 }
375 if (point > inner_start) {
376 point += (delta_l_h_ + p_rem_low * (delta_outer - delta_inner));
377 }
378 else {
379 point -= (delta_l_h_ + p_rem_low * (delta_outer - delta_inner));
380 }
381 }
382
383 return normalised(point);
384 }
385
386
387 /**
388 * Inverse of general stretch from a point in variable grid to the
389 * a correspective point in a regular grid
390 */
391
392 template <typename Rotation>
393 double VariableResolutionProjectionT<Rotation>::general_stretch_inv(const double crd, const bool L_long,
394 const int n_stretched) const {
395 constexpr float epstest =
396 std::numeric_limits<float>::epsilon(); ///< correction used to change from double to integer
397
398 double inner_size; ///< number of new internal regular grid in double
399 double inner_start; ///< start of the regular grid
400 double inner_end; ///< end of the regular grid
401 double point_st = crd; ///< input point in the variational grid
402 double point_reg = 0.; ///< point in the regular grid
403
404
405 auto normalised = [L_long](double p) {
406 if (L_long) {
407 p = (p < 180) ? p + 360.0 : p;
408 }
409 return p;
410 };
411
412 point_st = normalised(point_st);
413
414 /*
415 * regular grids
416 */
417 if (var_ratio_ == 1) {
418 point_reg = point_st;
419 return normalised(point_reg);
420 }
421
422 /*
423 * SECTION 1
424 * INTERNAL REGULAR GRID the point is mapped to the same point
425 */
426
427
428 if (L_long) {
429 inner_start = x_reg_start_;
430 inner_size = lam_hires_size;
431 }
432 else {
433 inner_start = y_reg_start_;
434 inner_size = phi_hires_size;
435 }
436
437 inner_end = inner_start + inner_size;
438
439 //< points in the internal regular grid
440 if ((point_st >= inner_start - epstest) && (point_st <= inner_end + epstest)) {
441 point_reg = point_st;
442 return normalised(point_reg);
443 }
444
445
446 /* SECTION 2
447 * Start variable res 'STRETCHED'
448 * The point is mapped in a regular grid
449 * from stretch internal grid: delta_dist
450 * simply using delta_high
451 */
452
453 double delta_add; ///< additional part in stretch different from internal high resolution
454 double new_ratio = new_ratio_[L_long ? 1 : 0];
455
456 /*
457 * number of high resolution points intervals, that are not
458 * in the internal regular grid on one side,
459 * this work just for the part of the stretch not the rim
460 */
461
462 ///< computation of the new stretched delta integer part
463 double delta = delta_inner;
464 ///< initialization if is not using the cycle (first interval)
465 double delta_last = delta;
466 double deltacheck = 0;
467 /*
468 * using difference in delta for stretch
469 * The point stretched is not in the regular grid and took out the points for the rim
470 */
471
472 double point_var = 0.;
473 for (int i = 1; i < n_stretched / 2.; i += 1) {
474 delta_last = delta * new_ratio;
475 delta_add = delta_last - delta_inner;
476 delta = delta_last;
477 deltacheck += delta_add;
478 if (point_st > inner_start) {
479 point_reg = inner_end + (delta_inner * i);
480 point_var = point_reg + deltacheck;
481 if ((point_st <= point_var + epstest) && (point_st >= point_var - epstest)) {
482 return normalised(point_reg);
483 }
484 }
485 else {
486 point_reg = inner_start - (delta_inner * i);
487 point_var = point_reg - deltacheck;
488 if ((point_st <= point_var + epstest) && (point_st >= point_var - epstest)) {
489 return normalised(point_reg);
490 }
491 }
492 }
493
494 ///< SECTION 3_inv rim area
495 int n_rim; ///< number of rim points
496 if (point_st > point_var) {
497 n_rim = (point_st - point_var) / delta_outer;
498 point_reg = inner_end + (delta_inner * (n_stretched / 2 + n_rim));
499 return normalised(point_reg);
500 }
501 else {
502 if (point_st < point_var) {
503 n_rim = (point_var - point_st) / delta_outer;
504 point_reg = inner_start - (delta_inner * (n_stretched / 2 + n_rim));
505 return normalised(point_reg);
506 }
507 }
508 return normalised(point_reg);
509 }
510
511
512 ///< xy unstretched, only unrotation
513 template <typename Rotation>
514 void VariableResolutionProjectionT<Rotation>::lonlat2xy(double crd[]) const {
515 ///< unrotate
516 rotation_.unrotate(crd);
517 //< normalization
518 crd[0] = (crd[0] < 0) ? crd[0] + 360.0 : crd[0];
519 ///< PUT the unstretch
520 crd[0] = general_stretch_inv(crd[0], true, nx_stretched);
521 crd[1] = general_stretch_inv(crd[1], false, ny_stretched);
522 }
523
524
525 ///< From unstretched to stretched
526 template <typename Rotation>
527 void VariableResolutionProjectionT<Rotation>::xy2lonlat(double crd[]) const {
528 /** PUT here the stretch for a point that come input
529 * give number of power as input,work it out using as from the start of regular grid.
530 * atlas::PointXY unstretchedXY = crd;
531 * atlas::PointXY stretchedXY = stretch_LAM_gen(unstretchedXY);
532 * stretch_LAM_gen(crd[]);
533 */
534
535 crd[0] = general_stretch(crd[0], true, nx_stretched);
536 crd[1] = general_stretch(crd[1], false, ny_stretched);
537
538 rotation_.rotate(crd);
539 }
540
541 template <typename Rotation>
542 ProjectionImpl::Jacobian VariableResolutionProjectionT<Rotation>::jacobian(const PointLonLat&) const {
543 throw_NotImplemented("VariableResolution::jacobian", Here());
544 }
545
546 template <typename Rotation>
547 void VariableResolutionProjectionT<Rotation>::hash(eckit::Hash& hsh) const {
548 hsh.add(static_type());
549 rotation_.hash(hsh);
550 //hsh.add( radius_ );
551 }
552
553 template class VariableResolutionProjectionT<NotRotated>;
554 template class VariableResolutionProjectionT<Rotated>;
555
556 namespace {
557 static ProjectionBuilder<VariableResolutionProjection> register_1(VariableResolutionProjection::static_type());
558 static ProjectionBuilder<RotatedVariableResolutionProjection> register_2(
559 RotatedVariableResolutionProjection::static_type());
560 } // namespace
561
562 } // namespace detail
563 } // namespace projection
564 } // namespace atlas