projectCurveEdge.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2024 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"
29 #include "pointConstraint.H"
30 #include "OBJstream.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
46 (
47  const dictionary& dict,
48  const label index,
49  const searchableSurfaces& geometry,
50  const pointField& points,
51  Istream& is
52 )
53 :
54  blockEdge(dict, index, points, is),
55  geometry_(geometry)
56 {
57  wordList names(is);
58  surfaces_.setSize(names.size());
59  forAll(names, i)
60  {
61  surfaces_[i] = geometry_.findSurfaceID(names[i]);
62 
63  if (surfaces_[i] == -1)
64  {
66  << "Cannot find surface " << names[i] << " in geometry"
67  << exit(FatalIOError);
68  }
69 
70  if (isA<searchableExtrudedCircle>(geometry_[surfaces_[i]]))
71  {
72  Info<< type() << " : Using curved surface "
73  << geometry_[surfaces_[i]].name()
74  << " to predict starting points." << endl;
75  }
76  }
77 }
78 
79 
80 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 
84 {
85  // For debugging to tag the output
86  static label eIter = 0;
87 
88  autoPtr<OBJstream> debugStr;
89  if (debug)
90  {
91  debugStr.reset
92  (
93  new OBJstream("projectCurveEdge_" + Foam::name(eIter++) + ".obj")
94  );
95  Info<< "Writing lines from straight-line start points"
96  << " to projected points to " << debugStr().name() << endl;
97  }
98 
99 
100  tmp<pointField> tpoints(new pointField(lambdas.size()));
101  pointField& points = tpoints.ref();
102 
103  const point& startPt = points_[start_];
104  const point& endPt = points_[end_];
105  const vector d = endPt-startPt;
106 
107  // Initial guess
108  forAll(lambdas, i)
109  {
110  points[i] = startPt+lambdas[i]*d;
111  }
112 
113  // Use special interpolation to keep initial guess on same position on
114  // surface
115  forAll(surfaces_, i)
116  {
117  if (isA<searchableExtrudedCircle>(geometry_[surfaces_[i]]))
118  {
119  const searchableExtrudedCircle& s =
120  refCast<const searchableExtrudedCircle>
121  (
122  geometry_[surfaces_[i]]
123  );
125  s.findParametricNearest
126  (
127  points[0],
128  points.last(),
129  scalarField(lambdas),
130  scalarField(points.size(), magSqr(d)),
131  nearInfo
132  );
133  forAll(nearInfo, i)
134  {
135  if (nearInfo[i].hit())
136  {
137  points[i] = nearInfo[i].hitPoint();
138  }
139  }
140 
141  break;
142  }
143  }
144 
145 
146 
147  // Upper limit for number of iterations
148  const label maxIter = 10;
149 
150  // Residual tolerance
151  const scalar relTol = 0.1;
152  const scalar absTol = 1e-4;
153 
154  scalar initialResidual = 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 - 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;
195  for (label i = 1; i < points.size(); i++)
196  {
197  projLambdas[i] =
198  projLambdas[i-1] + mag(points[i] - points[i-1]);
199  }
200  projLambdas /= projLambdas.last();
201  }
202  linearInterpolationWeights interpolator(projLambdas);
203 
204  // Compare actual distances and move points (along straight line;
205  // not along surface)
206  vectorField residual(points.size(), vector::zero);
207  labelList indices;
208  scalarField weights;
209  for (label i = 1; i < points.size() - 1; i++)
210  {
211  interpolator.valueWeights(lambdas[i], indices, weights);
212 
213  point predicted = vector::zero;
214  forAll(indices, indexi)
215  {
216  predicted += weights[indexi]*points[indices[indexi]];
217  }
218  residual[i] = predicted - points[i];
219  }
220 
221  const scalar scalarResidual = sum(mag(residual));
222 
223  if (debug)
224  {
225  Pout<< "Iter:" << iter << " initialResidual:" << initialResidual
226  << " residual:" << scalarResidual << endl;
227  }
228 
229  if (scalarResidual < absTol*0.5*lambdas.size())
230  {
231  break;
232  }
233  else if (iter == 0)
234  {
235  initialResidual = scalarResidual;
236  }
237  else if (scalarResidual/initialResidual < relTol)
238  {
239  break;
240  }
241 
242 
243  if (debugStr.valid())
244  {
245  forAll(points, i)
246  {
247  const point predicted(points[i] + residual[i]);
248  debugStr().write(linePointRef(points[i], predicted));
249  }
250  }
251 
252  points += residual;
253  }
254 
255  return tpoints;
256 }
257 
258 
259 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
OFstream which keeps track of vertices.
Definition: OBJstream.H:56
T & last()
Return the last element of the list.
Definition: UListI.H:128
static const Form zero
Definition: VectorSpace.H:118
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
Define a curved edge that is parameterised for 0<lambda<1 between the start and end point.
Definition: blockEdge.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
virtual bool valueWeights(const scalar t, labelList &indices, scalarField &weights) const
Calculate weights and indices to calculate t from samples.
Defines the edge from the projection onto a surface (single surface) or intersection of two surfaces.
projectCurveEdge(const dictionary &dict, const label index, const searchableSurfaces &geometry, const pointField &points, Istream &)
Construct from Istream setting pointsList.
virtual point position(const scalar) const
Return the point positions corresponding to the curve parameters.
Surface geometry with a tube shape, which can be used with snappyHexMesh. The geometry is formed from...
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.
Container for searchableSurfaces.
label findSurfaceID(const word &name) const
Find index of surface. Return -1 if not found.
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
const pointField & points
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Tuple2< scalar, label > nearInfo
Private class for finding nearest.
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const doubleScalar e
Definition: doubleScalar.H:106
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
addToRunTimeSelectionTable(polyPatch, mergedCyclicPolyPatch, word)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
line< point, const point & > linePointRef
Line using referred points.
Definition: linePointRef.H:45
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
messageStream Info
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
dimensioned< scalar > mag(const dimensioned< Type > &)
defineTypeNameAndDebug(combustionModel, 0)
IOerror FatalIOError
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
dimensioned< scalar > magSqr(const dimensioned< Type > &)
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict