Debian Codegraph @

src/atlas/trans/ifs/TransIFS.cc

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.
9 */
10
11 #include "eckit/log/JSON.h"
12
13 #include "atlas/library/config.h"
14 #if ATLAS_HAVE_ECTRANS
15 #include "ectrans/transi.h"
16 #else
17 #include "transi/trans.h"
18 #endif
19
20 #include "atlas/array.h"
21 #include "atlas/field/FieldSet.h"
22 #include "atlas/functionspace/NodeColumns.h"
23 #include "atlas/functionspace/Spectral.h"
24 #include "atlas/functionspace/StructuredColumns.h"
25 #include "atlas/mesh/IsGhostNode.h"
26 #include "atlas/mesh/Nodes.h"
27 #include "atlas/parallel/mpi/mpi.h"
28 #include "atlas/runtime/Exception.h"
29 #include "atlas/runtime/Log.h"
30 #include "atlas/trans/Trans.h"
31 #include "atlas/trans/detail/TransFactory.h"
32 #include "atlas/trans/ifs/TransIFS.h"
33
34 using Topology = atlas::mesh::Nodes::Topology;
35 using atlas::Field;
36 using atlas::FunctionSpace;
37 using atlas::array::ArrayView;
38 using atlas::array::LocalView;
39 using atlas::array::make_shape;
40 using atlas::array::make_view;
41 using atlas::functionspace::NodeColumns;
42 using atlas::functionspace::Spectral;
43 using atlas::functionspace::StructuredColumns;
44 using atlas::mesh::IsGhostNode;
45
46 namespace atlas {
47 namespace trans {
48
49 namespace {
50 static TransBuilderGrid<TransIFS> builder_ifs("ifs", "ifs"); // deprecated
51 static TransBuilderGrid<TransIFS> builder_ectrans("ectrans", "ectrans");
52 } // namespace
53
54 class TransParameters {
55 public:
56 TransParameters(const TransIFS& trans, const eckit::Configuration& config): trans_(trans), config_(config) {}
57 ~TransParameters() = default;
58
59 bool scalar_derivatives() const { return config_.getBool("scalar_derivatives", false); }
60
61 bool wind_EW_derivatives() const { return config_.getBool("wind_EW_derivatives", false); }
62
63 bool vorticity_divergence_fields() const { return config_.getBool("vorticity_divergence_fields", false); }
64
65 bool split_y() const { return config_.getBool("split_y", true); }
66
67 int fft() const {
68 static const std::map<std::string, int> string_to_FFT = {{"FFT992", TRANS_FFT992}, {"FFTW", TRANS_FFTW}};
69 return string_to_FFT.at(config_.getString("fft", "FFTW"));
70 }
71
72 bool flt() const { return config_.getBool("flt", false); }
73
74 std::string read_legendre() const { return config_.getString("read_legendre", ""); }
75
76 std::string write_legendre() const { return config_.getString("write_legendre", ""); }
77
78 bool global() const { return config_.getBool("global", false); }
79
80 int nproma() const { return config_.getInt("nproma", trans_->ngptot); }
81
82 int ngpblks() const {
83 int _ngptot = trans_->ngptot;
84 int _nproma = nproma();
85 ATLAS_ASSERT(_ngptot % _nproma == 0); // assert _ngptot is divisable by nproma
86 return _ngptot / _nproma;
87 }
88
89 private:
90 const Trans_t* trans_;
91 const eckit::Configuration& config_;
92 };
93
94 namespace {
95 std::string fieldset_functionspace(const FieldSet& fields) {
96 std::string functionspace("undefined");
97 for (idx_t jfld = 0; jfld < fields.size(); ++jfld) {
98 if (functionspace == "undefined") {
99 functionspace = fields[jfld].functionspace().type();
100 }
101 if (fields[jfld].functionspace().type() != functionspace) {
102 throw_Exception(": fielset has fields with different functionspaces", Here());
103 }
104 }
105 return functionspace;
106 }
107 void assert_spectral_functionspace(const FieldSet& fields) {
108 for (idx_t jfld = 0; jfld < fields.size(); ++jfld) {
109 ATLAS_ASSERT(functionspace::Spectral(fields[jfld].functionspace()));
110 }
111 }
112
113 void trans_check(const int code, const char* msg, const eckit::CodeLocation& location) {
114 if (code != TRANS_SUCCESS) {
115 std::stringstream errmsg;
116 errmsg << "atlas::trans ERROR: " << msg << " failed: \n";
117 errmsg << ::trans_error_msg(code);
118 throw_Exception(errmsg.str(), location);
119 }
120 }
121 #define TRANS_CHECK(CALL) trans_check(CALL, #CALL, Here())
122
123
124 static int compute_nfld(const Field& f) {
125 int nfld = 1;
126 for (int i = 1; i < f.rank(); ++i) {
127 nfld *= f.shape(i);
128 }
129 return nfld;
130 }
131
132 static int compute_nfld(const FieldSet& f) {
133 int nfld = 0;
134 for (int i = 0; i < f.size(); ++i) {
135 nfld += compute_nfld(f[i]);
136 }
137 return nfld;
138 }
139
140 // --------------------------------------------------------------------------------------------
141
142 } // namespace
143
144 void TransIFS::dirtrans(const Field& gpfield, Field& spfield, const eckit::Configuration& config) const {
145 ATLAS_ASSERT(functionspace::Spectral(spfield.functionspace()));
146 if (functionspace::StructuredColumns(gpfield.functionspace())) {
147 __dirtrans(functionspace::StructuredColumns(gpfield.functionspace()), gpfield,
148 functionspace::Spectral(spfield.functionspace()), spfield, config);
149 }
150 else if (functionspace::NodeColumns(gpfield.functionspace())) {
151 __dirtrans(functionspace::NodeColumns(gpfield.functionspace()), gpfield,
152 functionspace::Spectral(spfield.functionspace()), spfield, config);
153 }
154 else {
155 ATLAS_NOTIMPLEMENTED;
156 }
157 }
158
159 void TransIFS::dirtrans(const FieldSet& gpfields, FieldSet& spfields, const eckit::Configuration& config) const {
160 ATLAS_TRACE();
161 assert_spectral_functionspace(spfields);
162 std::string functionspace(fieldset_functionspace(gpfields));
163
164 if (functionspace == StructuredColumns::type()) {
165 __dirtrans(StructuredColumns(gpfields[0].functionspace()), gpfields, Spectral(spfields[0].functionspace()),
166 spfields, config);
167 }
168 else if (functionspace == NodeColumns::type()) {
169 __dirtrans(NodeColumns(gpfields[0].functionspace()), gpfields, Spectral(spfields[0].functionspace()), spfields,
170 config);
171 }
172 else {
173 ATLAS_NOTIMPLEMENTED;
174 }
175 }
176
177 void TransIFS::dirtrans_wind2vordiv(const Field& gpwind, Field& spvor, Field& spdiv,
178 const eckit::Configuration& config) const {
179 ATLAS_ASSERT(Spectral(spvor.functionspace()));
180 ATLAS_ASSERT(Spectral(spdiv.functionspace()));
181 if (StructuredColumns(gpwind.functionspace())) {
182 __dirtrans_wind2vordiv(StructuredColumns(gpwind.functionspace()), gpwind, Spectral(spvor.functionspace()),
183 spvor, spdiv, config);
184 }
185 else if (NodeColumns(gpwind.functionspace())) {
186 __dirtrans_wind2vordiv(NodeColumns(gpwind.functionspace()), gpwind, Spectral(spvor.functionspace()), spvor,
187 spdiv, config);
188 }
189 else {
190 ATLAS_NOTIMPLEMENTED;
191 }
192 }
193
194
195 void TransIFS::dirtrans_adj(const Field& spfield, Field& gpfield, const eckit::Configuration& config) const {
196 ATLAS_ASSERT(functionspace::Spectral(spfield.functionspace()));
197 if (functionspace::StructuredColumns(gpfield.functionspace())) {
198 __dirtrans_adj(functionspace::Spectral(spfield.functionspace()), spfield,
199 functionspace::StructuredColumns(gpfield.functionspace()), gpfield,
200 config);
201 }
202 else if (functionspace::NodeColumns(gpfield.functionspace())) {
203 __dirtrans_adj(functionspace::Spectral(spfield.functionspace()), spfield,
204 functionspace::NodeColumns(gpfield.functionspace()), gpfield,
205 config);
206 }
207 else {
208 ATLAS_NOTIMPLEMENTED;
209 }
210 }
211
212 void TransIFS::dirtrans_adj(const FieldSet& spfields, FieldSet& gpfields, const eckit::Configuration& config) const {
213 ATLAS_TRACE();
214 assert_spectral_functionspace(spfields);
215 std::string functionspace(fieldset_functionspace(gpfields));
216 if (functionspace == StructuredColumns::type()) {
217 __dirtrans_adj(Spectral(spfields[0].functionspace()), spfields,
218 StructuredColumns(gpfields[0].functionspace()), gpfields,
219 config);
220 }
221 else if (functionspace == NodeColumns::type()) {
222 // __dirtrans_adj(NodeColumns(gpfields[0].functionspace()), gpfields, Spectral(spfields[0].functionspace()), spfields,
223 // config);
224 }
225 else {
226 ATLAS_NOTIMPLEMENTED;
227 }
228 }
229
230 void TransIFS::dirtrans_wind2vordiv_adj(const Field& spvor, const Field& spdiv, Field& gpwind,
231 const eckit::Configuration& config) const {
232 ATLAS_ASSERT(Spectral(spvor.functionspace()));
233 ATLAS_ASSERT(Spectral(spdiv.functionspace()));
234 if (StructuredColumns(gpwind.functionspace())) {
235 __dirtrans_wind2vordiv_adj(Spectral(spvor.functionspace()), spvor, spdiv,
236 StructuredColumns(gpwind.functionspace()), gpwind,
237 config);
238 }
239 else if (NodeColumns(gpwind.functionspace())) {
240 // __dirtrans_wind2vordiv_adj(Spectral(spvor.functionspace()), spvor, spdiv,
241 // NodeColumns(gpwind.functionspace()), gpwind,
242 // config);
243 }
244 else {
245 ATLAS_NOTIMPLEMENTED;
246 }
247 }
248
249 void TransIFS::invtrans(const Field& spfield, Field& gpfield, const eckit::Configuration& config) const {
250 ATLAS_TRACE();
251 ATLAS_ASSERT(Spectral(spfield.functionspace()));
252 if (StructuredColumns(gpfield.functionspace())) {
253 __invtrans(Spectral(spfield.functionspace()), spfield, StructuredColumns(gpfield.functionspace()), gpfield,
254 config);
255 }
256 else if (NodeColumns(gpfield.functionspace())) {
257 __invtrans(Spectral(spfield.functionspace()), spfield, NodeColumns(gpfield.functionspace()), gpfield, config);
258 }
259 else {
260 ATLAS_NOTIMPLEMENTED;
261 }
262 }
263
264 void TransIFS::invtrans(const FieldSet& spfields, FieldSet& gpfields, const eckit::Configuration& config) const {
265 ATLAS_TRACE();
266 assert_spectral_functionspace(spfields);
267 std::string functionspace(fieldset_functionspace(gpfields));
268
269 if (functionspace == StructuredColumns::type()) {
270 __invtrans(Spectral(spfields[0].functionspace()), spfields, StructuredColumns(gpfields[0].functionspace()),
271 gpfields, config);
272 }
273 else if (functionspace == NodeColumns::type()) {
274 __invtrans(Spectral(spfields[0].functionspace()), spfields, NodeColumns(gpfields[0].functionspace()), gpfields,
275 config);
276 }
277 else {
278 ATLAS_NOTIMPLEMENTED;
279 }
280 }
281
282
283 void TransIFS::invtrans_adj(const Field& gpfield, Field& spfield, const eckit::Configuration& config) const {
284 ATLAS_ASSERT(Spectral(spfield.functionspace()));
285 if (StructuredColumns(gpfield.functionspace())) {
286 __invtrans_adj(Spectral(spfield.functionspace()), spfield, StructuredColumns(gpfield.functionspace()), gpfield,
287 config);
288 }
289 else if (NodeColumns(gpfield.functionspace())) {
290 __invtrans_adj(Spectral(spfield.functionspace()), spfield, NodeColumns(gpfield.functionspace()), gpfield,
291 config);
292 }
293 else {
294 ATLAS_NOTIMPLEMENTED;
295 }
296 }
297
298 void TransIFS::invtrans_adj(const FieldSet& gpfields, FieldSet& spfields, const eckit::Configuration& config) const {
299 assert_spectral_functionspace(spfields);
300 std::string functionspace(fieldset_functionspace(gpfields));
301
302 if (functionspace == StructuredColumns::type()) {
303 __invtrans_adj(Spectral(spfields[0].functionspace()), spfields, StructuredColumns(gpfields[0].functionspace()),
304 gpfields, config);
305 }
306 else if (functionspace == NodeColumns::type()) {
307 __invtrans_adj(Spectral(spfields[0].functionspace()), spfields, NodeColumns(gpfields[0].functionspace()),
308 gpfields, config);
309 }
310 else {
311 ATLAS_NOTIMPLEMENTED;
312 }
313 }
314
315 // --------------------------------------------------------------------------------------------
316
317 void TransIFS::invtrans_grad(const Field& spfield, Field& gradfield, const eckit::Configuration& config) const {
318 ATLAS_ASSERT(Spectral(spfield.functionspace()));
319 ATLAS_ASSERT(NodeColumns(gradfield.functionspace()));
320 if (StructuredColumns(gradfield.functionspace())) {
321 __invtrans_grad(Spectral(spfield.functionspace()), spfield, StructuredColumns(gradfield.functionspace()),
322 gradfield, config);
323 }
324 else if (NodeColumns(gradfield.functionspace())) {
325 __invtrans_grad(Spectral(spfield.functionspace()), spfield, NodeColumns(gradfield.functionspace()), gradfield,
326 config);
327 }
328 else {
329 ATLAS_NOTIMPLEMENTED;
330 }
331 }
332
333 void TransIFS::invtrans_grad(const FieldSet& spfields, FieldSet& gradfields, const eckit::Configuration& config) const {
334 assert_spectral_functionspace(spfields);
335 std::string functionspace(fieldset_functionspace(gradfields));
336
337 if (functionspace == StructuredColumns::type()) {
338 __invtrans_grad(Spectral(spfields[0].functionspace()), spfields,
339 StructuredColumns(gradfields[0].functionspace()), gradfields, config);
340 }
341 else if (functionspace == NodeColumns::type()) {
342 __invtrans_grad(Spectral(spfields[0].functionspace()), spfields, NodeColumns(gradfields[0].functionspace()),
343 gradfields, config);
344 }
345 else {
346 ATLAS_NOTIMPLEMENTED;
347 }
348 }
349
350 // --------------------------------------------------------------------------------------------
351
352 void TransIFS::invtrans_grad_adj(const Field& gradfield, Field& spfield, const eckit::Configuration& config) const {
353 ATLAS_ASSERT(Spectral(spfield.functionspace()));
354 ATLAS_ASSERT(NodeColumns(gradfield.functionspace()));
355 if (StructuredColumns(gradfield.functionspace())) {
356 __invtrans_grad_adj(Spectral(spfield.functionspace()), spfield, StructuredColumns(gradfield.functionspace()),
357 gradfield, config);
358 }
359 else if (NodeColumns(gradfield.functionspace())) {
360 __invtrans_grad_adj(Spectral(spfield.functionspace()), spfield, NodeColumns(gradfield.functionspace()),
361 gradfield, config);
362 }
363 else {
364 ATLAS_NOTIMPLEMENTED;
365 }
366 }
367
368 void TransIFS::invtrans_grad_adj(const FieldSet& gradfields, FieldSet& spfields,
369 const eckit::Configuration& config) const {
370 assert_spectral_functionspace(spfields);
371 std::string functionspace(fieldset_functionspace(gradfields));
372
373 if (functionspace == StructuredColumns::type()) {
374 __invtrans_grad_adj(Spectral(spfields[0].functionspace()), spfields,
375 StructuredColumns(gradfields[0].functionspace()), gradfields, config);
376 }
377 else if (functionspace == NodeColumns::type()) {
378 __invtrans_grad_adj(Spectral(spfields[0].functionspace()), spfields, NodeColumns(gradfields[0].functionspace()),
379 gradfields, config);
380 }
381 else {
382 ATLAS_NOTIMPLEMENTED;
383 }
384 }
385
386
387
388 void TransIFS::invtrans_vordiv2wind(const Field& spvor, const Field& spdiv, Field& gpwind,
389 const eckit::Configuration& config) const {
390 ATLAS_ASSERT(Spectral(spvor.functionspace()));
391 ATLAS_ASSERT(Spectral(spdiv.functionspace()));
392 if (StructuredColumns(gpwind.functionspace())) {
393 __invtrans_vordiv2wind(Spectral(spvor.functionspace()), spvor, spdiv, StructuredColumns(gpwind.functionspace()),
394 gpwind, config);
395 }
396 else if (NodeColumns(gpwind.functionspace())) {
397 __invtrans_vordiv2wind(Spectral(spvor.functionspace()), spvor, spdiv, NodeColumns(gpwind.functionspace()),
398 gpwind, config);
399 }
400 else {
401 ATLAS_NOTIMPLEMENTED;
402 }
403 }
404
405
406 void TransIFS::invtrans_vordiv2wind_adj(const Field& gpwind, Field& spvor, Field& spdiv,
407 const eckit::Configuration& config) const {
408 ATLAS_ASSERT(Spectral(spvor.functionspace()));
409 ATLAS_ASSERT(Spectral(spdiv.functionspace()));
410 if (StructuredColumns(gpwind.functionspace())) {
411 __invtrans_vordiv2wind_adj(Spectral(spvor.functionspace()), spvor, spdiv,
412 StructuredColumns(gpwind.functionspace()), gpwind, config);
413 }
414 else if (NodeColumns(gpwind.functionspace())) {
415 __invtrans_vordiv2wind_adj(Spectral(spvor.functionspace()), spvor, spdiv, NodeColumns(gpwind.functionspace()),
416 gpwind, config);
417 }
418 else {
419 ATLAS_NOTIMPLEMENTED;
420 }
421 }
422
423 // --------------------------------------------------------------------------------------------
424
425 void TransIFS::invtrans(const int nb_scalar_fields, const double scalar_spectra[], const int nb_vordiv_fields,
426 const double vorticity_spectra[], const double divergence_spectra[], double gp_fields[],
427 const eckit::Configuration& config) const {
428 ATLAS_TRACE("TransIFS::invtrans");
429 TransParameters params(*this, config);
430 struct ::InvTrans_t args = new_invtrans(trans_.get());
431 args.nscalar = nb_scalar_fields;
432 args.rspscalar = scalar_spectra;
433 args.nvordiv = nb_vordiv_fields;
434 args.rspvor = vorticity_spectra;
435 args.rspdiv = divergence_spectra;
436 args.rgp = gp_fields;
437 args.lglobal = params.global();
438 args.lscalarders = params.scalar_derivatives();
439 args.luvder_EW = params.wind_EW_derivatives();
440 args.lvordivgp = params.vorticity_divergence_fields();
441 args.nproma = params.nproma();
442 args.ngpblks = params.ngpblks();
443 TRANS_CHECK(::trans_invtrans(&args));
444 }
445
446 void TransIFS::invtrans_adj(const int nb_scalar_fields, const double gp_fields[], const int nb_vordiv_fields,
447 double vorticity_spectra[], double divergence_spectra[], double scalar_spectra[],
448 const eckit::Configuration& config) const {
449 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
450 ATLAS_TRACE("TransIFS::invtrans_adj");
451 TransParameters params(*this, config);
452 struct ::InvTransAdj_t args = new_invtrans_adj(trans_.get());
453 args.nscalar = nb_scalar_fields;
454 args.rspscalar = scalar_spectra;
455 args.nvordiv = nb_vordiv_fields;
456 args.rspvor = vorticity_spectra;
457 args.rspdiv = divergence_spectra;
458 args.rgp = gp_fields;
459 args.lglobal = params.global();
460 args.lscalarders = params.scalar_derivatives();
461 args.luvder_EW = params.wind_EW_derivatives();
462 args.lvordivgp = params.vorticity_divergence_fields();
463 args.nproma = params.nproma();
464 args.ngpblks = params.ngpblks();
465 TRANS_CHECK(::trans_invtrans_adj(&args));
466 #else
467 ATLAS_NOTIMPLEMENTED;
468 #endif
469 }
470
471
472 ///////////////////////////////////////////////////////////////////////////////
473
474 void TransIFS::invtrans(const int nb_scalar_fields, const double scalar_spectra[], double gp_fields[],
475 const eckit::Configuration& config) const {
476 return invtrans(nb_scalar_fields, scalar_spectra, 0, nullptr, nullptr, gp_fields, config);
477 }
478
479 ///////////////////////////////////////////////////////////////////////////////
480
481 void TransIFS::invtrans(const int nb_vordiv_fields, const double vorticity_spectra[], const double divergence_spectra[],
482 double gp_fields[], const eckit::Configuration& config) const {
483 return invtrans(0, nullptr, nb_vordiv_fields, vorticity_spectra, divergence_spectra, gp_fields, config);
484 }
485
486 ///////////////////////////////////////////////////////////////////////////////
487
488
489 void TransIFS::invtrans_adj(const int nb_scalar_fields, const double gp_fields[], double scalar_spectra[],
490 const eckit::Configuration& config) const {
491 return invtrans_adj(nb_scalar_fields, gp_fields, 0, nullptr, nullptr, scalar_spectra, config);
492 }
493
494 ///////////////////////////////////////////////////////////////////////////////
495
496
497 void TransIFS::invtrans_adj(const int nb_vordiv_fields, const double gp_fields[], double divergence_spectra[],
498 double vorticity_spectra[], const eckit::Configuration& config) const {
499 return invtrans_adj(0, gp_fields, nb_vordiv_fields, vorticity_spectra, divergence_spectra, nullptr, config);
500 }
501
502
503 void TransIFS::dirtrans(const int nb_fields, const double scalar_fields[], double scalar_spectra[],
504 const eckit::Configuration& config) const {
505 ATLAS_TRACE();
506 TransParameters params(*this, config);
507 struct ::DirTrans_t args = new_dirtrans(trans_.get());
508 args.nscalar = nb_fields;
509 args.rgp = scalar_fields;
510 args.rspscalar = scalar_spectra;
511 args.lglobal = params.global();
512 args.nproma = params.nproma();
513 args.ngpblks = params.ngpblks();
514 TRANS_CHECK(::trans_dirtrans(&args));
515 }
516
517 ///////////////////////////////////////////////////////////////////////////////
518
519 void TransIFS::dirtrans(const int nb_fields, const double wind_fields[], double vorticity_spectra[],
520 double divergence_spectra[], const eckit::Configuration& config) const {
521 ATLAS_TRACE();
522 TransParameters params(*this, config);
523 struct ::DirTrans_t args = new_dirtrans(trans_.get());
524 args.nvordiv = nb_fields;
525 args.rspvor = vorticity_spectra;
526 args.rspdiv = divergence_spectra;
527 args.rgp = wind_fields;
528 args.lglobal = params.global();
529 args.nproma = params.nproma();
530 args.ngpblks = params.ngpblks();
531 TRANS_CHECK(::trans_dirtrans(&args));
532 }
533
534 } // namespace trans
535
536 // anonymous namespace
537 namespace {
538
539 struct PackNodeColumns {
540 LocalView<double, 2>& rgpview_;
541 IsGhostNode is_ghost;
542 size_t f;
543
544 PackNodeColumns(LocalView<double, 2>& rgpview, const NodeColumns& fs):
545 rgpview_(rgpview), is_ghost(fs.nodes()), f(0) {}
546
547 void operator()(const Field& field, idx_t components = 0) {
548 switch (field.rank()) {
549 case 1:
550 pack_1(field, components);
551 break;
552 case 2:
553 pack_2(field, components);
554 break;
555 case 3:
556 pack_3(field, components);
557 break;
558 default:
559 ATLAS_DEBUG_VAR(field.rank());
560 ATLAS_NOTIMPLEMENTED;
561 //break;
562 }
563 }
564
565 void pack_1(const Field& field, idx_t) {
566 auto gpfield = make_view<double, 1>(field);
567 idx_t n = 0;
568 for (idx_t jnode = 0; jnode < gpfield.shape(0); ++jnode) {
569 if (!is_ghost(jnode)) {
570 rgpview_(f, n) = gpfield(jnode);
571 ++n;
572 }
573 }
574 ++f;
575 }
576 void pack_2(const Field& field, idx_t) {
577 auto gpfield = make_view<double, 2>(field);
578 const idx_t nvars = gpfield.shape(1);
579 for (idx_t jvar = 0; jvar < nvars; ++jvar) {
580 idx_t n = 0;
581 for (idx_t jnode = 0; jnode < gpfield.shape(0); ++jnode) {
582 if (!is_ghost(jnode)) {
583 rgpview_(f, n) = gpfield(jnode, jvar);
584 ++n;
585 }
586 }
587 ++f;
588 }
589 }
590 void pack_3(const Field& field, idx_t components) {
591 auto gpfield = make_view<double, 3>(field);
592 if (not components) {
593 components = gpfield.shape(2);
594 }
595 for (idx_t jcomp = 0; jcomp < components; ++jcomp) {
596 for (idx_t jlev = 0; jlev < gpfield.shape(1); ++jlev) {
597 idx_t n = 0;
598 for (idx_t jnode = 0; jnode < gpfield.shape(0); ++jnode) {
599 if (!is_ghost(jnode)) {
600 rgpview_(f, n) = gpfield(jnode, jlev, jcomp);
601 ++n;
602 }
603 }
604 ++f;
605 }
606 }
607 }
608 };
609
610 struct PackStructuredColumns {
611 LocalView<double, 2>& rgpview_;
612 size_t f;
613
614 PackStructuredColumns(LocalView<double, 2>& rgpview): rgpview_(rgpview), f(0) {}
615
616 void operator()(const StructuredColumns& sc, const Field& field, idx_t components = 0) {
617 switch (field.rank()) {
618 case 1:
619 pack_1(sc, field, components);
620 break;
621 case 2:
622 pack_2(sc, field, components);
623 break;
624 case 3:
625 pack_3(sc, field, components);
626 break;
627 default:
628 ATLAS_DEBUG_VAR(field.rank());
629 ATLAS_NOTIMPLEMENTED;
630 //break;
631 }
632 }
633
634 void pack_1(const StructuredColumns& sc, const Field& field, idx_t) {
635 auto gpfield = make_view<double, 1>(field);
636
637 for (idx_t jnode = 0; jnode < sc.sizeOwned(); ++jnode) {
638 rgpview_(f, jnode) = gpfield(jnode);
639 }
640 ++f;
641 }
642 void pack_2(const StructuredColumns& sc, const Field& field, idx_t) {
643 auto gpfield = make_view<double, 2>(field);
644 const idx_t nvars = gpfield.shape(1);
645 for (idx_t jvar = 0; jvar < nvars; ++jvar) {
646 for (idx_t jnode = 0; jnode < sc.sizeOwned(); ++jnode) {
647 rgpview_(f, jnode) = gpfield(jnode, jvar);
648 }
649 ++f;
650 }
651 }
652 void pack_3(const StructuredColumns& sc, const Field& field, idx_t components) {
653 auto gpfield = make_view<double, 3>(field);
654 if (not components) {
655 components = gpfield.shape(2);
656 }
657 for (idx_t jcomp = 0; jcomp < components; ++jcomp) {
658 const idx_t nvars = gpfield.shape(1);
659 for (idx_t jvar = 0; jvar < nvars; ++jvar) {
660 for (idx_t jnode = 0; jnode < sc.sizeOwned(); ++jnode) {
661 rgpview_(f, jnode) = gpfield(jnode, jvar, jcomp);
662 }
663 ++f;
664 }
665 }
666 }
667 };
668
669 struct PackSpectral {
670 LocalView<double, 2>& rspecview_;
671 size_t f;
672 PackSpectral(LocalView<double, 2>& rspecview): rspecview_(rspecview), f(0) {}
673
674 void operator()(const Field& field) {
675 switch (field.rank()) {
676 case 1:
677 pack_1(field);
678 break;
679 case 2:
680 pack_2(field);
681 break;
682 default:
683 ATLAS_DEBUG_VAR(field.rank());
684 ATLAS_NOTIMPLEMENTED;
685 //break;
686 }
687 }
688
689 void pack_1(const Field& field) {
690 auto spfield = make_view<double, 1>(field);
691
692 for (idx_t jwave = 0; jwave < spfield.shape(0); ++jwave) {
693 rspecview_(jwave, f) = spfield(jwave);
694 }
695 ++f;
696 }
697 void pack_2(const Field& field) {
698 auto spfield = make_view<double, 2>(field);
699
700 const idx_t nvars = spfield.shape(1);
701
702 for (idx_t jvar = 0; jvar < nvars; ++jvar) {
703 for (idx_t jwave = 0; jwave < spfield.shape(0); ++jwave) {
704 rspecview_(jwave, f) = spfield(jwave, jvar);
705 }
706 ++f;
707 }
708 }
709 };
710
711 struct UnpackNodeColumns {
712 const LocalView<double, 2>& rgpview_;
713 IsGhostNode is_ghost;
714 size_t f;
715
716 UnpackNodeColumns(const LocalView<double, 2>& rgpview, const NodeColumns& fs):
717 rgpview_(rgpview), is_ghost(fs.nodes()), f(0) {}
718
719 void operator()(Field& field, int components = 0) {
720 switch (field.rank()) {
721 case 1:
722 unpack_1(field, components);
723 break;
724 case 2:
725 unpack_2(field, components);
726 break;
727 case 3:
728 unpack_3(field, components);
729 break;
730 default:
731 ATLAS_DEBUG_VAR(field.rank());
732 ATLAS_NOTIMPLEMENTED;
733 //break;
734 }
735 }
736
737 void unpack_1(Field& field, idx_t) {
738 auto gpfield = make_view<double, 1>(field);
739 idx_t n(0);
740 for (idx_t jnode = 0; jnode < gpfield.shape(0); ++jnode) {
741 if (!is_ghost(jnode)) {
742 gpfield(jnode) = rgpview_(f, n);
743 ++n;
744 }
745 }
746 ++f;
747 }
748 void unpack_2(Field& field, idx_t) {
749 auto gpfield = make_view<double, 2>(field);
750 const idx_t nvars = gpfield.shape(1);
751 for (idx_t jvar = 0; jvar < nvars; ++jvar) {
752 idx_t n = 0;
753 for (idx_t jnode = 0; jnode < gpfield.shape(0); ++jnode) {
754 if (!is_ghost(jnode)) {
755 gpfield(jnode, jvar) = rgpview_(f, n);
756 ++n;
757 }
758 }
759 ++f;
760 }
761 }
762 void unpack_3(Field& field, idx_t components) {
763 auto gpfield = make_view<double, 3>(field);
764 if (not components) {
765 components = gpfield.shape(2);
766 }
767 for (idx_t jcomp = 0; jcomp < components; ++jcomp) {
768 for (idx_t jlev = 0; jlev < gpfield.shape(1); ++jlev) {
769 idx_t n = 0;
770 for (idx_t jnode = 0; jnode < gpfield.shape(0); ++jnode) {
771 if (!is_ghost(jnode)) {
772 gpfield(jnode, jlev, jcomp) = rgpview_(f, n);
773 ++n;
774 }
775 }
776 ++f;
777 }
778 }
779 }
780 };
781
782 struct UnpackStructuredColumns {
783 const LocalView<double, 2>& rgpview_;
784 size_t f;
785
786 UnpackStructuredColumns(const LocalView<double, 2>& rgpview): rgpview_(rgpview), f(0) {}
787
788 void operator()(const StructuredColumns& sc, Field& field, int components = 0) {
789 switch (field.rank()) {
790 case 1:
791 unpack_1(sc, field, components);
792 break;
793 case 2:
794 unpack_2(sc, field, components);
795 break;
796 case 3:
797 unpack_3(sc, field, components);
798 break;
799 default:
800 ATLAS_DEBUG_VAR(field.rank());
801 ATLAS_NOTIMPLEMENTED;
802 //break;
803 }
804 }
805
806 void unpack_1(const StructuredColumns& sc, Field& field, idx_t) {
807 auto gpfield = make_view<double, 1>(field);
808 for (idx_t jnode = 0; jnode < sc.sizeOwned(); ++jnode) {
809 gpfield(jnode) = rgpview_(f, jnode);
810 }
811 ++f;
812 }
813 void unpack_2(const StructuredColumns& sc, Field& field, idx_t) {
814 auto gpfield = make_view<double, 2>(field);
815 const idx_t nvars = gpfield.shape(1);
816 for (idx_t jvar = 0; jvar < nvars; ++jvar) {
817 for (idx_t jnode = 0; jnode < sc.sizeOwned(); ++jnode) {
818 gpfield(jnode, jvar) = rgpview_(f, jnode);
819 }
820 ++f;
821 }
822 }
823 void unpack_3(const StructuredColumns& sc, Field& field, idx_t components) {
824 auto gpfield = make_view<double, 3>(field);
825 if (not components) {
826 components = gpfield.shape(2);
827 }
828 for (idx_t jcomp = 0; jcomp < components; ++jcomp) {
829 for (idx_t jlev = 0; jlev < gpfield.shape(1); ++jlev) {
830 for (idx_t jnode = 0; jnode < sc.sizeOwned(); ++jnode) {
831 gpfield(jnode, jlev, jcomp) = rgpview_(f, jnode);
832 }
833 ++f;
834 }
835 }
836 }
837 };
838
839 struct UnpackSpectral {
840 const LocalView<double, 2>& rspecview_;
841 size_t f;
842 UnpackSpectral(const LocalView<double, 2>& rspecview): rspecview_(rspecview), f(0) {}
843
844 void operator()(Field& field) {
845 switch (field.rank()) {
846 case 1:
847 unpack_1(field);
848 break;
849 case 2:
850 unpack_2(field);
851 break;
852 default:
853 ATLAS_DEBUG_VAR(field.rank());
854 ATLAS_NOTIMPLEMENTED;
855 //break;
856 }
857 }
858
859 void unpack_1(Field& field) {
860 ArrayView<double, 1> spfield = make_view<double, 1>(field);
861
862 for (idx_t jwave = 0; jwave < spfield.shape(0); ++jwave) {
863 spfield(jwave) = rspecview_(jwave, f);
864 }
865 ++f;
866 }
867 void unpack_2(Field& field) {
868 ArrayView<double, 2> spfield = make_view<double, 2>(field);
869
870 const idx_t nvars = spfield.shape(1);
871
872 for (idx_t jvar = 0; jvar < nvars; ++jvar) {
873 for (idx_t jwave = 0; jwave < spfield.shape(0); ++jwave) {
874 spfield(jwave, jvar) = rspecview_(jwave, f);
875 }
876 ++f;
877 }
878 }
879 };
880
881 } // end anonymous namespace
882 } // namespace atlas
883
884 namespace atlas {
885 namespace trans {
886
887 void TransIFS::assertCompatibleDistributions(const FunctionSpace& gp, const FunctionSpace& /*sp*/) const {
888 std::string gp_dist = gp.distribution();
889 if (gp_dist != "ectrans" && // distribution computed by TransPartitioner
890 gp_dist != "serial" && // serial distribution always works
891 gp_dist != "custom") { // trust user that he knows what he is doing
892 throw_Exception(gp.type() + " functionspace has unsupported distribution (" + gp_dist +
893 ") "
894 "to do spectral transforms. Please "
895 "partition grid with TransPartitioner",
896 Here());
897 }
898 }
899
900 TransIFS::TransIFS(const Cache& cache, const Grid& grid, const long truncation, const eckit::Configuration& config):
901 grid_(grid), cache_(cache.legendre().data()), cachesize_(cache.legendre().size()) {
902 ATLAS_ASSERT(grid.domain().global());
903 ATLAS_ASSERT(not grid.projection());
904 ctor(grid, truncation, config);
905 }
906
907 TransIFS::TransIFS(const Grid& grid, const long truncation, const eckit::Configuration& config):
908 TransIFS(Cache(), grid, truncation, config) {}
909
910 TransIFS::TransIFS(const Grid& grid, const eckit::Configuration& config): TransIFS(grid, /*grid-only*/ -1, config) {}
911
912 int atlas::trans::TransIFS::handle() const {
913 return trans_->handle;
914 }
915
916 int atlas::trans::TransIFS::ndgl() const {
917 return trans_->ndgl;
918 }
919
920 int atlas::trans::TransIFS::nsmax() const {
921 return trans_->nsmax;
922 }
923
924 int atlas::trans::TransIFS::ngptot() const {
925 return trans_->ngptot;
926 }
927
928 int atlas::trans::TransIFS::ngptotg() const {
929 return trans_->ngptotg;
930 }
931
932 int atlas::trans::TransIFS::ngptotmx() const {
933 return trans_->ngptotmx;
934 }
935
936 int atlas::trans::TransIFS::nspec() const {
937 return trans_->nspec;
938 }
939
940 int atlas::trans::TransIFS::nspec2() const {
941 return trans_->nspec2;
942 }
943
944 int atlas::trans::TransIFS::nspec2g() const {
945 return trans_->nspec2g;
946 }
947
948 int atlas::trans::TransIFS::nspec2mx() const {
949 return trans_->nspec2mx;
950 }
951
952 int atlas::trans::TransIFS::n_regions_NS() const {
953 return trans_->n_regions_NS;
954 }
955
956 int atlas::trans::TransIFS::n_regions_EW() const {
957 return trans_->n_regions_EW;
958 }
959
960 int atlas::trans::TransIFS::nump() const {
961 return trans_->nump;
962 }
963
964 int atlas::trans::TransIFS::nproc() const {
965 return trans_->nproc;
966 }
967
968 int atlas::trans::TransIFS::myproc(int proc0) const {
969 return trans_->myproc - 1 + proc0;
970 }
971
972 const int* atlas::trans::TransIFS::nloen(int& size) const {
973 size = trans_->ndgl;
974 ATLAS_ASSERT(trans_->nloen != nullptr);
975 return trans_->nloen;
976 }
977
978 const int* atlas::trans::TransIFS::n_regions(int& size) const {
979 size = trans_->n_regions_NS;
980 ATLAS_ASSERT(trans_->n_regions != nullptr);
981 return trans_->n_regions;
982 }
983
984 const int* atlas::trans::TransIFS::nfrstlat(int& size) const {
985 size = trans_->n_regions_NS;
986 if (trans_->nfrstlat == nullptr) {
987 ::trans_inquire(trans_.get(), "nfrstlat");
988 }
989 return trans_->nfrstlat;
990 }
991
992 const int* atlas::trans::TransIFS::nlstlat(int& size) const {
993 size = trans_->n_regions_NS;
994 if (trans_->nlstlat == nullptr) {
995 ::trans_inquire(trans_.get(), "nlstlat");
996 }
997 return trans_->nlstlat;
998 }
999
1000 const int* atlas::trans::TransIFS::nptrfrstlat(int& size) const {
1001 size = trans_->n_regions_NS;
1002 if (trans_->nptrfrstlat == nullptr) {
1003 ::trans_inquire(trans_.get(), "nptrfrstlat");
1004 }
1005 return trans_->nptrfrstlat;
1006 }
1007
1008 const int* atlas::trans::TransIFS::nsta(int& sizef2, int& sizef1) const {
1009 sizef1 = trans_->ndgl + trans_->n_regions_NS - 1;
1010 sizef2 = trans_->n_regions_EW;
1011 if (trans_->nsta == nullptr) {
1012 ::trans_inquire(trans_.get(), "nsta");
1013 }
1014 return trans_->nsta;
1015 }
1016
1017 const int* atlas::trans::TransIFS::nonl(int& sizef2, int& sizef1) const {
1018 sizef1 = trans_->ndgl + trans_->n_regions_NS - 1;
1019 sizef2 = trans_->n_regions_EW;
1020 if (trans_->nonl == nullptr) {
1021 ::trans_inquire(trans_.get(), "nonl");
1022 }
1023 return trans_->nonl;
1024 }
1025
1026 const int* atlas::trans::TransIFS::nmyms(int& size) const {
1027 size = trans_->nump;
1028 if (trans_->nmyms == nullptr) {
1029 ::trans_inquire(trans_.get(), "nmyms");
1030 }
1031 return trans_->nmyms;
1032 }
1033
1034 const int* atlas::trans::TransIFS::nasm0(int& size) const {
1035 size = trans_->nsmax + 1; // +1 because zeroth wave included
1036 if (trans_->nasm0 == nullptr) {
1037 ::trans_inquire(trans_.get(), "nasm0");
1038 }
1039 return trans_->nasm0;
1040 }
1041
1042 const int* atlas::trans::TransIFS::nvalue(int& size) const {
1043 size = trans_->nspec2;
1044 if (trans_->nvalue == nullptr) {
1045 ::trans_inquire(trans_.get(), "nvalue");
1046 }
1047 return trans_->nvalue;
1048 }
1049
1050 array::LocalView<int, 1> atlas::trans::TransIFS::nvalue() const {
1051 if (trans_->nvalue == nullptr) {
1052 ::trans_inquire(trans_.get(), "nvalue");
1053 }
1054 return array::LocalView<int, 1>(trans_->nvalue, array::make_shape(trans_->nspec2));
1055 }
1056
1057 array::LocalView<int, 1> atlas::trans::TransIFS::nasm0() const {
1058 if (trans_->nasm0 == nullptr) {
1059 ::trans_inquire(trans_.get(), "nasm0");
1060 }
1061 return array::LocalView<int, 1>(trans_->nasm0, array::make_shape(trans_->nsmax + 1));
1062 }
1063
1064 array::LocalView<int, 1> atlas::trans::TransIFS::nmyms() const {
1065 if (trans_->nmyms == nullptr) {
1066 ::trans_inquire(trans_.get(), "nmyms");
1067 }
1068 return array::LocalView<int, 1>(trans_->nmyms, array::make_shape(trans_->nump));
1069 }
1070
1071 array::LocalView<int, 2> atlas::trans::TransIFS::nonl() const {
1072 if (trans_->nonl == nullptr) {
1073 ::trans_inquire(trans_.get(), "nonl");
1074 }
1075 return array::LocalView<int, 2>(trans_->nonl,
1076 array::make_shape(trans_->n_regions_EW, trans_->ndgl + trans_->n_regions_NS - 1));
1077 }
1078
1079 array::LocalView<int, 2> atlas::trans::TransIFS::nsta() const {
1080 if (trans_->nsta == nullptr) {
1081 ::trans_inquire(trans_.get(), "nsta");
1082 }
1083 return array::LocalView<int, 2>(trans_->nsta,
1084 array::make_shape(trans_->n_regions_EW, trans_->ndgl + trans_->n_regions_NS - 1));
1085 }
1086
1087 array::LocalView<int, 1> atlas::trans::TransIFS::nptrfrstlat() const {
1088 if (trans_->nptrfrstlat == nullptr) {
1089 ::trans_inquire(trans_.get(), "nptrfrstlat");
1090 }
1091 return array::LocalView<int, 1>(trans_->nptrfrstlat, array::make_shape(trans_->n_regions_NS));
1092 }
1093
1094 array::LocalView<int, 1> atlas::trans::TransIFS::nlstlat() const {
1095 if (trans_->nlstlat == nullptr) {
1096 ::trans_inquire(trans_.get(), "nlstlat");
1097 }
1098 return array::LocalView<int, 1>(trans_->nlstlat, array::make_shape(trans_->n_regions_NS));
1099 }
1100
1101 array::LocalView<int, 1> atlas::trans::TransIFS::nfrstlat() const {
1102 if (trans_->nfrstlat == nullptr) {
1103 ::trans_inquire(trans_.get(), "nfrstlat");
1104 }
1105 return array::LocalView<int, 1>(trans_->nfrstlat, array::make_shape(trans_->n_regions_NS));
1106 }
1107
1108 array::LocalView<int, 1> atlas::trans::TransIFS::n_regions() const {
1109 ATLAS_ASSERT(trans_->n_regions != nullptr);
1110 return array::LocalView<int, 1>(trans_->n_regions, array::make_shape(trans_->n_regions_NS));
1111 }
1112
1113 array::LocalView<int, 1> atlas::trans::TransIFS::nloen() const {
1114 ATLAS_ASSERT(trans_->nloen != nullptr);
1115 return array::LocalView<int, 1>(trans_->nloen, array::make_shape(trans_->ndgl));
1116 }
1117
1118
1119 TransIFS::TransIFS(const Grid& grid, const Domain& domain, const long truncation, const eckit::Configuration& config):
1120 TransIFS(Cache(), grid, truncation, config) {
1121 ATLAS_ASSERT(domain.global());
1122 }
1123
1124 TransIFS::TransIFS(const Cache& cache, const Grid& grid, const Domain& domain, const long truncation,
1125 const eckit::Configuration& config):
1126 TransIFS(cache, grid, truncation, config) {
1127 ATLAS_ASSERT(domain.global());
1128 }
1129
1130 TransIFS::~TransIFS() = default;
1131
1132 int atlas::trans::TransIFS::truncation() const {
1133 return std::max(0, trans_->nsmax);
1134 }
1135
1136 size_t TransIFS::nb_spectral_coefficients() const {
1137 return trans_->nspec2;
1138 }
1139
1140 size_t TransIFS::nb_spectral_coefficients_global() const {
1141 return trans_->nspec2g;
1142 }
1143
1144 const functionspace::Spectral& TransIFS::spectral() const {
1145 if (not spectral_) {
1146 spectral_ = functionspace::Spectral( truncation() );
1147 }
1148 return spectral_;
1149 }
1150
1151 void TransIFS::ctor(const Grid& grid, long truncation, const eckit::Configuration& config) {
1152 trans_ = std::shared_ptr<::Trans_t>(new ::Trans_t, [](::Trans_t* p) {
1153 ::trans_delete(p);
1154 delete p;
1155 });
1156
1157 if (auto gg = GaussianGrid(grid)) {
1158 ctor_rgg(gg.ny(), gg.nx().data(), truncation, config);
1159 return;
1160 }
1161 if (auto ll = RegularLonLatGrid(grid)) {
1162 if (ll.standard() || ll.shifted()) {
1163 ctor_lonlat(ll.nx(), ll.ny(), truncation, config);
1164 return;
1165 }
1166 }
1167 throw_NotImplemented("Grid type not supported for Spectral Transforms", Here());
1168 }
1169
1170 void TransIFS::ctor_rgg(const long nlat, const idx_t pl[], long truncation, const eckit::Configuration& config) {
1171 TransParameters p(*this, config);
1172 std::vector<int> nloen(nlat);
1173 for (long jlat = 0; jlat < nlat; ++jlat) {
1174 nloen[jlat] = pl[jlat];
1175 }
1176
1177 // Set MPI communicator to the same as Atlas. Must be set prior to MPL_INIT in ectrans/fiat.
1178 #if ATLAS_ECTRANS_VERSION_AT_LEAST(1, 8, 0)
1179 TRANS_CHECK(::trans_set_mpi_comm(mpi::comm().communicator()));
1180 #endif
1181 TRANS_CHECK(::trans_new(trans_.get()));
1182 TRANS_CHECK(::trans_use_mpi(mpi::size() > 1));
1183 TRANS_CHECK(::trans_set_resol(trans_.get(), nlat, nloen.data()));
1184 if (truncation >= 0) {
1185 TRANS_CHECK(::trans_set_trunc(trans_.get(), truncation));
1186 }
1187
1188 TRANS_CHECK(::trans_set_cache(trans_.get(), cache_, cachesize_));
1189
1190 if (p.read_legendre().size() && mpi::size() == 1) {
1191 eckit::PathName file(p.read_legendre());
1192 if (not file.exists()) {
1193 std::stringstream msg;
1194 msg << "File " << file << " doesn't exist";
1195 throw_CantOpenFile(msg.str(), Here());
1196 }
1197 TRANS_CHECK(::trans_set_read(trans_.get(), file.asString().c_str()));
1198 }
1199 if (p.write_legendre().size() && mpi::size() == 1) {
1200 eckit::PathName file(p.write_legendre());
1201 TRANS_CHECK(::trans_set_write(trans_.get(), file.asString().c_str()));
1202 }
1203
1204 trans_->fft = p.fft();
1205 trans_->lsplit = p.split_y();
1206 trans_->flt = p.flt();
1207 ATLAS_TRACE_SCOPE("trans_setup") { TRANS_CHECK(::trans_setup(trans_.get())); }
1208 }
1209
1210 void TransIFS::ctor_lonlat(const long nlon, const long nlat, long truncation, const eckit::Configuration& config) {
1211 TransParameters p(*this, config);
1212 #if ATLAS_ECTRANS_VERSION_AT_LEAST(1, 8, 0)
1213 TRANS_CHECK(::trans_set_mpi_comm(mpi::comm().communicator()));
1214 #endif
1215 TRANS_CHECK(::trans_new(trans_.get()));
1216 TRANS_CHECK(::trans_use_mpi(mpi::size() > 1));
1217 TRANS_CHECK(::trans_set_resol_lonlat(trans_.get(), nlon, nlat));
1218 if (truncation >= 0) {
1219 TRANS_CHECK(::trans_set_trunc(trans_.get(), truncation));
1220 }
1221 TRANS_CHECK(::trans_set_cache(trans_.get(), cache_, cachesize_));
1222
1223 if (p.read_legendre().size() && mpi::size() == 1) {
1224 eckit::PathName file(p.read_legendre());
1225 if (not file.exists()) {
1226 std::stringstream msg;
1227 msg << "File " << file << " doesn't exist";
1228 throw_CantOpenFile(msg.str(), Here());
1229 }
1230 TRANS_CHECK(::trans_set_read(trans_.get(), file.asString().c_str()));
1231 }
1232 if (p.write_legendre().size() && mpi::size() == 1) {
1233 eckit::PathName file(p.write_legendre());
1234 TRANS_CHECK(::trans_set_write(trans_.get(), file.asString().c_str()));
1235 }
1236
1237 trans_->fft = p.fft();
1238 trans_->lsplit = p.split_y();
1239 trans_->flt = p.flt();
1240
1241 TRANS_CHECK(::trans_setup(trans_.get()));
1242 }
1243
1244
1245 // --------------------------------------------------------------------------------------------
1246
1247 void TransIFS::__dirtrans(const StructuredColumns& gp, const Field& gpfield, const Spectral& sp, Field& spfield,
1248 const eckit::Configuration&) const {
1249 ATLAS_ASSERT(gpfield.functionspace() == 0 || functionspace::StructuredColumns(gpfield.functionspace()));
1250 ATLAS_ASSERT(spfield.functionspace() == 0 || functionspace::Spectral(spfield.functionspace()));
1251
1252 assertCompatibleDistributions(gp, sp);
1253
1254 if (compute_nfld(gpfield) != compute_nfld(spfield)) {
1255 throw_Exception("dirtrans: different number of gridpoint fields than spectral fields", Here());
1256 }
1257 if ((int)gpfield.shape(0) < ngptot()) {
1258 throw_Exception("dirtrans: slowest moving index must be >= ngptot", Here());
1259 }
1260 const int nfld = compute_nfld(gpfield);
1261
1262 // Arrays Trans expects
1263 std::vector<double> rgp(nfld * ngptot());
1264 std::vector<double> rsp(nspec2() * nfld);
1265 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1266 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1267
1268 // Pack gridpoints
1269 {
1270 PackStructuredColumns pack(rgpview);
1271 pack(gp, gpfield);
1272 }
1273
1274 // Do transform
1275 {
1276 struct ::DirTrans_t transform = ::new_dirtrans(trans_.get());
1277 transform.nscalar = nfld;
1278 transform.rgp = rgp.data();
1279 transform.rspscalar = rsp.data();
1280 TRANS_CHECK(::trans_dirtrans(&transform));
1281 }
1282
1283 // Unpack spectral
1284 {
1285 UnpackSpectral unpack(rspview);
1286 unpack(spfield);
1287 }
1288 }
1289
1290 // --------------------------------------------------------------------------------------------
1291
1292 void TransIFS::__dirtrans(const functionspace::NodeColumns& gp, const Field& gpfield, const Spectral& sp,
1293 Field& spfield, const eckit::Configuration& config) const {
1294 FieldSet gpfields;
1295 gpfields.add(gpfield);
1296 FieldSet spfields;
1297 spfields.add(spfield);
1298 __dirtrans(gp, gpfields, sp, spfields, config);
1299 }
1300
1301 // --------------------------------------------------------------------------------------------
1302
1303 void TransIFS::__dirtrans(const StructuredColumns& gp, const FieldSet& gpfields, const Spectral& sp, FieldSet& spfields,
1304 const eckit::Configuration&) const {
1305 assertCompatibleDistributions(gp, sp);
1306
1307 // Count total number of fields and do sanity checks
1308 const idx_t nfld = compute_nfld(gpfields);
1309 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1310 const Field& f = gpfields[jfld];
1311 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::StructuredColumns(f.functionspace()));
1312 }
1313
1314 const int trans_sp_nfld = compute_nfld(spfields);
1315
1316 if (nfld != trans_sp_nfld) {
1317 throw_Exception("dirtrans: different number of gridpoint fields than spectral fields", Here());
1318 }
1319 // Arrays Trans expects
1320 std::vector<double> rgp(nfld * ngptot());
1321 std::vector<double> rsp(nspec2() * nfld);
1322 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1323 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1324
1325 // Pack gridpoints
1326 {
1327 PackStructuredColumns pack(rgpview);
1328 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1329 pack(gp, gpfields[jfld]);
1330 }
1331 }
1332
1333 // Do transform
1334 {
1335 struct ::DirTrans_t transform = ::new_dirtrans(trans_.get());
1336 transform.nscalar = int(nfld);
1337 transform.rgp = rgp.data();
1338 transform.rspscalar = rsp.data();
1339
1340 TRANS_CHECK(::trans_dirtrans(&transform));
1341 }
1342
1343 // Unpack the spectral fields
1344 {
1345 UnpackSpectral unpack(rspview);
1346 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
1347 unpack(spfields[jfld]);
1348 }
1349 }
1350 }
1351
1352 // --------------------------------------------------------------------------------------------
1353
1354 void TransIFS::__dirtrans(const functionspace::NodeColumns& gp, const FieldSet& gpfields, const Spectral& sp,
1355 FieldSet& spfields, const eckit::Configuration&) const {
1356 assertCompatibleDistributions(gp, sp);
1357
1358 // Count total number of fields and do sanity checks
1359 const int nfld = compute_nfld(gpfields);
1360 const int trans_sp_nfld = compute_nfld(spfields);
1361
1362 if (nfld != trans_sp_nfld) {
1363 throw_Exception("dirtrans: different number of gridpoint fields than spectral fields", Here());
1364 }
1365
1366 // Arrays Trans expects
1367 std::vector<double> rgp(nfld * ngptot());
1368 std::vector<double> rsp(nspec2() * nfld);
1369 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1370 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1371
1372 // Pack gridpoints
1373 {
1374 PackNodeColumns pack(rgpview, gp);
1375 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1376 pack(gpfields[jfld]);
1377 }
1378 }
1379
1380 // Do transform
1381 {
1382 struct ::DirTrans_t transform = ::new_dirtrans(trans_.get());
1383 transform.nscalar = nfld;
1384 transform.rgp = rgp.data();
1385 transform.rspscalar = rsp.data();
1386 TRANS_CHECK(::trans_dirtrans(&transform));
1387 }
1388
1389 // Unpack the spectral fields
1390 {
1391 UnpackSpectral unpack(rspview);
1392 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
1393 unpack(spfields[jfld]);
1394 }
1395 }
1396 }
1397
1398 // --------------------------------------------------------------------------------------------
1399
1400 void TransIFS::__dirtrans_wind2vordiv(const functionspace::StructuredColumns& gp, const Field& gpwind,
1401 const Spectral& sp, Field& spvor, Field& spdiv,
1402 const eckit::Configuration&) const {
1403 assertCompatibleDistributions(gp, sp);
1404
1405 // Count total number of fields and do sanity checks
1406 const size_t nfld = compute_nfld(spvor);
1407 if (spdiv.shape(0) != spvor.shape(0)) {
1408 throw_Exception("dirtrans:vorticity not compatible with divergence for 1st dimension", Here());
1409 }
1410 if (spdiv.shape(1) != spvor.shape(1)) {
1411 throw_Exception("dirtrans:vorticity not compatible with divergence for 2st dimension", Here());
1412 }
1413 const size_t nwindfld = compute_nfld(gpwind);
1414 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
1415 throw_Exception("dirtrans:wind field is not compatible with vorticity, divergence.", Here());
1416 }
1417
1418 if (spdiv.shape(0) != nspec2()) {
1419 std::stringstream msg;
1420 msg << "dirtrans: Spectral vorticity and divergence have wrong dimension: "
1421 "nspec2 "
1422 << spdiv.shape(0) << " should be " << nspec2();
1423 throw_Exception(msg.str(), Here());
1424 }
1425
1426 if (spvor.size() == 0) {
1427 throw_Exception("dirtrans: spectral vorticity field is empty.");
1428 }
1429 if (spdiv.size() == 0) {
1430 throw_Exception("dirtrans: spectral divergence field is empty.");
1431 }
1432
1433 // Arrays Trans expects
1434 std::vector<double> rgp(2 * nfld * ngptot());
1435 std::vector<double> rspvor(nspec2() * nfld);
1436 std::vector<double> rspdiv(nspec2() * nfld);
1437 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
1438 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
1439 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
1440
1441 // Pack gridpoints
1442 {
1443 PackStructuredColumns pack(rgpview);
1444 int wind_components = 2;
1445 pack(gpwind.functionspace(), gpwind, wind_components);
1446 }
1447
1448 // Do transform
1449 {
1450 struct ::DirTrans_t transform = ::new_dirtrans(trans_.get());
1451 transform.nvordiv = int(nfld);
1452 transform.rgp = rgp.data();
1453 transform.rspvor = rspvor.data();
1454 transform.rspdiv = rspdiv.data();
1455
1456 ATLAS_ASSERT(transform.rspvor);
1457 ATLAS_ASSERT(transform.rspdiv);
1458 TRANS_CHECK(::trans_dirtrans(&transform));
1459 }
1460
1461 // Pack spectral fields
1462 UnpackSpectral unpack_vor(rspvorview);
1463 unpack_vor(spvor);
1464 UnpackSpectral unpack_div(rspdivview);
1465 unpack_div(spdiv);
1466 }
1467
1468 // -----------------------------------------------------------------------------------------------
1469
1470 void TransIFS::__dirtrans_wind2vordiv(const functionspace::NodeColumns& gp, const Field& gpwind, const Spectral& sp,
1471 Field& spvor, Field& spdiv, const eckit::Configuration&) const {
1472 assertCompatibleDistributions(gp, sp);
1473
1474 // Count total number of fields and do sanity checks
1475 const size_t nfld = compute_nfld(spvor);
1476 if (spdiv.shape(0) != spvor.shape(0)) {
1477 throw_Exception("dirtrans: vorticity not compatible with divergence for 1st dimension.", Here());
1478 }
1479 if (spdiv.shape(1) != spvor.shape(1)) {
1480 throw_Exception("dirtrans: vorticity not compatible with divergence for 2nd dimension.", Here());
1481 }
1482 const size_t nwindfld = compute_nfld(gpwind);
1483 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
1484 throw_Exception("dirtrans: wind field is not compatible with vorticity, divergence.", Here());
1485 }
1486
1487 if (spdiv.shape(0) != nspec2()) {
1488 std::stringstream msg;
1489 msg << "dirtrans: Spectral vorticity and divergence have wrong dimension: "
1490 "nspec2 "
1491 << spdiv.shape(0) << " should be " << nspec2();
1492 throw_Exception(msg.str(), Here());
1493 }
1494
1495 if (spvor.size() == 0) {
1496 throw_Exception("dirtrans: spectral vorticity field is empty.");
1497 }
1498 if (spdiv.size() == 0) {
1499 throw_Exception("dirtrans: spectral divergence field is empty.");
1500 }
1501
1502 // Arrays Trans expects
1503 std::vector<double> rgp(2 * nfld * ngptot());
1504 std::vector<double> rspvor(nspec2() * nfld);
1505 std::vector<double> rspdiv(nspec2() * nfld);
1506 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
1507 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
1508 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
1509
1510 // Pack gridpoints
1511 {
1512 PackNodeColumns pack(rgpview, gp);
1513 int wind_components = 2;
1514 pack(gpwind, wind_components);
1515 }
1516
1517 // Do transform
1518 {
1519 struct ::DirTrans_t transform = ::new_dirtrans(trans_.get());
1520 transform.nvordiv = int(nfld);
1521 transform.rgp = rgp.data();
1522 transform.rspvor = rspvor.data();
1523 transform.rspdiv = rspdiv.data();
1524
1525 ATLAS_ASSERT(transform.rspvor);
1526 ATLAS_ASSERT(transform.rspdiv);
1527 TRANS_CHECK(::trans_dirtrans(&transform));
1528 }
1529
1530 // Pack spectral fields
1531 UnpackSpectral unpack_vor(rspvorview);
1532 unpack_vor(spvor);
1533 UnpackSpectral unpack_div(rspdivview);
1534 unpack_div(spdiv);
1535 }
1536
1537 // --------------------------------------------------------------------------------------------
1538
1539 void TransIFS::__dirtrans_adj(const Spectral& sp, const FieldSet& spfields,
1540 const StructuredColumns& gp, FieldSet& gpfields,
1541 const eckit::Configuration&) const {
1542 #if ATLAS_HAVE_ECTRANS
1543 assertCompatibleDistributions(gp, sp);
1544
1545 // Count total number of fields and do sanity checks
1546 const idx_t nfld = compute_nfld(gpfields);
1547 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1548 const Field& f = gpfields[jfld];
1549 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::StructuredColumns(f.functionspace()));
1550 }
1551
1552 const int trans_sp_nfld = compute_nfld(spfields);
1553
1554 if (nfld != trans_sp_nfld) {
1555 throw_Exception("dirtrans_adj: different number of gridpoint fields than spectral fields", Here());
1556 }
1557 // Arrays Trans expects
1558 std::vector<double> rgp(nfld * ngptot());
1559 std::vector<double> rsp(nspec2() * nfld);
1560 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1561 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1562
1563 // Pack spectral fields
1564 {
1565 PackSpectral pack(rspview);
1566 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
1567 pack(spfields[jfld]);
1568 }
1569 }
1570
1571 // Do transform
1572 {
1573 struct ::DirTransAdj_t transform = ::new_dirtrans_adj(trans_.get());
1574 transform.nscalar = int(nfld);
1575 transform.rgp = rgp.data();
1576 transform.rspscalar = rsp.data();
1577
1578 TRANS_CHECK(::trans_dirtrans_adj(&transform));
1579 }
1580
1581 // Unpack the gridpoint fields
1582 {
1583 UnpackStructuredColumns unpack(rgpview);
1584 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1585 unpack(gp, gpfields[jfld]);
1586 }
1587 }
1588 #else
1589 ATLAS_NOTIMPLEMENTED;
1590 #endif
1591 }
1592
1593 // --------------------------------------------------------------------------------------------
1594
1595 void TransIFS::__dirtrans_adj( const Spectral& sp, const Field& spfield,
1596 const NodeColumns& gp, Field& gpfield,
1597 const eckit::Configuration& ) const {
1598 #if ATLAS_HAVE_ECTRANS
1599 ATLAS_ASSERT(gpfield.functionspace() == 0 || functionspace::NodeColumns(gpfield.functionspace()));
1600 ATLAS_ASSERT(spfield.functionspace() == 0 || functionspace::Spectral(spfield.functionspace()));
1601
1602 assertCompatibleDistributions(gp, sp);
1603
1604 if (compute_nfld(gpfield) != compute_nfld(spfield)) {
1605 throw_Exception("dirtrans: different number of gridpoint fields than spectral fields", Here());
1606 }
1607 if ((int)gpfield.shape(0) < ngptot()) {
1608 throw_Exception("dirtrans: slowest moving index must be >= ngptot", Here());
1609 }
1610 const int nfld = compute_nfld(gpfield);
1611
1612 // Arrays Trans expects
1613 std::vector<double> rgp(nfld * ngptot());
1614 std::vector<double> rsp(nspec2() * nfld);
1615 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1616 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1617
1618 // Pack spectral fields
1619 {
1620 PackSpectral pack(rspview);
1621 pack(spfield);
1622 }
1623
1624 // Do transform
1625 {
1626 struct ::DirTransAdj_t transform = ::new_dirtrans_adj(trans_.get());
1627 transform.nscalar = nfld;
1628 transform.rgp = rgp.data();
1629 transform.rspscalar = rsp.data();
1630 TRANS_CHECK(::trans_dirtrans_adj(&transform));
1631 }
1632
1633 // Unpack gridpoint fields
1634 {
1635 UnpackNodeColumns unpack(rgpview, gp);
1636 unpack(gpfield);
1637 }
1638 #else
1639 ATLAS_NOTIMPLEMENTED;
1640 #endif
1641 }
1642
1643 // --------------------------------------------------------------------------------------------
1644
1645 void TransIFS::__dirtrans_adj( const Spectral& sp, const Field& spfield,
1646 const StructuredColumns& gp, Field& gpfield,
1647 const eckit::Configuration& ) const {
1648 #if ATLAS_HAVE_ECTRANS
1649 ATLAS_ASSERT(gpfield.functionspace() == 0 || functionspace::StructuredColumns(gpfield.functionspace()));
1650 ATLAS_ASSERT(spfield.functionspace() == 0 || functionspace::Spectral(spfield.functionspace()));
1651
1652 assertCompatibleDistributions(gp, sp);
1653
1654 if (compute_nfld(gpfield) != compute_nfld(spfield)) {
1655 throw_Exception("dirtrans: different number of gridpoint fields than spectral fields", Here());
1656 }
1657 if ((int)gpfield.shape(0) < ngptot()) {
1658 throw_Exception("dirtrans: slowest moving index must be >= ngptot", Here());
1659 }
1660 const int nfld = compute_nfld(gpfield);
1661
1662 // Arrays Trans expects
1663 std::vector<double> rgp(nfld * ngptot());
1664 std::vector<double> rsp(nspec2() * nfld);
1665 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1666 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1667
1668 // Pack spectral fields
1669 {
1670 PackSpectral pack(rspview);
1671 pack(spfield);
1672 }
1673
1674 // Do transform
1675 {
1676 struct ::DirTransAdj_t transform = ::new_dirtrans_adj(trans_.get());
1677 transform.nscalar = nfld;
1678 transform.rgp = rgp.data();
1679 transform.rspscalar = rsp.data();
1680 TRANS_CHECK(::trans_dirtrans_adj(&transform));
1681 }
1682
1683 // Unpack gridpoint fields
1684 {
1685 UnpackStructuredColumns unpack(rgpview);
1686 unpack(gp, gpfield);
1687 }
1688 #else
1689 ATLAS_NOTIMPLEMENTED;
1690 #endif
1691 }
1692
1693
1694 // --------------------------------------------------------------------------------------------
1695
1696 void TransIFS::__dirtrans_adj(const Spectral& sp, const FieldSet& spfields,
1697 const NodeColumns& gp, FieldSet& gpfields,
1698 const eckit::Configuration&) const {
1699 #if ATLAS_HAVE_ECTRANS
1700 assertCompatibleDistributions(gp, sp);
1701
1702 // Count total number of fields and do sanity checks
1703 const idx_t nfld = compute_nfld(gpfields);
1704 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1705 const Field& f = gpfields[jfld];
1706 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::NodeColumns(f.functionspace()));
1707 }
1708
1709 const int trans_sp_nfld = compute_nfld(spfields);
1710
1711 if (nfld != trans_sp_nfld) {
1712 throw_Exception("dirtrans_adj: different number of gridpoint fields than spectral fields", Here());
1713 }
1714 // Arrays Trans expects
1715 std::vector<double> rgp(nfld * ngptot());
1716 std::vector<double> rsp(nspec2() * nfld);
1717 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1718 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1719
1720 // Pack spectral fields
1721 {
1722 PackSpectral pack(rspview);
1723 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
1724 pack(spfields[jfld]);
1725 }
1726 }
1727
1728 // Do transform
1729 {
1730 struct ::DirTransAdj_t transform = ::new_dirtrans_adj(trans_.get());
1731 transform.nscalar = int(nfld);
1732 transform.rgp = rgp.data();
1733 transform.rspscalar = rsp.data();
1734
1735 TRANS_CHECK(::trans_dirtrans_adj(&transform));
1736 }
1737
1738 // Unpack the gridpoint fields
1739 {
1740 UnpackNodeColumns unpack(rgpview, gp);
1741 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1742 unpack(gpfields[jfld]);
1743 }
1744 }
1745 #else
1746 ATLAS_NOTIMPLEMENTED;
1747 #endif
1748 }
1749
1750 // --------------------------------------------------------------------------------------------
1751
1752 void TransIFS::__dirtrans_wind2vordiv_adj(const Spectral& sp, const Field& spvor, const Field& spdiv,
1753 const functionspace::StructuredColumns& gp, Field& gpwind,
1754 const eckit::Configuration&) const {
1755 #if ATLAS_HAVE_ECTRANS
1756 assertCompatibleDistributions(gp, sp);
1757
1758 // Count total number of fields and do sanity checks
1759 const size_t nfld = compute_nfld(spvor);
1760 if (spdiv.shape(0) != spvor.shape(0)) {
1761 throw_Exception("dirtrans_adj:vorticity not compatible with divergence for 1st dimension", Here());
1762 }
1763 if (spdiv.shape(1) != spvor.shape(1)) {
1764 throw_Exception("dirtrans_adj:vorticity not compatible with divergence for 2st dimension", Here());
1765 }
1766 const size_t nwindfld = compute_nfld(gpwind);
1767 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
1768 throw_Exception("dirtrans_adj:wind field is not compatible with vorticity, divergence.", Here());
1769 }
1770
1771 if (spdiv.shape(0) != nspec2()) {
1772 std::stringstream msg;
1773 msg << "dirtrans_adj: Spectral vorticity and divergence have wrong dimension: "
1774 "nspec2 "
1775 << spdiv.shape(0) << " should be " << nspec2();
1776 throw_Exception(msg.str(), Here());
1777 }
1778
1779 if (spvor.size() == 0) {
1780 throw_Exception("dirtrans_adj: spectral vorticity field is empty.");
1781 }
1782 if (spdiv.size() == 0) {
1783 throw_Exception("dirtrans_adj: spectral divergence field is empty.");
1784 }
1785
1786 // Arrays Trans expects
1787 std::vector<double> rgp(2 * nfld * ngptot());
1788 std::vector<double> rspvor(nspec2() * nfld);
1789 std::vector<double> rspdiv(nspec2() * nfld);
1790 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
1791 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
1792 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
1793
1794 // Pack spectral fields
1795 PackSpectral pack_vor(rspvorview);
1796 pack_vor(spvor);
1797 PackSpectral pack_div(rspdivview);
1798 pack_div(spdiv);
1799
1800 // Do transform
1801 {
1802 struct ::DirTransAdj_t transform = ::new_dirtrans_adj(trans_.get());
1803 transform.nvordiv = int(nfld);
1804 transform.rgp = rgp.data();
1805 transform.rspvor = rspvor.data();
1806 transform.rspdiv = rspdiv.data();
1807
1808 ATLAS_ASSERT(transform.rspvor);
1809 ATLAS_ASSERT(transform.rspdiv);
1810 TRANS_CHECK(::trans_dirtrans_adj(&transform));
1811 }
1812
1813 // Unpack the gridpoint fields
1814 {
1815 UnpackStructuredColumns unpack(rgpview);
1816 int wind_components = 2;
1817 unpack(gp, gpwind, wind_components);
1818 }
1819 #else
1820 ATLAS_NOTIMPLEMENTED;
1821 #endif
1822 }
1823
1824 // --------------------------------------------------------------------------------------------
1825
1826 void TransIFS::__dirtrans_wind2vordiv_adj(const Spectral& sp, const Field& spvor, const Field& spdiv,
1827 const functionspace::NodeColumns& gp, Field& gpwind,
1828 const eckit::Configuration&) const {
1829 #if ATLAS_HAVE_ECTRANS
1830 assertCompatibleDistributions(gp, sp);
1831
1832 // Count total number of fields and do sanity checks
1833 const size_t nfld = compute_nfld(spvor);
1834 if (spdiv.shape(0) != spvor.shape(0)) {
1835 throw_Exception("dirtrans_adj:vorticity not compatible with divergence for 1st dimension", Here());
1836 }
1837 if (spdiv.shape(1) != spvor.shape(1)) {
1838 throw_Exception("dirtrans_adj:vorticity not compatible with divergence for 2st dimension", Here());
1839 }
1840 const size_t nwindfld = compute_nfld(gpwind);
1841 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
1842 throw_Exception("dirtrans_adj:wind field is not compatible with vorticity, divergence.", Here());
1843 }
1844
1845 if (spdiv.shape(0) != nspec2()) {
1846 std::stringstream msg;
1847 msg << "dirtrans_adj: Spectral vorticity and divergence have wrong dimension: "
1848 "nspec2 "
1849 << spdiv.shape(0) << " should be " << nspec2();
1850 throw_Exception(msg.str(), Here());
1851 }
1852
1853 if (spvor.size() == 0) {
1854 throw_Exception("dirtrans_adj: spectral vorticity field is empty.");
1855 }
1856 if (spdiv.size() == 0) {
1857 throw_Exception("dirtrans_adj: spectral divergence field is empty.");
1858 }
1859
1860 // Arrays Trans expects
1861 std::vector<double> rgp(2 * nfld * ngptot());
1862 std::vector<double> rspvor(nspec2() * nfld);
1863 std::vector<double> rspdiv(nspec2() * nfld);
1864 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
1865 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
1866 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
1867
1868 // Pack spectral fields
1869 PackSpectral pack_vor(rspvorview);
1870 pack_vor(spvor);
1871 PackSpectral pack_div(rspdivview);
1872 pack_div(spdiv);
1873
1874 // Do transform
1875 {
1876 struct ::DirTransAdj_t transform = ::new_dirtrans_adj(trans_.get());
1877 transform.nvordiv = int(nfld);
1878 transform.rgp = rgp.data();
1879 transform.rspvor = rspvor.data();
1880 transform.rspdiv = rspdiv.data();
1881
1882 ATLAS_ASSERT(transform.rspvor);
1883 ATLAS_ASSERT(transform.rspdiv);
1884 TRANS_CHECK(::trans_dirtrans_adj(&transform));
1885 }
1886
1887 // Unpack the gridpoint fields
1888 {
1889 UnpackNodeColumns unpack(rgpview, gp);
1890 int wind_components = 2;
1891 unpack(gpwind, wind_components);
1892 }
1893 #else
1894 ATLAS_NOTIMPLEMENTED;
1895 #endif
1896 }
1897
1898
1899
1900 // --------------------------------------------------------------------------------------------
1901
1902 void TransIFS::__invtrans(const functionspace::Spectral& sp, const Field& spfield,
1903 const functionspace::StructuredColumns& gp, Field& gpfield,
1904 const eckit::Configuration& config) const {
1905 assertCompatibleDistributions(gp, sp);
1906
1907 ATLAS_ASSERT(gpfield.functionspace() == 0 || functionspace::StructuredColumns(gpfield.functionspace()));
1908 ATLAS_ASSERT(spfield.functionspace() == 0 || functionspace::Spectral(spfield.functionspace()));
1909 if (compute_nfld(gpfield) != compute_nfld(spfield)) {
1910 throw_Exception("invtrans: different number of gridpoint fields than spectral fields", Here());
1911 }
1912 if ((int)gpfield.shape(0) < ngptot()) {
1913 throw_Exception("invtrans: slowest moving index must be >= ngptot", Here());
1914 }
1915 const int nfld = compute_nfld(gpfield);
1916
1917 // Arrays Trans expects
1918 std::vector<double> rgp(nfld * ngptot());
1919 std::vector<double> rsp(nspec2() * nfld);
1920 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1921 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1922
1923 // Pack spectral fields
1924 {
1925 PackSpectral pack(rspview);
1926 pack(spfield);
1927 }
1928
1929 // Do transform
1930 {
1931 struct ::InvTrans_t transform = ::new_invtrans(trans_.get());
1932 transform.nscalar = nfld;
1933 transform.rgp = rgp.data();
1934 transform.rspscalar = rsp.data();
1935 TRANS_CHECK(::trans_invtrans(&transform));
1936 }
1937
1938 // Unpack gridpoint fields
1939 {
1940 UnpackStructuredColumns unpack(rgpview);
1941 unpack(gp, gpfield);
1942 }
1943 }
1944
1945 // --------------------------------------------------------------------------------------------
1946
1947 void TransIFS::__invtrans(const Spectral& sp, const Field& spfield, const functionspace::NodeColumns& gp,
1948 Field& gpfield, const eckit::Configuration& config) const {
1949 FieldSet spfields;
1950 spfields.add(spfield);
1951 FieldSet gpfields;
1952 gpfields.add(gpfield);
1953 __invtrans(sp, spfields, gp, gpfields, config);
1954 }
1955
1956 // --------------------------------------------------------------------------------------------
1957
1958 void TransIFS::__invtrans(const functionspace::Spectral& sp, const FieldSet& spfields,
1959 const functionspace::StructuredColumns& gp, FieldSet& gpfields,
1960 const eckit::Configuration& config) const {
1961 assertCompatibleDistributions(gp, sp);
1962
1963 // Count total number of fields and do sanity checks
1964 const int nfld = compute_nfld(gpfields);
1965 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
1966 const Field& f = gpfields[jfld];
1967 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::StructuredColumns(f.functionspace()));
1968 }
1969
1970 const int nb_spectral_fields = compute_nfld(spfields);
1971
1972 if (nfld != nb_spectral_fields) {
1973 std::stringstream msg;
1974 msg << "invtrans: different number of gridpoint fields than spectral fields"
1975 << "[ " << nfld << " != " << nb_spectral_fields << " ]";
1976 throw_Exception(msg.str(), Here());
1977 }
1978
1979 // Arrays Trans expects
1980 std::vector<double> rgp(nfld * ngptot());
1981 std::vector<double> rsp(nspec2() * nfld);
1982 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
1983 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
1984
1985 // Pack spectral fields
1986 {
1987 PackSpectral pack(rspview);
1988 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
1989 pack(spfields[jfld]);
1990 }
1991 }
1992
1993 // Do transform
1994 {
1995 struct ::InvTrans_t transform = ::new_invtrans(trans_.get());
1996 transform.nscalar = nfld;
1997 transform.rgp = rgp.data();
1998 transform.rspscalar = rsp.data();
1999
2000 TRANS_CHECK(::trans_invtrans(&transform));
2001 }
2002
2003 // Unpack the gridpoint fields
2004 {
2005 UnpackStructuredColumns unpack(rgpview);
2006 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
2007 unpack(gp, gpfields[jfld]);
2008 }
2009 }
2010 }
2011
2012 // --------------------------------------------------------------------------------------------
2013
2014 void TransIFS::__invtrans(const Spectral& sp, const FieldSet& spfields, const functionspace::NodeColumns& gp,
2015 FieldSet& gpfields, const eckit::Configuration& config) const {
2016 assertCompatibleDistributions(gp, sp);
2017
2018
2019 // Count total number of fields and do sanity checks
2020 const int nfld = compute_nfld(gpfields);
2021 const int nb_spectral_fields = compute_nfld(spfields);
2022
2023 if (nfld != nb_spectral_fields) {
2024 throw_Exception("invtrans: different number of gridpoint fields than spectral fields", Here());
2025 }
2026
2027 // Arrays Trans expects
2028 std::vector<double> rgp(nfld * ngptot());
2029 std::vector<double> rsp(nspec2() * nfld);
2030 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
2031 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2032
2033 // Pack spectral fields
2034 {
2035 PackSpectral pack(rspview);
2036 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
2037 pack(spfields[jfld]);
2038 }
2039 }
2040
2041 // Do transform
2042 {
2043 struct ::InvTrans_t transform = ::new_invtrans(trans_.get());
2044 transform.nscalar = nfld;
2045 transform.rgp = rgp.data();
2046 transform.rspscalar = rsp.data();
2047 TRANS_CHECK(::trans_invtrans(&transform));
2048 }
2049
2050 // Unpack the gridpoint fields
2051 {
2052 UnpackNodeColumns unpack(rgpview, gp);
2053 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
2054 unpack(gpfields[jfld]);
2055 }
2056 }
2057 }
2058
2059 // --------------------------------------------------------------------------------------------
2060
2061 void TransIFS::__invtrans_grad(const Spectral& sp, const Field& spfield, const functionspace::StructuredColumns& gp,
2062 Field& gradfield, const eckit::Configuration& config) const {
2063 FieldSet spfields;
2064 spfields.add(spfield);
2065 FieldSet gradfields;
2066 gradfields.add(gradfield);
2067 __invtrans_grad(sp, spfields, gp, gradfields, config);
2068 }
2069
2070 // --------------------------------------------------------------------------------------------
2071
2072 void TransIFS::__invtrans_grad(const Spectral& sp, const Field& spfield, const functionspace::NodeColumns& gp,
2073 Field& gradfield, const eckit::Configuration& config) const {
2074 FieldSet spfields;
2075 spfields.add(spfield);
2076 FieldSet gradfields;
2077 gradfields.add(gradfield);
2078 __invtrans_grad(sp, spfields, gp, gradfields, config);
2079 }
2080
2081 // --------------------------------------------------------------------------------------------
2082
2083 void TransIFS::__invtrans_grad(const Spectral& sp, const FieldSet& spfields, const functionspace::StructuredColumns& gp,
2084 FieldSet& gradfields, const eckit::Configuration& config) const {
2085 assertCompatibleDistributions(gp, sp);
2086
2087 // Count total number of fields and do sanity checks
2088 const int nb_gridpoint_field = compute_nfld(gradfields);
2089 const int nfld = compute_nfld(spfields);
2090
2091 if (nb_gridpoint_field != 2 * nfld) { // factor 2 because N-S and E-W derivatives
2092 throw_Exception(
2093 "invtrans_grad: different number of gridpoint "
2094 "fields than spectral fields",
2095 Here());
2096 }
2097
2098 // Arrays Trans expects
2099 // Allocate space for
2100 std::vector<double> rgp(3 * nfld * ngptot()); // (scalars) + (NS ders) + (EW ders)
2101 std::vector<double> rsp(nspec2() * nfld);
2102 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(3 * nfld, ngptot()));
2103 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2104
2105
2106 // Pack spectral fields
2107 {
2108 PackSpectral pack(rspview);
2109 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
2110 pack(spfields[jfld]);
2111 }
2112 }
2113
2114 // Do transform
2115 {
2116 struct ::InvTrans_t transform = ::new_invtrans(trans_.get());
2117 transform.nscalar = nfld;
2118 transform.rgp = rgp.data();
2119 transform.rspscalar = rsp.data();
2120 transform.lscalarders = true;
2121
2122 TRANS_CHECK(::trans_invtrans(&transform));
2123 }
2124
2125 // Unpack the gridpoint fields
2126 {
2127 int f = nfld; // skip to where derivatives start
2128 for (idx_t dim = 0; dim < 2; ++dim) {
2129 for (idx_t jfld = 0; jfld < gradfields.size(); ++jfld) {
2130 const idx_t nb_nodes = StructuredColumns(gradfields[jfld].functionspace()).sizeOwned();
2131 const idx_t nlev = gradfields[jfld].levels();
2132 if (nlev) {
2133 auto field = make_view<double, 3>(gradfields[jfld]);
2134 for (idx_t jlev = 0; jlev < nlev; ++jlev) {
2135 for (idx_t jnode = 0; jnode < nb_nodes; ++jnode) {
2136 field(jnode, jlev, 1 - dim) = rgpview(f, jnode);
2137 }
2138 }
2139 }
2140 else {
2141 auto field = make_view<double, 2>(gradfields[jfld]);
2142 for (idx_t jnode = 0; jnode < nb_nodes; ++jnode) {
2143 field(jnode, 1 - dim) = rgpview(f, jnode);
2144 }
2145 }
2146 ++f;
2147 }
2148 }
2149 }
2150 }
2151
2152 // --------------------------------------------------------------------------------------------
2153
2154 void TransIFS::__invtrans_grad(const Spectral& sp, const FieldSet& spfields, const functionspace::NodeColumns& gp,
2155 FieldSet& gradfields, const eckit::Configuration& config) const {
2156 assertCompatibleDistributions(gp, sp);
2157
2158 // Count total number of fields and do sanity checks
2159 const int nb_gridpoint_field = compute_nfld(gradfields);
2160 const int nfld = compute_nfld(spfields);
2161
2162 if (nb_gridpoint_field != 2 * nfld) { // factor 2 because N-S and E-W derivatives
2163 throw_Exception(
2164 "invtrans_grad: different number of gridpoint "
2165 "fields than spectral fields",
2166 Here());
2167 }
2168
2169 // Arrays Trans expects
2170 // Allocate space for
2171 std::vector<double> rgp(3 * nfld * ngptot()); // (scalars) + (NS ders) + (EW ders)
2172 std::vector<double> rsp(nspec2() * nfld);
2173 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(3 * nfld, ngptot()));
2174 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2175
2176
2177 // Pack spectral fields
2178 {
2179 PackSpectral pack(rspview);
2180 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
2181 pack(spfields[jfld]);
2182 }
2183 }
2184
2185 // Do transform
2186 {
2187 struct ::InvTrans_t transform = ::new_invtrans(trans_.get());
2188 transform.nscalar = nfld;
2189 transform.rgp = rgp.data();
2190 transform.rspscalar = rsp.data();
2191 transform.lscalarders = true;
2192
2193 TRANS_CHECK(::trans_invtrans(&transform));
2194 }
2195
2196 // Unpack the gridpoint fields
2197 {
2198 mesh::IsGhostNode is_ghost(gp.nodes());
2199 int f = nfld; // skip to where derivatives start
2200 for (idx_t dim = 0; dim < 2; ++dim) {
2201 for (idx_t jfld = 0; jfld < gradfields.size(); ++jfld) {
2202 const idx_t nb_nodes = gradfields[jfld].shape(0);
2203 const idx_t nlev = gradfields[jfld].levels();
2204 if (nlev) {
2205 auto field = make_view<double, 3>(gradfields[jfld]);
2206 for (idx_t jlev = 0; jlev < nlev; ++jlev) {
2207 int n = 0;
2208 for (idx_t jnode = 0; jnode < nb_nodes; ++jnode) {
2209 if (!is_ghost(jnode)) {
2210 field(jnode, jlev, 1 - dim) = rgpview(f, n);
2211 ++n;
2212 }
2213 }
2214 ATLAS_ASSERT(n == ngptot());
2215 }
2216 }
2217 else {
2218 auto field = make_view<double, 2>(gradfields[jfld]);
2219 int n = 0;
2220 for (idx_t jnode = 0; jnode < nb_nodes; ++jnode) {
2221 if (!is_ghost(jnode)) {
2222 field(jnode, 1 - dim) = rgpview(f, n);
2223 ++n;
2224 }
2225 }
2226 ATLAS_ASSERT(n == ngptot());
2227 }
2228 ++f;
2229 }
2230 }
2231 }
2232 }
2233
2234 // --------------------------------------------------------------------------------------------
2235 void TransIFS::__invtrans_vordiv2wind(const Spectral& sp, const Field& spvor, const Field& spdiv,
2236 const functionspace::StructuredColumns& gp, Field& gpwind,
2237 const eckit::Configuration&) const {
2238 assertCompatibleDistributions(gp, sp);
2239
2240 // Count total number of fields and do sanity checks
2241 const int nfld = compute_nfld(spvor);
2242 if (spdiv.shape(0) != spvor.shape(0)) {
2243 throw_Exception("invtrans: vorticity not compatible with divergence.", Here());
2244 }
2245 if (spdiv.shape(1) != spvor.shape(1)) {
2246 throw_Exception("invtrans: vorticity not compatible with divergence.", Here());
2247 }
2248 const int nwindfld = compute_nfld(gpwind);
2249 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
2250 throw_Exception("invtrans: wind field is not compatible with vorticity, divergence.", Here());
2251 }
2252
2253 if (spdiv.shape(0) != nspec2()) {
2254 std::stringstream msg;
2255 msg << "invtrans: Spectral vorticity and divergence have wrong dimension: "
2256 "nspec2 "
2257 << spdiv.shape(0) << " should be " << nspec2();
2258 throw_Exception(msg.str(), Here());
2259 }
2260
2261 ATLAS_ASSERT(spvor.rank() == 2);
2262 ATLAS_ASSERT(spdiv.rank() == 2);
2263 if (spvor.size() == 0) {
2264 throw_Exception("invtrans: spectral vorticity field is empty.");
2265 }
2266 if (spdiv.size() == 0) {
2267 throw_Exception("invtrans: spectral divergence field is empty.");
2268 }
2269
2270 // Arrays Trans expects
2271 std::vector<double> rgp(2 * nfld * ngptot());
2272 std::vector<double> rspvor(nspec2() * nfld);
2273 std::vector<double> rspdiv(nspec2() * nfld);
2274 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
2275 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
2276 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
2277
2278 // Pack spectral fields
2279 PackSpectral pack_vor(rspvorview);
2280 pack_vor(spvor);
2281 PackSpectral pack_div(rspdivview);
2282 pack_div(spdiv);
2283
2284 // Do transform
2285 {
2286 struct ::InvTrans_t transform = ::new_invtrans(trans_.get());
2287 transform.nvordiv = nfld;
2288 transform.rgp = rgp.data();
2289 transform.rspvor = rspvor.data();
2290 transform.rspdiv = rspdiv.data();
2291
2292 ATLAS_ASSERT(transform.rspvor);
2293 ATLAS_ASSERT(transform.rspdiv);
2294 TRANS_CHECK(::trans_invtrans(&transform));
2295 }
2296
2297 // Unpack the gridpoint fields
2298 {
2299 UnpackStructuredColumns unpack(rgpview);
2300 int wind_components = 2;
2301 unpack(gp, gpwind, wind_components);
2302 }
2303 }
2304
2305 //---------------------------------------------------------------------------------------------
2306
2307 void TransIFS::__invtrans_vordiv2wind(const Spectral& sp, const Field& spvor, const Field& spdiv,
2308 const functionspace::NodeColumns& gp, Field& gpwind,
2309 const eckit::Configuration&) const {
2310 assertCompatibleDistributions(gp, sp);
2311
2312 // Count total number of fields and do sanity checks
2313 const int nfld = compute_nfld(spvor);
2314 if (spdiv.shape(0) != spvor.shape(0)) {
2315 throw_Exception("invtrans: vorticity not compatible with divergence.", Here());
2316 }
2317 if (spdiv.shape(1) != spvor.shape(1)) {
2318 throw_Exception("invtrans: vorticity not compatible with divergence.", Here());
2319 }
2320 const int nwindfld = compute_nfld(gpwind);
2321 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
2322 throw_Exception("invtrans: wind field is not compatible with vorticity, divergence.", Here());
2323 }
2324
2325 if (spdiv.shape(0) != nspec2()) {
2326 std::stringstream msg;
2327 msg << "invtrans: Spectral vorticity and divergence have wrong dimension: "
2328 "nspec2 "
2329 << spdiv.shape(0) << " should be " << nspec2();
2330 throw_Exception(msg.str(), Here());
2331 }
2332
2333 ATLAS_ASSERT(spvor.rank() == 2);
2334 ATLAS_ASSERT(spdiv.rank() == 2);
2335 if (spvor.size() == 0) {
2336 throw_Exception("invtrans: spectral vorticity field is empty.");
2337 }
2338 if (spdiv.size() == 0) {
2339 throw_Exception("invtrans: spectral divergence field is empty.");
2340 }
2341
2342 // Arrays Trans expects
2343 std::vector<double> rgp(2 * nfld * ngptot());
2344 std::vector<double> rspvor(nspec2() * nfld);
2345 std::vector<double> rspdiv(nspec2() * nfld);
2346 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
2347 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
2348 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
2349
2350 // Pack spectral fields
2351 PackSpectral pack_vor(rspvorview);
2352 pack_vor(spvor);
2353 PackSpectral pack_div(rspdivview);
2354 pack_div(spdiv);
2355
2356 // Do transform
2357 {
2358 struct ::InvTrans_t transform = ::new_invtrans(trans_.get());
2359 transform.nvordiv = nfld;
2360 transform.rgp = rgp.data();
2361 transform.rspvor = rspvor.data();
2362 transform.rspdiv = rspdiv.data();
2363
2364 ATLAS_ASSERT(transform.rspvor);
2365 ATLAS_ASSERT(transform.rspdiv);
2366 TRANS_CHECK(::trans_invtrans(&transform));
2367 }
2368
2369 // Unpack the gridpoint fields
2370 {
2371 UnpackNodeColumns unpack(rgpview, gp);
2372 int wind_components = 2;
2373 unpack(gpwind, wind_components);
2374 }
2375 }
2376
2377 // --------------------------------------------------------------------------------------------
2378
2379 void TransIFS::__invtrans_adj(const functionspace::Spectral& sp, Field& spfield,
2380 const functionspace::StructuredColumns& gp, const Field& gpfield,
2381 const eckit::Configuration& config) const {
2382 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
2383
2384 ATLAS_ASSERT(gpfield.functionspace() == 0 || functionspace::StructuredColumns(gpfield.functionspace()));
2385 ATLAS_ASSERT(spfield.functionspace() == 0 || functionspace::Spectral(spfield.functionspace()));
2386
2387 assertCompatibleDistributions(gp, sp);
2388
2389 if (compute_nfld(gpfield) != compute_nfld(spfield)) {
2390 throw_Exception("dirtrans: different number of gridpoint fields than spectral fields", Here());
2391 }
2392 if ((int)gpfield.shape(0) < ngptot()) {
2393 throw_Exception("dirtrans: slowest moving index must be >= ngptot", Here());
2394 }
2395 const int nfld = compute_nfld(gpfield);
2396
2397 // Arrays Trans expects
2398 std::vector<double> rgp(nfld * ngptot());
2399 std::vector<double> rsp(nspec2() * nfld);
2400 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
2401 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2402
2403 // Pack gridpoints
2404 {
2405 PackStructuredColumns pack(rgpview);
2406 pack(gp, gpfield);
2407 }
2408
2409 // Do transform
2410 {
2411 struct ::InvTransAdj_t transform = ::new_invtrans_adj(trans_.get());
2412 transform.nscalar = nfld;
2413 transform.rgp = rgp.data();
2414 transform.rspscalar = rsp.data();
2415 TRANS_CHECK(::trans_invtrans_adj(&transform));
2416 }
2417
2418 // Unpack spectral
2419 {
2420 UnpackSpectral unpack(rspview);
2421 unpack(spfield);
2422 }
2423
2424 #else
2425 ATLAS_NOTIMPLEMENTED;
2426 #endif
2427 }
2428
2429 // --------------------------------------------------------------------------------------------
2430
2431 void TransIFS::__invtrans_adj(const Spectral& sp, Field& spfield, const functionspace::NodeColumns& gp,
2432 const Field& gpfield, const eckit::Configuration& config) const {
2433 FieldSet spfields;
2434 spfields.add(spfield);
2435 FieldSet gpfields;
2436 gpfields.add(gpfield);
2437 __invtrans_adj(sp, spfields, gp, gpfields, config);
2438 }
2439
2440 // --------------------------------------------------------------------------------------------
2441
2442 void TransIFS::__invtrans_adj(const functionspace::Spectral& sp, FieldSet& spfields,
2443 const functionspace::StructuredColumns& gp, const FieldSet& gpfields,
2444 const eckit::Configuration& config) const {
2445 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
2446
2447 assertCompatibleDistributions(gp, sp);
2448
2449 // Count total number of fields and do sanity checks
2450 const idx_t nfld = compute_nfld(gpfields);
2451 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
2452 const Field& f = gpfields[jfld];
2453 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::StructuredColumns(f.functionspace()));
2454 }
2455
2456 const int trans_sp_nfld = compute_nfld(spfields);
2457
2458 if (nfld != trans_sp_nfld) {
2459 throw_Exception("invtrans_adj: different number of gridpoint fields than spectral fields", Here());
2460 }
2461
2462 // Arrays Trans expects
2463 std::vector<double> rgp(nfld * ngptot());
2464 std::vector<double> rsp(nspec2() * nfld);
2465 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
2466 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2467
2468 // Pack gridpoints
2469 {
2470 PackStructuredColumns pack(rgpview);
2471 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
2472 pack(gp, gpfields[jfld]);
2473 }
2474 }
2475
2476 // Do transform
2477 {
2478 struct ::InvTransAdj_t transform = ::new_invtrans_adj(trans_.get());
2479 transform.nscalar = int(nfld);
2480 transform.rgp = rgp.data();
2481 transform.rspscalar = rsp.data();
2482
2483 TRANS_CHECK(::trans_invtrans_adj(&transform));
2484 }
2485
2486 // Unpack the spectral fields
2487 {
2488 UnpackSpectral unpack(rspview);
2489 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
2490 unpack(spfields[jfld]);
2491 }
2492 }
2493
2494 #else
2495 ATLAS_NOTIMPLEMENTED;
2496 #endif
2497 }
2498
2499 // --------------------------------------------------------------------------------------------
2500
2501 void TransIFS::__invtrans_adj(const Spectral& sp, FieldSet& spfields, const functionspace::NodeColumns& gp,
2502 const FieldSet& gpfields, const eckit::Configuration& config) const {
2503 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
2504
2505 assertCompatibleDistributions(gp, sp);
2506
2507 // Count total number of fields and do sanity checks
2508 const idx_t nfld = compute_nfld(gpfields);
2509 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
2510 const Field& f = gpfields[jfld];
2511 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::StructuredColumns(f.functionspace()));
2512 }
2513
2514 const int trans_sp_nfld = compute_nfld(spfields);
2515
2516 if (nfld != trans_sp_nfld) {
2517 throw_Exception("invtrans_adj: different number of gridpoint fields than spectral fields", Here());
2518 }
2519 // Arrays Trans expects
2520 std::vector<double> rgp(nfld * ngptot());
2521 std::vector<double> rsp(nspec2() * nfld);
2522 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
2523 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2524
2525 // Pack gridpoints
2526 {
2527 PackNodeColumns pack(rgpview, gp);
2528 for (idx_t jfld = 0; jfld < gpfields.size(); ++jfld) {
2529 pack(gpfields[jfld]);
2530 }
2531 }
2532
2533 // Do transform
2534 {
2535 struct ::InvTransAdj_t transform = ::new_invtrans_adj(trans_.get());
2536 transform.nscalar = int(nfld);
2537 transform.rgp = rgp.data();
2538 transform.rspscalar = rsp.data();
2539
2540 TRANS_CHECK(::trans_invtrans_adj(&transform));
2541 }
2542
2543 // Unpack the spectral fields
2544 {
2545 UnpackSpectral unpack(rspview);
2546 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
2547 unpack(spfields[jfld]);
2548 }
2549 }
2550 #else
2551 ATLAS_NOTIMPLEMENTED;
2552 #endif
2553 }
2554
2555 //---------------------------------------------------------------------------------------------
2556
2557 void TransIFS::__invtrans_grad_adj(const Spectral& sp, Field& spfield, const functionspace::StructuredColumns& gp,
2558 const Field& gradfield, const eckit::Configuration& config) const {
2559 FieldSet spfields;
2560 spfields.add(spfield);
2561 FieldSet gradfields;
2562 gradfields.add(gradfield);
2563 __invtrans_grad_adj(sp, spfields, gp, gradfields, config);
2564 }
2565
2566 //---------------------------------------------------------------------------------------------
2567
2568 void TransIFS::__invtrans_grad_adj(const Spectral& sp, Field& spfield, const functionspace::NodeColumns& gp,
2569 const Field& gradfield, const eckit::Configuration& config) const {
2570 FieldSet spfields;
2571 spfields.add(spfield);
2572 FieldSet gradfields;
2573 gradfields.add(gradfield);
2574 __invtrans_grad_adj(sp, spfields, gp, gradfields, config);
2575 }
2576
2577 //---------------------------------------------------------------------------------------------
2578
2579 void TransIFS::__invtrans_grad_adj(const Spectral& sp, FieldSet& spfields, const functionspace::StructuredColumns& gp,
2580 const FieldSet& gradfields, const eckit::Configuration& config) const {
2581 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
2582 assertCompatibleDistributions(gp, sp);
2583
2584 // Count total number of fields and do sanity checks
2585 const idx_t nfld = compute_nfld(gradfields);
2586 for (idx_t jfld = 0; jfld < gradfields.size(); ++jfld) {
2587 const Field& f = gradfields[jfld];
2588 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::StructuredColumns(f.functionspace()));
2589 }
2590
2591 const int trans_sp_nfld = compute_nfld(spfields);
2592
2593 if (nfld != trans_sp_nfld) {
2594 throw_Exception("__invtrans_grad_adj: different number of gridpoint fields than spectral fields", Here());
2595 }
2596 // Arrays Trans expects
2597 std::vector<double> rgp(nfld * ngptot());
2598 std::vector<double> rsp(nspec2() * nfld);
2599 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
2600 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2601
2602 // Pack gridpoints
2603 {
2604 PackStructuredColumns pack(rgpview);
2605 for (idx_t jfld = 0; jfld < gradfields.size(); ++jfld) {
2606 pack(gp, gradfields[jfld]);
2607 }
2608 }
2609
2610 // Do transform
2611 {
2612 struct ::InvTransAdj_t transform = ::new_invtrans_adj(trans_.get());
2613 transform.nscalar = nfld;
2614 transform.rgp = rgp.data();
2615 transform.rspscalar = rsp.data();
2616 transform.lscalarders = true;
2617
2618 TRANS_CHECK(::trans_invtrans_adj(&transform));
2619 }
2620
2621 // Unpack the spectral fields
2622 {
2623 UnpackSpectral unpack(rspview);
2624 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
2625 unpack(spfields[jfld]);
2626 }
2627 }
2628 #else
2629 ATLAS_NOTIMPLEMENTED;
2630 #endif
2631 }
2632
2633 //---------------------------------------------------------------------------------------------
2634
2635 void TransIFS::__invtrans_grad_adj(const Spectral& sp, FieldSet& spfields, const functionspace::NodeColumns& gp,
2636 const FieldSet& gradfields, const eckit::Configuration& config) const {
2637 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
2638 assertCompatibleDistributions(gp, sp);
2639
2640 // Count total number of fields and do sanity checks
2641 const idx_t nfld = compute_nfld(gradfields);
2642 for (idx_t jfld = 0; jfld < gradfields.size(); ++jfld) {
2643 const Field& f = gradfields[jfld];
2644 ATLAS_ASSERT(f.functionspace() == 0 || functionspace::StructuredColumns(f.functionspace()));
2645 }
2646
2647 const int trans_sp_nfld = compute_nfld(spfields);
2648
2649 if (nfld != trans_sp_nfld) {
2650 throw_Exception("dirtrans: different number of gridpoint fields than spectral fields", Here());
2651 }
2652 // Arrays Trans expects
2653 std::vector<double> rgp(nfld * ngptot());
2654 std::vector<double> rsp(nspec2() * nfld);
2655 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(nfld, ngptot()));
2656 auto rspview = LocalView<double, 2>(rsp.data(), make_shape(nspec2(), nfld));
2657
2658 // Pack gridpoints
2659 {
2660 PackNodeColumns pack(rgpview, gp);
2661 for (idx_t jfld = 0; jfld < gradfields.size(); ++jfld) {
2662 pack(gradfields[jfld]);
2663 }
2664 }
2665
2666 // Do transform
2667 {
2668 struct ::InvTransAdj_t transform = ::new_invtrans_adj(trans_.get());
2669 transform.nscalar = nfld;
2670 transform.rgp = rgp.data();
2671 transform.rspscalar = rsp.data();
2672 transform.lscalarders = true;
2673
2674 TRANS_CHECK(::trans_invtrans_adj(&transform));
2675 }
2676
2677 // Unpack the spectral fields
2678 {
2679 UnpackSpectral unpack(rspview);
2680 for (idx_t jfld = 0; jfld < spfields.size(); ++jfld) {
2681 unpack(spfields[jfld]);
2682 }
2683 }
2684 #else
2685 ATLAS_NOTIMPLEMENTED;
2686 #endif
2687 }
2688
2689 // --------------------------------------------------------------------------------------------
2690
2691 void TransIFS::__invtrans_vordiv2wind_adj(const Spectral& sp, Field& spvor, Field& spdiv,
2692 const functionspace::StructuredColumns& gp, const Field& gpwind,
2693 const eckit::Configuration&) const {
2694 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
2695
2696 assertCompatibleDistributions(gp, sp);
2697
2698 // Count total number of fields and do sanity checks
2699 const size_t nfld = compute_nfld(spvor);
2700 if (spdiv.shape(0) != spvor.shape(0)) {
2701 throw_Exception("invtrans_vordiv2wind_adj: vorticity not compatible with divergence.", Here());
2702 }
2703 if (spdiv.shape(1) != spvor.shape(1)) {
2704 throw_Exception("invtrans_vordiv2wind_adj: vorticity not compatible with divergence.", Here());
2705 }
2706 const size_t nwindfld = compute_nfld(gpwind);
2707 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
2708 throw_Exception("dirtrans: wind field is not compatible with vorticity, divergence.", Here());
2709 }
2710
2711 if (spdiv.shape(0) != nspec2()) {
2712 std::stringstream msg;
2713 msg << "invtrans_vordiv2wind_adj: Spectral vorticity and divergence have wrong dimension: "
2714 "nspec2 "
2715 << spdiv.shape(0) << " should be " << nspec2();
2716 throw_Exception(msg.str(), Here());
2717 }
2718
2719 if (spvor.size() == 0) {
2720 throw_Exception("invtrans_vordiv2wind_adj: spectral vorticity field is empty.");
2721 }
2722 if (spdiv.size() == 0) {
2723 throw_Exception("invtrans_vordiv2wind_adj: spectral divergence field is empty.");
2724 }
2725
2726 // Arrays Trans expects
2727 std::vector<double> rgp(2 * nfld * ngptot());
2728 std::vector<double> rspvor(nspec2() * nfld);
2729 std::vector<double> rspdiv(nspec2() * nfld);
2730 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
2731 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
2732 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
2733
2734 // Pack gridpoints
2735 {
2736 PackStructuredColumns pack(rgpview);
2737 int wind_components = 2;
2738 pack(gp, gpwind, wind_components);
2739 }
2740
2741 // Do transform
2742 {
2743 struct ::InvTransAdj_t transform = ::new_invtrans_adj(trans_.get());
2744 transform.nvordiv = int(nfld);
2745 transform.rgp = rgp.data();
2746 transform.rspvor = rspvor.data();
2747 transform.rspdiv = rspdiv.data();
2748
2749 ATLAS_ASSERT(transform.rspvor);
2750 ATLAS_ASSERT(transform.rspdiv);
2751 TRANS_CHECK(::trans_invtrans_adj(&transform));
2752 }
2753
2754 // Pack spectral fields
2755 UnpackSpectral unpack_vor(rspvorview);
2756 unpack_vor(spvor);
2757 UnpackSpectral unpack_div(rspdivview);
2758 unpack_div(spdiv);
2759
2760 #else
2761 ATLAS_NOTIMPLEMENTED;
2762 #endif
2763 }
2764
2765 void TransIFS::__invtrans_vordiv2wind_adj(const Spectral& sp, Field& spvor, Field& spdiv,
2766 const functionspace::NodeColumns& gp, const Field& gpwind,
2767 const eckit::Configuration&) const {
2768 #if ATLAS_HAVE_ECTRANS || defined(TRANS_HAVE_INVTRANS_ADJ)
2769
2770 assertCompatibleDistributions(gp, sp);
2771
2772 // Count total number of fields and do sanity checks
2773 const size_t nfld = compute_nfld(spvor);
2774 if (spdiv.shape(0) != spvor.shape(0)) {
2775 throw_Exception("invtrans_vordiv2wind_adj: vorticity not compatible with divergence.", Here());
2776 }
2777 if (spdiv.shape(1) != spvor.shape(1)) {
2778 throw_Exception("invtrans_vordiv2wind_adj: vorticity not compatible with divergence.", Here());
2779 }
2780 const size_t nwindfld = compute_nfld(gpwind);
2781 if (nwindfld != 2 * nfld && nwindfld != 3 * nfld) {
2782 throw_Exception("dirtrans: wind field is not compatible with vorticity, divergence.", Here());
2783 }
2784
2785 if (spdiv.shape(0) != nspec2()) {
2786 std::stringstream msg;
2787 msg << "invtrans_vordiv2wind_adj: Spectral vorticity and divergence have wrong dimension: "
2788 "nspec2 "
2789 << spdiv.shape(0) << " should be " << nspec2();
2790 throw_Exception(msg.str(), Here());
2791 }
2792
2793 if (spvor.size() == 0) {
2794 throw_Exception("invtrans_vordiv2wind_adj: spectral vorticity field is empty.");
2795 }
2796 if (spdiv.size() == 0) {
2797 throw_Exception("invtrans_vordiv2wind_adj: spectral divergence field is empty.");
2798 }
2799
2800 // Arrays Trans expects
2801 std::vector<double> rgp(2 * nfld * ngptot());
2802 std::vector<double> rspvor(nspec2() * nfld);
2803 std::vector<double> rspdiv(nspec2() * nfld);
2804 auto rgpview = LocalView<double, 2>(rgp.data(), make_shape(2 * nfld, ngptot()));
2805 auto rspvorview = LocalView<double, 2>(rspvor.data(), make_shape(nspec2(), nfld));
2806 auto rspdivview = LocalView<double, 2>(rspdiv.data(), make_shape(nspec2(), nfld));
2807
2808 // Pack gridpoints
2809 {
2810 PackNodeColumns pack(rgpview, gp);
2811 int wind_components = 2;
2812 pack(gpwind, wind_components);
2813 }
2814
2815 // Do transform
2816 {
2817 struct ::InvTransAdj_t transform = ::new_invtrans_adj(trans_.get());
2818 transform.nvordiv = int(nfld);
2819 transform.rgp = rgp.data();
2820 transform.rspvor = rspvor.data();
2821 transform.rspdiv = rspdiv.data();
2822
2823 ATLAS_ASSERT(transform.rspvor);
2824 ATLAS_ASSERT(transform.rspdiv);
2825 TRANS_CHECK(::trans_invtrans_adj(&transform));
2826 }
2827
2828 // Pack spectral fields
2829 UnpackSpectral unpack_vor(rspvorview);
2830 unpack_vor(spvor);
2831 UnpackSpectral unpack_div(rspdivview);
2832 unpack_div(spdiv);
2833
2834 #else
2835 ATLAS_NOTIMPLEMENTED;
2836 #endif
2837 }
2838
2839 ///////////////////////////////////////////////////////////////////////////////
2840
2841 void TransIFS::distspec(const int nb_fields, const int origin[], const double global_spectra[],
2842 double spectra[]) const {
2843 struct ::DistSpec_t args = new_distspec(trans_.get());
2844 args.nfld = nb_fields;
2845 args.rspecg = global_spectra;
2846 args.nfrom = origin;
2847 args.rspec = spectra;
2848 TRANS_CHECK(::trans_distspec(&args));
2849 }
2850
2851 /////////////////////////////////////////////////////////////////////////////
2852
2853 void TransIFS::gathspec(const int nb_fields, const int destination[], const double spectra[],
2854 double global_spectra[]) const {
2855 struct ::GathSpec_t args = new_gathspec(trans_.get());
2856 args.nfld = nb_fields;
2857 args.rspecg = global_spectra;
2858 args.nto = destination;
2859 args.rspec = spectra;
2860 TRANS_CHECK(::trans_gathspec(&args));
2861 }
2862
2863 /////////////////////////////////////////////////////////////////////////////
2864
2865 void TransIFS::distgrid(const int nb_fields, const int origin[], const double global_fields[], double fields[]) const {
2866 struct ::DistGrid_t args = new_distgrid(trans_.get());
2867 args.nfld = nb_fields;
2868 args.nfrom = origin;
2869 args.rgpg = global_fields;
2870 args.rgp = fields;
2871 TRANS_CHECK(::trans_distgrid(&args));
2872 }
2873
2874 /////////////////////////////////////////////////////////////////////////////
2875
2876 void TransIFS::gathgrid(const int nb_fields, const int destination[], const double fields[],
2877 double global_fields[]) const {
2878 struct ::GathGrid_t args = new_gathgrid(trans_.get());
2879 args.nfld = nb_fields;
2880 args.nto = destination;
2881 args.rgp = fields;
2882 args.rgpg = global_fields;
2883 TRANS_CHECK(::trans_gathgrid(&args));
2884 }
2885
2886 ///////////////////////////////////////////////////////////////////////////////
2887
2888 void TransIFS::specnorm(const int nb_fields, const double spectra[], double norms[], int rank) const {
2889 struct ::SpecNorm_t args = new_specnorm(trans_.get());
2890 args.nfld = nb_fields;
2891 args.rspec = spectra;
2892 args.rnorm = norms;
2893 args.nmaster = rank + 1;
2894 TRANS_CHECK(::trans_specnorm(&args));
2895 }
2896
2897 ///////////////////////////////////////////////////////////////////////////////
2898
2899 } // namespace trans
2900 } // namespace atlas