t8  UNKNOWN
t8code is a C library to manage a forest of adaptive space-trees of general element classes in parallel.
t8_geometry_handler.hxx
Go to the documentation of this file.
1 /*
2  This file is part of t8code.
3  t8code is a C library to manage a collection (a forest) of multiple
4  connected adaptive space-trees of general element classes in parallel.
5 
6  Copyright (C) 2024 the developers
7 
8  t8code is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  t8code is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with t8code; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
27 #pragma once
28 
29 #include <t8.h>
32 #include <memory>
33 #include <string>
34 #include <unordered_map>
35 
37 {
38  public:
42  t8_geometry_handler (): active_geometry (nullptr), active_tree (-1) {};
43 
48 
56  template <typename geometry_type, typename... _args>
57  geometry_type *
58  register_geometry (_args &&...args)
59  {
60  std::unique_ptr<t8_geometry> geom_ptr = std::make_unique<geometry_type> (std::forward<_args> (args)...);
61  return add_geometry<geometry_type> (std::move (geom_ptr));
62  }
63 
69  void
71 
77  inline t8_geometry *
78  get_geometry (const std::string &name)
79  {
80  const size_t hash = std::hash<std::string> {}(name);
82  }
83 
89  inline t8_geometry *
90  get_geometry (const size_t hash)
91  {
92  auto found = registered_geometries.find (hash);
93  if (found != registered_geometries.end ()) {
94  return found->second.get ();
95  }
96  return nullptr;
97  }
98 
103  inline size_t
105  {
106  return registered_geometries.size ();
107  }
108 
115  inline t8_geometry *
117  {
118  T8_ASSERT (registered_geometries.size () == 1);
119  return active_geometry;
120  }
121 
126  inline void
128  {
129  active_tree = -1;
130  }
131 
138  inline t8_geometry *
140  {
141  update_tree (cmesh, gtreeid);
142  return active_geometry;
143  }
144 
153  inline void
154  evaluate_tree_geometry (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords,
155  double *out_coords)
156  {
157  update_tree (cmesh, gtreeid);
158  active_geometry->t8_geom_evaluate (cmesh, gtreeid, ref_coords, num_coords, out_coords);
159  }
160 
169  inline void
170  evaluate_tree_geometry_jacobian (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords,
171  const size_t num_coords, double *out_coords)
172  {
173  update_tree (cmesh, gtreeid);
174  active_geometry->t8_geom_evaluate_jacobian (cmesh, gtreeid, ref_coords, num_coords, out_coords);
175  }
176 
183  inline t8_geometry_type_t
185  {
186  update_tree (cmesh, gtreeid);
187  return active_geometry->t8_geom_get_type ();
188  }
189 
196  inline bool
197  tree_negative_volume (const t8_cmesh_t cmesh, const t8_gloidx_t gtreeid)
198  {
199  update_tree (cmesh, gtreeid);
200  return active_geometry->t8_geom_tree_negative_volume ();
201  }
202 
203  private:
210  template <typename geometry_type>
211  inline geometry_type *
212  add_geometry (std::unique_ptr<t8_geometry> geom)
213  {
214  t8_debugf ("Registering geometry with name %s\n", geom->t8_geom_get_name ().c_str ());
215  const size_t hash = geom->t8_geom_get_hash ();
216  if (registered_geometries.find (hash) == registered_geometries.end ()) {
217  registered_geometries.emplace (hash, std::move (geom));
218  }
219  if (registered_geometries.size () == 1) {
220  active_geometry = registered_geometries.at (hash).get ();
221  }
222  return static_cast<geometry_type *> (registered_geometries.at (hash).get ());
223  }
224 
230  void
231  update_tree (t8_cmesh_t cmesh, t8_gloidx_t gtreeid);
232 
234  std::unordered_map<size_t, std::unique_ptr<t8_geometry>> registered_geometries;
236  t8_geometry *active_geometry;
238  t8_gloidx_t active_tree;
239 };
This structure holds the connectivity data of the coarse mesh.
Definition: t8_cmesh_types.h:88
Definition: t8_geometry_handler.hxx:37
t8_geometry * get_unique_geometry()
If a geometry handler only has one registered geometry, get a pointer to this geometry.
Definition: t8_geometry_handler.hxx:116
t8_geometry * get_geometry(const std::string &name)
Find a geometry by its name.
Definition: t8_geometry_handler.hxx:78
void evaluate_tree_geometry_jacobian(t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords, double *out_coords)
Evaluate the Jacobian of the geometry of the provided tree at the given reference coordinates.
Definition: t8_geometry_handler.hxx:170
void evaluate_tree_geometry(t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords, double *out_coords)
Evaluate the geometry of the provided tree at the given reference coordinates.
Definition: t8_geometry_handler.hxx:154
t8_geometry * get_tree_geometry(t8_cmesh_t cmesh, t8_gloidx_t gtreeid)
Get the geometry of the provided tree.
Definition: t8_geometry_handler.hxx:139
geometry_type * register_geometry(_args &&...args)
Register a geometry with the geometry handler.
Definition: t8_geometry_handler.hxx:58
t8_geometry * get_geometry(const size_t hash)
Find a geometry by its hash.
Definition: t8_geometry_handler.hxx:90
void deactivate_tree()
Deactivate the current active tree.
Definition: t8_geometry_handler.hxx:127
size_t get_num_geometries() const
Get the number of registered geometries.
Definition: t8_geometry_handler.hxx:104
~t8_geometry_handler()
Destructor.
Definition: t8_geometry_handler.hxx:47
t8_geometry_handler()
Constructor.
Definition: t8_geometry_handler.hxx:42
bool tree_negative_volume(const t8_cmesh_t cmesh, const t8_gloidx_t gtreeid)
Check if the volume of a tree is negative.
Definition: t8_geometry_handler.hxx:197
t8_geometry_type_t get_tree_geometry_type(t8_cmesh_t cmesh, t8_gloidx_t gtreeid)
Get the geometry type of the provided tree.
Definition: t8_geometry_handler.hxx:184
The base class for all geometries.
Definition: t8_geometry_base.hxx:48
virtual bool t8_geom_tree_negative_volume() const
Check if the currently active tree has a negative volume.
Definition: t8_geometry_base.hxx:138
virtual void t8_geom_evaluate(t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords, double *out_coords) const =0
Maps points in the reference space .
virtual void t8_geom_evaluate_jacobian(t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords, double *jacobian) const =0
Compute the jacobian of the t8_geom_evaluate map at a point in the reference space .
virtual t8_geometry_type_t t8_geom_get_type() const =0
Get the type of this geometry.
This is the administrative header file for t8code.
int64_t t8_gloidx_t
A type for global indexing that holds really big numbers.
Definition: t8.h:100
void t8_debugf(const char *fmt,...)
Log a message, no matter what rank, with priority SC_LP_DEBUG.
Definition: t8.c:123
#define T8_ASSERT(c)
Widely used assertion.
Definition: t8.h:71
Typedef for the t8_geometry class in order to be usable as a pointer from .c files.
enum t8_geometry_type t8_geometry_type_t
This enumeration contains all possible geometries.
Implements the base pure virtual struct t8_geometry which provides a general template for all geometr...