From 62e7ddccdc930e40f7338c2d2bda170ea85fe124 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 31 May 2017 10:16:19 +0200 Subject: [PATCH] ENH: add (C++11) user-literal for degrees to radians conversion - Programming convenience. Eg, cos(45_deg) vs cos(degToRad(45)) STYLE: conversions marked as constexpr and noexcept --- applications/test/unitConversion/Make/files | 3 ++ applications/test/unitConversion/Make/options | 0 .../test/unitConversion/Test-unitConversion.C | 50 +++++++++++++++++++ .../global/unitConversion/unitConversion.H | 26 +++++++--- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 applications/test/unitConversion/Make/files create mode 100644 applications/test/unitConversion/Make/options create mode 100644 applications/test/unitConversion/Test-unitConversion.C diff --git a/applications/test/unitConversion/Make/files b/applications/test/unitConversion/Make/files new file mode 100644 index 0000000000..0e675c11a2 --- /dev/null +++ b/applications/test/unitConversion/Make/files @@ -0,0 +1,3 @@ +Test-unitConversion.C + +EXE = $(FOAM_USER_APPBIN)/Test-unitConversion diff --git a/applications/test/unitConversion/Make/options b/applications/test/unitConversion/Make/options new file mode 100644 index 0000000000..e69de29bb2 diff --git a/applications/test/unitConversion/Test-unitConversion.C b/applications/test/unitConversion/Test-unitConversion.C new file mode 100644 index 0000000000..869007ec40 --- /dev/null +++ b/applications/test/unitConversion/Test-unitConversion.C @@ -0,0 +1,50 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Application + Test-unitConversion + +\*---------------------------------------------------------------------------*/ + +#include "IOstreams.H" +#include "unitConversion.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + Info<< "30_deg: " << 30_deg << nl; + Info<< "30.0_deg: " << 30.0_deg << nl; + Info<< "3e+1_deg: " << 3e+1_deg << nl; + Info<< "degToRad(30): " << degToRad(30) << nl; + + Info<< "cos(30_deg): " << ::cos(30_deg) << nl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/global/unitConversion/unitConversion.H b/src/OpenFOAM/global/unitConversion/unitConversion.H index 6582d07ffe..10263f2a61 100644 --- a/src/OpenFOAM/global/unitConversion/unitConversion.H +++ b/src/OpenFOAM/global/unitConversion/unitConversion.H @@ -42,29 +42,43 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // //- Conversion from degrees to radians -inline scalar degToRad(const scalar deg) +inline constexpr scalar degToRad(const scalar deg) noexcept { - return (deg*constant::mathematical::pi/180.0); + return (deg*Foam::constant::mathematical::pi/180.0); } //- Conversion from radians to degrees -inline scalar radToDeg(const scalar rad) +inline constexpr scalar radToDeg(const scalar rad) noexcept { - return (rad*180.0/constant::mathematical::pi); + return (rad*180.0/Foam::constant::mathematical::pi); } //- Conversion from atm to Pa -inline scalar atmToPa(const scalar atm) +inline constexpr scalar atmToPa(const scalar atm) noexcept { return (atm*101325.0); } //- Conversion from atm to Pa -inline scalar paToAtm(const scalar pa) +inline constexpr scalar paToAtm(const scalar pa) noexcept { return (pa/101325.0); } + +//- User literal for degrees to radians conversion (integers) +inline constexpr scalar operator "" _deg(unsigned long long int deg) noexcept +{ + return (deg*Foam::constant::mathematical::pi/180.0); +} + +//- User literal for degrees to radians conversion (floats) +inline constexpr scalar operator "" _deg(long double deg) noexcept +{ + return (deg*Foam::constant::mathematical::pi/180.0); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam