00001 #ifndef NMWR_GB_COARSE_FROM_PARTITIONING_C
00002 #define NMWR_GB_COARSE_FROM_PARTITIONING_C
00003
00004
00005
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
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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,
00055 const Partition& P,
00056 CoarseToFineVertex& coarse2fine_v,
00057 CoarseCellToPart & coarsecell2part)
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
00065
00066 partial_grid_function<Vertex, std::vector<int> > partitions_of_vertex(P.TheGrid());
00067
00068
00069 std::vector<Vertex> coarse_vertices;
00070
00071 partial_grid_function<Vertex,int> coarse_vertex_num(P.TheGrid());
00072
00073
00074
00075 std::vector< std::vector<int> > local_vertices(P.NumOfPartitions());
00076
00077
00078
00079 collect_vertex_partitions(P,partitions_of_vertex);
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
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
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);
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
00116
00117
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,
00125 StreamGridMask((int)coarse_vertices.size(),
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