Ice Sheet System Model  4.18
Code documentation
BamgVertex.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstring>
3 #include <cmath>
4 #include <ctime>
5 
6 #include "./bamgobjects.h"
7 #include "../shared/shared.h"
8 #include "./det.h"
9 
10 namespace bamg {
11 
12  /*Constructor/Destructor*/
14  this->PreviousNumber = 0;
15  }/*}}}*/
16 
17  /*Methods*/
18  void BamgVertex::Echo(void){/*{{{*/
19 
20  _printf_("Vertex:\n");
21  _printf_(" integer coordinates i.x: " << i.x << ", i.y: " << i.y << "\n");
22  _printf_(" Euclidean coordinates r.x: " << r.x << ", r.y: " << r.y << "\n");
23  _printf_(" ReferenceNumber = " << ReferenceNumber << "\n");
24  _printf_(" PreviousNumber = " << PreviousNumber << "\n");
25  m.Echo();
26 
27  return;
28  }
29  /*}}}*/
30  int BamgVertex::GetReferenceNumber() const { /*{{{*/
31  return ReferenceNumber;
32  }
33  /*}}}*/
34  void BamgVertex::MetricFromHessian(const double Hxx,const double Hyx, const double Hyy,const double smin,const double smax,const double s,double err,BamgOpts* bamgopts){/*{{{*/
35  /*Compute Metric from Hessian*/
36 
37  /*get options*/
38  double power=(bamgopts->power);
39  double anisomax=(bamgopts->anisomax);
40  double CutOff=bamgopts->cutoff;
41  double hmin=(bamgopts->hmin);
42  double hmax=(bamgopts->hmax);
43  double coef=bamgopts->coeff;
44  int Metrictype=(bamgopts->Metrictype);
45 
46  /*Intermediary*/
47  double ci;
48 
49  /*compute multiplicative coefficient depending on Metric Type (2/9 because it is 2d)*/
50 
51  //Absolute Error
52  /*
53  * 2 1
54  *Metric M = --- ------------ Abs(Hessian)
55  * 9 err * coeff^2
56  */
57  if (Metrictype==0){
58  ci= 2.0/9.0 * 1/(err*coef*coef);
59  }
60 
61  //Relative Error
62  /*
63  * 2 1 Abs(Hessian)
64  *Metric M = --- ------------ ----------------------
65  * 9 err * coeff^2 max( |s| , cutoff*max(|s|) )
66  *
67  */
68  else if (Metrictype==1){
69  ci= 2.0/9.0 * 1/(err*coef*coef) * 1/Max( Abs(s), CutOff*(Max(Abs(smin),Abs(smax))));
70  }
71 
72  //Rescaled absolute error
73  /*
74  * 2 1 Abs(Hessian)
75  *Metric M = --- ------------ ----------------------
76  * 9 err * coeff^2 (smax-smin)
77  */
78  else if (Metrictype==2){
79  ci= 2.0/9.0 * 1/(err*coef*coef) * 1/(smax-smin);
80  }
81  else{
82  _error_("Metrictype " << Metrictype << " not supported yet (use 0,1 or 2(default))");
83  }
84 
85  //initialize metric Miv with ci*H
86  Metric Miv(Hxx*ci,Hyx*ci,Hyy*ci);
87 
88  //Get eigen values and vectors of Miv
89  EigenMetric Vp(Miv);
90 
91  //move eigen valuse to their absolute values
92  Vp.Abs();
93 
94  //Apply a power if requested by user
95  if(power!=1.0) Vp.pow(power);
96 
97  //modify eigen values according to hmin and hmax
98  Vp.Maxh(hmax);
99  Vp.Minh(hmin);
100 
101  //Bound anisotropy by 1/(anisomax)^2
102  Vp.BoundAniso2(1/(anisomax*anisomax));
103 
104  //rebuild Metric from Vp
105  Metric MVp(Vp);
106 
107  //Apply Metric to vertex
108  m.IntersectWith(MVp);
109 
110  }
111  /*}}}*/
112  long BamgVertex::Optim(int i,int koption){ /*{{{*/
113  long ret=0;
114  if ( t && (IndexInTriangle >= 0 ) && (IndexInTriangle <3) ){
115  ret = t->Optim(IndexInTriangle,koption);
116  if(i==0){
117  t =0; // for no future optim
118  IndexInTriangle= 0;
119  }
120  }
121  return ret;
122  }
123  /*}}}*/
124  double BamgVertex::Smoothing(Mesh &Th,Mesh &BTh,Triangle* &tstart ,double omega){/*{{{*/
125  /*Original code from Frederic Hecht <hecht@ann.jussieu.fr> (BAMG v1.01, Mesh2.cpp/Smoothing)*/
126 
127  BamgVertex* s=this;
128  BamgVertex &vP = *s,vPsave=vP;
129 
130  Triangle* tbegin= t , *tria = t , *ttc;
131 
132  int k=0,kk=0,j = EdgesVertexTriangle[IndexInTriangle][0],jc;
133  R2 P(s->r),PNew(0,0);
134  do {
135  k++;
136 
137  if (!tria->Hidden(j)){
138  BamgVertex &vQ = (*tria)[VerticesOfTriangularEdge[j][0]];
139 
140  R2 Q = vQ,QP(P-Q);
141  double lQP = LengthInterpole(vP,vQ,QP);
142  PNew += Q+QP/Max(lQP,1e-20);
143  kk ++;
144  }
145  ttc = tria->TriangleAdj(j);
146  jc = NextEdge[tria->NuEdgeTriangleAdj(j)];
147  tria = ttc;
148  j = NextEdge[jc];
149  if (k>=2000){
150  _error_("k>=2000 (Maximum number of iterations reached)");
151  }
152  } while ( tbegin != tria);
153  if (kk<4) return 0;
154  PNew = PNew/(double)kk;
155  R2 Xmove((PNew-P)*omega);
156  PNew = P+Xmove;
157  double delta=Norme2_2(Xmove);
158 
159  long long deta[3];
160  I2 IBTh = BTh.R2ToI2(PNew);
161 
162  tstart=BTh.TriangleFindFromCoord(IBTh,deta,tstart);
163 
164  if (tstart->det <0){ // outside
165  double ba,bb;
166  AdjacentTriangle edge= CloseBoundaryEdge(IBTh,tstart,ba,bb) ;
167  tstart = edge;
168  vP.m= Metric(ba,*edge.EdgeVertex(0),bb,*edge.EdgeVertex(1));
169  }
170  else { // inside
171  double aa[3];
172  double s = deta[0]+deta[1]+deta[2];
173  aa[0]=deta[0]/s;
174  aa[1]=deta[1]/s;
175  aa[2]=deta[2]/s;
176  vP.m = Metric(aa,(*tstart)[0],(*tstart)[1],(*tstart)[2]);
177  }
178 
179  // recompute the det of the triangle
180  vP.r = PNew;
181 
182  vP.i = Th.R2ToI2(PNew);
183 
184  BamgVertex vPnew = vP;
185 
186  int ok=1;
187  int loop=1;
188  k=0;
189  while (ok){
190  ok =0;
191  do {
192  k++;
193  double detold = tria->det;
194  tria->det = bamg::det( (*tria)[0],(*tria)[1] ,(*tria)[2]);
195  if (loop) {
196  if (tria->det<0) ok =1;
197  else if ( (double)tria->det < detold/2 ) ok=1;
198  }
199  tria->SetUnMarkUnSwap(0);
200  tria->SetUnMarkUnSwap(1);
201  tria->SetUnMarkUnSwap(2);
202  ttc = tria->TriangleAdj(j);
203  jc = NextEdge[tria->NuEdgeTriangleAdj(j)];
204  tria = ttc;
205  j = NextEdge[jc];
206  if (k>=2000){
207  _error_("k>=2000");
208  }
209  }while ( tbegin != tria);
210 
211  if (ok && loop) vP=vPsave; // no move
212  loop=0;
213  }
214  return delta;
215  }
216  /*}}}*/
217 }
BamgOpts::cutoff
double cutoff
Definition: BamgOpts.h:14
bamg::BamgVertex
Definition: BamgVertex.h:15
bamg::BamgVertex::MetricFromHessian
void MetricFromHessian(const double Hxx, const double Hyx, const double Hyy, const double smin, const double smax, const double s, const double err, BamgOpts *bamgopts)
Definition: BamgVertex.cpp:34
BamgOpts::power
double power
Definition: BamgOpts.h:25
bamg::BamgVertex::i
I2 i
Definition: BamgVertex.h:20
bamg::BamgVertex::r
R2 r
Definition: BamgVertex.h:21
bamg::Triangle::Optim
long Optim(short a, int=0)
Definition: Triangle.cpp:115
_printf_
#define _printf_(StreamArgs)
Definition: Print.h:22
bamg::BamgVertex::GetReferenceNumber
int GetReferenceNumber() const
Definition: BamgVertex.cpp:30
bamg
Definition: AdjacentTriangle.cpp:9
bamg::BamgVertex::Smoothing
double Smoothing(Mesh &, Mesh &, Triangle *&, double=1)
Definition: BamgVertex.cpp:124
bamg::BamgVertex::PreviousNumber
long PreviousNumber
Definition: BamgVertex.h:24
bamg::EigenMetric::pow
void pow(double p)
Definition: EigenMetric.cpp:141
BamgOpts::coeff
double coeff
Definition: BamgOpts.h:15
bamg::Triangle
Definition: Triangle.h:13
bamg::EigenMetric::Minh
void Minh(double h)
Definition: EigenMetric.cpp:134
bamg::Max
T Max(const T &a, const T &b)
Definition: extrema.h:7
bamg::EigenMetric::Maxh
void Maxh(double h)
Definition: EigenMetric.cpp:137
bamg::AdjacentTriangle
Definition: AdjacentTriangle.h:12
bamg::BamgVertex::BamgVertex
BamgVertex()
Definition: BamgVertex.cpp:13
det.h
bamg::det
long long det(const I2 &a, const I2 &b, const I2 &c)
Definition: det.h:8
BamgOpts::hmax
double hmax
Definition: BamgOpts.h:35
bamg::EigenMetric::Abs
void Abs()
Definition: EigenMetric.cpp:98
BamgOpts
Definition: BamgOpts.h:8
bamg::Metric
Definition: Metric.h:17
bamg::EigenMetric::BoundAniso2
void BoundAniso2(const double coef)
Definition: Metric.h:101
bamg::LengthInterpole
double LengthInterpole(const Metric &Ma, const Metric &Mb, R2 AB)
Definition: Metric.cpp:158
bamg::BamgVertex::Echo
void Echo()
Definition: BamgVertex.cpp:18
bamg::BamgVertex::ReferenceNumber
long ReferenceNumber
Definition: BamgVertex.h:23
bamg::Metric::Echo
void Echo()
Definition: Metric.cpp:91
bamg::Mesh
Definition: Mesh.h:21
bamg::EdgesVertexTriangle
static const short EdgesVertexTriangle[3][2]
Definition: macros.h:14
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
BamgOpts::hmin
double hmin
Definition: BamgOpts.h:34
bamg::Triangle::det
long long det
Definition: Triangle.h:23
bamg::CloseBoundaryEdge
AdjacentTriangle CloseBoundaryEdge(I2 A, Triangle *t, double &a, double &b)
Definition: Mesh.cpp:4927
bamg::VerticesOfTriangularEdge
static const short VerticesOfTriangularEdge[3][2]
Definition: macros.h:13
bamg::Mesh::R2ToI2
I2 R2ToI2(const R2 &P) const
Definition: Mesh.cpp:3937
bamg::P2::x
R x
Definition: R2.h:15
bamg::EigenMetric
Definition: Metric.h:54
bamg::Norme2_2
RR Norme2_2(const P2< R, RR > x)
Definition: R2.h:89
bamg::AdjacentTriangle::EdgeVertex
BamgVertex * EdgeVertex(const int &i) const
Definition: AdjacentTriangle.cpp:32
BamgOpts::anisomax
double anisomax
Definition: BamgOpts.h:13
bamg::P2::y
R y
Definition: R2.h:15
BamgOpts::Metrictype
int Metrictype
Definition: BamgOpts.h:21
bamg::P2< double, double >
bamg::Abs
T Abs(const T &a)
Definition: Abs.h:5
bamg::Mesh::TriangleFindFromCoord
Triangle * TriangleFindFromCoord(const I2 &, long long[3], Triangle *tstart=0)
Definition: Mesh.cpp:3945
bamg::Metric::IntersectWith
int IntersectWith(const Metric &M2)
Definition: Metric.cpp:99
bamg::BamgVertex::m
Metric m
Definition: BamgVertex.h:22
bamgobjects.h
bamg::BamgVertex::IndexInTriangle
short IndexInTriangle
Definition: BamgVertex.h:25
bamg::NextEdge
static const short NextEdge[3]
Definition: macros.h:17
bamg::BamgVertex::t
Triangle * t
Definition: BamgVertex.h:28
bamg::BamgVertex::Optim
long Optim(int=1, int=0)
Definition: BamgVertex.cpp:112