Arcane  4.1.15.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
29namespace Arcane::Impl
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35template <typename ItemType, typename DataType>
36class MeshMDVariableRefWrapperT
37: public MeshVariableArrayRefT<ItemType, DataType>
38{
39 template <typename _ItemType, typename _DataType, typename _Extents>
41
42 public:
43
45 using VariableType = typename BaseClass::PrivatePartType;
46 using ValueDataType = typename VariableType::ValueDataType;
47
48 private:
49
50 explicit MeshMDVariableRefWrapperT(const VariableBuildInfo& vbi)
51 : BaseClass(vbi)
52 {
53 }
54
55 private:
56
57 ValueDataType* trueData() { return this->m_private_part->trueData(); }
58 const ValueDataType* trueData() const { return this->m_private_part->trueData(); }
59
60 void fillShape(ArrayShape& shape_with_item)
61 {
62 this->m_private_part->fillShape(shape_with_item);
63 }
64};
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
68
69} // namespace Arcane::Impl
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
74namespace Arcane
75{
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79/*!
80 * \brief Base class managing a multi-dimensional variable on a mesh entity.
81 */
82template <typename ItemType, typename DataType, typename Extents>
83class MeshMDVariableRefBaseT
84: public MeshVariableRef
85{
86 public:
87
88 using UnderlyingVariableType = MeshVariableArrayRefT<ItemType, DataType>;
90 using ItemLocalIdType = typename ItemType::LocalIdType;
91 using FullExtentsType = Extents;
92
93 public:
94
95 explicit MeshMDVariableRefBaseT(const VariableBuildInfo& b)
97 , m_underlying_var(b)
98 {
99 _internalInit(m_underlying_var.variable());
100 }
101
102 //! Associated underlying variable.
103 UnderlyingVariableType& underlyingVariable() { return m_underlying_var; }
104
105 //! Full shape (static + dynamic) of the variable.
106 ArrayShape fullShape() const { return m_underlying_var.trueData()->shape(); }
107
108 protected:
109
110 void updateFromInternal() override
111 {
112 const Int32 nb_rank = Extents::rank();
113 ArrayShape shape_with_item;
114 shape_with_item.setNbDimension(nb_rank);
115 m_underlying_var.fillShape(shape_with_item);
116
118 m_mdspan = MDSpanType(m_underlying_var.trueData()->view().data(), new_extents);
119 }
120
121 protected:
122
124 MDSpanType m_mdspan;
125};
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129/*!
130 * \brief Class managing a multi-dimensional variable on a mesh entity.
131 *
132 * \warning You need to call the method reshape() before using this kind
133 * of variables.
134 *
135 * For more information, see \ref arcanedoc_core_types_axl_md_variable_use.
136 */
137template <typename ItemType, typename DataType, typename Extents>
138class MeshMDVariableRefT
139: public MeshMDVariableRefBaseT<ItemType, DataType, typename Extents::template AddedFirstExtentsType<DynExtent>>
140{
141 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
142 using BasicType = typename DataTypeTraitsT<DataType>::BasicType;
143 static_assert(Extents::rank() >= 0 && Extents::rank() <= 3, "Only Extents of rank 0, 1, 2 or 3 are implemented");
144 static_assert(std::is_same_v<DataType, BasicType>, "DataType should be a basic type (Real, Int32, Int64, ... )");
145
146 public:
147
148 using BaseClass = MeshMDVariableRefBaseT<ItemType, DataType, AddedFirstExtentsType>;
149 using ItemLocalIdType = typename ItemType::LocalIdType;
150 static constexpr int nb_dynamic = Extents::nb_dynamic;
151
152 public:
153
154 explicit MeshMDVariableRefT(const VariableBuildInfo& b)
155 : BaseClass(b)
156 {}
157
158 public:
159
160 DataType& operator()(ItemLocalIdType id) requires(Extents::rank() == 0)
161 {
162 return this->m_mdspan(id.localId());
163 }
164
165 const DataType& operator()(ItemLocalIdType id) const requires(Extents::rank() == 0)
166 {
167 return this->m_mdspan(id.localId());
168 }
169
170 DataType& operator()(ItemLocalIdType id, Int32 i1) requires(Extents::rank() == 1)
171 {
172 return this->m_mdspan(id.localId(), i1);
173 }
174
175 const DataType& operator()(ItemLocalIdType id, Int32 i1) const requires(Extents::rank() == 1)
176 {
177 return this->m_mdspan(id.localId(), i1);
178 }
179
180 DataType& operator()(ItemLocalIdType id, Int32 i1, Int32 i2)
181 requires(Extents::rank() == 2)
182 {
183 return this->m_mdspan(id.localId(), i1, i2);
184 }
185
186 const DataType& operator()(ItemLocalIdType id, Int32 i1, Int32 i2) const
187 requires(Extents::rank() == 2)
188 {
189 return this->m_mdspan(id.localId(), i1, i2);
190 }
191
192 DataType& operator()(ItemLocalIdType id, Int32 i, Int32 j, Int32 k)
193 requires(Extents::rank() == 3)
194 {
195 return this->m_mdspan(id.localId(), i, j, k);
196 }
197
198 const DataType& operator()(ItemLocalIdType id, Int32 i, Int32 j, Int32 k) const
199 requires(Extents::rank() == 3)
200 {
201 return this->m_mdspan(id.localId(), i, j, k);
202 }
203
204 /*!
205 * \brief Changes the data shape.
206 *
207 * The number of elements in \a dims must correspond to the number of dynamic values
208 * in \a Extents.
209 */
210 void reshape(std::array<Int32, Extents::nb_dynamic> dims)
211 {
212 ArrayShape shape(dims);
213 this->m_underlying_var.resizeAndReshape(shape);
214 }
215};
216
217/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219/*!
220 * \brief Class managing a multi-dimensional NumVector type variable on a mesh entity.
221 *
222 * \warning You need to call the method reshape() before using this kind
223 * of variables.
224 *
225 * For more information, see \ref arcanedoc_core_types_axl_md_variable_use.
226 */
227template <typename ItemType, typename DataType, int Size, typename Extents>
228class MeshVectorMDVariableRefT
229: public MeshMDVariableRefBaseT<ItemType, DataType, typename Extents::template AddedFirstLastExtentsType<DynExtent, Size>>
230{
231 public:
232
233 using NumVectorType = NumVector<DataType, Size>;
234
235 private:
236
237 using BasicType = typename DataTypeTraitsT<DataType>::BasicType;
238 using AddedFirstLastExtentsType = typename Extents::template AddedFirstLastExtentsType<DynExtent, Size>;
239 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
240 using BaseClass = MeshMDVariableRefBaseT<ItemType, DataType, AddedFirstLastExtentsType>;
241 static_assert(Extents::rank() >= 0 && Extents::rank() <= 2, "Only Extents of rank 0, 1 or 2 are implemented");
242 static_assert(std::is_same_v<DataType, BasicType>, "DataType should be a basic type (Real, Int32, Int64, ... )");
243
244 public:
245
246 using ItemLocalIdType = typename ItemType::LocalIdType;
247 using ReferenceType = DataViewGetterSetter<NumVectorType>;
248 using ConstReferenceType = DataViewGetter<NumVectorType>;
250 static constexpr int nb_dynamic = Extents::nb_dynamic;
251
252 public:
253
254 explicit MeshVectorMDVariableRefT(const VariableBuildInfo& b)
255 : BaseClass(b)
256 {}
257
258 public:
259
260 //! \name Operations for variable of dimension MDDim0
261 ///@{
262 //! Accesses the data for reading/writing
263 ReferenceType operator()(ItemLocalIdType id)
264 requires(Extents::rank() == 0)
265 {
266 return ReferenceType(m_vector_mdspan.ptrAt(id.localId()));
267 }
268
269 //! Accesses the data for reading
270 ConstReferenceType operator()(ItemLocalIdType id) const
271 requires(Extents::rank() == 0)
272 {
273 return ConstReferenceType(m_vector_mdspan.ptrAt(id.localId()));
274 }
275 ///@}
276
277 //! \name Operations for variable of dimension MDDim1
278 ///@{
279 //! Accesses the data for reading/writing
280 ReferenceType operator()(ItemLocalIdType id, Int32 i1)
281 requires(Extents::rank() == 1)
282 {
283 return ReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1));
284 }
285
286 //! Accesses the data for reading
287 ConstReferenceType operator()(ItemLocalIdType id, Int32 i1) const
288 requires(Extents::rank() == 1)
289 {
290 return ConstReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1));
291 }
292 ///@}
293
294 //! \name Operations for variable of dimension MDDim2
295 ///@{
296 //! Accesses the data for reading/writing
297 ReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2)
298 requires(Extents::rank() == 2)
299 {
300 return ReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1, i2));
301 }
302
303 //! Accesses the data for reading
304 ConstReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2) const
305 requires(Extents::rank() == 2)
306 {
307 return ConstReferenceType(m_vector_mdspan.ptrAt(id.localId(), i1, i2));
308 }
309 ///@}
310
311 /*!
312 * \brief Changes the data shape.
313 *
314 * The number of elements in \a dims must correspond to the number of dynamic values
315 * in \a Extents.
316 */
317 void reshape(std::array<Int32, Extents::nb_dynamic> dims)
318 {
319 std::array<Int32, nb_dynamic + 1> full_dims;
320 // We add 'Size' to the end of the dimensions.
321 for (int i = 0; i < nb_dynamic; ++i)
322 full_dims[i] = dims[i];
323 full_dims[nb_dynamic] = Size;
324 ArrayShape shape(full_dims);
325 this->m_underlying_var.resizeAndReshape(shape);
326 }
327
328 protected:
329
330 void updateFromInternal() override
331 {
333 // Positions the value of m_vector_mdspan.
334 // It will have the same dimensions as m_mdspan except that we
335 // remove the last dimension and change the type
336 // from 'DataType' to 'NumVector<DataType,Size>'.
337 DataType* v = this->m_mdspan.to1DSpan().data();
338 NumVectorType* nv = reinterpret_cast<NumVectorType*>(v);
339 m_vector_mdspan = MDSpanType(nv, this->m_mdspan.extents().dynamicExtents());
340 }
341
342 private:
343
344 MDSpanType m_vector_mdspan;
345};
346
347/*---------------------------------------------------------------------------*/
348/*---------------------------------------------------------------------------*/
349/*!
350 * \brief Class managing a multi-dimensional NumMatrix type variable on a
351 * mesh entity.
352 *
353 * The dimension of the matrix is fixed and is given by (Row,Column).
354 *
355 * \warning You need to call the method reshape() before using this kind
356 * of variables.
357 *
358 * For more information, see \ref arcanedoc_core_types_axl_md_variable_use.
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 DataType = DataType_;
367 using NumMatrixType = NumMatrix<DataType, Row, Column>;
368
369 private:
370
371 using BasicType = typename DataTypeTraitsT<DataType>::BasicType;
372 using AddedFirstLastLastExtentsType = typename Extents::template AddedFirstLastLastExtentsType<DynExtent, Row, Column>;
373 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
374 using BaseClass = MeshMDVariableRefBaseT<ItemType, DataType, AddedFirstLastLastExtentsType>;
375 static_assert(Extents::rank() >= 0 && Extents::rank() <= 1, "Only Extents of rank 0 or 1 are implemented");
376 static_assert(std::is_same_v<DataType, BasicType>, "DataType should be a basic type (Real, Int32, Int64, ... )");
377
378 public:
379
380 using ItemLocalIdType = typename ItemType::LocalIdType;
381 using ReferenceType = DataViewGetterSetter<NumMatrixType>;
382 using ConstReferenceType = DataViewGetter<NumMatrixType>;
384 static constexpr int nb_dynamic = Extents::nb_dynamic;
385
386 public:
387
388 explicit MeshMatrixMDVariableRefT(const VariableBuildInfo& b)
389 : BaseClass(b)
390 {}
391
392 public:
393
394 //! \name Operations for variable of dimension MDDim0
395 ///@{
396 //! Mutable view of the matrix for item \a id
397 ReferenceType operator()(ItemLocalIdType id) requires(Extents::rank()==0)
398 {
399 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId()));
400 }
401
402 //! Read-only view of the matrix for item \a id
403 ConstReferenceType operator()(ItemLocalIdType id) const requires(Extents::rank()==0)
404 {
405 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId()));
406 }
407
408 //! Mutable view of the element (i,j) of the matrix for item \a id
409 DataType& operator()(ItemLocalIdType id, Int32 i, Int32 j) requires(Extents::rank() == 0)
410 {
411 return m_matrix_mdspan(id.localId())(i, j);
412 }
413
414 //! Read-only view of the element (i,j) of the matrix for item \a id
415 DataType operator()(ItemLocalIdType id, Int32 i, Int32 j) const requires(Extents::rank() == 0)
416 {
417 return m_matrix_mdspan(id.localId())(i, j);
418 }
419 ///@}
420
421 //! \name Operations for variable of dimension MDDim1
422 ///@{
423 //! Mutable view of the matrix of index \a index for item \a id
424 ReferenceType operator()(ItemLocalIdType id, Int32 index)
425 requires(Extents::rank() == 1)
426 {
427 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId(), index));
428 }
429
430 //! Read-only view of the matrix of index \a index for item \a id
431 ConstReferenceType operator()(ItemLocalIdType id, Int32 index) const
432 requires(Extents::rank() == 1)
433 {
434 return ReferenceType(m_matrix_mdspan.ptrAt(id.localId(), index));
435 }
436
437 //! Mutable view of the element (i,j) of the matrix for item \a id and index \a index
438 DataType& operator()(ItemLocalIdType id, Int32 index, Int32 i, Int32 j)
439 requires(Extents::rank() == 1)
440 {
441 return m_matrix_mdspan(id.localId(), index)(i, j);
442 }
443
444 //! Read-only view of the element (i,j) of the matrix for item \a id and index \a index
445 DataType operator()(ItemLocalIdType id, Int32 index, Int32 i, Int32 j) const
446 requires(Extents::rank() == 1)
447 {
448 return m_matrix_mdspan(id.localId(), index)(i, j);
449 }
450 ///@}
451
452 /*!
453 * \brief Changes the data shape.
454 *
455 * The number of elements in \a dims must correspond to the number of dynamic values
456 * in \a Extents.
457 */
458 void reshape(std::array<Int32, Extents::nb_dynamic> dims)
459 {
460 std::array<Int32, nb_dynamic + 2> full_dims;
461 // We add 'Row' and 'Column' to the end of the dimensions.
462 for (int i = 0; i < nb_dynamic; ++i)
463 full_dims[i] = dims[i];
464 full_dims[nb_dynamic] = Row;
465 full_dims[nb_dynamic + 1] = Column;
466 ArrayShape shape(full_dims);
467 this->m_underlying_var.resizeAndReshape(shape);
468 }
469
470 protected:
471
472 void updateFromInternal() override
473 {
475 // Positions the value of m_vector_mdspan.
476 // It will have the same dimensions as m_mdspan except that we
477 // remove the last dimension and change the type
478 // from 'DataType' to 'NumMatrix<DataType,Row,Column>'.
479 DataType* v = this->m_mdspan.to1DSpan().data();
480 NumMatrixType* nv = reinterpret_cast<NumMatrixType*>(v);
481 m_matrix_mdspan = MDSpanType(nv, this->m_mdspan.extents().dynamicExtents());
482 }
483
484 private:
485
486 MDSpanType m_matrix_mdspan;
487};
488
489/*---------------------------------------------------------------------------*/
490/*---------------------------------------------------------------------------*/
491
492} // namespace Arcane
493
494/*---------------------------------------------------------------------------*/
495/*---------------------------------------------------------------------------*/
496
497#endif
__host__ static __device__ ArrayExtentsBase fromSpan(SmallSpan< const ExtentIndexType > 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 multidimensional 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.
DataType & operator()(ItemLocalIdType id, Int32 index, Int32 i, Int32 j)
Mutable view of the element (i,j) of the matrix for item id and index index.
ReferenceType operator()(ItemLocalIdType id, Int32 index)
DataType & operator()(ItemLocalIdType id, Int32 i, Int32 j)
Mutable view of the element (i,j) of the matrix for item id.
DataType operator()(ItemLocalIdType id, Int32 index, Int32 i, Int32 j) const
Read-only view of the element (i,j) of the matrix for item id and index index.
void updateFromInternal() override
Updates from the internal part.
void reshape(std::array< Int32, Extents::nb_dynamic > dims)
Changes the data shape.
ConstReferenceType operator()(ItemLocalIdType id) const
Read-only view of the matrix for item id.
DataType operator()(ItemLocalIdType id, Int32 i, Int32 j) const
Read-only view of the element (i,j) of the matrix for item id.
ConstReferenceType operator()(ItemLocalIdType id, Int32 index) const
Read-only view of the matrix of index index for item id.
ReferenceType operator()(ItemLocalIdType id)
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.
ReferenceType operator()(ItemLocalIdType id, Int32 i1)
ConstReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2) const
Accesses the data for reading.
ConstReferenceType operator()(ItemLocalIdType id) const
Accesses the data for reading.
void updateFromInternal() override
Updates from the internal part.
ConstReferenceType operator()(ItemLocalIdType id, Int32 i1) const
Accesses the data for reading.
ReferenceType operator()(ItemLocalIdType id, Int32 i1, Int32 i2)
ReferenceType operator()(ItemLocalIdType id)
Small fixed-size matrix containing RowSize rows and ColumnSize columns.
Definition NumMatrix.h:42
Small fixed-size vector of N numerical data points.
Definition NumVector.h:43
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.