Commit 1a8b90a6 authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

shmem: support initial values; add distributed example

parent a6602c4d
......@@ -2,7 +2,8 @@
using namespace DPsim;
ExternalCurrentSource::ExternalCurrentSource(std::string name, int src, int dest) : CurrentSource(name, src, dest, 0, 0) {
ExternalCurrentSource::ExternalCurrentSource(std::string name, int src, int dest, Real initCurrent, Real initPhase) :
CurrentSource(name, src, dest, initCurrent, initPhase) {
}
void ExternalCurrentSource::setCurrent(Real real, Real imag) {
......
......@@ -6,13 +6,10 @@ namespace DPsim {
/** Ideal current source, but the current value can be changed between simulation
* steps (for example for interfacing with another simulator) */
class ExternalCurrentSource : public CurrentSource {
private:
Real mPhase;
public:
ExternalCurrentSource() {};
ExternalCurrentSource(std::string name, int src, int dest);
ExternalCurrentSource(std::string name, int src, int dest, Real initCurrent, Real initPhase);
void setCurrent(Real real, Real imag);
};
......
......@@ -2,7 +2,8 @@
using namespace DPsim;
ExternalVoltageSource::ExternalVoltageSource(std::string name, int src, int dest, int num): IdealVoltageSource(name, src, dest, 0, 0, num) {
ExternalVoltageSource::ExternalVoltageSource(std::string name, int src, int dest, Real initVoltage, Real initPhase, int num) :
IdealVoltageSource(name, src, dest, initVoltage, initPhase, num) {
}
void ExternalVoltageSource::setVoltage(Real real, Real imag) {
......
......@@ -6,13 +6,10 @@ namespace DPsim {
/** Ideal voltage source, but the voltage value can be changed between simulation
* steps (for example for interfacing with another simulator) */
class ExternalVoltageSource : public IdealVoltageSource {
private:
Real mPhase;
public:
ExternalVoltageSource() {};
ExternalVoltageSource(std::string name, int src, int dest, int num);
ExternalVoltageSource(std::string name, int src, int dest, Real initVoltage, Real initPhase, int num);
void setVoltage(Real real, Real imag);
};
......
......@@ -13,7 +13,8 @@ int main(int argc, char* argv[]) {
villasExample();
//villasExample();
villasDistributedExample(argc, argv);
//simulationExample1();
//simulationExample2();
//simulationExample3();
......
......@@ -7,11 +7,12 @@ using namespace DPsim;
void DPsim::villasExample()
{
// Very simple test circuit. Just 2 resistors and a current read from VILLASnode.
// Very simple test circuit. Just a few resistors and an inductance.
// Voltage is read from VILLASnode and current through everything is written back.
Logger log, llog, rlog;
std::vector<BaseComponent*> comps;
ExternalVoltageSource *evs = new ExternalVoltageSource("v_s", 1, 0, 1);
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));
......@@ -42,3 +43,63 @@ void DPsim::villasExample()
}
delete villas;
}
void DPsim::villasDistributedExample(int argc, char *argv[])
{
// Testing the interface with the same circuit as above,
// but the load is simulated in a different instance.
// Values are exchanged using the ideal transformator model: an ideal
// current source on the supply side and an ideal voltage source on the
// supply side, whose values are received from the respective other circuit.
Logger log, llog, rlog;
std::vector<BaseComponent*> comps;
VillasInterface *villas;
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")) {
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));
ExternalCurrentSource *ecs = new ExternalCurrentSource("i_s", 1, 0, 0, 0);
comps.push_back(ecs);
villas = new VillasInterface("/villas0");
villas->registerCurrentSource(ecs, 0, 1);
villas->registerExportedVoltage(1, 0, 0, 1);
} else if (!strcmp(argv[1], "1")) {
ExternalVoltageSource *evs = new ExternalVoltageSource("v_s", 1, 0, 0, 0, 1);
comps.push_back(evs);
comps.push_back(new LinearResistor("r_load", 1, 2, 100));
comps.push_back(new Inductor("l_load", 2, 0, 0.1));
villas = new VillasInterface("/villas1");
villas->registerVoltageSource(evs, 0, 1);
villas->registerExportedCurrent(evs, 0, 1);
} else {
std::cerr << "invalid test number" << std::endl;
std::exit(1);
}
// Set up simulation
Real timeStep = 0.001;
Simulation newSim(comps, 2.0*M_PI*50.0, timeStep, 0.3, log);
newSim.addExternalInterface(villas);
// Main Simulation Loop
std::cout << "Start simulation." << std::endl;
while (newSim.step(log, llog, rlog))
{
newSim.increaseByTimeStep();
updateProgressBar(newSim.getTime(), newSim.getFinalTime());
}
std::cout << "Simulation finished." << std::endl;
log.WriteLogToFile("output" + std::string(argv[1]) + ".log");
rlog.WriteLogToFile("rvector" + std::string(argv[1]) + ".log");
llog.WriteLogToFile("lvector" + std::string(argv[1]) + ".log");
for (auto comp : comps) {
delete comp;
}
delete villas;
}
......@@ -4,6 +4,7 @@
namespace DPsim {
void villasExample();
void villasDistributedExample(int argc, char *argv[]);
}
#endif
......@@ -30,6 +30,7 @@ namespace DPsim {
std::vector<ExtComponent> mExtComponents;
std::vector<VoltDiff> mExportedVoltages;
std::vector<ExtComponent> mExportedCurrents;
bool mInit = 0;
public:
void registerVoltageSource(ExternalVoltageSource* evs, Int realIdx, Int imagIdx);
void registerCurrentSource(ExternalCurrentSource* ecs, Int realIdx, Int imagIdx);
......
......@@ -23,6 +23,10 @@ VillasInterface::~VillasInterface() {
}
void VillasInterface::readValues() {
if (!mInit) {
mInit = 1;
return;
}
struct sample *sample;
int ret = 0;
while (ret == 0)
......@@ -47,6 +51,7 @@ void VillasInterface::readValues() {
if (evs)
evs->setVoltage(real, imag);
}
sample_put(sample);
}
void VillasInterface::writeValues(SystemModel& model) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment