ENH: add VectorSpace component fill() method

- as per std::array and FixedList

STYLE: rename Scalar.[CH] -> scalarImpl.[CH] (these are internal files)

- added inclusion guards to protect against bad use
This commit is contained in:
Mark Olesen
2025-01-27 10:25:15 +01:00
parent 39e1eb9dc6
commit a18c15a4cf
11 changed files with 53 additions and 12 deletions

View File

@ -31,8 +31,9 @@ void test(const vector& a, const vector& b, const scalar tolerance)
int main() int main()
{ {
vector a(1.0, 1.0, 1.0); vector a(vector::uniform(1));
vector b(2.0, 2.0, 2.0); vector b;
b.fill(2);
test(a, b, 0.0); test(a, b, 0.0);
test(a, b, VSMALL); test(a, b, VSMALL);
@ -67,5 +68,9 @@ int main()
test(a, b, 1e-3); test(a, b, 1e-3);
test(a, b, 1e-1); test(a, b, 1e-1);
Info<< "\nEnd\n" << nl;
return 0; return 0;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1 @@
#warning File removed - left for old dependency check only

View File

@ -0,0 +1 @@
#warning File removed - left for old dependency check only

View File

@ -31,12 +31,12 @@ License
#include "parsing.H" #include "parsing.H"
#include "IOstreams.H" #include "IOstreams.H"
#include <cstdlib> #include <cstdlib> // For string -> floating point conversions
#include <sstream> #include <sstream>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Scalar.C is used for template-like substitution // scalarImpl.C is used for template-like substitution (but using macros)
#define Scalar doubleScalar #define Scalar doubleScalar
#define ScalarVGREAT doubleScalarVGREAT #define ScalarVGREAT doubleScalarVGREAT
@ -47,7 +47,9 @@ License
// Convert using larger representation to properly capture underflow // Convert using larger representation to properly capture underflow
#define ScalarConvert ::strtold #define ScalarConvert ::strtold
#include "Scalar.C" #define Foam_use_scalarImpl_code
#include "scalarImpl.C"
#undef Foam_use_scalarImpl_code
#undef Scalar #undef Scalar
#undef ScalarVGREAT #undef ScalarVGREAT

View File

@ -144,7 +144,11 @@ inline Scalar func(const Scalar s) \
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "Scalar.H" // scalarImpl.H is used for template-like substitution (but using macros)
#define Foam_use_scalarImpl_header
#include "scalarImpl.H"
#undef Foam_use_scalarImpl_header
#undef Scalar #undef Scalar
#undef ScalarVGREAT #undef ScalarVGREAT

View File

@ -31,11 +31,12 @@ License
#include "parsing.H" #include "parsing.H"
#include "IOstreams.H" #include "IOstreams.H"
#include <cstdlib> // For string -> floating point conversions
#include <sstream> #include <sstream>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Scalar.C is used for template-like substitution // scalarImpl.C is used for template-like substitution (but using macros)
#define Scalar floatScalar #define Scalar floatScalar
#define ScalarVGREAT floatScalarVGREAT #define ScalarVGREAT floatScalarVGREAT
@ -46,7 +47,9 @@ License
// Convert using larger representation to properly capture underflow // Convert using larger representation to properly capture underflow
#define ScalarConvert ::strtod #define ScalarConvert ::strtod
#include "Scalar.C" #define Foam_use_scalarImpl_code
#include "scalarImpl.C"
#undef Foam_use_scalarImpl_code
#undef Scalar #undef Scalar
#undef ScalarVGREAT #undef ScalarVGREAT

View File

@ -157,7 +157,11 @@ inline Scalar func(const Scalar s) \
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "Scalar.H" // scalarImpl.H is used for template-like substitution (but using macros)
#define Foam_use_scalarImpl_header
#include "scalarImpl.H"
#undef Foam_use_scalarImpl_header
#undef Scalar #undef Scalar
#undef ScalarVGREAT #undef ScalarVGREAT

View File

@ -26,6 +26,11 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
// Could also check if 'Scalar' is defined...
#ifndef Foam_use_scalarImpl_code
#error "scalarImpl.C" is only to be included internally
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam

View File

@ -51,10 +51,16 @@ Note
\endcode \endcode
SourceFiles SourceFiles
Scalar.C scalarImpl.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
// Could also check if 'Scalar' is defined...
#ifndef Foam_use_scalarImpl_header
#error "scalarImpl.H" is only to be included internally
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd. Copyright (C) 2019-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -191,6 +191,9 @@ public:
//- Return pointer to the first data element //- Return pointer to the first data element
inline Cmpt* data() noexcept; inline Cmpt* data() noexcept;
//- Assign all components to given value
inline void fill(const Cmpt& s);
//- Return a VectorSpace with all elements = s //- Return a VectorSpace with all elements = s
inline static Form uniform(const Cmpt& s); inline static Form uniform(const Cmpt& s);

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2022 OpenCFD Ltd. Copyright (C) 2019-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -160,6 +160,13 @@ inline void Foam::VectorSpace<Form, Cmpt, Ncmpts>::replace
} }
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline void Foam::VectorSpace<Form, Cmpt, Ncmpts>::fill(const Cmpt& s)
{
VectorSpaceOps<Ncmpts,0>::eqOpS(*this, s, eqOp<Cmpt>());
}
template<class Form, class Cmpt, Foam::direction Ncmpts> template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Form Foam::VectorSpace<Form, Cmpt, Ncmpts>::uniform(const Cmpt& s) inline Form Foam::VectorSpace<Form, Cmpt, Ncmpts>::uniform(const Cmpt& s)
{ {