Coloring.cpp 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
///
////// \file
///
///
/// Created by Sebastien Gilles <sebastien.gilles@inria.fr> on the Fri, 15 Apr 2016 23:01:45 +0200
/// Copyright (c) Inria. All rights reserved.
///
/// \ingroup GeometryGroup
/// \addtogroup GeometryGroup
/// \{
11

12
#include "Utilities/Containers/UnorderedMap.hpp"
13

14 15
#include "Geometry/Mesh/Internal/Coloring.hpp"
#include "Geometry/Mesh/GeometricMeshRegion.hpp"
16 17


18
namespace MoReFEM
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
{
    
    
    namespace Internal
    {
        
        
        namespace ColoringNS
        {
            
            
            namespace // anonymous
            {
                
                
                
                /*
36
                 * \brief Same as the namesake function in MoReFEM namespace, with different call arguments.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
                 *
                 * This form is used in ComputeConnectivity(): the subset of GeometricElements has already been computed
                 * there and it would be a waste to perform againe xactly the same operation.
                 *
                 * \param[in] geometric_elt_range A pair of iterators that indicates which geometric elements must be considered.
                 * To go through all of them, use:
                 * \code
                 * for (auto it = geometric_elt_range.first; it != geometric_elt_range.second; ++it)
                 * { ... }
                 * \endcode
                 */
                std::unordered_map<Vertex::shared_ptr, GeometricElt::vector_shared_ptr>
                ComputeGeometricElementForEachVertex(const GeometricMeshRegion::subset_range& geometric_elt_range,
                                                     const unsigned int Nvertex);
                
                
                
            } // namespace anonymous
            
            
            std::unordered_map<Vertex::shared_ptr, GeometricElt::vector_shared_ptr>
            ComputeGeometricElementForEachVertex(const GeometricMeshRegion& geometric_mesh_region,
                                                 const unsigned int dimension)
            {
                auto geometric_elt_range = geometric_mesh_region.GetSubsetGeometricEltList(dimension);
                assert(geometric_elt_range.second > geometric_elt_range.first);
                
                return ComputeGeometricElementForEachVertex(geometric_elt_range, geometric_mesh_region.Nvertex());
            }
            
            
            
            
70
            connecticity_helper_type
71 72 73
            ComputeConnectivity(const GeometricMeshRegion& geometric_mesh_region,
                                const unsigned int dimension)
            {
74
                connecticity_helper_type ret;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                ret.max_load_factor(Utilities::DefaultMaxLoadFactor());
                
                auto geometric_elt_range = geometric_mesh_region.GetSubsetGeometricEltList(dimension);
                assert(geometric_elt_range.second > geometric_elt_range.first);
                const auto& end = geometric_elt_range.second;
                const auto Ngeometric_elt = static_cast<std::size_t>(end - geometric_elt_range.first);
                ret.reserve(Ngeometric_elt);
                
                // For each vertex, determine the list of related geometric elements.
                std::unordered_map<Vertex::shared_ptr, GeometricElt::vector_shared_ptr>&& geometric_elt_list_per_vertex
                = ComputeGeometricElementForEachVertex(geometric_elt_range, geometric_mesh_region.Nvertex());
                
                
                // From this, deduce the expected returned value.
                {
                    for (auto it = geometric_elt_range.first; it != end; ++it)
                    {
                        auto geometric_elt_ptr = *it;
                        assert(!(!geometric_elt_ptr));
                        const auto& vertex_list = geometric_elt_ptr->GetVertexList();
                        
                        GeometricElt::vector_shared_ptr buf;
                        
98
                        for (const auto& vertex_ptr : vertex_list)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                        {
                            assert(!(!vertex_ptr));
                            auto it_in_vertex = geometric_elt_list_per_vertex.find(vertex_ptr);
                            assert(it_in_vertex != geometric_elt_list_per_vertex.cend());
                            
                            std::copy_if(it_in_vertex->second.cbegin(),
                                         it_in_vertex->second.cend(),
                                         std::back_inserter(buf),
                                         [&geometric_elt_ptr](GeometricElt::shared_ptr current_geo_elt_ptr)
                                         {
                                             assert(!(!current_geo_elt_ptr));
                                             return current_geo_elt_ptr != geometric_elt_ptr;
                                         });
                        }
                        
114 115 116 117
                        Utilities::EliminateDuplicate(buf,
                                                      Utilities::PointerComparison::Less<GeometricElt::shared_ptr>(),
                                                      Utilities::PointerComparison::Equal<GeometricElt::shared_ptr>());
                                                      
118 119 120 121 122 123 124 125 126 127
                        
                        ret.insert({ geometric_elt_ptr, std::move(buf) });
                    }
                }
                
                assert(ret.size() == Ngeometric_elt);
                return ret;
            }
            
            
128
            void PrintConnectivity(const connecticity_helper_type& connectivity)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
            {
                
                for (const auto& pair : connectivity)
                {
                    std::cout << pair.first->GetIndex() << '\t';
                    const auto& connected_geom_elt_list = pair.second;
                    
                    std::vector<unsigned int> buf(connected_geom_elt_list.size());
                    
                    std::transform(connected_geom_elt_list.cbegin(), connected_geom_elt_list.cend(),
                                   buf.begin(),
                                   [](const GeometricElt::shared_ptr& geometric_elt_ptr)
                                   {
                                       assert(!(!geometric_elt_ptr));
                                       return geometric_elt_ptr->GetIndex();
                                   });
                    
                    Utilities::PrintContainer(buf);
                }
            }
            
            
            
            
            namespace // anonymous
            {
                
                
                std::unordered_map<Vertex::shared_ptr, GeometricElt::vector_shared_ptr>
                ComputeGeometricElementForEachVertex(const GeometricMeshRegion::subset_range& geometric_elt_range,
                                                     const unsigned int Nvertex)
                {
                    std::unordered_map<Vertex::shared_ptr, GeometricElt::vector_shared_ptr> ret;
                    
                    ret.max_load_factor(Utilities::DefaultMaxLoadFactor());
                    ret.reserve(Nvertex);
                    const auto& end = geometric_elt_range.second;
                    assert(geometric_elt_range.first < end);
                    
                    for (auto it = geometric_elt_range.first; it != end; ++it)
                    {
                        const auto& geometric_elt_ptr = *it;
                        assert(geometric_elt_ptr != nullptr);
                        
                        const auto& vertex_list = geometric_elt_ptr->GetVertexList();
                        
                        for (const auto& vertex_ptr : vertex_list)
                        {
                            assert(!(!vertex_ptr));
                            auto it_in_vertex = ret.find(vertex_ptr);
                            
                            if (it_in_vertex == ret.cend())
                                ret.insert({vertex_ptr, { geometric_elt_ptr } });
                            else
                                it_in_vertex->second.push_back(geometric_elt_ptr);
                        }
                    }
                    
                    return ret;
                }
                
                
            } // namespace anonymous
            
            
        } // namespace ColoringNS
        
        
    } // namespace Internal
  

200
} // namespace MoReFEM
201 202 203


/// @} // addtogroup GeometryGroup