Debian Codegraph @

src/atlas/interpolation/method/structured/StructuredInterpolation3D.tcc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
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 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction. and Interpolation
9 */
10
11 #pragma once
12
13 #include "StructuredInterpolation3D.h"
14
15 #include "atlas/array/ArrayView.h"
16 #include "atlas/field/Field.h"
17 #include "atlas/field/FieldSet.h"
18 #include "atlas/functionspace/NodeColumns.h"
19 #include "atlas/functionspace/PointCloud.h"
20 #include "atlas/functionspace/StructuredColumns.h"
21 #include "atlas/grid/StructuredGrid.h"
22 #include "atlas/mesh/Nodes.h"
23 #include "atlas/parallel/mpi/mpi.h"
24 #include "atlas/parallel/omp/omp.h"
25 #include "atlas/runtime/Exception.h"
26 #include "atlas/runtime/Log.h"
27 #include "atlas/runtime/Trace.h"
28 #include "atlas/util/CoordinateEnums.h"
29 #include "atlas/util/Point.h"
30
31 namespace atlas {
32 namespace interpolation {
33 namespace method {
34
35 template <typename Kernel>
36 double StructuredInterpolation3D<Kernel>::convert_units_multiplier( const Field& field ) {
37 std::string units = field.metadata().getString( "units", "degrees" );
38 if ( units == "degrees" ) {
39 return 1.;
40 }
41 if ( units == "radians" ) {
42 return 180. / M_PI;
43 }
44 ATLAS_NOTIMPLEMENTED;
45 }
46
47 template <typename Kernel>
48 StructuredInterpolation3D<Kernel>::StructuredInterpolation3D( const Method::Config& config ) :
49 Method( config ),
50 matrix_free_{false},
51 limiter_{false} {
52 config.get( "matrix_free", matrix_free_ );
53 config.get( "limiter", limiter_ );
54
55 if ( not matrix_free_ ) {
56 throw_NotImplemented( "Matrix-free StructuredInterpolation3D not implemented", Here() );
57 }
58 }
59
60
61 template <typename Kernel>
62 void StructuredInterpolation3D<Kernel>::do_setup( const Grid& source, const Grid& target, const Cache& ) {
63 if ( mpi::size() > 1 ) {
64 ATLAS_NOTIMPLEMENTED;
65 }
66 ATLAS_ASSERT( StructuredGrid( source ) );
67 FunctionSpace source_fs = functionspace::StructuredColumns( source, option::halo( kernel_->stencil_halo() ) );
68 FunctionSpace target_fs = functionspace::PointCloud( target );
69
70 do_setup( source_fs, target_fs );
71 }
72
73 template <typename Kernel>
74 void StructuredInterpolation3D<Kernel>::do_setup( const FunctionSpace& source, const FunctionSpace& target, const Cache& cache) {
75 ATLAS_TRACE( "StructuredInterpolation3D<" + Kernel::className() + ">::do_setup(FunctionSpace source, FunctionSpace target, const Cache)" );
76 if (! matrix_free_ && interpolation::MatrixCache(cache)) {
77 setMatrix(cache);
78 source_ = source;
79 target_ = target;
80 ATLAS_ASSERT(matrix().rows() == target.size());
81 ATLAS_ASSERT(matrix().cols() == source.size());
82 return;
83 }
84 else {
85 do_setup( source, target );
86 }
87 }
88
89 template <typename Kernel>
90 void StructuredInterpolation3D<Kernel>::do_setup( const FunctionSpace& source, const FunctionSpace& target ) {
91 ATLAS_TRACE( "StructuredInterpolation<" + Kernel::className() + ">::do_setup()" );
92
93 source_ = source;
94 target_ = target;
95
96 if ( functionspace::PointCloud tgt = target ) {
97 target_lonlat_ = tgt.lonlat();
98 target_vertical_ = tgt.vertical();
99 target_ghost_ = tgt.ghost();
100 }
101 else {
102 ATLAS_NOTIMPLEMENTED;
103 }
104
105 setup( source );
106 }
107
108 template <typename Kernel>
109 void StructuredInterpolation3D<Kernel>::do_setup( const FunctionSpace& source, const Field& target ) {
110 ATLAS_TRACE( "StructuredInterpolation<" + Kernel::className() + ">::do_setup(FunctionSpace source, Field target)" );
111
112 source_ = source;
113
114 if ( target.functionspace() ) {
115 target_ = target.functionspace();
116 }
117
118 target_3d_ = target;
119
120 setup( source );
121 }
122
123 template <typename Kernel>
124 void StructuredInterpolation3D<Kernel>::do_setup( const FunctionSpace& source, const FieldSet& target ) {
125 ATLAS_TRACE( "StructuredInterpolation<" + Kernel::className() + ">::do_setup(FunctionSpace source,FieldSet target)" );
126
127 source_ = source;
128
129 ATLAS_ASSERT( target.size() >= 3 );
130 if ( target[0].functionspace() ) {
131 target_ = target[0].functionspace();
132 }
133
134 target_xyz_ = target;
135
136 setup( source );
137 }
138
139 template <typename Kernel>
140 void StructuredInterpolation3D<Kernel>::print( std::ostream& ) const {
141 ATLAS_NOTIMPLEMENTED;
142 }
143
144
145 template <typename Kernel>
146 void StructuredInterpolation3D<Kernel>::setup( const FunctionSpace& source ) {
147 kernel_.reset( new Kernel( source, util::Config( "limiter", limiter_ ) ) );
148 }
149
150
151 template <typename Kernel>
152 void StructuredInterpolation3D<Kernel>::do_execute( const Field& src_field, Field& tgt_field, Metadata& metadata ) const {
153 FieldSet tgt( tgt_field );
154 do_execute( FieldSet( src_field ), tgt, metadata );
155 }
156
157
158 template <typename Kernel>
159 void StructuredInterpolation3D<Kernel>::do_execute( const FieldSet& src_fields, FieldSet& tgt_fields, Metadata& metadata ) const {
160 if ( not matrix_free_ ) {
161 Method::do_execute( src_fields, tgt_fields, metadata );
162 return;
163 }
164
165 const idx_t N = src_fields.size();
166 ATLAS_ASSERT( N == tgt_fields.size() );
167
168 if ( N == 0 )
169 return;
170
171 haloExchange( src_fields );
172
173 ATLAS_TRACE( "StructuredInterpolation<" + Kernel::className() + ">::do_execute()" );
174
175 array::DataType datatype = src_fields[0].datatype();
176 int rank = src_fields[0].rank();
177
178 ATLAS_ASSERT( rank > 1 );
179
180 for ( idx_t i = 0; i < N; ++i ) {
181 ATLAS_ASSERT( src_fields[i].datatype() == datatype );
182 ATLAS_ASSERT( src_fields[i].rank() == rank );
183 ATLAS_ASSERT( tgt_fields[i].datatype() == datatype );
184 }
185
186 if ( datatype.kind() == array::DataType::KIND_REAL64 && rank == 2 ) {
187 execute_impl<double, 2>( *kernel_, src_fields, tgt_fields );
188 }
189 if ( datatype.kind() == array::DataType::KIND_REAL32 && rank == 2 ) {
190 execute_impl<float, 2>( *kernel_, src_fields, tgt_fields );
191 }
192 if ( datatype.kind() == array::DataType::KIND_REAL64 && rank == 3 ) {
193 execute_impl<double, 3>( *kernel_, src_fields, tgt_fields );
194 }
195 if ( datatype.kind() == array::DataType::KIND_REAL32 && rank == 3 ) {
196 execute_impl<float, 3>( *kernel_, src_fields, tgt_fields );
197 }
198
199 tgt_fields.set_dirty();
200 }
201
202
203 template <typename Kernel>
204 template <typename Value, int Rank>
205 void StructuredInterpolation3D<Kernel>::execute_impl( const Kernel& kernel, const FieldSet& src_fields,
206 FieldSet& tgt_fields ) const {
207 const idx_t N = src_fields.size();
208
209 auto make_src_view = [&]( const FieldSet& src_fields ) {
210 std::vector<array::ArrayView<const Value, Rank> > src_view;
211 src_view.reserve( N );
212 for ( idx_t i = 0; i < N; ++i ) {
213 src_view.emplace_back( array::make_view<const Value, Rank>( src_fields[i] ) );
214 }
215 return src_view;
216 };
217
218 // Assertions
219 ATLAS_ASSERT( tgt_fields.size() == src_fields.size() );
220 idx_t tgt_rank = -1;
221 for ( auto& f : tgt_fields ) {
222 if ( tgt_rank == -1 )
223 tgt_rank = f.rank();
224 if ( f.rank() != tgt_rank ) {
225 throw_Exception( "target fields don't all have the same rank!", Here() );
226 }
227 }
228
229 if ( functionspace::PointCloud( target() ) && tgt_rank == 1 ) {
230 const idx_t out_npts = target_lonlat_.shape( 0 );
231
232 const auto ghost = array::make_view<int, 1>( target_ghost_ );
233 const auto lonlat = array::make_view<double, 2>( target_lonlat_ );
234 const auto vertical = array::make_view<double, 1>( target_vertical_ );
235
236 auto src_view = make_src_view( src_fields );
237
238 constexpr int TargetRank = 1;
239 std::vector<array::ArrayView<Value, TargetRank> > tgt_view;
240 tgt_view.reserve( N );
241 for ( idx_t i = 0; i < N; ++i ) {
242 tgt_view.emplace_back( array::make_view<Value, TargetRank>( tgt_fields[i] ) );
243 }
244
245 const double convert_units = convert_units_multiplier( target_lonlat_ );
246 atlas_omp_parallel {
247 typename Kernel::Stencil stencil;
248 typename Kernel::Weights weights;
249 atlas_omp_for( idx_t n = 0; n < out_npts; ++n ) {
250 if ( not ghost( n ) ) {
251 double x = lonlat( n, LON ) * convert_units;
252 double y = lonlat( n, LAT ) * convert_units;
253 double z = vertical( n );
254 kernel.compute_stencil( x, y, z, stencil );
255 kernel.compute_weights( x, y, z, stencil, weights );
256 for ( idx_t i = 0; i < N; ++i ) {
257 kernel.interpolate( stencil, weights, src_view[i], tgt_view[i], n );
258 }
259 }
260 }
261 }
262 }
263 else if ( target_3d_ && tgt_rank == Rank ) {
264 const idx_t out_npts = target_3d_.shape( 0 );
265 const idx_t out_nlev = target_3d_.shape( 1 );
266
267 const auto coords = array::make_view<const double, 3>( target_3d_ );
268 const auto src_view = make_src_view( src_fields );
269
270 constexpr int TargetRank = Rank;
271 std::vector<array::ArrayView<Value, TargetRank> > tgt_view;
272 tgt_view.reserve( N );
273
274 for ( idx_t i = 0; i < N; ++i ) {
275 tgt_view.emplace_back( array::make_view<Value, TargetRank>( tgt_fields[i] ) );
276
277 if ( Rank == 3 &&
278 ( src_fields[i].stride( Rank - 1 ) != 1 || tgt_fields[i].stride( TargetRank - 1 ) != 1 ) ) {
279 throw_Exception(
280 "Something will go seriously wrong if we continue from here as "
281 "the implementation assumes stride=1 for fastest moving index (variables).",
282 Here() );
283 }
284 }
285
286 const double convert_units = convert_units_multiplier( target_3d_ );
287
288 atlas_omp_parallel {
289 typename Kernel::Stencil stencil;
290 typename Kernel::Weights weights;
291 atlas_omp_for( idx_t n = 0; n < out_npts; ++n ) {
292 for ( idx_t k = 0; k < out_nlev; ++k ) {
293 double x = coords( n, k, LON ) * convert_units;
294 double y = coords( n, k, LAT ) * convert_units;
295 double z = coords( n, k, ZZ );
296
297 kernel.compute_stencil( x, y, z, stencil );
298 kernel.compute_weights( x, y, z, stencil, weights );
299 for ( idx_t i = 0; i < N; ++i ) {
300 kernel.interpolate( stencil, weights, src_view[i], tgt_view[i], n, k );
301 }
302 }
303 }
304 }
305 }
306 else if ( not target_xyz_.empty() && tgt_rank == Rank ) {
307 const idx_t out_npts = target_xyz_[0].shape( 0 );
308 const idx_t out_nlev = target_xyz_[0].shape( 1 );
309
310 const auto xcoords = array::make_view<double, 2>( target_xyz_[LON] );
311 const auto ycoords = array::make_view<double, 2>( target_xyz_[LAT] );
312 const auto zcoords = array::make_view<double, 2>( target_xyz_[ZZ] );
313 const auto src_view = make_src_view( src_fields );
314
315 constexpr int TargetRank = Rank;
316 std::vector<array::ArrayView<Value, TargetRank> > tgt_view;
317 tgt_view.reserve( N );
318
319 for ( idx_t i = 0; i < N; ++i ) {
320 tgt_view.emplace_back( array::make_view<Value, TargetRank>( tgt_fields[i] ) );
321
322 if ( Rank == 3 &&
323 ( src_fields[i].stride( Rank - 1 ) != 1 || tgt_fields[i].stride( TargetRank - 1 ) != 1 ) ) {
324 throw_Exception(
325 "Something will go seriously wrong if we continue from here as "
326 "the implementation assumes stride=1 for fastest moving index (variables).",
327 Here() );
328 }
329 }
330
331 const double convert_units = convert_units_multiplier( target_xyz_[LON] );
332
333 atlas_omp_parallel {
334 typename Kernel::Stencil stencil;
335 typename Kernel::Weights weights;
336 atlas_omp_for( idx_t n = 0; n < out_npts; ++n ) {
337 for ( idx_t k = 0; k < out_nlev; ++k ) {
338 double x = xcoords( n, k ) * convert_units;
339 double y = ycoords( n, k ) * convert_units;
340 double z = zcoords( n, k );
341 kernel.compute_stencil( x, y, z, stencil );
342 kernel.compute_weights( x, y, z, stencil, weights );
343 for ( idx_t i = 0; i < N; ++i ) {
344 kernel.interpolate( stencil, weights, src_view[i], tgt_view[i], n, k );
345 }
346 }
347 }
348 }
349 }
350
351 else {
352 ATLAS_NOTIMPLEMENTED;
353 }
354 }
355
356 } // namespace method
357 } // namespace interpolation
358 } // namespace atlas