GetFEM  5.5
getfem_mesh_region.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2005-2026 Yves Renard, Julien Pommier
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file getfem_mesh_region.h
32 @author Yves Renard, Julien Pommier
33 @date 2005.
34 @brief region objects (set of convexes and/or convex faces)
35 */
36 
37 #pragma once
38 
39 #include <atomic>
40 #include <bitset>
41 #include <iostream>
42 #include <map>
43 
44 #include "dal_bit_vector.h"
45 #include "bgeot_convex_structure.h"
46 #include "getfem_config.h"
47 
48 namespace getfem {
49  class mesh;
50 
51  /** structure used to hold a set of convexes and/or convex faces.
52  @see mesh::region
53  */
54  class APIDECL mesh_region {
55  public:
56  using face_bitset = std::bitset<MAX_FACES_PER_CV+1>;
57  using map_t = std::map<size_type, face_bitset>;
58 
59  private:
60 
61  using const_iterator = map_t::const_iterator;
62 
63  struct impl {
64  mutable map_t m;
65  mutable omp_distribute<dal::bit_vector> index_;
66  mutable dal::bit_vector serial_index_;
67  };
68  std::shared_ptr<impl> p; /* the real region data */
69 
70  size_type id_; /* used temporarily when the
71  mesh_region(size_type)
72  constructor is used */
73 
74  size_type type_; //optional type of the region
75 
76  std::atomic_bool partitioning_allowed; /* specifies that in multithreaded code only a
77  partition of the region is visible in index()
78  and size() methods, as well as during iteration
79  with mr_visitor */
80 
81  mesh *parent_mesh; /* used for mesh_region "extracted" from
82  a mesh (to provide feedback) */
83 
84 
85  //caching iterators for partitions
86  mutable omp_distribute<const_iterator> itbegin;
87  mutable omp_distribute<const_iterator> itend;
88 
89  //flags for all the caches
90  mutable omp_distribute<bool> index_updated;
91  mutable omp_distribute<bool> partitions_updated;
92  mutable bool serial_index_updated;
93 
94  void mark_region_changed() const;
95 
96  void update_index() const;
97 
98  void update_partition_iterators() const;
99 
100  impl &wp() { return *p.get(); }
101  const impl &rp() const { return *p.get(); }
102  void clean();
103  /** tells the owner mesh that the region is valid */
104  void touch_parent_mesh();
105 
106  /**when running while multithreaded, gives the iterator
107  for the beginning of the region partition for the current thread*/
108  const_iterator partition_begin() const;
109 
110  /**when running while multithreaded, gives the iterator
111  for the end of the region partition for the current thread*/
112  const_iterator partition_end() const;
113 
114  /**begin iterator of the region depending if its partitioned or not*/
115  const_iterator begin() const;
116 
117  /**end iterator of the region depending if its partitioned or not*/
118  const_iterator end() const;
119 
120  /**number of region entries before partitioning*/
121  size_type unpartitioned_size() const;
122 
123  public:
124  mesh_region(const mesh_region &other);
125  mesh_region();
126  /** a mesh_region can be built from a integer parameter
127  (a region number in a mesh),
128  but it won't be usable until 'from_mesh(m)' has been called
129  Note that these regions are read-only, this constructor is
130  mostly used for backward-compatibility.
131  */
132  mesh_region(size_type id__);
133 
134  /** internal constructor. You should used m.region(id) instead. */
135  mesh_region(mesh& m, size_type id__, size_type type = size_type(-1));
136  /** build a mesh_region from a convex list stored in a bit_vector. */
137  mesh_region(const dal::bit_vector &bv);
138 
139  /** provide a default value for the mesh_region parameters of assembly
140  procedures etc. */
142  return mesh_region(size_type(-1));
143  }
144  /** return the intersection of two mesh regions */
145  static mesh_region intersection(const mesh_region& a,
146  const mesh_region& b);
147  /** return the union of two mesh_regions */
148  static mesh_region merge(const mesh_region &a,
149  const mesh_region &b);
150  /** subtract the second region from the first one */
151  static mesh_region subtract(const mesh_region &a,
152  const mesh_region &b);
153  /** Test if the region is a boundary of a list of faces of elements of
154  region `rg`. Return 0 if not, -1 if only partially, 1 if the region
155  contains only some faces which are all faces of elements of `rg`. */
156  int region_is_faces_of(const getfem::mesh& m1,
157  const mesh_region &rg2,
158  const getfem::mesh& m2) const;
159 
160  size_type id() const { return id_; }
161 
162  size_type get_type() const { return type_; }
163 
164  void set_type(size_type type) { type_ = type; }
165 
166  /** In multithreaded part of the program makes only a partition of the
167  region visible in the index() and size() operations, as well as during
168  iterations with mr_visitor. This is a default behaviour*/
169  void allow_partitioning();
170 
171  /** Return the bounding box [Pmin - Pmax] of the mesh_region. */
172  void bounding_box(base_node& Pmin, base_node& Pmax) const;
173 
174  /** Disregard partitioning, which means being able to see the whole region
175  in multithreaded code. Can be used, for instance, for contact problems
176  where master region is partitioned, while the slave region is not*/
177  void prohibit_partitioning();
178 
179  bool is_partitioning_allowed() const;
180 
181  /** Extract the next region number
182  that does not yet exists in the mesh*/
183  static size_type free_region_id(const getfem::mesh& m);
184 
185  /** For regions which have been built with just a number 'id',
186  from_mesh(m) sets the current region to 'm.region(id)'.
187  (works only once)
188  */
189  const mesh_region& from_mesh(const mesh &m) const;
190 
191  mesh_region& operator=(const mesh_region &mr);
192 
193  bool compare(const mesh &m1, const mesh_region &mr, const mesh &m2) const;
194 
195  face_bitset operator[](size_t cv) const;
196 
197  /** Index of the region convexes, or the convexes from the partition on the
198  current thread. */
199  const dal::bit_vector& index() const;
200  void add(const dal::bit_vector &bv);
201  void add(size_type cv, short_type f = short_type(-1));
202  void sup(size_type cv, short_type f = short_type(-1));
203  void sup_all(size_type cv);
204  void clear();
205  void swap_convex(size_type cv1, size_type cv2);
206  bool is_in(size_type cv, short_type f = short_type(-1)) const;
207  bool is_in(size_type cv, short_type f, const mesh &m) const;
208 
209  /** Region size, or the size of the region partition on the current
210  thread if the region is partitioned*/
211  size_type size() const;
212 
213  /** Number of convexes in the region, or on the partition on the current
214  thread*/
215  size_type nb_convex() const { return index().card();}
216  bool is_empty() const;
217  /** Return true if the region do contain only convex faces */
218  bool is_only_faces() const;
219  bool is_boundary() const { return is_only_faces(); }
220  /** Return true if the region do not contain any convex face */
221  bool is_only_convexes() const;
222  face_bitset faces_of_convex(size_type cv) const;
223  face_bitset and_mask() const;
224  face_bitset or_mask() const;
225  void error_if_not_faces() const;
226  void error_if_not_convexes() const;
227  void error_if_not_homogeneous() const;
228  const mesh *get_parent_mesh(void) const { return parent_mesh; }
229  void set_parent_mesh(mesh *pm) { parent_mesh = pm; }
230 
231  /** "iterator" class for regions. Usage similar to bv_visitor:
232  for (mr_visitor i(region); !i.finished(); ++i) {
233  ...
234  }
235  */
236  class visitor {
237 
238  typedef mesh_region::map_t::const_iterator const_iterator;
239  bool whole_mesh;
240  dal::bit_const_iterator itb, iteb;
241  const_iterator it, ite;
242  face_bitset c;
243  size_type cv_;
244  short_type f_;
245  bool finished_;
246 #if GETFEM_PARA_LEVEL > 1
247  std::unique_ptr<mesh_region> mpi_rg;
248 #endif
249  void init(const mesh_region &s);
250  void init(const dal::bit_vector &s);
251 
252  public:
253  visitor(const mesh_region &s);
254  visitor(const mesh_region &s, const mesh &m,
255  bool intersect_with_mpi = false);
256  size_type cv() const { return cv_; }
257  size_type is_face() const { return f_ != 0; }
258  short_type f() const { return short_type(f_-1); }
259 
260  bool next();
261  bool operator++() { return next(); }
262 
263  bool finished() const { return finished_; }
264 
265  bool next_face(){
266  if (whole_mesh) return false;
267  if (c.none()) return false;
268  do { ++f_; } while (!c.test(f_));
269  c.set(f_,0);
270  return true;
271  }
272  };
273 
274  friend std::ostream & operator <<(std::ostream &os, const mesh_region &w);
275  };
276 
278 
279  /** Dummy mesh_region for default parameter of functions. */
281 
282 } /* end of namespace getfem. */
Definition of convex structures.
"iterator" class for regions.
structure used to hold a set of convexes and/or convex faces.
static mesh_region all_convexes()
provide a default value for the mesh_region parameters of assembly procedures etc.
size_type nb_convex() const
Number of convexes in the region, or on the partition on the current thread.
Describe a mesh (collection of convexes (elements) and points).
Definition: getfem_mesh.h:98
Provide a dynamic bit container.
defines and typedefs for namespace getfem
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:58
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
gmm::uint16_type short_type
used as the common short type integer in the library
Definition: bgeot_config.h:72
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
GEneric Tool for Finite Element Methods.
const mesh_region & dummy_mesh_region()
Dummy mesh_region for default parameter of functions.