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

villas interface: read/write values as phasors


Former-commit-id: 9667f481
parent 033dc796
......@@ -2,11 +2,10 @@
using namespace DPsim;
ExternalCurrentSource::ExternalCurrentSource(std::string name, int src, int dest, Real current, Real phase) : CurrentSource(name, src, dest, current, phase) {
this->mPhase = phase;
ExternalCurrentSource::ExternalCurrentSource(std::string name, int src, int dest) : CurrentSource(name, src, dest, 0, 0) {
}
void ExternalCurrentSource::setCurrent(Real current) {
this->currentr = current*cos(this->mPhase);
this->currenti = current*sin(this->mPhase);
void ExternalCurrentSource::setCurrent(Real real, Real imag) {
this->currentr = real;
this->currenti = imag;
}
......@@ -12,8 +12,8 @@ namespace DPsim {
public:
ExternalCurrentSource() {};
ExternalCurrentSource(std::string name, int src, int dest, Real current, Real phase);
ExternalCurrentSource(std::string name, int src, int dest);
void setCurrent(Real current);
void setCurrent(Real real, Real imag);
};
}
......@@ -2,11 +2,10 @@
using namespace DPsim;
ExternalVoltageSource::ExternalVoltageSource(std::string name, int src, int dest, Real voltage, Real phase, int num): IdealVoltageSource(name, src, dest, voltage, phase, num) {
this->mPhase = phase;
ExternalVoltageSource::ExternalVoltageSource(std::string name, int src, int dest, int num): IdealVoltageSource(name, src, dest, 0, 0, num) {
}
void ExternalVoltageSource::setVoltage(Real voltage) {
this->mVoltageDiffr = voltage*cos(mPhase);
this->mVoltageDiffi = voltage*sin(mPhase);
void ExternalVoltageSource::setVoltage(Real real, Real imag) {
this->mVoltageDiffr = real;
this->mVoltageDiffi = imag;
}
......@@ -12,8 +12,8 @@ namespace DPsim {
public:
ExternalVoltageSource() {};
ExternalVoltageSource(std::string name, int src, int dest, Real voltage, Real phase, int num);
ExternalVoltageSource(std::string name, int src, int dest, int num);
void setVoltage(Real voltage);
void setVoltage(Real real, Real imag);
};
}
......@@ -11,13 +11,13 @@ void DPsim::villasExample()
Logger log, llog, rlog;
std::vector<BaseComponent*> comps;
ExternalCurrentSource *ecs = new ExternalCurrentSource("i1", 1, 0, 0, 0);
ExternalCurrentSource *ecs = new ExternalCurrentSource("i1", 1, 0);
comps.push_back(ecs);
comps.push_back(new LinearResistor("r1", 1, 2, 1));
comps.push_back(new LinearResistor("r2", 2, 0, 1));
VillasInterface *villas = new VillasInterface("/villas1");
villas->registerCurrentSource(ecs, 0);
villas->registerExportedVoltage({1, 2}, 0);
villas->registerCurrentSource(ecs, 0, 1);
villas->registerExportedVoltage(1, 2, 0, 1);
// Set up simulation
Real timeStep = 0.01;
......
......@@ -3,23 +3,14 @@
using namespace DPsim;
void ExternalInterface::registerVoltageSource(ExternalVoltageSource *evs, int num) {
int sz = mExtComponents.size();
if (num >= sz)
mExtComponents.resize(num+1, NULL);
mExtComponents[num] = evs;
void ExternalInterface::registerVoltageSource(ExternalVoltageSource *evs, Int realIdx, Int imagIdx) {
mExtComponents.push_back({evs, realIdx, imagIdx});
}
void ExternalInterface::registerCurrentSource(ExternalCurrentSource *ecs, int num) {
int sz = mExtComponents.size();
if (num >= sz)
mExtComponents.resize(num+1, NULL);
mExtComponents[num] = ecs;
void ExternalInterface::registerCurrentSource(ExternalCurrentSource *ecs, Int realIdx, Int imagIdx) {
mExtComponents.push_back({ecs, realIdx, imagIdx});
}
void ExternalInterface::registerExportedVoltage(VoltDiff vd, int num) {
int sz = mExportedVoltages.size();
if (num >= sz)
mExportedVoltages.resize(num+1, {-1, -1});
mExportedVoltages[num] = vd;
void ExternalInterface::registerExportedVoltage(Int from, Int to, Int realIdx, Int imagIdx) {
mExportedVoltages.push_back({from, to, realIdx, imagIdx});
}
......@@ -7,8 +7,16 @@
namespace DPsim {
struct VoltDiff {
int from;
int to;
Int from;
Int to;
Int realIdx;
Int imagIdx;
};
struct ExtComponent {
BaseComponent *comp;
Int realIdx;
Int imagIdx;
};
/** Abstract base class for interfacing the simulator with other data sources or sinks.
* After an ExternalInterface is created, components that should use values
......@@ -19,12 +27,12 @@ namespace DPsim {
*/
class ExternalInterface {
protected:
std::vector<BaseComponent*> mExtComponents;
std::vector<ExtComponent> mExtComponents;
std::vector<VoltDiff> mExportedVoltages;
public:
void registerVoltageSource(ExternalVoltageSource* evs, int num);
void registerCurrentSource(ExternalCurrentSource* ecs, int num);
void registerExportedVoltage(VoltDiff vd, int num);
void registerVoltageSource(ExternalVoltageSource* evs, Int realIdx, Int imagIdx);
void registerCurrentSource(ExternalCurrentSource* ecs, Int realIdx, Int imagIdx);
void registerExportedVoltage(Int from, Int to, Int realIdx, Int imagIdx);
virtual void readValues() = 0;
virtual void writeValues(SystemModel &model) = 0;
virtual ~ExternalInterface() {};
......
......@@ -31,45 +31,50 @@ void VillasInterface::readValues() {
std::cerr << "Fatal error: failed to read sample from shmem interface" << std::endl;
std::exit(1);
}
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++) {
for (ExtComponent extComp : mExtComponents) {
if (extComp.realIdx >= sample->length || extComp.imagIdx >= sample->length) {
std::cerr << "Fatal error: incomplete data received from shmem interface" << std::endl;
std::exit(1);
}
// TODO integer format?
if (i < mExtComponents.size()) {
ExternalVoltageSource *evs = dynamic_cast<ExternalVoltageSource*>(mExtComponents[i]);
if (evs)
evs->setVoltage(sample->data[i].f);
Real real = sample->data[extComp.realIdx].f;
Real imag = sample->data[extComp.imagIdx].f;
ExternalCurrentSource *ecs = dynamic_cast<ExternalCurrentSource*>(mExtComponents[i]);
if (ecs)
ecs->setCurrent(sample->data[i].f);
}
sample_put(sample);
ExternalCurrentSource *ecs = dynamic_cast<ExternalCurrentSource*>(extComp.comp);
if (ecs)
ecs->setCurrent(real, imag);
ExternalVoltageSource *evs = dynamic_cast<ExternalVoltageSource*>(extComp.comp);
if (evs)
evs->setVoltage(real, imag);
}
}
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;
Int len = 0;
for (auto vd : mExportedVoltages) {
Real real = 0, imag = 0;
if (vd.from > 0) {
real += model.getRealFromLeftSideVector(vd.from-1);
imag += model.getImagFromLeftSideVector(vd.from-1);
}
if (vd.to > 0) {
real -= model.getRealFromLeftSideVector(vd.to-1);
imag -= model.getImagFromLeftSideVector(vd.to-1);
}
if (vd.realIdx >= sample->capacity || vd.imagIdx >= sample->capacity) {
std::cerr << "fatal error: not enough space in allocated struct sample" << std::endl;
std::exit(1);
}
sample->data[vd.realIdx].f = real;
sample->data[vd.imagIdx].f = imag;
if (vd.realIdx > len)
len = vd.realIdx;
if (vd.imagIdx > len)
len = vd.imagIdx;
}
sample->length = len+1;
sample->sequence = mSeq++;
clock_gettime(CLOCK_REALTIME, &sample->ts.origin);
int ret = 0;
......
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