Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from JTS22/main
Browse files Browse the repository at this point in the history
This PR adapts dpsim-villas and the examples contained within it to the new attribute system for DPsim, which will be added in PR sogno-platform/dpsim#62.
  • Loading branch information
m-mirz authored Apr 22, 2022
2 parents ee0deda + 8a7a24b commit 270b380
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 98 deletions.
14 changes: 7 additions & 7 deletions examples/cxx/FileExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ int main(int argc, char* argv[]) {

// Interface
//evs->setAttributeRef("V_ref", intf.importComplex(0));
intf.exportComplex(evs->attributeMatrixComp("i_intf")->coeff(0, 0), 0);
intf.exportComplex(evs->mIntfCurrent->deriveCoeff<Complex>(0, 0), 0);
sim.addInterface(&intf);

// Logger
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("v2", n2->attribute("v"));
logger->addAttribute("v3", n3->attribute("v"));
logger->addAttribute("v4", n4->attribute("v"));
logger->addAttribute("V_ref", evs->attribute("V_ref"));
logger->addAttribute("i_evs", evs->attributeMatrixComp("i_intf"), 1, 1);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
logger->logAttribute("v4", n4->mVoltage);
logger->logAttribute("V_ref", evs->mVoltageRef);
logger->logAttribute("i_evs", evs->mIntfCurrent, 1, 1);
sim.addLogger(logger);

sim.run(1);
Expand Down
20 changes: 10 additions & 10 deletions examples/cxx/MqttExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,20 @@ int main(int argc, char* argv[]) {
InterfaceVillas intf("dpsim-mqtt", mqttConfig);

// Interface
evs->setAttributeRef("V_ref", intf.importComplex(0));
intf.exportComplex(evs->attributeMatrixComp("v_intf")->coeff(0, 0), 0, "v_src");
intf.exportComplex(rL->attributeMatrixComp("v_intf")->coeff(0, 0), 1, "v_load");
evs->mVoltageRef->setReference(intf.importComplex(0));
intf.exportComplex(evs->mIntfVoltage->deriveCoeff<Complex>(0, 0), 0, "v_src");
intf.exportComplex(rL->mIntfVoltage->deriveCoeff<Complex>(0, 0), 1, "v_load");
sim.addInterface(&intf, true);

// Logger
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("v2", n2->attribute("v"));
logger->addAttribute("v3", n3->attribute("v"));
logger->addAttribute("v4", n4->attribute("v"));
logger->addAttribute("v_src", evs->attribute("V_ref"));
logger->addAttribute("i_evs", evs->attributeMatrixComp("i_intf"), 1, 1);
logger->addAttribute("v_evs", evs->attributeMatrixComp("v_intf"), 1, 1);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
logger->logAttribute("v4", n4->mVoltage);
logger->logAttribute("v_src", evs->mVoltageRef);
logger->logAttribute("i_evs", evs->mIntfCurrent, 1, 1);
logger->logAttribute("v_evs", evs->mIntfVoltage, 1, 1);
sim.addLogger(logger);

sim.run(1);
Expand Down
18 changes: 9 additions & 9 deletions examples/cxx/SharedMemExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ int main(int argc, char* argv[]) {

// Interface
//evs->setAttributeRef("V_ref", intf.importComplex(0));
intf.exportComplex(evs->attributeMatrixComp("v_intf")->coeff(0, 0), 0, "v_src");
intf.exportComplex(rL->attributeMatrixComp("v_intf")->coeff(0, 0), 1, "v_load");
intf.exportComplex(evs->mIntfVoltage->deriveCoeff<Complex>(0, 0), 0, "v_src");
intf.exportComplex(rL->mIntfVoltage->deriveCoeff<Complex>(0, 0), 1, "v_load");
sim.addInterface(&intf, true);

// Logger
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("v2", n2->attribute("v"));
logger->addAttribute("v3", n3->attribute("v"));
logger->addAttribute("v4", n4->attribute("v"));
logger->addAttribute("v_src", evs->attribute("V_ref"));
logger->addAttribute("i_evs", evs->attributeMatrixComp("i_intf"), 1, 1);
logger->addAttribute("v_evs", evs->attributeMatrixComp("v_intf"), 1, 1);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
logger->logAttribute("v4", n4->mVoltage);
logger->logAttribute("v_src", evs->mVoltageRef);
logger->logAttribute("i_evs", evs->mIntfCurrent, 1, 1);
logger->logAttribute("v_evs", evs->mIntfVoltage, 1, 1);
sim.addLogger(logger);

sim.run(1);
Expand Down
4 changes: 2 additions & 2 deletions examples/cxx/ShmemControllableFiltSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ int main(int argc, char *argv[]) {
r1->connect({ SimNode::GND, n1 });
load->connect({ n1 });

load->setAttributeRef("power_active", filtP->attribute<Real>("output"));
load->setAttributeRef("power_reactive", filtQ->attribute<Real>("output"));
load->mActivePower->setReference(filtP->mOutput);
load->mReactivePower->setReference(filtQ->mOutput);

filtP->setInput(intf.importReal(0));
filtQ->setInput(intf.importReal(1));
Expand Down
4 changes: 2 additions & 2 deletions examples/cxx/ShmemControllableSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ int main(int argc, char *argv[]) {
ecs->connect({ SimNode::GND, n1 });
r1->connect({ SimNode::GND, n1 });

ecs->setAttributeRef("I_ref", intf.importComplex(0));
intf.exportComplex(ecs->attributeMatrixComp("v_intf")->coeff(0, 0), 0);
ecs->mCurrentRef->setReference(intf.importComplex(0));
intf.exportComplex(ecs->mIntfVoltage->deriveCoeff<Complex>(0, 0), 0);

auto sys = SystemTopology(50, SystemNodeList{n1}, SystemComponentList{ecs, r1});

Expand Down
28 changes: 14 additions & 14 deletions examples/cxx/ShmemDistributedDirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ int main(int argc, char *argv[]) {

// Logging
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("v2", n2->attribute("v"));
logger->addAttribute("r12", r12->attribute("i_intf"));
logger->addAttribute("ievs", evs->attribute("i_intf"));
logger->addAttribute("vevs", evs->attribute("v_intf"));
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("r12", r12->mIntfCurrent);
logger->logAttribute("ievs", evs->mIntfCurrent);
logger->logAttribute("vevs", evs->mIntfVoltage);
sim.addLogger(logger);

// Map attributes to interface entries
InterfaceShmem intf(in, out);
evs->setAttributeRef("V_ref", intf.importComplex(0));
auto evsAttrMinus = evs->attributeMatrixComp("i_intf")->coeff(0,0);
evs->mVoltageRef->setReference(intf.importComplex(0));
auto evsAttrMinus = evs->mIntfCurrent->deriveCoeff<Complex>(0,0);
intf.exportComplex(evsAttrMinus, 0);
sim.addInterface(&intf);

Expand Down Expand Up @@ -132,17 +132,17 @@ int main(int argc, char *argv[]) {

// Logging
auto logger = DataLogger::make(simName);
logger->addAttribute("v2", n2->attribute("v"));
logger->addAttribute("r02", r02->attribute("i_intf"));
logger->addAttribute("vecs", ecs->attribute("v_intf"));
logger->addAttribute("iecs", ecs->attribute("i_intf"));
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("r02", r02->mIntfCurrent);
logger->logAttribute("vecs", ecs->mIntfVoltage);
logger->logAttribute("iecs", ecs->mIntfCurrent);
sim.addLogger(logger);

// Map attributes to interface entries
InterfaceShmem intf(in, out);
ecs->setAttributeRef("I_ref", intf.importComplex(0));
//intf.exportComplex(ecs->attributeMatrixComp("v_intf")->coeff(0, 0), 0);
intf.exportComplex(ecs->attributeMatrixComp("v_intf")->coeff(0, 0)->scale(Complex(-1.,0)), 0);
ecs->mCurrentRef->setReference(intf.importComplex(0));
//intf.exportComplex(ecs->mIntfVoltage->coeff(0, 0), 0);
intf.exportComplex(ecs->mIntfVoltage->deriveCoeff<Complex>(0, 0)->deriveScaled(Complex(-1.,0)), 0);
sim.addInterface(&intf);

sim.run();
Expand Down
8 changes: 4 additions & 4 deletions examples/cxx/ShmemDistributedReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ int main(int argc, char* argv[]) {

// Logging
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("v2", n2->attribute("v"));
logger->addAttribute("r12", r12->attribute("i_intf"));
logger->addAttribute("r02", r02->attribute("i_intf"));
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("r12", r12->mIntfCurrent);
logger->logAttribute("r02", r02->mIntfCurrent);

Simulation sim(simName);
sim.setSystem(sys);
Expand Down
8 changes: 4 additions & 4 deletions examples/cxx/ShmemDistributedVillas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ int main(int argc, char *argv[]) {
comps = SystemComponentList{evs, vs, l1, r1};
nodes = SystemNodeList{SimNode::GND, n1, n2, n3};

evs->setAttributeRef("V_ref", intf.importComplex(0));
intf.exportComplex(evs->attributeMatrixComp("i_intf")->coeff(0, 0), 0);
evs->mVoltageRef->setReference(intf.importComplex(0));
intf.exportComplex(evs->mIntfCurrent->deriveCoeff<Complex>(0, 0), 0);

}
else if (String(argv[1]) == "1") {
Expand Down Expand Up @@ -100,8 +100,8 @@ int main(int argc, char *argv[]) {
comps = SystemComponentList{ecs, sw, r2A, r2B};
nodes = SystemNodeList{SimNode::GND, n4, n5};

ecs->setAttributeRef("I_ref", intf.importComplex(0));
intf.exportComplex(ecs->attributeMatrixComp("v_intf")->coeff(0, 0), 0);
ecs->mCurrentRef->setReference(intf.importComplex(0));
intf.exportComplex(ecs->mIntfVoltage->deriveCoeff<Complex>(0, 0), 0);
}
else {
std::cerr << "invalid test number" << std::endl;
Expand Down
16 changes: 8 additions & 8 deletions examples/cxx/ShmemExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ int main(int argc, char* argv[]) {
#endif

// Interface
evs->setAttributeRef("V_ref", intf.importComplex(0));
intf.exportComplex(evs->attributeMatrixComp("i_intf")->coeff(0, 0), 0);
evs->mVoltageRef->setReference(intf.importComplex(0));
intf.exportComplex(evs->mIntfCurrent->deriveCoeff<Complex>(0, 0), 0);
sim.addInterface(&intf);

// Logger
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("v2", n2->attribute("v"));
logger->addAttribute("v3", n3->attribute("v"));
logger->addAttribute("v4", n4->attribute("v"));
logger->addAttribute("V_ref", evs->attribute("V_ref"));
logger->addAttribute("i_evs", evs->attributeMatrixComp("i_intf"), 1, 1);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
logger->logAttribute("v4", n4->mVoltage);
logger->logAttribute("V_ref", evs->mVoltageRef);
logger->logAttribute("i_evs", evs->mIntfCurrent, 1, 1);
sim.addLogger(logger);

sim.run();
Expand Down
6 changes: 3 additions & 3 deletions examples/cxx/Shmem_CIGRE_MV_PowerFlowTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ int main(int argc, char** argv) {
}

auto n_stat = std::dynamic_pointer_cast<CPS::SP::SimNode>(n);
auto v = n_stat->attributeMatrixComp("v")->coeff(0, 0);
auto v = n_stat->mVoltage->deriveCoeff<Complex>(0, 0);

std::cout << "Signal " << (i*2)+0 << ": Mag " << n->name() << std::endl;
std::cout << "Signal " << (i*2)+1 << ": Phas " << n->name() << std::endl;

intf.exportReal(v->mag(), (i*2)+0); o++;
intf.exportReal(v->phase(), (i*2)+1); o++;
intf.exportReal(v->deriveMag(), (i*2)+0); o++;
intf.exportReal(v->derivePhase(), (i*2)+1); o++;
}

sim.addInterface(&intf, false);
Expand Down
6 changes: 3 additions & 3 deletions examples/cxx/Shmem_CIGRE_MV_PowerFlowTest_LoadProfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ int main(int argc, char** argv){
}

auto n_stat = std::dynamic_pointer_cast<CPS::SP::SimNode>(n);
auto v = n_stat->attributeMatrixComp("v")->coeff(0, 0);
auto v = n_stat->mVoltage->deriveCoeff<Complex>(0, 0);

std::cout << "Signal " << (i*2)+0 << ": Mag " << n->name() << std::endl;
std::cout << "Signal " << (i*2)+1 << ": Phas " << n->name() << std::endl;

intf.exportReal(v->mag(), (i*2)+0); o++;
intf.exportReal(v->phase(), (i*2)+1); o++;
intf.exportReal(v->deriveMag(), (i*2)+0); o++;
intf.exportReal(v->derivePhase(), (i*2)+1); o++;

list_varnames[(i*2)+0] = n->name() + ".V.mag";
list_varnames[(i*2)+1] = n->name() + ".V.phase";
Expand Down
6 changes: 3 additions & 3 deletions examples/cxx/Shmem_WSCC-9bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ int main(int argc, char *argv[]) {
// Register exportable node voltages
UInt o = 0;
for (auto n : sys.mNodes) {
auto v = n->attributeComplex("v");
auto v = n->attribute<Complex>("v");

intf.exportReal(v->mag(), o+0);
intf.exportReal(v->phase(), o+1);
intf.exportReal(v->deriveMag(), o+0);
intf.exportReal(v->derivePhase(), o+1);

o += 2;
}
Expand Down
16 changes: 8 additions & 8 deletions examples/cxx/Shmem_WSCC-9bus_Ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ int main(int argc, char *argv[]) {
std::vector<Real> coefficients = std::vector<Real>(100, 1./100);

auto filtP_profile = FIRFilter::make("filter_p_profile", coefficients_profile, 0, CPS::Logger::Level::off);
load_profile->setAttributeRef("P", filtP_profile->attribute<Real>("output"));
load_profile->mActivePower->setReference(filtP_profile->mOutput);

sys.mComponents.push_back(filtP_profile);

auto filtP = FIRFilter::make("filter_p", coefficients, 0, CPS::Logger::Level::off);
load->setAttributeRef("P", filtP->attribute<Real>("output"));
load->mActivePower->setReference(filtP->mOutput);
sys.mComponents.push_back(filtP);

RealTimeSimulation sim(simName, CPS::Logger::Level::off);
Expand All @@ -97,19 +97,19 @@ int main(int argc, char *argv[]) {
i--;

auto n_dp = std::dynamic_pointer_cast<CPS::DP::SimNode>(n);
auto v = n_dp->attributeMatrixComp("v")->coeff(0, 0);
auto v = n_dp->mVoltage->deriveCoeff<Complex>(0, 0);

std::cout << "Signal " << (i*2)+0 << ": Mag " << n->name() << std::endl;
std::cout << "Signal " << (i*2)+1 << ": Phas " << n->name() << std::endl;

intf.exportReal(v->mag(), (i*2)+0); o++;
intf.exportReal(v->phase(), (i*2)+1); o++;
intf.exportReal(v->deriveMag(), (i*2)+0); o++;
intf.exportReal(v->derivePhase(), (i*2)+1); o++;

logger->addAttribute(fmt::format("mag_{}", i), v->mag());
logger->addAttribute(fmt::format("phase_{}", i), v->phase());
logger->logAttribute(fmt::format("mag_{}", i), v->deriveMag());
logger->logAttribute(fmt::format("phase_{}", i), v->derivePhase());
}

logger->addAttribute("v3", sys.node<CPS::DP::SimNode>("BUS3")->attribute("v"));
logger->logAttribute("v3", sys.node<CPS::DP::SimNode>("BUS3")->mVoltage);

// TODO gain by 20e8
filtP->setInput(intf.importReal(0));
Expand Down
24 changes: 12 additions & 12 deletions examples/cxx/Shmem_WSCC-9bus_CtrlDist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ int main(int argc, char *argv[]) {
std::vector<Real> coefficients_profile = std::vector<Real>(2000, 1./2000);

auto filtP_profile = FIRFilter::make("filter_p_profile", coefficients_profile, 0, CPS::Logger::Level::info);
load_profile->setAttributeRef("power_active", filtP_profile->attribute<Real>("output"));
load_profile->mActivePower->setReference(filtP_profile->mOutput);
sys.mComponents.push_back(filtP_profile);

// Register interface current source and voltage drop
ecs->setAttributeRef("I_ref", intf1.importComplex(0));
intf1.exportComplex(ecs->attributeMatrixComp("v_intf")->coeff(0, 0), 0);
ecs->mCurrentRef->setReference(intf1.importComplex(0));
intf1.exportComplex(ecs->mIntfVoltage->deriveCoeff<Complex>(0, 0), 0);

// TODO: gain by 20e8
filtP_profile->setInput(intf2.importReal(0));
Expand All @@ -95,13 +95,13 @@ int main(int argc, char *argv[]) {

i--;

auto v = n->attributeComplex("v");
auto v = n->attribute<Complex>("v");

std::cout << "Signal " << (i*2)+0 << ": Mag " << n->name() << std::endl;
std::cout << "Signal " << (i*2)+1 << ": Phas " << n->name() << std::endl;

intf2.exportReal(v->mag(), (i*2)+0);
intf2.exportReal(v->phase(), (i*2)+1);
intf2.exportReal(v->deriveMag(), (i*2)+0);
intf2.exportReal(v->derivePhase(), (i*2)+1);
}

sim.run(args.startTime);
Expand All @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) {
// Controllers and filter
std::vector<Real> coefficients = std::vector<Real>(100, 1./100);
auto filtP = FIRFilter::make("filter_p", coefficients, 0, CPS::Logger::Level::info);
load->setAttributeRef("active_power", filtP->attribute<Real>("output"));
load->mActivePower->setReference(filtP->mOutput);

auto sys = SystemTopology(args.sysFreq, SystemNodeList{n1}, SystemComponentList{evs, load, filtP});
RealTimeSimulation sim(args.name + "_2");
Expand All @@ -139,15 +139,15 @@ int main(int argc, char *argv[]) {

// Register voltage source reference and current flowing through source
// multiply with -1 to consider passive sign convention
evs->setAttributeRef("V_ref", intf1.importComplex(0));
evs->mVoltageRef->setReference(intf1.importComplex(0));
// TODO: invalid sign
intf1.exportComplex(evs->attributeMatrixComp("i_intf")->coeff(0, 0), 0);
intf1.exportComplex(evs->mIntfCurrent->deriveCoeff<Complex>(0, 0), 0);

// Register controllable load
filtP->setInput(intf2.importReal(0));
intf2.exportReal(load->attribute<Real>("power_active"), 0);
intf2.exportComplex(load->attributeMatrixComp("v_intf")->coeff(0, 0), 1);
intf2.exportComplex(load->attributeMatrixComp("i_intf")->coeff(0, 0), 2);
intf2.exportReal(load->mActivePower, 0);
intf2.exportComplex(load->mIntfVoltage->deriveCoeff<Complex>(0, 0), 1);
intf2.exportComplex(load->mIntfCurrent->deriveCoeff<Complex>(0, 0), 2);

sim.run(args.startTime);
}
Expand Down
Loading

0 comments on commit 270b380

Please sign in to comment.