Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
MeshMDVariableRef.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MeshMDVariableRef.h (C) 2000-2026 */
9/* */
10/* Class managing a multi-dimensional variable on a mesh entity. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_MESHMDVARIABLEREF_H
13#define ARCANE_CORE_MESHMDVARIABLEREF_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/ArrayLayout.h"
18#include "arcane/utils/ArrayShape.h"
19#include "arcane/utils/MDSpan.h"
20
21#include "arcane/core/DataView.h"
22
23#include "arcane/core/MeshVariableArrayRef.h"
24#include "arcane/core/datatype/DataTypeTraits.h"
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29/*
30 * ATTENTION:
31 *
32 * All classes in this file are experimental and the API is not
33 * fixed. DO NOT USE OUTSIDE OF ARCANE.
34 */
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39namespace Arcane::impl
40{
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44
45template <typename ItemType, typename DataType>
46class MeshMDVariableRefWrapperT
47: public MeshVariableArrayRefT<ItemType, DataType>
48{
49 template <typename _ItemType, typename _DataType, typename _Extents>
51
52 public:
53
55 using VariableType = typename BaseClass::PrivatePartType;
56 using ValueDataType = typename VariableType::ValueDataType;
57
58 private:
59
60 explicit MeshMDVariableRefWrapperT(const VariableBuildInfo& vbi)
61 : BaseClass(vbi)
62 {
63 }
64
65 private:
66
67 ValueDataType* trueData() { return this->m_private_part->trueData(); }
68 const ValueDataType* trueData() const { return this->m_private_part->trueData(); }
69
70 void fillShape(ArrayShape& shape_with_item)
71 {
72 this->m_private_part->fillShape(shape_with_item);
73 }
74};
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79} // namespace Arcane::impl
80
81/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
84namespace Arcane
85{
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90/*!
91 * \brief Base class managing a multi-dimensional variable on a mesh entity.
92 *
93 * \warning API is under definition. Do not use outside of Arcane.
94 */
95template <typename ItemType, typename DataType, typename Extents>
96class MeshMDVariableRefBaseT
97: public MeshVariableRef
98{
99 public:
100
101 using UnderlyingVariableType = MeshVariableArrayRefT<ItemType, DataType>;
103 using ItemLocalIdType = typename ItemType::LocalIdType;
104 using FullExtentsType = Extents;
105
106 public:
107
108 explicit MeshMDVariableRefBaseT(const VariableBuildInfo& b)
109 : MeshVariableRef(b)
110 , m_underlying_var(b)
111 {
112 _internalInit(m_underlying_var.variable());
113 }
114
115 //! Associated underlying variable.
116 UnderlyingVariableType& underlyingVariable() { return m_underlying_var; }
117
118 //! Full shape (static + dynamic) of the variable.
119 ArrayShape fullShape() const { return m_underlying_var.trueData()->shape(); }
120
121 protected:
122
123 void updateFromInternal() override
124 {
125 const Int32 nb_rank = Extents::rank();
126 ArrayShape shape_with_item;
127 shape_with_item.setNbDimension(nb_rank);
128 m_underlying_var.fillShape(shape_with_item);
129
131 m_mdspan = MDSpanType(m_underlying_var.trueData()->view().data(), new_extents);
132 }
133
134 protected:
135
137 MDSpanType m_mdspan;
138};
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143/*!
144 * \brief Class managing a multi-dimensional variable on a mesh entity.
145 *
146 * \warning API is under definition. Do not use outside of Arcane.
147 */
148template <typename ItemType, typename DataType, typename Extents>
149class MeshMDVariableRefT
150: public MeshMDVariableRefBaseT<ItemType, DataType, typename Extents::template AddedFirstExtentsType<DynExtent>>
151{
152 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
153 using BasicType = typename DataTypeTraitsT<DataType>::BasicType;
154 static_assert(Extents::rank() >= 0 && Extents::rank() <= 3, "Only Extents of rank 0, 1, 2 or 3 are implemented");
155 static_assert(std::is_same_v<DataType, BasicType>, "DataType should be a basic type (Real, Int32, Int64, ... )");
156
157 public:
158
159 using BaseClass = MeshMDVariableRefBaseT<ItemType, DataType, AddedFirstExtentsType>;
160 using ItemLocalIdType = typename ItemType::LocalIdType;
161 static constexpr int nb_dynamic = Extents::nb_dynamic;
162
163 public:
164
165 explicit MeshMDVariableRefT(const VariableBuildInfo& b)
166 : BaseClass(b)
167 {}
168
169 public:
170
171 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 0, void>>
172 DataType& operator()(ItemLocalIdType id)
173 {
174 return this->m_mdspan(id.localId());
175 }
176
177 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 0, void>>
178 const DataType& operator()(ItemLocalIdType id) const
179 {
180 return this->m_mdspan(id.localId());
181 }
182
183 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 1, void>>
184 DataType& operator()(ItemLocalIdType id, Int32 i1)
185 {
186 return this->m_mdspan(id.localId(), i1);
187 }
188
189 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 1, void>>
190 const DataType& operator()(ItemLocalIdType id, Int32 i1) const
191 {
192 return this->m_mdspan(id.localId(), i1);
193 }
194
195 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 2, void>>
196 DataType& operator()(ItemLocalIdType id, Int32 i1, Int32 i2)
197 {
198 return this->m_mdspan(id.localId(), i1, i2);
199 }
200
201 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 2, void>>
202 const DataType& operator()(ItemLocalIdType id, Int32 i1, Int32 i2) const
203 {
204 return this->m_mdspan(id.localId(), i1, i2);
205 }
206
207 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 3, void>>
208 DataType& operator()(ItemLocalIdType id, Int32 i, Int32 j, Int32 k)
209 {
210 return this->m_mdspan(id.localId(), i, j, k);
211 }
212
213 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 3, void>>
214 const DataType& operator()(ItemLocalIdType id, Int32 i, Int32 j, Int32 k) const
215 {
216 return this->m_mdspan(id.localId(), i, j, k);
217 }
218
219 /*!
220 * \brief Changes the data shape.
221 *
222 * The number of elements in \a dims must correspond to the number of dynamic values
223 * in \a Extents.
224 */
225 void reshape(std::array<Int32, Extents::nb_dynamic> dims)
226 {
227 ArrayShape shape(dims);
228 this->m_underlying_var.resizeAndReshape(shape);
229 }
230};
231
232/*---------------------------------------------------------------------------*/
233/*---------------------------------------------------------------------------*/
234
235/*!
236 * \brief Class managing a multi-dimensional 'NumVector' type variable on a mesh entity.
237 *
238 * \warning API is under definition. Do not use outside of Arcane.
239 */
240template <typename ItemType, typename DataType, int Size, typename Extents>
241class MeshVectorMDVariableRefT
242: public MeshMDVariableRefBaseT<ItemType, DataType, typename Extents::template AddedFirstLastExtentsType<DynExtent, Size>>
243{
244 public:
245
246 using NumVectorType = NumVector<DataType, Size>;
247
248 private:
249
250 using BasicType = typename DataTypeTraitsT<DataType>::BasicType;
251 using AddedFirstLastExtentsType = typename Extents::template AddedFirstLastExtentsType<DynExtent, Size>;
252 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
253 using BaseClass = MeshMDVariableRefBaseT<ItemType, DataType, AddedFirstLastExtentsType>;
254 static_assert(Extents::rank() >= 0 && Extents::rank() <= 2, "Only Extents of rank 0, 1 or 2 are implemented");
255 static_assert(std::is_same_v<DataType, BasicType>, "DataType should be a basic type (Real, Int32, Int64, ... )");
256
257 public:
258
259 using ItemLocalIdType = typename ItemType::LocalIdType;
260 using ReferenceType = DataViewGetterSetter<NumVectorType>;
261 using ConstReferenceType = DataViewGetter<NumVectorType>;
263 static constexpr int nb_dynamic = Extents::nb_dynamic;
264
265 public:
266
267 explicit MeshVectorMDVariableRefT(const VariableBuildInfo& b)
268 : BaseClass(b)
269 {}
270
271 public:
272
273 //! Accesses the data for reading/writing
274 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 0, void>>
275 ReferenceType operator()(ItemLocalIdType id)
276 {
277 return ReferenceType(m_vector_mdspan.ptrAt(id.localId()));
278 }
279
280 //! Accesses the data for reading
281 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 0, void>>
282 ConstReferenceType operator()(ItemLocalIdType id) const
283 {
284 return ConstReferenceType(m_vector_mdspan.ptrAt(id.localId()));
285 }
286
287 //! Accesses the data for reading/writing
288 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 1, void>>
289 ReferenceType operator()(ItemLocalIdType id, Int32 i1)
290 {
291 return ReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1));
292 }
293
294 //! Accesses the data for reading
295 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 1, void>>
296 ConstReferenceType operator()(ItemLocalIdType id, Int32 i1) const
297 {
298 return ConstReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1));
299 }
300
301 //! Accesses the data for reading/writing
302 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 2, void>>
303 ReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2)
304 {
305 return ReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1, i2));
306 }
307
308 //! Accesses the data for reading
309 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 2, void>>
310 ConstReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2) const
311 {
312 return ConstReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1, i2));
313 }
314
315 /*!
316 * \brief Changes the data shape.
317 *
318 * The number of elements in \a dims must correspond to the number of dynamic values
319 * in \a Extents.
320 */
321 void reshape(std::array<Int32, Extents::nb_dynamic> dims)
322 {
323 std::array<Int32, nb_dynamic + 1> full_dims;
324 // We add 'Size' to the end of the dimensions.
325 for (int i = 0; i < nb_dynamic; ++i)
326 full_dims[i] = dims[i];
327 full_dims[nb_dynamic] = Size;
328 ArrayShape shape(full_dims);
329 this->m_underlying_var.resizeAndReshape(shape);
330 }
331
332 protected:
333
334 void updateFromInternal() override
335 {
337 // Positions the value of m_vector_mdspan.
338 // It will have the same dimensions as m_mdspan except that we
339 // remove the last dimension and change the type
340 // from 'DataType' to 'NumVector<DataType,Size>'.
341 DataType* v = this->m_mdspan.to1DSpan().data();
342 NumVectorType* nv = reinterpret_cast<NumVectorType*>(v);
343 m_vector_mdspan = MDSpanType(nv, this->m_mdspan.extents().dynamicExtents());
344 }
345
346 private:
347
348 MDSpanType m_vector_mdspan;
349};
350
351/*---------------------------------------------------------------------------*/
352/*---------------------------------------------------------------------------*/
353
354/*!
355 * \brief Class managing a multi-dimensional 'NumMatrix' type variable on a
356 * mesh entity.
357 *
358 * \warning API is under definition. Do not use outside of Arcane.
359 */
360template <typename ItemType, typename DataType, int Row, int Column, typename Extents>
361class MeshMatrixMDVariableRefT
362: public MeshMDVariableRefBaseT<ItemType, DataType, typename Extents::template AddedFirstLastLastExtentsType<DynExtent, Row, Column>>
363{
364 public:
365
366 using NumMatrixType = NumMatrix<DataType, Row, Column>;
367
368 private:
369
370 using BasicType = typename DataTypeTraitsT<DataType>::BasicType;
371 using AddedFirstLastLastExtentsType = typename Extents::template AddedFirstLastLastExtentsType<DynExtent, Row, Column>;
372 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
373 using BaseClass = MeshMDVariableRefBaseT<ItemType, DataType, AddedFirstLastLastExtentsType>;
374 static_assert(Extents::rank() >= 0 && Extents::rank() <= 1, "Only Extents of rank 0 or 1 are implemented");
375 static_assert(std::is_same_v<DataType, BasicType>, "DataType should be a basic type (Real, Int32, Int64, ... )");
376
377 public:
378
379 using ItemLocalIdType = typename ItemType::LocalIdType;
380 using ReferenceType = DataViewGetterSetter<NumMatrixType>;
381 using ConstReferenceType = DataViewGetter<NumMatrixType>;
383 static constexpr int nb_dynamic = Extents::nb_dynamic;
384
385 public:
386
387 explicit MeshMatrixMDVariableRefT(const VariableBuildInfo& b)
388 : BaseClass(b)
389 {}
390
391 public:
392
393 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 0, void>>
394 ReferenceType operator()(ItemLocalIdType id)
395 {
396 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId()));
397 }
398
399 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 0, void>>
400 ConstReferenceType operator()(ItemLocalIdType id) const
401 {
402 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId()));
403 }
404
405 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 1, void>>
406 ReferenceType operator()(ItemLocalIdType id, Int32 i1)
407 {
408 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId(), i1));
409 }
410
411 template <typename X = Extents, typename = std::enable_if_t<X::rank() == 1, void>>
412 ConstReferenceType operator()(ItemLocalIdType id, Int32 i1) const
413 {
414 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId(), i1));
415 }
416
417 /*!
418 * \brief Changes the data shape.
419 *
420 * The number of elements in \a dims must correspond to the number of dynamic values
421 * in \a Extents.
422 */
423 void reshape(std::array<Int32, Extents::nb_dynamic> dims)
424 {
425 std::array<Int32, nb_dynamic + 2> full_dims;
426 // We add 'Row' and 'Column' to the end of the dimensions.
427 for (int i = 0; i < nb_dynamic; ++i)
428 full_dims[i] = dims[i];
429 full_dims[nb_dynamic] = Row;
430 full_dims[nb_dynamic + 1] = Column;
431 ArrayShape shape(full_dims);
432 this->m_underlying_var.resizeAndReshape(shape);
433 }
434
435 protected:
436
437 void updateFromInternal() override
438 {
440 // Positions the value of m_vector_mdspan.
441 // It will have the same dimensions as m_mdspan except that we
442 // remove the last dimension and change the type
443 // from 'DataType' to 'NumMatrix<DataType,Row,Column>'.
444 DataType* v = this->m_mdspan.to1DSpan().data();
445 NumMatrixType* nv = reinterpret_cast<NumMatrixType*>(v);
446 m_matrix_mdspan = MDSpanType(nv, this->m_mdspan.extents().dynamicExtents());
447 }
448
449 private:
450
451 MDSpanType m_matrix_mdspan;
452};
453
454/*---------------------------------------------------------------------------*/
455/*---------------------------------------------------------------------------*/
456
457} // namespace Arcane
458
459/*---------------------------------------------------------------------------*/
460/*---------------------------------------------------------------------------*/
461
462#endif
__host__ static __device__ ArrayExtentsBase< Extents > fromSpan(SmallSpan< const Int32 > extents)
Constructs an instance from the values given in extents.
Array shape.
Definition ArrayShape.h:42
void setNbDimension(Int32 nb_value)
Sets the rank of the shape.
Definition ArrayShape.cc:39
SmallSpan< const Int32 > dimensions() const
Values of each dimension.
Definition ArrayShape.h:56
Class for accessing an element of a read/write view.
Class for accessing an element of a read view.
Base class for multi-dimensional views.
Base class managing a multi-dimensional variable on a mesh entity.
UnderlyingVariableType & underlyingVariable()
Associated underlying variable.
void updateFromInternal() override
Updates from the internal part.
ArrayShape fullShape() const
Full shape (static + dynamic) of the variable.
void reshape(std::array< Int32, Extents::nb_dynamic > dims)
Changes the data shape.
void reshape(std::array< Int32, Extents::nb_dynamic > dims)
Changes the data shape.
void updateFromInternal() override
Updates from the internal part.
Array variable on a mesh entity type.
MeshVariableRef(const VariableBuildInfo &vb)
Constructs a reference linked to the module.
void reshape(std::array< Int32, Extents::nb_dynamic > dims)
Changes the data shape.
ConstReferenceType operator()(ItemLocalIdType id, Int32 i1) const
Accesses the data for reading.
ReferenceType operator()(ItemLocalIdType id, Int32 i1)
Accesses the data for reading/writing.
ConstReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2) const
Accesses the data for reading.
ReferenceType operator()(ItemLocalIdType id)
Accesses the data for reading/writing.
void updateFromInternal() override
Updates from the internal part.
ConstReferenceType operator()(ItemLocalIdType id) const
Accesses the data for reading.
ReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2)
Accesses the data for reading/writing.
Small fixed-size matrix containing RowSize rows and ColumnSize columns.
Definition NumMatrix.h:43
Small fixed-size vector of N numerical data points.
Definition NumVector.h:44
Parameters necessary for building a variable.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int32_t Int32
Signed integer type of 32 bits.