From 9318cd4da255a38c0970e5b896376c40752c9e82 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Wed, 13 Sep 2017 18:10:19 +0200 Subject: [PATCH] Created base structure for TriaInfoCache. --- include/deal.II/grid/tria_info_cache.h | 137 +++++++++++++++++++ include/deal.II/grid/tria_info_cache_flags.h | 133 ++++++++++++++++++ source/grid/CMakeLists.txt | 2 + source/grid/tria_info_cache.cc | 56 ++++++++ source/grid/tria_info_cache.inst.in | 23 ++++ 5 files changed, 351 insertions(+) create mode 100644 include/deal.II/grid/tria_info_cache.h create mode 100644 include/deal.II/grid/tria_info_cache_flags.h create mode 100644 source/grid/tria_info_cache.cc create mode 100644 source/grid/tria_info_cache.inst.in diff --git a/include/deal.II/grid/tria_info_cache.h b/include/deal.II/grid/tria_info_cache.h new file mode 100644 index 0000000000..9a9febfe82 --- /dev/null +++ b/include/deal.II/grid/tria_info_cache.h @@ -0,0 +1,137 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + +#ifndef dealii_grid_tria_info_cache_h +#define dealii_grid_tria_info_cache_h + + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +DEAL_II_NAMESPACE_OPEN + +/** + * A class that caches computationally intensive information about a + * Triangulation. + * + * This class attaches a signal to the Triangulation passed at construction + * time, and rebuilds all the structures specified in the list of specified + * TriangulationInfoCacheFlags, whenever the Triangulation is changed. + * + * If you try to access one of the objects that has not been cached (due to a + * missing TriangulationInfoCacheFlags flag), an Assertion is thrown. + * + * Notice that this class only notices if the underlying Triangulation has + * changed due to a Triangulation::Signals::any_change() signal beeing triggered. + * + * If the triangulation changes due to a MappingQEulerian being passed to this + * class, or because you manually change some vertex locations, then some of + * the structures in this class become obsolete, and you will have to call the + * method update() manually. + * + * @author Luca Heltai, 2017. + */ +template +class TriangulationInfoCache : public Subscriptor +{ +public: + /** + * Constructor. + * + * You can specify what to cache by passing appropriate + * TriangulationInfoCacheFlags. If you provide the optional `mapping` + * argument, then this is used whenever a mapping is required. + * + * @param tria The triangulation to work on + * @param flags TriangulationInfoCacheFlags that specify what to cache + * @param mapping The mapping to use when computing cached objects + */ + TriangulationInfoCache (const Triangulation &tria, + const TriangulationInfoCacheFlags &flags = cache_nothing, + const Mapping &mapping=StaticMappingQ1::mapping); + + /** + * Loop over all TriangulationInfoCacheFlags and rebuilds the specific data + * structure. + * + * The optional parameter can be set to true to update only data that is + * dependent on the position of the vertices. This may happen, for example, + * if you have a MappingQEulerian class associated to the triangulation. In + * this case, even if the Triangulation has not changed, the vector that + * specifies the position of the vertices may actually change *independently* + * of the triangulation, requiring you to recompute vertex dependent things. + * In this case, the topological information is not changed, and does not + * need to be updated. + * + * @param topology_is_unchanged + */ + void update(bool topology_is_unchanged=false); + + + /** + * Return the cached vertex_to_cell_map as computed by GridTools::vertex_to_cell_map(). + */ + const std::vector< std::set::active_cell_iterator> > + &get_vertex_to_cell_map() const; + + /** + * Exception for uninitialized cache fields. + */ + DeclException2(ExcAccessToInvalidCacheObject, + TriangulationInfoCacheFlags &, + TriangulationInfoCacheFlags &, + << "You tried to access a cache object that has not " + << "been constructed by this class. You specified " + << arg2 << " at construction time, but you need also " + << arg1 << " to access the field you are requesting now."); +private: + /** + * A pointer to the Triangulation. + */ + SmartPointer, + TriangulationInfoCache> tria; + + /** + * Specifies what to cache. + */ + const TriangulationInfoCacheFlags flags; + + /** + * Mapping to use when computing on the Triangulation. + */ + SmartPointer, + TriangulationInfoCache> mapping; + + + /** + * Store vertex to cell map information, as generated by + * GridTools::vertex_to_cell_map() + */ + std::vector< std::set::active_cell_iterator> > vertex_to_cells; +}; + +DEAL_II_NAMESPACE_CLOSE + +#endif diff --git a/include/deal.II/grid/tria_info_cache_flags.h b/include/deal.II/grid/tria_info_cache_flags.h new file mode 100644 index 0000000000..ddc0234b6c --- /dev/null +++ b/include/deal.II/grid/tria_info_cache_flags.h @@ -0,0 +1,133 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + +#ifndef dealii_grid_tria_info_cache_flags_h +#define dealii_grid_tria_info_cache_flags_h + + +#include + +DEAL_II_NAMESPACE_OPEN + +/** + * The enum type given to the TriangulationInfoCache class to select what + * information to cache. + * + * You can select more than one flag by concatenation using the bitwise or + * operator|(TriangulationInfoCacheFlags,TriangulationInfoCacheFlags). + * + * @author Luca Heltai, 2017. + */ +enum TriangulationInfoCacheFlags +{ + /** + * Cache Nothing. + */ + cache_nothing = 0, + + /** + * Cache vertex_to_cell_map, as returned by GridTools::vertex_to_cell_map(). + */ + cache_vertex_to_cell_map = 0x0001, +}; + + +/** + * Output operator which outputs assemble flags as a set of or'd text values. + * + * @ref TriangulationInfoCacheFlags + */ +template +inline +StreamType &operator << (StreamType &s, TriangulationInfoCacheFlags u) +{ + s << " TriangulationInfoCacheFlags"; + if (u & cache_vertex_to_cell_map) s << "|vertex_to_cell_map" ; + return s; +} + + +/** + * Global operator which returns an object in which all bits are set which are + * either set in the first or the second argument. This operator exists since + * if it did not then the result of the bit-or operator | would be an + * integer which would in turn trigger a compiler warning when we tried to + * assign it to an object of type TriangulationInfoCacheFlags. + * + * @ref TriangulationInfoCacheFlags + */ +inline +TriangulationInfoCacheFlags +operator | (TriangulationInfoCacheFlags f1, TriangulationInfoCacheFlags f2) +{ + return static_cast ( + static_cast (f1) | + static_cast (f2)); +} + + + + +/** + * Global operator which sets the bits from the second argument also in the + * first one. + * + * @ref TriangulationInfoCacheFlags + */ +inline +TriangulationInfoCacheFlags & +operator |= (TriangulationInfoCacheFlags &f1, TriangulationInfoCacheFlags f2) +{ + f1 = f1 | f2; + return f1; +} + + +/** + * Global operator which returns an object in which all bits are set which are + * set in the first as well as the second argument. This operator exists since + * if it did not then the result of the bit-and operator & would be + * an integer which would in turn trigger a compiler warning when we tried to + * assign it to an object of type TriangulationInfoCacheFlags. + * + * @ref TriangulationInfoCacheFlags + */ +inline +TriangulationInfoCacheFlags +operator & (TriangulationInfoCacheFlags f1, TriangulationInfoCacheFlags f2) +{ + return static_cast ( + static_cast (f1) & + static_cast (f2)); +} + + +/** + * Global operator which clears all the bits in the first argument if they are + * not also set in the second argument. + * + * @ref TriangulationInfoCacheFlags + */ +inline +TriangulationInfoCacheFlags & +operator &= (TriangulationInfoCacheFlags &f1, TriangulationInfoCacheFlags f2) +{ + f1 = f1 & f2; + return f1; +} + +DEAL_II_NAMESPACE_CLOSE + +#endif diff --git a/source/grid/CMakeLists.txt b/source/grid/CMakeLists.txt index aef9879d8d..2e26d5f41c 100644 --- a/source/grid/CMakeLists.txt +++ b/source/grid/CMakeLists.txt @@ -28,6 +28,7 @@ SET(_unity_include_src tria_accessor.cc tria_boundary.cc tria_boundary_lib.cc + tria_info_cache.cc tria_faces.cc tria_levels.cc tria_objects.cc @@ -61,6 +62,7 @@ SET(_inst tria_accessor.inst.in tria_boundary.inst.in tria_boundary_lib.inst.in + tria_info_cache.inst.in tria.inst.in tria_objects.inst.in ) diff --git a/source/grid/tria_info_cache.cc b/source/grid/tria_info_cache.cc new file mode 100644 index 0000000000..3f4c759e62 --- /dev/null +++ b/source/grid/tria_info_cache.cc @@ -0,0 +1,56 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + +#include +#include + +DEAL_II_NAMESPACE_OPEN + +template +TriangulationInfoCache::TriangulationInfoCache(const Triangulation &tria, + const TriangulationInfoCacheFlags &flags, + const Mapping &mapping) : + tria(&tria), + flags(flags), + mapping(&mapping) +{ + tria->signals.any_change.connect([&]() + { + update(); + }); +} + +template +void TriangulationInfoCache::update(bool topology_is_unchanged) +{ + if (cache_vertex_to_cell_map & flags) + { + vertex_to_cells = GridTools::vertex_to_cell_map(*tria); + } +} + +template +const std::vector::active_cell_iterator> > & +TriangulationInfoCache::get_vertex_to_cell_map() const +{ + Assert(flags & cache_vertex_to_cell_map, + ExcAccessToInvalidCacheObject(cache_vertex_to_cell_map, flags)); + return vertex_to_cells; +} + + + + +DEAL_II_NAMESPACE_CLOSE diff --git a/source/grid/tria_info_cache.inst.in b/source/grid/tria_info_cache.inst.in new file mode 100644 index 0000000000..b55e5ef223 --- /dev/null +++ b/source/grid/tria_info_cache.inst.in @@ -0,0 +1,23 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2010 - 2016 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + + +for (deal_II_dimension : DIMENSIONS; deal_II_space_dimension : SPACE_DIMENSIONS) +{ +#if deal_II_dimension <= deal_II_space_dimension + template class TriangulationInfoCache; +#endif +} -- 2.39.5