From 9f60aa1bda3d15f68d77b0a7b4dd12394881549f Mon Sep 17 00:00:00 2001 From: Martin Moraga Date: Wed, 8 Mar 2023 15:31:14 +0100 Subject: [PATCH] WIP Signed-off-by: Martin Moraga --- .../dpsim-models/EMT/EMT_Ph1_VoltageSource.h | 2 - .../include/dpsim-models/SP/SP_Ph1_Inductor.h | 2 +- .../src/EMT/EMT_Ph1_VoltageSource.cpp | 4 +- .../Circuits/Validation_RL_Element.ipynb | 106 ++++++++++++++---- 4 files changed, 88 insertions(+), 26 deletions(-) diff --git a/dpsim-models/include/dpsim-models/EMT/EMT_Ph1_VoltageSource.h b/dpsim-models/include/dpsim-models/EMT/EMT_Ph1_VoltageSource.h index 0f663c5f40..6e9a990f17 100644 --- a/dpsim-models/include/dpsim-models/EMT/EMT_Ph1_VoltageSource.h +++ b/dpsim-models/include/dpsim-models/EMT/EMT_Ph1_VoltageSource.h @@ -25,8 +25,6 @@ namespace Ph1 { class VoltageSource : public MNASimPowerComp, public SharedFactory { - private: - Real mTimeStep; protected: void updateVoltage(Real time); public: diff --git a/dpsim-models/include/dpsim-models/SP/SP_Ph1_Inductor.h b/dpsim-models/include/dpsim-models/SP/SP_Ph1_Inductor.h index 143cbf959f..1c56d52f7c 100644 --- a/dpsim-models/include/dpsim-models/SP/SP_Ph1_Inductor.h +++ b/dpsim-models/include/dpsim-models/SP/SP_Ph1_Inductor.h @@ -51,7 +51,7 @@ namespace Ph1 { void mnaCompPostStep(Real time, Int timeStepCount, Attribute::Ptr &leftVector); /// Add MNA post step dependencies void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute::Ptr &leftVector); - + // void mnaTearApplyMatrixStamp(Matrix& tearMatrix) override; }; } diff --git a/dpsim-models/src/EMT/EMT_Ph1_VoltageSource.cpp b/dpsim-models/src/EMT/EMT_Ph1_VoltageSource.cpp index cc4afb5cba..8af828066f 100644 --- a/dpsim-models/src/EMT/EMT_Ph1_VoltageSource.cpp +++ b/dpsim-models/src/EMT/EMT_Ph1_VoltageSource.cpp @@ -36,8 +36,6 @@ SimPowerComp::Ptr EMT::Ph1::VoltageSource::clone(String name) { void EMT::Ph1::VoltageSource::mnaCompInitialize(Real omega, Real timeStep, Attribute::Ptr leftVector) { updateMatrixNodeIndices(); (**mIntfVoltage)(0,0) = Math::abs(**mVoltageRef) * cos(Math::phase(**mVoltageRef)); - - mTimeStep = timeStep; } void EMT::Ph1::VoltageSource::mnaCompApplySystemMatrixStamp(SparseMatrixRow& systemMatrix) { @@ -68,7 +66,7 @@ void EMT::Ph1::VoltageSource::updateVoltage(Real time) { Complex voltageRef = mVoltageRef->get(); Real srcFreq = mSrcFreq->get(); if (srcFreq > 0) - (**mIntfVoltage)(0,0) = Math::abs(voltageRef) * cos((time) * 2.*PI*srcFreq + Math::phase(voltageRef)); + (**mIntfVoltage)(0,0) = Math::abs(voltageRef) * cos(time * 2. * PI * srcFreq + Math::phase(voltageRef)); else (**mIntfVoltage)(0,0) = voltageRef.real(); } diff --git a/examples/Notebooks/Circuits/Validation_RL_Element.ipynb b/examples/Notebooks/Circuits/Validation_RL_Element.ipynb index 40ca86eb2b..b9a24506bd 100644 --- a/examples/Notebooks/Circuits/Validation_RL_Element.ipynb +++ b/examples/Notebooks/Circuits/Validation_RL_Element.ipynb @@ -42,7 +42,8 @@ "resistance = 10\n", "inductance = 1e-3\n", "capacitance = 5e-6\n", - "frequency = 50" + "frequency = 50\n", + "epsilon = 1e-12" ] }, { @@ -319,7 +320,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -327,8 +328,11 @@ "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" + "print(err_current)\n", + "print(err_voltage)\n", + "\n", + "assert err_current < epsilon\n", + "assert err_voltage < epsilon" ] }, { @@ -450,9 +454,9 @@ "# Components\n", "vs = dpsimpy.emt.ph3.VoltageSource('vs', dpsimpy.LogLevel.off)\n", "vs.set_parameters(V_ref=dpsimpy.Math.single_phase_variable_to_three_phase(complex(10, 0)), f_src=frequency)\n", - "rl = dpsimpy.emt.ph3.ResInductor('rl', dpsimpy.LogLevel.off)\n", + "rl = dpsimpy.emt.ph3.ResInductor('rl', dpsimpy.LogLevel.debug)\n", "rl.set_parameters(R=dpsimpy.Math.single_phase_parameter_to_three_phase(resistance), L=dpsimpy.Math.single_phase_parameter_to_three_phase(inductance))\n", - "c1 = dpsimpy.emt.ph3.Capacitor('c1', dpsimpy.LogLevel.off)\n", + "c1 = dpsimpy.emt.ph3.Capacitor('c1', dpsimpy.LogLevel.debug)\n", "c1.set_parameters(C=dpsimpy.Math.single_phase_parameter_to_three_phase(capacitance))\n", "\n", "# Connections\n", @@ -473,7 +477,7 @@ "sim = dpsimpy.Simulation(name_emt, dpsimpy.LogLevel.off)\n", "sim.set_system(system)\n", "sim.set_domain(dpsimpy.Domain.EMT)\n", - "sim.set_time_step(0.00001)\n", + "sim.set_time_step(0.0001)\n", "sim.set_final_time(0.02)\n", "sim.add_logger(logger)\n", "sim.run()" @@ -503,7 +507,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Plot Results" + "### Plot Results phase a" ] }, { @@ -512,10 +516,10 @@ "metadata": {}, "outputs": [], "source": [ - "ts_dpsim_emt_ref[\"l1.i_intf_0\"].label = 'i - REF'\n", - "ts_dpsim_emt_ref[\"n3.v_0\"].label = 'vc - REF'\n", - "ts_dpsim_emt[\"rl.i_intf_0\"].label = 'i RL Element'\n", - "ts_dpsim_emt[\"n2.v_0\"].label = 'vc RL Element'\n", + "ts_dpsim_emt_ref[\"l1.i_intf_0\"].label = 'i (phase a) - REF'\n", + "ts_dpsim_emt_ref[\"n3.v_0\"].label = 'vc (phase a) - REF'\n", + "ts_dpsim_emt[\"rl.i_intf_0\"].label = 'i (phase a) RL Element'\n", + "ts_dpsim_emt[\"n2.v_0\"].label = 'vc (phase a) RL Element'\n", "\n", "pt.plot_timeseries(3, ts_dpsim_emt_ref[\"n3.v_0\"])\n", "pt.plot_timeseries(3, ts_dpsim_emt[\"n2.v_0\"], plt_linestyle='--')\n", @@ -524,6 +528,42 @@ "pt.plot_timeseries(4, ts_dpsim_emt[\"rl.i_intf_0\"], plt_linestyle='--')" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ts_dpsim_emt_ref[\"l1.i_intf_1\"].label = 'i (phase b) - REF'\n", + "ts_dpsim_emt_ref[\"n3.v_1\"].label = 'vc (phase b) - REF'\n", + "ts_dpsim_emt[\"rl.i_intf_1\"].label = 'i (phase b) RL Element'\n", + "ts_dpsim_emt[\"n2.v_1\"].label = 'vc RL (phase b) Element'\n", + "\n", + "pt.plot_timeseries(3, ts_dpsim_emt_ref[\"n3.v_1\"])\n", + "pt.plot_timeseries(3, ts_dpsim_emt[\"n2.v_1\"], plt_linestyle='--')\n", + "\n", + "pt.plot_timeseries(4, ts_dpsim_emt_ref[\"l1.i_intf_1\"])\n", + "pt.plot_timeseries(4, ts_dpsim_emt[\"rl.i_intf_1\"], plt_linestyle='--')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ts_dpsim_emt_ref[\"l1.i_intf_2\"].label = 'i (phase c) - REF'\n", + "ts_dpsim_emt_ref[\"n3.v_2\"].label = 'vc (phase c) - REF'\n", + "ts_dpsim_emt[\"rl.i_intf_2\"].label = 'i (phase c) RL Element'\n", + "ts_dpsim_emt[\"n2.v_2\"].label = 'vc RL (phase c) Element'\n", + "\n", + "pt.plot_timeseries(3, ts_dpsim_emt_ref[\"n3.v_2\"])\n", + "pt.plot_timeseries(3, ts_dpsim_emt[\"n2.v_2\"], plt_linestyle='--')\n", + "\n", + "pt.plot_timeseries(4, ts_dpsim_emt_ref[\"l1.i_intf_2\"])\n", + "pt.plot_timeseries(4, ts_dpsim_emt[\"rl.i_intf_2\"], plt_linestyle='--')" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -534,16 +574,39 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# calculate the RMSE between both models\n", + "# calculate the RMSE between both models phase a\n", "err_current = ts.rmse(ts_dpsim_emt_ref[\"l1.i_intf_0\"], ts_dpsim_emt[\"rl.i_intf_0\"])\n", "err_voltage = ts.rmse(ts_dpsim_emt_ref[\"n3.v_0\"], ts_dpsim_emt[\"n2.v_0\"])\n", "\n", - "assert err_current < 0.001\n", - "assert err_voltage < 0.001" + "print(err_current)\n", + "print(err_voltage)\n", + "\n", + "assert err_current < epsilon\n", + "assert err_voltage < epsilon\n", + "\n", + "# calculate the RMSE between both models phase b\n", + "err_current = ts.rmse(ts_dpsim_emt_ref[\"l1.i_intf_1\"], ts_dpsim_emt[\"rl.i_intf_1\"])\n", + "err_voltage = ts.rmse(ts_dpsim_emt_ref[\"n3.v_1\"], ts_dpsim_emt[\"n2.v_1\"])\n", + "\n", + "print(err_current)\n", + "print(err_voltage)\n", + "\n", + "assert err_current < epsilon\n", + "assert err_voltage < epsilon\n", + "\n", + "# calculate the RMSE between both models phase a\n", + "err_current = ts.rmse(ts_dpsim_emt_ref[\"l1.i_intf_2\"], ts_dpsim_emt[\"rl.i_intf_2\"])\n", + "err_voltage = ts.rmse(ts_dpsim_emt_ref[\"n3.v_2\"], ts_dpsim_emt[\"n2.v_2\"])\n", + "\n", + "print(err_current)\n", + "print(err_voltage)\n", + "\n", + "assert err_current < epsilon\n", + "assert err_voltage < epsilon" ] }, { @@ -756,7 +819,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -764,8 +827,11 @@ "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" + "print(err_current)\n", + "print(err_voltage)\n", + "\n", + "assert err_current < epsilon\n", + "assert err_voltage < epsilon" ] } ], @@ -785,7 +851,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13 (main, May 18 2022, 00:00:00) \n[GCC 11.3.1 20220421 (Red Hat 11.3.1-2)]" + "version": "3.9.13" }, "orig_nbformat": 4, "vscode": {