From bf044f5c47dadbbf64bf1fe78b7523ed19d4fe6f Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Thu, 27 Jul 2023 15:48:23 +0100 Subject: [PATCH] DimensionedFunction1: Function1 with dimensions This can be used identically to a standard Function1. In addition, it also permits specification of the dimensions. This allows the dimensions to be checked. It also permits unit conversions. For example: { type table; // Dimensions xDimensions [CAD]; // Optional. Argument dimensions/units. // Here, this specifies coordinates are in // crank angle degrees (available if using // engine time). dimensions [mm]; // Optional. Value dimensions/units. // Here, values are in mm. // Tablulated data values ( (0 0) (60 12) // <-- i.e., 12 mm at 60 degrees (180 20) (240 8) (360 0) ); outOfBounds repeat; } This replaces TimeFunction1, as it allows per-function control over whether the function is considered to be one of real-time or user-time. --- .../db/Time/userTime/engineTime/engineTime.C | 4 +- src/OpenFOAM/dimensionSet/dimensionSets.C | 23 ++ src/OpenFOAM/dimensionSet/dimensionSets.H | 4 +- .../functions/Function1/Constant/Constant.C | 25 +- .../functions/Function1/Constant/Constant.H | 7 +- .../functions/Function1/Constant/ConstantI.H | 9 +- .../Dimensioned/DimensionedFunction1.C | 213 ++++++++++++++++++ .../Dimensioned/DimensionedFunction1.H | 193 ++++++++++++++++ .../Dimensioned/DimensionedFunction1I.H | 96 ++++++++ .../functions/Function1/Function1/Function1.C | 11 +- .../functions/Function1/Function1/Function1.H | 127 ++++++----- .../Function1/Function1/Function1New.C | 155 ++++++++----- .../NonUniformTable1/NonUniformTable1.C | 6 +- .../NonUniformTable1/NonUniformTable1.H | 3 +- .../Function1/Polynomial1/Polynomial1.C | 76 ++----- .../Function1/Polynomial1/Polynomial1.H | 18 +- .../functions/Function1/Scale/Scale.C | 23 +- .../functions/Function1/Scale/Scale.H | 5 +- .../functions/Function1/Scale/ScaleI.H | 9 +- .../functions/Function1/Sine/Sine.C | 6 +- .../functions/Function1/Square/Square.C | 6 +- .../functions/Function1/Table/Table.C | 29 ++- .../functions/Function1/Table/Table.H | 19 +- .../Table/TableReader/Csv/CsvTableReader.C | 7 +- .../Table/TableReader/Csv/CsvTableReader.H | 13 +- .../Embedded/EmbeddedTableReader.C | 42 ++-- .../Embedded/EmbeddedTableReader.H | 19 +- .../Table/TableReader/Foam/FoamTableReader.C | 9 +- .../Table/TableReader/Foam/FoamTableReader.H | 11 +- .../TableFileReader/TableFileReader.C | 19 +- .../TableFileReader/TableFileReader.H | 7 +- .../TableReader/TableReader/TableReader.C | 15 +- .../TableReader/TableReader/TableReader.H | 34 ++- .../TableReader/TableReader/TableReaderNew.C | 11 +- .../Table/TableReader/makeTableReaders.C | 13 +- .../Table/TableReader/makeTableReaders.H | 66 ++++++ .../functions/Function1/Uniform/Uniform.C | 41 ---- .../functions/Function1/Uniform/Uniform.H | 18 +- .../exponentialSqrRamp/exponentialSqrRamp.C | 4 +- .../Function1/halfCosineRamp/halfCosineRamp.C | 4 +- .../Function1/linearRamp/linearRamp.C | 4 +- .../functions/Function1/makeFunction1s.C | 22 +- .../functions/Function1/makeFunction1s.H | 83 +++++++ .../Function1/quadraticRamp/quadraticRamp.C | 4 +- .../quarterCosineRamp/quarterCosineRamp.C | 4 +- .../quarterSineRamp/quarterSineRamp.C | 4 +- .../Function1/reverseRamp/reverseRamp.C | 4 +- .../Function1/squarePulse/squarePulse.C | 4 +- .../functions/Function2/Constant/Constant2.C | 24 +- .../functions/Function2/Constant/Constant2.H | 4 +- .../functions/Function2/Function2/Function2.C | 4 +- .../functions/Function2/Function2/Function2.H | 83 +++++-- .../Function2/Function2/Function2New.C | 118 +++++++++- .../Function2/UniformTable2/UniformTable2.C | 8 +- .../functions/Function2/makeFunction2s.C | 30 +-- .../functions/Function2/makeFunction2s.H | 64 ++++++ .../functions/TimeFunction1/TimeFunction1.C | 142 ------------ .../functions/TimeFunction1/TimeFunction1.H | 156 ------------- .../sixDoFMotion/sixDoFMotion.C | 26 +-- .../laminarBL/laminarBL.C | 4 +- .../turbulentBL/turbulentBL.C | 4 +- .../sixDoFAccelerationSource.C | 27 +-- .../ConeInjection/ConeInjection.C | 120 ++++++---- .../ConeInjection/ConeInjection.H | 23 +- .../InjectionModel/InjectionModel.C | 85 ++++--- .../InjectionModel/InjectionModel.H | 9 +- .../MomentumLookupTableInjection.C | 2 +- .../MomentumLookupTableInjection.H | 2 +- .../PatchFlowRateInjection.C | 10 +- .../PatchFlowRateInjection.H | 3 +- .../PatchInjection/PatchInjection.C | 5 +- .../PatchInjection/PatchInjection.H | 12 +- .../ReactingLookupTableInjection.C | 2 +- .../ReactingLookupTableInjection.H | 2 +- .../ReactingMultiphaseLookupTableInjection.C | 2 +- .../ReactingMultiphaseLookupTableInjection.H | 2 +- .../ThermoLookupTableInjection.C | 2 +- .../ThermoLookupTableInjection.H | 2 +- .../APIdiffCoef/APIdiffCoef.C | 2 +- .../NSRDS/NSRDS0/NSRDS0.C | 2 +- .../NSRDS/NSRDS1/NSRDS1.C | 3 +- .../NSRDS/NSRDS14/NSRDS14.C | 2 +- .../NSRDS/NSRDS2/NSRDS2.C | 3 +- .../NSRDS/NSRDS3/NSRDS3.C | 3 +- .../NSRDS/NSRDS4/NSRDS4.C | 3 +- .../NSRDS/NSRDS5/NSRDS5.C | 2 +- .../NSRDS/NSRDS6/NSRDS6.C | 2 +- .../NSRDS/NSRDS7/NSRDS7.C | 2 +- .../integratedNonUniformTable1.C | 2 +- 89 files changed, 1514 insertions(+), 1018 deletions(-) create mode 100644 src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.C create mode 100644 src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.H create mode 100644 src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1I.H create mode 100644 src/OpenFOAM/primitives/functions/Function1/Table/TableReader/makeTableReaders.H delete mode 100644 src/OpenFOAM/primitives/functions/Function1/Uniform/Uniform.C create mode 100644 src/OpenFOAM/primitives/functions/Function1/makeFunction1s.H create mode 100644 src/OpenFOAM/primitives/functions/Function2/makeFunction2s.H delete mode 100644 src/OpenFOAM/primitives/functions/TimeFunction1/TimeFunction1.C delete mode 100644 src/OpenFOAM/primitives/functions/TimeFunction1/TimeFunction1.H diff --git a/src/OpenFOAM/db/Time/userTime/engineTime/engineTime.C b/src/OpenFOAM/db/Time/userTime/engineTime/engineTime.C index 89fd2cf886..b09e5689cb 100644 --- a/src/OpenFOAM/db/Time/userTime/engineTime/engineTime.C +++ b/src/OpenFOAM/db/Time/userTime/engineTime/engineTime.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2021-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -46,6 +46,7 @@ Foam::userTimes::engine::engine(const dictionary& controlDict) rpm_("rpm", dimless/dimTime, 0) { read(controlDict); + addUnit(dimensionedScalar(unit(), dimTime, userTimeToTime(1))); } @@ -87,6 +88,7 @@ Foam::word Foam::userTimes::engine::unit() const bool Foam::userTimes::engine::read(const dictionary& controlDict) { rpm_.read(dict(controlDict)); + addUnit(dimensionedScalar(unit(), dimTime, userTimeToTime(1))); return true; } diff --git a/src/OpenFOAM/dimensionSet/dimensionSets.C b/src/OpenFOAM/dimensionSet/dimensionSets.C index f19d52c3a4..5739c4761b 100644 --- a/src/OpenFOAM/dimensionSet/dimensionSets.C +++ b/src/OpenFOAM/dimensionSet/dimensionSets.C @@ -36,6 +36,7 @@ namespace Foam // the controlDict subDict (v.s. a reference to the subDict for e.g. // dimensionedConstants) dictionary* dimensionSystemsPtr_(nullptr); +HashTable* addedUnitsPtr_(nullptr); HashTable* unitSetPtr_(nullptr); dimensionSets* writeUnitSetPtr_(nullptr); @@ -74,6 +75,20 @@ Foam::dictionary& Foam::dimensionSystems() } +void Foam::addUnit(const dimensionedScalar& unit) +{ + deleteDemandDrivenData(unitSetPtr_); + deleteDemandDrivenData(writeUnitSetPtr_); + + if (!addedUnitsPtr_) + { + addedUnitsPtr_ = new HashTable(); + } + + addedUnitsPtr_->insert(unit.name(), unit); +} + + const Foam::HashTable& Foam::unitSet() { if (!unitSetPtr_) @@ -116,6 +131,14 @@ const Foam::HashTable& Foam::unitSet() } } + if (addedUnitsPtr_) + { + forAllConstIter(HashTable, *addedUnitsPtr_, iter) + { + unitSetPtr_->insert(iter.key(), iter()); + } + } + const wordList writeUnitNames ( unitDict.lookupOrDefault diff --git a/src/OpenFOAM/dimensionSet/dimensionSets.H b/src/OpenFOAM/dimensionSet/dimensionSets.H index 5367a2c275..bef693410e 100644 --- a/src/OpenFOAM/dimensionSet/dimensionSets.H +++ b/src/OpenFOAM/dimensionSet/dimensionSets.H @@ -32,7 +32,6 @@ SourceFiles \*---------------------------------------------------------------------------*/ - #ifndef dimensionSets_H #define dimensionSets_H @@ -132,6 +131,9 @@ public: //- Top level dictionary dictionary& dimensionSystems(); +//- Add a unit +void addUnit(const dimensionedScalar& unit); + //- Set of all dimensions const HashTable& unitSet(); diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C index 0b5c6920eb..fdd282e32b 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -47,27 +47,8 @@ Foam::Function1s::Constant::Constant ) : FieldFunction1>(name), - value_(Zero) -{ - if (name == "value" || !dict.found(name)) - { - dict.lookup("value") >> value_; - } - else - { - Istream& is(dict.lookup(name)); - word entryType(is); - - if (is.eof()) - { - dict.lookup("value") >> value_; - } - else - { - is >> value_; - } - } -} + value_(dict.lookup("value")) +{} template diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H index c37b9681b5..8356558e08 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -62,7 +62,7 @@ class Constant // Private Data //- Constant value - Type value_; + const Type value_; public: @@ -106,6 +106,9 @@ public: //- Integrate between two values virtual inline Type integral(const scalar x1, const scalar x2) const; + //- Is this function guaranteed to be constant? + virtual inline bool constant() const; + //- Write in dictionary format virtual void write(Ostream& os) const; diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H b/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H index 65b8a849e1..4aba331625 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2017-2020 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2017-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -45,4 +45,11 @@ inline Type Foam::Function1s::Constant::integral } +template +inline bool Foam::Function1s::Constant::constant() const +{ + return true; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.C b/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.C new file mode 100644 index 0000000000..c3b60b6b33 --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.C @@ -0,0 +1,213 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2023 OpenFOAM Foundation + \\/ 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 . + +\*---------------------------------------------------------------------------*/ + +#include "DimensionedFunction1.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template +void Foam::Function1s::Dimensioned::read +( + const word& name, + const dictionary& dict +) +{ + // Read units from the given stream + auto readUnits = [] + ( + Istream& is, + const word& prefix, + dimensionedScalar& units + ) + { + units.name() = + prefix + (prefix.empty() ? 'u' : 'U') + "nits"; + + dimensionSet d(dimless); + d.read(is, units.value()); + + if (d != units.dimensions()) + { + FatalIOErrorInFunction(is) + << "The " << prefix << (prefix.empty() ? "" : "-") + << "dimensions " << d << " provided do not match the required " + << "dimensions " << units.dimensions() + << exit(FatalIOError); + } + }; + + // Read units if present in the given dictionary + auto lookupUnitsIfPresent = [&readUnits] + ( + const dictionary& dict, + const word& prefix, + dimensionedScalar& units + ) + { + const word unitsKey = + prefix + (prefix.empty() ? 'u' : 'U') + "nits"; + const word dimsKey = + prefix + (prefix.empty() ? 'd' : 'D') + "imensions"; + + const bool haveUnits = dict.found(unitsKey); + const bool haveDims = dict.found(dimsKey); + + if (haveUnits && haveDims) + { + FatalIOErrorInFunction(dict) + << "Both " << unitsKey << " and " << dimsKey + << " are specified. Only one is permitted." + << exit(FatalError); + } + + if (haveUnits) + { + units = dimensionedScalar(unitsKey, units.dimensions(), dict); + } + + if (haveDims) + { + readUnits(dict.lookup(dimsKey), prefix, units); + } + }; + + // If the function is a dictionary (preferred) then read straightforwardly + if (dict.isDict(name)) + { + const dictionary& coeffsDict(dict.subDict(name)); + + lookupUnitsIfPresent(coeffsDict, "x", xUnits_); + lookupUnitsIfPresent(coeffsDict, "", units_); + + value_.reset(Function1::New(name, dict).ptr()); + + return; + } + + // Find the entry + Istream& is(dict.lookup(name)); + + // Peek at the first token + token firstToken(is); + is.putBack(firstToken); + + // Read the type, or assume constant + const word Function1Type = + firstToken.isWord() ? word(is) : Constant::typeName; + + // If the entry is not a type followed by a end statement then + // construct the function from the stream + if (!firstToken.isWord() || !is.eof()) + { + // Peek at the next token + token nextToken(is); + is.putBack(nextToken); + + // Read dimensions if they are provided + if (nextToken == token::BEGIN_SQR) + { + readUnits(is, "", units_); + } + + // Construct from a stream + value_.reset(Function1::New(name, Function1Type, is).ptr()); + + return; + } + + // Otherwise, construct from the current or coeffs dictionary + const dictionary& coeffsDict = + dict.optionalSubDict(name + "Coeffs"); + + lookupUnitsIfPresent(coeffsDict, "x", xUnits_); + lookupUnitsIfPresent(coeffsDict, "", units_); + + value_.reset(Function1::New(name, dict).ptr()); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +Foam::Function1s::Dimensioned::Dimensioned +( + const word& name, + const dimensionSet& xDimensions, + const dimensionSet& dimensions, + const dictionary& dict +) +: + FieldFunction1>(name), + xUnits_(word::null, xDimensions, scalar(1)), + units_(word::null, dimensions, scalar(1)), + value_(nullptr) +{ + read(name, dict); +} + + +template +Foam::Function1s::Dimensioned::Dimensioned(const Dimensioned& df1) +: + FieldFunction1>(df1), + xUnits_(df1.xUnits_), + units_(df1.units_), + value_(df1.value_, false) +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +template +Foam::Function1s::Dimensioned::~Dimensioned() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +void Foam::Function1s::Dimensioned::write(Ostream& os) const +{ + if (xUnits_.name() != word::null) + { + writeKeyword(os, xUnits_.name()); + writeEntry(os, xUnits_.dimensions()); + os << token::SPACE; + writeEntry(os, xUnits_.value()); + os << token::END_STATEMENT << endl; + } + if (units_.name() != word::null) + { + writeKeyword(os, units_.name()); + writeEntry(os, units_.dimensions()); + os << token::SPACE; + writeEntry(os, units_.value()); + os << token::END_STATEMENT << endl; + } + value_().write(os); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.H b/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.H new file mode 100644 index 0000000000..3d4b40b920 --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1.H @@ -0,0 +1,193 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2023 OpenFOAM Foundation + \\/ 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 . + +Class + Foam::Function1s::Dimensioned + +Description + Function1 with dimensions. + + The dimensions are specified in the constructing code, in much the same way + as for a dimensioned constant, except that both argument (x) and value + dimensions must be specified. + + This can be used identically to a standard Function1. In addition, it also + permits specification of the dimensions. This allows the dimensions to be + checked. It also permits unit conversions. + +Usage + + { + type table; + + // Dimensions + xDimensions [CAD]; // Optional. Argument dimensions/units. + // Here, this specifies coordinates are in + // crank angle degrees (available if using + // engine time). + dimensions [mm]; // Optional. Value dimensions/units. + // Here, values are in mm. + + // Tabulated data + values + ( + (0 0) + (60 12) // <-- i.e., 12 mm at 60 degrees + (180 20) + (240 8) + (360 0) + ); + outOfBounds repeat; + } + +SourceFiles + DimensionedFunction1.C + DimensionedFunction1I.H + +\*---------------------------------------------------------------------------*/ + +#ifndef DimensionedFunction1_H +#define DimensionedFunction1_H + +#include "Function1.H" +#include "dimensionedType.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace Function1s +{ + +/*---------------------------------------------------------------------------*\ + Class Dimensioned Declaration +\*---------------------------------------------------------------------------*/ + +template +class Dimensioned +: + public FieldFunction1> +{ + // Private Data + + //- Argument dimensions + dimensionedScalar xUnits_; + + //- Value dimensions + dimensionedScalar units_; + + //- Value function + autoPtr> value_; + + + // Private Member Functions + + //- Read the coefficients from the given dictionary. Note that this is + // the outer dictionary, not the coefficients dictionary, as is the + // case for most/all other Function1-s. + void read(const word& name, const dictionary& dict); + + +public: + + // Runtime type information + virtual const word& type() const + { + return value_->type(); + } + + + // Constructors + + //- Construct from name and dictionary. Note that this is the outer + // dictionary, not the coefficients dictionary, as is the case for + // most/all other Function1-s. + Dimensioned + ( + const word& name, + const dimensionSet& xDimensions, + const dimensionSet& dimensions, + const dictionary& dict + ); + + //- Copy constructor + Dimensioned(const Dimensioned& se); + + + //- Destructor + virtual ~Dimensioned(); + + + // Member Functions + + //- Return a value + virtual inline Type value(const scalar x) const; + + //- Integrate between two values + virtual inline Type integral(const scalar x1, const scalar x2) const; + + //- Return a dimensioned value + virtual inline dimensioned value + ( + const dimensionedScalar& x + ) const; + + //- Integrate between two dimensioned values + virtual inline dimensioned integral + ( + const dimensionedScalar& x1, + const dimensionedScalar& x2 + ) const; + + //- Is this function guaranteed to be constant? + virtual inline bool constant() const; + + //- Write data to dictionary stream + virtual void write(Ostream& os) const; + + + // Member Operators + + //- Disallow default bitwise assignment + void operator=(const Dimensioned&) = delete; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Function1s +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "DimensionedFunction1I.H" + +#ifdef NoRepository + #include "DimensionedFunction1.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1I.H b/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1I.H new file mode 100644 index 0000000000..10cf0b7771 --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/Dimensioned/DimensionedFunction1I.H @@ -0,0 +1,96 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2023 OpenFOAM Foundation + \\/ 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 . + +\*---------------------------------------------------------------------------*/ + +#include "DimensionedFunction1.H" +#include "dimensionedType.H" + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +inline Type Foam::Function1s::Dimensioned::value(const scalar x) const +{ + return + value_->value(x/xUnits_.value()) + *units_.value(); +} + + +template +inline Type Foam::Function1s::Dimensioned::integral +( + const scalar x1, + const scalar x2 +) const +{ + return + value_->integral(x1/xUnits_.value(), x2/xUnits_.value()) + *xUnits_.value()*units_.value(); +} + + +template +inline Foam::dimensioned Foam::Function1s::Dimensioned::value +( + const dimensionedScalar& x +) const +{ + x.dimensions() = xUnits_.dimensions(); + + return + dimensioned + ( + units_.dimensions(), + value(x.value()) + ); +} + + +template +inline Foam::dimensioned Foam::Function1s::Dimensioned::integral +( + const dimensionedScalar& x1, + const dimensionedScalar& x2 +) const +{ + x1.dimensions() = xUnits_.dimensions(); + x2.dimensions() = xUnits_.dimensions(); + + return + dimensioned + ( + units_.dimensions()*xUnits_.dimensions(), + integral(x1.value(), x2.value()) + ); +} + + +template +inline bool Foam::Function1s::Dimensioned::constant() const +{ + return value_->constant(); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C index 6d45bffc38..07f52f2524 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C @@ -36,10 +36,10 @@ Foam::Function1::Function1(const word& name) template -Foam::Function1::Function1(const Function1& de) +Foam::Function1::Function1(const Function1& f1) : tmp>::refCount(), - name_(de.name_) + name_(f1.name_) {} @@ -85,6 +85,13 @@ const Foam::word& Foam::Function1::name() const } +template +bool Foam::Function1::constant() const +{ + return false; +} + + template Foam::tmp> Foam::FieldFunction1::value ( diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H index 206f089af1..dced788b14 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H @@ -66,7 +66,7 @@ class Function1 protected: - // Protected data + // Protected Data //- Name of entry const word name_; @@ -76,21 +76,36 @@ public: typedef Type returnType; + //- Runtime type information TypeName("Function1"); - //- Declare runtime constructor selection table - declareRunTimeSelectionTable - ( - autoPtr, - Function1, - dictionary, + + // Declare runtime constructor selection tables + + declareRunTimeSelectionTable ( - const word& name, - const dictionary& dict - ), - (name, dict) - ); + autoPtr, + Function1, + dictionary, + ( + const word& name, + const dictionary& dict + ), + (name, dict) + ); + + declareRunTimeSelectionTable + ( + autoPtr, + Function1, + Istream, + ( + const word& name, + Istream& is + ), + (name, is) + ); // Constructors @@ -105,12 +120,22 @@ public: virtual tmp> clone() const = 0; - //- Selector - static autoPtr> New - ( - const word& name, - const dictionary& dict - ); + // Selectors + + //- Select from dictionary + static autoPtr> New + ( + const word& name, + const dictionary& dict + ); + + //- Select from Istream + static autoPtr> New + ( + const word& name, + const word& Function1Type, + Istream& is + ); //- Destructor @@ -138,6 +163,9 @@ public: const scalarField& x2 ) const = 0; + //- Is this function guaranteed to be constant? + virtual bool constant() const; + //- Write data to dictionary stream virtual void write(Ostream& os) const = 0; @@ -150,12 +178,12 @@ public: // IOstream Operators - //- Ostream Operator - friend Ostream& operator<< - ( - Ostream& os, - const Function1& func - ); + //- Ostream Operator + friend Ostream& operator<< + ( + Ostream& os, + const Function1& func + ); }; @@ -214,13 +242,14 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -#define makeFunction1(Type) \ +#define defineFunction1(Type) \ \ defineNamedTemplateTypeNameAndDebug(Function1, 0); \ - defineTemplateRunTimeSelectionTable(Function1, dictionary); + defineTemplateRunTimeSelectionTable(Function1, dictionary); \ + defineTemplateRunTimeSelectionTable(Function1, Istream); -#define makeFunction1Type(SS, Type) \ +#define addFunction1(SS, Type) \ \ defineNamedTemplateTypeNameAndDebug(SS, 0); \ typedef Function1 Type##Function1; \ @@ -233,7 +262,18 @@ public: ) -#define makeNamedFunction1Type(SS, Type, Name) \ +#define addStreamConstructableFunction1(SS, Type) \ + \ + addFunction1(SS, Type); \ + addToRunTimeSelectionTable \ + ( \ + Type##Function1, \ + Type##SS##Function1, \ + Istream \ + ) + + +#define addNamedFunction1(SS, Type, Name) \ \ typedef Function1 Type##Function1; \ typedef SS Type##SS##Function1; \ @@ -246,42 +286,13 @@ public: ) -#define makeScalarFunction1(SS) \ +#define addScalarFunction1(SS) \ \ defineTypeNameAndDebug(SS, 0); \ typedef Function1 scalarFunction1; \ addToRunTimeSelectionTable(scalarFunction1, SS, dictionary) -#define makeFunction1s(Type, nullArg) \ - \ - template<> \ - const char* const Foam::Tuple2::typeName \ - ( \ - "Tuple2" \ - ); \ - \ - makeFunction1(Type); \ - \ - namespace Function1s \ - { \ - makeFunction1Type(None, Type); \ - makeFunction1Type(Constant, Type); \ - makeFunction1Type(Uniform, Type); \ - makeFunction1Type(ZeroConstant, Type); \ - makeFunction1Type(OneConstant, Type); \ - makeFunction1Type(Polynomial, Type); \ - makeFunction1Type(Sine, Type); \ - makeFunction1Type(Square, Type); \ - makeFunction1Type(Table, Type); \ - makeFunction1Type(UniformTable, Type); \ - makeFunction1Type(NonUniformTable, Type); \ - makeNamedFunction1Type(Table, Type, tableFile); \ - makeFunction1Type(Scale, Type); \ - makeFunction1Type(Coded, Type); \ - } - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #ifdef NoRepository diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C index cf45f3a853..bea72951ae 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -25,7 +25,7 @@ License #include "Constant.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // template Foam::autoPtr> Foam::Function1::New @@ -34,6 +34,7 @@ Foam::autoPtr> Foam::Function1::New const dictionary& dict ) { + // If the function is a dictionary (preferred) then read straightforwardly if (dict.isDict(name)) { const dictionary& coeffsDict(dict.subDict(name)); @@ -56,62 +57,104 @@ Foam::autoPtr> Foam::Function1::New return cstrIter()(name, coeffsDict); } - else + + // Find the entry + Istream& is(dict.lookup(name)); + + // Peek at the first token + token firstToken(is); + is.putBack(firstToken); + + // Read the type, or assume constant + const word Function1Type = + firstToken.isWord() ? word(is) : Function1s::Constant::typeName; + + // If the entry is not a type followed by a end statement then + // construct the function from the stream + if (!firstToken.isWord() || !is.eof()) { - Istream& is(dict.lookup(name, false)); - - token firstToken(is); - word Function1Type; - - if (!firstToken.isWord()) - { - is.putBack(firstToken); - return autoPtr> - ( - new Function1s::Constant(name, is) - ); - } - else - { - Function1Type = firstToken.wordToken(); - } - - typename dictionaryConstructorTable::iterator cstrIter = - dictionaryConstructorTablePtr_->find(Function1Type); - - if (cstrIter == dictionaryConstructorTablePtr_->end()) - { - FatalErrorInFunction - << "Unknown Function1 type " - << Function1Type << " for Function1 " - << name << nl << nl - << "Valid Function1 types are:" << nl - << dictionaryConstructorTablePtr_->sortedToc() << nl - << exit(FatalError); - } - - autoPtr> funcPtr - ( - cstrIter() - ( - name, - dict.found(name + "Coeffs") - ? dict.subDict(name + "Coeffs") - : dict - ) - ); - - if (dict.found(name + "Coeffs")) - { - IOWarningInFunction(dict) - << "Using deprecated " - << (name + "Coeffs") << " sub-dictionary."<< nl - << " Please use the simpler form" << endl; - funcPtr->write(Info); - } - - return funcPtr; + return New(name, Function1Type, is); } + + // Otherwise, construct from the current or coeffs dictionary + typename dictionaryConstructorTable::iterator dictCstrIter = + dictionaryConstructorTablePtr_->find(Function1Type); + + if (dictCstrIter == dictionaryConstructorTablePtr_->end()) + { + FatalErrorInFunction + << "Unknown Function1 type " + << Function1Type << " for Function1 " + << name << nl << nl + << "Valid Function1 types are:" << nl + << dictionaryConstructorTablePtr_->sortedToc() << nl + << exit(FatalError); + } + + const bool haveCoeffsDict = dict.found(name + "Coeffs"); + + autoPtr> funcPtr + ( + dictCstrIter() + ( + name, + haveCoeffsDict ? dict.subDict(name + "Coeffs") : dict + ) + ); + + if (haveCoeffsDict) + { + IOWarningInFunction(dict) + << "Using deprecated " + << (name + "Coeffs") << " sub-dictionary."<< nl + << " Please use the simpler form" << endl; + funcPtr->write(Info); + } + + return funcPtr; +} + + +template +Foam::autoPtr> Foam::Function1::New +( + const word& name, + const word& Function1Type, + Istream& is +) +{ + typename dictionaryConstructorTable::iterator dictCstrIter = + dictionaryConstructorTablePtr_->find(Function1Type); + const bool haveDictCstrIter = + dictCstrIter != dictionaryConstructorTablePtr_->end(); + + typename IstreamConstructorTable::iterator isCstrIter = + IstreamConstructorTablePtr_->find(Function1Type); + const bool haveIstreamCstrIter = + isCstrIter != IstreamConstructorTablePtr_->end(); + + if (!haveDictCstrIter && !haveIstreamCstrIter) + { + FatalErrorInFunction + << "Unknown Function1 type " + << Function1Type << " for Function1 " + << name << nl << nl + << "Valid Function1 types are:" << nl + << dictionaryConstructorTablePtr_->sortedToc() << nl + << exit(FatalError); + } + + if (!haveIstreamCstrIter) + { + FatalErrorInFunction + << "Function1 type " + << Function1Type << " for Function1 " + << name << " cannot be specified inline" << nl << nl + << "Make " << name << " a sub-dictionary" + << exit(FatalError); + } + + return isCstrIter()(name, is); } diff --git a/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.C b/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.C index fceaa2bd3e..2f0fd974bd 100644 --- a/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.C +++ b/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2020 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2020-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -39,8 +39,10 @@ Foam::Function1s::NonUniformTable::NonUniformTable high_(-great), values_(), delta_(great), - reader_(TableReader::New(name, dict, this->values_)) + reader_(TableReader::New(name, dict)) { + values_ = reader_->read(dict); + if (values_.size() < 2) { FatalIOErrorInFunction(dict) diff --git a/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.H b/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.H index 2285b40eda..464d50834b 100644 --- a/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.H +++ b/src/OpenFOAM/primitives/functions/Function1/NonUniformTable1/NonUniformTable1.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2020-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2020-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -92,6 +92,7 @@ class NonUniformTable //- Increment derived from low_, high_ and values_.size() scalar delta_; + //- The jump table List