Debian Codegraph @

src/atlas/mesh/Elements.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 "atlas/mesh/Elements.h"
12 #include "atlas/array/MakeView.h"
13 #include "atlas/field/Field.h"
14 #include "atlas/library/config.h"
15 #include "atlas/mesh/ElementType.h"
16 #include "atlas/runtime/Exception.h"
17
18 namespace atlas {
19 namespace mesh {
20
21 //-----------------------------------------------------------------------------
22
23 void Elements::rebuild() {
24 size_ = hybrid_elements_->elements_size_[type_idx_];
25 nb_nodes_ = hybrid_elements_->element_type(type_idx_).nb_nodes();
26 nb_edges_ = hybrid_elements_->element_type(type_idx_).nb_edges();
27 begin_ = hybrid_elements_->elements_begin_[type_idx_];
28 end_ = hybrid_elements_->elements_begin_[type_idx_ + 1];
29 }
30
31 Elements::Elements(HybridElements& elements, idx_t type_idx):
32 owns_(false), hybrid_elements_(&elements), type_idx_(type_idx) {
33 rebuild();
34 }
35
36 Elements::Elements(ElementType* element_type, idx_t nb_elements, const std::vector<idx_t>& node_connectivity):
37 owns_(true) {
38 hybrid_elements_ = new HybridElements();
39 type_idx_ = hybrid_elements_->add(element_type, nb_elements, node_connectivity.data());
40 rebuild();
41 }
42
43 Elements::Elements(ElementType* element_type, idx_t nb_elements, const idx_t node_connectivity[], bool fortran_array):
44 owns_(true) {
45 hybrid_elements_ = new HybridElements();
46 type_idx_ = hybrid_elements_->add(element_type, nb_elements, node_connectivity, fortran_array);
47 rebuild();
48 }
49
50 Elements::~Elements() {
51 if (owns_) {
52 delete hybrid_elements_;
53 }
54 }
55
56 const std::string& Elements::name() const {
57 return hybrid_elements_->element_type(type_idx_).name();
58 }
59
60 template <>
61 array::LocalView<const double, 1> Elements::view(const Field& field) const {
62 return array::make_host_view<double, 1>(field).slice(array::Range{begin(), begin() + size()});
63 }
64
65 template <>
66 array::LocalView<const float, 1> Elements::view(const Field& field) const {
67 return array::make_host_view<float, 1>(field).slice(array::Range{begin(), begin() + size()});
68 }
69
70 template <>
71 array::LocalView<const int, 1> Elements::view(const Field& field) const {
72 return array::make_host_view<int, 1>(field).slice(array::Range{begin(), begin() + size()});
73 }
74
75 template <>
76 array::LocalView<const long, 1> Elements::view(const Field& field) const {
77 return array::make_host_view<long, 1>(field).slice(array::Range{begin(), begin() + size()});
78 }
79
80 template <>
81 array::LocalView<const double, 2> Elements::view(const Field& field) const {
82 return array::make_host_view<double, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
83 }
84
85 template <>
86 array::LocalView<const float, 2> Elements::view(const Field& field) const {
87 return array::make_host_view<float, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
88 }
89
90 template <>
91 array::LocalView<const int, 2> Elements::view(const Field& field) const {
92 return array::make_host_view<int, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
93 }
94
95 template <>
96 array::LocalView<const long, 2> Elements::view(const Field& field) const {
97 return array::make_host_view<long, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
98 }
99
100 // ----------------------------------------------------------------------------
101
102 template <>
103 array::LocalView<double, 1> Elements::view(Field& field) const {
104 return array::make_host_view<double, 1>(field).slice(array::Range{begin(), begin() + size()});
105 }
106
107 template <>
108 array::LocalView<float, 1> Elements::view(Field& field) const {
109 return array::make_host_view<float, 1>(field).slice(array::Range{begin(), begin() + size()});
110 }
111
112 template <>
113 array::LocalView<int, 1> Elements::view(Field& field) const {
114 return array::make_host_view<int, 1>(field).slice(array::Range{begin(), begin() + size()});
115 }
116
117 template <>
118 array::LocalView<long, 1> Elements::view(Field& field) const {
119 return array::make_host_view<long, 1>(field).slice(array::Range{begin(), begin() + size()});
120 }
121
122 template <>
123 array::LocalView<double, 2> Elements::view(Field& field) const {
124 return array::make_host_view<double, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
125 }
126
127 template <>
128 array::LocalView<float, 2> Elements::view(Field& field) const {
129 return array::make_host_view<float, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
130 }
131
132 template <>
133 array::LocalView<int, 2> Elements::view(Field& field) const {
134 return array::make_host_view<int, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
135 }
136
137 template <>
138 array::LocalView<long, 2> Elements::view(Field& field) const {
139 return array::make_host_view<long, 2>(field).slice(array::Range{begin(), begin() + size()}, array::Range::all());
140 }
141
142 idx_t Elements::add(const idx_t nb_elements) {
143 idx_t position = size();
144 hybrid_elements_->insert(type_idx_, end(), nb_elements);
145 return position;
146 }
147
148
149 template <>
150 array::LocalIndexView<idx_t, 1> Elements::indexview(Field& field) const {
151 auto local_view = array::make_host_view<idx_t, 1>(field).slice(array::Range{begin(), begin() + size()});
152 return array::LocalIndexView<idx_t, 1>(local_view.data(), local_view.shape(), local_view.strides());
153 }
154
155 template <>
156 array::LocalIndexView<const idx_t, 1> Elements::indexview(const Field& field) const {
157 auto local_view = array::make_host_view<idx_t, 1>(field).slice(array::Range{begin(), begin() + size()});
158 return array::LocalIndexView<const idx_t, 1>(local_view.data(), local_view.shape(), local_view.strides());
159 }
160
161 //-----------------------------------------------------------------------------
162
163 extern "C" {
164
165 void atlas__mesh__Elements__delete(Elements* This) {
166 delete This;
167 }
168
169 idx_t atlas__mesh__Elements__size(const Elements* This) {
170 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
171 return This->size();
172 }
173
174 idx_t atlas__mesh__Elements__begin(const Elements* This) {
175 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
176 return This->begin();
177 }
178
179 idx_t atlas__mesh__Elements__end(const Elements* This) {
180 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
181 return This->end();
182 }
183
184 BlockConnectivity* atlas__mesh__Elements__node_connectivity(Elements* This) {
185 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
186 return &This->node_connectivity();
187 }
188
189 BlockConnectivity* atlas__mesh__Elements__edge_connectivity(Elements* This) {
190 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
191 return &This->edge_connectivity();
192 }
193
194 BlockConnectivity* atlas__mesh__Elements__cell_connectivity(Elements* This) {
195 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
196 return &This->cell_connectivity();
197 }
198
199 int atlas__mesh__Elements__has_field(const Elements* This, char* name) {
200 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
201 return This->has_field(std::string(name));
202 }
203
204 int atlas__mesh__Elements__nb_fields(const Elements* This) {
205 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
206 return This->nb_fields();
207 }
208
209 field::FieldImpl* atlas__mesh__Elements__field_by_idx(Elements* This, idx_t idx) {
210 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
211 return This->field(idx).get();
212 }
213
214 field::FieldImpl* atlas__mesh__Elements__field_by_name(Elements* This, char* name) {
215 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
216 return This->field(std::string(name)).get();
217 }
218
219 field::FieldImpl* atlas__mesh__Elements__global_index(Elements* This) {
220 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
221 return This->global_index().get();
222 }
223
224 field::FieldImpl* atlas__mesh__Elements__remote_index(Elements* This) {
225 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
226 return This->remote_index().get();
227 }
228
229 field::FieldImpl* atlas__mesh__Elements__partition(Elements* This) {
230 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
231 return This->partition().get();
232 }
233
234 field::FieldImpl* atlas__mesh__Elements__halo(Elements* This) {
235 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
236 return This->halo().get();
237 }
238
239 const ElementType* atlas__mesh__Elements__element_type(const Elements* This) {
240 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
241 return &This->element_type();
242 }
243
244 void atlas__mesh__Elements__add(Elements* This, idx_t nb_elements) {
245 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Elements");
246 This->add(nb_elements);
247 }
248 }
249
250 //-----------------------------------------------------------------------------
251
252 } // namespace mesh
253 } // namespace atlas
254
255 #undef FORTRAN_BASE
256 #undef TO_FORTRAN