NiHu  2.0
mesh.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 
24 #ifndef NIHU_MESH_HPP_INCLUDED
25 #define NIHU_MESH_HPP_INCLUDED
26 
27 #include "../util/crtp_base.hpp"
28 #include "../util/eigen_utils.hpp"
29 #include "../util/type2tag.hpp"
30 
31 #include "../tmp/integer.hpp"
32 #include "../tmp/sequence.hpp"
33 #include "../tmp/algorithm.hpp"
34 #include "../tmp/control.hpp"
35 #include "../tmp/vector.hpp"
36 
37 #include "element.hpp"
38 
39 namespace NiHu
40 {
41 
45 template <class ElemType>
47 {
50 };
51 
52 
57 template <class xType>
59 {
60 public:
61  typedef xType x_t;
62  static unsigned const nDim = x_t::SizeAtCompileTime;
69  void add_point(x_t const &p)
70  {
71  points.push_back(p);
72  }
73 
78  size_t get_num_points() const
79  {
80  return points.size();
81  }
82 
83 protected:
85 };
86 
88 template <class ElemTypeVector>
90 {
91  typedef typename tmp::deref<
93  >::type::x_t type;
94 };
95 
96 
97 template <class Derived>
98 class mesh_base
99 {
100 public:
102 };
103 
104 
109 template <class ElemTypeVector>
110 class mesh
111  : public mesh_base<mesh<ElemTypeVector>>
112  , public field_points<typename first_elements_x_type<ElemTypeVector>::type>
113 {
114 public:
116  typedef ElemTypeVector elem_type_vector_t;
117 
120 
122  static unsigned const nDim = base_t::nDim;
123 
125  typedef typename tmp::inherit<
126  typename tmp::transform<
131  >,
133  >::type
135 
137  typedef typename base_t::x_t x_t;
138 
140  typedef typename x_t::Scalar scalar_t;
141 
142  template <class ElemType>
143  struct elem_iterator_t : mesh_elem_iterator_t<ElemType> {};
144 
145 
146 protected:
147  template <class elem_t>
148  struct elem_adder { struct type {
154  bool operator() (unsigned const input[], mesh &m)
155  {
156  if (input[0] == elem_t::id)
157  {
158  // construct element
159  typename elem_t::nodes_t nodes;
160  typename elem_t::coords_t coords;
161  for (unsigned i = 0; i < elem_t::num_nodes; ++i)
162  {
163  nodes[i] = input[i+1];
164  coords.col(i) = m.points[nodes[i]];
165  }
166  m.push_element(elem_t(coords, m.m_num_elements++, nodes));
167  return true;
168  }
169  return false;
170  }
171  };};
172 
173 public:
174  mesh() : m_num_elements(0)
175  {
176  }
177 
185  template <class node_t, class elem_t>
186  mesh(node_t const &nodes, elem_t const &elements) : m_num_elements(0)
187  {
188  unsigned const N_MAX_ELEM = 1+9;
189 
190  for (unsigned i = 0; i < unsigned(nodes.rows()); ++i)
191  {
192  double c[nDim];
193  for (unsigned j = 0; j < nDim; ++j)
194  c[j] = nodes(i,j);
195  add_node(c);
196  }
197  for (unsigned i = 0; i < unsigned(elements.rows()); ++i)
198  {
199  unsigned e[N_MAX_ELEM] = {0};
200  for (unsigned j = 0; j < unsigned(elements.cols()); ++j)
201  e[j] = unsigned(elements(i,j));
202  add_elem(e);
203  }
204  }
205 
209  template <class ElemType>
211  {
212  return m_elements.eigen_std_vector<ElemType>::type::begin();
213  }
214 
218  template <class ElemType>
220  {
221  return m_elements.eigen_std_vector<ElemType>::type::end();
222  }
223 
227  template <class ElemType>
228  ElemType const &get_elem(size_t i) const
229  {
230  return m_elements.eigen_std_vector<ElemType>::type::operator[](i);
231  }
232 
239  bool add_elem(unsigned const input[])
240  {
241  return tmp::call_until<
244  unsigned const*,
245  mesh &
246  >(input, *this);
247  }
248 
253  void add_node(scalar_t input[])
254  {
255  x_t c;
256  for (unsigned i = 0; i < nDim; ++i)
257  c[i] = input[i];
258  this->add_point(c);
259  }
260 
266  template <class elem_t>
268  {
269  m_elements.eigen_std_vector<elem_t>::type::push_back(e.derived());
270  return *(m_elements.eigen_std_vector<elem_t>::type::rbegin());
271  }
272 
277  unsigned get_num_elements() const
278  {
279  return m_num_elements;
280  }
281 
282 protected:
284  unsigned m_num_elements;
285 };
286 
287 
296 template <class nodes_t, class elements_t, class...Args>
298  create_mesh(nodes_t const &nodes, elements_t const &elements, Args...)
299 {
300  return mesh<tmp::vector<typename tag2type<Args>::type...> >(nodes, elements);
301 }
302 
303 
304 template <class Mesh, class Elem>
306  : public mesh_base<homogeneous_submesh<Mesh, Elem>>
307 {
308 public:
309  typedef Mesh mesh_t;
310  typedef Elem elem_t;
311 
312  homogeneous_submesh(mesh_t const &parent) : m_parent(parent)
313  {
314  }
315 
317  typename mesh_t::template elem_iterator_t<elem_t>::type begin() const
318  {
319  return m_parent.template begin<elem_t>();
320  }
321 
323  typename mesh_t::template elem_iterator_t<elem_t>::type end() const
324  {
325  return m_parent.template end<elem_t>();
326  }
327 
328  Elem const &get_elem(int i) const
329  {
330  return m_parent.template get_elem<elem_t>(i);
331  }
332 
333 private:
334  mesh_t const &m_parent;
335 };
336 
342 template <class Mesh, class Tag>
343 homogeneous_submesh<Mesh, typename tag2type<Tag>::type>
345 {
347 }
348 
349 }
350 
351 #endif /* NIHU_MESH_HPP_INCLUDED */
352 
NiHu::mesh::base_t
field_points< typename first_elements_x_type< ElemTypeVector >::type > base_t
type of base class
Definition: mesh.hpp:119
tmp::inherit
combine a sequence of classes so that the result is inherited from each element
Definition: algorithm.hpp:125
NiHu::eigen_std_vector
Convert T to an std::vector<T> with Eigen allocator.
Definition: eigen_utils.hpp:41
NiHu::create_homogeneous_submesh
homogeneous_submesh< Mesh, typename tag2type< Tag >::type > create_homogeneous_submesh(Mesh const &mesh, Tag)
factory function to create a homogeneous submesh from a mesh
Definition: mesh.hpp:344
NiHu::mesh::nDim
static const unsigned nDim
number of dimensions of the mesh
Definition: mesh.hpp:122
NiHu::volume_element
class describing a volume element that has no normal vector
Definition: element.hpp:455
NiHu::mesh::x_t
base_t::x_t x_t
type of a nodal vector
Definition: mesh.hpp:137
tmp::deref
metafunction to dereference an iterator
Definition: sequence.hpp:114
NiHu::mesh::scalar_t
x_t::Scalar scalar_t
type of a nodal coordinate
Definition: mesh.hpp:140
NiHu::mesh::add_elem
bool add_elem(unsigned const input[])
add a new element to the mesh
Definition: mesh.hpp:239
NiHu::create_mesh
mesh< tmp::vector< typename tag2type< Args >::type... > > create_mesh(nodes_t const &nodes, elements_t const &elements, Args...)
factory function to create a mesh from nodes and elements matrices
Definition: mesh.hpp:298
NiHu::mesh_elem_iterator_t::type
eigen_std_vector< ElemType >::type::const_iterator type
the iterator that traverses a submesh's element container
Definition: mesh.hpp:49
NiHu::mesh::m_num_elements
unsigned m_num_elements
total number of elements in the mesh
Definition: mesh.hpp:284
NiHu::mesh::elem_adder::type
Definition: mesh.hpp:148
NiHu::volume_element::coords_t
element_traits::coords_type< Derived >::type coords_t
matrix type that stores the element's corner nodes
Definition: element.hpp:235
tmp::begin
return begin iterator of a sequence
Definition: sequence.hpp:79
NiHu::mesh::get_num_elements
unsigned get_num_elements() const
return number of elements
Definition: mesh.hpp:277
NiHu::mesh::begin
elem_iterator_t< ElemType >::type begin() const
return begin iterator of the elements
Definition: mesh.hpp:210
NiHu::field_points::points
eigen_std_vector< x_t >::type points
nodal coordinates
Definition: mesh.hpp:84
NIHU_CRTP_HELPERS
#define NIHU_CRTP_HELPERS
define CRTP helper function
Definition: crtp_base.hpp:29
NiHu::field_points::get_num_points
size_t get_num_points() const
return number of points
Definition: mesh.hpp:78
NiHu::mesh::push_element
const elem_t & push_element(element_base< elem_t > const &e)
add a new element to the mesh
Definition: mesh.hpp:267
NiHu::mesh::elem_adder::type::operator()
bool operator()(unsigned const input[], mesh &m)
add an element of given type to the mesh
Definition: mesh.hpp:154
NiHu::field_points::nDim
static const unsigned nDim
number of dimensions
Definition: mesh.hpp:62
NiHu::first_elements_x_type
metafunction computing the first element's x_t in a vector of elements
Definition: mesh.hpp:89
tmp::empty
return empty sequence
Definition: sequence.hpp:64
NiHu::homogeneous_submesh
Definition: mesh.hpp:305
NiHu::homogeneous_submesh::end
mesh_t::template elem_iterator_t< elem_t >::type end() const
return end iterator of the elements
Definition: mesh.hpp:323
NiHu::volume_element::nodes_t
Eigen::Matrix< unsigned, num_nodes, 1 > nodes_t
vector type that stores the element's corner node indices
Definition: element.hpp:233
NiHu::homogeneous_submesh::begin
mesh_t::template elem_iterator_t< elem_t >::type begin() const
return begin iterator of the elements
Definition: mesh.hpp:317
NiHu::field_points::add_point
void add_point(x_t const &p)
add a point to the field point mesh
Definition: mesh.hpp:69
NiHu::mesh::add_node
void add_node(scalar_t input[])
add a new node to the mesh
Definition: mesh.hpp:253
element.hpp
Declaration of element classes.
NiHu::mesh::elem_container_t
tmp::inherit< typename tmp::transform< elem_type_vector_t, tmp::inserter< typename tmp::empty< elem_type_vector_t >::type, tmp::push_back< tmp::_1, tmp::_2 > >, eigen_std_vector< tmp::_1 > >::type >::type elem_container_t
combine elem_vector into a BIG heterogeneous std::vector container
Definition: mesh.hpp:134
NiHu::field_points::iterator_t
eigen_std_vector< x_t >::type::const_iterator iterator_t
node iterator type
Definition: mesh.hpp:63
NiHu::mesh::m_elements
elem_container_t m_elements
element geometries (BIG heterogeneous container)
Definition: mesh.hpp:283
NiHu::mesh::elem_adder
Definition: mesh.hpp:148
tmp::push_back
push an element to the back
Definition: sequence.hpp:99
NiHu::mesh::elem_type_vector_t
ElemTypeVector elem_type_vector_t
define template parameter as nested type
Definition: mesh.hpp:116
NiHu::mesh::get_elem
const ElemType & get_elem(size_t i) const
return element with a given index
Definition: mesh.hpp:228
NiHu::mesh
container class for a mesh
Definition: mesh.hpp:110
NiHu::field_points
container class for field points
Definition: mesh.hpp:58
NiHu::mesh::mesh
mesh(node_t const &nodes, elem_t const &elements)
build the mesh from mesh description matrices
Definition: mesh.hpp:186
NiHu::mesh::elem_iterator_t
Definition: mesh.hpp:143
tmp::transform
transform elements in a sequence using a user-specified metafunctor and an inserter
Definition: algorithm.hpp:212
NiHu::element_base
The geometrical element representation.
Definition: element.hpp:194
NiHu::mesh::end
elem_iterator_t< ElemType >::type end() const
return end iterator of the elements
Definition: mesh.hpp:219
NiHu::field_points::x_t
xType x_t
template parameter as nested type
Definition: mesh.hpp:61
tmp::inserter
a compile time inserter
Definition: sequence.hpp:118
NiHu::mesh_base
Definition: mesh.hpp:98
NiHu::mesh_elem_iterator_t
metafunction returning the iterator that traverses the homogeneous element vector of specified elemen...
Definition: mesh.hpp:46