arcEdge.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "arcEdge.H"
30 #include "unitConversion.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace blockEdges
38 {
41 }
42 }
43 
44 
45 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
46 
47 void Foam::blockEdges::arcEdge::calcFromMidPoint
48 (
49  const point& p1,
50  const point& p3,
51  const point& p2
52 )
53 {
54  const vector a = p2 - p1;
55  const vector b = p3 - p1;
56 
57  // Find centre of arcEdge
58  const scalar asqr = a & a;
59  const scalar bsqr = b & b;
60  const scalar adotb = a & b;
61 
62  const scalar denom = asqr*bsqr - adotb*adotb;
63 
64  if (mag(denom) < ROOTVSMALL)
65  {
67  << denom
68  << abort(FatalError);
69  }
70 
71  const scalar fact = 0.5*(bsqr - adotb)/denom;
72 
73  const point centre = p1 + 0.5*a + fact*((a ^ b) ^ a);
74 
75  // Position vectors from centre
76  const vector r1(p1 - centre);
77  const vector r2(p2 - centre);
78  const vector r3(p3 - centre);
79 
80  const scalar mag1(mag(r1));
81  const scalar mag3(mag(r3));
82 
83  vector arcAxis(r1 ^ r3);
84 
85  // The radius from r1 and from r3 will be identical
86  radius_ = mag(r3);
87 
88 
89  // Determine the angle
90  angle_ = acos((r1 & r3)/(mag1*mag3));
91 
92  // Check if the vectors define an exterior or an interior arcEdge
93  if (((r1 ^ r2) & (r1 ^ r3)) < 0.0)
94  {
95  angle_ = constant::mathematical::twoPi - angle_;
96  }
97 
98  if (angle_ <= constant::mathematical::pi)
99  {
100  if (mag(arcAxis)/(mag1*mag3) < 0.001)
101  {
102  arcAxis = r1 ^ r2;
103  }
104  }
105  else
106  {
107  arcAxis = -arcAxis;
108  }
109 
110  // Corresponding local cylindrical coordinate system
111  cs_ = coordSystem::cylindrical(centre, arcAxis, r1);
112 }
113 
114 
115 void Foam::blockEdges::arcEdge::calcFromCentre
116 (
117  const point& p1,
118  const point& p3,
119  const point& centre,
120  bool adjustCentre,
121  scalar rMultiplier
122 )
123 {
124  // Position vectors from centre
125  const vector r1(p1 - centre);
126  const vector r3(p3 - centre);
127 
128  const scalar mag1(mag(r1));
129  const scalar mag3(mag(r3));
130 
131  const vector chord(p3 - p1);
132 
133  const vector arcAxis(r1 ^ r3);
134 
135  // The average radius
136  radius_ = 0.5*(mag1 + mag3);
137 
138  // The included angle
139  angle_ = acos((r1 & r3)/(mag1*mag3));
140 
141  // TODO? check for 180 degrees (co-linear points)?
142 
143  bool needsAdjust = false;
144 
145  if (adjustCentre)
146  {
147  needsAdjust = !equal(mag1, mag3);
148 
149  if (!equal(rMultiplier, 1))
150  {
151  // The min radius is constrained by the chord,
152  // otherwise bad things will happen.
153 
154  needsAdjust = true;
155  radius_ *= rMultiplier;
156  radius_ = max(radius_, (1.001*0.5*mag(chord)));
157  }
158  }
159 
160  if (needsAdjust)
161  {
162  // The centre is not equidistant to p1 and p3.
163  // Use the chord and the arcAxis to determine the vector to
164  // the midpoint of the chord and adjust the centre along this
165  // line.
166 
167  const point newCentre =
168  (
169  (0.5 * (p3 + p1)) // mid-chord point
170  + sqrt(sqr(radius_) - 0.25 * magSqr(chord))
171  * normalised(arcAxis ^ chord) // mid-chord -> centre
172  );
173 
178 
179  // Recalculate - do attempt to readjust
180  calcFromCentre(p1, p3, newCentre, false);
181  }
182  else
183  {
184  // Corresponding local cylindrical coordinate system
185  cs_ = coordSystem::cylindrical(centre, arcAxis, r1);
186  }
187 }
188 
189 
190 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
191 
192 Foam::blockEdges::arcEdge::arcEdge
193 (
194  const pointField& points,
195  const point& origin,
196  const label start,
197  const label end
198 )
199 :
200  blockEdge(points, start, end),
201  radius_(0),
202  angle_(0),
203  cs_()
204 {
205  calcFromCentre(points[start_], points[end_], origin);
206 }
207 
208 
209 Foam::blockEdges::arcEdge::arcEdge
210 (
211  const pointField& points,
212  const label start,
213  const label end,
214  const point& midPoint
215 )
216 :
217  blockEdge(points, start, end),
218  radius_(0),
219  angle_(0),
220  cs_()
221 {
222  calcFromMidPoint(points[start_], points[end_], midPoint);
223 }
224 
225 
226 Foam::blockEdges::arcEdge::arcEdge
227 (
228  const dictionary& dict,
229  const label index,
230  const searchableSurfaces& geometry,
231  const pointField& points,
232  Istream& is
233 )
234 :
235  blockEdge(dict, index, points, is),
236  radius_(0),
237  angle_(0),
238  cs_()
239 {
240  point p;
241 
242  token tok(is);
243  if (tok.isWord())
244  {
245  // Can be
246  // - origin (0 0 0)
247  // - origin 1.2 (0 0 0)
248 
249  scalar rMultiplier = 1;
250 
251  is >> tok;
252  if (tok.isNumber())
253  {
254  rMultiplier = tok.number();
255  }
256  else
257  {
258  is.putBack(tok);
259  }
260 
261  is >> p; // The origin (centre)
262 
263  calcFromCentre(points_[start_], points_[end_], p, true, rMultiplier);
264  }
265  else
266  {
267  is.putBack(tok);
268 
269  is >> p; // A mid-point
270 
271  calcFromMidPoint(points_[start_], points_[end_], p);
272  }
273 
274  if (debug)
275  {
276  Info<< "arc " << start_ << ' ' << end_ << ' '
277  << position(0.5) << " origin " << cs_.origin() << " // ";
278  cs_.rotation().write(Info);
279  Info<< nl;
280  }
281 }
282 
283 
284 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
285 
287 {
288  #ifdef FULLDEBUG
289  if (lambda < -SMALL || lambda > 1 + SMALL)
290  {
292  << "Limit parameter to [0-1] range: " << lambda << nl;
293  }
294  #endif
295 
296  if (lambda < SMALL)
297  {
298  return points_[start_];
299  }
300  else if (lambda >= 1 - SMALL)
301  {
302  return points_[end_];
303  }
304 
305  return cs_.globalPosition(vector(radius_, (lambda*angle_), 0));
306 }
307 
308 
309 Foam::scalar Foam::blockEdges::arcEdge::length() const noexcept
310 {
311  return (radius_*angle_);
312 }
313 
314 
315 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::blockEdges::arcEdge::length
scalar length() const noexcept
The length of the curve.
Definition: arcEdge.C:309
unitConversion.H
Unit conversion functions.
Foam::midPoint
Mid-point interpolation (weighting factors = 0.5) scheme class.
Definition: midPoint.H:55
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::blockEdge
Define a curved edge that is parameterized for 0<lambda<1 between the start/end points.
Definition: blockEdge.H:62
Foam::token::isWord
bool isWord() const noexcept
Token is WORD or DIRECTIVE word.
Definition: tokenI.H:583
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Foam::token::number
scalar number() const
Return label, float or double value.
Definition: tokenI.H:567
Foam::blockEdges::defineTypeNameAndDebug
defineTypeNameAndDebug(arcEdge, 0)
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Field< vector >
Foam::blockEdges::addToRunTimeSelectionTable
addToRunTimeSelectionTable(blockEdge, arcEdge, Istream)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::constant::mathematical::twoPi
constexpr scalar twoPi(2 *M_PI)
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::blockEdges::arcEdge::position
point position(const scalar lambda) const
The point corresponding to the curve parameter [0-1].
Definition: arcEdge.C:286
lambda
dimensionedScalar lambda("lambda", dimTime/sqr(dimLength), laminarTransport)
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:51
Foam::normalised
VectorSpace< Form, Cmpt, Ncmpts > normalised(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:483
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::constant::mathematical::pi
constexpr scalar pi(M_PI)
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Vector< scalar >
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::acos
dimensionedScalar acos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:268
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
Foam::searchableSurfaces
Container for searchableSurfaces. The collection is specified as a dictionary. For example,...
Definition: searchableSurfaces.H:92
arcEdge.H
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::blockEdges::arcEdge
A blockEdge defined as an arc of a circle.
Definition: arcEdge.H:79
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46