Commit 033dc796 authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

implement sending voltage values back to VILLASnode


Former-commit-id: dabcba83
parent 6e218cb3
...@@ -14,9 +14,10 @@ void DPsim::villasExample() ...@@ -14,9 +14,10 @@ void DPsim::villasExample()
ExternalCurrentSource *ecs = new ExternalCurrentSource("i1", 1, 0, 0, 0); ExternalCurrentSource *ecs = new ExternalCurrentSource("i1", 1, 0, 0, 0);
comps.push_back(ecs); comps.push_back(ecs);
comps.push_back(new LinearResistor("r1", 1, 2, 1)); comps.push_back(new LinearResistor("r1", 1, 2, 1));
comps.push_back(new LinearResistor("r1", 2, 0, 1)); comps.push_back(new LinearResistor("r2", 2, 0, 1));
VillasInterface *villas = new VillasInterface("/villas1"); VillasInterface *villas = new VillasInterface("/villas1");
villas->registerCurrentSource(ecs, 0); villas->registerCurrentSource(ecs, 0);
villas->registerExportedVoltage({1, 2}, 0);
// Set up simulation // Set up simulation
Real timeStep = 0.01; Real timeStep = 0.01;
......
...@@ -5,21 +5,21 @@ using namespace DPsim; ...@@ -5,21 +5,21 @@ using namespace DPsim;
void ExternalInterface::registerVoltageSource(ExternalVoltageSource *evs, int num) { void ExternalInterface::registerVoltageSource(ExternalVoltageSource *evs, int num) {
int sz = mExtComponents.size(); int sz = mExtComponents.size();
if (num >= sz) { if (num >= sz)
mExtComponents.reserve(num+1); mExtComponents.resize(num+1, NULL);
for (int i = sz; i < num; i++)
mExtComponents[i] = NULL;
}
mExtComponents[num] = evs; mExtComponents[num] = evs;
} }
void ExternalInterface::registerCurrentSource(ExternalCurrentSource *ecs, int num) { void ExternalInterface::registerCurrentSource(ExternalCurrentSource *ecs, int num) {
int sz = mExtComponents.size(); int sz = mExtComponents.size();
if (num >= sz) { if (num >= sz)
mExtComponents.reserve(num+1); mExtComponents.resize(num+1, NULL);
for (int i = sz; i < num; i++)
mExtComponents[i] = NULL;
}
mExtComponents[num] = ecs; mExtComponents[num] = ecs;
} }
void ExternalInterface::registerExportedVoltage(VoltDiff vd, int num) {
int sz = mExportedVoltages.size();
if (num >= sz)
mExportedVoltages.resize(num+1, {-1, -1});
mExportedVoltages[num] = vd;
}
...@@ -6,6 +6,10 @@ ...@@ -6,6 +6,10 @@
#include "Components/ExternalVoltageSource.h" #include "Components/ExternalVoltageSource.h"
namespace DPsim { namespace DPsim {
struct VoltDiff {
int from;
int to;
};
/** Abstract base class for interfacing the simulator with other data sources or sinks. /** Abstract base class for interfacing the simulator with other data sources or sinks.
* After an ExternalInterface is created, components that should use values * After an ExternalInterface is created, components that should use values
* from this interface can be registered with it using the appropiate * from this interface can be registered with it using the appropiate
...@@ -16,10 +20,13 @@ namespace DPsim { ...@@ -16,10 +20,13 @@ namespace DPsim {
class ExternalInterface { class ExternalInterface {
protected: protected:
std::vector<BaseComponent*> mExtComponents; std::vector<BaseComponent*> mExtComponents;
std::vector<VoltDiff> mExportedVoltages;
public: public:
void registerVoltageSource(ExternalVoltageSource* evs, int num); void registerVoltageSource(ExternalVoltageSource* evs, int num);
void registerCurrentSource(ExternalCurrentSource* ecs, int num); void registerCurrentSource(ExternalCurrentSource* ecs, int num);
void registerExportedVoltage(VoltDiff vd, int num);
virtual void readValues() = 0; virtual void readValues() = 0;
virtual void writeValues(SystemModel &model) = 0;
virtual ~ExternalInterface() {}; virtual ~ExternalInterface() {};
}; };
} }
...@@ -122,6 +122,10 @@ int Simulation::step(Logger& logger) ...@@ -122,6 +122,10 @@ int Simulation::step(Logger& logger)
(*it)->postStep(mSystemModel); (*it)->postStep(mSystemModel);
} }
for (auto it = mExternalInterfaces.begin(); it != mExternalInterfaces.end(); ++it) {
(*it)->writeValues(mSystemModel);
}
if (mCurrentSwitchTimeIndex < mSwitchEventVector.size()) { if (mCurrentSwitchTimeIndex < mSwitchEventVector.size()) {
if (mTime >= mSwitchEventVector[mCurrentSwitchTimeIndex].switchTime) { if (mTime >= mSwitchEventVector[mCurrentSwitchTimeIndex].switchTime) {
switchSystemMatrix(mSwitchEventVector[mCurrentSwitchTimeIndex].systemIndex); switchSystemMatrix(mSwitchEventVector[mCurrentSwitchTimeIndex].systemIndex);
......
...@@ -15,6 +15,7 @@ VillasInterface::VillasInterface(const char* name) { ...@@ -15,6 +15,7 @@ VillasInterface::VillasInterface(const char* name) {
std::perror("Failed to open/map shared memory object"); std::perror("Failed to open/map shared memory object");
std::exit(1); std::exit(1);
} }
mSeq = 0;
} }
VillasInterface::~VillasInterface() { VillasInterface::~VillasInterface() {
...@@ -49,3 +50,32 @@ void VillasInterface::readValues() { ...@@ -49,3 +50,32 @@ void VillasInterface::readValues() {
sample_put(sample); sample_put(sample);
} }
} }
void VillasInterface::writeValues(SystemModel& model) {
struct sample *sample;
sample_alloc(&mShmem->pool, &sample, 1);
int len = mExportedVoltages.size();
if (sample->capacity < len) {
std::cerr << "struct sample returned from pool has to small capacity" << std::endl;
len = sample->capacity;
}
sample->length = len;
Matrix lvect = model.getLeftSideVector();
for (int i = 0; i < len; i++) {
Real f = 0.0f;
VoltDiff vd = mExportedVoltages[i];
if (vd.from > 0)
f += lvect(vd.from-1, 0);
if (vd.to > 0)
f -= lvect(vd.to-1, 0);
sample->data[i].f = f;
}
sample->sequence = mSeq++;
clock_gettime(CLOCK_REALTIME, &sample->ts.origin);
int ret = 0;
while (ret == 0)
ret = shmem_shared_write(mShmem, &sample, 1);
if (ret < 0) {
std::cerr << "Failed to write samples to shmem interface" << std::endl;
}
}
#pragma once #pragma once
#include <atomic> #include <atomic>
#include <unordered_map>
#include <villas/shmem.h> #include <villas/shmem.h>
...@@ -15,11 +14,13 @@ namespace DPsim { ...@@ -15,11 +14,13 @@ namespace DPsim {
private: private:
const char* mShmemName; const char* mShmemName;
struct shmem_shared* mShmem; struct shmem_shared* mShmem;
int mSeq;
void* mBase; void* mBase;
public: public:
VillasInterface(const char* name); VillasInterface(const char* name);
~VillasInterface(); ~VillasInterface();
virtual void readValues(); virtual void readValues();
virtual void 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