Skip to content

Commit

Permalink
#216 Add B values diagram writers
Browse files Browse the repository at this point in the history
  • Loading branch information
Le Courtois Florent committed Dec 8, 2021
1 parent 53fb1cb commit 8828b12
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 10 deletions.
3 changes: 2 additions & 1 deletion sources/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ Context::exportOutputs() {
// Diagram
file::path diagramDirectory(config_.outputDir());
diagramDirectory.append(basename_ + outputs::constants::diagramDirectorySuffix);
outputs::Diagram diagramWriter(outputs::Diagram::DiagramDefinition(basename_, diagramDirectory.generic_string(), generators_, hvdcLineDefinitions_));
outputs::Diagram diagramWriter(
outputs::Diagram::DiagramDefinition(basename_, diagramDirectory.generic_string(), generators_, hvdcLineDefinitions_, shuntDefinitions_));
diagramWriter.write();

if (def_.simulationKind == SimulationKind::SECURITY_ANALYSIS) {
Expand Down
11 changes: 9 additions & 2 deletions sources/Outputs/include/Diagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include "Algo.h"
#include "Constants.h"

#include <string>
#include <vector>
Expand All @@ -42,17 +43,19 @@ class Diagram {
* @param hvdcDefinitions the HVDC definitions to used
*/
DiagramDefinition(const std::string& base, const std::string& directoryPath, const std::vector<algo::GeneratorDefinition>& gens,
const algo::HVDCLineDefinitions& hvdcDefinitions) :
const algo::HVDCLineDefinitions& hvdcDefinitions, const algo::ShuntDefinitions& shuntDefinitions) :
basename(base),
directoryPath(directoryPath),
generators(gens),
hvdcDefinitions(hvdcDefinitions) {}
hvdcDefinitions(hvdcDefinitions),
shuntDefinitions(shuntDefinitions) {}

const std::string basename; ///< basename for file
const std::string directoryPath; ///< directory path for files to write
// non const copies instead of const references because we need to modify them before use
std::vector<algo::GeneratorDefinition> generators; ///< generators found
algo::HVDCLineDefinitions hvdcDefinitions; ///< HVDC definitions
algo::ShuntDefinitions shuntDefinitions;
};

/**
Expand Down Expand Up @@ -106,6 +109,7 @@ class Diagram {
void writeGenerators() const;
/// @brief Write VSC converters diagrams
void writeConverters() const;
void writeShunts() const;

/**
* @brief Write VSC converter diagram
Expand All @@ -121,8 +125,11 @@ class Diagram {
*/
void writeLCC(const algo::HVDCDefinition::ConverterId& converterId, double powerFactor, double pMax) const;

void writeShunt(const inputs::Shunt& shunt) const;

private:
DiagramDefinition def_; ///< Diagram file information
constants::RefShuntsByIdMap shuntsByBusId_;
};
} // namespace outputs
} // namespace dfl
35 changes: 33 additions & 2 deletions sources/Outputs/src/Diagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#include "Diagram.h"

#include "Constants.h"

#include <boost/filesystem.hpp>
#include <fstream>

Expand All @@ -35,12 +33,14 @@ Diagram::Diagram(DiagramDefinition&& def) : def_{std::forward<DiagramDefinition>
std::sort(points.begin(), points.end(),
[](const algo::VSCDefinition::ReactiveCurvePoint& lhs, const algo::VSCDefinition::ReactiveCurvePoint& rhs) { return lhs.p < rhs.p; });
}
shuntsByBusId_ = constants::computeFilteredShuntsByIds(def_.shuntDefinitions);
}

void
Diagram::write() const {
writeGenerators();
writeConverters();
writeShunts();
}

void
Expand Down Expand Up @@ -105,6 +105,37 @@ Diagram::writeLCC(const algo::HVDCDefinition::ConverterId& converterId, double p
ofs.close();
}

void
Diagram::writeShunts() const {
for (const auto& shuntPair : shuntsByBusId_) {
for (const auto& shuntRef : shuntPair.second) {
writeShunt(shuntRef.get());
}
}
}

