Tatooine
binary_operation_field.h
Go to the documentation of this file.
1#ifndef TATOOINE_BINARY_OPERATION_FIELD_H
2#define TATOOINE_BINARY_OPERATION_FIELD_H
3//==============================================================================
4#include <tatooine/field.h>
5//==============================================================================
6namespace tatooine {
7//==============================================================================
8template <typename LHSInternalField, typename RHSInternalField, typename Op>
10 : field<binary_operation_field<LHSInternalField, RHSInternalField, Op>,
11 common_type<field_real_type<LHSInternalField>,
12 field_real_type<RHSInternalField>>,
13 field_num_dimensions<LHSInternalField>,
14 std::invoke_result_t<Op,
15 field_tensor_type<LHSInternalField>,
16 field_tensor_type<RHSInternalField>>> {
17 static_assert(field_num_dimensions<LHSInternalField> ==
18 field_num_dimensions<RHSInternalField>);
19
20 public:
26 field_num_dimensions<LHSInternalField>,
27 std::invoke_result_t<Op, field_tensor_type<LHSInternalField>,
29 using typename parent_type::pos_type;
30 using typename parent_type::real_type;
31 using typename parent_type::tensor_type;
32 //----------------------------------------------------------------------------
33 // members
34 //----------------------------------------------------------------------------
35 private:
36 LHSInternalField m_lhs;
37 RHSInternalField m_rhs;
38 Op m_op;
39 //----------------------------------------------------------------------------
40 // ctors
41 //----------------------------------------------------------------------------
42 public:
44 : m_lhs{other.m_lhs}, m_rhs{other.m_rhs}, m_op{other.m_op} {}
45 //----------------------------------------------------------------------------
47 : m_lhs{std::move(other.m_lhs)},
48 m_rhs{std::move(other.m_rhs)},
49 m_op{std::move(other.m_op)} {}
50 //----------------------------------------------------------------------------
51 template <typename LHS, typename Rhs, typename Op_>
52 constexpr binary_operation_field(LHS&& lhs, Rhs&& rhs, Op_&& op)
53 : m_lhs{std::forward<LHSInternalField>(lhs)},
54 m_rhs{std::forward<RHSInternalField>(rhs)},
55 m_op{std::forward<Op>(op)} {}
56 //----------------------------------------------------------------------------
57 // assignement operators
58 //----------------------------------------------------------------------------
59 public:
60 constexpr auto operator=(binary_operation_field const& other)
62 m_lhs = other.m_lhs;
63 m_rhs = other.m_rhs;
64 return *this;
65 }
66 constexpr auto operator=(binary_operation_field&& other) noexcept
68 m_lhs = std::move(other.m_lhs);
69 m_rhs = std::move(other.m_rhs);
70 return *this;
71 }
72
73 public:
74 //----------------------------------------------------------------------------
75 // dtor
76 //----------------------------------------------------------------------------
77 ~binary_operation_field() override = default;
78 //----------------------------------------------------------------------------
79 // methods
80 //----------------------------------------------------------------------------
81 constexpr auto evaluate(pos_type const& x, real_type const t) const
82 -> tensor_type final {
83 return m_op(lhs()(x, t), rhs()(x, t));
84 }
85 auto lhs() const -> auto const& {
86 if constexpr (is_pointer<LHSInternalField>) {
87 return *m_lhs;
88 } else {
89 return m_lhs;
90 }
91 }
92 auto rhs() const -> auto const& {
93 if constexpr (is_pointer<RHSInternalField>) {
94 return *m_rhs;
95 } else {
96 return m_rhs;
97 }
98 }
99 //----------------------------------------------------------------------------
100 auto set_v0(LHSInternalField lhs)
101 -> void requires(is_pointer<LHSInternalField>) {
102 m_lhs = lhs;
103 }
104 //----------------------------------------------------------------------------
105 auto set_v1(RHSInternalField rhs)
106 -> void requires(is_pointer<RHSInternalField>) {
107 m_rhs = rhs;
108 }
109 //----------------------------------------------------------------------------
110 auto fields_available() const -> bool requires(
111 is_pointer<LHSInternalField>&& is_pointer<RHSInternalField>) {
112 return m_lhs != nullptr && m_rhs != nullptr;
113 }
114};
115//==============================================================================
116template <typename LHSInternalField, typename LHSReal, size_t N,
117 typename LHSTensor, typename RHSInternalField, typename RHSReal,
118 typename RHSTensor, typename Op>
121 const field<RHSInternalField, RHSReal, N, RHSTensor>& rhs, const Op& op) {
125 lhs, rhs, op};
126}
127//------------------------------------------------------------------------------
128template <typename LHSInternalField, typename LHSReal, size_t N,
129 typename LHSTensor, typename RHSInternalField, typename RHSReal,
130 typename RHSTensor, typename Op>
133 const field<RHSInternalField, RHSReal, N, RHSTensor>& rhs, const Op& op) {
137 std::move(lhs), rhs, op};
138}
139//------------------------------------------------------------------------------
140template <typename LHSInternalField, typename LHSReal, size_t N,
141 typename LHSTensor, typename RHSInternalField, typename RHSReal,
142 typename RHSTensor, typename Op>
149 lhs, std::move(rhs), op};
150}
151//------------------------------------------------------------------------------
152template <typename LHSInternalField, typename LHSReal, size_t N,
153 typename LHSTensor, typename RHSInternalField, typename RHSReal,
154 typename RHSTensor, typename Op>
160 const Op&>{std::move(lhs), std::move(rhs), op};
161}
162//------------------------------------------------------------------------------
163template <typename LHSInternalField, typename LHSReal, size_t N,
164 typename LHSTensor, typename RHSInternalField, typename RHSReal,
165 typename RHSTensor, typename Op>
172 std::move(op)};
173}
174//------------------------------------------------------------------------------
175template <typename LHSInternalField, typename LHSReal, size_t N,
176 typename LHSTensor, typename RHSInternalField, typename RHSReal,
177 typename RHSTensor, typename Op>
184 std::move(lhs), rhs, std::move(op)};
185}
186//------------------------------------------------------------------------------
187template <typename LHSInternalField, typename LHSReal, size_t N,
188 typename LHSTensor, typename RHSInternalField, typename RHSReal,
189 typename RHSTensor, typename Op>
195 field<RHSInternalField, RHSReal, N, RHSTensor>, Op>{lhs, std::move(rhs),
196 std::move(op)};
197}
198//------------------------------------------------------------------------------
199template <typename LHSInternalField, typename LHSReal, size_t N,
200 typename LHSTensor, typename RHSInternalField, typename RHSReal,
201 typename RHSTensor, typename Op>
207 Op>{std::move(lhs), std::move(rhs),
208 std::move(op)};
209}
210//------------------------------------------------------------------------------
211template <typename LHSReal, size_t N, typename LHSTensor, typename RHSReal,
212 typename RHSTensor, typename Op>
219 std::move(op)};
220}
221//------------------------------------------------------------------------------
222template <typename RealOut, size_t NOut, size_t... TensorDimsOut,
223 typename LHSReal, size_t N, typename LHSTensor, typename RHSReal,
224 typename RHSTensor, typename Op>
228 return make_binary_operation_field(&lhs, &rhs, std::forward<Op>(op));
229}
230//==============================================================================
231} // namespace tatooine
232//==============================================================================
233#endif
Definition: algorithm.h:6
typename common_type_impl< Ts... >::type common_type
Definition: common_type.h:23
constexpr auto make_binary_operation_field(const field< LHSInternalField, LHSReal, N, LHSTensor > &lhs, const field< RHSInternalField, RHSReal, N, RHSTensor > &rhs, const Op &op)
Definition: binary_operation_field.h:119
typename std::decay_t< std::remove_pointer_t< std::decay_t< Field > > >::real_type field_real_type
Definition: field_type_traits.h:10
static constexpr forward_tag forward
Definition: tags.h:9
static constexpr auto is_pointer
Definition: type_traits.h:22
typename std::decay_t< std::remove_pointer_t< std::decay_t< Field > > >::tensor_type field_tensor_type
Definition: field_type_traits.h:14
Definition: binary_operation_field.h:16
auto set_v0(LHSInternalField lhs) -> void requires(is_pointer< LHSInternalField >)
Definition: binary_operation_field.h:100
constexpr binary_operation_field(binary_operation_field &&other) noexcept
Definition: binary_operation_field.h:46
auto rhs() const -> auto const &
Definition: binary_operation_field.h:92
constexpr auto evaluate(pos_type const &x, real_type const t) const -> tensor_type final
Definition: binary_operation_field.h:81
constexpr binary_operation_field(binary_operation_field const &other)
Definition: binary_operation_field.h:43
constexpr binary_operation_field(LHS &&lhs, Rhs &&rhs, Op_ &&op)
Definition: binary_operation_field.h:52
LHSInternalField m_lhs
Definition: binary_operation_field.h:36
binary_operation_field< LHSInternalField, RHSInternalField, Op > this_type
Definition: binary_operation_field.h:21
Op m_op
Definition: binary_operation_field.h:38
RHSInternalField m_rhs
Definition: binary_operation_field.h:37
auto set_v1(RHSInternalField rhs) -> void requires(is_pointer< RHSInternalField >)
Definition: binary_operation_field.h:105
auto lhs() const -> auto const &
Definition: binary_operation_field.h:85
~binary_operation_field() override=default
constexpr auto operator=(binary_operation_field &&other) noexcept -> binary_operation_field &
Definition: binary_operation_field.h:66
auto fields_available() const -> bool requires(is_pointer< LHSInternalField > &&is_pointer< RHSInternalField >)
Definition: binary_operation_field.h:110
constexpr auto operator=(binary_operation_field const &other) -> binary_operation_field &
Definition: binary_operation_field.h:60
Definition: field.h:134
Real real_type
Definition: field.h:17
vec< real_type, NumDimensions > pos_type
Definition: field.h:20
Tensor tensor_type
Definition: field.h:18
Definition: field.h:13
Definition: vec.h:12