LifeV
ConvertBareMesh.hpp
Go to the documentation of this file.
1 /********************************************************************************
2  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
3  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
4 
5  This file is part of LifeV.
6 
7  LifeV is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  LifeV is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
19 ********************************************************************************/
20 
21 /**
22  * @file ConvertBareMesh.hpp
23  * @brief Convert a BareMesh into a RegionMesh.
24  * @author Simone Pezzuto <simone.pezzuto@mail.polimi.it>
25  * @date 10/2012
26 **/
27 
28 #ifndef CONVERT_BARE_MESH_HPP__
29 #define CONVERT_BARE_MESH_HPP__
30 
31 #include <lifev/core/LifeV.hpp>
32 #include <lifev/core/mesh/MeshElementBare.hpp>
33 #include <lifev/core/mesh/MeshChecks.hpp>
34 #include <lifev/core/mesh/BareMesh.hpp>
35 #include <lifev/core/mesh/RegionMesh.hpp>
36 
37 namespace LifeV
38 {
39 
40 namespace
41 {
42 
43 template <int dim> struct convert_spec;
44 
45 template <class S, class MC>
46 struct converter
47 {
48 
49  typedef BareMesh<S> bmesh_t;
50  typedef RegionMesh<S, MC> mesh_t;
51 
52  typedef typename mesh_t::point_Type point_t;
53  typedef typename mesh_t::ridge_Type ridge_t;
54  typedef typename mesh_t::facet_Type facet_t;
55  typedef typename mesh_t::element_Type element_t;
56 
57  static bool convert (bmesh_t& baremesh, mesh_t& mesh, bool verbose = false)
58  {
59  // make sure global id are stored when the mesh is unpartitioned
60  if ( !baremesh.isPartitioned )
61  {
62  if ( baremesh.pointIDs.size() == 0 )
63  {
64  baremesh.pointIDs.resize ( baremesh.points.numberOfColumns() );
65  for ( UInt i = 0; i < baremesh.points.numberOfColumns(); ++i )
66  {
67  baremesh.pointIDs[i] = i;
68  }
69  }
70  if ( baremesh.ridgeIDs.size() == 0 )
71  {
72  baremesh.ridgeIDs.resize ( baremesh.ridges.numberOfColumns() );
73  for ( UInt i = 0; i < baremesh.ridges.numberOfColumns(); ++i )
74  {
75  baremesh.ridgeIDs[i] = i;
76  }
77  }
78  if ( baremesh.facetIDs.size() == 0 )
79  {
80  baremesh.facetIDs.resize ( baremesh.facets.numberOfColumns() );
81  for ( UInt i = 0; i < baremesh.facets.numberOfColumns(); ++i )
82  {
83  baremesh.facetIDs[i] = i;
84  }
85  }
86  if ( baremesh.elementIDs.size() == 0 )
87  {
88  baremesh.elementIDs.resize ( baremesh.elements.numberOfColumns() );
89  for ( UInt i = 0; i < baremesh.elements.numberOfColumns(); ++i )
90  {
91  baremesh.elementIDs[i] = i;
92  }
93  }
94  }
95 
96  // ============
97  // begin.POINTS
98  // ============
99  {
100  UInt numberPoints = baremesh.points.numberOfColumns();
101 
102  mesh.setIsPartitioned (baremesh.isPartitioned);
103  // Update mesh containers
104  mesh.setMaxNumPoints (numberPoints, true);
105  mesh.setMaxNumGlobalPoints (numberPoints);
106  mesh.setNumVertices ( baremesh.numVertices );
107  mesh.setNumGlobalVertices ( baremesh.numVertices );
108  mesh.setNumBPoints ( baremesh.numBoundaryPoints );
109  mesh.setNumBVertices ( baremesh.numBoundaryVertices );
110  // Add the points
111  for (UInt i = 0; i < numberPoints; ++i)
112  {
113  // Add a generic point (vertex selection is made later)
114  point_t& p = mesh.addPoint (false, false);
115  // Coordinates
116  p.x() = baremesh.points (0, i);
117  p.y() = baremesh.points (1, i);
118  p.z() = baremesh.points (2, i);
119  // global Id
120  p.setId (baremesh.pointIDs[i]);
121  // Marker
122  p.setMarkerID (baremesh.pointMarkers[i]);
123  }
124  }
125  if (verbose)
126  std::cout << "[INFO:convertBareMesh] Added "
127  << mesh.numPoints() << " marked points." << std::endl;
128 
129  // ==============
130  // begin.ELEMENTS
131  // ==============
132  {
133  // Update containers
134  UInt numberElements = baremesh.elements.numberOfColumns();
135  mesh.setMaxNumGlobalElements (numberElements);
136  mesh.setMaxNumElements (numberElements, true);
137  mesh.setNumElements (numberElements);
138  for (UInt i = 0; i < numberElements; ++i)
139  {
140  // Add the element
141  element_t& e = mesh.addElement();
142  e.setId (baremesh.elementIDs[i]);
143  e.setMarkerID (baremesh.elementMarkers[i]);
144  // Points
145  for (UInt j = 0; j < element_t::S_numPoints; ++j)
146  {
147  UInt pid = baremesh.elements (j, i);
148  e.setPoint (j, mesh.point (pid) );
149  // Mark vertices
150  if (j < element_t::S_numVertices)
151  {
152  mesh.pointList (pid).setFlag (LifeV::EntityFlags::VERTEX);
153  }
154  }
155  }
156  // Update number of vertices
157  UInt numVertices = mesh.pointList.countElementsWithFlag (
158  LifeV::EntityFlags::VERTEX,
159  &LifeV::Flag::testAllSet);
160  mesh.setNumVertices (numVertices);
161  }
162  if (verbose)
163  std::cout << "[INFO:convertBareMesh] Added "
164  << mesh.numElements() << " marked elements." << "\n"
165  << "[INFO:convertBareMesh] Found "
166  << mesh.numVertices() << " vertices." << std::endl;
167 
168  // Set the region marker
169  mesh.setMarkerID (baremesh.regionMarkerID);
170  mesh.setId (baremesh.regionMarkerID);
171 
172  // dimension specific entities
173  convert_spec<S::S_nDimensions>::add_facets (baremesh, mesh, verbose);
174  convert_spec<S::S_nDimensions>::add_ridges (baremesh, mesh, verbose);
175 
176  // discard baremesh
177  baremesh.clear();
178 
179  return convert_spec<S::S_nDimensions>::check (mesh, verbose);
180 
181  }
182 
183 };
184 
185 // Generic conversion (say 3d)
186 template <int dim>
187 struct convert_spec
188 {
189 
190  template <class S, class MC>
191  static void add_facets (BareMesh<S>& baremesh, RegionMesh<S, MC>& mesh, bool verbose)
192  {
193  // ============
194  // begin.FACETS
195  // ============
196  typedef RegionMesh<S, MC> mesh_t;
197  typedef typename mesh_t::facet_Type facet_t;
198  if (verbose)
199  std::cout << "[INFO:convertBareMesh] Updating facets"
200  << std::endl;
201  // Update containers
202  UInt numberFacets = baremesh.facets.numberOfColumns();
203  mesh.setMaxNumFacets (numberFacets);
204  mesh.setMaxNumGlobalFacets (numberFacets);
205  mesh.setNumFacets (numberFacets);
206  mesh.setNumBoundaryFacets (0);
207  for (UInt i = 0; i < numberFacets; ++i)
208  {
209  // Add the facet
210  facet_t& f = mesh.addFacet (false);
211  f.setId (baremesh.facetIDs[i]);
212  f.setMarkerID (baremesh.facetMarkers[i]);
213  // Points
214  for (UInt j = 0; j < facet_t::S_numPoints; ++j)
215  {
216  UInt pid = baremesh.facets (j, i);
217  f.setPoint (j, mesh.point (pid) );
218  }
219  }
220  }
221 
222  template <class S, class MC>
223  static void add_ridges (BareMesh<S>& baremesh, RegionMesh<S, MC>& mesh, bool verbose)
224  {
225  // ============
226  // begin.RIDGES
227  // ============
228  typedef RegionMesh<S, MC> mesh_t;
229  typedef typename mesh_t::ridge_Type ridge_t;
230  if (verbose)
231  std::cout << "[INFO:convertBareMesh] Updating ridges"
232  << std::endl;
233  // Update containers
234  UInt numberRidges = baremesh.ridges.numberOfColumns();
235  mesh.setMaxNumRidges (numberRidges);
236  mesh.setMaxNumGlobalRidges (numberRidges);
237  mesh.setNumRidges (numberRidges);
238  mesh.setNumBoundaryRidges (0);
239  for (UInt i = 0; i < numberRidges; ++i)
240  {
241  // Add the facet
242  ridge_t& r = mesh.addRidge (false);
243  r.setId (baremesh.ridgeIDs[i]);
244  r.setMarkerID (baremesh.ridgeMarkers[i]);
245  // Points
246  for (UInt j = 0; j < ridge_t::S_numPoints; ++j)
247  {
248  UInt pid = baremesh.ridges (j, i);
249  r.setPoint (j, mesh.point (pid) );
250  }
251  }
252  }
253 
254  template <class S, class MC>
255  static bool check (RegionMesh<S, MC>& mesh, bool verbose)
256  {
257  Switch sw;
258  std::stringstream discardedLog;
259  std::ostream& oStr = verbose ? std::cout : discardedLog;
260  return checkMesh3D (mesh, sw, true, verbose, oStr, std::cerr, oStr);
261  }
262 
263 };
264 
265 // 2d mesh version
266 template <>
267 struct convert_spec<2>
268 {
269  template <class S, class MC>
270  static void add_facets (BareMesh<S>& baremesh, RegionMesh<S, MC>& mesh, bool verbose)
271  {
272  convert_spec<3>::template add_facets (baremesh, mesh, verbose);
273  }
274 
275  template <class S, class MC>
276  static void add_ridges (BareMesh<S>&, RegionMesh<S, MC>&, bool)
277  {}
278 
279  template <class S, class MC>
280  static bool check (RegionMesh<S, MC>&, bool)
281  {
282  return true;
283  }
284 
285 };
286 
287 // 1d mesh version
288 template <>
289 struct convert_spec<1>
290 {
291  template <class S, class MC>
292  static void add_facets (BareMesh<S>&, RegionMesh<S, MC>&, bool)
293  {}
294 
295  template <class S, class MC>
296  static void add_ridges (BareMesh<S>&, RegionMesh<S, MC>&, bool)
297  {}
298 
299  template <class S, class MC>
300  static bool check (RegionMesh<S, MC>&, bool)
301  {
302  return true;
303  }
304 
305 };
306 
307 }
308 
309 //! convertBareMesh - convert a previously read BareMesh in a RegionMesh object
310 /*!
311  Starting from a BareMesh, this routine generates a fully compliant RegionMesh object
312 
313  @param bareMesh, the bare mesh data structure in input.
314  @param mesh, the mesh data structure to fill in.
315  @param verbose, setting it as true, the output is verbose (the default is false).
316  @return true if everything went fine, false otherwise.
317 */
318 template <typename GeoShapeType, typename MCType>
319 bool
320 convertBareMesh ( BareMesh<GeoShapeType>& bareMesh,
321  RegionMesh<GeoShapeType, MCType>& mesh,
322  bool verbose = false)
323 {
324  return converter<GeoShapeType, MCType>::convert (bareMesh, mesh, verbose);
325 }// Function convertBareMesh
326 
327 } // Namespace LifeV
328 
329 #endif // CONVERT_BARE_MESH_HPP__
static bool convert(bmesh_t &baremesh, mesh_t &mesh, bool verbose=false)
static bool check(RegionMesh< S, MC > &mesh, bool verbose)
static void add_ridges(BareMesh< S > &, RegionMesh< S, MC > &, bool)
bool convertBareMesh(BareMesh< GeoShapeType > &bareMesh, RegionMesh< GeoShapeType, MCType > &mesh, bool verbose=false)
convertBareMesh - convert a previously read BareMesh in a RegionMesh object
static void add_ridges(BareMesh< S > &, RegionMesh< S, MC > &, bool)
I use standard constructor/destructors.
Definition: Switch.hpp:50
static void add_facets(BareMesh< S > &, RegionMesh< S, MC > &, bool)
static void add_facets(BareMesh< S > &baremesh, RegionMesh< S, MC > &mesh, bool verbose)
static void add_facets(BareMesh< S > &baremesh, RegionMesh< S, MC > &mesh, bool verbose)
Class for 3D, 2D and 1D Mesh.
Definition: RegionMesh.hpp:87
static void add_ridges(BareMesh< S > &baremesh, RegionMesh< S, MC > &mesh, bool verbose)
A struct for a bare mesh.
Definition: BareMesh.hpp:53
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191