ENH: cylindricalCS is now in radians only (issue #863)

- this provides internal consistency and allows direct use of the
  coordinate angle with sin(), cos() functions.
  It eliminates potential issues that could otherwise arise from
  alternative user input.

  Eg, in mixerFvMesh it would have previously been possible to specify
  the coordinate system to use degrees or radians, but these units were
  not checked when determining the tangential sweep positions.

NOTE: this may represent a breaking change if user coding has been
relying on cylindrical coordinate system in degrees.
This commit is contained in:
Mark Olesen
2018-09-24 11:31:04 +02:00
parent efaa9f84be
commit dcc1dc1383
8 changed files with 113 additions and 143 deletions

View File

@ -381,7 +381,7 @@ void Foam::fv::rotorDiskSource::createCoordinateSystem()
} }
} }
coordSys_ = cylindricalCS("rotorCoordSys", origin, axis, refDir, false); coordSys_ = cylindricalCS("rotorCS", origin, axis, refDir);
const scalar sumArea = gSum(area_); const scalar sumArea = gSum(area_);
const scalar diameter = Foam::sqrt(4.0*sumArea/mathematical::pi); const scalar diameter = Foam::sqrt(4.0*sumArea/mathematical::pi);
@ -483,7 +483,7 @@ Foam::fv::rotorDiskSource::rotorDiskSource
R_(cells_.size(), I), R_(cells_.size(), I),
invR_(cells_.size(), I), invR_(cells_.size(), I),
area_(cells_.size(), 0.0), area_(cells_.size(), 0.0),
coordSys_(false), coordSys_(),
cylindrical_(), cylindrical_(),
rMax_(0.0), rMax_(0.0),
trim_(trimModel::New(*this, coeffs_)), trim_(trimModel::New(*this, coeffs_)),

View File

@ -196,7 +196,7 @@ protected:
//- Area [m2] //- Area [m2]
List<scalar> area_; List<scalar> area_;
//- Rotor local cylindrical coordinate system (r, theta, z) //- Rotor local cylindrical coordinate system (r-theta-z)
cylindricalCS coordSys_; cylindricalCS coordSys_;
//- Rotor transformation coordinate system //- Rotor transformation coordinate system
@ -279,7 +279,7 @@ public:
// (Cylindrical r, theta, z) // (Cylindrical r, theta, z)
inline const List<point>& x() const; inline const List<point>& x() const;
//- Return the rotor coordinate system (r, theta, z) //- Return the rotor coordinate system (r-theta-z)
inline const cylindricalCS& coordSys() const; inline const cylindricalCS& coordSys() const;

View File

@ -207,7 +207,7 @@ void Foam::ParticleCollector<CloudType>::initConcentricCircles()
faces_.setSize(nFace); faces_.setSize(nFace);
area_.setSize(nFace); area_.setSize(nFace);
coordSys_ = cylindricalCS("coordSys", origin, normal_[0], refDir, false); coordSys_ = cylindricalCS("collector", origin, normal_[0], refDir);
List<label> ptIDs(identity(nPointPerRadius)); List<label> ptIDs(identity(nPointPerRadius));
@ -539,7 +539,7 @@ Foam::ParticleCollector<CloudType>::ParticleCollector
faceTris_(), faceTris_(),
nSector_(0), nSector_(0),
radius_(), radius_(),
coordSys_(false), coordSys_(),
normal_(), normal_(),
negateParcelsOppositeNormal_ negateParcelsOppositeNormal_
( (

View File

@ -98,8 +98,8 @@ Foam::cylindricalCS Foam::blockEdges::arcEdge::calcAngle()
radius_ = mag(r3); radius_ = mag(r3);
// The corresponding local cylindrical coordinate system (degrees) // The corresponding local cylindrical coordinate system (radians)
return cylindricalCS("arcEdgeCS", centre, arcAxis, r1, true); return cylindricalCS("arc", centre, arcAxis, r1);
} }
@ -158,13 +158,14 @@ Foam::point Foam::blockEdges::arcEdge::position(const scalar lambda) const
return p3_; return p3_;
} }
return cs_.globalPosition(vector(radius_, lambda*angle_, 0.0)); // The angle is degrees, the coordinate system in radians
return cs_.globalPosition(vector(radius_, degToRad(lambda*angle_), 0));
} }
Foam::scalar Foam::blockEdges::arcEdge::length() const Foam::scalar Foam::blockEdges::arcEdge::length() const
{ {
return degToRad(angle_*radius_); return degToRad(radius_*angle_);
} }

