-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dpsim-villas/models: add co-simulation example
This adds FpgaCosimulation.cpp, an example for a cosimulation with another simulator running a WSCC 9 bus and DPsim running the load connected to bus 5. This also modifies EMT::Ph3::RXLoad to allow it to represent an R-L series load rather than only a parallel load. It also adds EMT::Ph3::ControlledVoltageSource, a voltage source with EMT setpoints for feeding interface data into the DPsim simulation. Signed-off-by: Niklas Eiling <[email protected]>
- Loading branch information
Showing
7 changed files
with
481 additions
and
33 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
dpsim-models/include/dpsim-models/EMT/EMT_Ph3_ControlledVoltageSource.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* Copyright 2017-2021 Institute for Automation of Complex Power Systems, | ||
* EONERC, RWTH Aachen University | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*********************************************************************************/ | ||
#pragma once | ||
|
||
#include <dpsim-models/MNASimPowerComp.h> | ||
#include <dpsim-models/Signal/CosineFMGenerator.h> | ||
#include <dpsim-models/Signal/FrequencyRampGenerator.h> | ||
#include <dpsim-models/Signal/SignalGenerator.h> | ||
#include <dpsim-models/Signal/SineWaveGenerator.h> | ||
#include <dpsim-models/Solver/MNAInterface.h> | ||
|
||
namespace CPS { | ||
namespace EMT { | ||
namespace Ph3 { | ||
/// \brief Controlled Ideal Voltage source model | ||
/// | ||
/// This model uses modified nodal analysis to represent an ideal voltage source. | ||
/// This voltage source derives it's output purely from attributes rather than an internal signal generator. | ||
class ControlledVoltageSource : public MNASimPowerComp<Real>, public SharedFactory<ControlledVoltageSource> { | ||
protected: | ||
// Updates voltage according to reference phasor and frequency | ||
void updateVoltage(Real time); | ||
|
||
public: | ||
const CPS::Attribute<Matrix>::Ptr mVoltageRef; | ||
|
||
/// Defines UID, name and logging level | ||
ControlledVoltageSource(String uid, String name, Logger::Level logLevel = Logger::Level::off); | ||
/// | ||
ControlledVoltageSource(String name, Logger::Level logLevel = Logger::Level::off) : ControlledVoltageSource(name, name, logLevel) {} | ||
|
||
SimPowerComp<Real>::Ptr clone(String name) override; | ||
// #### General #### | ||
/// Initializes component from power flow data | ||
void initializeFromNodesAndTerminals(Real frequency) override; | ||
/// Setter for reference voltage with a real valued generator | ||
/// This will initialize the values of mVoltageRef to match the given parameters | ||
/// However, the attributes can be modified during the simulation to dynamically change the value of the output voltage. | ||
void setParameters(Matrix voltageRef); | ||
|
||
// #### MNA section #### | ||
/// Initializes internal variables of the component | ||
void mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) override; | ||
/// Stamps system matrix | ||
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override; | ||
/// Stamps right side (source) vector | ||
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override; | ||
/// Returns current through the component | ||
void mnaCompUpdateCurrent(const Matrix &leftVector) override; | ||
/// MNA pre step operations | ||
void mnaCompPreStep(Real time, Int timeStepCount) override; | ||
/// MNA post step operations | ||
void mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) override; | ||
/// Add MNA pre step dependencies | ||
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override; | ||
/// Add MNA post step dependencies | ||
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, | ||
Attribute<Matrix>::Ptr &leftVector) override; | ||
}; | ||
} // namespace Ph3 | ||
} // namespace EMT | ||
} // namespace CPS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
dpsim-models/src/EMT/EMT_Ph3_ControlledVoltageSource.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* Copyright 2017-2021 Institute for Automation of Complex Power Systems, | ||
* EONERC, RWTH Aachen University | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*********************************************************************************/ | ||
|
||
#include <dpsim-models/EMT/EMT_Ph3_ControlledVoltageSource.h> | ||
|
||
using namespace CPS; | ||
|
||
EMT::Ph3::ControlledVoltageSource::ControlledVoltageSource(String uid, String name, Logger::Level logLevel) | ||
: MNASimPowerComp<Real>(uid, name, true, true, logLevel), mVoltageRef(mAttributes->createDynamic<Matrix>("V_ref")) // rms-value, phase-to-phase | ||
{ | ||
mPhaseType = PhaseType::ABC; | ||
setVirtualNodeNumber(1); | ||
setTerminalNumber(2); | ||
**mIntfVoltage = Matrix::Zero(3, 1); | ||
**mIntfCurrent = Matrix::Zero(3, 1); | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::setParameters(Matrix voltageRef) { | ||
**mVoltageRef = voltageRef; | ||
|
||
mParametersSet = true; | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::initializeFromNodesAndTerminals(Real frequency) { | ||
SPDLOG_LOGGER_INFO(mSLog, "\n--- Initialization from node voltages ---"); | ||
// TODO: this approach currently overwrites voltage reference set from outside, when not using setParameters | ||
if (!mParametersSet) { | ||
// initialize voltage reference as zeroes | ||
SPDLOG_LOGGER_INFO(mSLog, | ||
"\nReference voltage: {:s}" | ||
"\nTerminal 0 voltage: {:s}" | ||
"\nTerminal 1 voltage: {:s}", | ||
Logger::matrixCompToString(**mVoltageRef), Logger::phasorToString(initialSingleVoltage(0)), Logger::phasorToString(initialSingleVoltage(1))); | ||
} else { | ||
SPDLOG_LOGGER_INFO(mSLog, | ||
"\nInitialization from node voltages omitted (parameter already set)." | ||
"\nReference voltage: {:s}", | ||
Logger::matrixCompToString(**mVoltageRef)); | ||
} | ||
SPDLOG_LOGGER_INFO(mSLog, "\n--- Initialization from node voltages ---"); | ||
mSLog->flush(); | ||
} | ||
|
||
SimPowerComp<Real>::Ptr EMT::Ph3::ControlledVoltageSource::clone(String name) { | ||
auto copy = ControlledVoltageSource::make(name, mLogLevel); | ||
copy->setParameters(**mVoltageRef); | ||
return copy; | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) { updateMatrixNodeIndices(); } | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) { | ||
if (terminalNotGrounded(0)) { | ||
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), -1); | ||
Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), matrixNodeIndex(0, 0), -1); | ||
|
||
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), -1); | ||
Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), matrixNodeIndex(0, 1), -1); | ||
|
||
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), -1); | ||
Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), matrixNodeIndex(0, 2), -1); | ||
} | ||
if (terminalNotGrounded(1)) { | ||
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), 1); | ||
Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), matrixNodeIndex(1, 0), 1); | ||
|
||
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), 1); | ||
Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), matrixNodeIndex(1, 1), 1); | ||
|
||
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), 1); | ||
Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), matrixNodeIndex(1, 2), 1); | ||
} | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) { | ||
Math::setVectorElement(rightVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), (**mIntfVoltage)(0, 0)); | ||
Math::setVectorElement(rightVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), (**mIntfVoltage)(1, 0)); | ||
Math::setVectorElement(rightVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), (**mIntfVoltage)(2, 0)); | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::updateVoltage(Real time) { | ||
**mIntfVoltage = **mVoltageRef; | ||
|
||
SPDLOG_LOGGER_DEBUG(mSLog, "\nUpdate Voltage: {:s}", Logger::matrixToString(**mIntfVoltage)); | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) { | ||
attributeDependencies.push_back(mVoltageRef); | ||
modifiedAttributes.push_back(mRightVector); | ||
modifiedAttributes.push_back(mIntfVoltage); | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompPreStep(Real time, Int timeStepCount) { | ||
updateVoltage(time); | ||
mnaCompApplyRightSideVectorStamp(**mRightVector); | ||
} | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, | ||
Attribute<Matrix>::Ptr &leftVector) { | ||
attributeDependencies.push_back(leftVector); | ||
modifiedAttributes.push_back(mIntfCurrent); | ||
}; | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) { mnaCompUpdateCurrent(**leftVector); } | ||
|
||
void EMT::Ph3::ControlledVoltageSource::mnaCompUpdateCurrent(const Matrix &leftVector) { | ||
(**mIntfCurrent)(0, 0) = Math::realFromVectorElement(leftVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A)); | ||
(**mIntfCurrent)(1, 0) = Math::realFromVectorElement(leftVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B)); | ||
(**mIntfCurrent)(2, 0) = Math::realFromVectorElement(leftVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.