BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
concrete_grid.hpp
1 // Copyright (C) 2011-2012 by the BEM++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef bempp_concrete_grid_hpp
22 #define bempp_concrete_grid_hpp
23 
24 #include "../common/common.hpp"
25 #include "../common/ensure_not_null.hpp"
26 #include "grid_parameters.hpp"
27 #include "grid_factory.hpp"
28 #include "../common/shared_ptr.hpp"
29 
30 #include "grid.hpp"
31 #include "concrete_domain_index.hpp"
32 #include "concrete_entity.hpp"
33 #include "concrete_geometry_factory.hpp"
34 #include "concrete_grid_view.hpp"
35 #include "concrete_id_set.hpp"
36 #include <armadillo>
37 
38 #include <memory>
39 
40 namespace Bempp
41 {
42 
44 template<int codim> class Entity;
45 class GridView;
55 template<typename DuneGrid>
56 class ConcreteGrid: public Grid
57 {
58 private:
59  DuneGrid* m_dune_grid;
60  bool m_owns_dune_grid;
61  GridParameters::Topology m_topology;
63  ConcreteDomainIndex<DuneGrid> m_domain_index;
64 
65 public:
67  typedef DuneGrid DuneGridType;
68 
76  explicit ConcreteGrid(DuneGrid* dune_grid,
77  GridParameters::Topology topology, bool own = false,
78  bool leafIsBarycentric = false) :
79  m_dune_grid(ensureNotNull(dune_grid)),
80  m_topology(topology),
81  m_owns_dune_grid(own),
82  m_global_id_set(&dune_grid->globalIdSet()),
83  m_domain_index(*dune_grid,
84  std::vector<int>(
85  dune_grid->size(0 /*level*/, 0 /*codim*/),
86  0 /*default index*/))
87  {
88 
89  }
90 
96  explicit ConcreteGrid(DuneGrid* dune_grid,
98  const std::vector<int>& domainIndices,
99  bool own = false) :
100  m_dune_grid(ensureNotNull(dune_grid)),
101  m_topology(topology),
102  m_owns_dune_grid(own),
103  m_global_id_set(&dune_grid->globalIdSet()),
104  m_domain_index(*dune_grid, domainIndices)
105  {
106  }
107 
110  if (m_owns_dune_grid)
111  delete m_dune_grid;
112  }
113 
115  const DuneGrid& duneGrid() const {
116  return *m_dune_grid;
117  }
118 
120  DuneGrid& duneGrid() {
121  return *m_dune_grid;
122  }
123 
127  virtual int dimWorld() const {
128  return DuneGrid::dimensionworld;
129  }
130 
131  virtual int dim() const {
132  return DuneGrid::dimension;
133  }
134 
135  virtual int maxLevel() const {
136  return m_dune_grid->maxLevel();
137  }
138 
143  virtual std::auto_ptr<GridView> levelView(size_t level) const {
144  return std::auto_ptr<GridView>(
146  m_dune_grid->levelView(level), m_domain_index));
147  }
148 
149  virtual std::auto_ptr<GridView> leafView() const {
150  return std::auto_ptr<GridView>(
152  m_dune_grid->leafView(), m_domain_index));
153  }
154 
159  virtual std::auto_ptr<GeometryFactory> elementGeometryFactory() const {
160  return std::auto_ptr<GeometryFactory>(
161  new ConcreteGeometryFactory<
162  typename DuneGrid::template Codim<0>::Geometry>());
163  }
164 
169  virtual const IdSet& globalIdSet() const {
170  return m_global_id_set;
171  }
172 
175  virtual GridParameters::Topology topology() const {
176  return m_topology;
177  }
178 
179 
185  virtual shared_ptr<Grid> barycentricGrid() const {
186 
187 
188  if (!m_barycentricGrid.get()){
189  tbb::mutex::scoped_lock lock(m_barycentricSpaceMutex);
190  if (!m_barycentricGrid.get()){
191 
192  arma::Mat<double> vertices;
193  arma::Mat<int> elementCorners;
194  arma::Mat<char> auxData;
195  std::vector<int> domainIndices;
196 
197  std::auto_ptr<GridView> view = this->leafView();
198 
199  view->getRawElementData(vertices,
200  elementCorners,
201  auxData,
202  domainIndices);
203 
204  GridParameters params;
205  params.topology = GridParameters::TRIANGULAR;
206  shared_ptr<Grid> newGrid = GridFactory::createGridFromConnectivityArrays(params,vertices,elementCorners,
207  domainIndices);
208  shared_ptr<ConcreteGrid<DuneGrid> > concreteGrid = dynamic_pointer_cast<ConcreteGrid<DuneGrid> >(newGrid);
209  (concreteGrid->duneGrid()).globalBarycentricRefine(1);
210 
211  m_barycentricGrid = newGrid;
213  }
214  }
215  return m_barycentricGrid;
216  }
217 
220  virtual bool hasBarycentricGrid() const {
221  if (!m_barycentricGrid.get())
222  return false;
223  else
224  return true;
225  }
226 
227 
231 private:
232  // Disable copy constructor and assignment operator
233  // (unclear what to do with the pointer to the grid)
234  ConcreteGrid(const ConcreteGrid&);
235  ConcreteGrid& operator =(const ConcreteGrid&);
236  mutable shared_ptr<Grid> m_barycentricGrid;
237  mutable tbb::mutex m_barycentricSpaceMutex;
238 
239 };
240 
241 } // namespace Bempp
242 
243 #endif
Wrapper of a Dune surface grid of type DuneGrid.
Definition: concrete_grid.hpp:56
virtual const IdSet & globalIdSet() const
Reference to the grid&#39;s global id set.
Definition: concrete_grid.hpp:163
DuneGrid & duneGrid()
Access to the underlying Dune grid object. Use at your own risk!
Definition: concrete_grid.hpp:120
virtual GridParameters::Topology topology() const
Get the grid topology.
Definition: concrete_grid.hpp:169
virtual int dimWorld() const
Dimension of the space containing the grid.
Definition: concrete_grid.hpp:127
virtual int dim() const
Dimension of the grid.
Definition: concrete_grid.hpp:131
Grid parameters.
Definition: grid_parameters.hpp:31
two-dimensional grid composed of triangular elements, embedded in a three-dimensional space ...
Definition: grid_parameters.hpp:38
virtual std::auto_ptr< GeometryFactory > elementGeometryFactory() const
Factory able to construct empty geometries of codimension 0 compatible with this grid.
Definition: concrete_grid.hpp:155
virtual shared_ptr< Grid > barycentricGrid() const
Return a barycentrically refined grid based on the LeafView.
Definition: concrete_grid.hpp:177
virtual bool hasBarycentricGrid() const
Return true if a barycentric refinement of this grid has been created.
Definition: concrete_grid.hpp:212
static shared_ptr< Grid > createGridFromConnectivityArrays(const GridParameters &params, const arma::Mat< double > &vertices, const arma::Mat< int > &elementCorners, const std::vector< int > &domainIndices=std::vector< int >())
Create a grid from connectivity arrays.
Definition: grid_factory.cpp:154
DuneGrid DuneGridType
Underlying Dune grid&#39;s type.
Definition: concrete_grid.hpp:67
Definition: concrete_domain_index.hpp:31
const DuneGrid & duneGrid() const
Read-only access to the underlying Dune grid object.
Definition: concrete_grid.hpp:115
Wrapper of a Dune grid view of type DuneGridView.
Definition: concrete_grid_view.hpp:42
Abstract wrapper of a grid.
Definition: grid.hpp:50
Abstract wrapper of an id set.
Definition: id_set.hpp:37
Topology
Grid topology
Definition: grid_parameters.hpp:33
ConcreteGrid(DuneGrid *dune_grid, GridParameters::Topology topology, const std::vector< int > &domainIndices, bool own=false)
Wrap an existing Dune grid object.
Definition: concrete_grid.hpp:96
ConcreteGrid(DuneGrid *dune_grid, GridParameters::Topology topology, bool own=false, bool leafIsBarycentric=false)
Wrap an existing Dune grid object.
Definition: concrete_grid.hpp:76
~ConcreteGrid()
Destructor.
Definition: concrete_grid.hpp:109
virtual std::auto_ptr< GridView > leafView() const
View of the leaf entities.
Definition: concrete_grid.hpp:147
virtual std::auto_ptr< GridView > levelView(size_t level) const
View of the entities on grid level level.
Definition: concrete_grid.hpp:141
virtual int maxLevel() const
Maximum level defined in this grid.
Definition: concrete_grid.hpp:135