Commit dabcba83 authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

implement sending voltage values back to VILLASnode

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