Commit 21ac462a authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

examples: fixing shmem examples

parent 2de81fac
......@@ -67,13 +67,13 @@ if(WITH_SHMEM)
list(APPEND INCLUDE_DIRS ${VILLASNODE_INCLUDE_DIRS})
set(SHMEM_SOURCES
#Shmem/ShmemExample.cpp
#Shmem/ShmemDistributedReference.cpp
#Shmem/ShmemDistributedDirect.cpp
#Shmem/ShmemDistributedVillas.cpp
#Shmem/ShmemControllableSource.cpp
#Shmem/ShmemControllableFiltSource.cpp
#Shmem/ShmemRealTime.cpp
Shmem/ShmemExample.cpp
Shmem/ShmemDistributedReference.cpp
Shmem/ShmemDistributedDirect.cpp
Shmem/ShmemDistributedVillas.cpp
Shmem/ShmemControllableSource.cpp
Shmem/ShmemControllableFiltSource.cpp
Shmem/ShmemRealTime.cpp
)
endif()
......
......@@ -22,13 +22,13 @@
#include <DPsim.h>
using namespace DPsim;
using namespace CPS;
using namespace CPS::DP::Ph1;
using namespace CPS::Signal;
using namespace CPS::DP;
using namespace CPS::DP::Ph1;
int main(int argc, char *argv[]) {
Interface::Config conf;
CPS::Interface::Config conf;
conf.samplelen = 64;
conf.queuelen = 1024;
conf.polling = false;
......@@ -40,7 +40,7 @@ int main(int argc, char *argv[]) {
Real finalTime = 10;
String simName = "ShmemControllableSource";
Interface intf(out, in, &conf);
CPS::Interface intf(out, in, &conf);
// Controllers and filter
std::vector<Real> coefficients = { -0.0024229,-0.0020832,0.0067703,0.016732,
......@@ -64,9 +64,9 @@ int main(int argc, char *argv[]) {
auto r1 = Resistor::make("r_1", 1);
auto load = PQLoadCS::make("load_cs", 10., 0., 10.);
ecs->connect({GND, n1});
r1->connect({GND, n1});
load->connect({n1});
ecs->connect({ Node::GND, n1 });
r1->connect({ Node::GND, n1 });
load->connect({ n1 });
filtP->setConnection(load->findAttribute<Real>("active_power"));
filtQ->setConnection(load->findAttribute<Real>("reactive_power"));
......
......@@ -22,11 +22,12 @@
#include <DPsim.h>
using namespace DPsim;
using namespace CPS::DP;
using namespace CPS::DP::Ph1;
int main(int argc, char *argv[]) {
Interface::Config conf;
CPS::Interface::Config conf;
conf.samplelen = 64;
conf.queuelen = 1024;
conf.polling = false;
......@@ -38,7 +39,7 @@ int main(int argc, char *argv[]) {
Real finalTime = 10;
String simName = "ShmemControllableSource";
Interface intf(out, in, &conf);
CPS::Interface intf(out, in, &conf);
// Nodes
auto n1 = Node::make("n1");
......@@ -47,8 +48,8 @@ int main(int argc, char *argv[]) {
auto ecs = CurrentSource::make("v_intf", Complex(10, 0));
auto r1 = Resistor::make("r_1", 1);
ecs->connect({GND, n1});
r1->connect({GND, n1});
ecs->connect({ Node::GND, n1 });
r1->connect({ Node::GND, n1 });
intf.addImport(ecs->findAttribute<Complex>("current_ref"), 1.0, 0, 1);
intf.addExport(ecs->findAttribute<Complex>("comp_voltage"), 1.0, 0, 1);
......
......@@ -22,6 +22,7 @@
#include <DPsim.h>
using namespace DPsim;
using namespace CPS::DP;
using namespace CPS::DP::Ph1;
int main(int argc, char *argv[]) {
......@@ -33,7 +34,7 @@ int main(int argc, char *argv[]) {
// Here, the two instances directly communicate with each other without using
// VILLASnode in between.
Interface::Config conf;
CPS::Interface::Config conf;
conf.samplelen = 4;
conf.queuelen = 1024;
conf.polling = false;
......@@ -54,7 +55,7 @@ int main(int argc, char *argv[]) {
out = "/dpsim10";
}
Interface intf(in, out, &conf);
auto intf = CPS::Interface(in, out, &conf);
Real timeStep = 0.000150;
......@@ -64,13 +65,16 @@ int main(int argc, char *argv[]) {
auto n2 = Node::make("n2");
// Components
auto evs = VoltageSource::make("v_intf", Complex(5, 0), Logger::Level::DEBUG);
auto vs1 = VoltageSource::make("vs_1", Complex(10, 0), Logger::Level::DEBUG);
auto evs = VoltageSource::make("v_intf", Logger::Level::DEBUG);
auto vs1 = VoltageSource::make("vs_1", Logger::Level::DEBUG);
auto r01 = Resistor::make("r_0_1", 1, Logger::Level::DEBUG);
evs->connect({GND, n2});
vs1->connect({GND, n1});
r01->connect({n1, n2});
evs->setParameters(Complex(5, 0));
vs1->setParameters(Complex(10, 0));
evs->connect({ Node::GND, n2 });
vs1->connect({ Node::GND, n1 });
r01->connect({ n1, n2 });
intf.addImport(evs->findAttribute<Complex>("voltage_ref"), 1.0, 0, 1);
intf.addExport(evs->findAttribute<Complex>("comp_current"), 1.0, 0, 1);
......@@ -89,8 +93,8 @@ int main(int argc, char *argv[]) {
auto ecs = CurrentSource::make("i_intf", Complex(5, 0), Logger::Level::DEBUG);
auto rgnd0 = Resistor::make("r_gnd_0", 1, Logger::Level::DEBUG);
ecs->connect({GND, n1});
rgnd0->connect({GND, n1});
ecs->connect({ Node::GND, n1 });
rgnd0->connect({ Node::GND, n1 });
//auto ecs_switch = CurrentSource::make("i_switch", GND, 1, Complex(0, 0));
//auto r01 = Resistor::make("r_0_1", 0, 1, 1);
......
......@@ -22,6 +22,7 @@
#include <DPsim.h>
using namespace DPsim;
using namespace CPS::DP;
using namespace CPS::DP::Ph1;
int main(int argc, char* argv[])
......@@ -38,13 +39,13 @@ int main(int argc, char* argv[])
auto r2A = Resistor::make("r_2", 10);
auto r2B = Resistor::make("r_2", 8);
vs->connect({GND, n1});
l1->connect({n1, n2});
r1->connect({n2, n3});
r2A->connect({n3, GND});
r2B->connect({n3, GND});
vs->connect({ Node::GND, n1 });
l1->connect({ n1, n2 });
r1->connect({ n2, n3 });
r2A->connect({ n3, Node::GND });
r2B->connect({ n3, Node::GND });
auto nodes = SystemNodeList{GND, n1, n2, n3};
auto nodes = SystemNodeList{Node::GND, n1, n2, n3};
auto sys1 = SystemTopology(50, nodes, SystemComponentList{vs, l1, r1, r2A});
auto sys2 = SystemTopology(50, nodes, SystemComponentList{vs, l1, r1, r2B});
......
......@@ -22,6 +22,7 @@
#include <DPsim.h>
using namespace DPsim;
using namespace CPS::DP;
using namespace CPS::DP::Ph1;
int main(int argc, char *argv[])
......@@ -29,7 +30,7 @@ int main(int argc, char *argv[])
SystemComponentList comps, comps2;
SystemNodeList nodes;
Interface::Config conf;
CPS::Interface::Config conf;
conf.samplelen = 4;
conf.queuelen = 1024;
conf.polling = true;
......@@ -50,7 +51,7 @@ int main(int argc, char *argv[])
out = "/villas1-out";
}
Interface intf(in, out, &conf);
auto intf = CPS::Interface(in, out, &conf);
if (String(argv[1]) == "0") {
// Nodes
......@@ -59,13 +60,25 @@ int main(int argc, char *argv[])
auto n3 = Node::make("n3");
// Components
auto evs = VoltageSource::make("v_t", SystemNodeList{GND, n3}, Complex(0, 0));
auto vs = VoltageSourceNorton::make("v_s", SystemNodeList{GND, n1}, Complex(10000, 0), 1);
auto l1 = Inductor::make("l_1", SystemNodeList{n1, n2}, 0.1);
auto r1 = Resistor::make("r_1", SystemNodeList{n2, n3}, 1);
auto evs = VoltageSource::make("v_t");
auto vs = VoltageSourceNorton::make("v_s");
auto l1 = Inductor::make("l_1");
auto r1 = Resistor::make("r_1");
// Topology
evs->connect({ Node::GND, n3 });
vs->connect({ Node::GND, n1 });
l1->connect({ n1, n2 });
r1->connect({ n2, n3 });
// Parameters
evs->setParameters(Complex(0, 0));
vs->setParameters(Complex(10000, 0), 1);
l1->setParameters(0.1);
r1->setParameters(1);
comps = SystemComponentList{evs, vs, l1, r1};
nodes = SystemNodeList{GND, n1, n2, n3};
nodes = SystemNodeList{Node::GND, n1, n2, n3};
intf.addImport(evs->findAttribute<Complex>("voltage_ref"), 1.0, 0, 1);
intf.addExport(evs->findAttribute<Complex>("comp_current"), 1.0, 0, 1);
......@@ -75,13 +88,23 @@ int main(int argc, char *argv[])
auto n4 = Node::make("n4");
// Components
auto ecs = CurrentSource::make("v_s", SystemNodeList{GND, n4}, Complex(0, 0));
auto r2A = Resistor::make("r_2", SystemNodeList{GND, n4}, 10);
auto r2B = Resistor::make("r_2", SystemNodeList{GND, n4}, 8);
auto ecs = CurrentSource::make("v_s");
auto r2A = Resistor::make("r_2");
auto r2B = Resistor::make("r_2");
// Topology
ecs->connect({ Node::GND, n4 });
r2A->connect({ Node::GND, n4 });
r2B->connect({ Node::GND, n4 });
// Parameters
ecs->setParameters(Complex(0, 0));
r2A->setParameters(10);
r2B->setParameters(8);
comps = SystemComponentList{ecs, r2A};
comps2 = SystemComponentList{ecs, r2B};
nodes = SystemNodeList{GND, n4};
nodes = SystemNodeList{Node::GND, n4};
intf.addImport(ecs->findAttribute<Complex>("current_ref"), 1.0, 0, 1);
intf.addExport(ecs->findAttribute<Complex>("comp_voltage"), 1.0, 0, 1);
......
......@@ -22,6 +22,7 @@
#include <DPsim.h>
using namespace DPsim;
using namespace CPS::DP;
using namespace CPS::DP::Ph1;
int main(int argc, char* argv[])
......@@ -36,21 +37,29 @@ int main(int argc, char* argv[])
auto n4 = Node::make("n4");
// Components
auto evs = VoltageSource::make("v_s", Complex(0, 0));
auto rs = Resistor::make("r_s", 1);
auto rl = Resistor::make("r_line", 1);
auto ll = Inductor::make("l_line", 1);
auto rL = Resistor::make("r_load", 1000);
auto evs = VoltageSource::make("v_s");
auto rs = Resistor::make("r_s");
auto rl = Resistor::make("r_line");
auto ll = Inductor::make("l_line");
auto rL = Resistor::make("r_load");
evs->connect({GND, n1});
rs->connect({n1, n2});
rl->connect({n2, n3});
ll->connect({n3, n4});
rL->connect({n4, GND});
// Topology
evs->connect({ Node::GND, n1 });
rs->connect({ n1, n2 });
rl->connect({ n2, n3 });
ll->connect({ n3, n4 });
rL->connect({ n4, Node::GND });
auto sys = SystemTopology(50, SystemNodeList{GND, n1, n2, n3, n4}, SystemComponentList{evs, rs, rl, ll, rL});
// Parameters
evs->setParameters(Complex(0, 0));
rs->setParameters(1);
rl->setParameters(1);
ll->setParameters(1);
rL->setParameters(1000);
auto intf = Interface("/villas1-in", "/villas1-out");
auto sys = SystemTopology(50, SystemNodeList{Node::GND, n1, n2, n3, n4}, SystemComponentList{evs, rs, rl, ll, rL});
auto intf = CPS::Interface("/villas1-in", "/villas1-out");
intf.addImport(evs->findAttribute<Complex>("voltage_ref"), 1.0, 0, 1);
intf.addExport(evs->findAttribute<Complex>("comp_current"), 1.0, 0, 1);
......
......@@ -22,6 +22,7 @@
#include <DPsim.h>
using namespace DPsim;
using namespace CPS::DP;
using namespace CPS::DP::Ph1;
int main(int argc, char* argv[])
......@@ -29,7 +30,7 @@ int main(int argc, char* argv[])
// Same circuit as above, but now with realtime support.
SystemComponentList comps;
Interface::Config conf;
CPS::Interface::Config conf;
conf.samplelen = 4;
conf.queuelen = 1024;
conf.polling = false;
......@@ -41,24 +42,32 @@ int main(int argc, char* argv[])
auto n4 = Node::make("n4");
// Components
auto evs = VoltageSource::make("v_s", Complex(0, 0));
auto rs = Resistor::make("r_s", 1);
auto rl = Resistor::make("r_line", 1);
auto ll = Inductor::make("l_line", 1);
auto rL = Resistor::make("r_load", 1000);
auto evs = VoltageSource::make("v_s");
auto rs = Resistor::make("r_s");
auto rl = Resistor::make("r_line");
auto ll = Inductor::make("l_line");
auto rL = Resistor::make("r_load");
evs->connect({GND, n1});
rs->connect({n1, n2});
rl->connect({n2, n3});
ll->connect({n3, n4});
rL->connect({n4, GND});
// Topology
evs->connect({ Node::GND, n1 });
rs->connect({ n1, n2 });
rl->connect({ n2, n3 });
ll->connect({ n3, n4 });
rL->connect({ n4, Node::GND });
Interface intf("/villas1-in", "/villas1-out", &conf);
// Parameters
evs->setParameters(Complex(0, 0));
rs->setParameters(1);
rl->setParameters(1);
ll->setParameters(1);
rL->setParameters(1000);
auto intf = CPS::Interface("/villas1-in", "/villas1-out", &conf);
intf.addImport(evs->findAttribute<Complex>("voltage_ref"), 1.0, 0, 1);
intf.addExport(evs->findAttribute<Complex>("comp_current"), 1.0, 0, 1);
Real timeStep = 0.001;
auto sys = SystemTopology(50, SystemNodeList{GND, n1, n2, n3, n4}, SystemComponentList{evs, rs, rl, ll, rL});
auto sys = SystemTopology(50, SystemNodeList{Node::GND, n1, n2, n3, n4}, SystemComponentList{evs, rs, rl, ll, rL});
auto sim = RealTimeSimulation("ShmemRealTime", sys, timeStep, 5.0);
sim.addInterface(&intf, false, true);
......
import dpsim
import pytest
import asyncio
import time
import datetime as dt
import logging
from dpsim.Event import Event
def my_callback(event, sim, myvar):
assert myvar == 1337
print("Received Event: %d" % event)
if event == EventChannel.Event.stopped:
print("Simulation stopped")
if event == EventChannel.Event.starting:
print("Simulation starting")
if event == EventChannel.Event.running:
print("Simulation running")
if event == EventChannel.Event.pausing:
print("Simulation pausing")
if event == EventChannel.Event.paused:
print("Simulation paused")
if event == EventChannel.Event.resuming:
print("Simulation resuming")
if event == EventChannel.Event.stopping:
print("Simulation stopping")
if event == EventChannel.Event.failed:
print("Simulation failed")
if event == EventChannel.Event.overrun:
print("Simulation overrun")
if event == EventChannel.Event.done:
print("Simulation done")
if event in [ EventChannel.Event.stopped, EventChannel.Event.stopped, EventChannel.Event.failed, EventChannel.Event.overrun ]:
print("Received Event: %s" % event)
if event in [ Event.done, Event.stopped, Event.stopped, Event.failed, Event.overrun ]:
el = asyncio.get_event_loop()
el.stop()
def test_async():
el = asyncio.get_event_loop()
......@@ -46,12 +26,14 @@ def test_async():
v1 = dpsim.dp.ph1.VoltageSource("v_1", [gnd, n1], voltage_ref=10)
r1 = dpsim.dp.ph1.Resistor("r_1", [n1, gnd], resistance=1)
system = dpsim.SystemTopology(20, [gnd, n1], [v1, r1])
system = dpsim.SystemTopology(50, [gnd, n1], [v1, r1])
sim = dpsim.RealTimeSimulation('async', system, duration=20, timestep=0.0005)
start = dt.datetime.now() + dt.timedelta(seconds=4)
sim = dpsim.RealTimeSimulation(__name__, system, duration=10, timestep=0.0005)#, start_time=start)
# Start in two seconds!
sim.start(when=time.time() + 2)
sim.start()
sim.show_progressbar()
sim.add_callback(my_callback, sim, 1337)
......@@ -63,3 +45,6 @@ def test_async():
el.call_at(el.time() + 10, sim.start)
el.run_forever()
if __name__ == "__main__":
test_async()
import os
import dpsim
import dataprocessing.readtools as rt
import dataprocessing.timeseries as ts
PATH = os.path.dirname(__file__)
def test_IdealVS_RXLine_1_RL():
def test_circuit():
# Nodes
gnd = dpsim.dp.Node.GND()
n1 = dpsim.dp.Node("n1")
......@@ -20,7 +18,7 @@ def test_IdealVS_RXLine_1_RL():
system = dpsim.SystemTopology(50, [gnd, n1, n2, n3], [v1, lL, rL, r1])
sim = dpsim.Simulation('IdealVS_RXLine_1_RL', system, duration=0.2, timestep=0.00005)
sim = dpsim.Simulation(__name__, system, duration=0.2, timestep=0.0005)
sim.run()
#results = rt.read_timeseries_dpsim_cmpl('Logs/' + sim.name + '_LeftVector.csv')
......@@ -33,3 +31,6 @@ def test_IdealVS_RXLine_1_RL():
print("Total RMSE: %g" % (err))
assert err < 1e-4
if __name__ == "__main__":
test_circuit()
import dpsim
import datetime as dt
from dpsim.Event import Event
def test_realtime():
# Nodes
gnd = dpsim.dp.Node.GND()
n1 = dpsim.dp.Node("n1")
# Components
v1 = dpsim.dp.ph1.VoltageSource("v_1", [gnd, n1], voltage_ref=10)
r1 = dpsim.dp.ph1.Resistor("r_1", [n1, gnd], resistance=1)
system = dpsim.SystemTopology(50, [gnd, n1], [v1, r1])
start = dt.datetime.now() + dt.timedelta(seconds=4)
print(repr(start))
sim = dpsim.RealTimeSimulation(__name__, system, duration=10, timestep=0.001, start_time=start)
sim.show_progressbar()
sim.run(pbar=True)
if __name__ == "__main__":
test_realtime()
import dpsim
from dpsim.Event import Event
import logging
from dpsim.Event import Event
def test_simulation():
logging.getLogger().setLevel(logging.DEBUG)
logging.info("hello\n")
n1 = dpsim.dp.Node("n1")
gnd = dpsim.dp.Node.GND()
r = dpsim.dp.ph1.Resistor("r1", [gnd, n1])
sys = dpsim.SystemTopology(50, [n1], [r])
sim = dpsim.Simulation(__name__, sys, duration=10, pbar=True)
sim.start()
assert sim.wait_until() == Event.starting
assert sim.wait_until() == Event.running
sim.pause()
assert sim.wait_until() == Event.pausing
assert sim.wait_until() == Event.paused
steps_start = sim.steps
while sim.steps < steps_start + 100:
steps_before = sim.steps
sim.step()
assert sim.wait_until() == Event.resuming
assert sim.wait_until() == Event.running
assert sim.wait_until() == Event.paused
assert steps_before + 1 == sim.steps
sim.start()
assert sim.wait_until() == Event.resuming
assert sim.wait_until() == Event.running
sim.stop()
assert sim.wait_until() == Event.stopping
assert sim.wait_until() == Event.stopped
if __name__ == "__main__":
test_simulation()
import dpsim
from dpsim.Event import Event
import logging
from dpsim.Event import Event
def test_simulation():
logging.getLogger().setLevel(logging.DEBUG)
logging.info("hello\n")
n1 = dpsim.dp.Node("n1")
gnd = dpsim.dp.Node.GND()
r = dpsim.dp.ph1.Resistor("r1", [gnd, n1])
sys = dpsim.SystemTopology(50, [n1], [r])
sim = dpsim.Simulation(__name__, sys, duration=10, rt=True, pbar=True, single_stepping=True)
sim.step()
assert sim.wait_until() == Event.starting
assert sim.wait_until() == Event.running
assert sim.wait_until() == Event.paused
for x in range(2,10):
sim.step()
assert sim.wait_until() == Event.resuming
assert sim.wait_until() == Event.running
assert sim.wait_until() == Event.paused