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