Sine.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2017 OpenFOAM Foundation
9  Copyright (C) 2020-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::Function1Types::Sine
29 
30 Description
31  A templated sine function, with support for offset etc.
32 
33  The wave period can be specified directly
34 
35  \f[
36  a sin(2 \pi (t - t0) / p)) s + l
37  \f]
38 
39  Or it can be specified by the frequency
40 
41  \f[
42  a sin(2 \pi f (t - t0)) s + l
43  \f]
44 
45  where
46  \vartable
47  Symbol | Description | Units
48  a | Amplitude | -
49  f | Frequency | [1/s]
50  p | Period | [s]
51  s | Type scale factor | -
52  l | Type offset level | -
53  t | Time | [s]
54  t0 | Start time offset | [s]
55  \endvartable
56 
57  The dictionary specification would typically resemble this:
58  \verbatim
59  entry1
60  {
61  type sine;
62  frequency 10;
63  amplitude 0.1;
64 
65  // A scalar Function1
66  scale 2e-6;
67  level 2e-6;
68  }
69  entry2
70  {
71  type sine;
72  frequency 10;
73 
74  // A vector Function1
75  scale (1 0.1 0);
76  level (10 1 0);
77  }
78  \endverbatim
79 
80  where the entries mean:
81  \table
82  Property | Description | Type | Reqd | Default
83  type | Function type: sine | word | yes |
84  amplitude | Amplitude | Function1<scalar> | no | 1
85  frequency | Frequency [1/s] | Function1<scalar> | or period |
86  period | Period [s] | Function1<scalar> | or frequency |
87  scale | Scale factor (Type) | Function1<Type> | yes |
88  level | Offset level (Type) | Function1<Type> | yes |
89  t0 | Start time offset | scalar | no | 0
90  \endtable
91 
92 Note
93  For slow oscillations it can be more intuitive to specify the period.
94 
95 SourceFiles
96  Sine.C
97  SineI.H
98 
99 \*---------------------------------------------------------------------------*/
100 
101 #ifndef Function1Types_Sine_H
102 #define Function1Types_Sine_H
103 
104 #include "Function1.H"
105 
106 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
107 
108 namespace Foam
109 {
110 namespace Function1Types
111 {
112 
113 /*---------------------------------------------------------------------------*\
114  Class Sine Declaration
115 \*---------------------------------------------------------------------------*/
116 
117 template<class Type>
118 class Sine
119 :
120  public Function1<Type>
121 {
122 protected:
123 
124  // Protected Data
125 
126  //- Start-time for the function
127  scalar t0_;
128 
129  //- Scalar amplitude of the function (optional)
130  autoPtr<Function1<scalar>> amplitude_;
131 
132  //- Period of the function (or specify frequency)
133  autoPtr<Function1<scalar>> period_;
134 
135  //- Frequency of the function (or specify period)
136  autoPtr<Function1<scalar>> frequency_;
137 
138  //- Scaling factor for the function
139  autoPtr<Function1<Type>> scale_;
140 
141  //- Level to add to the scaled function
142  autoPtr<Function1<Type>> level_;
143 
144 
145  // Protected Member Functions
146 
147  //- The cycle: (freq * time) or (time / period)
148  inline scalar cycle(const scalar t) const;
149 
150  //- Calculated cos value at time t
151  inline scalar cosForm(const scalar t) const;
152 
153  //- Calculated sin value at time t
154  inline scalar sinForm(const scalar t) const;
155 
156  //- Calculated square value at time t.
157  // The positive fraction is 0-1
158  inline scalar squareForm(const scalar t, const scalar posFrac) const;
159 
160  //- Return value for time t, using cos form
161  inline Type cosValue(const scalar t) const;
162 
163  //- Return value for time t, using sin form
164  inline Type sinValue(const scalar t) const;
165 
166  //- Return value for time t, using square form
167  inline Type squareValue(const scalar t, const scalar posFrac) const;
168 
169 
170 public:
171 
172  // Runtime type information
173  TypeName("sine");
174 
175 
176  // Generated Methods
177 
178  //- No copy assignment
179  void operator=(const Sine<Type>&) = delete;
180 
181 
182  // Constructors
183 
184  //- Construct from entry name, dictionary and optional registry
185  Sine
186  (
187  const word& entryName,
188  const dictionary& dict,
189  const objectRegistry* obrPtr = nullptr
190  );
191 
192  //- Copy construct
193  explicit Sine(const Sine<Type>& rhs);
194 
195 
196  //- Destructor
197  virtual ~Sine() = default;
198 
199 
200  // Member Functions
201 
202  //- Convert time
203  virtual void userTimeToTime(const Time& t);
204 
205  //- Return value for time t
206  virtual inline Type value(const scalar t) const
207  {
208  return Sine<Type>::sinValue(t);
209  }
210 
211  //- Write in dictionary format
212  virtual void writeData(Ostream& os) const;
213 
214  //- Write coefficient entries in dictionary format
215  void writeEntries(Ostream& os) const;
216 };
217 
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 } // End namespace Function1Types
222 } // End namespace Foam
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 
226 #include "SineI.H"
227 
228 #ifdef NoRepository
229  #include "Sine.C"
230 #endif
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 #endif
235 
236 // ************************************************************************* //
Foam::Function1Types::Sine::amplitude_
autoPtr< Function1< scalar > > amplitude_
Scalar amplitude of the function (optional)
Definition: Sine.H:217
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::Function1Types::Sine::Sine
Sine(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name, dictionary and optional registry.
Definition: Sine.C:35
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Function1::dictionary
dictionary
Definition: Function1.H:131
Sine.C
Foam::Function1::entryName
const word & entryName
Definition: Function1.H:133
Function1.H
Foam::Function1Types::Sine
A templated sine function, with support for offset etc.
Definition: Sine.H:205
Foam::Function1::dict
const word const dictionary & dict
Definition: Function1.H:134
Foam::Function1Types::Sine::frequency_
autoPtr< Function1< scalar > > frequency_
Frequency of the function (or specify period)
Definition: Sine.H:223
Foam::Function1Types::Sine::writeEntries
void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition: Sine.C:85
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
Foam::Function1Types::Sine::userTimeToTime
virtual void userTimeToTime(const Time &t)
Convert time.
Definition: Sine.C:78
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
SineI.H
Foam::Function1Types::Sine::~Sine
virtual ~Sine()=default
Destructor.
Foam::Function1Types::Sine::scale_
autoPtr< Function1< Type > > scale_
Scaling factor for the function.
Definition: Sine.H:226
Foam::Function1Types::Sine::squareForm
scalar squareForm(const scalar t, const scalar posFrac) const
Calculated square value at time t.
Definition: SineI.H:76
Foam::Function1::obrPtr
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:136
Foam::Function1Types::Sine::level_
autoPtr< Function1< Type > > level_
Level to add to the scaled function.
Definition: Sine.H:229
Foam::Function1Types::Sine::sinValue
Type sinValue(const scalar t) const
Return value for time t, using sin form.
Definition: SineI.H:103
Foam::Function1Types::Sine::t0_
scalar t0_
Start-time for the function.
Definition: Sine.H:214
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Function1Types::Sine::period_
autoPtr< Function1< scalar > > period_
Period of the function (or specify frequency)
Definition: Sine.H:220
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::Function1Types::Sine::value
virtual Type value(const scalar t) const
Return value for time t.
Definition: Sine.H:293
Foam::Function1Types::Sine::cosValue
Type cosValue(const scalar t) const
Return value for time t, using cos form.
Definition: SineI.H:93
Foam::Function1Types::Sine::sinForm
scalar sinForm(const scalar t) const
Calculated sin value at time t.
Definition: SineI.H:63
Foam::Function1Types::Sine::TypeName
TypeName("sine")
Foam::Function1Types::Sine::cycle
scalar cycle(const scalar t) const
The cycle: (freq * time) or (time / period)
Definition: SineI.H:35
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Function1Types::Sine::operator=
void operator=(const Sine< Type > &)=delete
No copy assignment.
Foam::Function1Types::Sine::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Sine.C:106
Foam::Function1Types::Sine::cosForm
scalar cosForm(const scalar t) const
Calculated cos value at time t.
Definition: SineI.H:51
Foam::Function1Types::Sine::squareValue
Type squareValue(const scalar t, const scalar posFrac) const
Return value for time t, using square form.
Definition: SineI.H:114