omega_h
Reliable mesh adaptation
Omega_h_mesh.hpp
1 #ifndef OMEGA_H_MESH_HPP
2 #define OMEGA_H_MESH_HPP
3 
4 #include <Omega_h_adj.hpp>
5 #include <Omega_h_comm.hpp>
6 #include <Omega_h_dist.hpp>
7 #include <Omega_h_library.hpp>
8 #include <Omega_h_tag.hpp>
9 #include <array>
10 #include <map>
11 #include <set>
12 #include <string>
13 #include <vector>
14 
15 namespace Omega_h {
16 
17 namespace inertia {
18 struct Rib;
19 }
20 
21 struct ClassPair {
22  inline ClassPair() = default;
23  inline ClassPair(Int t_dim, LO t_id) : dim(t_dim), id(t_id) {}
24  Int dim;
25  LO id;
26  OMEGA_H_INLINE bool operator<(ClassPair const& other) const {
27  if (dim != other.dim) return dim < other.dim;
28  if (id != other.id) return id < other.id;
29  return false;
30  }
31 };
32 
33 using ClassSets = std::map<std::string, std::vector<ClassPair>>;
34 
35 class Mesh {
36  public:
37  Mesh();
38  Mesh(Library* library);
39  void set_library(Library* library);
40  void set_comm(CommPtr const& comm);
41  void set_family(Omega_h_Family family);
42  void set_matched(I8 is_matched);
43  virtual void set_dim(Int dim_in);
44  void set_verts(LO nverts_in);
45  void set_ents(Int ent_dim, Adj down);
46  void set_parents(Int ent_dim, Parents parents);
47  Library* library() const;
48  CommPtr comm() const;
49  Omega_h_Parting parting() const;
50  virtual inline Int dim() const {
51  OMEGA_H_CHECK(0 <= dim_ && dim_ <= 3);
52  return dim_;
53  }
54  inline Omega_h_Family family() const { return family_; }
55  inline I8 is_matched() const { return matched_; }
56  LO nents(Int ent_dim) const;
57  LO nelems() const;
58  LO nregions() const;
59  LO nfaces() const;
60  LO nedges() const;
61  LO nverts() const;
62  GO nglobal_ents(Int dim);
63  template <typename T>
64  void add_tag(Int dim, std::string const& name, Int ncomps);
65  template <typename T>
66  void add_tag(Int dim, std::string const& name, Int ncomps, ArrayType array_type);
67  template <typename T>
68  void add_tag(Int dim, std::string const& name, Int ncomps, Read<T> array,
69  bool internal = false, ArrayType array_type = ArrayType::VectorND);
70  template <typename T>
71  void set_tag(
72  Int dim, std::string const& name, Read<T> array, bool internal = false, ArrayType array_type = ArrayType::VectorND);
73  TagBase const* get_tagbase(Int dim, std::string const& name) const;
74  template <typename T>
75  Tag<T> const* get_tag(Int dim, std::string const& name) const;
76  template <typename T>
77  Read<T> get_array(Int dim, std::string const& name) const;
78  void remove_tag(Int dim, std::string const& name);
79  bool has_tag(Int dim, std::string const& name) const;
80  [[nodiscard]] Int ntags(Int dim) const;
81  [[nodiscard]] Int nrctags(Int dim) const;
82  TagBase const* get_tag(Int dim, Int i) const;
83  bool has_ents(Int dim) const;
84  bool has_adj(Int from, Int to) const;
85  Adj get_adj(Int from, Int to) const;
86  Adj ask_down(Int from, Int to);
87  LOs ask_verts_of(Int dim);
88  LOs ask_elem_verts();
89  Adj ask_up(Int from, Int to);
90  Graph ask_star(Int dim);
91  Graph ask_dual();
92 
100  Adj ask_revClass(Int edim, LOs class_ids);
101 
106  Adj ask_revClass(Int edim);
107 
119  Adj ask_revClass_downAdj(Int from, Int to);
120 
125  bool has_revClass(Int edim) const;
126 
132  template <typename T>
133  void add_rcField(
134  LOs class_ids, Int ent_dim, std::string const& name, Int ncomps);
135 
139  template <typename T>
140  void set_rcField_array(Int ent_dim, std::string const& name, Read<T> array);
141 
145  void remove_rcField(Int ent_dim, std::string const& name);
146 
147  [[nodiscard]] Adj get_revClass(Int edim) const;
148 
154  template <typename T>
155  void add_rcField(Int ent_dim, std::string const& name, Int ncomps);
156 
157  template <typename T>
158  [[nodiscard]] Read<T> get_rcField_array(
159  Int ent_dim, std::string const& name) const;
160  void reduce_rcField(Int ent_dim, std::string const& name, Omega_h_Op op);
161  template <typename T>
162  void add_rcField(
163  Int ent_dim, std::string const& name, Int ncomps, Read<T> array);
164  void sync_rcField(Int ent_dim, std::string const& name);
165  [[nodiscard]] bool has_rcField(Int ent_dim, std::string const& name) const;
166 
167  template <typename T>
168  void set_rc_from_mesh_array(Int ent_dim, Int ncomps, LOs class_ids,
169  std::string const& name, Read<T> array);
170  friend class ScopedChangeRCFieldsToMesh;
171 
172  #if defined(OMEGA_H_USE_KOKKOS)
185  [[nodiscard]] Graph get_vtx_patches(Int minPatchSize, Int tgtDim = -1);
186  #endif
187 
188 
189  private:
190  bool change_all_rcFieldsToMesh();
191  bool change_all_rcFieldsTorc();
192  template <typename T>
193  void change_tagTorc(Int ent_dim, Int ncomps, std::string const& name,
194  LOs class_ids, bool remove = true);
195  template <typename T>
196  void change_tagToMesh(Int ent_dim, Int ncomps, std::string const& name,
197  LOs class_ids, bool remove = true);
198 // Don't use these functions... they need to be public for OOMEGA_H_LAMBDA
199  public:
200  template <typename T>
201  [[nodiscard]] Read<T> get_rc_mesh_array(
202  Int ent_dim, Int ncomps, std::string const& name, LOs class_ids);
203 
204  template <typename T>
205  [[nodiscard]] Read<T> get_rc_array_from_mesh_array(Int ent_dim, Int ncomps,
206  std::string const& name, LOs class_ids, Read<T> mesh_array);
207 
208  template <typename T>
209  [[nodiscard]] std::unique_ptr<Tag<T>> get_rc_mesh_tag_from_rc_tag(
210  Int dim, Tag<T> const*);
211 
212  template <typename T>
213  [[nodiscard]] Read<T> get_rc_array(Int dim, std::string const& name) const;
214 
215  template <typename T>
216  [[nodiscard]] Read<T> get_rc_mesh_array_from_rc_array(
217  Int ent_dim, Int ncomps, LOs class_ids, Read<T> rc_field);
218 
219  public:
220  typedef std::shared_ptr<const TagBase> TagPtr;
221  typedef std::shared_ptr<const Adj> AdjPtr;
222  typedef std::shared_ptr<const Dist> DistPtr;
223  typedef std::shared_ptr<const inertia::Rib> RibPtr;
224  typedef std::shared_ptr<const Parents> ParentPtr;
225  typedef std::shared_ptr<const Children> ChildrenPtr;
226 
227  private:
228  typedef std::vector<TagPtr> TagVector;
229  typedef TagVector::iterator TagIter;
230  typedef TagVector::const_iterator TagCIter;
231  struct TagIterResult {
232  bool had_tag;
233  Mesh::TagIter it;
234  };
235  struct TagCIterResult {
236  bool had_tag;
237  Mesh::TagCIter it;
238  };
239  TagIter tag_iter(Int dim, std::string const& name);
240  TagCIter tag_iter(Int dim, std::string const& name) const;
241  TagIterResult rc_tag_iter(Int dim, std::string const& name);
242  TagCIterResult rc_tag_iter(Int dim, std::string const& name) const;
243  void check_dim(Int dim) const;
244  void check_dim2(Int dim) const;
245  void add_adj(Int from, Int to, Adj adj);
246  Adj derive_adj(Int from, Int to);
247  Adj ask_adj(Int from, Int to);
248  void react_to_set_tag(Int dim, std::string const& name);
249  Omega_h_Family family_;
250  I8 matched_ = -1;
251  CommPtr comm_;
252  Int parting_;
253  Int nghost_layers_;
254  LO nents_[DIMS];
255  TagVector tags_[DIMS];
256  // rc field tags stored in "rc" format
257  TagVector rc_field_tags_[DIMS];
258  AdjPtr adjs_[DIMS][DIMS];
259  Remotes owners_[DIMS];
260  DistPtr dists_[DIMS];
261  RibPtr rib_hints_;
262  ParentPtr parents_[DIMS];
263  ChildrenPtr children_[DIMS][DIMS];
264  Library* library_;
265  Remotes match_owners_[DIMS];
266  LOs model_ents_[DIMS];
267  LOs model_matches_[DIMS - 1];
268 
269  AdjPtr revClass_[DIMS];
270 
271  void add_rcField(Int ent_dim, std::string const& name, TagPtr tag);
272 
273 protected:
274  Int dim_;
275 
276  public:
277  void add_coords(Reals array);
278  Reals coords() const;
279  void set_coords(Reals const& array);
280  Read<GO> globals(Int dim) const;
281  Reals ask_lengths();
282  Reals ask_qualities();
284  Reals ask_sizes();
285  Bytes ask_levels(Int dim);
286  Bytes ask_leaves(Int dim);
287  Parents ask_parents(Int child_dim);
288  Children ask_children(Int parent_dim, Int child_dim);
289  bool has_any_parents() const;
290  void set_owners(Int dim, Remotes owners);
291  Remotes ask_owners(Int dim);
292  Read<I8> owned(Int dim);
293  Dist ask_dist(Int dim);
294  Int nghost_layers() const;
295  void set_parting(Omega_h_Parting parting_in, Int nlayers, bool verbose);
296  void set_parting(Omega_h_Parting parting_in, bool verbose = false);
297  void balance(bool predictive = false);
298  void balance(Reals weights);
307  void migrate(Remotes& owners);
308  Graph ask_graph(Int from, Int to);
309  template <typename T>
310  Read<T> sync_array(Int ent_dim, Read<T> a, Int width);
311  template <typename T>
312  Future<T> isync_array(Int ent_dim, Read<T> a, Int width);
313  template <typename T>
314  Read<T> sync_subset_array(
315  Int ent_dim, Read<T> a_data, LOs a2e, T default_val, Int width);
316  template <typename T>
317  Read<T> reduce_array(Int ent_dim, Read<T> a, Int width, Omega_h_Op op);
318  template <typename T>
319  Read<T> owned_array(Int ent_dim, Read<T> a, Int width);
320  template <typename T>
321  Read<T> owned_subset_array(
322  Int ent_dim, Read<T> a_data, LOs a2e, T default_val, Int width);
323  void sync_tag(Int dim, std::string const& name);
324  void reduce_tag(Int dim, std::string const& name, Omega_h_Op op);
325  bool operator==(Mesh& other);
326  Real min_quality();
327  Real max_length();
328  bool could_be_shared(Int ent_dim) const;
329  bool owners_have_all_upward(Int ent_dim) const;
330  bool have_all_upward() const;
331  Mesh copy_meta() const;
332  RibPtr rib_hints() const;
333  void set_rib_hints(RibPtr hints);
334  Real imbalance(Int ent_dim = -1) const;
335  Adj derive_revClass(Int edim, I8 should_sort = -1);
336 
337  void set_model_ents(Int ent_dim, LOs Ids);
338  void set_model_matches(Int ent_dim, LOs matches);
339  LOs get_model_ents(Int ent_dim);
340  LOs get_model_matches(Int ent_dim);
341  void set_match_owners(Int dim, Remotes owners);
342  Remotes ask_match_owners(Int dim);
343  c_Remotes matches_[DIMS];
344  void set_matches(Int dim, c_Remotes matches);
345  c_Remotes get_matches(Int dim);
346  void swap_root_owner(Int dim);
347  void sync_tag_matched(Int dim, std::string const& name);
348  template <typename T>
349  Read<T> sync_array_matched(Int ent_dim, Read<T> a, Int width);
350  Real ghosted_ratio(Int ent_dim);
351  LO nents_owned(Int ent_dim);
352  std::string string(int verbose = 0);
353 
354  virtual ~Mesh() = default;
355 
363  ClassSets class_sets;
364  [[nodiscard]] const TagVector& get_rc_tags(Int dim) const {
365  return rc_field_tags_[dim];
366  }
367  [[nodiscard]] std::unique_ptr<TagBase> get_rc_mesh_tag_from_rc_tag(
368  Int dim, TagBase const*);
369 };
370 
372  public:
373  // [[nodiscard]] // should be nodiscard when c++17 turned on
374  explicit ScopedChangeRCFieldsToMesh(Mesh& mesh) : mesh_(mesh) {
375  changed_here_ = mesh_.change_all_rcFieldsToMesh();
376  }
378  if (changed_here_) {
379  mesh_.change_all_rcFieldsTorc();
380  }
381  }
382  [[nodiscard]] bool did_conversion() const noexcept { return changed_here_; }
383 
384  private:
385  Mesh& mesh_;
386  bool changed_here_;
387 };
388 
389 bool can_print(Mesh* mesh);
390 
391 Real repro_sum_owned(Mesh* mesh, Int dim, Reals a);
392 
393 Reals average_field(Mesh* mesh, Int dim, LOs a2e, Int ncomps, Reals v2x);
394 Reals average_field(Mesh* mesh, Int dim, Int ncomps, Reals v2x);
395 
396 using TagSet = std::array<std::set<std::string>, DIMS>;
397 
398 void get_all_dim_tags(Mesh* mesh, Int dim, TagSet* tags);
399 TagSet get_all_mesh_tags(Mesh* mesh);
400 void ask_for_mesh_tags(Mesh* mesh, TagSet const& tags);
401 
402 void reorder_by_hilbert(Mesh* mesh);
403 void reorder_by_globals(Mesh* mesh);
404 
405 LOs ents_on_closure(
406  Mesh* mesh, std::set<std::string> const& class_names, Int ent_dim);
407 
408 LOs nodes_on_closure(
409  Mesh* mesh, std::set<std::string> const& class_names, Graph nodes2ents[4]);
410 
411 bool is_rc_tag(std::string const& name);
412 
413 void assign(Mesh& a, Mesh const& b);
414 
415 #define OMEGA_H_EXPL_INST_DECL(T) \
416  extern template Tag<T> const* Mesh::get_tag<T>( \
417  Int dim, std::string const& name) const; \
418  extern template Read<T> Mesh::get_array<T>(Int dim, std::string const& name) \
419  const; \
420  extern template void Mesh::add_tag<T>( \
421  Int dim, std::string const& name, Int ncomps); \
422  extern template void Mesh::add_tag<T>( \
423  Int dim, std::string const& name, Int ncomps, ArrayType array_type); \
424  extern template void Mesh::add_tag<T>(Int dim, std::string const& name, \
425  Int ncomps, Read<T> array, bool internal, ArrayType array_type); \
426  extern template void Mesh::set_tag(Int dim, std::string const& name, \
427  Read<T> array, bool internal, ArrayType array_type); \
428  extern template Read<T> Mesh::sync_array(Int ent_dim, Read<T> a, Int width); \
429  extern template Future<T> Mesh::isync_array( \
430  Int ent_dim, Read<T> a, Int width); \
431  extern template Read<T> Mesh::owned_array( \
432  Int ent_dim, Read<T> a, Int width); \
433  extern template Read<T> Mesh::sync_subset_array( \
434  Int ent_dim, Read<T> a_data, LOs a2e, T default_val, Int width); \
435  extern template Read<T> Mesh::owned_subset_array( \
436  Int ent_dim, Read<T> a_data, LOs a2e, T default_val, Int width); \
437  extern template Read<T> Mesh::reduce_array( \
438  Int ent_dim, Read<T> a, Int width, Omega_h_Op op); \
439  extern template void Mesh::change_tagTorc<T>( \
440  Int ent_dim, Int ncomps, std::string const& name, LOs class_ids, bool); \
441  extern template void Mesh::change_tagToMesh<T>( \
442  Int ent_dim, Int ncomps, std::string const& name, LOs class_ids, bool); \
443  extern template Read<T> Mesh::get_rc_mesh_array( \
444  Int ent_dim, Int ncomps, std::string const& name, LOs class_ids); \
445  extern template Read<T> Mesh::get_rc_array_from_mesh_array(Int ent_dim, \
446  Int ncomps, std::string const& name, LOs class_ids, Read<T> mesh_array); \
447  extern template Read<T> Mesh::get_rcField_array<T>( \
448  Int dim, std::string const& name) const; \
449  extern template void Mesh::add_rcField<T>( \
450  Int dim, std::string const& name, Int ncomps); \
451  extern template void Mesh::add_rcField<T>( \
452  LOs class_ids, Int dim, std::string const& name, Int ncomps); \
453  extern template void Mesh::add_rcField<T>( \
454  Int dim, std::string const& name, Int ncomps, Read<T> array); \
455  extern template void Mesh::set_rcField_array( \
456  Int dim, std::string const& name, Read<T> array); \
457  extern template void Mesh::set_rc_from_mesh_array(Int ent_dim, Int ncomps, \
458  LOs class_ids, std::string const& name, Read<T> array); \
459  extern template std::unique_ptr<Tag<T>> Mesh::get_rc_mesh_tag_from_rc_tag( \
460  Int, Tag<T> const*); \
461  extern template Read<T> Mesh::get_rc_array(Int dim, std::string const& name) \
462  const; \
463  extern template Read<T> Mesh::get_rc_mesh_array_from_rc_array( \
464  Int ent_dim, Int ncomps, LOs class_ids, Read<T> rc_field);
465 OMEGA_H_EXPL_INST_DECL(I8)
466 OMEGA_H_EXPL_INST_DECL(I32)
467 OMEGA_H_EXPL_INST_DECL(I64)
468 OMEGA_H_EXPL_INST_DECL(Real)
469 #undef OMEGA_H_EXPL_INST_DECL
470 
471 } // namespace Omega_h
472 
473 #endif
an MPI distributor object which encapsulates the idea of a communication pattern between lots of smal...
Definition: Omega_h_dist.hpp:119
Abstraction for asynchronous communication.
Definition: Omega_h_future.hpp:19
Definition: Omega_h_library.hpp:10
Definition: Omega_h_mesh.hpp:35
void add_rcField(LOs class_ids, Int ent_dim, std::string const &name, Int ncomps)
Definition: Omega_h_rcFields.cpp:356
Adj ask_revClass_downAdj(Int from, Int to)
Definition: Omega_h_rcFields.cpp:496
Reals ask_sizes()
Definition: Omega_h_mesh.cpp:445
bool has_revClass(Int edim) const
Definition: Omega_h_rcFields.cpp:62
void migrate(Remotes &owners)
Definition: Omega_h_mesh.cpp:652
ClassSets class_sets
Definition: Omega_h_mesh.hpp:363
void set_rcField_array(Int ent_dim, std::string const &name, Read< T > array)
Definition: Omega_h_rcFields.cpp:397
void remove_rcField(Int ent_dim, std::string const &name)
Definition: Omega_h_rcFields.cpp:388
Adj ask_revClass(Int edim, LOs class_ids)
Definition: Omega_h_rcFields.cpp:154
Definition: Omega_h_array.hpp:89
Definition: Omega_h_mesh.hpp:371
Definition: Omega_h_tag.hpp:31
Definition: Omega_h_tag.hpp:53
Definition: amr_mpi_test.cpp:6
Definition: Omega_h_adj.hpp:11
Definition: Omega_h_adj.hpp:30
Definition: Omega_h_mesh.hpp:21
directed graph (as defined by graph theory) in compressed row format
Definition: Omega_h_graph.hpp:22
Definition: Omega_h_adj.hpp:22
Definition: Omega_h_remotes.hpp:9
Definition: Omega_h_remotes.hpp:23