WaveBlocksND
localQuadratic.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../../types.hpp"
4 #include "../../utilities/evaluations.hpp"
5 
6 #include "taylor.hpp"
7 
8 
9 namespace waveblocks
10 {
11  namespace potentials
12  {
13  namespace modules
14  {
15  namespace localQuadratic
16  {
30  template <class Subtype, class Basis>
31  struct Abstract {
33  IMPORT_TYPES_FROM( Basis)
34 
35 
36  public:
37  potential_evaluation_type evaluate_local_quadratic_at(
38  const argument_type &arg,
39  const argument_type &position ) const {
40  return static_cast<const Subtype*>(this)->evaluate_local_quadratic_at_implementation( arg,position );
41  }
42 
43  template < template <typename...> class grid_in = std::vector,
44  template <typename...> class grid_out = grid_in >
45  grid_out<potential_evaluation_type> evaluate_local_remainder(
46  const grid_in<argument_type > &args,
47  argument_type position ) const {
48  return utilities::evaluate_function_in_grid < argument_type,
49  potential_evaluation_type,
50  grid_in,
51  grid_out,
52  function_t > (
54  this,
55  std::placeholders::_1,
56  position ),
57  args );
58  }
59  };
60 
70  template <class TaylorImpl, class Basis>
71  class Standard : public Abstract<Standard<TaylorImpl, Basis>, Basis>,
72  public TaylorImpl
73  {
74  public:
75  IMPORT_TYPES_FROM( Basis)
76 
77  public:
78  Standard( potential_type potential,
79  jacobian_type jacobian,
80  hessian_type hessian )
81  : TaylorImpl( potential, jacobian, hessian ) {
82  }
83 
84  potential_evaluation_type evaluate_local_quadratic_at_implementation(
85  const argument_type &x,
86  const argument_type &q ) const {
87 
88  potential_evaluation_type result_matrix;
89 
90  auto V_mat = TaylorImpl::evaluate_at(q );
91  auto J_mat = TaylorImpl::evaluate_jacobian_at(q );
92  auto H_mat = TaylorImpl::evaluate_hessian_at(q );
93 
94  for ( int l = 0; l < Basis::number_of_levels; ++l ) {
95  for ( int m = 0; m < Basis::number_of_columns; ++m ) {
96  const auto& V = V_mat(l,m);
97  const auto& J = J_mat(l,m);
98  const auto& H = H_mat(l,m);
99 
100  auto result = V;
101 
102  for ( int i = 0; i < Basis::argument_dimension; ++i ) {
103  auto xmqi = x[i] - q[i];
104  result += J[i] * ( xmqi );
105 
106  for ( int j = 0; j < Basis::argument_dimension; ++j ) {
107  result += 0.5 * xmqi * H( i, j ) * ( x[j] - q[j] );
108  }
109  }
110 
111  result_matrix(l,m) = result;
112  }
113  }
114 
115  return result_matrix;
116  }
117  };
118 
132  template <class TaylorImpl, template <int, int, int> class B, int N, int C>
133  class Standard<TaylorImpl, B<N, 1, C>> : public Abstract<Standard<TaylorImpl, B<N,1, C>>, B<N,1, C>>,
134  public TaylorImpl
135  {
136  public:
137  using Basis = B<N,1, C>;
139 
140  public:
141  Standard( potential_type potential,
142  jacobian_type jacobian,
143  hessian_type hessian )
144  : TaylorImpl( potential, jacobian, hessian ) {
145  }
146 
148  const argument_type &x,
149  const argument_type &q ) const {
150  auto xmq = x - q;
151 
152  potential_evaluation_type result_matrix;
153 
154  auto V_mat = TaylorImpl::evaluate_at(q );
155  auto J_mat = TaylorImpl::evaluate_jacobian_at(q );
156  auto H_mat = TaylorImpl::evaluate_hessian_at(q );
157 
158  for ( int l = 0; l < Basis::number_of_levels; ++l ) {
159  for ( int m = 0; m < Basis::number_of_columns; ++m ) {
160  const auto& V = V_mat(l,m);
161  const auto& J = J_mat(l,m);
162  const auto& H = H_mat(l,m);
163 
164 
165  result_matrix(l,m) = V + J*xmq + 0.5*xmq*H*xmq;
166  }
167  }
168 
169  return result_matrix;
170  }
171  };
172 
173 
187  template <class TaylorImpl, template <int, int, int> class B, int D, int C>
188  class Standard<TaylorImpl, B<1, D,C>> : public Abstract <
189  Standard<TaylorImpl, B<1,D,C>>, B<1,D,C> >, public TaylorImpl
190  {
191  public:
192  using Basis = B<1,D,C>;
194 
195  Standard( potential_type potential,
196  jacobian_type jacobian,
197  hessian_type hessian )
198  : TaylorImpl( potential, jacobian, hessian ) {
199  }
200 
202  const argument_type &x,
203  const argument_type &q ) const {
204  auto V = TaylorImpl::evaluate_at( q );
205  auto J = TaylorImpl::evaluate_jacobian_at(q );
206  auto H = TaylorImpl::evaluate_hessian_at(q );
207 
208  auto result = V;
209 
210  for ( int i = 0; i < D; ++i ) {
211  auto xmqi = x[i] - q[i];
212  result += J[i] * ( xmqi );
213 
214  for ( int j = 0; j < D; ++j ) {
215  result += 0.5 * xmqi * H( i, j ) * ( x[j] - q[j] );
216  }
217  }
218 
219  return result;
220  }
221  };
222 
234  template <class TaylorImpl, template <int, int, int> class B, int C>
235  class Standard<TaylorImpl, B<1,1,C>> : public Abstract<Standard<TaylorImpl, B<1, 1,C>>, B<1, 1,C>>,
236  public TaylorImpl
237  {
238  public:
239  using Basis = B<1,1,C>;
241 
242  public:
243  Standard( potential_type potential,
244  jacobian_type jacobian,
245  hessian_type hessian )
246  : TaylorImpl( potential, jacobian, hessian ) {
247  }
248 
250  const argument_type &x,
251  const argument_type &q ) const {
252  auto xmq = x - q;
253 
254  auto V = TaylorImpl::evaluate_at(q );
255  auto J = TaylorImpl::evaluate_jacobian_at(q );
256  auto H = TaylorImpl::evaluate_hessian_at(q );
257 
258  return V + J*xmq + 0.5*xmq*H*xmq;
259  }
260 
261  };
262  }
263  template <class Basis>
265  }
266  }
267 }
potential_evaluation_type evaluate_local_quadratic_at_implementation(const argument_type &x, const argument_type &q) const
Definition: localQuadratic.hpp:249
Definition: coefficients_file_parser.cpp:10
G_out< R > evaluate_function_in_grid(const F< R(A)> &f, const G_in< A > &g)
Evaluate a function in multiple points at once.
Definition: evaluations.hpp:130
potential_evaluation_type evaluate_local_quadratic_at_implementation(const argument_type &x, const argument_type &q) const
Definition: localQuadratic.hpp:84
Abstract class for local quadratic evaluation.
Definition: localQuadratic.hpp:31
potential_evaluation_type evaluate_local_quadratic_at(const argument_type &arg, const argument_type &position) const
Definition: localQuadratic.hpp:37
std::function< P > function_t
Definition: types.hpp:57
potential_evaluation_type evaluate_local_quadratic_at_implementation(const argument_type &x, const argument_type &q) const
Definition: localQuadratic.hpp:201
Standard(potential_type potential, jacobian_type jacobian, hessian_type hessian)
Definition: localQuadratic.hpp:78
#define IMPORT_TYPES_FROM(B)
Definition: bases.hpp:3
grid_out< potential_evaluation_type > evaluate_local_remainder(const grid_in< argument_type > &args, argument_type position) const
Definition: localQuadratic.hpp:45
potential_evaluation_type evaluate_local_quadratic_at_implementation(const argument_type &x, const argument_type &q) const
Definition: localQuadratic.hpp:147
Helper class for easier template specialization.
Definition: localQuadratic.hpp:71