From 10a641d19c9851700f277f19478a392b5c08fa69 Mon Sep 17 00:00:00 2001 From: Martin Moraga Date: Fri, 3 Mar 2023 16:02:20 +0100 Subject: [PATCH] added comp. ResIndSeries SP domain Signed-off-by: Martin Moraga --- .../include/dpsim-models/Components.h | 1 + .../dpsim-models/DP/DP_Ph1_ResIndSeries.h | 2 +- .../dpsim-models/SP/SP_Ph1_ResIndSeries.h | 61 +++++ .../include/dpsim-models/SP/SP_Ph1_Resistor.h | 14 +- dpsim-models/src/CMakeLists.txt | 1 + dpsim-models/src/DP/DP_Ph1_ResIndSeries.cpp | 2 +- dpsim-models/src/SP/SP_Ph1_Capacitor.cpp | 4 +- dpsim-models/src/SP/SP_Ph1_Inductor.cpp | 5 +- dpsim-models/src/SP/SP_Ph1_ResIndSeries.cpp | 126 +++++++++ dpsim-models/src/SP/SP_Ph1_Resistor.cpp | 3 + dpsim-models/src/SP/SP_Ph1_VoltageSource.cpp | 2 + dpsim/src/pybind/SPComponents.cpp | 6 + .../Circuits/Validation_RL_Element.ipynb | 258 ++++++++++++++++-- 13 files changed, 456 insertions(+), 29 deletions(-) create mode 100644 dpsim-models/include/dpsim-models/SP/SP_Ph1_ResIndSeries.h create mode 100644 dpsim-models/src/SP/SP_Ph1_ResIndSeries.cpp diff --git a/dpsim-models/include/dpsim-models/Components.h b/dpsim-models/include/dpsim-models/Components.h index bcaefc1572..d90ae1a11f 100644 --- a/dpsim-models/include/dpsim-models/Components.h +++ b/dpsim-models/include/dpsim-models/Components.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include diff --git a/dpsim-models/include/dpsim-models/DP/DP_Ph1_ResIndSeries.h b/dpsim-models/include/dpsim-models/DP/DP_Ph1_ResIndSeries.h index 0a68568300..e36c4196a7 100644 --- a/dpsim-models/include/dpsim-models/DP/DP_Ph1_ResIndSeries.h +++ b/dpsim-models/include/dpsim-models/DP/DP_Ph1_ResIndSeries.h @@ -44,7 +44,7 @@ namespace Ph1 { /// Initializes state variables considering the number of frequencies void initialize(Matrix frequencies); /// Initializes states from power flow data - void initializeFromNodesAndTerminals(Real frequency); + void initializeFromNodesAndTerminals(Real frequency) override; /// Initializes auxiliar variables void initVars(Real timeStep); diff --git a/dpsim-models/include/dpsim-models/SP/SP_Ph1_ResIndSeries.h b/dpsim-models/include/dpsim-models/SP/SP_Ph1_ResIndSeries.h new file mode 100644 index 0000000000..ec3f2e3868 --- /dev/null +++ b/dpsim-models/include/dpsim-models/SP/SP_Ph1_ResIndSeries.h @@ -0,0 +1,61 @@ +/* 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 +#include + +namespace CPS { +namespace SP { +namespace Ph1 { + /// Static phasor ResIndSeries model (only implemented for dynamic simulations!) + class ResIndSeries : + public MNASimPowerComp, + public SharedFactory { + private: + /// Impedance + Complex mImpedance; + public: + /// Inductance [H] + const Attribute::Ptr mInductance; + ///Resistance [ohm] + const Attribute::Ptr mResistance; + /// Defines UID, name and logging level + ResIndSeries(String uid, String name, Logger::Level logLevel = Logger::Level::off); + /// Defines name and logging level + ResIndSeries(String name, Logger::Level logLevel = Logger::Level::off) + : ResIndSeries(name, name, logLevel) { } + + SimPowerComp::Ptr clone(String name); + + // #### General #### + /// Sets model specific parameters + void setParameters(Real resistance, Real inductance); + /// Initializes component from power flow data + void initializeFromNodesAndTerminals(Real frequency) override; + + + // #### MNA section #### + /// Initializes MNA specific variables + void mnaCompInitialize(Real omega, Real timeStep, Attribute::Ptr leftVector) override; + /// Stamps system matrix + void mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) 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 pre and post step operations + void mnaCompPostStep(Real time, Int timeStepCount, Attribute::Ptr &leftVector) override; + /// add MNA pre and post step dependencies + void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute::Ptr &leftVector) override; + + }; +} +} +} diff --git a/dpsim-models/include/dpsim-models/SP/SP_Ph1_Resistor.h b/dpsim-models/include/dpsim-models/SP/SP_Ph1_Resistor.h index 25c8c64c6e..bedf59fdff 100644 --- a/dpsim-models/include/dpsim-models/SP/SP_Ph1_Resistor.h +++ b/dpsim-models/include/dpsim-models/SP/SP_Ph1_Resistor.h @@ -55,7 +55,7 @@ namespace Ph1 { // #### General #### /// Initializes component from power flow data - void initializeFromNodesAndTerminals(Real frequency); + void initializeFromNodesAndTerminals(Real frequency) override; // #### Powerflow section #### /// Set base voltage @@ -67,17 +67,17 @@ namespace Ph1 { // #### MNA section #### /// - void mnaCompInitialize(Real omega, Real timeStep, Attribute::Ptr leftVector); + void mnaCompInitialize(Real omega, Real timeStep, Attribute::Ptr leftVector) override; /// Stamps system matrix - void mnaCompApplySystemMatrixStamp(Matrix& systemMatrix); + void mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) override; /// Update interface voltage from MNA system result - void mnaCompUpdateVoltage(const Matrix& leftVector); + void mnaCompUpdateVoltage(const Matrix& leftVector) override; /// Update interface current from MNA system result - void mnaCompUpdateCurrent(const Matrix& leftVector); + void mnaCompUpdateCurrent(const Matrix& leftVector) override; /// MNA pre and post step operations - void mnaCompPostStep(Real time, Int timeStepCount, Attribute::Ptr &leftVector); + void mnaCompPostStep(Real time, Int timeStepCount, Attribute::Ptr &leftVector) override; /// add MNA pre and post step dependencies - void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute::Ptr &leftVector); + void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute::Ptr &leftVector) override; // #### MNA Tear Section #### void mnaTearApplyMatrixStamp(Matrix& tearMatrix); diff --git a/dpsim-models/src/CMakeLists.txt b/dpsim-models/src/CMakeLists.txt index c108445898..5546e62bba 100644 --- a/dpsim-models/src/CMakeLists.txt +++ b/dpsim-models/src/CMakeLists.txt @@ -100,6 +100,7 @@ list(APPEND MODELS_SOURCES SP/SP_Ph1_Capacitor.cpp SP/SP_Ph1_Inductor.cpp SP/SP_Ph1_Resistor.cpp + SP/SP_Ph1_ResIndSeries.cpp SP/SP_Ph1_AvVoltageSourceInverterDQ.cpp SP/SP_Ph1_RXLine.cpp SP/SP_Ph1_VoltageSourceInverter.cpp diff --git a/dpsim-models/src/DP/DP_Ph1_ResIndSeries.cpp b/dpsim-models/src/DP/DP_Ph1_ResIndSeries.cpp index d016018925..bbd5441664 100644 --- a/dpsim-models/src/DP/DP_Ph1_ResIndSeries.cpp +++ b/dpsim-models/src/DP/DP_Ph1_ResIndSeries.cpp @@ -78,7 +78,7 @@ void DP::Ph1::ResIndSeries::initVars(Real timeStep) { mPrevCurrFac(freq,0) = { preCurrFracReal, preCurrFracImag }; mEquivCurrent(freq,0) = mEquivCond(freq,0) * (**mIntfVoltage)(0,freq) + mPrevCurrFac(freq,0) * (**mIntfCurrent)(0,freq); - (**mIntfCurrent)(0,freq) = mEquivCond(freq,0) * (**mIntfVoltage)(0,freq) + mEquivCurrent(freq,0); + //(**mIntfCurrent)(0,freq) = mEquivCond(freq,0) * (**mIntfVoltage)(0,freq) + mEquivCurrent(freq,0); } } diff --git a/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp b/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp index cfe157a8db..e491e6231d 100644 --- a/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp +++ b/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp @@ -49,6 +49,7 @@ void SP::Ph1::Capacitor::initializeFromNodesAndTerminals(Real frequency) { Logger::phasorToString((**mIntfCurrent)(0, 0)), Logger::phasorToString(initialSingleVoltage(0)), Logger::phasorToString(initialSingleVoltage(1))); + mSLog->flush(); } // #### MNA section #### @@ -62,6 +63,7 @@ void SP::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep, Attribute< "\n--- MNA initialization finished ---", Logger::phasorToString((**mIntfVoltage)(0, 0)), Logger::phasorToString((**mIntfCurrent)(0, 0))); + mSLog->flush(); } void SP::Ph1::Capacitor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) { @@ -105,7 +107,7 @@ void SP::Ph1::Capacitor::mnaCompPostStep(Real time, Int timeStepCount, Attribute void SP::Ph1::Capacitor::mnaCompUpdateVoltage(const Matrix& leftVector) { // v1 - v0 - **mIntfVoltage = Matrix::Zero(3, 1); + **mIntfVoltage = Matrix::Zero(1, 1); if (terminalNotGrounded(1)) { (**mIntfVoltage)(0, 0) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(1)); } diff --git a/dpsim-models/src/SP/SP_Ph1_Inductor.cpp b/dpsim-models/src/SP/SP_Ph1_Inductor.cpp index d31efabc7f..82bd312305 100644 --- a/dpsim-models/src/SP/SP_Ph1_Inductor.cpp +++ b/dpsim-models/src/SP/SP_Ph1_Inductor.cpp @@ -45,6 +45,7 @@ void SP::Ph1::Inductor::initializeFromNodesAndTerminals(Real frequency) { Logger::phasorToString((**mIntfCurrent)(0, 0)), Logger::phasorToString(initialSingleVoltage(0)), Logger::phasorToString(initialSingleVoltage(1))); + mSLog->flush(); } // #### MNA section #### @@ -59,6 +60,7 @@ void SP::Ph1::Inductor::mnaCompInitialize(Real omega, Real timeStep, Attributeflush(); } void SP::Ph1::Inductor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) { @@ -87,6 +89,7 @@ void SP::Ph1::Inductor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) { mSLog->info("Add {:e}+j{:e} to system at ({:d},{:d})", -mSusceptance.real(), -mSusceptance.imag(), matrixNodeIndex(1), matrixNodeIndex(0)); } + mSLog->flush(); } void SP::Ph1::Inductor::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute::Ptr &leftVector) { @@ -102,7 +105,7 @@ void SP::Ph1::Inductor::mnaCompPostStep(Real time, Int timeStepCount, Attribute< void SP::Ph1::Inductor::mnaCompUpdateVoltage(const Matrix& leftVector) { // v1 - v0 - **mIntfVoltage = Matrix::Zero(3, 1); + **mIntfVoltage = Matrix::Zero(1, 1); if (terminalNotGrounded(1)) { (**mIntfVoltage)(0, 0) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(1)); } diff --git a/dpsim-models/src/SP/SP_Ph1_ResIndSeries.cpp b/dpsim-models/src/SP/SP_Ph1_ResIndSeries.cpp new file mode 100644 index 0000000000..e2d770a8eb --- /dev/null +++ b/dpsim-models/src/SP/SP_Ph1_ResIndSeries.cpp @@ -0,0 +1,126 @@ +/* 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 + +using namespace CPS; + + +SP::Ph1::ResIndSeries::ResIndSeries(String uid, String name, Logger::Level logLevel) + : MNASimPowerComp(uid, name, false, true, logLevel), + mInductance(mAttributes->create("L")), + mResistance(mAttributes->create("R")) { + setTerminalNumber(2); + **mIntfVoltage = MatrixComp::Zero(1, 1); + **mIntfCurrent = MatrixComp::Zero(1, 1); +} + +SimPowerComp::Ptr SP::Ph1::ResIndSeries::clone(String name) { + auto copy = ResIndSeries::make(name, mLogLevel); + copy->setParameters(**mResistance, **mInductance); + return copy; +} + +void SP::Ph1::ResIndSeries::setParameters(Real resistance, Real inductance) { + **mResistance = resistance; + **mInductance = inductance; +} + +void SP::Ph1::ResIndSeries::initializeFromNodesAndTerminals(Real frequency) { + + Real omega = 2. * PI * frequency; + mImpedance = { **mResistance, omega * **mInductance }; + (**mIntfVoltage)(0,0) = initialSingleVoltage(1) - initialSingleVoltage(0); + (**mIntfCurrent)(0,0) = (**mIntfVoltage)(0,0) / mImpedance; + + mSLog->info( + "\n--- Initialization from powerflow ---" + "\nVoltage across: {:s}" + "\nCurrent: {:s}" + "\nTerminal 0 voltage: {:s}" + "\nTerminal 1 voltage: {:s}" + "\n--- Initialization from powerflow finished ---", + Logger::phasorToString((**mIntfVoltage)(0,0)), + Logger::phasorToString((**mIntfCurrent)(0,0)), + Logger::phasorToString(initialSingleVoltage(0)), + Logger::phasorToString(initialSingleVoltage(1))); + mSLog->flush(); +} + +// #### MNA section #### + +void SP::Ph1::ResIndSeries::mnaCompInitialize(Real omega, Real timeStep, Attribute::Ptr leftVector) { + updateMatrixNodeIndices(); + **mRightVector = Matrix::Zero(0, 0); + + mSLog->info( + "\n--- MNA initialization ---" + "\nInitial voltage {:s}" + "\nInitial current {:s}" + "\n--- MNA initialization finished ---", + Logger::phasorToString((**mIntfVoltage)(0, 0)), + Logger::phasorToString((**mIntfCurrent)(0, 0))); +} + +void SP::Ph1::ResIndSeries::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) { + Complex conductance = 1. / mImpedance; + + for (UInt freq = 0; freq < mNumFreqs; freq++) { + // Set diagonal entries + if (terminalNotGrounded(0)) + Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(0), conductance, mNumFreqs, freq); + if (terminalNotGrounded(1)) + // Set off diagonal entries + Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(1), conductance, mNumFreqs, freq); + if (terminalNotGrounded(0) && terminalNotGrounded(1)) { + Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), -conductance, mNumFreqs, freq); + Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(0), -conductance, mNumFreqs, freq); + } + + mSLog->info("-- Stamp frequency {:d} ---", freq); + if (terminalNotGrounded(0)) + mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(conductance), matrixNodeIndex(0), matrixNodeIndex(0)); + if (terminalNotGrounded(1)) + mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(conductance), matrixNodeIndex(1), matrixNodeIndex(1)); + if (terminalNotGrounded(0) && terminalNotGrounded(1)) { + mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(-conductance), matrixNodeIndex(0), matrixNodeIndex(1)); + mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(-conductance), matrixNodeIndex(1), matrixNodeIndex(0)); + } + } +} + +void SP::Ph1::ResIndSeries::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute::Ptr &leftVector) { + attributeDependencies.push_back(leftVector); + modifiedAttributes.push_back(mIntfVoltage); + modifiedAttributes.push_back(mIntfCurrent); +} + +void SP::Ph1::ResIndSeries::mnaCompPostStep(Real time, Int timeStepCount, Attribute::Ptr &leftVector) { + this->mnaUpdateVoltage(**leftVector); + this->mnaUpdateCurrent(**leftVector); +} + +void SP::Ph1::ResIndSeries::mnaCompUpdateVoltage(const Matrix& leftVector) { + // v1 - v0 + for (UInt freq = 0; freq < mNumFreqs; freq++) { + (**mIntfVoltage)(0,freq) = 0; + if (terminalNotGrounded(1)) + (**mIntfVoltage)(0,freq) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(1), mNumFreqs, freq); + if (terminalNotGrounded(0)) + (**mIntfVoltage)(0,freq) = (**mIntfVoltage)(0,freq) - Math::complexFromVectorElement(leftVector, matrixNodeIndex(0), mNumFreqs, freq); + + SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}", Logger::phasorToString((**mIntfVoltage)(0,freq))); + } +} + +void SP::Ph1::ResIndSeries::mnaCompUpdateCurrent(const Matrix& leftVector) { + for (UInt freq = 0; freq < mNumFreqs; freq++) { + (**mIntfCurrent)(0,freq) = (**mIntfVoltage)(0,freq) / mImpedance; + SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}", Logger::phasorToString((**mIntfCurrent)(0,freq))); + } +} \ No newline at end of file diff --git a/dpsim-models/src/SP/SP_Ph1_Resistor.cpp b/dpsim-models/src/SP/SP_Ph1_Resistor.cpp index c0de2d688e..89a272617f 100644 --- a/dpsim-models/src/SP/SP_Ph1_Resistor.cpp +++ b/dpsim-models/src/SP/SP_Ph1_Resistor.cpp @@ -43,6 +43,7 @@ void SP::Ph1::Resistor::initializeFromNodesAndTerminals(Real frequency) { Logger::phasorToString((**mIntfCurrent)(0, 0)), Logger::phasorToString(initialSingleVoltage(0)), Logger::phasorToString(initialSingleVoltage(1))); + mSLog->flush(); } // #### Powerflow section #### @@ -95,6 +96,7 @@ void SP::Ph1::Resistor::mnaCompInitialize(Real omega, Real timeStep, Attributeflush(); } void SP::Ph1::Resistor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) { @@ -122,6 +124,7 @@ void SP::Ph1::Resistor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) { mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(-conductance), matrixNodeIndex(1), matrixNodeIndex(0)); } } + mSLog->flush(); } void SP::Ph1::Resistor::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute::Ptr &leftVector) { diff --git a/dpsim-models/src/SP/SP_Ph1_VoltageSource.cpp b/dpsim-models/src/SP/SP_Ph1_VoltageSource.cpp index 0ebe20b419..9de389cff7 100644 --- a/dpsim-models/src/SP/SP_Ph1_VoltageSource.cpp +++ b/dpsim-models/src/SP/SP_Ph1_VoltageSource.cpp @@ -76,6 +76,7 @@ void SP::Ph1::VoltageSource::initializeFromNodesAndTerminals(Real frequency) { Logger::phasorToString(mSrcSig->getSignal()), Logger::phasorToString(initialSingleVoltage(0)), Logger::phasorToString(initialSingleVoltage(1))); + mSLog->flush(); } // #### MNA functions #### @@ -103,6 +104,7 @@ void SP::Ph1::VoltageSource::mnaCompInitialize(Real omega, Real timeStep, Attrib "\n--- MNA initialization finished ---", Logger::phasorToString((**mIntfVoltage)(0,0)), Logger::phasorToString((**mIntfCurrent)(0,0))); + mSLog->flush(); } void SP::Ph1::VoltageSource::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) { diff --git a/dpsim/src/pybind/SPComponents.cpp b/dpsim/src/pybind/SPComponents.cpp index d174ba2cd8..a4b3ae14b2 100644 --- a/dpsim/src/pybind/SPComponents.cpp +++ b/dpsim/src/pybind/SPComponents.cpp @@ -55,6 +55,12 @@ void addSPPh1Components(py::module_ mSPPh1) { .def("connect", &CPS::SP::Ph1::Inductor::connect) .def_property("L", createAttributeGetter("L"), createAttributeSetter("L")); + py::class_, CPS::SimPowerComp>(mSPPh1, "ResInductor", py::multiple_inheritance()) + .def(py::init()) + .def(py::init()) + .def("set_parameters", &CPS::SP::Ph1::ResIndSeries::setParameters, "R"_a, "L"_a) + .def("connect", &CPS::SP::Ph1::ResIndSeries::connect); + py::class_, CPS::SimPowerComp>(mSPPh1, "NetworkInjection", py::multiple_inheritance()) .def(py::init(), "name"_a, "loglevel"_a = CPS::Logger::Level::off) .def("set_parameters", py::overload_cast(&CPS::SP::Ph1::NetworkInjection::setParameters), "voltage_set_point"_a) diff --git a/examples/Notebooks/Circuits/Validation_RL_Element.ipynb b/examples/Notebooks/Circuits/Validation_RL_Element.ipynb index 114e8b39db..40ca86eb2b 100644 --- a/examples/Notebooks/Circuits/Validation_RL_Element.ipynb +++ b/examples/Notebooks/Circuits/Validation_RL_Element.ipynb @@ -70,7 +70,7 @@ "\n", "# Components\n", "vs_pf = dpsimpy.sp.ph1.VoltageSource('vs_pf', dpsimpy.LogLevel.debug)\n", - "vs_pf.set_parameters(V_ref=complex(10,0), f_src=frequency)\n", + "vs_pf.set_parameters(V_ref=complex(10,0))\n", "r1_pf = dpsimpy.sp.ph1.Resistor('r1_pf', dpsimpy.LogLevel.debug)\n", "r1_pf.set_parameters(R=resistance)\n", "c1_pf = dpsimpy.sp.ph1.Capacitor('c1_pf', dpsimpy.LogLevel.debug)\n", @@ -140,7 +140,7 @@ "\n", "# Components\n", "vs = dpsimpy.dp.ph1.VoltageSource('vs')\n", - "vs.set_parameters(V_ref=complex(10,0), f_src=frequency)\n", + "vs.set_parameters(V_ref=complex(10,0))\n", "r1 = dpsimpy.dp.ph1.Resistor('r_1', dpsimpy.LogLevel.off)\n", "r1.R = resistance\n", "l1 = dpsimpy.dp.ph1.Inductor('l_1', dpsimpy.LogLevel.off)\n", @@ -201,7 +201,7 @@ "ts_dpsim_comps = rt.read_timeseries_dpsim(file_path)\n", "\n", "#convert to emt\n", - "#ts_dpsim_comps = ts.frequency_shift_list(ts_dpsim_comps, frequency)" + "ts_dpsim_comps = ts.frequency_shift_list(ts_dpsim_comps, frequency)" ] }, { @@ -230,7 +230,7 @@ "\n", "# Components\n", "vs = dpsimpy.dp.ph1.VoltageSource('vs', dpsimpy.LogLevel.debug)\n", - "vs.set_parameters(V_ref=complex(10,0), f_src=frequency)\n", + "vs.set_parameters(V_ref=complex(10,0))\n", "rl = dpsimpy.dp.ph1.ResInductor('rl', dpsimpy.LogLevel.debug)\n", "rl.set_parameters(R=resistance, L=inductance)\n", "c1 = dpsimpy.dp.ph1.Capacitor('c1', dpsimpy.LogLevel.debug)\n", @@ -279,7 +279,7 @@ "ts_dpsim_dp = rt.read_timeseries_dpsim(file_path)\n", "\n", "#convert to emt\n", - "#ts_dpsim_dp = ts.frequency_shift_list(ts_dpsim_dp, frequency)" + "ts_dpsim_dp = ts.frequency_shift_list(ts_dpsim_dp, frequency)" ] }, { @@ -296,17 +296,17 @@ "metadata": {}, "outputs": [], "source": [ - "ts_dpsim_comps[\"n3.v\"].label = 'vc - RLC circuit'\n", - "ts_dpsim_comps[\"l1.i_intf\"].label = 'i - RLC circuit'\n", + "ts_dpsim_comps[\"n3.v_shift\"].label = 'vc - RLC circuit'\n", + "ts_dpsim_comps[\"l1.i_intf_shift\"].label = 'i - RLC circuit'\n", "\n", - "ts_dpsim_dp[\"n2.v\"].label = 'vc RL Element - DP'\n", - "ts_dpsim_dp[\"rl.i_intf\"].label = 'i RL Element - DP'\n", + "ts_dpsim_dp[\"n2.v_shift\"].label = 'vc RL Element - DP'\n", + "ts_dpsim_dp[\"rl.i_intf_shift\"].label = 'i RL Element - DP'\n", "\n", - "pt.plot_timeseries(1, ts_dpsim_comps[\"n3.v\"])\n", - "pt.plot_timeseries(1, ts_dpsim_dp[\"n2.v\"], plt_linestyle='--')\n", + "pt.plot_timeseries(1, ts_dpsim_comps[\"n3.v_shift\"])\n", + "pt.plot_timeseries(1, ts_dpsim_dp[\"n2.v_shift\"], plt_linestyle='--')\n", "\n", - "pt.plot_timeseries(2, ts_dpsim_comps[\"l1.i_intf\"])\n", - "pt.plot_timeseries(2, ts_dpsim_dp[\"rl.i_intf\"], plt_linestyle='--')" + "pt.plot_timeseries(2, ts_dpsim_comps[\"l1.i_intf_shift\"])\n", + "pt.plot_timeseries(2, ts_dpsim_dp[\"rl.i_intf_shift\"], plt_linestyle='--')" ] }, { @@ -319,13 +319,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "# calculate the RMSE between both models\n", - "err_current = ts.rmse(ts_dpsim_comps[\"l1.i_intf\"], ts_dpsim_dp[\"rl.i_intf\"])\n", - "err_voltage = ts.rmse(ts_dpsim_comps[\"n3.v\"], ts_dpsim_dp[\"n2.v\"])\n", + "err_current = ts.rmse(ts_dpsim_comps[\"l1.i_intf_shift\"], ts_dpsim_dp[\"rl.i_intf_shift\"])\n", + "err_voltage = ts.rmse(ts_dpsim_comps[\"n3.v_shift\"], ts_dpsim_dp[\"n2.v_shift\"])\n", "\n", "assert err_current < 0.00001\n", "assert err_voltage < 0.00001" @@ -534,7 +534,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -545,6 +545,228 @@ "assert err_current < 0.001\n", "assert err_voltage < 0.001" ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3. SP" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### RLC circuit (reference)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "name_sp_ref = 'SP_VS_RLC'\n", + "dpsimpy.Logger.set_log_dir(\"logs/\" + name_sp_ref)\n", + "\n", + "# Nodes\n", + "gnd = dpsimpy.sp.SimNode.gnd\n", + "n1 = dpsimpy.sp.SimNode('n1')\n", + "n1.set_initial_voltage(sim_pf.get_idobj_attr(n1_pf.name(), 'v').get()[0])\n", + "n2 = dpsimpy.sp.SimNode('n2')\n", + "n2.set_initial_voltage(sim_pf.get_idobj_attr(n2_pf.name(), 'v').get()[0])\n", + "n3 = dpsimpy.sp.SimNode('n3')\n", + "n3.set_initial_voltage(sim_pf.get_idobj_attr(n3_pf.name(), 'v').get()[0])\n", + "\n", + "# Components\n", + "vs = dpsimpy.sp.ph1.VoltageSource('vs')\n", + "vs.set_parameters(V_ref=complex(10,0))\n", + "r1 = dpsimpy.sp.ph1.Resistor('r_1', dpsimpy.LogLevel.info)\n", + "r1.set_parameters(R=resistance)\n", + "l1 = dpsimpy.sp.ph1.Inductor('l_1', dpsimpy.LogLevel.info)\n", + "l1.set_parameters(L=inductance)\n", + "c1 = dpsimpy.sp.ph1.Capacitor('c1', dpsimpy.LogLevel.info)\n", + "c1.set_parameters(C=capacitance)\n", + "\n", + "# Connections\n", + "vs.connect([gnd, n1])\n", + "r1.connect([n2, n1])\n", + "l1.connect([n3, n2])\n", + "c1.connect([gnd, n3])\n", + "\n", + "# Define system topology\n", + "system = dpsimpy.SystemTopology(frequency, [gnd, n1, n2, n3], [vs, r1, l1, c1])\n", + "\n", + "# Logging\n", + "logger = dpsimpy.Logger(name_sp_ref)\n", + "logger.log_attribute('n1.v', 'v', n1)\n", + "logger.log_attribute('n2.v', 'v', n2)\n", + "logger.log_attribute('n3.v', 'v', n3)\n", + "logger.log_attribute('l1.i_intf', 'i_intf', l1)\n", + "\n", + "\n", + "sim = dpsimpy.Simulation(name_sp_ref, dpsimpy.LogLevel.info)\n", + "sim.set_system(system)\n", + "sim.set_domain(dpsimpy.Domain.SP)\n", + "sim.set_time_step(0.0001)\n", + "sim.set_final_time(0.02)\n", + "sim.add_logger(logger)\n", + "sim.run()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Read Results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# read Simulink log file\n", + "\n", + "# read EMT results\n", + "file_path = os.getcwd() + \"/logs/\" + name_sp_ref + \"/\" + name_sp_ref + \".csv\"\n", + "ts_dpsim_sp_ref = rt.read_timeseries_dpsim(file_path)\n", + "\n", + "#convert to emt\n", + "ts_dpsim_sp_ref = ts.frequency_shift_list(ts_dpsim_sp_ref, frequency)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Circuit with RL-Element" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "name_sp = 'SP_RL-element'\n", + "dpsimpy.Logger.set_log_dir(\"logs/\" + name_sp)\n", + "\n", + "# Nodes\n", + "gnd = dpsimpy.sp.SimNode.gnd\n", + "n1 = dpsimpy.sp.SimNode('n1')\n", + "n1.set_initial_voltage(sim_pf.get_idobj_attr(n1_pf.name(), 'v').get()[0])\n", + "n2 = dpsimpy.sp.SimNode('n2')\n", + "n2.set_initial_voltage(sim_pf.get_idobj_attr(n3_pf.name(), 'v').get()[0])\n", + "\n", + "# Components\n", + "vs = dpsimpy.sp.ph1.VoltageSource('vs', dpsimpy.LogLevel.debug)\n", + "vs.set_parameters(V_ref=complex(10,0))\n", + "rl = dpsimpy.sp.ph1.ResInductor('rl', dpsimpy.LogLevel.debug)\n", + "rl.set_parameters(R=resistance, L=inductance)\n", + "c1 = dpsimpy.sp.ph1.Capacitor('c1', dpsimpy.LogLevel.debug)\n", + "c1.set_parameters(C=capacitance)\n", + "\n", + "# Connections\n", + "vs.connect([gnd, n1])\n", + "rl.connect([n2, n1])\n", + "c1.connect([gnd, n2])\n", + "\n", + "# Define system topology\n", + "system = dpsimpy.SystemTopology(frequency, [gnd, n1, n2], [vs, rl, c1])\n", + "\n", + "# Logging\n", + "logger = dpsimpy.Logger(name_sp)\n", + "logger.log_attribute('n1.v', 'v', n1)\n", + "logger.log_attribute('n2.v', 'v', n2)\n", + "logger.log_attribute('rl.i_intf', 'i_intf', rl)\n", + "logger.log_attribute('rl.v', 'v_intf', rl)\n", + "\n", + "sim = dpsimpy.Simulation(name_sp)\n", + "sim.set_system(system)\n", + "sim.set_domain(dpsimpy.Domain.SP)\n", + "sim.set_time_step(0.0001)\n", + "sim.set_final_time(0.02)\n", + "sim.add_logger(logger)\n", + "sim.run()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Read Results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# read Simulink log file\n", + "\n", + "# read EMT results\n", + "file_path = os.getcwd() + \"/logs/\" + name_sp + \"/\" + name_sp + \".csv\"\n", + "ts_dpsim_sp = rt.read_timeseries_dpsim(file_path)\n", + "\n", + "#convert to emt\n", + "ts_dpsim_sp = ts.frequency_shift_list(ts_dpsim_sp, frequency)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Plot Results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ts_dpsim_sp_ref[\"l1.i_intf_shift\"].label = 'i - REF'\n", + "ts_dpsim_sp_ref[\"n3.v_shift\"].label = 'vc - REF'\n", + "ts_dpsim_sp[\"rl.i_intf_shift\"].label = 'i RL Element'\n", + "ts_dpsim_sp[\"n2.v_shift\"].label = 'vc RL Element'\n", + "\n", + "pt.plot_timeseries(5, ts_dpsim_sp_ref[\"n3.v_shift\"])\n", + "pt.plot_timeseries(5, ts_dpsim_sp[\"n2.v_shift\"], plt_linestyle='--')\n", + "\n", + "pt.plot_timeseries(6, ts_dpsim_sp_ref[\"l1.i_intf_shift\"])\n", + "pt.plot_timeseries(6, ts_dpsim_sp[\"rl.i_intf_shift\"], plt_linestyle='--')" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Assert" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "# calculate the RMSE between both models\n", + "err_current = ts.rmse(ts_dpsim_sp_ref[\"l1.i_intf_shift\"], ts_dpsim_sp[\"rl.i_intf_shift\"])\n", + "err_voltage = ts.rmse(ts_dpsim_sp_ref[\"n3.v_shift\"], ts_dpsim_sp[\"n2.v_shift\"])\n", + "\n", + "assert err_current < 0.00001\n", + "assert err_voltage < 0.00001" + ] } ], "metadata": { @@ -563,7 +785,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.13 (main, May 18 2022, 00:00:00) \n[GCC 11.3.1 20220421 (Red Hat 11.3.1-2)]" }, "orig_nbformat": 4, "vscode": {