quadricMetric.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "demandDrivenData.H"
29 #include "quadricMetric.H"
30 #include "partTetMeshSimplex.H"
31 
32 //#define DEBUGSmooth
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 // Private member functions
41 
43 {
44  scalar val(0.0);
45 
46  forAll(normals_, nI)
47  val += Foam::sqr(normals_[nI] & (p_ - centres_[nI]));
48 
49  return val;
50 }
51 
53 {
55  gradGrad = tensor::zero;
56 
57  forAll(normals_, nI)
58  {
59  const scalar fx = (normals_[nI] & (p_ - centres_[nI]));
60 
61  grad += fx * normals_[nI];
62  gradGrad += normals_[nI] * normals_[nI];
63  }
64 }
65 
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 
69 :
70  simplexSmoother(simplex),
71  p_(simplex.pts()[simplex.tets()[0][3]]),
72  normals_(),
73  centres_()
74 {
75  forAll(tets_, tetI)
76  {
77  const partTet& pt = tets_[tetI];
78  const triangle<point, point> tri
79  (
80  points_[pt.a()],
81  points_[pt.b()],
82  points_[pt.c()]
83  );
84 
85  const vector n = tri.normal();
86  const scalar d = mag(n);
87 
88  if( d > VSMALL )
89  {
90  centres_.append(tri.centre());
91  normals_.append(n/d);
92  }
93  }
94 }
95 
96 
98 {
99 }
100 
101 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
102 
103 // Implementation of knupp metric untangling
104 void quadricMetric::optimizeNodePosition(const scalar /*tolObsolete*/)
105 {
106  if( !bb_.contains(p_) )
107  p_ = 0.5 * (bb_.min() + bb_.max());
108 
109  const scalar tol = Foam::sqr(2.0 * SMALL) * magSqr(bb_.min() - bb_.max());
110 
111  label iterI, outerIter(0);
112 
113  vector gradF, disp;
114  tensor gradGradF;
115 
116  scalar func, lastFunc;
117 
118  # ifdef DEBUGSmooth
119  forAll(normals_, nI)
120  {
121  const scalar fx = normals_[nI] & (p_ - centres_[nI]);
122  Info << "Tet " << nI << " has distance " << fx << " func "
123  << Foam::sqr(mag(fx) - fx) << endl;
124  }
125  Info << "BoundBox size " << (bb_.max() - bb_.min()) << endl;
126  Info << "Tolerance " << tol << endl;
127  # endif
128 
129  bool finished;
130  do
131  {
132  finished = true;
133 
134  lastFunc = evaluateMetric();
135 
136  iterI = 0;
137  do
138  {
139  # ifdef DEBUGSmooth
140  Info << "Iteration " << iterI << endl;
141  Info << "Initial metric value " << lastFunc << endl;
142  # endif
143 
144  //- store previous value
145  const point pOrig = p_;
146 
147  //- evaluate gradients
148  evaluateGradients(gradF, gradGradF);
149 
150  //- calculate displacement
151  const scalar determinant = det(gradGradF);
152  if( determinant > SMALL )
153  {
154  disp = (inv(gradGradF, determinant) & gradF);
155 
156  for(direction i=0;i<vector::nComponents;++i)
157  {
158  const scalar& val = disp[i];
159  if( (val != val) || ((val - val) != (val - val)) )
160  {
161  disp = vector::zero;
162  break;
163  }
164  }
165 
166  p_ -= disp;
167 
168  func = evaluateMetric();
169 
170  # ifdef DEBUGSmooth
171  Info << "Second grad " << gradGradF << endl;
172  Info << "inv(gradGradF, determinant) "
173  << inv(gradGradF, determinant) << endl;
174  Info << "Gradient " << gradF << endl;
175  Info << "Determinant " << determinant << endl;
176  Info << "Displacement " << disp << endl;
177  Info << "New metric value " << func << endl;
178  # endif
179 
180  scalar relax(0.8);
181  label nLoops(0);
182  while( func > lastFunc )
183  {
184  p_ = pOrig - relax * disp;
185  relax *= 0.5;
186  func = evaluateMetric();
187 
188  if( func < lastFunc )
189  continue;
190 
191  //- it seems that this direction is wrong
192  if( ++nLoops == 5 )
193  {
194  p_ = pOrig;
195  disp = vector::zero;
196  func = 0.0;
197  }
198  }
199 
200  lastFunc = func;
201  }
202  else
203  {
204  disp = vector::zero;
205  }
206  } while( (magSqr(disp) > tol) && (++iterI < 10) );
207 
208  if( lastFunc < VSMALL )
209  finished = false;
210  } while( !finished && (++outerIter < 5) );
211 
212  # ifdef DEBUGSmooth
213  Info << "Last value " << lastFunc << endl;
214  Info << "Beta " << beta_ << endl;
215  Info << "Metric with no beta " << evaluateMetricNoBeta() << endl;
216  forAll(normals_, nI)
217  {
218  const scalar fx = normals_[nI] & (p_ - centres_[nI]);
219  Info << "Tet " << nI << " has distance " << fx << " func "
220  << Foam::sqr(mag(fx) - fx) << endl;
221  }
222  //::exit(1);
223  # endif
224 }
225 
226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 
228 } // End namespace Foam
229 
230 // ************************************************************************* //
Foam::Tensor
Templated 3D tensor derived from VectorSpace adding construction from 9 components,...
Definition: complexI.H:224
Foam::quadricMetric::p_
point & p_
free vertex
Definition: quadricMetric.H:57
Foam::fvc::grad
tmp< GeometricField< typename outerProduct< vector, Type >::type, fvPatchField, volMesh >> grad(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Definition: fvcGrad.C:52
relax
p relax()
Foam::Vector< scalar >::zero
static const Vector zero
Definition: Vector.H:80
Foam::partTetMeshSimplex
Definition: partTetMeshSimplex.H:52
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::partTet::b
label b() const
Definition: partTetI.H:68
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
Foam::partTet
Definition: partTet.H:53
Foam::VectorSpace< Vector< scalar >, scalar, 3 >::nComponents
@ nComponents
Number of components in this vector space.
Definition: VectorSpace.H:88
Foam::quadricMetric::~quadricMetric
~quadricMetric()
Definition: quadricMetric.C:97
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::quadricMetric::centres_
DynList< point, 64 > centres_
centres of triangles forming the outer hull
Definition: quadricMetric.H:63
Foam::triangle::centre
Point centre() const
Return centre (centroid)
Definition: triangleI.H:102
Foam::triangle
A triangle primitive used to calculate face normals and swept volumes.
Definition: triangle.H:59
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::quadricMetric::evaluateMetric
scalar evaluateMetric() const
evaluate the value of the metric
Definition: quadricMetric.C:42
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::simplexSmoother
class for volume optimizer
Definition: simplexSmoother.H:56
Foam::triangle::normal
vector normal() const
Return vector normal.
Definition: triangleI.H:116
Foam::inv
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
Definition: dimensionedSphericalTensor.C:71
Foam::Info
messageStream Info
partTetMeshSimplex.H
Foam::simplexSmoother::points_
DynList< point, 128 > & points_
mesh points
Definition: simplexSmoother.H:62
Foam::func
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
Foam::simplexSmoother::tets_
const DynList< partTet, 128 > & tets_
list tets around the given vertex
Definition: simplexSmoother.H:65
Foam::partTet::c
label c() const
Definition: partTetI.H:73
Foam::Tensor::zero
static const Tensor zero
Definition: Tensor.H:80
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::quadricMetric::evaluateGradients
void evaluateGradients(vector &grad, tensor &gradGrad) const
evaluate metric gradients
Definition: quadricMetric.C:52
Foam::simplexSmoother::bb_
boundBox bb_
bound box
Definition: simplexSmoother.H:71
Foam::quadricMetric::normals_
DynList< vector, 64 > normals_
normals of triangles forming the outer hull
Definition: quadricMetric.H:60
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:49
Foam::quadricMetric::quadricMetric
quadricMetric(partTetMeshSimplex &simplex)
Definition: quadricMetric.C:68
Foam::Vector< scalar >
Foam::boundBox::contains
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:170
Foam::direction
unsigned char direction
Definition: direction.H:43
quadricMetric.H
Foam::det
dimensionedScalar det(const dimensionedSphericalTensor &dt)
Definition: dimensionedSphericalTensor.C:60
Foam::partTet::a
label a() const
Return vertices.
Definition: partTetI.H:63
Foam::magSqr
dimensioned< scalar > magSqr(const dimensioned< Type > &)
Foam::quadricMetric::optimizeNodePosition
void optimizeNodePosition(const scalar tol=0.001)
Definition: quadricMetric.C:104