Skip to content

Commit

Permalink
Addition of EMT::Ph1::Switch component (#312)
Browse files Browse the repository at this point in the history
This branch adds:
- A simple Ph1::Switch component version for the EMT domain.
- A test Notebook for a circuit VS, Switch, RL1 to test and compare the
switch components for the domains EMT_1Ph EMT_3Ph DP_1Ph and SP_1Ph
  • Loading branch information
m-mirz authored Aug 16, 2024
2 parents ce74d8e + 64b5021 commit 832c484
Show file tree
Hide file tree
Showing 6 changed files with 860 additions and 1 deletion.
1 change: 1 addition & 0 deletions dpsim-models/include/dpsim-models/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#include <dpsim-models/EMT/EMT_Ph1_VoltageSource.h>
#include <dpsim-models/EMT/EMT_Ph1_VoltageSourceNorton.h>
#include <dpsim-models/EMT/EMT_Ph1_VoltageSourceRamp.h>
#include <dpsim-models/EMT/EMT_Ph1_Switch.h>

#include <dpsim-models/EMT/EMT_Ph3_AvVoltSourceInverterStateSpace.h>
#include <dpsim-models/EMT/EMT_Ph3_AvVoltageSourceInverterDQ.h>
Expand Down
75 changes: 75 additions & 0 deletions dpsim-models/include/dpsim-models/EMT/EMT_Ph1_Switch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* 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/Base/Base_Ph1_Switch.h>
#include <dpsim-models/Definitions.h>
#include <dpsim-models/Logger.h>
#include <dpsim-models/MNASimPowerComp.h>
#include <dpsim-models/Solver/MNAInterface.h>
#include <dpsim-models/Solver/MNASwitchInterface.h>

namespace CPS {
namespace EMT {
namespace Ph1 {
/// \brief One phase EMT switch
///
/// The switch can be opened and closed.
/// Each state has a specific resistance value.
class Switch : public MNASimPowerComp<Real>,
public Base::Ph1::Switch,
public SharedFactory<Switch>,
public MNASwitchInterface {

public:
/// Defines UID, name, component parameters and logging level
Switch(String uid, String name, Logger::Level loglevel = Logger::Level::off);
/// Defines name, component parameters and logging level
Switch(String name, Logger::Level logLevel = Logger::Level::off)
: Switch(name, name, logLevel) {}

SimPowerComp<Real>::Ptr clone(String name) override;

// #### General ####
/// Initializes component from power flow data
void initializeFromNodesAndTerminals(Real frequency) override;

// #### General MNA section ####
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;
/// Update interface voltage from MNA system result
void mnaCompUpdateVoltage(const Matrix &leftVector) override;
/// Update interface current from MNA system result
void mnaCompUpdateCurrent(const Matrix &leftVector) override;

// #### MNA section for switches ####
/// Check if switch is closed
Bool mnaIsClosed() override;
/// Stamps system matrix considering the defined switch position
void mnaCompApplySwitchSystemMatrixStamp(Bool closed,
SparseMatrixRow &systemMatrix,
Int freqIdx) override;
/// MNA post step operations
void mnaCompPostStep(Real time, Int timeStepCount,
Attribute<Matrix>::Ptr &leftVector) override;

/// Add MNA post step dependencies
void
mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies,
AttributeBase::List &attributeDependencies,
AttributeBase::List &modifiedAttributes,
Attribute<Matrix>::Ptr &leftVector) override;
};
} // namespace Ph1
} // namespace EMT
} // namespace CPS
1 change: 1 addition & 0 deletions dpsim-models/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ list(APPEND MODELS_SOURCES
EMT/EMT_Ph1_VoltageSource.cpp
EMT/EMT_Ph1_VoltageSourceRamp.cpp
EMT/EMT_Ph1_VoltageSourceNorton.cpp
EMT/EMT_Ph1_Switch.cpp

EMT/EMT_Ph3_CurrentSource.cpp
EMT/EMT_Ph3_VoltageSource.cpp
Expand Down
116 changes: 116 additions & 0 deletions dpsim-models/src/EMT/EMT_Ph1_Switch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* 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_Ph1_Switch.h>

using namespace CPS;

EMT::Ph1::Switch::Switch(String uid, String name, Logger::Level logLevel)
: MNASimPowerComp<Real>(uid, name, false, true, logLevel),
Base::Ph1::Switch(mAttributes) {
setTerminalNumber(2);
**mIntfVoltage = Matrix::Zero(1, 1);
**mIntfCurrent = Matrix::Zero(1, 1);
}

SimPowerComp<Real>::Ptr EMT::Ph1::Switch::clone(String name) {
auto copy = Switch::make(name, mLogLevel);
copy->setParameters(**mOpenResistance, **mClosedResistance, **mIsClosed);
return copy;
}

void EMT::Ph1::Switch::initializeFromNodesAndTerminals(Real frequency) {

Real resistance = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;

(**mIntfVoltage)(0, 0) =
RMS3PH_TO_PEAK1PH * (initialSingleVoltage(1) - initialSingleVoltage(0)).real();
(**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / resistance;

SPDLOG_LOGGER_INFO(mSLog,
"\n--- Initialization from powerflow ---"
"\nVoltage across: {:s}"
"\nCurrent: {:s}"
"\nTerminal 0 voltage: {:s}"
"\nTerminal 1 voltage: {:s}"
"\n--- Initialization from powerflow finished ---",
Logger::matrixToString(**mIntfVoltage),
Logger::matrixToString(**mIntfCurrent),
Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(0).real()),
Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(1).real()));
}

