Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
RealArray2Variant.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/* RealArray2Variant.h (C) 2000-2024 */
9/* */
10/* Variant that can contain the types ConstArray2View, Real2x2, and Real3x3. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_DATATYPE_REALARRAY2VARIANT_H
14#define ARCANE_DATATYPE_REALARRAY2VARIANT_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/Array2.h"
19#include "arcane/utils/Array2View.h"
20#include "arcane/utils/Real2x2.h"
21#include "arcane/utils/Real3x3.h"
22#if defined(ARCANE_HAS_ACCELERATOR_API)
23#include "arcane/utils/NumArray.h"
24#endif
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
39class RealArray2Variant
40{
41 public:
42
43 static const Integer MAX_DIM1_SIZE = 3;
44 static const Integer MAX_DIM2_SIZE = 3;
45
46 RealArray2Variant() = default;
47 RealArray2Variant(UniqueArray2<Real> v)
48 : RealArray2Variant(v.constView())
49 {}
50 RealArray2Variant(ConstArray2View<Real> v)
51 {
52 _setValue(v.data(), v.dim1Size(), v.dim2Size());
53 }
54 RealArray2Variant(Real2x2 r)
55 {
56 _setValue(reinterpret_cast<Real*>(&r), 2, 2);
57 }
58 RealArray2Variant(Real3x3 r)
59 {
60 _setValue(reinterpret_cast<Real*>(&r), 3, 3);
61 }
62
63#if defined(ARCANE_HAS_ACCELERATOR_API)
64 template <typename LayoutType>
65 RealArray2Variant(const NumArray<Real, MDDim2, LayoutType>& v)
66 : RealArray2Variant(v.mdspan())
67 {}
68 template <typename LayoutType>
69 RealArray2Variant(MDSpan<Real, MDDim2, LayoutType> v)
70 {
71 _setValue(v.to1DSpan().data(), v.extent0(), v.extent1());
72 }
73 template <typename LayoutType>
75 {
76 _setValue(v.to1DSpan().data(), v.extent0(), v.extent1());
77 }
78#endif
79
80 RealArray2Variant& operator=(const RealArray2Variant& rhs) = default;
81 RealArray2Variant& operator=(ConstArray2View<Real> v)
82 {
83 _setValue(v.data(), v.dim1Size(), v.dim2Size());
84 return (*this);
85 }
86 RealArray2Variant& operator=(Real2x2 r)
87 {
88 _setValue(reinterpret_cast<Real*>(&r), 2, 2);
89 return (*this);
90 }
91 RealArray2Variant& operator=(Real3x3 r)
92 {
93 _setValue(reinterpret_cast<Real*>(&r), 3, 3);
94 return (*this);
95 }
96
97 Real* operator[](Integer index)
98 {
99 ARCANE_ASSERT(index < m_nb_dim1, ("Index out of range"));
100 return m_value[index];
101 }
102 const Real* operator[](Integer index) const
103 {
104 ARCANE_ASSERT(index < m_nb_dim1, ("Index out of range"));
105 return m_value[index];
106 }
107
108 Real& operator()(Int32 i, Int32 j)
109 {
110 ARCANE_ASSERT(i < m_nb_dim1, ("Index i out of range"));
111 ARCANE_ASSERT(j < m_nb_dim2, ("Index j out of range"));
112 return m_value[i][j];
113 }
114 Real operator()(Int32 i, Int32 j) const
115 {
116 ARCANE_ASSERT(i < m_nb_dim1, ("Index i out of range"));
117 ARCANE_ASSERT(j < m_nb_dim2, ("Index j out of range"));
118 return m_value[i][j];
119 }
120
121 Int32 dim1Size() const { return m_nb_dim1; }
122 Int32 dim2Size() const { return m_nb_dim2; }
123 Real* data() { return reinterpret_cast<Real*>(&m_value[0]); }
124 const Real* data() const { return reinterpret_cast<const Real*>(m_value); }
125 operator ConstArray2View<Real>() const
126 {
127 return ConstArray2View<Real>(data(), m_nb_dim1, m_nb_dim2);
128 }
129 operator Real2x2() const
130 {
131 return Real2x2::fromLines(m_value[0][0], m_value[0][1], m_value[1][0], m_value[1][1]);
132 }
133 operator Real3x3() const
134 {
135 return Real3x3::fromLines(m_value[0][0], m_value[0][1], m_value[0][2],
136 m_value[1][0], m_value[1][1], m_value[1][2],
137 m_value[2][0], m_value[2][1], m_value[2][2]);
138 }
139#if defined(ARCANE_HAS_ACCELERATOR_API)
140 template <typename LayoutType>
142 {
143 NumArray<Real, MDDim2> v(m_nb_dim1, m_nb_dim2);
144 for (Integer i = 0, m = m_nb_dim1; i < m; ++i)
145 for (Integer j = 0, n = m_nb_dim2; j < n; ++j)
146 v(i, j) = m_value[i][j];
147 return v;
148 }
149#endif
150
151 private:
152
153 Real m_value[MAX_DIM1_SIZE][MAX_DIM2_SIZE];
154 Int32 m_nb_dim1 = 0;
155 Int32 m_nb_dim2 = 0;
156
157 private:
158
159 void _setValue(const Real* v, Integer nb_dim1, Integer nb_dim2)
160 {
161 m_nb_dim1 = nb_dim1;
162 ARCANE_ASSERT(nb_dim1 <= MAX_DIM1_SIZE, ("Dim1 size too large"));
163 m_nb_dim2 = nb_dim2;
164 ARCANE_ASSERT(nb_dim2 <= MAX_DIM2_SIZE, ("Dim2 size too large"));
165 for (Integer i = 0; i < nb_dim1; ++i)
166 for (Integer j = 0; j < nb_dim2; ++j)
167 m_value[i][j] = v[i * nb_dim2 + j];
168 }
169};
170
171/*---------------------------------------------------------------------------*/
172/*---------------------------------------------------------------------------*/
173
174} // namespace Arcane
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
179#endif
constexpr const DataType * data() const
Pointer to the allocated memory.
Base class for multi-dimensional views.
constexpr __host__ __device__ Int32 extent1() const
Value of the second dimension.
constexpr __host__ __device__ Int32 extent0() const
Value of the first dimension.
Multi-dimensional arrays for numerical types accessible on accelerators.
Class managing a 2x2 matrix of reals.
Definition Real2x2.h:55
constexpr __host__ static __device__ Real2x2 fromLines(Real ax, Real bx, Real ay, Real by)
Constructs the pair ((ax,bx),(ay,by)).
Definition Real2x2.h:127
Class managing a 3x3 real matrix.
Definition Real3x3.h:67
constexpr __host__ static __device__ Real3x3 fromLines(Real ax, Real bx, Real cx, Real ay, Real by, Real cy, Real az, Real bz, Real cz)
Constructs the matrix ((ax,bx,cx),(ay,by,cy),(az,bz,cz)).
Definition Real3x3.h:151
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:539
2D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.