ENH: coordSet - added protection for the 'distance' option

This commit is contained in:
Andrew Heather
2017-09-15 11:17:06 +01:00
parent fedf588245
commit 85877cb0f4
2 changed files with 78 additions and 43 deletions

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) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -41,6 +41,21 @@ Foam::coordSet::coordFormatNames_
}; };
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::coordSet::checkDimensions() const
{
if (size() != curveDist_.size())
{
FatalErrorInFunction
<< "Size of points and curve distance must be the same" << nl
<< " points size : " << size()
<< " curve size : " << curveDist_.size()
<< abort(FatalError);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::coordSet::coordSet Foam::coordSet::coordSet
@ -68,49 +83,61 @@ Foam::coordSet::coordSet
name_(name), name_(name),
axis_(coordFormatNames_[axis]), axis_(coordFormatNames_[axis]),
curveDist_(curveDist) curveDist_(curveDist)
{} {
checkDimensions();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::coordSet::hasVectorAxis() const bool Foam::coordSet::hasVectorAxis() const
{ {
return axis_ == XYZ; return axis_ == coordFormat::XYZ;
} }
Foam::scalar Foam::coordSet::scalarCoord Foam::scalar Foam::coordSet::scalarCoord(const label index) const
(
const label index
) const
{ {
const point& p = operator[](index); const point& p = operator[](index);
if (axis_ == X) switch (axis_)
{ {
return p.x(); case coordFormat::X:
} {
else if (axis_ == Y) return p.x();
{ }
return p.y(); case coordFormat::Y:
} {
else if (axis_ == Z) return p.y();
{ }
return p.z(); case coordFormat::Z:
} {
else if (axis_ == DISTANCE) return p.z();
{ }
// Use distance to reference point case coordFormat::DISTANCE:
return curveDist_[index]; {
} // Note: If this has been constructed from the 'name' and 'axis'
else // constructor the curveDist list will not have been set
{
FatalErrorInFunction
<< "Illegal axis specification " << axis_
<< " for sampling line " << name_
<< exit(FatalError);
return 0; if (curveDist_.empty())
{
FatalErrorInFunction
<< "Axis type '" << coordFormatNames_[axis_]
<< "' requested but curve distance has not been set"
<< abort(FatalError);
}
return curveDist_[index];
}
default:
{
FatalErrorInFunction
<< "Illegal axis specification '" << coordFormatNames_[axis_]
<< "' for sampling line " << name_
<< exit(FatalError);
return 0;
}
} }
} }
@ -125,14 +152,14 @@ Foam::point Foam::coordSet::vectorCoord(const label index) const
Foam::Ostream& Foam::coordSet::write(Ostream& os) const Foam::Ostream& Foam::coordSet::write(Ostream& os) const
{ {
os << "name:" << name_ << " axis:" << axis_ os << "name:" << name_ << " axis:" << coordFormatNames_[axis_]
<< endl << nl
<< endl << "\t(coord)" << nl << "\t(coord)"
<< endl; << endl;
forAll(*this, sampleI) for (const point& pt : *this)
{ {
os << '\t' << operator[](sampleI) << endl; os << '\t' << pt << endl;
} }
return os; return os;

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-2012 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -45,7 +45,7 @@ namespace Foam
{ {
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class coordSet Declaration Class coordSet Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
class coordSet class coordSet
@ -58,7 +58,7 @@ public:
// Public data types // Public data types
//- Enumeration defining the output format for coordinates //- Enumeration defining the output format for coordinates
enum coordFormat enum class coordFormat
{ {
XYZ, XYZ,
X, X,
@ -73,6 +73,9 @@ private:
//- String representation of coordFormat enums //- String representation of coordFormat enums
static const Enum<coordFormat> coordFormatNames_; static const Enum<coordFormat> coordFormatNames_;
//- Check for consistent dimensions of points and curve distance
void checkDimensions() const;
protected: protected:
@ -91,11 +94,8 @@ public:
// Constructors // Constructors
//- Construct from components //- Construct from components
coordSet // Note: curveDist will be empty
( coordSet(const word& name, const word& axis);
const word& name,
const word& axis
);
//- Construct from components //- Construct from components
@ -126,6 +126,13 @@ public:
return curveDist_; return curveDist_;
} }
//- Set cumulative distance
void setCurveDist(const scalarList& curveDist)
{
curveDist_ = curveDist;
checkDimensions();
}
//- Is axis specification a vector //- Is axis specification a vector
bool hasVectorAxis() const; bool hasVectorAxis() const;
@ -136,6 +143,7 @@ public:
//- Get point according to axis="xyz" specification //- Get point according to axis="xyz" specification
vector vectorCoord(const label index) const; vector vectorCoord(const label index) const;
//- Write to stream
Ostream& write(Ostream& os) const; Ostream& write(Ostream& os) const;
}; };