Go to Overview over all GrAL packages.
Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Gral/Partitioning/internal/coarse-grid-from-partitioning.C

Go to the documentation of this file.
00001 #ifndef NMWR_GB_COARSE_FROM_PARTITIONING_C
00002 #define NMWR_GB_COARSE_FROM_PARTITIONING_C
00003 
00004 
00005 // $LICENSE
00006 
00007 #include "Gral/Partitioning/coarse-grid-from-partitioning.h"
00008 #include "Gral/Partitioning/collect-element-partitions.h"
00009 
00010 #include "Gral/IO/stream-grid-adapter.h"
00011 
00012 #include "Gral/Base/construct-grid.h"
00013 
00014 
00015 //----------------------------------------------------------------
00016 //  
00017 //  Construct a coarse grid from a partitioning of a fine grid.
00018 //  The cells of the coarse grid correspond to the partitions of
00019 //  the fine grid. This work only for 2D grids (see below).
00020 //
00021 //  Algorithm:
00022 //  ----------
00023 //  (1) for all vertices on partition boundaries: collect all
00024 //      adjacent partitions.
00025 //  (2) for all partitions p: scan all vertices v on the  boundary
00026 //      of p, and see if it is a vertex of the coarse grid.
00027 //      - in 2D, v is a vertex of p <=> v has >= 3 adjacent partitions
00028 //        (including the outside of the underlying fine grid)
00029 //      - in 3D, we will have to adapt this test:
00030 //         First identify the facets of the partition, then the edges
00031 //         and finally the vertices.
00032 //
00033 //----------------------------------------------------------------
00034 
00035 
00036 template<class T>
00037 class vector_map : public std::vector<T>
00038 {
00039   typedef std::vector<T> base;
00040 public:
00041   vector_map() {}
00042   vector_map(const std::vector<T>& v) : base(v) {}
00043   vector_map(size_t sz)          : base(sz) {}
00044   vector_map(size_t sz, const T& t) : base(sz,t) {}
00045 
00046   typedef int argument_type;
00047   typedef T   result_type;
00048   const T& operator()(int i) const { return *(begin()+i);}
00049 };
00050 
00051 
00052 template<class CoarseGrid, class Partition, 
00053          class CoarseToFineVertex, class CoarseCellToPart>
00054 void ConstructCoarsePartitionGrid(CoarseGrid& G,      // out
00055                                   const Partition& P, // in
00056                                   CoarseToFineVertex& coarse2fine_v,   // out
00057                                   CoarseCellToPart  & coarsecell2part) // out
00058 {
00059   typedef typename Partition::PartBdVertexIterator PartBdVertexIterator;
00060   typedef typename Partition::PartBdFacetIterator  PartBdFacetIterator;
00061   typedef typename Partition::grid_type   grid_type;
00062   typedef grid_types<grid_type>           gt;
00063   typedef typename gt::Vertex             Vertex;
00064   //  typedef typename gt::BoundaryFacetIterator  BdFacetIterator;
00065 
00066   partial_grid_function<Vertex, std::vector<int> > partitions_of_vertex(P.TheGrid());
00067 
00068   // fine vertices that correspond to vertices of coarse grid
00069   std::vector<Vertex>                     coarse_vertices;
00070   // the inverse function
00071   partial_grid_function<Vertex,int>  coarse_vertex_num(P.TheGrid());
00072 
00073   // numbers of coarse vertices of partitions 
00074   // {0 ... npart-1} --> P{ 0 ... nvert-1}
00075   std::vector< std::vector<int> > local_vertices(P.NumOfPartitions());
00076 
00077   //-----  collect adjacent partitions for all vertices on part. bd. ----
00078 
00079   collect_vertex_partitions(P,partitions_of_vertex);
00080   
00081   //-------- collect local vertices of all partitions ------------------
00082 
00083   // a vertex of the partition grid is caracterized by the fact that
00084   // it is adjacent to at least d+1 partitions, where d is the (intrinsic)
00085   // dimension of the grid. Here also the complement of the grid counts 
00086   // as partition. (not true in 3D!)
00087   // Note: the collection of the vertices relies on an ordered transversal
00088   // through the boundary of a partition. This is not possible in 3D, where
00089   // more information has to be gathered!
00090 
00091   for(int pt = 0; pt < (int)P.NumOfPartitions(); ++pt) {
00092     for(PartBdVertexIterator pbv = P.FirstBdVertex(pt); ! pbv.IsDone(); ++pbv)
00093       if( partitions_of_vertex(*pbv).size() > 2) {
00094         if (! coarse_vertex_num.defined(*pbv)) {
00095            coarse_vertices.push_back(*pbv);
00096            coarse_vertex_num[*pbv] = coarse_vertices.size() -1;
00097         }
00098         local_vertices[pt].push_back(coarse_vertex_num[*pbv]);
00099       }
00100   }
00101 
00102   //---- flatten local vertex list ----------
00103 
00104   typedef std::vector< std::vector<int> >::const_iterator vvi_iter;
00105   typedef std::vector<int>::const_iterator vi_iter;
00106   std::vector<int> vertex_list;
00107   vertex_list.reserve(P.NumOfPartitions() * 5); // heuristical
00108   for(vvi_iter v = local_vertices.begin(); v != local_vertices.end(); ++v) {
00109     vertex_list.push_back((*v).size());
00110     for (vi_iter lv = (*v).begin(); lv != (*v).end(); ++lv)
00111       vertex_list.push_back(*lv);
00112   }
00113 
00114 
00115   //  copy(vertex_list.begin(),vertex_list.end(), ostream_iterator<int>(cerr," "));
00116  
00117   //--- construct coarse grid from vertex_list
00118   typedef grid_types<CoarseGrid>  cgt;
00119   typedef typename cgt::vertex_handle coarse_vertex_handle;
00120   typedef typename cgt::cell_handle   coarse_cell_handle;
00121 
00122   vector_map<coarse_vertex_handle>  CoarseVertexCorr(coarse_vertices.size());
00123   vector_map<coarse_cell_handle>    CoarseCellCorr(P.NumOfPartitions());
00124   ConstructGrid0(G,                                        // out
00125                  StreamGridMask((int)coarse_vertices.size(),    // in
00126                                 (int)P.NumOfPartitions(),
00127                                 vertex_list.begin()),
00128                  CoarseVertexCorr,
00129                  CoarseCellCorr);
00130 
00131   for(unsigned v0 = 0; v0 < coarse_vertices.size(); ++v0)
00132     coarse2fine_v[CoarseVertexCorr[v0]] = coarse_vertices[v0].handle();  
00133   
00134   for(unsigned c = 0; c < P.NumOfPartitions(); ++c)
00135     coarsecell2part[CoarseCellCorr[c]] = c;
00136 
00137 }
00138 
00139 #endif
00140 

Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 16:07:58 2002 for GrAL Partitioning Components by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000