void
Diagram::writeShunt(const inputs::Shunt& shunt) const {
if (!boost::filesystem::exists(def_.directoryPath)) {
boost::filesystem::create_directories(def_.directoryPath);
}
std::stringstream buffer;
// Modelica requires this file to start with "#1", if it is not present, problems occurs
buffer << "#1" << std::endl;
buffer << "double tableBPu(" << shunt.bSections.size() << ", 2)" << std::endl;

for (auto it = shunt.bSections.begin(); it != shunt.bSections.end(); ++it) {
unsigned int index = it - shunt.bSections.begin();
buffer << index << " " << *it << std::endl;
}

boost::filesystem::path dir(def_.directoryPath);
std::string filename = dir.append(outputs::constants::diagramFilename(shunt.id)).generic_string();
std::ofstream ofs(filename, std::ofstream::out);
ofs << buffer.str();
ofs.close();
}

void
Diagram::writeConverters() const {
for (const auto& hvdcDefPair : def_.hvdcDefinitions.hvdcLines) {
Expand Down
70 changes: 65 additions & 5 deletions tests/outputs/TestDiagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ TEST(Diagram, writeWithCurvePoint) {
3., 30., 33., 330., 100, bus1)};

dfl::algo::HVDCLineDefinitions defs;
dfl::algo::ShuntDefinitions shuntsDefinitions;

dfl::outputs::Diagram DiagramWriter(dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs));
dfl::outputs::Diagram DiagramWriter(
dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs, shuntsDefinitions));

DiagramWriter.write();
testMultiplesFilesEquality(generators, outputDirectory, basename, prefixDir);
Expand Down Expand Up @@ -110,7 +112,9 @@ TEST(Diagram, writeWithCurveAndDefaultPoints) {
3., 30., 33., 330., 100, bus1)};

dfl::algo::HVDCLineDefinitions defs;
dfl::outputs::Diagram DiagramWriter(dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs));
dfl::algo::ShuntDefinitions shuntsDefinitions;
dfl::outputs::Diagram DiagramWriter(
dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs, shuntsDefinitions));

DiagramWriter.write();
testMultiplesFilesEquality(generators, outputDirectory, basename, prefixDir);
Expand All @@ -136,7 +140,9 @@ TEST(Diagram, writeEmpty) {
GeneratorDefinition("G4", GeneratorDefinition::ModelType::PROP_SIGNALN, "04", {}, -20., -2., 22., 220., 100, bus1)};

dfl::algo::HVDCLineDefinitions defs;
dfl::outputs::Diagram DiagramWriter(dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs));
dfl::algo::ShuntDefinitions shuntsDefinitions;
dfl::outputs::Diagram DiagramWriter(
dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs, shuntsDefinitions));

DiagramWriter.write();
std::string directoryPath = outputDirectory.generic_string();
Expand Down Expand Up @@ -185,8 +191,10 @@ TEST(Diagram, writeVSC) {
dfl::algo::HVDCLineDefinitions::BusVSCMap vscIds{};
dfl::algo::HVDCLineDefinitions defs{map, vscIds};
std::vector<dfl::algo::GeneratorDefinition> generators;
dfl::algo::ShuntDefinitions shuntsDefinitions;

dfl::outputs::Diagram DiagramWriter(dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs));
dfl::outputs::Diagram DiagramWriter(
dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs, shuntsDefinitions));

DiagramWriter.write();

Expand Down Expand Up @@ -232,8 +240,10 @@ TEST(Diagram, writeLCC) {
dfl::algo::HVDCLineDefinitions::BusVSCMap vscIds{};
dfl::algo::HVDCLineDefinitions defs{map, vscIds};
std::vector<dfl::algo::GeneratorDefinition> generators;
dfl::algo::ShuntDefinitions shuntsDefinitions;

dfl::outputs::Diagram DiagramWriter(dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs));
dfl::outputs::Diagram DiagramWriter(
dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs, shuntsDefinitions));

