Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Moraga <[email protected]>
  • Loading branch information
martinmoraga committed Mar 27, 2023
1 parent 4c8df43 commit 9f60aa1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 26 deletions.
2 changes: 0 additions & 2 deletions dpsim-models/include/dpsim-models/EMT/EMT_Ph1_VoltageSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace Ph1 {
class VoltageSource :
public MNASimPowerComp<Real>,
public SharedFactory<VoltageSource> {
private:
Real mTimeStep;
protected:
void updateVoltage(Real time);
public:
Expand Down
2 changes: 1 addition & 1 deletion dpsim-models/include/dpsim-models/SP/SP_Ph1_Inductor.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Ph1 {
void mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector);
/// Add MNA post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector);

//
void mnaTearApplyMatrixStamp(Matrix& tearMatrix) override;
};
}
Expand Down
4 changes: 1 addition & 3 deletions dpsim-models/src/EMT/EMT_Ph1_VoltageSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ SimPowerComp<Real>::Ptr EMT::Ph1::VoltageSource::clone(String name) {
void EMT::Ph1::VoltageSource::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
updateMatrixNodeIndices();
(**mIntfVoltage)(0,0) = Math::abs(**mVoltageRef) * cos(Math::phase(**mVoltageRef));

mTimeStep = timeStep;
}

void EMT::Ph1::VoltageSource::mnaCompApplySystemMatrixStamp(SparseMatrixRow& systemMatrix) {
Expand Down Expand Up @@ -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();
}
Expand Down
106 changes: 86 additions & 20 deletions examples/Notebooks/Circuits/Validation_RL_Element.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"resistance = 10\n",
"inductance = 1e-3\n",
"capacitance = 5e-6\n",
"frequency = 50"
"frequency = 50\n",
"epsilon = 1e-12"
]
},
{
Expand Down Expand Up @@ -319,16 +320,19 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate the RMSE between both models\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"
"print(err_current)\n",
"print(err_voltage)\n",
"\n",
"assert err_current < epsilon\n",
"assert err_voltage < epsilon"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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()"
Expand Down Expand Up @@ -503,7 +507,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot Results"
"### Plot Results phase a"
]
},
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -756,16 +819,19 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": null,
"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"
"print(err_current)\n",
"print(err_voltage)\n",
"\n",
"assert err_current < epsilon\n",
"assert err_voltage < epsilon"
]
}
],
Expand All @@ -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": {
Expand Down

0 comments on commit 9f60aa1

Please sign in to comment.