DPsim
FrequencyRampGenerator.cpp
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #include <dpsim-models/Signal/FrequencyRampGenerator.h>
10 
11 using namespace CPS;
12 
14  Real freqStart, Real rocof,
15  Real timeStart,
16  Real duration,
17  bool smoothRamp) {
18  mMagnitude = Math::abs(initialPhasor);
19  mInitialPhase = Math::phase(initialPhasor);
20 
21  mFreqStart = freqStart;
22  mRocof = rocof;
23  mTimeStart = timeStart;
24  mDuration = duration;
25  mFreqEnd = freqStart + rocof * duration;
26  mOldTime = 0.0;
27 
28  mSmoothRamp = smoothRamp;
29 
30  **mSigOut = initialPhasor;
31  **mFreq = freqStart;
32 
33  SPDLOG_LOGGER_INFO(mSLog, "Parameters:");
34  SPDLOG_LOGGER_INFO(mSLog,
35  "\nInitial Phasor={}"
36  "\nStart Frequency={} [Hz]"
37  "\nRoCoF={} [Hz/s]"
38  "\nStart time={} [s]"
39  "\nDuration={} [s]",
40  Logger::phasorToString(initialPhasor), freqStart, rocof,
41  timeStart, duration);
42 }
43 
45  if (mUseAbsoluteCalc) {
46  stepAbsolute(time);
47  return;
48  }
49 
50  Real currPhase;
51  Real currFreq;
52  Real timestep = time - mOldTime;
53  mOldTime = time;
54 
55  currPhase = Math::phase(attributeTyped<Complex>("sigOut")->get());
56 
57  if (time <= mTimeStart) {
58  currFreq = mFreqStart;
59  } else if (time <= mTimeStart + mDuration) {
60  currFreq = mFreqStart + mRocof * (time - mTimeStart);
61  } else {
62  currFreq = mFreqEnd;
63  }
64  currPhase += 2. * PI * currFreq * timestep;
65 
66  **mSigOut = mMagnitude * Complex(cos(currPhase), sin(currPhase));
67  **mFreq = currFreq;
68 }
69 
71  Real currPhase = mInitialPhase + 2 * PI * time * mFreqStart;
72  Real currFreq = mFreqStart;
73 
74  if (time > mTimeStart + mDuration) {
75  currPhase += 2 * PI * mRocof / 2 * pow(mDuration, 2);
76  currPhase +=
77  2 * PI * mRocof * mDuration * (time - (mTimeStart + mDuration));
78  currFreq = mFreqEnd;
79  } else if (time > mTimeStart) {
80  if (mSmoothRamp) { // cos shape
81  // the phase is calculated as 2pi times the integral of the frequency term below
82  currPhase += 2 * PI * (mFreqEnd - mFreqStart) / 2 *
83  ((time - mTimeStart) -
84  2 * mDuration / (2 * PI) *
85  sin(2 * PI / (2 * mDuration) * (time - mTimeStart)));
86  currFreq += (mFreqEnd - mFreqStart) / 2 *
87  (1 - cos(2 * PI / (2 * mDuration) * (time - mTimeStart)));
88  } else { // linear
89  currPhase += 2 * PI * mRocof * pow(time - mTimeStart, 2) / 2;
90  currFreq += mRocof * (time - mTimeStart);
91  }
92  }
93 
94  **mSigOut = mMagnitude * Complex(cos(currPhase), sin(currPhase));
95  **mFreq = currFreq;
96 }
void stepAbsolute(Real time)
implementation of inherited method step to update and return the current signal value
void setParameters(Complex initialPhasor, Real freqStart, Real ramp, Real timeStart, Real duration, bool smoothRamp=true)
set frequency ramp specific parameters
void step(Real time)
implementation of inherited method step to update and return the current signal value
Logger::Log mSLog
Component logger.