Skip to content

Commit 73e5e37

Browse files
committed
Add TensorialFunction
1 parent b31f0b1 commit 73e5e37

8 files changed

Lines changed: 431 additions & 4 deletions

File tree

include/MGIS/Function/EvaluatorConcept.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ namespace mgis::function {
225225
* the getSpace function
226226
*/
227227
template <EvaluatorConcept EvaluatorType>
228-
decltype(auto) disambiguateGetSpace(const EvaluatorType&);
228+
constexpr decltype(auto) disambiguateGetSpace(const EvaluatorType&);
229229

230230
/*!
231231
* \brief number of components of a type when known at compile-time,

include/MGIS/Function/EvaluatorConcept.ixx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace mgis::function::internals {
1212

1313
template <EvaluatorConcept EvaluatorType>
14-
decltype(auto) disambiguateGetSpace(const EvaluatorType& e) {
14+
constexpr decltype(auto) disambiguateGetSpace(const EvaluatorType& e) {
1515
return getSpace(e);
1616
}
1717

include/MGIS/Function/FunctionConcept.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ namespace mgis::function {
339339
* the getSpace function
340340
*/
341341
template <FunctionConcept FunctionType>
342-
decltype(auto) disambiguateGetSpace(const FunctionType&) //
342+
constexpr decltype(auto) disambiguateGetSpace(const FunctionType&) //
343343
requires(!EvaluatorConcept<FunctionType>);
344344

345345
/*!

include/MGIS/Function/FunctionConcept.ixx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace mgis::function::internals {
1515

1616
template <FunctionConcept FunctionType>
17-
decltype(auto) disambiguateGetSpace(const FunctionType& f) //
17+
constexpr decltype(auto) disambiguateGetSpace(const FunctionType& f) //
1818
requires(!EvaluatorConcept<FunctionType>) {
1919
return getSpace(f);
2020
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*!
2+
* \file MGIS/Function/TensorialFunction.hxx
3+
* \brief
4+
* \author Thomas Helfer
5+
* \date 28/08/2025
6+
*/
7+
8+
#ifndef LIB_MGIS_FUNCTION_TENSORIALFUNCTION_HXX
9+
#define LIB_MGIS_FUNCTION_TENSORIALFUNCTION_HXX
10+
11+
#include "MGIS/Function/Function.hxx"
12+
#include "MGIS/Function/Tensors.hxx"
13+
14+
#ifdef MGIS_HAVE_TFEL
15+
16+
namespace mgis::function {
17+
18+
/*!
19+
* \brief default implementation of a function
20+
*
21+
* \tparam Space: type of the functional space
22+
* \tparam TensorType: tensor type
23+
*
24+
* \note the data stride is equal to the data size
25+
*/
26+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
27+
requires(LinearElementSpaceConcept<Space> ||
28+
LinearQuadratureSpaceConcept<Space>) //
29+
struct TensorialFunction
30+
: private PreconditionsChecker<TensorialFunction<Space, TensorType>>,
31+
private Function<Space,
32+
tfel::math::getUnderlyingArrayMinimalSize<
33+
typename TensorType::indexing_policy>()>,
34+
public TensorView<Function<Space,
35+
tfel::math::getUnderlyingArrayMinimalSize<
36+
typename TensorType::indexing_policy>()>,
37+
TensorType> {
38+
//! \brief a simple alias
39+
using UnderlyingFunctionType =
40+
Function<Space,
41+
tfel::math::getUnderlyingArrayMinimalSize<
42+
typename TensorType::indexing_policy>()>;
43+
//! \brief a simple alias
44+
using UnderlyingTensorView =
45+
TensorView<Function<Space,
46+
tfel::math::getUnderlyingArrayMinimalSize<
47+
typename TensorType::indexing_policy>()>,
48+
TensorType>;
49+
/*!
50+
* \brief constructor from a space and a data size
51+
* \param[in] eh: error handler
52+
* \param[in] s: space
53+
*/
54+
[[nodiscard]] static constexpr bool checkPreconditions(
55+
AbstractErrorHandler&, const Space&);
56+
/*!
57+
* \brief constructor from a space
58+
* Vieparam[in] s: space
59+
*/
60+
constexpr TensorialFunction(const Space&);
61+
/*!
62+
* \brief constructor from a space
63+
* \param[in] s: space
64+
*/
65+
template <bool doPreconditionsCheck>
66+
constexpr TensorialFunction(const PreconditionsCheck<doPreconditionsCheck>&,
67+
const Space&);
68+
//! \brief move constructor
69+
constexpr TensorialFunction(TensorialFunction&&);
70+
//! \brief copy constructor
71+
constexpr TensorialFunction(const TensorialFunction&);
72+
//! \brief return a view of the function
73+
constexpr UnderlyingTensorView& view() noexcept;
74+
//! \brief return a view of the function
75+
constexpr const UnderlyingTensorView& view() const noexcept;
76+
//
77+
using UnderlyingFunctionType::getSpace;
78+
using UnderlyingFunctionType::getNumberOfComponents;
79+
using UnderlyingTensorView::operator();
80+
using UnderlyingFunctionType::data;
81+
// /*!
82+
// * \brief fill the structure using raw data
83+
// *
84+
// * \param[in] eh: error handler
85+
// * \param[in] values: raw data
86+
// */
87+
// constexpr bool fill(AbstractErrorHandler&, std::span<const real>)
88+
// noexcept;
89+
// /*!
90+
// * \brief fill the structure using raw data
91+
// *
92+
// * \param[in] eh: error handler
93+
// * \param[in] values: raw data
94+
// */
95+
// constexpr bool fill(AbstractErrorHandler&,
96+
// std::initializer_list<real>) noexcept;
97+
//! \brief destructor
98+
constexpr ~TensorialFunction();
99+
100+
protected:
101+
/*
102+
* This function is made protected to avoid Function from being treated
103+
* as an evaluator
104+
*/
105+
using TensorView<Function<Space,
106+
tfel::math::getUnderlyingArrayMinimalSize<
107+
typename TensorType::indexing_policy>()>,
108+
TensorType>::check;
109+
/*
110+
* This function is made protected to avoid Function from being treated as
111+
* an evaluator
112+
*/
113+
using TensorView<Function<Space,
114+
tfel::math::getUnderlyingArrayMinimalSize<
115+
typename TensorType::indexing_policy>()>,
116+
TensorType>::allocateWorkspace;
117+
};
118+
119+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
120+
constexpr decltype(auto)
121+
getSpace(const TensorialFunction<Space, TensorType>&);
122+
123+
template <FunctionalSpaceConcept Space, unsigned short N>
124+
using StensorFunction =
125+
TensorialFunction<Space, tfel::math::stensor<N, real>>;
126+
template <FunctionalSpaceConcept Space, unsigned short N>
127+
using TensorFunction = TensorialFunction<Space, tfel::math::tensor<N, real>>;
128+
template <FunctionalSpaceConcept Space, unsigned short N>
129+
using ST2toST2Function =
130+
TensorialFunction<Space, tfel::math::st2tost2<N, real>>;
131+
template <FunctionalSpaceConcept Space, unsigned short N>
132+
using T2toST2Function =
133+
TensorialFunction<Space, tfel::math::t2tost2<N, real>>;
134+
template <FunctionalSpaceConcept Space, unsigned short N>
135+
using ST2toT2Function =
136+
TensorialFunction<Space, tfel::math::st2tot2<N, real>>;
137+
template <FunctionalSpaceConcept Space, unsigned short N>
138+
using T2toT2Function =
139+
TensorialFunction<Space, tfel::math::t2tot2<N, real>>;
140+
141+
142+
/*!
143+
* \brief convert a tensorial function to a immutable view
144+
* \param[in] f: function
145+
*/
146+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
147+
constexpr auto view(const TensorialFunction<Space, TensorType>&);
148+
149+
// // class template deduction guide
150+
// template <FunctionalSpaceConcept SpaceType>
151+
// TensorialFunction(const SpaceType&, const size_type)
152+
// -> Function<SpaceType, dynamic_extent>;
153+
154+
} // end of namespace mgis::function
155+
156+
#endif /* MGIS_HAVE_TFEL */
157+
158+
#include "MGIS/Function/TensorialFunction.ixx"
159+
160+
#endif /* LIB_MGIS_FUNCTION_TENSORIALFUNCTION_HXX */
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*!
2+
* \file MGIS/Function/TensorialFunction.ixx
3+
* \brief
4+
* \author Thomas Helfer
5+
* \date 28/08/2025
6+
*/
7+
8+
#ifndef LIB_MGIS_FUNCTION_TENSORIALFUNCTION_IXX
9+
#define LIB_MGIS_FUNCTION_TENSORIALFUNCTION_IXX
10+
11+
#ifdef MGIS_HAVE_TFEL
12+
13+
namespace mgis::function {
14+
15+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
16+
requires(LinearElementSpaceConcept<Space> ||
17+
LinearQuadratureSpaceConcept<Space>) //
18+
constexpr TensorialFunction<Space, TensorType>::TensorialFunction(
19+
const Space& s)
20+
: TensorialFunction(preconditions_check, s) {
21+
} // end of TensorialFunction
22+
23+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
24+
requires(LinearElementSpaceConcept<Space> ||
25+
LinearQuadratureSpaceConcept<Space>) //
26+
constexpr bool TensorialFunction<Space, TensorType>::checkPreconditions(
27+
AbstractErrorHandler&, const Space&) {
28+
return true;
29+
}
30+
31+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
32+
requires(LinearElementSpaceConcept<Space> ||
33+
LinearQuadratureSpaceConcept<Space>) //
34+
template <bool doPreconditionsCheck>
35+
constexpr TensorialFunction<Space, TensorType>::TensorialFunction(
36+
const PreconditionsCheck<doPreconditionsCheck>& pcheck,
37+
const Space& s)
38+
: PreconditionsChecker<TensorialFunction>(pcheck, s),
39+
Function<Space,
40+
tfel::math::getUnderlyingArrayMinimalSize<
41+
typename TensorType::indexing_policy>()>(s),
42+
TensorView<Function<Space,
43+
tfel::math::getUnderlyingArrayMinimalSize<
44+
typename TensorType::indexing_policy>()>,
45+
TensorType>(
46+
pcheck,
47+
static_cast<Function<Space,
48+
tfel::math::getUnderlyingArrayMinimalSize<
49+
typename TensorType::indexing_policy>()>&>(
50+
*this)) {} // end of TensorialFunction
51+
52+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
53+
requires(LinearElementSpaceConcept<Space> ||
54+
LinearQuadratureSpaceConcept<Space>) //
55+
constexpr TensorView<
56+
Function<Space,
57+
tfel::math::getUnderlyingArrayMinimalSize<
58+
typename TensorType::indexing_policy>()>,
59+
TensorType>& //
60+
TensorialFunction<Space, TensorType>::view() noexcept {
61+
return *this;
62+
} // end of view
63+
64+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
65+
requires(LinearElementSpaceConcept<Space> ||
66+
LinearQuadratureSpaceConcept<Space>) //
67+
constexpr const
68+
TensorView<Function<Space,
69+
tfel::math::getUnderlyingArrayMinimalSize<
70+
typename TensorType::indexing_policy>()>,
71+
TensorType>& //
72+
TensorialFunction<Space, TensorType>::view() const noexcept {
73+
return *this;
74+
} // end of view
75+
76+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
77+
requires(LinearElementSpaceConcept<Space> ||
78+
LinearQuadratureSpaceConcept<Space>) //
79+
constexpr TensorialFunction<Space, TensorType>::~TensorialFunction() =
80+
default;
81+
82+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
83+
constexpr decltype(auto)
84+
getSpace(const TensorialFunction<Space, TensorType>& f) {
85+
return f.getSpace();
86+
}
87+
88+
template <FunctionalSpaceConcept Space, TensorConcept TensorType>
89+
constexpr auto view(const TensorialFunction<Space, TensorType>& f){
90+
return f.view();
91+
} // end of view
92+
93+
} // end of namespace mgis::function
94+
95+
#endif /* MGIS_HAVE_TFEL */
96+
97+
#endif /* LIB_MGIS_FUNCTION_TENSORIALFUNCTION_HXX */

tests/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,20 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
440440
endif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14.2.0)
441441
endif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
442442

443+
add_executable(TensorialFunctionTest
444+
EXCLUDE_FROM_ALL
445+
TensorialFunctionTest.cxx)
446+
target_link_libraries(TensorialFunctionTest
447+
PRIVATE MFrontGenericInterface tfel::TFELTests)
448+
add_test(NAME TensorialFunctionTest COMMAND TensorialFunctionTest)
449+
add_dependencies(check TensorialFunctionTest)
450+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
451+
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14.2.0)
452+
target_compile_definitions(TensorialFunctionTest
453+
PRIVATE MGIS_DISABLE_CONSTEXPR_FUNCTION_TESTS)
454+
endif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14.2.0)
455+
endif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
456+
443457
add_executable(ReduceTest
444458
EXCLUDE_FROM_ALL
445459
ReduceTest.cxx)

0 commit comments

Comments
 (0)