Commit 6e218cb3 authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

restructure external interface a bit


Former-commit-id: 87e56509
parent 075b0d50
#include "ExternalInterface.h"
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;
}
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;
}
mExtComponents[num] = ecs;
}
#pragma once
#include <vector>
#include "Components/ExternalCurrentSource.h"
#include "Components/ExternalVoltageSource.h"
namespace DPsim {
/** Abstract base class for interfacing the simulator with other data sources or sinks.
* After an ExternalInterface is created, components that should use values
......@@ -9,7 +14,11 @@ namespace DPsim {
* components.
*/
class ExternalInterface {
protected:
std::vector<BaseComponent*> mExtComponents;
public:
void registerVoltageSource(ExternalVoltageSource* evs, int num);
void registerCurrentSource(ExternalCurrentSource* ecs, int num);
virtual void readValues() = 0;
virtual ~ExternalInterface() {};
};
......
......@@ -21,14 +21,6 @@ VillasInterface::~VillasInterface() {
shmem_shared_close(mShmem, mBase);
}
void VillasInterface::registerVoltageSource(ExternalVoltageSource *evs, int num) {
mExtComponents[num] = evs;
}
void VillasInterface::registerCurrentSource(ExternalCurrentSource *ecs, int num) {
mExtComponents[num] = ecs;
}
void VillasInterface::readValues() {
struct sample *sample;
int ret = 0;
......@@ -38,19 +30,22 @@ void VillasInterface::readValues() {
std::cerr << "Fatal error: failed to read sample from shmem interface" << std::endl;
std::exit(1);
}
for (auto it = mExtComponents.begin(); it != mExtComponents.end(); ++it) {
if (sample->length <= it->first) {
std::cerr << "Warning: missing data in received sample" << std::endl;
continue;
}
int sz = mExtComponents.size();
if (sample->length < mExtComponents.size()) {
std::cerr << "Warning: missing data in received sample" << std::endl;
sz = sample->length;
}
for (int i = 0; i < sz; i++) {
// TODO integer format?
ExternalVoltageSource *evs = dynamic_cast<ExternalVoltageSource*>(it->second);
if (evs)
evs->setVoltage(sample->data[it->first].f);
ExternalCurrentSource *ecs = dynamic_cast<ExternalCurrentSource*>(it->second);
if (ecs)
ecs->setCurrent(sample->data[it->first].f);
if (i < mExtComponents.size()) {
ExternalVoltageSource *evs = dynamic_cast<ExternalVoltageSource*>(mExtComponents[i]);
if (evs)
evs->setVoltage(sample->data[i].f);
ExternalCurrentSource *ecs = dynamic_cast<ExternalCurrentSource*>(mExtComponents[i]);
if (ecs)
ecs->setCurrent(sample->data[i].f);
}
sample_put(sample);
}
}
......@@ -17,12 +17,9 @@ namespace DPsim {
struct shmem_shared* mShmem;
void* mBase;
std::unordered_map<int, BaseComponent*> mExtComponents;
public:
VillasInterface(const char* name);
~VillasInterface();
void registerVoltageSource(ExternalVoltageSource* evs, int num);
void registerCurrentSource(ExternalCurrentSource* ecs, int num);
virtual void readValues();
};
};
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