diff --git a/applications/test/Function1/case1/constant/function1Properties b/applications/test/Function1/case1/constant/function1Properties index f1148bbf534ddb73472dbbfa88d7895114bb583a..9f6176358071679155594d72e8ba9977d59de967 100644 --- a/applications/test/Function1/case1/constant/function1Properties +++ b/applications/test/Function1/case1/constant/function1Properties @@ -61,4 +61,48 @@ rampf1 } +sine1 +{ + type sine; + frequency 0.1; + scale 1; + level 0; +} + +sine2 +{ + type sine; + period 10; + scale 1; + level 0; +} + +cosine1 +{ + type cosine; + period 10; + scale 1; + level 0; +} + + +sine6 +{ + type sine; + period 6; + t0 -1.5; // want cos + scale 1; + level 0; +} + + +cosine6 +{ + type cosine; + period 6; + scale 1; + level 0; +} + + // ************************************************************************* // diff --git a/applications/test/plotFunction1/Make/files b/applications/test/plotFunction1/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..c76c7aa8a8794aa03b362e16306a4ca614aadaff --- /dev/null +++ b/applications/test/plotFunction1/Make/files @@ -0,0 +1,3 @@ +Test-plotFunction1.C + +EXE = $(FOAM_USER_APPBIN)/Test-plotFunction1 diff --git a/applications/test/plotFunction1/Make/options b/applications/test/plotFunction1/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..b8e72a86c6e63b78d92aae9bb56e4ec20fda46b5 --- /dev/null +++ b/applications/test/plotFunction1/Make/options @@ -0,0 +1,15 @@ +EXE_INC = \ + -DFULLDEBUG -g -O0 \ + -I$(LIB_SRC)/lagrangian/intermediate/lnInclude \ + -I$(LIB_SRC)/thermophysicalModels/radiation/lnInclude \ + -I$(LIB_SRC)/thermophysicalModels/thermophysicalFunctions/lnInclude \ + -I$(LIB_SRC)/finiteVolume/lnInclude \ + -I$(LIB_SRC)/meshTools/lnInclude + +EXE_LIBS = \ + -llagrangianIntermediate \ + -lradiationModels \ + -lregionModels \ + -lfiniteVolume \ + -lmeshTools \ + -lsampling diff --git a/applications/test/plotFunction1/Test-plotFunction1.C b/applications/test/plotFunction1/Test-plotFunction1.C new file mode 100644 index 0000000000000000000000000000000000000000..9ddc5cc0246ddde13496db5cd7ecf31357a0aa55 --- /dev/null +++ b/applications/test/plotFunction1/Test-plotFunction1.C @@ -0,0 +1,105 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +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 <http://www.gnu.org/licenses/>. + +Application + Test-plotFunction1 + +Description + Plot scalar Function1 entries + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "fieldTypes.H" +#include "Function1.H" +#include "PtrList.H" +#include "Fstream.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::setAdvanced("case"); // Hide -case : has no meaning here + + argList::addOption("begin", "scalar", "The start time (default: 0)"); + argList::addOption("end", "scalar", "The end time (default: 1)"); + argList::addOption("incr", "scalar", "The time increment (default: 0.1)"); + argList::addOption("timeBase", "scalar", "The time base (default: 1)"); + argList::addNote + ( + "Read scalar functions from each file and produces" + " time/value output for each" + ); + + argList::noMandatoryArgs(); + argList::addArgument("file1"); + argList::addArgument("..."); + argList::addArgument("fileN"); + + #include "setRootCase.H" + + const scalar begTime = args.getOrDefault<scalar>("begin", 0); + const scalar endTime = args.getOrDefault<scalar>("end", 1); + const scalar incrTime = args.getOrDefault<scalar>("incr", 0.1); + const scalar timeBase = args.getOrDefault<scalar>("timeBase", 1); + + Info().precision(10); + + for (label argi=1; argi < args.size(); ++argi) + { + IFstream is(args[argi]); + + dictionary dict(is); + + for (const entry& dEntry : dict) + { + autoPtr<Function1<scalar>> funPtr = + Function1<scalar>::New(dEntry.keyword(), dict); + + auto& fun = *funPtr; + + InfoErr<< nl; + fun.writeData(InfoErr); + InfoErr<< nl; + + Info<< nl << "# " << fun.type() << nl; + + for (scalar t = begTime; t < endTime; t += incrTime) + { + Info<< t << tab << fun.value(t*timeBase) << nl; + } + Info<< nl; + } + } + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/Cosine/Cosine.H b/src/OpenFOAM/primitives/functions/Function1/Cosine/Cosine.H new file mode 100644 index 0000000000000000000000000000000000000000..63705cce70ad71fc375d1fd0fef8b09158590f5e --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/Cosine/Cosine.H @@ -0,0 +1,170 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +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 <http://www.gnu.org/licenses/>. + +Class + Foam::Function1Types::Cosine + +Description + A templated cosine function, with support for offset etc. + + The wave period can be specified directly + + \f[ + a cos(2 \pi (t - t0) / p)) s + l + \f] + + Or it can be specified by the frequency + + \f[ + a cos(2 \pi f (t - t0)) s + l + \f] + + where + \vartable + Symbol | Description | Units + a | Amplitude | - + f | Frequency | [1/s] + p | Period | [s] + s | Type scale factor | - + l | Type offset level | - + t | Time | [s] + t0 | Start time offset | [s] + \endvartable + + The dictionary specification would typically resemble this: + \verbatim + entry1 + { + type cosine; + frequency 10; + amplitude 0.1; + + // A scalar Function1 + scale 2e-6; + level 2e-6; + } + entry2 + { + type cosine; + frequency 10; + + // A vector Function1 + scale (1 0.1 0); + level (10 1 0); + } + \endverbatim + + where the entries mean: + \table + Property | Description | Type | Reqd | Default + type | Function type: cosine | word | yes | + amplitude | Amplitude | Function1<scalar> | no | 1 + frequency | Frequency [1/s] | Function1<scalar> | or period | + period | Period [s] | Function1<scalar> | or frequency | + scale | Scale factor (Type) | Function1<Type> | yes | + level | Offset level (Type) | Function1<Type> | yes | + t0 | Start time offset | scalar | no | 0 + \endtable + +Note + For slow oscillations it can be more intuitive to specify the period. + +SourceFiles + Cosine.C + +\*---------------------------------------------------------------------------*/ + +#ifndef function1Types_Cosine_H +#define function1Types_Cosine_H + +#include "Sine.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace Function1Types +{ + +/*---------------------------------------------------------------------------*\ + Class Cosine Declaration +\*---------------------------------------------------------------------------*/ + +template<class Type> +class Cosine +: + public Function1Types::Sine<Type> +{ +public: + + // Runtime type information + TypeName("cosine"); + + + // Generated Methods + + //- No copy assignment + void operator=(const Cosine<Type>&) = delete; + + + // Constructors + + //- Construct from entry name and dictionary + Cosine(const word& entryName, const dictionary& dict) + : + Sine<Type>(entryName, dict) + {} + + //- Copy construct + explicit Cosine(const Cosine<Type>& rhs) + : + Sine<Type>(rhs) + {} + + + //- Destructor + virtual ~Cosine() = default; + + + // Member Functions + + //- Return value for time t + virtual inline Type value(const scalar t) const + { + return Sine<Type>::cosValue(t); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Function1Types +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C index 38948f029c0c43e65fd7d450d68a1d8ef98fe947..20954fa1de4039f5d9f026ad140de43f986935e7 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C +++ b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C @@ -30,17 +30,6 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -template<class Type> -void Foam::Function1Types::Sine<Type>::read(const dictionary& coeffs) -{ - t0_ = coeffs.getOrDefault<scalar>("t0", 0); - amplitude_ = Function1<scalar>::New("amplitude", coeffs); - frequency_ = Function1<scalar>::New("frequency", coeffs); - scale_ = Function1<Type>::New("scale", coeffs); - level_ = Function1<Type>::New("level", coeffs); -} - - template<class Type> Foam::Function1Types::Sine<Type>::Sine ( @@ -48,21 +37,31 @@ Foam::Function1Types::Sine<Type>::Sine const dictionary& dict ) : - Function1<Type>(entryName, dict) + Function1<Type>(entryName, dict), + t0_(dict.getOrDefault<scalar>("t0", 0)), + amplitude_(Function1<scalar>::NewIfPresent("amplitude", dict)), + period_(Function1<scalar>::NewIfPresent("period", dict)), + frequency_(nullptr), + scale_(Function1<Type>::New("scale", dict)), + level_(Function1<Type>::New("level", dict)) { - read(dict); + if (!period_) + { + frequency_ = Function1<scalar>::New("frequency", dict); + } } template<class Type> -Foam::Function1Types::Sine<Type>::Sine(const Sine<Type>& se) +Foam::Function1Types::Sine<Type>::Sine(const Sine<Type>& rhs) : - Function1<Type>(se), - t0_(se.t0_), - amplitude_(se.amplitude_.clone()), - frequency_(se.frequency_.clone()), - scale_(se.scale_.clone()), - level_(se.level_.clone()) + Function1<Type>(rhs), + t0_(rhs.t0_), + amplitude_(rhs.amplitude_.clone()), + period_(rhs.period_.clone()), + frequency_(rhs.frequency_.clone()), + scale_(rhs.scale_.clone()), + level_(rhs.level_.clone()) {} @@ -71,9 +70,19 @@ Foam::Function1Types::Sine<Type>::Sine(const Sine<Type>& se) template<class Type> void Foam::Function1Types::Sine<Type>::writeEntries(Ostream& os) const { - os.writeEntry("t0", t0_); - amplitude_->writeData(os); - frequency_->writeData(os); + os.writeEntryIfDifferent<scalar>("t0", 0, t0_); + if (amplitude_) + { + amplitude_->writeData(os); + } + if (period_) + { + period_->writeData(os); + } + if (frequency_) + { + frequency_->writeData(os); + } scale_->writeData(os); level_->writeData(os); } diff --git a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.H b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.H index 746385a04c1aa07ec41aff55c70c2055eba85fcb..92fdb038652f80b9244f9f9f14a54b5338358a45 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.H +++ b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.H @@ -28,55 +28,78 @@ Class Foam::Function1Types::Sine Description - Templated sine function with support for an offset level. + A templated sine function, with support for offset etc. + + The wave period can be specified directly \f[ - a sin(2 \pi f (t - t_0)) s + l + a sin(2 \pi (t - t0) / p)) s + l \f] - where + Or it can be specified by the frequency + + \f[ + a sin(2 \pi f (t - t0)) s + l + \f] + where \vartable - symbol | Description | Data type - a | Amplitude | Function1<scalar> - f | Frequency [1/s] | Function1<scalar> - s | Type scale factor | Function1<Type> - l | Type offset level | Function1<Type> - t_0 | Start time [s] | scalar - t | Time [s] | scalar + Symbol | Description | Units + a | Amplitude | - + f | Frequency | [1/s] + p | Period | [s] + s | Type scale factor | - + l | Type offset level | - + t | Time | [s] + t0 | Start time offset | [s] \endvartable - Example for a scalar: + The dictionary specification would typically resemble this: \verbatim - <entryName> sine; - <entryName>Coeffs - { - frequency 10; - amplitude 0.1; - scale 2e-6; - level 2e-6; - } + entry1 + { + type sine; + frequency 10; + amplitude 0.1; + + // A scalar Function1 + scale 2e-6; + level 2e-6; + } + entry2 + { + type sine; + frequency 10; + + // A vector Function1 + scale (1 0.1 0); + level (10 1 0); + } \endverbatim - Example for a vector: - \verbatim - <entryName> sine; - <entryName>Coeffs - { - frequency 10; - amplitude 1; - scale (1 0.1 0); - level (10 1 0); - } - \endverbatim + where the entries mean: + \table + Property | Description | Type | Reqd | Default + type | Function type: sine | word | yes | + amplitude | Amplitude | Function1<scalar> | no | 1 + frequency | Frequency [1/s] | Function1<scalar> | or period | + period | Period [s] | Function1<scalar> | or frequency | + scale | Scale factor (Type) | Function1<Type> | yes | + level | Offset level (Type) | Function1<Type> | yes | + t0 | Start time offset | scalar | no | 0 + \endtable + +Note + For slow oscillations it can be more intuitive to specify the period. SourceFiles Sine.C + SineI.H \*---------------------------------------------------------------------------*/ -#ifndef Sine_H -#define Sine_H +#ifndef function1Types_Sine_H +#define function1Types_Sine_H #include "Function1.H" @@ -88,7 +111,7 @@ namespace Function1Types { /*---------------------------------------------------------------------------*\ - Class Sine Declaration + Class Sine Declaration \*---------------------------------------------------------------------------*/ template<class Type> @@ -96,28 +119,53 @@ class Sine : public Function1<Type> { - // Private data +protected: - //- Start-time for the sin function + // Protected Data + + //- Start-time for the function scalar t0_; - //- Scalar amplitude of the sin function + //- Scalar amplitude of the function (optional) autoPtr<Function1<scalar>> amplitude_; - //- Frequency of the sin function + //- Period of the function (or specify frequency) + autoPtr<Function1<scalar>> period_; + + //- Frequency of the function (or specify period) autoPtr<Function1<scalar>> frequency_; - //- Scaling factor of the sin function + //- Scaling factor for the function autoPtr<Function1<Type>> scale_; - //- Level to which the sin function is added + //- Level to add to the scaled function autoPtr<Function1<Type>> level_; - // Private Member Functions + // Protected Member Functions + + //- The cycle: (freq * time) or (time / period) + inline scalar cycle(const scalar t) const; + + //- Calculated cos value at time t + inline scalar cosForm(const scalar t) const; + + //- Calculated sin value at time t + inline scalar sinForm(const scalar t) const; + + //- Calculated square value at time t. + // The positive fraction is 0-1 + inline scalar squareForm(const scalar t, const scalar posFrac) const; + + //- Return value for time t, using cos form + inline Type cosValue(const scalar t) const; + + //- Return value for time t, using sin form + inline Type sinValue(const scalar t) const; + + //- Return value for time t, using square form + inline Type squareValue(const scalar t, const scalar posFrac) const; - //- Read the coefficients from the given dictionary - void read(const dictionary& coeffs); public: @@ -134,14 +182,10 @@ public: // Constructors //- Construct from entry name and dictionary - Sine - ( - const word& entryName, - const dictionary& dict - ); + Sine(const word& entryName, const dictionary& dict); - //- Copy constructor - explicit Sine(const Sine<Type>& se); + //- Copy construct + explicit Sine(const Sine<Type>& rhs); //- Destructor @@ -151,7 +195,10 @@ public: // Member Functions //- Return value for time t - virtual inline Type value(const scalar t) const; + virtual inline Type value(const scalar t) const + { + return Sine<Type>::sinValue(t); + } //- Write in dictionary format virtual void writeData(Ostream& os) const; diff --git a/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H b/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H index 15533e6cd2b8f8652d471c2d7202abbd9ce39301..be03d62ddce81c60e3ed83ecc70392860b71fccb 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H +++ b/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,19 +26,100 @@ License \*---------------------------------------------------------------------------*/ -#include "Sine.H" #include "mathematicalConstants.H" -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // template<class Type> -inline Type Foam::Function1Types::Sine<Type>::value(const scalar t) const +inline Foam::scalar Foam::Function1Types::Sine<Type>::cycle +( + const scalar t +) const { + // The cycle: (freq * time) or (time / period) return - amplitude_->value(t) - *sin(constant::mathematical::twoPi*frequency_->value(t)*(t - t0_)) - *scale_->value(t) - + level_->value(t); + ( + frequency_ + ? (t - t0_) * frequency_->value(t) + : (t - t0_) / (period_->value(t) + VSMALL) + ); +} + + +template<class Type> +inline Foam::scalar +Foam::Function1Types::Sine<Type>::cosForm(const scalar t) const +{ + return + ( + cos(constant::mathematical::twoPi * this->cycle(t)) + * (amplitude_ ? amplitude_->value(t) : 1.0) + ); +} + + +template<class Type> +inline Foam::scalar +Foam::Function1Types::Sine<Type>::sinForm(const scalar t) const +{ + return + ( + sin(constant::mathematical::twoPi * this->cycle(t)) + * (amplitude_ ? amplitude_->value(t) : 1.0) + ); +} + + +template<class Type> +inline Foam::scalar +Foam::Function1Types::Sine<Type>::squareForm +( + const scalar t, + const scalar posFrac +) const +{ + const scalar cyc = this->cycle(t); + + return + ( + // Fraction of incomplete cycle + ((cyc - std::floor(cyc)) < posFrac ? 1.0 : -1.0) + * (amplitude_ ? amplitude_->value(t) : 1.0) + ); +} + + +template<class Type> +inline Type Foam::Function1Types::Sine<Type>::cosValue(const scalar t) const +{ + return + ( + cosForm(t) * scale_->value(t) + level_->value(t) + ); +} + + +template<class Type> +inline Type Foam::Function1Types::Sine<Type>::sinValue(const scalar t) const +{ + return + ( + sinForm(t) * scale_->value(t) + level_->value(t) + ); +} + + +template<class Type> +inline Type Foam::Function1Types::Sine<Type>::squareValue +( + const scalar t, + const scalar posFrac +) const +{ + return + ( + squareForm(t, posFrac) * scale_->value(t) + level_->value(t) + ); } diff --git a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C index 0016bbb5ecea9e586c441c0c26acc5be50545dfd..adc4d91f3582fec700dff459afb55184450f4f33 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C +++ b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C @@ -30,18 +30,6 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -template<class Type> -void Foam::Function1Types::Square<Type>::read(const dictionary& coeffs) -{ - t0_ = coeffs.getOrDefault<scalar>("t0", 0); - markSpace_ = coeffs.getOrDefault<scalar>("markSpace", 1); - amplitude_ = Function1<scalar>::New("amplitude", coeffs); - frequency_ = Function1<scalar>::New("frequency", coeffs); - scale_ = Function1<Type>::New("scale", coeffs); - level_ = Function1<Type>::New("level", coeffs); -} - - template<class Type> Foam::Function1Types::Square<Type>::Square ( @@ -49,22 +37,18 @@ Foam::Function1Types::Square<Type>::Square const dictionary& dict ) : - Function1<Type>(entryName, dict) -{ - read(dict); -} + Sine<Type>(entryName, dict), + mark_(dict.getOrDefaultCompat<scalar>("mark", {{"markSpace", 2006}}, 1)), + space_(dict.getOrDefault<scalar>("space", 1)) +{} template<class Type> -Foam::Function1Types::Square<Type>::Square(const Square<Type>& se) +Foam::Function1Types::Square<Type>::Square(const Square<Type>& rhs) : - Function1<Type>(se), - t0_(se.t0_), - markSpace_(se.markSpace_), - amplitude_(se.amplitude_.clone()), - frequency_(se.frequency_.clone()), - scale_(se.scale_.clone()), - level_(se.level_.clone()) + Sine<Type>(rhs), + mark_(rhs.mark_), + space_(rhs.space_) {} @@ -73,12 +57,9 @@ Foam::Function1Types::Square<Type>::Square(const Square<Type>& se) template<class Type> void Foam::Function1Types::Square<Type>::writeEntries(Ostream& os) const { - os.writeEntry("t0", t0_); - os.writeEntry("markSpace", markSpace_); - amplitude_->writeData(os); - frequency_->writeData(os); - scale_->writeData(os); - level_->writeData(os); + os.writeEntryIfDifferent<scalar>("mark", 1, mark_); + os.writeEntryIfDifferent<scalar>("space", 1, space_); + Sine<Type>::writeEntries(os); } diff --git a/src/OpenFOAM/primitives/functions/Function1/Square/Square.H b/src/OpenFOAM/primitives/functions/Function1/Square/Square.H index a0055da1ac2c24efd3afeaf5064d5d0a240daa29..730dc29cb3ff0d2fe48441da1dc59183782d85b0 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Square/Square.H +++ b/src/OpenFOAM/primitives/functions/Function1/Square/Square.H @@ -28,11 +28,19 @@ Class Foam::Function1Types::Square Description - Templated square-wave function with support for an offset level. + A templated square-wave function with support for offset, etc. - \f[ - a square(f (t - t_0)) s + l - \f] + The wave period can be specified directly + + \f[ + a square((t - t0) / p)) s + l + \f] + + Or it can be specified by the frequency + + \f[ + a square(f (t - t0)) s + l + \f] where @@ -40,49 +48,63 @@ Description with a mark/space ratio of \f$ r \f$ \vartable - symbol | Description | Data type | Default - a | Amplitude | Function1<scalar> | - f | Frequency [1/s] | Function1<scalar> | - s | Type scale factor | Function1<Type> | - l | Type offset level | Function1<Type> | - t_0 | Start time [s] | scalar | 0 - r | mark/space ratio | scalar | 1 - t | Time [s] | scalar + symbol | Description | Units + a | Amplitude | - + f | Frequency | [1/s] + p | Period | [s] + s | Type scale factor | - + l | Type offset level | - + t | Time | [s] + t0 | Start time offset | [s] + r | mark/space ratio | - \endvartable - Example for a scalar: + The dictionary specification would typically resemble this: \verbatim - <entryName> square; - <entryName>Coeffs - { - frequency 10; - amplitude 0.1; - scale 2e-6; - level 2e-6; - } + entry1 + { + type square; + frequency 10; + amplitude 0.1; + + // A scalar Function1 + scale 2e-6; + level 2e-6; + } + entry2 + { + type square; + frequency 10; + + // A vector Function1 + scale (1 0.1 0); + level (10 1 0); + } \endverbatim - Example for a vector: - \verbatim - <entryName> square; - <entryName>Coeffs - { - frequency 10; - amplitude 1; - scale (1 0.1 0); - level (10 1 0); - } - \endverbatim - -SourceFiles - Square.C + where the entries mean: + \table + Property | Description | Type | Reqd | Default + type | Function type: square | word | yes | + amplitude | Amplitude | Function1<scalar> | no | 1 + frequency | Frequency [1/s] | Function1<scalar> | or period | + period | Period [s] | Function1<scalar> | or frequency | + scale | Scale factor (Type) | Function1<Type> | yes | + level | Offset level (Type) | Function1<Type> | yes | + t0 | Start time offset | scalar | no | 0 + mark | Positive amount | scalar | no | 1 + space | Negative amount | scalar | no | 1 + \endtable + +Note + For slow oscillations it can be more intuitive to specify the period. \*---------------------------------------------------------------------------*/ -#ifndef Square_H -#define Square_H +#ifndef function1Types_Square_H +#define function1Types_Square_H -#include "Function1.H" +#include "Sine.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -98,34 +120,15 @@ namespace Function1Types template<class Type> class Square : - public Function1<Type> + public Function1Types::Sine<Type> { // Private Data - //- Start-time for the square function - scalar t0_; - - //- Mark/space ratio of the square function - scalar markSpace_; - - //- Scalar amplitude of the square function - autoPtr<Function1<scalar>> amplitude_; - - //- Frequency of the square function - autoPtr<Function1<scalar>> frequency_; - - //- Scaling factor of the square function - autoPtr<Function1<Type>> scale_; - - //- Level to which the square function is added - autoPtr<Function1<Type>> level_; - - - // Private Member Functions - - //- Read the coefficients from the given dictionary - void read(const dictionary& coeffs); + //- Mark/space ratio (square function only) + scalar mark_; + //- Mark/space ratio (square function only) + scalar space_; public: @@ -142,13 +145,9 @@ public: // Constructors //- Construct from entry name and dictionary - Square - ( - const word& entryName, - const dictionary& dict - ); + Square(const word& entryName, const dictionary& dict); - //- Copy constructor + //- Copy construct explicit Square(const Square<Type>& rhs); @@ -159,7 +158,10 @@ public: // Member Functions //- Return value for time t - virtual inline Type value(const scalar t) const; + virtual inline Type value(const scalar t) const + { + return Sine<Type>::squareValue(t, mark_ / (mark_ + space_)); + } //- Write in dictionary format virtual void writeData(Ostream& os) const; @@ -176,8 +178,6 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -#include "SquareI.H" - #ifdef NoRepository #include "Square.C" #endif diff --git a/src/OpenFOAM/primitives/functions/Function1/Square/SquareI.H b/src/OpenFOAM/primitives/functions/Function1/Square/SquareI.H deleted file mode 100644 index 29d9f6ce03e55a4628b6ba408c7d70223a9a339b..0000000000000000000000000000000000000000 --- a/src/OpenFOAM/primitives/functions/Function1/Square/SquareI.H +++ /dev/null @@ -1,55 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | www.openfoam.com - \\/ M anipulation | -------------------------------------------------------------------------------- - Copyright (C) 2017 OpenFOAM Foundation -------------------------------------------------------------------------------- -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 <http://www.gnu.org/licenses/>. - -\*---------------------------------------------------------------------------*/ - -#include "Square.H" - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -template<class Type> -inline Type Foam::Function1Types::Square<Type>::value(const scalar t) const -{ - // Number of waves including fractions - scalar waves = frequency_->value(t)*(t - t0_); - - // Number of complete waves - scalar nWaves; - - // Fraction of last incomplete wave - scalar waveFrac = std::modf(waves, &nWaves); - - // Mark fraction of a wave - scalar markFrac = markSpace_/(1.0 + markSpace_); - - return - amplitude_->value(t) - *(waveFrac < markFrac ? 1 : -1) - *scale_->value(t) - + level_->value(t); -} - - -// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C b/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C index b1a26621764573c77604e6caf764807f81a532f8..ee4681df2b50c379a3f7d8052b10065a1d3ac1a8 100644 --- a/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C +++ b/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -31,6 +32,7 @@ License #include "OneConstant.H" #include "PolynomialEntry.H" #include "Sine.H" +#include "Cosine.H" #include "Square.H" #include "CSV.H" #include "Table.H" @@ -48,6 +50,7 @@ License makeFunction1Type(ZeroConstant, Type); \ makeFunction1Type(OneConstant, Type); \ makeFunction1Type(Polynomial, Type); \ + makeFunction1Type(Cosine, Type); \ makeFunction1Type(Sine, Type); \ makeFunction1Type(Square, Type); \ makeFunction1Type(CSV, Type); \ diff --git a/src/meshTools/PatchFunction1/makePatchFunction1s.C b/src/meshTools/PatchFunction1/makePatchFunction1s.C index 7740a476ef1047a5370f0018a379c9bae6cb1331..b26fc4e509b5136248cf25c06a622b23dd7dd411 100644 --- a/src/meshTools/PatchFunction1/makePatchFunction1s.C +++ b/src/meshTools/PatchFunction1/makePatchFunction1s.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,7 +36,7 @@ License #define makePatchFunction1s(Type) \ makePatchFunction1(Type); \ makePatchFunction1Type(ConstantField, Type); \ - makePatchFunction1Type(MappedFile, Type); \ + makePatchFunction1Type(MappedFile, Type); \ makePatchFunction1Type(UniformValueField, Type); #define addUniformValueFieldFunction1s(F1Type, Type) \ @@ -77,6 +77,12 @@ namespace Foam addUniformValueFieldFunction1s(polynomial, symmTensor); addUniformValueFieldFunction1s(polynomial, tensor); + addUniformValueFieldFunction1s(cosine, scalar); + addUniformValueFieldFunction1s(cosine, vector); + addUniformValueFieldFunction1s(cosine, sphericalTensor); + addUniformValueFieldFunction1s(cosine, symmTensor); + addUniformValueFieldFunction1s(cosine, tensor); + addUniformValueFieldFunction1s(sine, scalar); addUniformValueFieldFunction1s(sine, vector); addUniformValueFieldFunction1s(sine, sphericalTensor); diff --git a/tutorials/compressible/rhoPimpleFoam/laminar/sineWaveDamping/0/p b/tutorials/compressible/rhoPimpleFoam/laminar/sineWaveDamping/0/p index 8bcd6726f0529057e6ee912390227f5c94cbcaab..35b6542bec7b1821c39a2edaad7624af60f4cbb8 100644 --- a/tutorials/compressible/rhoPimpleFoam/laminar/sineWaveDamping/0/p +++ b/tutorials/compressible/rhoPimpleFoam/laminar/sineWaveDamping/0/p @@ -23,11 +23,10 @@ boundaryField { inlet { - type uniformFixedValue; // oscillatingFixedValue; + type uniformFixedValue; uniformValue sine; uniformValueCoeffs { - amplitude constant 1; frequency constant 3000; scale constant 50; level constant 101325; diff --git a/tutorials/multiphase/interIsoFoam/discInReversedVortexFlow/system/controlDict b/tutorials/multiphase/interIsoFoam/discInReversedVortexFlow/system/controlDict index 341e9aca2cf8fa84d5d01d1ff9499f2e7e887510..a4ba85ec874d7cca1524cc317305b59dd11a81b5 100644 --- a/tutorials/multiphase/interIsoFoam/discInReversedVortexFlow/system/controlDict +++ b/tutorials/multiphase/interIsoFoam/discInReversedVortexFlow/system/controlDict @@ -63,12 +63,10 @@ functions libs (fieldFunctionObjects); writeControl writeTime; mode vortex2D; - scale sine; + scale cosine; scaleCoeffs { - amplitude 1; - frequency 0.0625; // = 1/16 - t0 -4; // want cos -> time shift = -(pi/2)/(2 pi f) + period 16 scale 1; level 0; } diff --git a/tutorials/multiphase/interIsoFoam/sphereInReversedVortexFlow/system/controlDict b/tutorials/multiphase/interIsoFoam/sphereInReversedVortexFlow/system/controlDict index 4a1aa4c7ae1c67777c81be20fc079999e4211b8d..e1f0b1adcc5aa699f435302a4f4df81eebf45cce 100644 --- a/tutorials/multiphase/interIsoFoam/sphereInReversedVortexFlow/system/controlDict +++ b/tutorials/multiphase/interIsoFoam/sphereInReversedVortexFlow/system/controlDict @@ -64,14 +64,12 @@ functions libs (fieldFunctionObjects); writeControl writeTime; mode vortex3D; - scale sine; + scale cosine; scaleCoeffs { - amplitude 1; - frequency 0.16666;// = 1/6 - t0 -1.5; // want cos -> time shift = -(pi/2)/(2 pi f) - scale 1; - level 0; + period 6; + scale 1; + level 0; } origin (0 0 0); refDir (1 0 0);