Skip to content

Commit 87f80c0

Browse files
committed
Add slice routines and vector conversions
to support idaholab/moose#32023
1 parent b6381bf commit 87f80c0

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

include/numerics/type_n_tensor.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,16 @@ class TypeNTensor
6565

6666
TypeNTensor & operator=(const TypeNTensor<N, T> &) { return *this; }
6767

68-
operator TypeVector<T> () const { libmesh_not_implemented(); return 0; }
69-
operator VectorValue<T> () const { libmesh_not_implemented(); return 0; }
68+
operator TypeVector<T>() const
69+
{
70+
libmesh_assert(N == 1);
71+
return TypeVector<T>(_coords[0], _coords[1], _coords[2]);
72+
}
73+
operator VectorValue<T>() const
74+
{
75+
libmesh_assert(N == 1);
76+
return VectorValue<T>(_coords[0], _coords[1], _coords[2]);
77+
}
7078

7179
operator TypeTensor<T> () const { libmesh_not_implemented(); return 0; }
7280
operator TensorValue<T> () const { libmesh_not_implemented(); return 0; }
@@ -79,21 +87,28 @@ class TypeNTensor
7987
/**
8088
* \returns A proxy for the \f$ i^{th} \f$ slice of the tensor.
8189
*/
82-
const TypeNTensor<N-1,T> slice (const unsigned int /*i*/) const
90+
const TypeNTensor<N - 1, T> slice(const unsigned int i) const
8391
{
84-
libmesh_not_implemented();
85-
return TypeNTensor<N-1,T>();
92+
return const_cast<TypeNTensor<N, T> *>(this)->slice(i);
8693
}
8794

8895
/**
8996
* \returns A writable proxy for the \f$ i^{th} \f$ slice of the tensor.
9097
*/
91-
TypeNTensor<N-1,T> slice (const unsigned int /*i*/)
98+
TypeNTensor<N - 1, T> slice(const unsigned int i)
9299
{
93-
libmesh_not_implemented();
94-
return TypeNTensor<N-1,T>();
100+
libmesh_assert(i <= N);
101+
TypeNTensor<N - 1, T> slice;
102+
unsigned int slice_size = int_pow(LIBMESH_DIM, N - 1);
103+
slice._coords.resize(slice_size);
104+
for (unsigned int j = 0; j < slice_size; j++)
105+
slice._coords[j] = _coords[i * slice_size + j];
106+
return slice;
95107
}
96108

109+
/**
110+
* Zero out a tensor by setting it with = 0
111+
*/
97112
template <typename Scalar>
98113
typename boostcopy::enable_if_c<
99114
ScalarTraits<Scalar>::value,
@@ -322,7 +337,10 @@ class TypeNTensor
322337
void add_scaled (const TypeNTensor<N, T2> &, const T &);
323338

324339
/**
325-
* The coordinates of the \p TypeNTensor
340+
* The coordinates of the \p TypeNTensor.
341+
* The vector is indexed such that slice i is contiguous.
342+
* In dimension 2, this would mean that slice i is the ith row,
343+
* and _coords has row-major ordering and column minor-ordering
326344
*/
327345
std::vector<T> _coords;
328346

0 commit comments

Comments
 (0)