Commit 9eaae741 authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

Merge branch 'rt-exceptions' into development

Fixes #8.
parents 40170493 09dd8795
......@@ -2,7 +2,6 @@
#include <string>
#include "NetlistSim.h"
#include "Examples/SynchronGenUnitTest.h"
#include "Examples/TestSequenceICCEP.h"
#include "Examples/ReferenceCircuits.h"
#include "Examples/ShmemTest.h"
#include <complex>
......@@ -13,8 +12,8 @@ int main(int argc, char* argv[]) {
//shmemExample();
//shmemDistributedExample(argc, argv);
//shmemRTExample();
//shmemDistributed(argc, argv);
//shmemDistributedRef();
/*simulationExample1();
simulationExample2();
......
......@@ -41,7 +41,42 @@ void DPsim::shmemExample()
delete villas;
}
void DPsim::shmemDistributedExample(int argc, char *argv[])
void DPsim::shmemRTExample()
{
// Same circuit as above, but now with realtime support.
std::vector<BaseComponent*> comps;
struct shmem_conf conf;
conf.samplelen = 4;
conf.queuelen = 1024;
conf.polling = false;
Logger log;
ExternalVoltageSource *evs = new ExternalVoltageSource("v_s", 1, 0, 0, 0, 1);
comps.push_back(evs);
comps.push_back(new LinearResistor("r_s", 1, 2, 1));
comps.push_back(new LinearResistor("r_line", 2, 3, 1));
comps.push_back(new Inductor("l_line", 3, 4, 1));
comps.push_back(new LinearResistor("r_load", 4, 0, 1000));
ShmemInterface *villas = new ShmemInterface("/villas1-in", "/villas1-out", &conf);
villas->registerVoltageSource(evs, 0, 1);
villas->registerExportedCurrent(evs, 0, 1);
// Set up simulation
Real timeStep = 0.001;
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 5.0, log);
newSim.addExternalInterface(villas);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
newSim.runRT(RTExceptions, false, log, log, log);
std::cout << "Simulation finished." << std::endl;
for (auto comp : comps) {
delete comp;
}
delete villas;
}
void DPsim::shmemDistributedDirect(int argc, char *argv[])
{
// Testing the interface with a simple circuit,
// but the load is simulated in a different instance.
......@@ -50,9 +85,13 @@ void DPsim::shmemDistributedExample(int argc, char *argv[])
// supply side, whose values are received from the respective other circuit.
// Here, the two instances directly communicate with each other without using
// VILLASnode in between.
Logger log("output.log"), llog("lvector.log"), rlog("rvector.log");
Logger log;
std::vector<BaseComponent*> comps;
ShmemInterface *shmem;
struct shmem_conf conf;
conf.samplelen = 4;
conf.queuelen = 1024;
conf.polling = false;
if (argc < 2) {
std::cerr << "not enough arguments (either 0 or 1 for the test number)" << std::endl;
......@@ -64,14 +103,14 @@ void DPsim::shmemDistributedExample(int argc, char *argv[])
comps.push_back(new Inductor("l_1", 1, 2, 1e-3));
ExternalVoltageSource *evs = new ExternalVoltageSource("v_t", 2, 0, 0, 0, 1);
comps.push_back(evs);
shmem = new ShmemInterface("/dpsim01", "/dpsim10");
shmem = new ShmemInterface("/dpsim01", "/dpsim10", &conf);
shmem->registerVoltageSource(evs, 0, 1);
shmem->registerExportedCurrent(evs, 0, 1);
} else if (!strcmp(argv[1], "1")) {
ExternalCurrentSource *ecs = new ExternalCurrentSource("v_s", 1, 0, 0, 0);
comps.push_back(ecs);
comps.push_back(new LinearResistor("r_2", 1, 0, 1));
shmem = new ShmemInterface("/dpsim10", "/dpsim01");
shmem = new ShmemInterface("/dpsim10", "/dpsim01", &conf);
shmem->registerCurrentSource(ecs, 0, 1);
shmem->registerExportedVoltage(1, 0, 0, 1);
} else {
......@@ -80,19 +119,80 @@ void DPsim::shmemDistributedExample(int argc, char *argv[])
}
// Set up simulation
Real timeStep = 0.001;
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 0.3, log);
Real timeStep = 0.000150;
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 1, log);
newSim.addExternalInterface(shmem);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, llog, rlog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
newSim.runRT(RTTimerFD, false, log, log, log);
std::cout << "Simulation finished." << std::endl;
for (auto comp : comps) {
delete comp;
}
delete shmem;
}
void DPsim::shmemDistributed(int argc, char *argv[])
{
Logger log;
std::vector<BaseComponent*> comps, comps2;
ShmemInterface *shmem;
struct shmem_conf conf;
std::string logname;
conf.samplelen = 4;
conf.queuelen = 1024;
conf.polling = true;
if (argc < 2) {
std::cerr << "not enough arguments (either 0 or 1 for the test number)" << std::endl;
std::exit(1);
}
if (!strcmp(argv[1], "0")) {
logname = "lvector0.log";
comps.push_back(new VoltSourceRes("v_s", 1, 0, 10000, 0, 1));
comps.push_back(new Inductor("l_1", 1, 2, 0.1));
comps.push_back(new LinearResistor("r_1", 2, 3, 1));
ExternalVoltageSource *evs = new ExternalVoltageSource("v_t", 3, 0, 0, 0, 1);
comps.push_back(evs);
shmem = new ShmemInterface("/villas1-in", "/villas1-out", &conf);
shmem->registerVoltageSource(evs, 0, 1);
shmem->registerExportedCurrent(evs, 0, 1);
} else if (!strcmp(argv[1], "1")) {
logname = "lvector1.log";
ExternalCurrentSource *ecs = new ExternalCurrentSource("v_s", 1, 0, 0, 0);
comps.push_back(ecs);
comps.push_back(new LinearResistor("r_2", 1, 0, 10));
shmem = new ShmemInterface("/villas2-in", "/villas2-out", &conf);
shmem->registerCurrentSource(ecs, 0, 1);
shmem->registerExportedVoltage(1, 0, 0, 1);
} else {
std::cerr << "invalid test number" << std::endl;
std::exit(1);
}
// Set up simulation
Real timeStep = 0.001000;
Logger llog(logname);
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 20, log);
newSim.addExternalInterface(shmem);
if (!strcmp(argv[1], "1")) {
comps2 = comps;
comps2.pop_back();
comps2.push_back(new LinearResistor("r_2", 1, 0, 8));
newSim.addSystemTopology(comps2);
newSim.setSwitchTime(10, 1);
}
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
newSim.runRT(RTTimerFD, true, log, llog, log);
std::cout << "Simulation finished." << std::endl;
if (!strcmp(argv[1], "1"))
delete comps2.back();
for (auto comp : comps) {
delete comp;
}
......@@ -103,15 +203,20 @@ void DPsim::shmemDistributedRef()
{
// Same circuit as above, but the simulation is done normally in one instance.
Logger log("output.log"), llog("lvector.log"), rlog("rvector.log");
std::vector<BaseComponent*> comps;
std::vector<BaseComponent*> comps, comps2;
comps.push_back(new VoltSourceRes("v_s", 1, 0, 10000, 0, 1));
comps.push_back(new Inductor("l_1", 1, 2, 1e-3));
comps.push_back(new LinearResistor("r_2", 2, 0, 1));
comps.push_back(new Inductor("l_1", 1, 2, 0.1));
comps.push_back(new LinearResistor("r_1", 2, 3, 1));
comps2 = comps;
comps.push_back(new LinearResistor("r_2", 3, 0, 10));
comps2.push_back(new LinearResistor("r_2", 3, 0, 8));
// Set up simulation
Real timeStep = 0.001;
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 0.3, log);
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 20, log);
newSim.addSystemTopology(comps2);
newSim.setSwitchTime(10, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
......
......@@ -4,7 +4,9 @@
namespace DPsim {
void shmemExample();
void shmemDistributedExample(int argc, char *argv[]);
void shmemRTExample();
void shmemDistributed(int argc, char *argv[]);
void shmemDistributedDirect(int argc, char *argv[]);
void shmemDistributedRef();
}
......
#include "SimpleCircuitTest.h"
#include "../Simulation.h"
#include "../Utilities.h"
......@@ -209,3 +210,24 @@ void DPsim::runDpEmtVarFreqStudy() {
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
}
void DPsim::RTExample() {
std::vector<BaseComponent*> comps;
Logger log;
comps.push_back(new VoltSourceRes("v_s", 1, 0, 10000, 0, 1));
comps.push_back(new LinearResistor("r_line", 1, 2, 1));
comps.push_back(new Inductor("l_line", 2, 3, 1));
comps.push_back(new LinearResistor("r_load", 3, 0, 1000));
// Set up simulation
Real timeStep = 0.00005;
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 1.0, log);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
newSim.runRT(RTExceptions, false, log, log, log);
std::cout << "Simulation finished." << std::endl;
for (auto comp : comps) {
delete comp;
}
}
#include "../Simulation.h"
#include "../Utilities.h"
using namespace DPsim;
void RXLineResLoad() {
Real timeStep = 0.001;
// Define Object for saving data on a file
std::ostringstream fileName;
fileName << "RXLineResLoad_" << timeStep;
Logger log("Logs/Log_" + fileName.str() + ".log"),
leftVectorLog("Logs/LeftVectorLog_" + fileName.str() + ".csv"),
rightVectorLog("Logs/RightVectorLog_" + fileName.str() + ".csv");
// Declare circuit components
std::vector<BaseComponent*> circElements0, circElements1, circElements2;
circElements0.push_back(new VoltSourceRes("v_s", 1, 0, 10000, 0, 1));
circElements0.push_back(new LinearResistor("r_line", 1, 2, 1));
circElements0.push_back(new Inductor("l_line", 2, 3, 1));
circElements1 = circElements0;
circElements2 = circElements0;
circElements1.push_back(new LinearResistor("r_load", 3, 0, 1000));
circElements2.push_back(new LinearResistor("r_load", 3, 0, 800));
// Set up simulation
Simulation newSim(circElements1, 2.0*M_PI*50.0, timeStep, 0.3, log);
newSim.addSystemTopology(circElements2);
newSim.setSwitchTime(0.1, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, leftVectorLog, rightVectorLog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
for (auto elem : circElements0)
delete elem;
delete circElements1[3];
delete circElements2[3];
}
void VarFreqRXLineResLoad(Real timeStep, Real finalTime, Real freqStep, Real loadStep, Real rampTime) {
// Define Object for saving data on a file
std::ostringstream fileName;
fileName << "VarFreqRXLineResLoad_" << timeStep;
Logger log("Logs/Log_" + fileName.str() + ".log"),
leftVectorLog("Logs/LeftVectorLog_" + fileName.str() + ".csv"),
rightVectorLog("Logs/RightVectorLog_" + fileName.str() + ".csv");
// Declare circuit components
std::vector<BaseComponent*> circElements0, circElements1, circElements2;
circElements0.push_back(new VoltSourceResFreq("v_s", 1, 0, 1000, 0, 1, 2*PI*-5, freqStep, rampTime));
circElements0.push_back(new LinearResistor("r_line", 1, 2, 1));
circElements0.push_back(new Inductor("l_line", 2, 3, 0.2));
circElements1 = circElements0;
circElements2 = circElements0;
circElements1.push_back(new LinearResistor("r_load", 3, 0, 100));
circElements2.push_back(new LinearResistor("r_load", 3, 0, 50));
// Set up simulation
Simulation newSim(circElements1, 2.0*PI*50.0, timeStep, finalTime, log);
newSim.addSystemTopology(circElements2);
newSim.setSwitchTime(loadStep, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, leftVectorLog, rightVectorLog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
for (auto elem : circElements0)
delete elem;
delete circElements1[3];
delete circElements2[3];
}
void RXLineResLoadEMT() {
Real timeStep = 0.001;
// Define Object for saving data on a file
std::ostringstream fileName;
fileName << "RXLineResLoadEMT_" << timeStep;
Logger log("Logs/Log_" + fileName.str() + ".log"),
leftVectorLog("Logs/LeftVectorLog_" + fileName.str() + ".csv"),
rightVectorLog("Logs/RightVectorLog_" + fileName.str() + ".csv");
// Declare circuit components
std::vector<BaseComponent*> circElements0, circElements1, circElements2;
circElements0.push_back(new VoltSourceResEMT("v_s", 1, 0, 10000, 0, 1));
circElements0.push_back(new LinearResistorEMT("r_line", 1, 2, 1));
circElements0.push_back(new InductorEMT("l_line", 2, 3, 1));
circElements1 = circElements0;
circElements2 = circElements0;
circElements1.push_back(new LinearResistorEMT("r_load", 3, 0, 1000));
circElements2.push_back(new LinearResistorEMT("r_load", 3, 0, 800));
// Set up simulation
Simulation newSim(circElements1, 2.0*PI*50.0, timeStep, 0.3, log, SimulationType::EMT);
newSim.addSystemTopology(circElements2);
newSim.setSwitchTime(0.1, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, leftVectorLog, rightVectorLog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
for (auto elem : circElements0)
delete elem;
delete circElements1[3];
delete circElements2[3];
}
void VarFreqRXLineResLoadEMT(Real timeStep, Real finalTime, Real freqStep, Real loadStep, Real rampTime) {
// Define Object for saving data on a file
std::ostringstream fileName;
fileName << "VarFreqRXLineResLoadEMT_" << timeStep;
Logger log("Logs/Log_" + fileName.str() + ".log"),
leftVectorLog("Logs/LeftVectorLog_" + fileName.str() + ".csv"),
rightVectorLog("Logs/RightVectorLog_" + fileName.str() + ".csv");
// Declare circuit components
std::vector<BaseComponent*> circElements0, circElements1, circElements2;
circElements0.push_back(new VoltSourceResFreqEMT("v_s", 1, 0, 1000, 0, 1, 2 * PI*-5, freqStep, rampTime));
circElements0.push_back(new LinearResistorEMT("r_line", 1, 2, 1));
circElements0.push_back(new InductorEMT("l_line", 2, 3, 0.2));
circElements1 = circElements0;
circElements2 = circElements0;
circElements1.push_back(new LinearResistorEMT("r_load", 3, 0, 100));
circElements2.push_back(new LinearResistorEMT("r_load", 3, 0, 50));
// Set up simulation
Simulation newSim(circElements1, 2.0*PI*50.0, timeStep, finalTime, log, SimulationType::EMT);
newSim.addSystemTopology(circElements2);
newSim.setSwitchTime(loadStep, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, leftVectorLog, rightVectorLog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
for (auto elem : circElements0)
delete elem;
delete circElements1[3];
delete circElements2[3];
}
void runDpEmtVarFreqStudy() {
Real timeStep = 0.0;
Real finalTime = 0.6;
Real freqStep = 0.4;
Real loadStep = 0.2;
Real rampTime = 0;
timeStep = 0.00005;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.001;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.005;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.01;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.015;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.02;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.025;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.03;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.035;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
timeStep = 0.04;
VarFreqRXLineResLoadEMT(timeStep, finalTime, freqStep, loadStep, rampTime);
VarFreqRXLineResLoad(timeStep, finalTime, freqStep, loadStep, rampTime);
}
#ifndef SIMPLECIRCUIT_H
#define SIMPLECIRCUIT_H
#include "../MathLibrary.h"
namespace DPsim {
void RXLineResLoad();
void VarFreqRXLineResLoad(Real timeStep, Real finalTime, Real freqStep, Real loadStep, Real rampTime);
void RXLineResLoadEMT();
void VarFreqRXLineResLoadEMT(Real timeStep, Real finalTime, Real freqStep, Real loadStep, Real rampTime);
void runDpEmtVarFreqStudy();
void RXLineResLoadStatic();
void RTExample();
};
#endif
#ifndef TESTSEQUENCEICEEP_H
#define TESTSEQUENCEICEEP_H
#include "../MathLibrary.h"
#include "../Simulation.h"
#include "../Utilities.h"
namespace DPsim {
void RXLineResLoad() {
// Define Object for saving data on a file
Logger log, leftVectorLog, rightVectorLog;
// Declare circuit components
std::vector<BaseComponent*> circElements0, circElements1, circElements2;
circElements0.push_back(new VoltSourceRes("v_s", 1, 0, 10000, 0, 1));
circElements0.push_back(new LinearResistor("r_line", 1, 2, 1));
circElements0.push_back(new Inductor("l_line", 2, 3, 1));
circElements1 = circElements0;
circElements2 = circElements0;
circElements1.push_back(new LinearResistor("r_load", 3, 0, 1000));
circElements2.push_back(new LinearResistor("r_load", 3, 0, 800));
// Set up simulation
Real timeStep = 0.001;
Simulation newSim(circElements1, 2.0*M_PI*50.0, timeStep, 0.3, log);
newSim.addSystemTopology(circElements2);
newSim.setSwitchTime(0.1, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, leftVectorLog, rightVectorLog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
// Write simulation data to file
std::ostringstream fileName;
fileName << "RXLineResLoad_" << timeStep;
//log.WriteLogToFile("Logs/Log_" + fileName.str() + ".log");
//leftVectorLog.WriteLogToFile("Logs/LeftVectorLog_" + fileName.str() + ".csv");
//rightVectorLog.WriteLogToFile("Logs/RightVectorLog_" + fileName.str() + ".csv");
}
void VarFreqRXLineResLoad(Real timeStep, Real finalTime, Real freqStep, Real loadStep, Real rampTime) {
// Define Object for saving data on a file
Logger log, leftVectorLog, rightVectorLog;
// Declare circuit components
std::vector<BaseComponent*> circElements0, circElements1, circElements2;
circElements0.push_back(new VoltSourceResFreq("v_s", 1, 0, 1000, 0, 1, 2 * PI*-5, freqStep, rampTime));
circElements0.push_back(new LinearResistor("r_line", 1, 2, 1));
circElements0.push_back(new Inductor("l_line", 2, 3, 0.2));
circElements1 = circElements0;
circElements2 = circElements0;
circElements1.push_back(new LinearResistor("r_load", 3, 0, 100));
circElements2.push_back(new LinearResistor("r_load", 3, 0, 50));
// Set up simulation
Simulation newSim(circElements1, 2.0*PI*50.0, timeStep, finalTime, log);
newSim.addSystemTopology(circElements2);
newSim.setSwitchTime(loadStep, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, leftVectorLog, rightVectorLog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
// Write simulation data to file
std::ostringstream fileName;
fileName << "VarFreqRXLineResLoad_" << timeStep << "_" << finalTime;
//log.WriteLogToFile("Logs/Log_" + fileName.str() + ".log");
//leftVectorLog.WriteLogToFile("Logs/LeftVectorLog_" + fileName.str() + ".csv");
//rightVectorLog.WriteLogToFile("Logs/RightVectorLog_" + fileName.str() + ".csv");
}
void RXLineResLoadEMT() {
// Define Object for saving data on a file
Logger log, leftVectorLog, rightVectorLog;
// Declare circuit components
std::vector<BaseComponent*> circElements0, circElements1, circElements2;
circElements0.push_back(new VoltSourceResEMT("v_s", 1, 0, 10000, 0, 1));
circElements0.push_back(new LinearResistorEMT("r_line", 1, 2, 1));
circElements0.push_back(new InductorEMT("l_line", 2, 3, 1));
circElements1 = circElements0;
circElements2 = circElements0;
circElements1.push_back(new LinearResistorEMT("r_load", 3, 0, 1000));
circElements2.push_back(new LinearResistorEMT("r_load", 3, 0, 800));
// Set up simulation
Real timeStep = 0.001;
Simulation newSim(circElements1, 2.0*PI*50.0, timeStep, 0.3, log, SimulationType::EMT);
newSim.addSystemTopology(circElements2);
newSim.setSwitchTime(0.1, 1);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, leftVectorLog, rightVectorLog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
// Write simulation data to file