LifeV
InternalEntitySelector.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief It contains the standard selector for internal entities
30 
31  @author
32  @contributor Nur Aiman Fadel <nur.fadel@mail.polimi.it>
33  @maintainer Nur Aiman Fadel <nur.fadel@mail.polimi.it>
34 
35  @date
36 
37  A more detailed description of the file (if necessary)
38  */
39 
40 #ifndef _SELECTMARKER_HH_
41 #define _SELECTMARKER_HH_ 1
42 
43 #include <functional>
44 #include <lifev/core/mesh/Marker.hpp>
45 #include <lifev/core/mesh/MeshEntityContainer.hpp>
46 
47 namespace LifeV
48 {
49 
50 /** InternalEntitySelector.
51  *
52  * Functor class that tells whether a marker ID corresponds to an internal face
53  * @author Luca Formaggia
54  * @see SetFlagAccordingToWatermarks
55  *
56  * This class takes in input an EntityFlag and it can be used <br>
57  * in order to understand if the input is or not an internal face.
58  *
59  * @deprecated OBSOLETE, now this is handled through SetFlagAccordingToWatermarks
60  */
61 class InternalEntitySelector
62 {
63 public:
64  //! The default watermark used when standard constructor is adopted.
65  static const markerID_Type defMarkFlag;
66 
67  /** @name Constructors & Destructor */
68  //@{
69 
70  //! Empty Constructor
71  InternalEntitySelector();
72 
73  //! Short description of the constructor
74  /*!
75  It is a constructor which requires the EntityFlag
76  @param w, the costant EntityFlag which is required in order
77  to create an InternalEntitySelector object.
78  */
79  explicit InternalEntitySelector ( const markerID_Type& w );
80  //@}
81 
82  //! @name Operators
83  //@{
84 
85  //! The round brackets operator
86  /*!
87  Operator returning true if the flag corresponds to internal entity.
88  If the EntityFlag is greater that the watermark, the associated geometry entity is internal.
89  @param test, it is the reference to geometric entity.
90  @return true, if the flag corresponds to an internal entity.
91  */
92  bool operator() ( markerID_Type const& test ) const;
93  //@}
94 
95 private:
96 
97  //! The current watermark
98  markerID_Type M_watermarkFlag;
99 
100 }; // Class InternalEntitySelector
101 
102 namespace Utility
103 {
104 
105 //! A helper functor
106 /*!
107  * This is a helper functor not of general use.
108  */
109 class MarkerMapTraits
110 {
111 public:
112  //! The type of a range of marker ID values
113  typedef std::pair<markerID_Type, markerID_Type> rangeID_Type;
114 
115  //! Comparison operator for ranges
116  bool operator() ( rangeID_Type const& a, rangeID_Type const& b ) const
117  {
118  return a.first < b.first;
119  }
120 };
121 
122 }// end namespace Utility
123 
124 /** @defgroup MeshEntityFlagsChangers
125  * Useful functors to change marker IDs according to certain conditions
126  *
127  * @{
128  */
129 //!Set the flags of a mesh entity according to predefined ranges
130 /*!
131 * This utility allows to associate to ranges [markerID_Type a, markerID_Type b] a marker ID.
132 * Then the operator ()(MeshEntitity const & e) will change the flag according to the following rule
133 * -# if e.marker falls in a stored range (a<=e.markerID()<=b) the corresponding flag is used
134 * with the given policy (which defaults to turnOn, so the flag is activated)
135 * -# otherwise the flag is left unchanged
136 *
137 * An example of its usage
138 * @verbatim
139 * setFlagAccordingToMarkerRanges changer(Flag::turnOn); //I may use the default constructor
140 * changer.insert(std::make_pair(3,5),entityFlags::INTERNAL_INTERFACE);
141 * changer.insert(std::make_pair(8,10),entityFlags::VERTEX);
142 * mesh.pointList.changeAccordingToFunctor(changer);
143 * @endverbatim
144 *
145 * Now all points with marker in the range [3,5] are set as INTERNAL_INTERFACE and those in [8,10]
146 * to VERTEX.
147 *
148 * @note It is up to the user to give consistent, i.e. non overlapping, ranges.
149 *
150 */
151 
152 class SetFlagAccordingToMarkerRanges
153 {
154 public:
155  //! The type of a flag policy
156  typedef flag_Type (*flagPolicy_ptr) (flag_Type const&, flag_Type const&);
157  //! The type of a range
158  typedef Utility::MarkerMapTraits::rangeID_Type rangeID_Type;
159 
160  //! Constructor optionally takes a policy
161  explicit SetFlagAccordingToMarkerRanges ( const flagPolicy_ptr& flagPolicy = &Flag::turnOn ) :
162  M_flagPolicy (flagPolicy) {};
163 
164  //! Inserts a range with associated flag
165  /*!
166  * The range may be given by calling std::make_pair(markerID_Type a, markerID_Type b)
167  * It is the user responsibility making sure that b>=a and that there are not overlapping
168  * ranges. Otherwise the result is unpredictable
169  *
170  * @param key a pair of markerIDs defining a range
171  * @param flag the associated flag
172  */
173  void insert (rangeID_Type const& key, flag_Type flag);
174 
175  //! Finds the flag associated to a range.
176  /*!
177  * It is mainly meant as helper function for operator(), but it is exposed
178  * because it might be useful for debugging
179  * @param m The markerID to be searched
180  * @return a pair containing the flag and a bool. If the bool is false it means that there is no
181  * corresponding range. In that case the flag defaults to DEFAULT
182  */
183  std::pair<flag_Type, bool> findFlag ( markerID_Type const& m) const;
184 
185  //! The operator doing the job
186  /*!
187  * It is the operator that does the check on a given mesh entity
188  * @param e The entity to change (possibly) using the given policy
189  */
190  template<typename MeshEntity>
191  void operator() (MeshEntity& e) const
192  {
193  std::pair<flag_Type, bool> tmp = this->findFlag ( e.markerID() );
194  if ( tmp.second )
195  {
196  e.replaceFlag ( M_flagPolicy ( e.flag(), tmp.first ) );
197  }
198  }
199 
200 private:
201  typedef std::map<rangeID_Type, flag_Type, Utility::MarkerMapTraits> map_Type;
202  typedef map_Type::const_iterator const_iterator_Type;
203  map_Type M_map;
204  flagPolicy_ptr M_flagPolicy;
205 };
206 
207 //!Sets the flag of a mesh entity according to the value of its Marker id.
208 /*!
209 * We compare the marker ID with a watermark according to a policy which is a comparison operator
210 *
211 * @code
212  bool operator()(markerID_Type const & mId, markerID_Type const & watermark)
213 * @endcode
214 *
215 * which by default is greater<> (i.e. returns true if mId > watermark).
216 * If the comparison is true the given flag is assigned to the entity using a second policy
217 * implemented as a function:
218 *
219 * @code
220  flag_Type flagPolicy ( flag_Type const & inputFlag, flag_Type const & refFlag )
221 * @endcode
222 *
223 * passed in the constructor and defaulted to Flag::turnOn
224 *
225 * Example:
226 * I want to set the flag INTERNAL_INTERFACE to all faces with marker ID > 1000
227 *
228 * @code
229  SetFlagAccordingToWatermark
230  changer(EntityFlags::INTERNAL_INTERFACE,1000,Flag::turnOn)
231  // the last argument (turnOn) is not needed
232  mesh.faceList.changeAccordingToFunctor(changer);
233  @endcode
234 * I want to set the flag INTERNAL_INTERFACE to all faces with marker ID =12000
235 * @code
236  SetFlagAccordingToWatermark<std::equal_to<markerID_Type> >
237  changer(EntityFlags::INTERNAL_INTERFACE,12000,Flag::turnOn)
238  (the last argument is not needed)
239  mesh.faceList.changeAccordingToFunctor(changer);
240 * @endcode
241 *
242 */
243 template<typename Policy = std::greater<markerID_Type> >
244 class SetFlagAccordingToWatermark
245 {
246 public:
247  typedef flag_Type (*flagPolicy_ptr) ( flag_Type const&, flag_Type const& );
248 
249  //! The constructor
250  /*!
251  * @param flagToSet the flag we want to set if the condition is satisfied
252  * @param watermark The watermark to be used in the comparison
253  * @param flagPolicy The policy we want to use. Default is turnOn, which
254  * turns on the bit-flags defined in flagToSet. Other possibilities are turnOff or
255  * change (the names are self explanatory)
256  */
257  SetFlagAccordingToWatermark ( const flag_Type& flagToSet,
258  const markerID_Type& watermark,
259  const flagPolicy_ptr& flagPolicy = &Flag::turnOn ) :
260  M_flagToSet ( flagToSet ),
261  M_watermark ( watermark ),
262  M_policy ( Policy() ),
263  M_flagPolicy ( flagPolicy ) {}
264 
265  //! The operator doing the job
266  /*!
267  * It is the operator that does the check on a given mesh entity
268  * @param e The entity to change (possibly) using the given policy
269  */
270  template<typename MeshEntity>
271  void operator() ( MeshEntity& e ) const
272  {
273  if ( M_policy (e.markerID(), M_watermark) )
274  {
275  e.replaceFlag ( M_flagPolicy ( e.flag(), M_flagToSet ) );
276  }
277  }
278 
279 private:
280  const flag_Type M_flagToSet;
281  const markerID_Type M_watermark;
282  const Policy M_policy;
283  const flagPolicy_ptr M_flagPolicy;
284 };
285 
286 //! Sets the flag of a mesh entity according to the value of a vector of Marker IDs.
287 /*!
288  * We compare the marker IDs with all values contained in the vector using the
289  * equal_to<> operator.
290  * If the comparison is true the given flag is assigned to the entity using the
291  * policy implemented as a function
292  * @code
293  flag_Type flagPolicy ( flag_Type const & inputFlag, flag_Type const & refFlag )
294  * @endcode
295  * passed in the constructor and defaulted to Flag::turnOn
296  *
297  * Example:
298  * I want to set the flag INTERNAL_INTERFACE to all faces with marker ID = 1000, 2000 and 3000
299  * @code
300  vector<markerID_Type> fl; fl.push_back(1000); fl.push_back(2000); fl.push_back(3000)
301  SetFlagAccordingToWatermarks<face_Type> changer(INTERNAL_INTERFACE,fl,Flag::turnOn)
302  //the last argument is not needed
303  mesh.faceList.changeAccordingToFunctor(changer);
304  @code
305  *
306  */
307 class SetFlagAccordingToWatermarks
308 {
309 public:
310  typedef flag_Type (*flagPolicy_ptr) ( flag_Type const&, flag_Type const& );
311 
312  //! The constructor
313  /*!
314  * @param flagToSet the flag we want to set if the condition is satisfied
315  * @param watermarks The std::vector of watermarks to be used in the comparison
316  * @param flagPolicy The policy we want to use. Default is turnOn, which
317  * turns on the bit-flags defined in flagToSet. Other possibilities are turnOff or
318  * change (the names are self explanatory)
319  */
320  SetFlagAccordingToWatermarks ( const flag_Type& flagToSet,
321  const std::vector<markerID_Type>& watermarks,
322  const flagPolicy_ptr& flagPolicy = &Flag::turnOn );
323 
324  //! The operator doing the job
325  /*!
326  * It is the operator that does the check on a given mesh entity
327  * @param e The entity to change (possibly) using the given policy
328  */
329  template<typename MeshEntity>
330  void operator() ( MeshEntity& e ) const
331  {
332  if ( std::binary_search ( M_watermarks.begin(), M_watermarks.end(), e.markerID() ) )
333  {
334  e.replaceFlag ( M_flagPolicy ( e.flag(), M_flagToSet ) );
335  }
336  }
337 
338 private:
339  const flag_Type M_flagToSet;
340  std::vector<markerID_Type> M_watermarks;
341  const flagPolicy_ptr M_flagPolicy;
342 };
343 
344 /** @}*/
345 /** @defgroup markerIDchangers
346  * Utility to change the marker ids according to some policy
347  * @{
348  */
349 //! Set markers according to a map.
350 /**
351  * This utility is used in some LifeV solvers to change the marker IDs of certain entities on the
352  * fly
353  *
354  * @param locDof Contains a map of integers whose second entry contains the entity id to be
355  * changed
356  * @param newMarker the new marker id
357  *
358  * @note it was originally a method of Regionmesh3D named edgeMarkers. It has been generalised
359  * and taken away from RegionMesh
360  *
361  * @todo Take away from here and put it in another header file (MeshUtility.hpp for instance)
362  */
363 template <typename MeshEntityList>
364 void
365 ChangeMarkersAccordingToMap ( MeshEntityList& entityList,
366  std::map<UInt, UInt> const& locDof,
367  UInt const newMarker )
368 {
369  typedef std::map<UInt, UInt>::const_iterator it_type;
370  for ( it_type it = locDof.begin(); it != locDof.end(); ++it )
371  {
372  entityList[ it->second ].setMarkerID ( newMarker );
373  }
374 }
375 /** @}*/
376 
377 } // Namespace LifeV
378 
379 #endif /* SELECTMARKER_H */
uint32_type flag_Type
bit-flag with up to 32 different flags
Definition: LifeV.hpp:197
flag_Type turnOn(flag_Type const &inputFlag, flag_Type const &refFlag)
turns on the refFlag active bits in inputFlag
Definition: LifeV.hpp:228
ID markerID_Type
markerID_Type is the type used to store the geometric entity marker IDs
Definition: Marker.hpp:81
flag related free functions and functors
Definition: LifeV.hpp:203
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191