NiHu  2.0
quadrature.hpp
Go to the documentation of this file.
1 // This file is a part of NiHu, a C++ BEM template library.
2 //
3 // Copyright (C) 2012-2014 Peter Fiala <fiala@hit.bme.hu>
4 // Copyright (C) 2012-2014 Peter Rucz <rucz@hit.bme.hu>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
22 #ifndef QUADRATURE_HPP_INCLUDED
23 #define QUADRATURE_HPP_INCLUDED
24 
25 #include "shapeset.hpp"
26 #include "../library/lib_shape.hpp"
27 #include "../util/eigen_utils.hpp"
28 #include "../util/crtp_base.hpp"
29 
30 namespace NiHu
31 {
32 
38 template <class XiType, class ScalarType>
40 {
41 public:
43  typedef XiType xi_t;
45  typedef ScalarType scalar_t;
46 
52  quadrature_elem(xi_t const &xi = xi_t(), scalar_t const &w = scalar_t())
53  : m_xi(xi), m_w(w)
54  {
55  }
56 
60  xi_t &get_xi(void)
61  {
62  return m_xi;
63  }
64 
69  xi_t const &get_xi(void) const
70  {
71  return m_xi;
72  }
73 
78  scalar_t const &get_w(void) const
79  {
80  return m_w;
81  }
82 
87  void set_w(scalar_t const &w)
88  {
89  m_w = w;
90  }
91 
92  void set_xi(xi_t const& xi)
93  {
94  m_xi = xi;
95  }
96 
102  {
103  m_w *= c;
104  return *this;
105  }
106 
114  template <class LSet, bool Signed = true>
116  Eigen::Matrix<scalar_t, LSet::num_nodes, LSet::domain_t::dimension> const &coords)
117  {
118  // CRTP check
119  static_assert(std::is_base_of<shape_set_base<LSet>, LSet>::value,
120  "LSet must be derived from shape_set_base<LSet>");
121  // compute Jacobian and multiply with original weight
122  auto j = (LSet::template eval_shape<1>(m_xi).transpose() * coords).determinant();
123  if (Signed)
124  m_w *= j;
125  else
126  m_w *= std::abs(j);
127  // compute new quadrature location
128  m_xi = LSet::template eval_shape<0>(m_xi).transpose() * coords;
129  return *this;
130  }
131 
132  // struct is used in a std::vector, therefore this declaration is necessary
133  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
134 
135 protected:
140 };
141 
142 
143 // forward declaration of quadrature traits
144 template <class Derived>
146 
148 template <class Derived>
150 {
151  typedef quadrature_elem<
154  > type;
155 };
156 
161 template <class Derived>
163  public eigen_std_vector<typename quadr_elem<Derived>::type>::type
164 {
165 public:
168 
170 
171 public:
175  typedef typename traits_t::domain_t domain_t;
177  typedef typename domain_t::xi_t xi_t;
179  typedef typename domain_t::scalar_t scalar_t;
182 
187  quadrature_base(size_t N = 0)
188  {
189  base_t::reserve(N);
190  }
191 
197  {
198  scalar_t res = 0.0;
199  for (auto it = base_t::begin(); it != base_t::end(); ++it)
200  res += it->get_w();
201  return res;
202  }
203 
209  std::ostream & print(std::ostream & os) const
210  {
211  os << base_t::size() << std::endl;
212  for (auto it = base_t::begin(); it != base_t::end(); ++it)
213  os << it->get_xi().transpose() << "\t\t" << it->get_w() << std::endl;
214  return os;
215  }
216 
222  Derived &operator *=(scalar_t const &c)
223  {
224  for (size_t i = 0; i < base_t::size(); ++i)
225  (*this)[i] *= c;
226  return derived();
227  }
228 
237  template <class LSet, bool Signed = true>
238  Derived transform(
239  Eigen::Matrix<scalar_t, LSet::num_nodes, LSet::domain_t::dimension> const &coords) const
240  {
241  // CRTP check
242  static_assert(std::is_base_of<shape_set_base<LSet>, LSet>::value,
243  "LSet must be derived from shape_set_base<LSet>");
244  // dimension check
245  static_assert(std::is_same<xi_t, typename LSet::xi_t>::value,
246  "Quadrature and shape set dimensions must match");
247  Derived result(derived());
248  return result.template transform_inplace<LSet, Signed>(coords);
249  }
250 
258  template <class LSet, bool Signed = true>
260  const Eigen::Matrix<scalar_t, LSet::num_nodes, LSet::domain_t::dimension> &coords)
261  {
262  // CRTP check
263  static_assert(std::is_base_of<shape_set_base<LSet>, LSet>::value,
264  "LSet must be derived from shape_set_base<LSet>");
265  // dimension check
266  static_assert(std::is_same<xi_t, typename LSet::xi_t>::value,
267  "Quadrature and shape set dimensions must match");
268  for (auto it = base_t::begin(); it != base_t::end(); ++it)
269  it->template transform_inplace<LSet, Signed>(coords);
270  return derived();
271  }
272 
280  template <class otherDerived>
281  Derived operator +(const quadrature_base<otherDerived> &other) const
282  {
283  static_assert(std::is_same<xi_t, typename otherDerived::xi_t>::value,
284  "Quadrature domain dimensions must match");
285  Derived result(base_t::size()+other.size());
286  result.insert(result.end(), base_t::begin(), base_t::end());
287  result.insert(result.end(), other.begin(), other.end());
288  return result;
289  }
290 
298  template <class otherDerived>
300  {
301  static_assert(std::is_same<xi_t, typename otherDerived::xi_t>::value,
302  "Quadrature domain dimensions must match");
303  base_t::reserve(base_t::size()+other.size());
304  base_t::insert(base_t::end(), other.begin(), other.end());
305  return derived();
306  }
307 }; // class quadrature_base
308 
316 template<class Derived>
317 std::ostream & operator << (std::ostream & os, const quadrature_base<Derived>& Q)
318 {
319  return Q.print(os);
320 }
321 
325 template <class Family, class Domain>
327 
328 }
329 
330 
331 #endif
NiHu::eigen_std_vector
Convert T to an std::vector<T> with Eigen allocator.
Definition: eigen_utils.hpp:41
NiHu::quadrature_traits
Definition: quadrature.hpp:145
NiHu::quadrature_elem::m_w
scalar_t m_w
weight
Definition: quadrature.hpp:139
NiHu::quadrature_base::scalar_t
domain_t::scalar_t scalar_t
local scalar type
Definition: quadrature.hpp:179
NiHu::quadrature_base::xi_t
domain_t::xi_t xi_t
local coordinate type
Definition: quadrature.hpp:177
NiHu::quadrature_base::transform_inplace
Derived & transform_inplace(const Eigen::Matrix< scalar_t, LSet::num_nodes, LSet::domain_t::dimension > &coords)
transform the domain of the quadrature in place
Definition: quadrature.hpp:259
NiHu::quadrature_base::domain_t
traits_t::domain_t domain_t
domain type
Definition: quadrature.hpp:175
NiHu::quadrature_elem::set_w
void set_w(scalar_t const &w)
set quadrature weight
Definition: quadrature.hpp:87
NiHu::quadrature_elem::scalar_t
ScalarType scalar_t
template argument as nested type
Definition: quadrature.hpp:45
NIHU_CRTP_HELPERS
#define NIHU_CRTP_HELPERS
define CRTP helper function
Definition: crtp_base.hpp:29
NiHu::quadrature_base
CRTP base class of all quadratures.
Definition: quadrature.hpp:162
NiHu::quadrature_elem
a quadrature element is a base point and a weight
Definition: quadrature.hpp:39
NiHu::shape_set_base
Shapeset base class for CRTP.
Definition: shapeset.hpp:195
NiHu::quadrature_elem::operator*=
quadrature_elem & operator*=(scalar_t const &c)
multiply a quadrature element by a scalar
Definition: quadrature.hpp:101
NiHu::quadrature_type
metafunction to assign a quadrature type to a quadrature family and a domain
Definition: quadrature.hpp:326
NiHu::quadrature_elem::transform_inplace
quadrature_elem & transform_inplace(Eigen::Matrix< scalar_t, LSet::num_nodes, LSet::domain_t::dimension > const &coords)
transform a quadrature element according to a shape function set and corner nodes
Definition: quadrature.hpp:115
NiHu::quadrature_base::quadrature_elem_t
quadrature_elem< xi_t, scalar_t > quadrature_elem_t
quadrature elem type
Definition: quadrature.hpp:181
NiHu::quadrature_base::sum_of_weights
scalar_t sum_of_weights(void) const
return sum of quadrature weights
Definition: quadrature.hpp:196
NiHu::quadr_elem
metafunction to assign a quadrature element to a quadrature
Definition: quadrature.hpp:149
NiHu::quadrature_base::transform
Derived transform(Eigen::Matrix< scalar_t, LSet::num_nodes, LSet::domain_t::dimension > const &coords) const
transform the domain of the quadrature with a given shape set and corner points
Definition: quadrature.hpp:238
NiHu::quadrature_elem::get_xi
const xi_t & get_xi(void) const
return constant reference to base point
Definition: quadrature.hpp:69
NiHu::quadrature_base::print
std::ostream & print(std::ostream &os) const
print a quadrature into an output stream
Definition: quadrature.hpp:209
NiHu::quadrature_elem::get_xi
xi_t & get_xi(void)
return reference to base point
Definition: quadrature.hpp:60
NiHu::quadrature_base::quadrature_base
quadrature_base(size_t N=0)
constructor allocating space for the quadrature elements
Definition: quadrature.hpp:187
NiHu::quadrature_base::base_t
eigen_std_vector< typename quadr_elem< Derived >::type >::type base_t
the base vector class of the quadrature
Definition: quadrature.hpp:167
NiHu::quadrature_base::traits_t
quadrature_traits< Derived > traits_t
traits type
Definition: quadrature.hpp:173
NiHu::quadrature_elem::m_xi
xi_t m_xi
base point
Definition: quadrature.hpp:137
NiHu::quadrature_elem::quadrature_elem
quadrature_elem(xi_t const &xi=xi_t(), scalar_t const &w=scalar_t())
constructor initialising all members
Definition: quadrature.hpp:52
NiHu::quadrature_base::operator+
Derived operator+(const quadrature_base< otherDerived > &other) const
add two quadratures
Definition: quadrature.hpp:281
shapeset.hpp
Definition of shape function sets.
NiHu::quadrature_elem::xi_t
XiType xi_t
template argument as tested type
Definition: quadrature.hpp:43
NiHu::quadrature_elem::get_w
const scalar_t & get_w(void) const
return constant reference to weight
Definition: quadrature.hpp:78
NiHu::quadrature_base::operator+=
Derived & operator+=(const quadrature_base< otherDerived > &other)
add another quadrature to this
Definition: quadrature.hpp:299
NiHu::quadrature_base::operator*=
Derived & operator*=(scalar_t const &c)
multiply the quadrature by a scalar
Definition: quadrature.hpp:222