Debian Codegraph @

src/atlas/trans/VorDivToUV.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 // file deepcode ignore CppMemoryLeak: static pointers for global registry are OK and will be cleaned up at end
12
13 #include "eckit/thread/AutoLock.h"
14 #include "eckit/thread/Mutex.h"
15 #include "eckit/utils/Hash.h"
16
17 #include "atlas/functionspace.h"
18 #include "atlas/grid/Grid.h"
19 #include "atlas/library/defines.h"
20 #include "atlas/runtime/Exception.h"
21 #include "atlas/runtime/Log.h"
22 #include "atlas/trans/VorDivToUV.h"
23
24 // For factory registration only
25 #if ATLAS_HAVE_TRANS
26 #include "atlas/trans/ifs/VorDivToUVIFS.h"
27 #define TRANS_DEFAULT "ectrans"
28 #else
29 #define TRANS_DEFAULT "local"
30 #endif
31 #include "atlas/trans/local/VorDivToUVLocal.h" // --> recommended "local"
32
33 namespace atlas {
34 namespace trans {
35
36 VorDivToUVImpl::~VorDivToUVImpl() = default;
37
38 namespace {
39
40 static eckit::Mutex* local_mutex = nullptr;
41 static std::map<std::string, VorDivToUVFactory*>* m = nullptr;
42 static pthread_once_t once = PTHREAD_ONCE_INIT;
43
44 static void init() {
45 local_mutex = new eckit::Mutex();
46 m = new std::map<std::string, VorDivToUVFactory*>();
47 }
48
49 template <typename T>
50 void load_builder() {
51 VorDivToUVBuilder<T>("tmp");
52 }
53
54 struct force_link {
55 force_link() {
56 #if ATLAS_HAVE_TRANS
57 load_builder<VorDivToUVIFS>();
58 #endif
59 load_builder<VorDivToUVLocal>();
60 }
61 };
62
63 VorDivToUVFactory& factory(const std::string& name) {
64 std::map<std::string, VorDivToUVFactory*>::const_iterator j = m->find(name);
65 if (j == m->end()) {
66 Log::error() << "No VorDivToUVFactory for [" << name << "]" << std::endl;
67 Log::error() << "VorDivToUVFactory are:" << std::endl;
68 for (j = m->begin(); j != m->end(); ++j) {
69 Log::error() << " " << (*j).first << std::endl;
70 }
71 throw_Exception(std::string("No VorDivToUVFactory called ") + name);
72 }
73 return *j->second;
74 }
75
76 } // namespace
77
78 VorDivToUVFactory::VorDivToUVFactory(const std::string& name): name_(name) {
79 pthread_once(&once, init);
80
81 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
82
83 ATLAS_ASSERT(m->find(name) == m->end());
84 (*m)[name] = this;
85 }
86
87 VorDivToUVFactory::~VorDivToUVFactory() {
88 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
89 m->erase(name_);
90 }
91
92 bool VorDivToUVFactory::has(const std::string& name) {
93 pthread_once(&once, init);
94
95 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
96
97 static force_link static_linking;
98
99 return (m->find(name) != m->end());
100 }
101
102 void VorDivToUVFactory::list(std::ostream& out) {
103 pthread_once(&once, init);
104
105 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
106
107 static force_link static_linking;
108
109 const char* sep = "";
110 for (std::map<std::string, VorDivToUVFactory*>::const_iterator j = m->begin(); j != m->end(); ++j) {
111 out << sep << (*j).first;
112 sep = ", ";
113 }
114 }
115
116 VorDivToUV::Implementation* VorDivToUVFactory::build(const FunctionSpace& sp, const eckit::Configuration& config) {
117 pthread_once(&once, init);
118
119 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
120
121 static force_link static_linking;
122
123 std::string suffix("(" + sp.type() + ")");
124 std::string name = config.getString("type", TRANS_DEFAULT) + suffix;
125
126 Log::debug() << "Looking for TransFactory [" << name << "]" << std::endl;
127
128 if (not config.has("type") and not has(name)) {
129 name = std::string("local") + suffix;
130 Log::debug() << "Looking for TransFactory [" << name << "]" << std::endl;
131 }
132
133 return factory(name).make(sp, config);
134 }
135
136 VorDivToUV::Implementation* VorDivToUVFactory::build(int truncation, const eckit::Configuration& config) {
137 pthread_once(&once, init);
138
139 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
140
141 static force_link static_linking;
142
143 std::string name = config.getString("type", TRANS_DEFAULT);
144
145 Log::debug() << "Looking for VorDivToUVFactory [" << name << "]" << std::endl;
146
147 if (not config.has("type") and not has(name)) {
148 name = std::string("local");
149 Log::debug() << "Looking for VorDivToUVFactory [" << name << "]" << std::endl;
150 }
151
152 return factory(name).make(truncation, config);
153 }
154
155 VorDivToUV::VorDivToUV(const FunctionSpace& sp, const eckit::Configuration& config):
156 Handle(VorDivToUVFactory::build(sp, config)) {}
157
158 VorDivToUV::VorDivToUV(int truncation, const eckit::Configuration& config):
159 Handle(VorDivToUVFactory::build(truncation, config)) {}
160
161 int VorDivToUV::truncation() const {
162 return get()->truncation();
163 }
164
165 // -- IFS type fields --
166 // These fields have special interpretation required. You need to know what
167 // you're doing.
168 // See IFS trans library.
169
170 void VorDivToUV::execute(const int nb_coeff, const int nb_fields, const double vorticity[], const double divergence[],
171 double U[], double V[], const eckit::Configuration& config) const {
172 get()->execute(nb_coeff, nb_fields, vorticity, divergence, U, V, config);
173 }
174
175 } // namespace trans
176 } // namespace atlas