DiagramWriter.write();

Expand All @@ -248,3 +258,53 @@ TEST(Diagram, writeLCC) {
ref.append(dfl::outputs::constants::diagramFilename(id)).generic_string());
}
}

TEST(Diagram, TestShunts) {
std::string basename = "TestDiagram";
std::string prefixDir = "Shunts";
boost::filesystem::path outputDirectory("results");
outputDirectory.append(basename);

if (!boost::filesystem::exists(outputDirectory)) {
boost::filesystem::create_directories(outputDirectory);
}
outputDirectory.append(prefixDir + dfl::outputs::constants::diagramDirectorySuffix);

const std::vector<std::string> shuntsIds = {"1", "2", "3", "5"};

std::vector<dfl::inputs::Shunt> shuntsDummy = {dfl::inputs::Shunt("0.1", "5", 0., true, {0., 10.}), dfl::inputs::Shunt("0.2", "5", 0., true, {0., 20.}),
dfl::inputs::Shunt("0.3", "5", 0., true, {0., 30.}), dfl::inputs::Shunt("0.4", "5", 0., true, {0., 40.}),
dfl::inputs::Shunt("0.5", "5", 0., true, {0., 50.})};
std::vector<dfl::inputs::Shunt> shunts = {dfl::inputs::Shunt("1", "5", 0., true, {0., 10., 20., 30., 40.}), dfl::inputs::Shunt("2", "5", 0., true, {0., 20.}),
dfl::inputs::Shunt("3", "5", 0., true, {0., 13., 23., 33.}),
dfl::inputs::Shunt("4", "5", 0., false, {0., 40., 80.}), dfl::inputs::Shunt("5", "5", 0., true, {0., 50.})};
dfl::algo::ShuntDefinitions shuntsDefinitions;
shuntsDefinitions.shunts["VL1"].dynamicModelAssociated = true;
shuntsDefinitions.shunts["VL1"].shunts.insert(shuntsDummy.begin(), shuntsDummy.end());
shuntsDefinitions.shunts["VL2"].shunts.insert(shunts.begin(), shunts.end());
dfl::algo::HVDCLineDefinitions::HvdcLineMap map;
dfl::algo::HVDCLineDefinitions::BusVSCMap vscIds{};
dfl::algo::HVDCLineDefinitions defs{map, vscIds};
std::vector<dfl::algo::GeneratorDefinition> generators;

dfl::outputs::Diagram DiagramWriter(
dfl::outputs::Diagram::DiagramDefinition(basename, outputDirectory.generic_string(), generators, defs, shuntsDefinitions));

DiagramWriter.write();

// check reference
boost::filesystem::path reference("reference");
reference.append(basename);
reference.append(prefixDir + dfl::outputs::constants::diagramDirectorySuffix);
auto nbFilesReference = std::count_if(boost::filesystem::directory_iterator(reference), boost::filesystem::directory_iterator(),
static_cast<bool (*)(const boost::filesystem::path&)>(boost::filesystem::is_regular_file));
auto nbFiles = std::count_if(boost::filesystem::directory_iterator(outputDirectory), boost::filesystem::directory_iterator(),
static_cast<bool (*)(const boost::filesystem::path&)>(boost::filesystem::is_regular_file));
ASSERT_EQ(nbFiles, nbFilesReference);
for (const auto& id : shuntsIds) {
boost::filesystem::path ref(reference);
boost::filesystem::path outputDir(outputDirectory);
dfl::test::checkFilesEqual(outputDir.append(dfl::outputs::constants::diagramFilename(id)).generic_string(),
ref.append(dfl::outputs::constants::diagramFilename(id)).generic_string());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#1
double tableBPu(5, 2)
0 0
1 10
2 20
3 30
4 40
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#1
double tableBPu(2, 2)
0 0
1 20
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#1
double tableBPu(4, 2)
0 0
1 13
2 23
3 33
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#1
double tableBPu(2, 2)
0 0
1 50

0 comments on commit 8828b12

Please sign in to comment.