Debian Codegraph @

src/atlas/mesh/HybridElements.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 <algorithm>
12
13 #include "eckit/log/Bytes.h"
14
15 #include "atlas/array/MakeView.h"
16 #include "atlas/field/Field.h"
17 #include "atlas/library/config.h"
18 #include "atlas/mesh/ElementType.h"
19 #include "atlas/mesh/Elements.h"
20 #include "atlas/mesh/HybridElements.h"
21 #include "atlas/mesh/Mesh.h"
22 #include "atlas/runtime/Exception.h"
23 #include "atlas/runtime/Log.h"
24
25 #if ATLAS_HAVE_FORTRAN
26 #define FORTRAN_BASE 1
27 #define TO_FORTRAN +1
28 #else
29 #define FORTRAN_BASE 0
30 #endif
31
32 using atlas::array::ArrayView;
33 using atlas::array::IndexView;
34 using atlas::array::make_datatype;
35 using atlas::array::make_indexview;
36 using atlas::array::make_shape;
37 using atlas::array::make_view;
38
39 namespace atlas {
40 namespace mesh {
41
42 //------------------------------------------------------------------------------------------------------
43
44 namespace {
45
46 static void set_uninitialized_fields_to_zero(HybridElements& elems, idx_t begin) {
47 ArrayView<gidx_t, 1> global_index = make_view<gidx_t, 1>(elems.global_index());
48 IndexView<idx_t, 1> remote_index = make_indexview<idx_t, 1>(elems.remote_index());
49 ArrayView<int, 1> partition = make_view<int, 1>(elems.partition());
50 ArrayView<int, 1> halo = make_view<int, 1>(elems.halo());
51 ArrayView<int, 1> flags = make_view<int, 1>(elems.flags());
52
53 for (idx_t j = begin; j < elems.size(); ++j) {
54 global_index(j) = 0;
55 remote_index(j) = 0;
56 partition(j) = 0;
57 halo(j) = 0;
58 flags(j) = 0;
59 }
60 }
61 } // namespace
62
63 //------------------------------------------------------------------------------------------------------
64
65 HybridElements::HybridElements(): size_(0), elements_size_(), elements_begin_(1, 0ul), type_idx_() {
66 add(Field("glb_idx", make_datatype<gidx_t>(), make_shape(size())));
67 add(Field("remote_idx", make_datatype<idx_t>(), make_shape(size())));
68 add(Field("partition", make_datatype<int>(), make_shape(size())));
69 add(Field("halo", make_datatype<int>(), make_shape(size())));
70 add(Field("flags", make_datatype<int>(), make_shape(size())));
71 set_uninitialized_fields_to_zero(*this, 0);
72
73 node_connectivity_ = &add(new Connectivity("node"));
74 edge_connectivity_ = &add(new Connectivity("edge"));
75 cell_connectivity_ = &add(new Connectivity("cell"));
76 }
77
78 HybridElements::~HybridElements() = default;
79
80 Field HybridElements::add(const Field& field) {
81 ATLAS_ASSERT(field);
82 ATLAS_ASSERT(!field.name().empty());
83
84 if (has_field(field.name())) {
85 std::stringstream msg;
86 msg << "Trying to add field '" << field.name()
87 << "' to HybridElements, but HybridElements already has a field with "
88 "this name.";
89 throw_Exception(msg.str(), Here());
90 }
91 fields_[field.name()] = field;
92 return field;
93 }
94
95 void HybridElements::resize(idx_t size) {
96 idx_t old_size = size_;
97 size_ = size;
98 for (FieldMap::iterator it = fields_.begin(); it != fields_.end(); ++it) {
99 Field& field = it->second;
100 array::ArrayShape shape = field.shape();
101 shape[0] = size_;
102 field.resize(shape);
103 }
104
105 set_uninitialized_fields_to_zero(*this, old_size);
106 }
107
108 void HybridElements::remove_field(const std::string& name) {
109 if (!has_field(name)) {
110 std::stringstream msg;
111 msg << "Trying to remove field `" << name
112 << "' in HybridElements, but no field with this name is present in HybridElements.";
113 throw_Exception(msg.str(), Here());
114 }
115 fields_.erase(name);
116 }
117
118 const Field& HybridElements::field(const std::string& name) const {
119 if (!has_field(name)) {
120 std::stringstream msg;
121 msg << "Trying to access field `" << name
122 << "' in HybridElements, but no field with this name is present in HybridElements.";
123 throw_Exception(msg.str(), Here());
124 }
125 return fields_.find(name)->second;
126 }
127
128 Field& HybridElements::field(const std::string& name) {
129 return const_cast<Field&>(static_cast<const HybridElements*>(this)->field(name));
130 }
131
132 const Field& HybridElements::field(idx_t idx) const {
133 ATLAS_ASSERT(idx < nb_fields());
134 idx_t c(0);
135 for (FieldMap::const_iterator it = fields_.begin(); it != fields_.end(); ++it) {
136 if (idx == c) {
137 const Field& field = it->second;
138 return field;
139 }
140 c++;
141 }
142 throw_Exception("Should not be here!", Here());
143 }
144
145 Field& HybridElements::field(idx_t idx) {
146 return const_cast<Field&>(static_cast<const HybridElements*>(this)->field(idx));
147 }
148
149 HybridElements::Connectivity& HybridElements::add(Connectivity* connectivity) {
150 connectivities_[connectivity->name()] = connectivity;
151 return *connectivity;
152 }
153
154 idx_t HybridElements::add(const ElementType* element_type, idx_t nb_elements, const std::vector<idx_t>& connectivity) {
155 return add(element_type, nb_elements, connectivity.data());
156 }
157
158 idx_t HybridElements::add(const ElementType* element_type, idx_t nb_elements, const idx_t connectivity[]) {
159 return add(element_type, nb_elements, connectivity, false);
160 }
161
162 idx_t HybridElements::add(const ElementType* element_type, idx_t nb_elements, const idx_t connectivity[],
163 bool fortran_array) {
164 util::ObjectHandle<const ElementType> etype(element_type);
165
166 idx_t old_size = size();
167 idx_t new_size = old_size + nb_elements;
168
169 idx_t nb_nodes = etype->nb_nodes();
170
171 type_idx_.resize(new_size);
172
173 for (idx_t e = old_size; e < new_size; ++e) {
174 type_idx_[e] = element_types_.size();
175 }
176
177 elements_begin_.push_back(new_size);
178 elements_size_.push_back(nb_elements);
179
180 element_types_.push_back(etype);
181 elements_.resize(element_types_.size());
182 for (idx_t t = 0; t < nb_types(); ++t) {
183 if (elements_[t]) {
184 elements_[t]->rebuild();
185 }
186 else {
187 elements_[t] = util::ObjectHandle<Elements>(new Elements(*this, t));
188 }
189 }
190
191 node_connectivity_->add(nb_elements, nb_nodes, connectivity, fortran_array);
192
193 resize(new_size);
194 return element_types_.size() - 1;
195 }
196
197 idx_t HybridElements::add(const ElementType* element_type, idx_t nb_elements) {
198 util::ObjectHandle<const ElementType> etype(element_type);
199
200 idx_t old_size = size();
201 idx_t new_size = old_size + nb_elements;
202
203 idx_t nb_nodes = etype->nb_nodes();
204
205 type_idx_.resize(new_size);
206
207 for (idx_t e = old_size; e < new_size; ++e) {
208 type_idx_[e] = element_types_.size();
209 }
210
211 elements_begin_.emplace_back(new_size);
212 elements_size_.emplace_back(nb_elements);
213
214 element_types_.emplace_back(etype);
215 elements_.emplace_back(new Elements(*this, elements_.size()));
216
217 node_connectivity_->add(nb_elements, nb_nodes);
218 resize(new_size);
219 return element_types_.size() - 1;
220 }
221
222 idx_t HybridElements::add(const Elements& elems) {
223 bool fortran_array = true;
224 return add(&elems.element_type(), elems.size(), elems.node_connectivity().data(), fortran_array);
225 }
226
227 const std::string& HybridElements::name(idx_t elem_idx) const {
228 return element_types_[type_idx_[elem_idx]]->name();
229 }
230
231 idx_t HybridElements::elemtype_nb_nodes(idx_t elem_idx) const {
232 return element_type(type_idx(elem_idx)).nb_nodes();
233 }
234
235 idx_t HybridElements::elemtype_nb_edges(idx_t elem_idx) const {
236 return element_type(type_idx(elem_idx)).nb_edges();
237 }
238
239 void HybridElements::insert(idx_t type_idx, idx_t position, idx_t nb_elements) {
240 type_idx_.insert(type_idx_.begin() + position, nb_elements, type_idx);
241 elements_size_[type_idx] += nb_elements;
242 for (idx_t jtype = type_idx + 1; jtype < nb_types() + 1; ++jtype) {
243 elements_begin_[jtype] += nb_elements;
244 }
245 for (idx_t t = 0; t < nb_types(); ++t) {
246 elements_[t]->rebuild();
247 }
248 node_connectivity_->insert(position, nb_elements, element_types_[type_idx]->nb_nodes());
249
250 size_ += nb_elements;
251 for (FieldMap::iterator it = fields_.begin(); it != fields_.end(); ++it) {
252 Field& field = it->second;
253 field.insert(position, nb_elements);
254 }
255 }
256
257 //-----------------------------------------------------------------------------
258
259 void HybridElements::clear() {
260 resize(0);
261 for (ConnectivityMap::iterator it = connectivities_.begin(); it != connectivities_.end(); ++it) {
262 it->second->clear();
263 }
264 size_ = 0;
265 elements_size_.clear();
266 elements_begin_.resize(1);
267 elements_begin_[0] = 0;
268 element_types_.clear();
269 type_idx_.clear();
270 elements_.clear();
271 }
272
273 //-----------------------------------------------------------------------------
274
275 void HybridElements::updateDevice() const {
276 std::for_each(fields_.begin(), fields_.end(), [](const FieldMap::value_type& v) { v.second.updateDevice(); });
277 }
278
279 void HybridElements::updateHost() const {
280 std::for_each(fields_.begin(), fields_.end(), [](const FieldMap::value_type& v) { v.second.updateHost(); });
281 }
282
283 void HybridElements::syncHostDevice() const {
284 std::for_each(fields_.begin(), fields_.end(), [](const FieldMap::value_type& v) { v.second.syncHostDevice(); });
285 }
286
287 size_t HybridElements::footprint() const {
288 size_t size = sizeof(*this);
289 for (FieldMap::const_iterator it = fields_.begin(); it != fields_.end(); ++it) {
290 size += (*it).second.footprint();
291 }
292 for (ConnectivityMap::const_iterator it = connectivities_.begin(); it != connectivities_.end(); ++it) {
293 size += (*it).second->footprint();
294 }
295 size += elements_size_.capacity() * sizeof(idx_t);
296 size += elements_begin_.capacity() * sizeof(idx_t);
297
298 size += metadata_.footprint();
299
300 return size;
301 }
302
303 //-----------------------------------------------------------------------------
304
305 extern "C" {
306 HybridElements* atlas__mesh__HybridElements__create() {
307 return new HybridElements();
308 }
309
310 void atlas__mesh__HybridElements__delete(HybridElements* This) {
311 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
312 delete This;
313 }
314
315 MultiBlockConnectivity* atlas__mesh__HybridElements__node_connectivity(HybridElements* This) {
316 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
317 return &This->node_connectivity();
318 }
319
320 MultiBlockConnectivity* atlas__mesh__HybridElements__edge_connectivity(HybridElements* This) {
321 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
322 return &This->edge_connectivity();
323 }
324
325 MultiBlockConnectivity* atlas__mesh__HybridElements__cell_connectivity(HybridElements* This) {
326 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
327 return &This->cell_connectivity();
328 }
329
330 idx_t atlas__mesh__HybridElements__size(const HybridElements* This) {
331 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
332 return This->size();
333 }
334
335 void atlas__mesh__HybridElements__add_elements(HybridElements* This, ElementType* elementtype, idx_t nb_elements) {
336 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
337 This->add(elementtype, nb_elements);
338 }
339
340 void atlas__mesh__HybridElements__add_elements_with_nodes(HybridElements* This, ElementType* elementtype,
341 idx_t nb_elements, idx_t node_connectivity[],
342 int fortran_array) {
343 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
344 ATLAS_ASSERT(elementtype != nullptr, "Cannot access uninitialised atlas_mesh_ElementType");
345 This->add(elementtype, nb_elements, node_connectivity, fortran_array);
346 }
347
348 int atlas__mesh__HybridElements__has_field(const HybridElements* This, char* name) {
349 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
350 return This->has_field(std::string(name));
351 }
352
353 int atlas__mesh__HybridElements__nb_fields(const HybridElements* This) {
354 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
355 return This->nb_fields();
356 }
357
358 int atlas__mesh__HybridElements__nb_types(const HybridElements* This) {
359 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
360 return This->nb_types();
361 }
362
363 field::FieldImpl* atlas__mesh__HybridElements__field_by_idx(HybridElements* This, idx_t idx) {
364 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
365 return This->field(idx).get();
366 }
367
368 field::FieldImpl* atlas__mesh__HybridElements__field_by_name(HybridElements* This, char* name) {
369 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
370 return This->field(std::string(name)).get();
371 }
372
373 field::FieldImpl* atlas__mesh__HybridElements__global_index(HybridElements* This) {
374 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
375 return This->global_index().get();
376 }
377
378 field::FieldImpl* atlas__mesh__HybridElements__remote_index(HybridElements* This) {
379 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
380 return This->remote_index().get();
381 }
382
383 field::FieldImpl* atlas__mesh__HybridElements__partition(HybridElements* This) {
384 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
385 return This->partition().get();
386 }
387
388 field::FieldImpl* atlas__mesh__HybridElements__halo(HybridElements* This) {
389 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
390 return This->halo().get();
391 }
392
393 Elements* atlas__mesh__HybridElements__elements(HybridElements* This, idx_t idx) {
394 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
395 return &This->elements(idx);
396 }
397
398 void atlas__mesh__HybridElements__add_field(HybridElements* This, field::FieldImpl* field) {
399 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_mesh_HybridElements");
400 This->add(field);
401 }
402 }
403
404 //-----------------------------------------------------------------------------
405
406 } // namespace mesh
407 } // namespace atlas
408
409 #undef FORTRAN_BASE