void EMT::Ph1::Switch::mnaCompInitialize(Real omega, Real timeStep,
Attribute<Matrix>::Ptr leftVector) {
updateMatrixNodeIndices();
mMnaTasks.push_back(std::make_shared<MnaPostStep>(*this, leftVector));
}

Bool EMT::Ph1::Switch::mnaIsClosed() { return **mIsClosed; }

void EMT::Ph1::Switch::mnaCompApplySystemMatrixStamp(
SparseMatrixRow &systemMatrix) {
Real conductance;

conductance =
(**mIsClosed) ? 1. / (**mClosedResistance) : 1. / (**mOpenResistance);

MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), terminalNotGrounded(0),
terminalNotGrounded(1), mSLog);
}

void EMT::Ph1::Switch::mnaCompApplySwitchSystemMatrixStamp(
Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
Real conductance;

conductance =
(closed) ? 1. / (**mClosedResistance) : 1. / (**mOpenResistance);

MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), terminalNotGrounded(0),
terminalNotGrounded(1), mSLog);
}

void EMT::Ph1::Switch::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) {}

void EMT::Ph1::Switch::mnaCompAddPostStepDependencies(
AttributeBase::List &prevStepDependencies,
AttributeBase::List &attributeDependencies,
AttributeBase::List &modifiedAttributes,
Attribute<Matrix>::Ptr &leftVector) {
attributeDependencies.push_back(leftVector);
modifiedAttributes.push_back(mIntfVoltage);
modifiedAttributes.push_back(mIntfCurrent);
}

void EMT::Ph1::Switch::mnaCompPostStep(Real time, Int timeStepCount,
Attribute<Matrix>::Ptr &leftVector) {
mnaCompUpdateVoltage(**leftVector);
mnaCompUpdateCurrent(**leftVector);
}

void EMT::Ph1::Switch::mnaCompUpdateVoltage(const Matrix &leftVector) {
// Voltage across component is defined as V1 - V0
**mIntfVoltage = Matrix::Zero(1, 1);
if (terminalNotGrounded(1)) {
(**mIntfVoltage)(0, 0) =
Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
}
if (terminalNotGrounded(0)) {
(**mIntfVoltage)(0, 0) =
(**mIntfVoltage)(0, 0) -
Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
}
}

void EMT::Ph1::Switch::mnaCompUpdateCurrent(const Matrix &leftVector) {
(**mIntfCurrent)(0, 0) = (**mIsClosed)
? (**mIntfVoltage)(0, 0) / (**mClosedResistance)
: (**mIntfVoltage)(0, 0) / (**mOpenResistance);
}
10 changes: 9 additions & 1 deletion dpsim/src/pybind/EMTComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ void addEMTPh1Components(py::module_ mEMTPh1) {
.def("connect", &CPS::EMT::Ph1::Inductor::connect)
.def_property("L", createAttributeGetter<CPS::Real>("L"),
createAttributeSetter<CPS::Real>("L"));

py::class_<CPS::EMT::Ph1::Switch, std::shared_ptr<CPS::EMT::Ph1::Switch>, CPS::SimPowerComp<CPS::Real>, CPS::Base::Ph1::Switch>(mEMTPh1, "Switch", py::multiple_inheritance())
.def(py::init<std::string, CPS::Logger::Level>(), "name"_a, "loglevel"_a = CPS::Logger::Level::off)
.def("set_parameters", &CPS::EMT::Ph1::Switch::setParameters, "open_resistance"_a, "closed_resistance"_a, "closed"_a = false) // cppcheck-suppress assignBoolToPointer
.def("open", &CPS::EMT::Ph1::Switch::open)
.def("close", &CPS::EMT::Ph1::Switch::close)
.def("connect", &CPS::EMT::Ph1::Switch::connect);
}

void addEMTPh3Components(py::module_ mEMTPh3) {
Expand All @@ -121,14 +128,15 @@ void addEMTPh3Components(py::module_ mEMTPh3) {
.def_property("f_src", createAttributeGetter<CPS::Real>("f_src"),
createAttributeSetter<CPS::Real>("f_src"));


py::class_<CPS::EMT::Ph3::Resistor, std::shared_ptr<CPS::EMT::Ph3::Resistor>,
CPS::SimPowerComp<CPS::Real>>(mEMTPh3, "Resistor",
py::multiple_inheritance())
.def(py::init<std::string>())
.def(py::init<std::string, CPS::Logger::Level>())
.def("set_parameters", &CPS::EMT::Ph3::Resistor::setParameters, "R"_a)
.def("connect", &CPS::EMT::Ph3::Resistor::connect);
;


py::class_<CPS::EMT::Ph3::Capacitor,
std::shared_ptr<CPS::EMT::Ph3::Capacitor>,
Expand Down
Loading

0 comments on commit 832c484

Please sign in to comment.