RefineStrategy.cpp
1 #include "RefineStrategy.hpp"
2 
4 refined_old(vector<int>()) {}
5 
6 vector<int> RefineStrategy::RemoveNearBoundary(vector<int> const& ToRefine,vector<Vector2D>
7  &directions,Tessellation
8  const& tess)
9 {
10  int nrefine=static_cast<int>(ToRefine.size());
11  int npoints=tess.GetPointNo();
12  vector<int> res;
13  vector<Vector2D> newdirections;
14  for(int i=0;i<nrefine;++i)
15  {
16  vector<int> const& edges=tess.GetCellEdges(ToRefine[static_cast<size_t>(i)]);
17  int nedge=static_cast<int>(edges.size());
18  bool good=true;
19  for(int j=0;j<nedge;++j)
20  {
21  Edge const& edge=tess.GetEdge(edges[static_cast<size_t>(j)]);
22  if(edge.neighbors.first>npoints||edge.neighbors.second>npoints)
23  {
24  good=false;
25  break;
26  }
27  }
28  if(good)
29  {
30  res.push_back(ToRefine[static_cast<size_t>(i)]);
31  if(!directions.empty())
32  newdirections.push_back(directions[static_cast<size_t>(i)]);
33  }
34  }
35  directions=newdirections;
36  return res;
37 }
38 
39 vector<int> RefineStrategy::RemoveDuplicatedLately(vector<int> const& ToRefine,
40  int Npoints,vector<Vector2D> &directions,vector<int> const& Removed,
41  Tessellation const& tess)
42 {
43 #ifdef RICH_MPI
44  if(!refined_old.empty())
45  {
46  vector<vector<int> > const& sentcells=tess.GetSentPoints();
47  vector<int> allsent;
48  for(size_t i=0;i<sentcells.size();++i)
49  if(!sentcells[i].empty())
50  allsent.insert(allsent.end(),sentcells[i].begin(),sentcells[i].end());
51  if(!allsent.empty())
52  {
53  sort(allsent.begin(),allsent.end());
54  allsent=unique(allsent);
55  for(size_t i=0;i<refined_old.size();++i)
56  {
57  const size_t toAdd=static_cast<size_t>(lower_bound(allsent.begin(),allsent.end(),refined_old[i])-
58  allsent.begin());
59  refined_old[i]-=int(toAdd);
60  }
61  }
62 
63  }
64 #endif
65  if(!Removed.empty())
66  {
67  // Update the refined_old list
68  for(size_t i=0;i<refined_old.size();++i)
69  {
70  const size_t toAdd=static_cast<size_t>(lower_bound(Removed.begin(),Removed.end(),refined_old[i])-
71  Removed.begin());
72  refined_old[i]-=int(toAdd);
73  }
74  }
75  vector<int> res;
76  vector<Vector2D> newdirections;
77  for(size_t i=0;i<ToRefine.size();++i)
78  {
79  if(!binary_search(refined_old.begin(),refined_old.end(),ToRefine[i]))
80  {
81  // Make sure not to close to any neighbor
82  const double R=tess.GetWidth(ToRefine[i]);
83  vector<int> neigh=tess.GetNeighbors(ToRefine[i]);
84  vector<int> temp(1, -1);
85  neigh=RemoveList(neigh,temp);
86  const int nn=static_cast<int>(neigh.size());
87  bool good=true;
88  for(int j=0;j<nn;++j)
89  if(tess.GetMeshPoint(neigh[static_cast<size_t>(j)]).distance(
90  tess.GetMeshPoint(ToRefine[i]))<0.4*R)
91  good=false;
92  if(good)
93  {
94  res.push_back(ToRefine[i]);
95  if(!directions.empty())
96  newdirections.push_back(directions[i]);
97  }
98  }
99  }
100  directions=newdirections;
101  vector<int> temp=res;
102  sort(temp.begin(),temp.end());
103  refined_old=temp;
104  // Add the new points that will be added
105  int N=int(res.size());
106  for(int i=0;i<N;++i)
107  refined_old.push_back(Npoints+i);
108  return res;
109 }
110 
112 
113 Vector2D FindBestSplit(Tessellation const* tess,int PointToRefine,
114  vector<Edge> const& edges,double R,Vector2D &normal)
115 {
116  Vector2D slope;
117  Vector2D point(tess->GetMeshPoint(PointToRefine));
118  int nedges=static_cast<int>(edges.size());
119  if(point.distance(tess->GetCellCM(PointToRefine))<0.1*R)
120  {
121  // Do Nearest edge
122  double dis=DistanceToEdge(point,edges[0]);
123  int min_edge=0;
124  for(int j=1;j<nedges;++j)
125  {
126  double temp=DistanceToEdge(point,edges[static_cast<size_t>(j)]);
127  if(temp<dis)
128  {
129  dis=temp;
130  min_edge=j;
131  }
132  }
133  slope=Parallel(edges[static_cast<size_t>(min_edge)]);
134  slope=slope/abs(slope);
135  Vector2D v=point-edges[static_cast<size_t>(min_edge)].vertices.first;
136  normal=v-ScalarProd(slope,v)*slope;
137  normal=normal/abs(normal);
138  }
139  else
140  {
141  normal=tess->GetCellCM(PointToRefine)-point;
142  normal=normal/abs(normal);
143  slope.Set(normal.y,-normal.x);
144  }
145  return slope;
146 }
vector< T > unique(vector< T > const &v)
Returns a vector containing only unique elements.
Definition: utils.hpp:376
double distance(Vector2D const &v1) const
Caluclates the distance from the Vector to v1.
Definition: geometry.cpp:13
Vector2D FindBestSplit(Tessellation const *tess, int PointToRefine, vector< Edge > const &edges, double R, Vector2D &normal)
Finds the best way to spit the cell by finding the line connecting the mesh point and the cell&#39;s CM...
RefineStrategy(void)
Class constructor.
Vector2D Parallel(Edge const &edge)
Calculates a unit vector parallel to an edge.
Definition: Edge.cpp:83
Abstract class for tessellation.
virtual int GetPointNo(void) const =0
Get Total number of mesh generating points.
vector< int > refined_old
The cells that were refined in the previous time step.
virtual Vector2D const & GetCellCM(int index) const =0
Returns Position of Cell&#39;s CM.
virtual ~RefineStrategy(void)
Virtual destructor.
vector< int > RemoveDuplicatedLately(vector< int > const &ToRefine, int Npoints, vector< Vector2D > &directions, vector< int > const &Removed, Tessellation const &tess)
Removes cells that were splitted in the last time step.
Interface between two cells.
Definition: Edge.hpp:13
virtual Vector2D GetMeshPoint(int index) const =0
Returns Position of mesh generating point.
double y
Component in the y direction.
Definition: geometry.hpp:48
virtual vector< vector< int > > const & GetSentPoints(void) const =0
Returns the indeces of the points that where sent to other processors.
virtual Edge const & GetEdge(int index) const =0
Returns edge (interface between cells)
double ScalarProd(Vector3D const &v1, Vector3D const &v2)
Scalar product of two vectors.
Definition: Vector3D.cpp:185
Abstract class for refinement scheme.
virtual double GetWidth(int index) const =0
Returns the effective width of a cell.
void Set(double ix, double iy)
Set vector components.
Definition: geometry.cpp:39
std::pair< int, int > neighbors
Neighboring cells.
Definition: Edge.hpp:21
double DistanceToEdge(Vector2D const &point, Edge const &edge)
Calculates the distance of a point to an edge.
Definition: Edge.cpp:31
double abs(Vector3D const &v)
Norm of a vector.
Definition: Vector3D.cpp:44
vector< int > RemoveNearBoundary(vector< int > const &ToRefine, vector< Vector2D > &directions, Tessellation const &tess)
Removed from the list cells near periodic boundaries.
virtual vector< int > const & GetCellEdges(int index) const =0
Returns the indexes of a cell&#39;s edges.
2D Mathematical vector
Definition: geometry.hpp:15
vector< T > RemoveList(vector< T > const &v, vector< T > const &list)
Returns only elements from vector v which are not in vector list, assumes list is sorted...
Definition: utils.hpp:416
virtual vector< int > GetNeighbors(int index) const =0
Returns the indeces of the neighbors.
double x
Component in the x direction.
Definition: geometry.hpp:45