From 8d2d894ae0d4a1b66064be2722e53d60e5eab03c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 16 Nov 2020 11:39:57 +0100 Subject: [PATCH] ENH: support frequency or period for Sine/Square Function1 (#1917) - For slow oscillations it can be more intuitive to specify the period. ENH: separate mark/space for Square - makes it easier to tailor the desired intervals. BUG: incorrect square wave fraction with negative phase shifts ENH: additional cosine Function1 STYLE: avoid code duplication by inheriting Cosine/Square from Sine. --- .../case1/constant/function1Properties | 44 +++++ applications/test/plotFunction1/Make/files | 3 + applications/test/plotFunction1/Make/options | 15 ++ .../test/plotFunction1/Test-plotFunction1.C | 105 +++++++++++ .../functions/Function1/Cosine/Cosine.H | 170 ++++++++++++++++++ .../functions/Function1/Sine/Sine.C | 55 +++--- .../functions/Function1/Sine/Sine.H | 147 +++++++++------ .../functions/Function1/Sine/SineI.H | 96 +++++++++- .../functions/Function1/Square/Square.C | 41 ++--- .../functions/Function1/Square/Square.H | 140 +++++++-------- .../functions/Function1/Square/SquareI.H | 55 ------ .../functions/Function1/makeFunction1s.C | 3 + .../PatchFunction1/makePatchFunction1s.C | 10 +- .../rhoPimpleFoam/laminar/sineWaveDamping/0/p | 3 +- .../system/controlDict | 6 +- .../system/controlDict | 10 +- 16 files changed, 654 insertions(+), 249 deletions(-) create mode 100644 applications/test/plotFunction1/Make/files create mode 100644 applications/test/plotFunction1/Make/options create mode 100644 applications/test/plotFunction1/Test-plotFunction1.C create mode 100644 src/OpenFOAM/primitives/functions/Function1/Cosine/Cosine.H delete mode 100644 src/OpenFOAM/primitives/functions/Function1/Square/SquareI.H diff --git a/applications/test/Function1/case1/constant/function1Properties b/applications/test/Function1/case1/constant/function1Properties index f1148bbf53..9f61763580 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 0000000000..c76c7aa8a8 --- /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 0000000000..b8e72a86c6 --- /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 0000000000..9ddc5cc024 --- /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 . + +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("begin", 0); + const scalar endTime = args.getOrDefault("end", 1); + const scalar incrTime = args.getOrDefault("incr", 0.1); + const scalar timeBase = args.getOrDefault("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> funPtr = + Function1::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 0000000000..63705cce70 --- /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 . + +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 | no | 1 + frequency | Frequency [1/s] | Function1 | or period | + period | Period [s] | Function1 | or frequency | + scale | Scale factor (Type) | Function1 | yes | + level | Offset level (Type) | Function1 | 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 Cosine +: + public Function1Types::Sine +{ +public: + + // Runtime type information + TypeName("cosine"); + + + // Generated Methods + + //- No copy assignment + void operator=(const Cosine&) = delete; + + + // Constructors + + //- Construct from entry name and dictionary + Cosine(const word& entryName, const dictionary& dict) + : + Sine(entryName, dict) + {} + + //- Copy construct + explicit Cosine(const Cosine& rhs) + : + Sine(rhs) + {} + + + //- Destructor + virtual ~Cosine() = default; + + + // Member Functions + + //- Return value for time t + virtual inline Type value(const scalar t) const + { + return Sine::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 38948f029c..20954fa1de 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 -void Foam::Function1Types::Sine::read(const dictionary& coeffs) -{ - t0_ = coeffs.getOrDefault("t0", 0); - amplitude_ = Function1::New("amplitude", coeffs); - frequency_ = Function1::New("frequency", coeffs); - scale_ = Function1::New("scale", coeffs); - level_ = Function1::New("level", coeffs); -} - - template Foam::Function1Types::Sine::Sine ( @@ -48,21 +37,31 @@ Foam::Function1Types::Sine::Sine const dictionary& dict ) : - Function1(entryName, dict) + Function1(entryName, dict), + t0_(dict.getOrDefault("t0", 0)), + amplitude_(Function1::NewIfPresent("amplitude", dict)), + period_(Function1::NewIfPresent("period", dict)), + frequency_(nullptr), + scale_(Function1::New("scale", dict)), + level_(Function1::New("level", dict)) { - read(dict); + if (!period_) + { + frequency_ = Function1::New("frequency", dict); + } } template -Foam::Function1Types::Sine::Sine(const Sine& se) +Foam::Function1Types::Sine::Sine(const Sine& rhs) : - Function1(se), - t0_(se.t0_), - amplitude_(se.amplitude_.clone()), - frequency_(se.frequency_.clone()), - scale_(se.scale_.clone()), - level_(se.level_.clone()) + Function1(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::Sine(const Sine& se) template void Foam::Function1Types::Sine::writeEntries(Ostream& os) const { - os.writeEntry("t0", t0_); - amplitude_->writeData(os); - frequency_->writeData(os); + os.writeEntryIfDifferent("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 746385a04c..92fdb03865 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] + + 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 - f | Frequency [1/s] | Function1 - s | Type scale factor | Function1 - l | Type offset level | Function1 - 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 - sine; - 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 - sine; - 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 | no | 1 + frequency | Frequency [1/s] | Function1 | or period | + period | Period [s] | Function1 | or frequency | + scale | Scale factor (Type) | Function1 | yes | + level | Offset level (Type) | Function1 | 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 @@ -96,28 +119,53 @@ class Sine : public Function1 { - // 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> amplitude_; - //- Frequency of the sin function + //- Period of the function (or specify frequency) + autoPtr> period_; + + //- Frequency of the function (or specify period) autoPtr> frequency_; - //- Scaling factor of the sin function + //- Scaling factor for the function autoPtr> scale_; - //- Level to which the sin function is added + //- Level to add to the scaled function autoPtr> 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& se); + //- Copy construct + explicit Sine(const Sine& 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::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 15533e6cd2..be03d62ddc 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 -inline Type Foam::Function1Types::Sine::value(const scalar t) const +inline Foam::scalar Foam::Function1Types::Sine::cycle +( + const scalar t +) const +{ + // The cycle: (freq * time) or (time / period) + return + ( + frequency_ + ? (t - t0_) * frequency_->value(t) + : (t - t0_) / (period_->value(t) + VSMALL) + ); +} + + +template +inline Foam::scalar +Foam::Function1Types::Sine::cosForm(const scalar t) const { return - amplitude_->value(t) - *sin(constant::mathematical::twoPi*frequency_->value(t)*(t - t0_)) - *scale_->value(t) - + level_->value(t); + ( + cos(constant::mathematical::twoPi * this->cycle(t)) + * (amplitude_ ? amplitude_->value(t) : 1.0) + ); +} + + +template +inline Foam::scalar +Foam::Function1Types::Sine::sinForm(const scalar t) const +{ + return + ( + sin(constant::mathematical::twoPi * this->cycle(t)) + * (amplitude_ ? amplitude_->value(t) : 1.0) + ); +} + + +template +inline Foam::scalar +Foam::Function1Types::Sine::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 +inline Type Foam::Function1Types::Sine::cosValue(const scalar t) const +{ + return + ( + cosForm(t) * scale_->value(t) + level_->value(t) + ); +} + + +template +inline Type Foam::Function1Types::Sine::sinValue(const scalar t) const +{ + return + ( + sinForm(t) * scale_->value(t) + level_->value(t) + ); +} + + +template +inline Type Foam::Function1Types::Sine::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 0016bbb5ec..adc4d91f35 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 -void Foam::Function1Types::Square::read(const dictionary& coeffs) -{ - t0_ = coeffs.getOrDefault("t0", 0); - markSpace_ = coeffs.getOrDefault("markSpace", 1); - amplitude_ = Function1::New("amplitude", coeffs); - frequency_ = Function1::New("frequency", coeffs); - scale_ = Function1::New("scale", coeffs); - level_ = Function1::New("level", coeffs); -} - - template Foam::Function1Types::Square::Square ( @@ -49,22 +37,18 @@ Foam::Function1Types::Square::Square const dictionary& dict ) : - Function1(entryName, dict) -{ - read(dict); -} + Sine(entryName, dict), + mark_(dict.getOrDefaultCompat("mark", {{"markSpace", 2006}}, 1)), + space_(dict.getOrDefault("space", 1)) +{} template -Foam::Function1Types::Square::Square(const Square& se) +Foam::Function1Types::Square::Square(const Square& rhs) : - Function1(se), - t0_(se.t0_), - markSpace_(se.markSpace_), - amplitude_(se.amplitude_.clone()), - frequency_(se.frequency_.clone()), - scale_(se.scale_.clone()), - level_(se.level_.clone()) + Sine(rhs), + mark_(rhs.mark_), + space_(rhs.space_) {} @@ -73,12 +57,9 @@ Foam::Function1Types::Square::Square(const Square& se) template void Foam::Function1Types::Square::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("mark", 1, mark_); + os.writeEntryIfDifferent("space", 1, space_); + Sine::writeEntries(os); } diff --git a/src/OpenFOAM/primitives/functions/Function1/Square/Square.H b/src/OpenFOAM/primitives/functions/Function1/Square/Square.H index a0055da1ac..730dc29cb3 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 | - f | Frequency [1/s] | Function1 | - s | Type scale factor | Function1 | - l | Type offset level | Function1 | - 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 - square; - 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 - square; - 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: square | word | yes | + amplitude | Amplitude | Function1 | no | 1 + frequency | Frequency [1/s] | Function1 | or period | + period | Period [s] | Function1 | or frequency | + scale | Scale factor (Type) | Function1 | yes | + level | Offset level (Type) | Function1 | yes | + t0 | Start time offset | scalar | no | 0 + mark | Positive amount | scalar | no | 1 + space | Negative amount | scalar | no | 1 + \endtable -SourceFiles - Square.C +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 Square : - public Function1 + public Function1Types::Sine { // 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> amplitude_; - - //- Frequency of the square function - autoPtr> frequency_; - - //- Scaling factor of the square function - autoPtr> scale_; - - //- Level to which the square function is added - autoPtr> 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& 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::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 29d9f6ce03..0000000000 --- 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 . - -\*---------------------------------------------------------------------------*/ - -#include "Square.H" - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -template -inline Type Foam::Function1Types::Square::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 b1a2662176..ee4681df2b 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 7740a476ef..b26fc4e509 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 8bcd6726f0..35b6542bec 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 341e9aca2c..a4ba85ec87 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 4a1aa4c7ae..e1f0b1adcc 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);