Program Listing for File t8_geometry_handler.hxx
↰ Return to documentation for file (src/t8_geometry/t8_geometry_handler.hxx)
/*
This file is part of t8code.
t8code is a C library to manage a collection (a forest) of multiple
connected adaptive space-trees of general element classes in parallel.
Copyright (C) 2024 the developers
t8code is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
t8code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with t8code; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef T8_GEOMETRY_HANDLER_HXX
#define T8_GEOMETRY_HANDLER_HXX
#include <t8.h>
#include <t8_geometry/t8_geometry.h>
#include <t8_geometry/t8_geometry_base.hxx>
#include <memory>
#include <string>
#include <unordered_map>
struct t8_geometry_handler
{
public:
t8_geometry_handler ()
{
t8_refcount_init (&rc);
t8_debugf ("Constructed the geometry_handler.\n");
};
~t8_geometry_handler ()
{
if (sc_refcount_is_active (&rc)) {
T8_ASSERT (t8_refcount_is_last (&rc));
t8_refcount_unref (&rc);
}
t8_debugf ("Deleted the geometry_handler.\n");
};
template <typename geometry_type, typename... _args>
geometry_type *
register_geometry (_args &&...args)
{
std::unique_ptr<t8_geometry> geom_ptr = std::make_unique<geometry_type> (std::forward<_args> (args)...);
return add_geometry<geometry_type> (std::move (geom_ptr));
}
void
register_geometry (t8_geometry *geom);
inline t8_geometry *
get_geometry (const std::string &name)
{
const t8_geometry_hash hash = t8_geometry_compute_hash (name);
return t8_geometry_handler::get_geometry (hash);
}
inline t8_geometry *
get_geometry (const t8_geometry_hash &hash)
{
if (t8_geometry_hash_is_null (hash)) {
/* The hash belongs to a non-existing geometry. */
return nullptr;
}
auto found = registered_geometries.find (hash);
if (found != registered_geometries.end ()) {
return found->second.get ();
}
t8_errorf ("Geometry with hash value %lu was not found.\n", static_cast<size_t> (hash));
return nullptr;
}
inline size_t
get_num_geometries () const
{
return registered_geometries.size ();
}
inline t8_geometry *
get_unique_geometry ()
{
T8_ASSERT (registered_geometries.size () == 1);
return active_geometry;
}
inline void
deactivate_tree ()
{
active_tree = -1;
}
inline t8_geometry *
get_tree_geometry (t8_cmesh_t cmesh, t8_gloidx_t gtreeid)
{
update_tree (cmesh, gtreeid);
return active_geometry;
}
inline void
evaluate_tree_geometry (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords,
double *out_coords)
{
update_tree (cmesh, gtreeid);
active_geometry->t8_geom_evaluate (cmesh, gtreeid, ref_coords, num_coords, out_coords);
}
inline 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)
{
update_tree (cmesh, gtreeid);
active_geometry->t8_geom_evaluate_jacobian (cmesh, gtreeid, ref_coords, num_coords, out_coords);
}
inline t8_geometry_type_t
get_tree_geometry_type (t8_cmesh_t cmesh, t8_gloidx_t gtreeid)
{
update_tree (cmesh, gtreeid);
return active_geometry->t8_geom_get_type ();
}
inline bool
tree_negative_volume (const t8_cmesh_t cmesh, const t8_gloidx_t gtreeid)
{
update_tree (cmesh, gtreeid);
return active_geometry->t8_geom_tree_negative_volume ();
}
inline bool
tree_compatible_with_geom (const t8_cmesh_t cmesh, const t8_gloidx_t gtreeid)
{
update_tree (cmesh, gtreeid);
return active_geometry->t8_geom_check_tree_compatibility ();
}
inline bool
get_tree_bounding_box (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, double bounds[6])
{
update_tree (cmesh, gtreeid);
return active_geometry->get_tree_bounding_box (cmesh, bounds);
}
inline void
ref ()
{
t8_refcount_ref (&rc);
}
inline void
unref ()
{
if (t8_refcount_unref (&rc)) {
t8_debugf ("Deleting the geometry_handler.\n");
delete this;
}
}
private:
template <typename geometry_type>
inline geometry_type *
add_geometry (std::unique_ptr<t8_geometry> geom)
{
t8_debugf ("Registering geometry with name %s\n", geom->t8_geom_get_name ().c_str ());
const t8_geometry_hash hash = geom->t8_geom_get_hash ();
if (registered_geometries.find (hash) == registered_geometries.end ()) {
registered_geometries.emplace (hash, std::move (geom));
}
else {
t8_productionf ("WARNING: Did not register the geometry %s because it is already registered.\n"
"Geometries only need to be registered once per process.\n"
"If you are registering a new geometry it probably has the same name as another one.\n",
geom->t8_geom_get_name ().c_str ());
}
if (registered_geometries.size () == 1) {
active_geometry = registered_geometries.at (hash).get ();
}
return static_cast<geometry_type *> (registered_geometries.at (hash).get ());
}
void
update_tree (t8_cmesh_t cmesh, t8_gloidx_t gtreeid);
std::unordered_map<t8_geometry_hash, std::unique_ptr<t8_geometry>> registered_geometries = {};
t8_geometry *active_geometry = nullptr;
t8_gloidx_t active_tree = -1;
t8_refcount_t rc;
};
#endif /* !T8_GEOMETRY_HANDLER_HXX */