From 9a4a3d5a7761d9ff5beb3a2292c92d132b0f751f Mon Sep 17 00:00:00 2001 From: Le Courtois Florent Date: Fri, 1 Oct 2021 14:31:08 +0200 Subject: [PATCH] #216 Add voltage level with association in shunt algo --- sources/Algo/include/Algo.h | 14 ++++++++++++-- sources/Algo/src/Algo.cpp | 15 +++++++++++++-- sources/Context.cpp | 2 +- sources/Outputs/src/Par.cpp | 5 +++-- tests/algo/CMakeLists.txt | 2 +- tests/algo/TestAlgo.cpp | 21 +++++++++++++++------ tests/outputs/TestPar.cpp | 11 +++++++---- 7 files changed, 52 insertions(+), 18 deletions(-) diff --git a/sources/Algo/include/Algo.h b/sources/Algo/include/Algo.h index e8e4197e8..26e4d8270 100644 --- a/sources/Algo/include/Algo.h +++ b/sources/Algo/include/Algo.h @@ -863,11 +863,20 @@ class DynModelAlgorithm : public NodeAlgorithm { const inputs::DynamicDataBaseManager& manager_; ///< dynamic database config manager }; +struct VLShuntsDefinition { + struct ShuntHash { + size_t operator()(const inputs::Shunt& shunt) const noexcept; + }; + + std::unordered_set shunts; + bool dynamicModelAssociated = false; +}; + /** * @brief Shunt definitions */ struct ShuntDefinitions { - std::unordered_map nbShunts; ///< Number of shunts by voltage level + std::unordered_map shunts; ///< Number of shunts by voltage level }; /** @@ -879,7 +888,7 @@ class ShuntDefinitionAlgorithm : public NodeAlgorithm { * @brief Constructor * @param shuntCounterDefs the counter definitions to update */ - explicit ShuntDefinitionAlgorithm(ShuntDefinitions& shuntCounterDefs); + ShuntDefinitionAlgorithm(ShuntDefinitions& shuntCounterDefs, const inputs::DynamicDataBaseManager& manager); /** * @brief Performs the algorithm @@ -892,6 +901,7 @@ class ShuntDefinitionAlgorithm : public NodeAlgorithm { private: ShuntDefinitions& shuntDefs_; ///< the counter definitions to update + std::unordered_set voltageLevelsWithAssociation_; }; /** diff --git a/sources/Algo/src/Algo.cpp b/sources/Algo/src/Algo.cpp index 295d1f116..f94c79f42 100644 --- a/sources/Algo/src/Algo.cpp +++ b/sources/Algo/src/Algo.cpp @@ -644,12 +644,23 @@ DynModelAlgorithm::MacroConnectHash::operator()(const MacroConnect& connect) con ///////////////////////////////////////////////////////////////////////////////// -ShuntDefinitionAlgorithm::ShuntDefinitionAlgorithm(ShuntDefinitions& defs) : shuntDefs_(defs) {} +ShuntDefinitionAlgorithm::ShuntDefinitionAlgorithm(ShuntDefinitions& defs, const inputs::DynamicDataBaseManager& manager) : shuntDefs_(defs) { + const auto& multiAssociations = manager.assemblingDocument().multipleAssociations(); + std::transform(multiAssociations.begin(), multiAssociations.end(), std::inserter(voltageLevelsWithAssociation_, voltageLevelsWithAssociation_.begin()), + [](const inputs::AssemblingXmlDocument::MultipleAssociation& association) { return association.shunt.voltageLevel; }); +} void ShuntDefinitionAlgorithm::operator()(const NodePtr& node) { auto vl = node->voltageLevel.lock(); - shuntDefs_.nbShunts[vl->id] += node->shunts.size(); + auto& map = shuntDefs_.shunts[vl->id]; + std::copy(node->shunts.begin(), node->shunts.end(), std::inserter(map.shunts, map.shunts.end())); + map.dynamicModelAssociated = (voltageLevelsWithAssociation_.count(vl->id) > 0); +} + +size_t +VLShuntsDefinition::ShuntHash::operator()(const inputs::Shunt& shunt) const noexcept { + return std::hash{}(shunt.id); } ////////////////////////////////////////////////////////////////////////////////// diff --git a/sources/Context.cpp b/sources/Context.cpp index 4b0e46207..8b0e277c4 100644 --- a/sources/Context.cpp +++ b/sources/Context.cpp @@ -66,7 +66,7 @@ Context::Context(const ContextDef& def, const inputs::Configuration& config) : networkManager_.onNode(algo::MainConnexComponentAlgorithm(mainConnexNodes_)); networkManager_.onNode(algo::DynModelAlgorithm(dynamicModels_, dynamicDataBaseManager_)); - networkManager_.onNode(algo::ShuntDefinitionAlgorithm(shunts_)); + networkManager_.onNode(algo::ShuntDefinitionAlgorithm(shunts_, dynamicDataBaseManager_)); networkManager_.onNode(algo::LinesByIdAlgorithm(linesById_)); } diff --git a/sources/Outputs/src/Par.cpp b/sources/Outputs/src/Par.cpp index 4aadd7227..1da7e1fe6 100644 --- a/sources/Outputs/src/Par.cpp +++ b/sources/Outputs/src/Par.cpp @@ -263,11 +263,12 @@ Par::writeDynamicModelParameterSet(const inputs::SettingXmlDocument::Set& set, c LOG(debug) << "Count id " << count.id << " not found as a multiple association in assembling: Configuration error" << LOG_ENDL; continue; } - if (shunts.nbShunts.count(found->second.shunt.voltageLevel) == 0) { + if (shunts.shunts.count(found->second.shunt.voltageLevel) == 0) { // case voltage level not in network, skip continue; } - new_set->addParameter(helper::buildParameter(count.name, static_cast(shunts.nbShunts.at(found->second.shunt.voltageLevel)))); + const auto& shuntsSet = shunts.shunts.at(found->second.shunt.voltageLevel).shunts; + new_set->addParameter(helper::buildParameter(count.name, static_cast(shuntsSet.size()))); } for (const auto& param : set.boolParameters) { diff --git a/tests/algo/CMakeLists.txt b/tests/algo/CMakeLists.txt index fa36bf5f2..935896c5f 100644 --- a/tests/algo/CMakeLists.txt +++ b/tests/algo/CMakeLists.txt @@ -7,7 +7,7 @@ # SPDX-License-Identifier: MPL-2.0 # -DEFINE_TEST(TestAlgo ALGO) +DEFINE_TEST_XML(TestAlgo ALGO) target_link_libraries(TestAlgo DynaFlowLauncher::algo) # Dummy Library for algo test diff --git a/tests/algo/TestAlgo.cpp b/tests/algo/TestAlgo.cpp index c928bd57e..8abeb291d 100644 --- a/tests/algo/TestAlgo.cpp +++ b/tests/algo/TestAlgo.cpp @@ -30,6 +30,11 @@ #include #include +// Required for testing unit tests +testing::Environment* initXmlEnvironment(); + +testing::Environment* const env = initXmlEnvironment(); + namespace test { /** @@ -690,7 +695,7 @@ TEST(Generators, oneReactiveCurvePoint) { testDiagramValidity(points, isDiagramValid); } -TEST(Counter, base) { +TEST(Shunts, base) { auto vl = std::make_shared("VL"); auto vl2 = std::make_shared("VL2"); auto vl3 = std::make_shared("VL3"); @@ -708,15 +713,19 @@ TEST(Counter, base) { dfl::inputs::Node::build("6", vl2, 0.0, shunts6), }; + dfl::inputs::DynamicDataBaseManager manager("res/setting.xml", "res/assembling.xml"); dfl::algo::ShuntDefinitions defs; - dfl::algo::ShuntDefinitionAlgorithm algo(defs); + dfl::algo::ShuntDefinitionAlgorithm algo(defs, manager); std::for_each(nodes.begin(), nodes.end(), algo); - ASSERT_EQ(defs.nbShunts.size(), 3); - ASSERT_EQ(defs.nbShunts.at("VL"), 6); - ASSERT_EQ(defs.nbShunts.at("VL2"), 15); - ASSERT_EQ(defs.nbShunts.at("VL3"), 0); + ASSERT_EQ(defs.shunts.size(), 3); + ASSERT_EQ(defs.shunts.at("VL").shunts.size(), 6); + ASSERT_EQ(defs.shunts.at("VL").dynamicModelAssociated, true); + ASSERT_EQ(defs.shunts.at("VL2").shunts.size(), 15); + ASSERT_EQ(defs.shunts.at("VL2").dynamicModelAssociated, false); + ASSERT_EQ(defs.shunts.at("VL3").shunts.size(), 0); + ASSERT_EQ(defs.shunts.at("VL3").dynamicModelAssociated, false); } TEST(LinesByIds, base) { diff --git a/tests/outputs/TestPar.cpp b/tests/outputs/TestPar.cpp index f59a2fa92..95005fe66 100644 --- a/tests/outputs/TestPar.cpp +++ b/tests/outputs/TestPar.cpp @@ -186,9 +186,12 @@ TEST(TestPar, DynModel) { boost::filesystem::create_directories(outputPath); } - dfl::algo::ShuntDefinitions counters; - counters.nbShunts["VL"] = 2; - counters.nbShunts["VL2"] = 1; + dfl::algo::ShuntDefinitions shuntsDefinitions; + shuntsDefinitions.shunts["VL"].shunts = { + dfl::inputs::Shunt("1"), + dfl::inputs::Shunt("1.1"), + }; + shuntsDefinitions.shunts["VL2"].shunts = {dfl::inputs::Shunt("2")}; dfl::algo::DynamicModelDefinitions defs; dfl::algo::DynamicModelDefinition dynModel("DM_VL61", "DummyLib"); @@ -205,7 +208,7 @@ TEST(TestPar, DynModel) { outputPath.append(filename); dfl::inputs::Configuration::ActivePowerCompensation activePowerCompensation(dfl::inputs::Configuration::ActivePowerCompensation::P); dfl::outputs::Par parWriter(dfl::outputs::Par::ParDefinition(basename, dirname, outputPath.generic_string(), generators, {}, activePowerCompensation, {}, - manager, counters, defs, {}, {})); + manager, shuntsDefinitions, defs, {}, {})); parWriter.write();