| 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 "TransInterface.h" |
| 12 |
|
| 13 |
#include <cstring> |
| 14 |
|
| 15 |
#include "atlas/array.h" |
| 16 |
#include "atlas/field/Field.h" |
| 17 |
#include "atlas/field/FieldSet.h" |
| 18 |
#include "atlas/functionspace/Spectral.h" |
| 19 |
#include "atlas/library/config.h" |
| 20 |
#include "atlas/mesh/Nodes.h" |
| 21 |
#include "atlas/runtime/Exception.h" |
| 22 |
#include "atlas/runtime/Log.h" |
| 23 |
#include "atlas/trans/Trans.h" |
| 24 |
#include "atlas/trans/detail/TransFactory.h" |
| 25 |
#include "atlas/trans/detail/TransImpl.h" |
| 26 |
|
| 27 |
namespace atlas { |
| 28 |
namespace trans { |
| 29 |
|
| 30 |
class TransInterface { |
| 31 |
private: |
| 32 |
const TransImpl* trans_; |
| 33 |
|
| 34 |
public: |
| 35 |
TransInterface(const TransImpl* trans): trans_(trans) {} |
| 36 |
int handle() const { return trans_->handle(); } |
| 37 |
}; |
| 38 |
|
| 39 |
/////////////////////////////////////////////////////////////////////////////// |
| 40 |
|
| 41 |
extern "C" { |
| 42 |
|
| 43 |
int atlas__Trans__has_backend(const char* backend) { |
| 44 |
return Trans::hasBackend(std::string(backend)); |
| 45 |
} |
| 46 |
void atlas__Trans__set_backend(const char* backend) { |
| 47 |
Trans::backend(std::string(backend)); |
| 48 |
} |
| 49 |
void atlas__Trans__backend(char*& backend, size_t& size) { |
| 50 |
std::string s = Trans::backend(); |
| 51 |
size = s.size(); |
| 52 |
backend = new char[size + 1]; |
| 53 |
std::strncpy(backend, s.c_str(), size + 1); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
TransImpl* atlas__Trans__new(const Grid::Implementation* grid, int truncation) { |
| 58 |
ATLAS_ASSERT(grid != nullptr, "Grid must not be null"); |
| 59 |
TransImpl* trans; |
| 60 |
{ |
| 61 |
Grid g(grid); |
| 62 |
Trans t(g, truncation); |
| 63 |
trans = t.get(); |
| 64 |
trans->attach(); |
| 65 |
} |
| 66 |
trans->detach(); |
| 67 |
return trans; |
| 68 |
} |
| 69 |
|
| 70 |
TransImpl* atlas__Trans__new_config(const Grid::Implementation* grid, int truncation, |
| 71 |
const eckit::Configuration* config) { |
| 72 |
ATLAS_ASSERT(grid != nullptr, "Grid must not be null"); |
| 73 |
ATLAS_ASSERT(config != nullptr, "config must not be null"); |
| 74 |
TransImpl* trans; |
| 75 |
{ |
| 76 |
Grid g(grid); |
| 77 |
Trans t(g, truncation, *config); |
| 78 |
trans = t.get(); |
| 79 |
trans->attach(); |
| 80 |
} |
| 81 |
trans->detach(); |
| 82 |
return trans; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void atlas__Trans__delete(TransImpl* This) { |
| 87 |
ATLAS_ASSERT(This != nullptr); |
| 88 |
delete This; |
| 89 |
} |
| 90 |
|
| 91 |
int atlas__Trans__handle(const TransImpl* This) { |
| 92 |
ATLAS_ASSERT(This != nullptr); |
| 93 |
return TransInterface(This).handle(); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
void atlas__Trans__invtrans_scalar(const TransImpl* t, int nb_fields, double scalar_spectra[], double scalar_fields[]) { |
| 98 |
ATLAS_ASSERT(t != nullptr); |
| 99 |
return t->invtrans(nb_fields, scalar_spectra, scalar_fields); |
| 100 |
} |
| 101 |
|
| 102 |
void atlas__Trans__invtrans_vordiv2wind(const TransImpl* t, int nb_fields, double vorticity_spectra[], |
| 103 |
double divergence_spectra[], double wind_fields[]) { |
| 104 |
ATLAS_ASSERT(t != nullptr); |
| 105 |
return t->invtrans(nb_fields, vorticity_spectra, divergence_spectra, wind_fields); |
| 106 |
} |
| 107 |
|
| 108 |
void atlas__Trans__invtrans_adj_scalar(const TransImpl* t, int nb_fields, double scalar_fields[], |
| 109 |
double scalar_spectra[]) { |
| 110 |
ATLAS_ASSERT(t != nullptr); |
| 111 |
return t->invtrans_adj(nb_fields, scalar_fields, scalar_spectra); |
| 112 |
} |
| 113 |
|
| 114 |
void atlas__Trans__invtrans_vordiv2wind_adj(const TransImpl* t, int nb_fields, double wind_fields[], |
| 115 |
double vorticity_spectra[], double divergence_spectra[]) { |
| 116 |
ATLAS_ASSERT(t != nullptr); |
| 117 |
return t->invtrans_adj(nb_fields, wind_fields, vorticity_spectra, divergence_spectra); |
| 118 |
} |
| 119 |
|
| 120 |
void atlas__Trans__dirtrans_scalar(const TransImpl* t, int nb_fields, double scalar_fields[], double scalar_spectra[]) { |
| 121 |
ATLAS_ASSERT(t != nullptr); |
| 122 |
return t->dirtrans(nb_fields, scalar_fields, scalar_spectra); |
| 123 |
} |
| 124 |
|
| 125 |
void atlas__Trans__dirtrans_wind2vordiv(const TransImpl* t, int nb_fields, double wind_fields[], |
| 126 |
double vorticity_spectra[], double divergence_spectra[]) { |
| 127 |
ATLAS_ASSERT(t != nullptr); |
| 128 |
return t->dirtrans(nb_fields, wind_fields, vorticity_spectra, divergence_spectra); |
| 129 |
} |
| 130 |
|
| 131 |
int atlas__Trans__truncation(const TransImpl* This) { |
| 132 |
ATLAS_ASSERT(This != nullptr); |
| 133 |
return This->truncation(); |
| 134 |
} |
| 135 |
|
| 136 |
const Grid::Implementation* atlas__Trans__grid(const TransImpl* This) { |
| 137 |
ATLAS_ASSERT(This != nullptr); |
| 138 |
ATLAS_ASSERT(This->grid()); |
| 139 |
return This->grid().get(); |
| 140 |
} |
| 141 |
|
| 142 |
const functionspace::FunctionSpaceImpl* atlas__Trans__spectral(const TransImpl* This) { |
| 143 |
ATLAS_ASSERT(This != nullptr); |
| 144 |
const auto spectral = This->spectral(); |
| 145 |
ATLAS_ASSERT(spectral); |
| 146 |
return spectral.get(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
void atlas__Trans__dirtrans_fieldset(const TransImpl* This, const field::FieldSetImpl* gpfields, |
| 151 |
field::FieldSetImpl* spfields, const eckit::Configuration* parameters) { |
| 152 |
ATLAS_ASSERT(This != nullptr); |
| 153 |
ATLAS_ASSERT(gpfields); |
| 154 |
ATLAS_ASSERT(spfields); |
| 155 |
ATLAS_ASSERT(parameters); |
| 156 |
FieldSet fspfields(spfields); |
| 157 |
This->dirtrans(gpfields, fspfields, *parameters); |
| 158 |
} |
| 159 |
|
| 160 |
void atlas__Trans__dirtrans_field(const TransImpl* This, const field::FieldImpl* gpfield, field::FieldImpl* spfield, |
| 161 |
const eckit::Configuration* parameters) { |
| 162 |
ATLAS_ASSERT(This != nullptr); |
| 163 |
ATLAS_ASSERT(spfield); |
| 164 |
ATLAS_ASSERT(gpfield); |
| 165 |
ATLAS_ASSERT(parameters); |
| 166 |
Field fspfield(spfield); |
| 167 |
This->dirtrans(gpfield, fspfield, *parameters); |
| 168 |
} |
| 169 |
|
| 170 |
void atlas__Trans__invtrans_fieldset(const TransImpl* This, const field::FieldSetImpl* spfields, |
| 171 |
field::FieldSetImpl* gpfields, const eckit::Configuration* parameters) { |
| 172 |
ATLAS_ASSERT(This != nullptr); |
| 173 |
ATLAS_ASSERT(spfields); |
| 174 |
ATLAS_ASSERT(gpfields); |
| 175 |
ATLAS_ASSERT(parameters); |
| 176 |
FieldSet fgpfields(gpfields); |
| 177 |
This->invtrans(spfields, fgpfields, *parameters); |
| 178 |
} |
| 179 |
|
| 180 |
void atlas__Trans__invtrans_field(const TransImpl* This, const field::FieldImpl* spfield, field::FieldImpl* gpfield, |
| 181 |
const eckit::Configuration* parameters) { |
| 182 |
ATLAS_ASSERT(This != nullptr); |
| 183 |
ATLAS_ASSERT(spfield); |
| 184 |
ATLAS_ASSERT(gpfield); |
| 185 |
ATLAS_ASSERT(parameters); |
| 186 |
Field fgpfield(gpfield); |
| 187 |
This->invtrans(spfield, fgpfield, *parameters); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
void atlas__Trans__invtrans_adj_fieldset(const TransImpl* This, const field::FieldSetImpl* gpfields, |
| 192 |
field::FieldSetImpl* spfields, const eckit::Configuration* parameters) { |
| 193 |
ATLAS_ASSERT(This != nullptr); |
| 194 |
ATLAS_ASSERT(spfields); |
| 195 |
ATLAS_ASSERT(gpfields); |
| 196 |
ATLAS_ASSERT(parameters); |
| 197 |
FieldSet fspfields(spfields); |
| 198 |
This->invtrans_adj(gpfields, fspfields, *parameters); |
| 199 |
} |
| 200 |
|
| 201 |
void atlas__Trans__invtrans_adj_field(const TransImpl* This, const field::FieldImpl* spfield, field::FieldImpl* gpfield, |
| 202 |
const eckit::Configuration* parameters) { |
| 203 |
ATLAS_ASSERT(This != nullptr); |
| 204 |
ATLAS_ASSERT(spfield); |
| 205 |
ATLAS_ASSERT(gpfield); |
| 206 |
ATLAS_ASSERT(parameters); |
| 207 |
Field fspfield(spfield); |
| 208 |
This->invtrans_adj(gpfield, fspfield, *parameters); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
void atlas__Trans__dirtrans_wind2vordiv_field(const TransImpl* This, const field::FieldImpl* gpwind, |
| 213 |
field::FieldImpl* spvor, field::FieldImpl* spdiv, |
| 214 |
const eckit::Configuration* parameters) { |
| 215 |
ATLAS_ASSERT(This != nullptr); |
| 216 |
ATLAS_ASSERT(gpwind); |
| 217 |
ATLAS_ASSERT(spvor); |
| 218 |
ATLAS_ASSERT(spdiv); |
| 219 |
ATLAS_ASSERT(parameters); |
| 220 |
Field fspvor(spvor); |
| 221 |
Field fspdiv(spdiv); |
| 222 |
This->dirtrans_wind2vordiv(gpwind, fspvor, fspdiv, *parameters); |
| 223 |
} |
| 224 |
|
| 225 |
void atlas__Trans__invtrans_vordiv2wind_field(const TransImpl* This, const field::FieldImpl* spvor, |
| 226 |
const field::FieldImpl* spdiv, field::FieldImpl* gpwind, |
| 227 |
const eckit::Configuration* parameters) { |
| 228 |
ATLAS_ASSERT(This != nullptr); |
| 229 |
ATLAS_ASSERT(spvor); |
| 230 |
ATLAS_ASSERT(spdiv); |
| 231 |
ATLAS_ASSERT(gpwind); |
| 232 |
ATLAS_ASSERT(parameters); |
| 233 |
Field fgpwind(gpwind); |
| 234 |
This->invtrans_vordiv2wind(spvor, spdiv, fgpwind, *parameters); |
| 235 |
} |
| 236 |
|
| 237 |
void atlas__Trans__invtrans(const TransImpl* This, int nb_scalar_fields, double scalar_spectra[], int nb_vordiv_fields, |
| 238 |
double vorticity_spectra[], double divergence_spectra[], double gp_fields[], |
| 239 |
const eckit::Configuration* parameters) { |
| 240 |
ATLAS_ASSERT(This != nullptr); |
| 241 |
This->invtrans(nb_scalar_fields, scalar_spectra, nb_vordiv_fields, vorticity_spectra, divergence_spectra, gp_fields, |
| 242 |
*parameters); |
| 243 |
} |
| 244 |
|
| 245 |
void atlas__Trans__invtrans_grad_field(const TransImpl* This, const field::FieldImpl* spfield, |
| 246 |
field::FieldImpl* gpfield, const eckit::Configuration* config) { |
| 247 |
ATLAS_ASSERT(This != nullptr); |
| 248 |
ATLAS_ASSERT(spfield); |
| 249 |
ATLAS_ASSERT(gpfield); |
| 250 |
Field fgpfield(gpfield); |
| 251 |
This->invtrans_grad(spfield, fgpfield, *config); |
| 252 |
} |
| 253 |
|
| 254 |
void atlas__Trans__invtrans_vordiv2wind_adj_field(const TransImpl* This, const field::FieldImpl* gpwind, |
| 255 |
field::FieldImpl* spvor, field::FieldImpl* spdiv, |
| 256 |
const eckit::Configuration* parameters) { |
| 257 |
ATLAS_ASSERT(This != nullptr); |
| 258 |
ATLAS_ASSERT(spvor); |
| 259 |
ATLAS_ASSERT(spdiv); |
| 260 |
ATLAS_ASSERT(gpwind); |
| 261 |
ATLAS_ASSERT(parameters); |
| 262 |
Field fspvor(spvor); |
| 263 |
Field fspdiv(spdiv); |
| 264 |
This->invtrans_vordiv2wind_adj(gpwind, fspvor, fspdiv, *parameters); |
| 265 |
} |
| 266 |
|
| 267 |
void atlas__Trans__invtrans_adj(const TransImpl* This, int nb_scalar_fields, double gp_fields[], int nb_vordiv_fields, |
| 268 |
double vorticity_spectra[], double divergence_spectra[], double scalar_spectra[], |
| 269 |
const eckit::Configuration* parameters) { |
| 270 |
ATLAS_ASSERT(This != nullptr); |
| 271 |
This->invtrans_adj(nb_scalar_fields, gp_fields, nb_vordiv_fields, vorticity_spectra, divergence_spectra, |
| 272 |
scalar_spectra, *parameters); |
| 273 |
} |
| 274 |
|
| 275 |
void atlas__Trans__invtrans_grad_adj_field(const TransImpl* This, const field::FieldImpl* gpfield, |
| 276 |
field::FieldImpl* spfield, const eckit::Configuration* config) { |
| 277 |
ATLAS_ASSERT(This != nullptr); |
| 278 |
ATLAS_ASSERT(spfield); |
| 279 |
ATLAS_ASSERT(gpfield); |
| 280 |
Field fspfield(spfield); |
| 281 |
This->invtrans_grad_adj(gpfield, fspfield, *config); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
} // namespace trans |
| 286 |
} // namespace atlas |