projectCurveEdge.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM 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 OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
27 #include "projectCurveEdge.H"
28 #include "unitConversion.H"
30 #include "pointConstraint.H"
31 #include "OBJstream.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(projectCurveEdge, 0);
40  addToRunTimeSelectionTable(blockEdge, projectCurveEdge, Istream);
41 }
42 
43 
44 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
45 
46 Foam::projectCurveEdge::projectCurveEdge
47 (
48  const dictionary& dict,
49  const label index,
50  const searchableSurfaces& geometry,
51  const pointField& points,
52  Istream& is
53 )
54 :
55  blockEdge(dict, index, points, is),
56  geometry_(geometry)
57 {
58  wordList names(is);
59  surfaces_.setSize(names.size());
60  forAll(names, i)
61  {
62  surfaces_[i] = geometry_.findSurfaceID(names[i]);
63 
64  if (surfaces_[i] == -1)
65  {
67  << "Cannot find surface " << names[i] << " in geometry"
68  << exit(FatalIOError);
69  }
70 
71  if (isA<searchableExtrudedCircle>(geometry_[surfaces_[i]]))
72  {
73  Info<< type() << " : Using curved surface "
74  << geometry_[surfaces_[i]].name()
75  << " to predict starting points." << endl;
76  }
77  }
78 }
79 
80 
81 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
82 
85 {
86  // For debugging to tag the output
87  static label eIter = 0;
88 
89  autoPtr<OBJstream> debugStr;
90  if (debug)
91  {
92  debugStr.reset
93  (
94  new OBJstream("projectCurveEdge_" + Foam::name(eIter++) + ".obj")
95  );
96  Info<< "Writing lines from straight-line start points"
97  << " to projected points to " << debugStr().name() << endl;
98  }
99 
100 
101  tmp<pointField> tpoints(new pointField(lambdas.size()));
102  pointField& points = tpoints.ref();
103 
104  const point& startPt = points_[start_];
105  const point& endPt = points_[end_];
106  const vector d = endPt-startPt;
107 
108  // Initial guess
109  forAll(lambdas, i)
110  {
111  points[i] = startPt+lambdas[i]*d;
112  }
113 
114  // Use special interpolation to keep initial guess on same position on
115  // surface
116  forAll(surfaces_, i)
117  {
118  if (isA<searchableExtrudedCircle>(geometry_[surfaces_[i]]))
119  {
120  const searchableExtrudedCircle& s =
121  refCast<const searchableExtrudedCircle>
122  (
123  geometry_[surfaces_[i]]
124  );
127  (
128  points[0],
129  points.last(),
130  scalarField(lambdas),
131  scalarField(points.size(), magSqr(d)),
132  nearInfo
133  );
134  forAll(nearInfo, i)
135  {
136  if (nearInfo[i].hit())
137  {
138  points[i] = nearInfo[i].hitPoint();
139  }
140  }
141 
142  break;
143  }
144  }
145 
146 
147 
148  // Upper limit for number of iterations
149  const label maxIter = 10;
150  // Residual tolerance
151  const scalar relTol = 0.1;
152  const scalar absTol = 1e-4;
153 
154  scalar initialResidual = 0.0;
155 
156  for (label iter = 0; iter < maxIter; iter++)
157  {
158  // Do projection
159  {
160  List<pointConstraint> constraints(lambdas.size());
161  pointField start(points);
163  (
164  geometry_,
165  surfaces_,
166  start,
167  scalarField(start.size(), magSqr(d)),
168  points,
169  constraints
170  );
171 
172  // Reset start and end point
173  if (lambdas[0] < SMALL)
174  {
175  points[0] = startPt;
176  }
177  if (lambdas.last() > 1.0-SMALL)
178  {
179  points.last() = endPt;
180  }
181 
182  if (debugStr.valid())
183  {
184  forAll(points, i)
185  {
186  debugStr().write(linePointRef(start[i], points[i]));
187  }
188  }
189  }
190 
191  // Calculate lambdas (normalised coordinate along edge)
192  scalarField projLambdas(points.size());
193  {
194  projLambdas[0] = 0.0;
195  for (label i = 1; i < points.size(); i++)
196  {
197  projLambdas[i] = projLambdas[i-1] + mag(points[i]-points[i-1]);
198  }
199  projLambdas /= projLambdas.last();
200  }
201  linearInterpolationWeights interpolator(projLambdas);
202 
203  // Compare actual distances and move points (along straight line;
204  // not along surface)
205  vectorField residual(points.size(), vector::zero);
206  labelList indices;
207  scalarField weights;
208  for (label i = 1; i < points.size() - 1; i++)
209  {
210  interpolator.valueWeights(lambdas[i], indices, weights);
211 
212  point predicted = vector::zero;
213  forAll(indices, indexi)
214  {
215  predicted += weights[indexi]*points[indices[indexi]];
216  }
217  residual[i] = predicted-points[i];
218  }
219 
220  scalar scalarResidual = sum(mag(residual));
221 
222  if (debug)
223  {
224  Pout<< "Iter:" << iter << " initialResidual:" << initialResidual
225  << " residual:" << scalarResidual << endl;
226  }
227 
228  if (scalarResidual < absTol*0.5*lambdas.size())
229  {
230  break;
231  }
232  else if (iter == 0)
233  {
234  initialResidual = scalarResidual;
235  }
236  else if (scalarResidual/initialResidual < relTol)
237  {
238  break;
239  }
240 
241 
242  if (debugStr.valid())
243  {
244  forAll(points, i)
245  {
246  const point predicted(points[i] + residual[i]);
247  debugStr().write(linePointRef(points[i], predicted));
248  }
249  }
250 
251  points += residual;
252  }
253 
254  return tpoints;
255 }
256 
257 
258 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
virtual point position(const scalar) const
Return the point positions corresponding to the curve parameters.
const double e
Elementary charge.
Definition: doubleFloat.H:78
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
static void findNearest(const PtrList< searchableSurface > &, const labelList &surfacesToTest, const pointField &, const scalarField &nearestDistSqr, labelList &surfaces, List< pointIndexHit > &)
Find nearest. Return -1 (and a miss()) or surface and nearest.
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:174
Unit conversion functions.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Macros for easy insertion into run-time selection tables.
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
virtual void findParametricNearest(const point &start, const point &end, const scalarField &lambdas, const scalarField &nearestDistSqr, List< pointIndexHit > &) const
Unique to parametric geometry: given points find.
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
virtual bool valueWeights(const scalar t, labelList &indices, scalarField &weights) const
Calculate weights and indices to calculate t from samples.
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
line< point, const point & > linePointRef
Line using referred points.
Definition: linePointRef.H:45
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Container for searchableSurfaces.
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
dimensioned< scalar > magSqr(const dimensioned< Type > &)
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
OFstream which keeps track of vertices.
Definition: OBJstream.H:53
defineTypeNameAndDebug(combustionModel, 0)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
fileName::Type type(const fileName &, const bool followLink=true)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:485
Define a curved edge that is parameterized for 0<lambda<1 between the start and end point...
Definition: blockEdge.H:56
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Searching on edgemesh with constant radius.
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
A class for managing temporary objects.
Definition: PtrList.H:53
T & last()
Return the last element of the list.
Definition: UListI.H:128
Tuple2< scalar, label > nearInfo
Private class for finding nearest.
Namespace for OpenFOAM.
IOerror FatalIOError