Skip to content

Commit

Permalink
WIP: Fix topology of RC benchmark Notebook.
Browse files Browse the repository at this point in the history
Signed-off-by: pipeacosta <pipeacosta@gmail.com>
  • Loading branch information
pipeacosta committed Jun 28, 2024
1 parent 5a23d95 commit df8002b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
47 changes: 30 additions & 17 deletions examples/Notebooks/cosim-villas-socket.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"metadata": {},
"outputs": [],
"source": [
"import dpsimpy"
"import dpsimpy\n",
"import numpy as np"
]
},
{
Expand All @@ -56,10 +57,14 @@
"metadata": {},
"outputs": [],
"source": [
"with_vs = False\n",
"with_vs = True\n",
"\n",
"# Nodes\n",
"gnd = dpsimpy.emt.SimNode.gnd\n",
"\n",
"if with_vs:\n",
" n0 = dpsimpy.emt.SimNode(\"n0\")\n",
"\n",
"n1 = dpsimpy.emt.SimNode(\"n1\")\n",
"n2 = dpsimpy.emt.SimNode(\"n2\")\n",
"\n",
Expand Down Expand Up @@ -89,9 +94,11 @@
"# Connections\n",
"\n",
"if with_vs:\n",
" v_s.connect([n1, gnd])\n",
" v_s.connect([gnd, n0])\n",
" r_1.connect([n0, n1])\n",
"else:\n",
" r_1.connect([n1, gnd])\n",
" \n",
"r_1.connect([n1, gnd])\n",
"r_line.connect([n1, n2])\n",
"c_1.connect([n1, gnd])\n",
"c_2.connect([n2, gnd])\n",
Expand All @@ -112,7 +119,7 @@
"outputs": [],
"source": [
"if with_vs:\n",
" sys = dpsimpy.SystemTopology(50, [ gnd, n1, n2 ], [ v_s, r_1, r_line, c_1, c_2, r_load ])\n",
" sys = dpsimpy.SystemTopology(50, [ gnd, n0, n1, n2 ], [ v_s, r_1, r_line, c_1, c_2, r_load ])\n",
"else:\n",
" sys = dpsimpy.SystemTopology(50, [ gnd, n1, n2 ], [ r_1, r_line, c_1, c_2, r_load ])"
]
Expand Down Expand Up @@ -182,8 +189,8 @@
"import subprocess\n",
"import numpy as np\n",
"\n",
"H_v = np.array([1e-4, 2e-4, 4e-4, 0.001, 0.003, 0.01])\n",
"H_v_legends = ['1e-4', '2e-4', '4e-4', '1e-3', '3e-3', '1e-2']\n",
"H_v = np.array([1e-4, 2e-4, 4e-4])\n",
"H_v_legends = ['1e-4', '2e-4', '4e-4']\n",
"\n",
"i=0\n",
"for H in H_v:\n",
Expand Down Expand Up @@ -274,6 +281,7 @@
"for k, results_att_list in results_attributes_linear.items():\n",
" results_emt_attributes_linear[k] = []\n",
" for series in results_att_list:\n",
" pt.set_timeseries_labels(results_att_list[series], series + ', H=' + k + ' - Linear')\n",
" results_emt_attributes_linear[k].append(results_att_list[series])\n",
" \n",
"# results_emt_villas_1 = []\n",
Expand All @@ -284,16 +292,21 @@
"# for series in results_villas2:\n",
"# results_emt_villas_2.append(results_villas2[series])\n",
"\n",
"pt.set_timeseries_labels(results_emt[1], results_emt[1].label + ' - Monolithic')\n",
"pt.plot_timeseries('Co-simulation results - V1', results_emt[1])\n",
"\n",
"pt.set_timeseries_labels(results_emt[2], results_emt[2].label + ' - Monolithic')\n",
"pt.plot_timeseries('Co-simulation results - V2', results_emt[2])\n",
"pt.plot_timeseries('Co-simulation results - V1', results_emt[2])\n",
"\n",
"pt.set_timeseries_labels(results_emt[3], results_emt[3].label + ' - Monolithic')\n",
"pt.plot_timeseries('Co-simulation results - V2', results_emt[3])\n",
"\n",
"pt.set_timeseries_labels(results_emt[0], results_emt[0].label + ' - Monolithic')\n",
"pt.plot_timeseries('Co-simulation results - i_intf', results_emt[0])\n",
"\n",
"for k, results_att_list in results_emt_attributes_zoh.items():\n",
"# for k, results_att_list in results_emt_attributes_zoh.items():\n",
"# pt.plot_timeseries('Co-simulation results - V1', results_att_list[2], '--')\n",
"# pt.plot_timeseries('Co-simulation results - V2', results_att_list[3], '--')\n",
"# pt.plot_timeseries('Co-simulation results - i_intf', results_att_list[0], '--')\n",
"\n",
"for k, results_att_list in results_emt_attributes_linear.items():\n",
" pt.plot_timeseries('Co-simulation results - V1', results_att_list[2], '--')\n",
" pt.plot_timeseries('Co-simulation results - V2', results_att_list[3], '--')\n",
" pt.plot_timeseries('Co-simulation results - i_intf', results_att_list[0], '--')\n",
Expand All @@ -306,7 +319,7 @@
"# pt.plot_timeseries('Co-simulation results', results_emt_matlab[4], '-.')\n",
"\n",
"plt.xlabel('t [s]')\n",
"plt.ylabel('Voltage [V]')\n",
"# plt.ylabel('Voltage [V]')\n",
"plt.grid()\n",
"plt.show()"
]
Expand Down Expand Up @@ -339,8 +352,8 @@
" \n",
" m = int(H/time_step)\n",
" \n",
" ts_1_m = results_emt[1]\n",
" ts_2_m = results_emt[2]\n",
" ts_1_m = results_emt[2]\n",
" ts_2_m = results_emt[3]\n",
" \n",
" ts_1_zoh = results_emt_attributes_zoh[H_v_legends[i]][2]\n",
" ts_2_zoh = results_emt_attributes_zoh[H_v_legends[i]][3]\n",
Expand All @@ -352,10 +365,10 @@
" v_zoh = np.array([ts_1_zoh.values[::m], ts_2_zoh.values[::m]])\n",
" v_linear = np.array([ts_1_linear.values[::m], ts_2_linear.values[::m]])\n",
" \n",
" gloal_error_zoh_t = np.max(np.linalg.norm(v_a - v_zoh, axis=0))\n",
" gloal_error_zoh_t = np.mean(np.linalg.norm(v_a - v_zoh, axis=0))\n",
" \n",
" global_error_zoh.append(gloal_error_zoh_t)\n",
" global_error_linear.append(np.max(np.linalg.norm(v_a - v_linear, axis=0)))\n",
" global_error_linear.append(np.mean(np.linalg.norm(v_a - v_linear, axis=0)))\n",
" \n",
"plt.figure()\n",
"plt.plot(np.log10(H_v), np.log10(global_error_zoh), 'b', label='ZOH')\n",
Expand Down
23 changes: 17 additions & 6 deletions examples/Python/Attributes/emt-cosim-attributes_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def set_dpsim1(t_s, t_f, u_1_0, vs_cond, logger_prefix):
sim_name = "S1"

gnd = dpsimpy.emt.SimNode.gnd

if with_vs:
n0 = dpsimpy.emt.SimNode("n0")
n1 = dpsimpy.emt.SimNode("n1")
n2 = dpsimpy.emt.SimNode("n2")

Expand All @@ -41,6 +44,7 @@ def set_dpsim1(t_s, t_f, u_1_0, vs_cond, logger_prefix):
# Initial conditions
if with_vs:
# This adds a transient at t=0
n0.set_initial_voltage(1 * dpsimpy.PEAK1PH_TO_RMS3PH)
n1.set_initial_voltage(0 * dpsimpy.PEAK1PH_TO_RMS3PH)
else:
n1.set_initial_voltage(5 * dpsimpy.PEAK1PH_TO_RMS3PH)
Expand All @@ -49,15 +53,17 @@ def set_dpsim1(t_s, t_f, u_1_0, vs_cond, logger_prefix):

# Connections
if with_vs:
v_s.connect([n1, gnd])
v_s.connect([gnd, n0])
r_1.connect([n0, n1])
else:
r_1.connect([gnd, n1])

r_1.connect([gnd, n1])
r_line.connect([n2, n1])
c_1.connect([gnd, n1])
evs.connect([gnd, n2])
evs.connect([n2, gnd])

if with_vs:
sys = dpsimpy.SystemTopology(50, [gnd, n1, n2], [v_s, evs, r_1, c_1, r_line])
sys = dpsimpy.SystemTopology(50, [gnd, n0, n1, n2], [v_s, evs, r_1, c_1, r_line])
else:
sys = dpsimpy.SystemTopology(50, [gnd, n1, n2], [evs, r_1, c_1, r_line])

Expand All @@ -79,12 +85,14 @@ def set_dpsim1(t_s, t_f, u_1_0, vs_cond, logger_prefix):
sim.add_logger(logger)

if with_vs:
n0_v0 = np.array([1.0])
n1_v0 = np.array([0.0])
ir_1_0 = (n0_v0 - n1_v0) / r_1_r
else:
n1_v0 = np.array([5.0])
ir_1_0 = n1_v0 / r_1_r
n2_v0 = np.array([u_1_0])

ir_1_0 = n1_v0 / r_1_r
i_r_line_0 = (n1_v0 - n2_v0) / r_line_r

r_1.set_intf_voltage(n1_v0)
Expand Down Expand Up @@ -219,7 +227,10 @@ def set_dpsim2(t_s, t_f, vs_cond, logger_prefix):
y_1 = y_1_0

# We have to assume the trajectory of y_2 extending its initial value, since we have no prior information
y_1_m_prev = np.tile(y_1_0, m)
# y_1_m_prev = np.tile(y_1_0, m)

# This one as computed in Matlab
y_1_m_prev = np.array([30.108, y_1_0])

for i in range(0, N):
y_1_prev = y_1_m_prev[-1]
Expand Down

0 comments on commit df8002b

Please sign in to comment.