View File

@ -64,7 +64,7 @@ class arcEdge
//- The arc radius //- The arc radius
scalar radius_; scalar radius_;
//- The local cylindrical coordinate system (degrees) //- The local cylindrical coordinate system
cylindricalCS cs_; cylindricalCS cs_;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -24,41 +24,69 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "cylindricalCS.H" #include "cylindricalCS.H"
#include "one.H"
#include "mathematicalConstants.H"
#include "addToRunTimeSelectionTable.H" #include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Issue warning if 'degrees' keyword was specified and true.
// Compatibility change after 1806.
static inline void warnCompatDegrees(const Foam::dictionary& dict)
{
if (Pstream::parRun() ? Pstream::master() : true)
{
std::cerr
<< "--> FOAM IOWarning :" << nl
<< " Found [v1806] 'degrees' keyword in dictionary \""
<< dict.name().c_str() << "\" Ignored, now radians only." << nl
<< std::endl;
}
}
//- Convert from Cartesian (to Cylindrical)
static inline vector fromCartesian(const vector& v)
{
return vector(hypot(v.x(), v.y()), atan2(v.y(), v.x()), v.z());
}
//- Convert to Cartesian (from Cylindrical)
static inline vector toCartesian(const vector& v)
{
return vector(v.x()*cos(v.y()), v.x()*sin(v.y()), v.z());
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::cylindricalCS::cylindricalCS(const bool inDegrees) Foam::cylindricalCS::cylindricalCS()
: :
coordinateSystem(), coordinateSystem()
inDegrees_(inDegrees)
{} {}
Foam::cylindricalCS::cylindricalCS Foam::cylindricalCS::cylindricalCS
( (
const coordinateSystem& cs, const coordinateSystem& cs
const bool inDegrees
) )
: :
coordinateSystem(cs), coordinateSystem(cs)
inDegrees_(inDegrees)
{} {}
Foam::cylindricalCS::cylindricalCS Foam::cylindricalCS::cylindricalCS
( (
const word& name, const word& name,
const coordinateSystem& cs, const coordinateSystem& cs
const bool inDegrees
) )
: :
coordinateSystem(name, cs), coordinateSystem(name, cs)
inDegrees_(inDegrees)
{} {}
@ -66,12 +94,10 @@ Foam::cylindricalCS::cylindricalCS
( (
const word& name, const word& name,
const point& origin, const point& origin,
const coordinateRotation& cr, const coordinateRotation& cr
const bool inDegrees
) )
: :
coordinateSystem(name, origin, cr), coordinateSystem(name, origin, cr)
inDegrees_(inDegrees)
{} {}
@ -80,12 +106,10 @@ Foam::cylindricalCS::cylindricalCS
const word& name, const word& name,
const point& origin, const point& origin,
const vector& axis, const vector& axis,
const vector& dirn, const vector& dirn
const bool inDegrees
) )
: :
coordinateSystem(name, origin, axis, dirn), coordinateSystem(name, origin, axis, dirn)
inDegrees_(inDegrees)
{} {}
@ -95,9 +119,13 @@ Foam::cylindricalCS::cylindricalCS
const dictionary& dict const dictionary& dict
) )
: :
coordinateSystem(name, dict), coordinateSystem(name, dict)
inDegrees_(dict.lookupOrDefault("degrees", true)) {
{} if (dict.lookupOrDefault("degrees", false))
{
warnCompatDegrees(dict);
}
}
Foam::cylindricalCS::cylindricalCS Foam::cylindricalCS::cylindricalCS
@ -106,45 +134,26 @@ Foam::cylindricalCS::cylindricalCS
const dictionary& dict const dictionary& dict
) )
: :
coordinateSystem(obr, dict), coordinateSystem(obr, dict)
inDegrees_(dict.lookupOrDefault("degrees", true)) {
{} if (dict.lookupOrDefault("degrees", false))
{
warnCompatDegrees(dict);
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // }
}
Foam::cylindricalCS::~cylindricalCS()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::cylindricalCS::inDegrees() const
{
return inDegrees_;
}
bool& Foam::cylindricalCS::inDegrees()
{
return inDegrees_;
}
Foam::vector Foam::cylindricalCS::localToGlobal Foam::vector Foam::cylindricalCS::localToGlobal
( (
const vector& local, const vector& local,
bool translate bool translate
) const ) const
{ {
scalar theta
(
local.y()*(inDegrees_ ? constant::mathematical::pi/180.0 : 1.0)
);
return coordinateSystem::localToGlobal return coordinateSystem::localToGlobal
( (
vector(local.x()*cos(theta), local.x()*sin(theta), local.z()), toCartesian(local),
translate translate
); );
} }
@ -156,19 +165,22 @@ Foam::tmp<Foam::vectorField> Foam::cylindricalCS::localToGlobal
bool translate bool translate
) const ) const
{ {
scalarField theta const label len = local.size();
(
local.component(vector::Y)
*(inDegrees_ ? constant::mathematical::pi/180.0 : 1.0)
);
auto tresult = tmp<vectorField>::New(len);
auto& result = tresult.ref();
vectorField lc(local.size()); for (label i=0; i<len; ++i)
lc.replace(vector::X, local.component(vector::X)*cos(theta)); {
lc.replace(vector::Y, local.component(vector::X)*sin(theta)); result[i] =
lc.replace(vector::Z, local.component(vector::Z)); coordinateSystem::localToGlobal
(
toCartesian(local[i]),
translate
);
}
return coordinateSystem::localToGlobal(lc, translate); return tresult;
} }
@ -178,21 +190,10 @@ Foam::vector Foam::cylindricalCS::globalToLocal
bool translate bool translate
) const ) const
{ {
const vector lc return fromCartesian
( (
coordinateSystem::globalToLocal(global, translate) coordinateSystem::globalToLocal(global, translate)
); );
return vector
(
sqrt(sqr(lc.x()) + sqr(lc.y())),
atan2
(
lc.y(),
lc.x()
)*(inDegrees_ ? 180.0/constant::mathematical::pi : 1.0),
lc.z()
);
} }
@ -202,31 +203,18 @@ Foam::tmp<Foam::vectorField> Foam::cylindricalCS::globalToLocal
bool translate bool translate
) const ) const
{ {
const vectorField lc const label len = global.size();
tmp<vectorField> tresult
( (
coordinateSystem::globalToLocal(global, translate) coordinateSystem::globalToLocal(global, translate)
); );
auto& result = tresult.ref();
tmp<vectorField> tresult(new vectorField(lc.size())); for (label i=0; i<len; ++i)
vectorField& result = tresult.ref(); {
result[i] = fromCartesian(result[i]);
result.replace }
(
vector::X,
sqrt(sqr(lc.component(vector::X)) + sqr(lc.component(vector::Y)))
);
result.replace
(
vector::Y,
atan2
(
lc.component(vector::Y),
lc.component(vector::X)
)*(inDegrees_ ? 180.0/constant::mathematical::pi : 1.0)
);
result.replace(vector::Z, lc.component(vector::Z));
return tresult; return tresult;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -25,7 +25,7 @@ Class
Foam::cylindricalCS Foam::cylindricalCS
Description Description
Cylindrical coordinate system Cylindrical coordinate system, always in radians.
SourceFiles SourceFiles
cylindricalCS.C cylindricalCS.C
@ -50,12 +50,6 @@ class cylindricalCS
: :
public coordinateSystem public coordinateSystem
{ {
// Private data members
//- Are angles in degrees? (default = true)
bool inDegrees_;
protected: protected:
// Protected Member Functions // Protected Member Functions
@ -92,21 +86,19 @@ public:
// Constructors // Constructors
//- Construct null //- Construct null
cylindricalCS(const bool inDegrees=true); cylindricalCS();
//- Construct copy //- Construct copy
cylindricalCS cylindricalCS
( (
const coordinateSystem&, const coordinateSystem&
const bool inDegrees=true
); );
//- Construct copy with a different name //- Construct copy with a different name
cylindricalCS cylindricalCS
( (
const word& name, const word& name,
const coordinateSystem&, const coordinateSystem&
const bool inDegrees=true
); );
//- Construct from origin and rotation //- Construct from origin and rotation
@ -114,8 +106,7 @@ public:
( (
const word& name, const word& name,
const point& origin, const point& origin,
const coordinateRotation&, const coordinateRotation&
const bool inDegrees=true
); );
//- Construct from origin and 2 axes //- Construct from origin and 2 axes
@ -124,8 +115,7 @@ public:
const word& name, const word& name,
const point& origin, const point& origin,
const vector& axis, const vector& axis,
const vector& dirn, const vector& dirn
const bool inDegrees=true
); );
//- Construct from dictionary and name //- Construct from dictionary and name
@ -136,16 +126,7 @@ public:
//- Destructor //- Destructor
virtual ~cylindricalCS(); virtual ~cylindricalCS() = default;
// Member Functions
//- Are angles in degrees?
bool inDegrees() const;
//- Non-const access to inDegrees
bool& inDegrees();
}; };

