Function1Types::Sine: Changed parameters to be of type Function1 for greater flexibility

Templated sine function with support for an offset level.

        \f[
            a sin(2 \pi f (t - t_0)) 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
    \endvartable
This commit is contained in:
Henry Weller
2016-02-08 21:42:46 +00:00
parent 82474e0929
commit bc7d6ff57c
3 changed files with 28 additions and 69 deletions

View File

@ -32,10 +32,10 @@ template<class Type>
void Foam::Function1Types::Sine<Type>::read(const dictionary& coeffs) void Foam::Function1Types::Sine<Type>::read(const dictionary& coeffs)
{ {
t0_ = coeffs.lookupOrDefault<scalar>("t0", 0); t0_ = coeffs.lookupOrDefault<scalar>("t0", 0);
amplitude_ = coeffs.lookupOrDefault<scalar>("amplitude", 1); amplitude_ = Function1<scalar>::New("amplitude", coeffs);
frequency_ = readScalar(coeffs.lookup("frequency")); frequency_ = Function1<scalar>::New("frequency", coeffs);
scale_ = pTraits<Type>(coeffs.lookup("scale")); scale_ = Function1<Type>::New("scale", coeffs);
level_ = pTraits<Type>(coeffs.lookup("level")); level_ = Function1<Type>::New("level", coeffs);
} }
@ -58,9 +58,10 @@ Foam::Function1Types::Sine<Type>::Sine(const Sine<Type>& se)
: :
Function1<Type>(se), Function1<Type>(se),
t0_(se.t0_), t0_(se.t0_),
amplitude_(se.amplitude_), amplitude_(se.amplitude_, false),
frequency_(se.frequency_), frequency_(se.frequency_, false),
level_(se.level_) scale_(se.scale_, false),
level_(se.level_, false)
{} {}
@ -77,9 +78,10 @@ template<class Type>
Type Foam::Function1Types::Sine<Type>::value(const scalar t) const Type Foam::Function1Types::Sine<Type>::value(const scalar t) const
{ {
return return
amplitude_*sin(constant::mathematical::twoPi*frequency_*(t - t0_)) amplitude_->value(t)
*scale_ *sin(constant::mathematical::twoPi*frequency_->value(t)*(t - t0_))
+ level_; *scale_->value(t)
+ level_->value(t);
} }
@ -91,7 +93,7 @@ Type Foam::Function1Types::Sine<Type>::integrate
) const ) const
{ {
NotImplemented; NotImplemented;
return level_; return level_->value(t1);
} }

View File

@ -34,12 +34,13 @@ Description
where where
\vartable \vartable
a | Amplitude symbol | Description | Data type
f | Frequency [1/s] a | Amplitude | Function1<scalar>
s | Type scale factor f | Frequency [1/s] | Function1<scalar>
l | Type offset level s | Type scale factor | Function1<Type>
t_0 | Start time [s] l | Type offset level | Function1<Type>
t | Time [s] t_0 | Start time [s] | scalar
t | Time [s] | scalar
\endvartable \endvartable
Example for a scalar: Example for a scalar:
@ -80,16 +81,6 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declaration of friend functions and operators
namespace Function1Types
{
template<class Type> class Sine;
};
template<class Type>
Ostream& operator<<(Ostream&, const Function1Types::Sine<Type>&);
namespace Function1Types namespace Function1Types
{ {
@ -108,16 +99,16 @@ class Sine
scalar t0_; scalar t0_;
//- Scalar amplitude of the sin function //- Scalar amplitude of the sin function
scalar amplitude_; autoPtr<Function1<scalar>> amplitude_;
//- Frequency of the sin function //- Frequency of the sin function
scalar frequency_; autoPtr<Function1<scalar>> frequency_;
//- Scaling factor of the sin function //- Scaling factor of the sin function
Type scale_; autoPtr<Function1<Type>> scale_;
//- Level to which the sin function is added //- Level to which the sin function is added
Type level_; autoPtr<Function1<Type>> level_;
// Private Member Functions // Private Member Functions
@ -167,16 +158,6 @@ public:
//- Integrate between the two time values t1 and t2 //- Integrate between the two time values t1 and t2
Type integrate(const scalar t1, const scalar t2) const; Type integrate(const scalar t1, const scalar t2) const;
// I/O
//- Ostream Operator
friend Ostream& operator<< <Type>
(
Ostream& os,
const Sine<Type>& cnst
);
//- Write in dictionary format //- Write in dictionary format
virtual void writeData(Ostream& os) const; virtual void writeData(Ostream& os) const;
}; };

View File

@ -27,30 +27,6 @@ License
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class Type>
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const Function1Types::Sine<Type>& se
)
{
os << static_cast<const Function1<Type>& >(se)
<< token::SPACE << se.t0_
<< token::SPACE << se.amplitude_
<< token::SPACE << se.frequency_
<< token::SPACE << se.scale_
<< token::SPACE << se.level_;
// Check state of Ostream
os.check
(
"Ostream& operator<<(Ostream&, const Sine<Type>&)"
);
return os;
}
template<class Type> template<class Type>
void Foam::Function1Types::Sine<Type>::writeData(Ostream& os) const void Foam::Function1Types::Sine<Type>::writeData(Ostream& os) const
{ {
@ -59,10 +35,10 @@ void Foam::Function1Types::Sine<Type>::writeData(Ostream& os) const
os << indent << word(this->name() + "Coeffs") << nl; os << indent << word(this->name() + "Coeffs") << nl;
os << indent << token::BEGIN_BLOCK << incrIndent << nl; os << indent << token::BEGIN_BLOCK << incrIndent << nl;
os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl; os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl;
os.writeKeyword("amplitude") << amplitude_ << token::END_STATEMENT << nl; amplitude_->writeData(os);
os.writeKeyword("frequency") << frequency_ << token::END_STATEMENT << nl; frequency_->writeData(os);
os.writeKeyword("scale") << scale_ << token::END_STATEMENT << nl; scale_->writeData(os);
os.writeKeyword("level") << level_ << token::END_STATEMENT << nl; level_->writeData(os);
os << decrIndent << indent << token::END_BLOCK << endl; os << decrIndent << indent << token::END_BLOCK << endl;
} }