DPsim
Integrator.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/Integrator.h>
10 
11 using namespace CPS;
12 using namespace CPS::Signal;
13 
14 Integrator::Integrator(String name, Logger::Level logLevel)
15  : SimSignalComp(name, name, logLevel),
16  mInputRef(mAttributes->createDynamic<Real>("input_ref")),
18  mInputPrev(mAttributes->create<Real>("input_prev")),
19  mStatePrev(mAttributes->create<Real>("state_prev")),
20  mOutputPrev(mAttributes->create<Real>("output_prev")),
21  mInputCurr(mAttributes->create<Real>("input_curr")),
22  mStateCurr(mAttributes->create<Real>("state_curr")),
23  mOutputCurr(mAttributes->create<Real>("output_curr")) {}
24 
25 void Integrator::setParameters(Real timestep) {
26  mTimeStep = timestep;
27 
28  SPDLOG_LOGGER_INFO(mSLog, "Integration step = {}", mTimeStep);
29 }
30 
31 void Integrator::setInitialValues(Real input_init, Real state_init,
32  Real output_init) {
33  **mInputCurr = input_init;
34  **mStateCurr = state_init;
35  **mOutputCurr = output_init;
36 
37  SPDLOG_LOGGER_INFO(mSLog, "Initial values:");
38  SPDLOG_LOGGER_INFO(
39  mSLog, "inputCurrInit = {}, stateCurrInit = {}, outputCurrInit = {}",
41 }
42 
44  AttributeBase::List &prevStepDependencies,
45  AttributeBase::List &attributeDependencies,
46  AttributeBase::List &modifiedAttributes) {
47  prevStepDependencies.push_back(mInputCurr);
48  prevStepDependencies.push_back(mOutputCurr);
49  modifiedAttributes.push_back(mInputPrev);
50  modifiedAttributes.push_back(mOutputPrev);
51 };
52 
53 void Integrator::signalPreStep(Real time, Int timeStepCount) {
54  **mInputPrev = **mInputCurr;
55  **mStatePrev = **mStateCurr;
57 }
58 
60  AttributeBase::List &prevStepDependencies,
61  AttributeBase::List &attributeDependencies,
62  AttributeBase::List &modifiedAttributes) {
63  attributeDependencies.push_back(mInputRef);
64  modifiedAttributes.push_back(mInputCurr);
65  modifiedAttributes.push_back(mOutputCurr);
66 };
67 
68 void Integrator::signalStep(Real time, Int timeStepCount) {
69  **mInputCurr = **mInputRef;
70 
71  SPDLOG_LOGGER_INFO(mSLog, "Time {}:", time);
72  SPDLOG_LOGGER_INFO(
73  mSLog, "Input values: inputCurr = {}, inputPrev = {}, statePrev = {}",
75 
76  **mStateCurr = **mStatePrev + mTimeStep / 2.0 * **mInputCurr +
77  mTimeStep / 2.0 * **mInputPrev;
78  **mOutputCurr = **mStateCurr;
79 
80  SPDLOG_LOGGER_INFO(mSLog, "State values: stateCurr = {}", **mStateCurr);
81  SPDLOG_LOGGER_INFO(mSLog, "Output values: outputCurr = {}:", **mOutputCurr);
82 }
83 
84 Task::List Integrator::getTasks() {
85  return Task::List(
86  {std::make_shared<PreStep>(*this), std::make_shared<Step>(*this)});
87 }
const Attribute< Real >::Ptr mInputRef
This is never explicitely set to reference anything, so the outside code is responsible for setting u...
Definition: Integrator.h:27
const Attribute< Real >::Ptr mOutputCurr
Current Output.
Definition: Integrator.h:40
const Attribute< Real >::Ptr mOutputPrev
Previous Output.
Definition: Integrator.h:38
Real mTimeStep
Integration time step.
Definition: Integrator.h:23
const Attribute< Real >::Ptr mStateCurr
Current State.
Definition: Integrator.h:36
void signalAddStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
add step dependencies
Definition: Integrator.cpp:59
void signalPreStep(Real time, Int timeStepCount)
pre step operations
Definition: Integrator.cpp:53
const Attribute< Real >::Ptr mInputPrev
Previous Input.
Definition: Integrator.h:30
void setInitialValues(Real input_init, Real state_init, Real output_init)
Setter for initial values.
Definition: Integrator.cpp:31
void signalAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
pre step dependencies
Definition: Integrator.cpp:43
const Attribute< Real >::Ptr mStatePrev
Previous State.
Definition: Integrator.h:34
void signalStep(Real time, Int timeStepCount)
step operations
Definition: Integrator.cpp:68
const Attribute< Real >::Ptr mInputCurr
Current Input.
Definition: Integrator.h:32
Logger::Log mSLog
Component logger.