View File

@ -269,13 +269,10 @@ Foam::mixerFvMesh::mixerFvMesh
), ),
csPtr_ csPtr_
( (
coordinateSystem::New // Caution: must be a cylindricalCS
( coordinateSystem::New(*this, motionDict_, "coordinateSystem")
"coordinateSystem",
motionDict_.subDict("coordinateSystem")
)
), ),
rpm_(readScalar(motionDict_.lookup("rpm"))), rpm_(motionDict_.get<scalar>("rpm")),
movingPointsMaskPtr_(nullptr) movingPointsMaskPtr_(nullptr)
{ {
addZonesAndModifiers(); addZonesAndModifiers();
@ -294,6 +291,7 @@ Foam::mixerFvMesh::~mixerFvMesh()
deleteDemandDrivenData(movingPointsMaskPtr_); deleteDemandDrivenData(movingPointsMaskPtr_);
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// Return moving points mask. Moving points marked with 1 // Return moving points mask. Moving points marked with 1
@ -310,13 +308,15 @@ const Foam::scalarField& Foam::mixerFvMesh::movingPointsMask() const
bool Foam::mixerFvMesh::update() bool Foam::mixerFvMesh::update()
{ {
// Rotational speed needs to be converted from rpm // The tangential sweep (radians)
const vector theta(0, rpmToRads(rpm_)*time().deltaTValue(), 0);
movePoints movePoints
( (
csPtr_->globalPosition csPtr_->globalPosition
( (
csPtr_->localPosition(points()) csPtr_->localPosition(points())
+ vector(0, rpm_*360.0*time().deltaTValue()/60.0, 0) + theta
*movingPointsMask() *movingPointsMask()
) )
); );
@ -339,7 +339,7 @@ bool Foam::mixerFvMesh::update()
csPtr_->globalPosition csPtr_->globalPosition
( (
csPtr_->localPosition(oldPoints()) csPtr_->localPosition(oldPoints())
+ vector(0, rpm_*360.0*time().deltaTValue()/60.0, 0) + theta
*movingPointsMask() *movingPointsMask()
) )
); );