Debian Codegraph @

src/atlas/numerics/Nabla.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 <map>
14 #include <string>
15
16 #include "eckit/thread/AutoLock.h"
17 #include "eckit/thread/Mutex.h"
18
19 #include "atlas/library/config.h"
20 #include "atlas/numerics/Method.h"
21 #include "atlas/numerics/Nabla.h"
22 #include "atlas/numerics/fvm/Method.h"
23 #include "atlas/numerics/fvm/Nabla.h"
24 #include "atlas/runtime/Exception.h"
25 #include "atlas/runtime/Log.h"
26 #include "atlas/util/Config.h"
27
28 namespace {
29
30 static eckit::Mutex* local_mutex = nullptr;
31 static std::map<std::string, atlas::numerics::NablaFactory*>* m = nullptr;
32 static pthread_once_t once = PTHREAD_ONCE_INIT;
33
34 static void init() {
35 local_mutex = new eckit::Mutex();
36 m = new std::map<std::string, atlas::numerics::NablaFactory*>();
37 }
38 } // namespace
39
40 namespace atlas {
41 namespace numerics {
42
43 NablaImpl::NablaImpl(const Method& method, const eckit::Parametrisation&): method_(&method) {}
44
45 NablaImpl::~NablaImpl() = default;
46
47 Nabla::Nabla(const Method& method, const eckit::Parametrisation& p): Handle(NablaFactory::build(method, p)) {}
48
49 Nabla::Nabla(const Method& method): Nabla(method, util::NoConfig()) {}
50
51 void Nabla::gradient(const Field& scalar, Field& grad) const {
52 get()->gradient(scalar, grad);
53 }
54
55 void Nabla::divergence(const Field& vector, Field& div) const {
56 get()->divergence(vector, div);
57 }
58
59 void Nabla::curl(const Field& vector, Field& curl) const {
60 get()->curl(vector, curl);
61 }
62
63 void Nabla::laplacian(const Field& scalar, Field& laplacian) const {
64 get()->laplacian(scalar, laplacian);
65 }
66
67 namespace {
68
69 template <typename T>
70 void load_builder() {
71 NablaBuilder<T>("tmp");
72 }
73
74 struct force_link {
75 force_link() { load_builder<fvm::Nabla>(); }
76 };
77
78 } // namespace
79
80 NablaFactory::NablaFactory(const std::string& name): name_(name) {
81 pthread_once(&once, init);
82
83 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
84
85 ATLAS_ASSERT(m->find(name) == m->end());
86 (*m)[name] = this;
87 }
88
89 NablaFactory::~NablaFactory() {
90 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
91 m->erase(name_);
92 }
93
94 void NablaFactory::list(std::ostream& out) {
95 pthread_once(&once, init);
96
97 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
98
99 static force_link static_linking;
100
101 const char* sep = "";
102 for (std::map<std::string, NablaFactory*>::const_iterator j = m->begin(); j != m->end(); ++j) {
103 out << sep << (*j).first;
104 sep = ", ";
105 }
106 }
107
108 bool NablaFactory::has(const std::string& name) {
109 pthread_once(&once, init);
110
111 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
112
113 static force_link static_linking;
114
115 return (m->find(name) != m->end());
116 }
117
118 const NablaImpl* NablaFactory::build(const Method& method, const eckit::Parametrisation& p) {
119 pthread_once(&once, init);
120
121 eckit::AutoLock<eckit::Mutex> lock(local_mutex);
122
123 static force_link static_linking;
124
125 std::map<std::string, NablaFactory*>::const_iterator j = m->find(method.name());
126
127 Log::debug() << "Looking for NablaFactory [" << method.name() << "]" << '\n';
128
129 if (j == m->end()) {
130 Log::error() << "No NablaFactory for [" << method.name() << "]" << '\n';
131 Log::error() << "NablaFactories are:" << '\n';
132 for (j = m->begin(); j != m->end(); ++j) {
133 Log::error() << " " << (*j).first << '\n';
134 }
135 throw_Exception(std::string("No NablaFactory called ") + method.name());
136 }
137
138 return (*j).second->make(method, p);
139 }
140
141 extern "C" {
142
143 void atlas__Nabla__delete(Nabla::Implementation* This) {
144 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_numerics_Nabla");
145 delete This;
146 }
147
148 const Nabla::Implementation* atlas__Nabla__create(const Method* method, const eckit::Parametrisation* config) {
149 ATLAS_ASSERT(method != nullptr, "Cannot access uninitialisd atlas_numerics_Method");
150 ATLAS_ASSERT(config != nullptr, "Cannot access uninitialisd atlas_Config");
151 const Nabla::Implementation* nabla(nullptr);
152 {
153 Nabla n(*method, *config);
154 nabla = n.get();
155 nabla->attach();
156 }
157 nabla->detach();
158 return nabla;
159 }
160
161 void atlas__Nabla__gradient(const Nabla::Implementation* This, const field::FieldImpl* scalar, field::FieldImpl* grad) {
162 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_numerics_Nabla");
163 ATLAS_ASSERT(scalar != nullptr, "Cannot access uninitialisd atlas_Field");
164 ATLAS_ASSERT(grad != nullptr, "Cannot access uninitialisd atlas_Field");
165 Field fgrad(grad);
166 This->gradient(scalar, fgrad);
167 }
168
169 void atlas__Nabla__divergence(const Nabla::Implementation* This, const field::FieldImpl* vector,
170 field::FieldImpl* div) {
171 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_numerics_Nabla");
172 ATLAS_ASSERT(vector != nullptr, "Cannot access uninitialisd atlas_Field");
173 ATLAS_ASSERT(div != nullptr, "Cannot access uninitialisd atlas_Field");
174 Field fdiv(div);
175 This->divergence(vector, fdiv);
176 }
177
178 void atlas__Nabla__curl(const Nabla::Implementation* This, const field::FieldImpl* vector, field::FieldImpl* curl) {
179 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_numerics_Nabla");
180 ATLAS_ASSERT(vector != nullptr, "Cannot access uninitialisd atlas_Field");
181 ATLAS_ASSERT(curl != nullptr, "Cannot access uninitialisd atlas_Field");
182 Field fcurl(curl);
183 This->curl(vector, fcurl);
184 }
185
186 void atlas__Nabla__laplacian(const Nabla::Implementation* This, const field::FieldImpl* scalar,
187 field::FieldImpl* laplacian) {
188 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_numerics_Nabla");
189 ATLAS_ASSERT(scalar != nullptr, "Cannot access uninitialisd atlas_Field");
190 ATLAS_ASSERT(laplacian != nullptr, "Cannot access uninitialisd atlas_Field");
191 Field flaplacian(laplacian);
192 This->laplacian(scalar, flaplacian);
193 }
194
195 const functionspace::FunctionSpaceImpl* atlas__Nabla__functionspace(const Nabla::Implementation* This) {
196 return This->functionspace().get();
197 }
198
199 } // extern "C"
200
201 } // namespace numerics
202 } // namespace atlas