From 6c3dc79f37182067f0d987836fc41661dcfb78cf Mon Sep 17 00:00:00 2001 From: stani Date: Thu, 13 Mar 2025 23:18:38 +0100 Subject: [PATCH] fixed memory leak --- CMakeLists.txt | 6 ++ src/interfaces/ICostComponent.h | 21 +++++- src/model/Building.cpp | 42 +++++++++++ src/model/Building.h | 74 +++++++++++-------- src/model/Community.h | 8 +- src/model/Energy_Tariff.h | 8 +- src/services/Cost/CalculateFinalSums.cpp | 18 +++++ src/services/Cost/CalculateFinalSums.h | 17 +++++ src/services/Cost/CostPipeline.h | 3 +- .../Cost/GridCost/MeasurementServiceFee.cpp | 35 +++++++++ .../Cost/GridCost/MeasurementServiceFee.h | 18 +++++ .../Cost/GridCost/NetUsagePerformance.cpp | 22 ++++++ .../Cost/GridCost/NetUsagePerformance.h | 17 +++++ src/services/Cost/TaxComponent.h | 2 +- src/services/CostHistory.h | 8 +- src/services/Surplus.cpp | 17 ++--- src/services/Surplus.h | 4 +- tests/model/Factory.h | 14 ++-- tests/services/test_Surplus.cpp | 16 ++++ 19 files changed, 284 insertions(+), 66 deletions(-) create mode 100644 src/services/Cost/CalculateFinalSums.cpp create mode 100644 src/services/Cost/CalculateFinalSums.h create mode 100644 src/services/Cost/GridCost/MeasurementServiceFee.cpp create mode 100644 src/services/Cost/GridCost/MeasurementServiceFee.h create mode 100644 src/services/Cost/GridCost/NetUsagePerformance.cpp create mode 100644 src/services/Cost/GridCost/NetUsagePerformance.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 44b98f2..3180459 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,12 @@ add_executable(Sim_C__ src/config.h src/services/Cost/NetworkProvider.cpp src/services/Cost/NetworkProvider.h + src/services/Cost/CalculateFinalSums.cpp + src/services/Cost/CalculateFinalSums.h + src/services/Cost/GridCost/MeasurementServiceFee.cpp + src/services/Cost/GridCost/MeasurementServiceFee.h + src/services/Cost/GridCost/NetUsagePerformance.cpp + src/services/Cost/GridCost/NetUsagePerformance.h src/services/Cost/BillCharge.cpp src/services/Cost/BillCharge.h src/services/CostHistory.cpp diff --git a/src/interfaces/ICostComponent.h b/src/interfaces/ICostComponent.h index d07dfa3..c44aa17 100644 --- a/src/interfaces/ICostComponent.h +++ b/src/interfaces/ICostComponent.h @@ -6,11 +6,30 @@ #include "../Config.h" class ICostComponent { + protected: + enum NET_LEVEL + { + NetLevel1, + NetLevel2, + NetLevel3, + NetLevel4, + NetLevel5, + NetLevel6, + NetLevel7mp, + NetLevel7op, + NetLevel7unt + }; + enum MEASUREMENT_TYPE + { + NIEDER_WANDLERZAEHLER, + MITTLE_WANDLERZAEHLER, + DREHSTROM_ZEAHLUNG + }; public: std::bitset applicableConditions; template - ICostComponent(Conditions... conditions) { + explicit ICostComponent(Conditions... conditions) { } virtual ~ICostComponent() = default; diff --git a/src/model/Building.cpp b/src/model/Building.cpp index 31eec31..9085d48 100644 --- a/src/model/Building.cpp +++ b/src/model/Building.cpp @@ -4,6 +4,8 @@ #include "Building.h" +#include + float Building::Metadata::special_rate_consumption() const { return SpecialRateConsumption; } @@ -60,6 +62,46 @@ void Building::Metadata::set_generation_net_price(const float value) GenerationNetPrice = value; } +float Building::Metadata::total_cost_with() const +{ + return TotalCostWith; +} + +void Building::Metadata::set_total_cost_with(const float value) +{ + TotalCostWith = value; +} + +float Building::Metadata::total_cost_without() const +{ + return TotalCostWithout; +} + +void Building::Metadata::set_total_cost_without(const float value) +{ + TotalCostWithout = value; +} + +std::unordered_map Building::Metadata::total_by_category_with() const +{ + return TotalByCategoryWith; +} + +void Building::Metadata::set_total_by_category_with(std::unordered_map value) +{ + TotalByCategoryWith = std::move(value); +} + +std::unordered_map Building::Metadata::total_by_category_without() const +{ + return TotalByCategoryWithout; +} + +void Building::Metadata::set_total_by_category_without(std::unordered_map value) +{ + TotalByCategoryWithout = std::move(value); +} + std::vector &Building::Simulation_Values::generation_after_community() { return GenerationAfterCommunity; } diff --git a/src/model/Building.h b/src/model/Building.h index a838ed5..87f3c78 100644 --- a/src/model/Building.h +++ b/src/model/Building.h @@ -18,8 +18,8 @@ namespace Building class Cost { private: - std::shared_ptr CostValuesWith{}; - std::shared_ptr CostValuesWithOut{}; + std::shared_ptr CostValuesWith{std::make_shared()}; + std::shared_ptr CostValuesWithOut{std::make_shared()}; public: std::shared_ptr& get_cost_values_with() { return CostValuesWith; } @@ -70,11 +70,11 @@ namespace Building void set_needed_gen(std::vector needed_gen); - float relativeSelfConsumption() const; + [[nodiscard]] float relativeSelfConsumption() const; void set_relativeSelfConsumption(float relativeSelfConsumption); - float relativeSelfGeneration() const; + [[nodiscard]] float relativeSelfGeneration() const; void set_relativeSelfGeneration(float relativeSelfGeneration); @@ -94,29 +94,29 @@ namespace Building void consumption_from_community(const std::vector& consumption_from_community); - float con_from_community_relative() const; + [[nodiscard]] float con_from_community_relative() const; void set_con_from_community_relative(float x); - float gen_to_community_relative() const; + [[nodiscard]] float gen_to_community_relative() const; void set_gen_to_community_relative(float x); - float consumption_after_own_coverage_sum() const; + [[nodiscard]] float consumption_after_own_coverage_sum() const; void set_consumption_after_own_coverage_sum(float value); - float own_usage_sum() const; + [[nodiscard]] float own_usage_sum() const; void set_ownUsageSum(float value); - float needed_con_sum() const; + [[nodiscard]] float needed_con_sum() const; void set_neededConSum(float value); - float needed_gen_sum() const; + [[nodiscard]] float needed_gen_sum() const; void set_neededGenSum(float value); - float consumption_after_community_sum() const; + [[nodiscard]] float consumption_after_community_sum() const; void set_consumption_after_community_sum(float value); - float generation_after_community_sum() const; + [[nodiscard]] float generation_after_community_sum() const; void set_generation_after_community_sum(float value); - float consumption_from_community_sum() const; + [[nodiscard]] float consumption_from_community_sum() const; void set_consumption_from_community_sum(float value); - float generation_to_community_sum() const; + [[nodiscard]] float generation_to_community_sum() const; void set_generation_to_community_sum(float values); }; @@ -138,17 +138,21 @@ namespace Building std::string GridOperator; float ConsumptionNetPrice{0.0f}; float GenerationNetPrice{0.0f}; + float TotalCostWith{0.0f}; + float TotalCostWithout{0.0f}; + std::unordered_map TotalByCategoryWith{}; + std::unordered_map TotalByCategoryWithout{}; public: Metadata() = default; - std::string name() const; + [[nodiscard]] std::string name() const; void set_name(const std::string& name); - float annual_consumption() const; + [[nodiscard]] float annual_consumption() const; void set_annual_consumption(float annual_consumption); - float annual_generation() const; + [[nodiscard]] float annual_generation() const; void set_annual_generation(float annual_generation); std::vector& consumption_profile(); @@ -157,43 +161,55 @@ namespace Building std::vector& generation_profile(); void set_generation_profile(std::vector generation_profile); - std::string consumption_profile_name() const; + [[nodiscard]] std::string consumption_profile_name() const; void set_consumption_profile_name(const std::string& consumption_profile_name); - std::string generation_profile_name() const; + [[nodiscard]] std::string generation_profile_name() const; void set_generation_profile_name(const std::string& generation_profile_name); - short connection_power() const; + [[nodiscard]] short connection_power() const; void set_connection_power(short connection_power); - short grid_power() const; + [[nodiscard]] short grid_power() const; void set_grid_power(short grid_power); - float special_rate_consumption() const; + [[nodiscard]] float special_rate_consumption() const; void set_special_rate_consumption(float special_rate_consumption); - float special_rate_generation() const; + [[nodiscard]] float special_rate_generation() const; void set_special_rate_generation(float special_rate_generation); - short grid_level() const; + [[nodiscard]] short grid_level() const; void set_grid_level(short value); std::string grid_operator(); void set_grid_operator(std::string value); - float consumption_net_price() const; + [[nodiscard]] float consumption_net_price() const; void set_consumption_net_price(float value); - float generation_net_price() const; + [[nodiscard]] float generation_net_price() const; void set_generation_net_price(float value); + + [[nodiscard]] float total_cost_with() const; + void set_total_cost_with(float value); + + [[nodiscard]] float total_cost_without() const; + void set_total_cost_without(float value); + + [[nodiscard]] std::unordered_map total_by_category_with() const; + void set_total_by_category_with(std::unordered_map value); + + [[nodiscard]] std::unordered_map total_by_category_without() const; + void set_total_by_category_without(std::unordered_map value); }; class Base : public Model { private: - std::unique_ptr Cost{}; - std::shared_ptr Values{}; - std::shared_ptr Metadata{}; + std::unique_ptr Cost{std::make_unique()}; + std::shared_ptr Values{std::make_unique()}; + std::shared_ptr Metadata{std::make_unique()}; public: std::unique_ptr& cost(); diff --git a/src/model/Community.h b/src/model/Community.h index 7ac55e7..a46acec 100644 --- a/src/model/Community.h +++ b/src/model/Community.h @@ -15,7 +15,9 @@ class Community : public Model { public: - std::string name() const; + float total{0.f}; + + [[nodiscard]] std::string name() const; void set_name(const std::string &name); @@ -23,7 +25,7 @@ public: void set_buildings(std::vector > buildings); - Energy_Tariff energy_tariff() const; + [[nodiscard]] Energy_Tariff energy_tariff() const; void set_energy_tariff(const Energy_Tariff &energy_tariff); @@ -39,8 +41,6 @@ public: void set_is_gen_bigger_than_con(std::vector &isGenBiggerThanCon); - std::bitset getConditionsBitmask() const; - static std::function &)>(const std:: function &)> diff --git a/src/model/Energy_Tariff.h b/src/model/Energy_Tariff.h index 2f870a4..9fb342c 100644 --- a/src/model/Energy_Tariff.h +++ b/src/model/Energy_Tariff.h @@ -8,10 +8,10 @@ class Energy_Tariff { public: - float consumption_tariff{0.0f}; - float generation_tariff{0.0f}; - float bill_charge{0.0f}; - float service_charge{0.0f}; + float consumption_tariff{1.0f}; + float generation_tariff{1.0f}; + float bill_charge{1.0f}; + float service_charge{1.0f}; }; diff --git a/src/services/Cost/CalculateFinalSums.cpp b/src/services/Cost/CalculateFinalSums.cpp new file mode 100644 index 0000000..6fc2a1a --- /dev/null +++ b/src/services/Cost/CalculateFinalSums.cpp @@ -0,0 +1,18 @@ +// +// Created by stani on 3/13/2025. +// + +#include "CalculateFinalSums.h" + +void CalculateFinalSums::apply(std::unique_ptr& building, std::unique_ptr& community) +{ + const std::shared_ptr& valuesWith = building->cost()->get_cost_values_with(); + const std::shared_ptr& valuesWithout = building->cost()->get_cost_values_without(); + const auto& metadata = building->metadata(); + metadata->set_total_cost_with(valuesWith->total_cost()); + metadata->set_total_by_category_with(valuesWith->calculateCategories()); + + metadata->set_total_cost_without(valuesWithout->total_cost()); + metadata->set_total_by_category_without(valuesWithout->calculateCategories()); + community->total = valuesWithout->total_cost(); +} diff --git a/src/services/Cost/CalculateFinalSums.h b/src/services/Cost/CalculateFinalSums.h new file mode 100644 index 0000000..80636e8 --- /dev/null +++ b/src/services/Cost/CalculateFinalSums.h @@ -0,0 +1,17 @@ +// +// Created by stani on 3/13/2025. +// + +#ifndef CALCULATEFINALSUMS_H +#define CALCULATEFINALSUMS_H +#include "../../interfaces/ICostComponent.h" + + +class CalculateFinalSums : public ICostComponent{ +public: + void apply(std::unique_ptr& building, std::unique_ptr& community) override; +}; + + + +#endif //CALCULATEFINALSUMS_H diff --git a/src/services/Cost/CostPipeline.h b/src/services/Cost/CostPipeline.h index 70f41b3..17beb11 100644 --- a/src/services/Cost/CostPipeline.h +++ b/src/services/Cost/CostPipeline.h @@ -7,7 +7,6 @@ #include #include -#include "NetworkProvider.h" #include "../../interfaces/ICostComponent.h" #include "../../model/Community.h" @@ -16,7 +15,7 @@ class CostPipeline { std::vector > components; public: - CostPipeline(std::vector > &communities) : communities(communities) { + explicit CostPipeline(std::vector > &communities) : communities(communities) { } void addCostComponent(const std::shared_ptr &components); diff --git a/src/services/Cost/GridCost/MeasurementServiceFee.cpp b/src/services/Cost/GridCost/MeasurementServiceFee.cpp new file mode 100644 index 0000000..cd7828d --- /dev/null +++ b/src/services/Cost/GridCost/MeasurementServiceFee.cpp @@ -0,0 +1,35 @@ +// +// Created by stani on 3/13/2025. +// + +#include "MeasurementServiceFee.h" + +void MeasurementServiceFee::apply(std::unique_ptr& building, std::unique_ptr& community) +{ + const auto& valuesWith = building->cost()->get_cost_values_with(); + const auto& valuesWithout = building->cost()->get_cost_values_without(); + const short grid_level = building->metadata()->grid_level(); + + //TODO use actual value + float measurement_cost = 1.f; + const short measurementType = getMeasurementServiceFeeType(grid_level); + + valuesWith->add_cost_point("Messentgelt",measurement_cost,1.f); + valuesWithout->add_cost_point("Messentgelt",measurement_cost,1.f); +} + +short MeasurementServiceFee::getMeasurementServiceFeeType(const short grid_level) +{ + switch (grid_level) + { + case NetLevel6: + case NetLevel7mp: + return MITTLE_WANDLERZAEHLER; + case NetLevel7op: + case NetLevel7unt: + return DREHSTROM_ZEAHLUNG; + default: + return NIEDER_WANDLERZAEHLER; + } +} + diff --git a/src/services/Cost/GridCost/MeasurementServiceFee.h b/src/services/Cost/GridCost/MeasurementServiceFee.h new file mode 100644 index 0000000..9a4a174 --- /dev/null +++ b/src/services/Cost/GridCost/MeasurementServiceFee.h @@ -0,0 +1,18 @@ +// +// Created by stani on 3/13/2025. +// + +#ifndef MEASUREMENTSERVICEFEE_H +#define MEASUREMENTSERVICEFEE_H +#include "../../../interfaces/ICostComponent.h" + + +class MeasurementServiceFee : public ICostComponent{ +public: + void apply(std::unique_ptr& building, std::unique_ptr& community) override; + [[nodiscard]] short getMeasurementServiceFeeType(short grid_level); +}; + + + +#endif //MEASUREMENTSERVICEFEE_H diff --git a/src/services/Cost/GridCost/NetUsagePerformance.cpp b/src/services/Cost/GridCost/NetUsagePerformance.cpp new file mode 100644 index 0000000..b4cbfd2 --- /dev/null +++ b/src/services/Cost/GridCost/NetUsagePerformance.cpp @@ -0,0 +1,22 @@ +// +// Created by stani on 3/13/2025. +// + +#include "NetUsagePerformance.h" + +void NetUsagePerformance::apply(std::unique_ptr& building, std::unique_ptr& community) +{ + const auto& valuesWith = building->cost()->get_cost_values_with(); + const auto& valuesWithout = building->cost()->get_cost_values_without(); + const auto& data = building->metadata(); + float connectionPower = 1; + //TODO use actual value + float netznutzungLeistungspreis = 1.f; + + if (data->grid_level()!=NetLevel7op) + { + connectionPower=data->connection_power(); + } + valuesWith->add_cost_point("Netznutzungs Leistungspreis",netznutzungLeistungspreis*connectionPower,1.f); + valuesWithout->add_cost_point("Netznutzungs Leistungspreis",netznutzungLeistungspreis*connectionPower,1.f); +} diff --git a/src/services/Cost/GridCost/NetUsagePerformance.h b/src/services/Cost/GridCost/NetUsagePerformance.h new file mode 100644 index 0000000..47a2fcd --- /dev/null +++ b/src/services/Cost/GridCost/NetUsagePerformance.h @@ -0,0 +1,17 @@ +// +// Created by stani on 3/13/2025. +// + +#ifndef NETWORKUSAGEPERFORMANCE_H +#define NETWORKUSAGEPERFORMANCE_H +#include "../../../interfaces/ICostComponent.h" + + +class NetUsagePerformance : public ICostComponent{ +public: + void apply(std::unique_ptr& building, std::unique_ptr& community) override; +}; + + + +#endif //NETWORKUSAGEPERFORMANCE_H diff --git a/src/services/Cost/TaxComponent.h b/src/services/Cost/TaxComponent.h index ce0eb78..3cee0c9 100644 --- a/src/services/Cost/TaxComponent.h +++ b/src/services/Cost/TaxComponent.h @@ -11,7 +11,7 @@ class TaxComponent : public ICostComponent { double taxRate; public: - TaxComponent(double rate) : ICostComponent(), taxRate(rate) { + explicit TaxComponent(const double rate) : ICostComponent(), taxRate(rate) { } void apply(std::unique_ptr &building, std::unique_ptr &community) override; diff --git a/src/services/CostHistory.h b/src/services/CostHistory.h index f5fffd7..623b5ce 100644 --- a/src/services/CostHistory.h +++ b/src/services/CostHistory.h @@ -9,19 +9,17 @@ #include #include -#include "../helper/StringOperations.h" - class CostHistory { private: struct CostPoint { std::string name; - float value; - float amount; + float value{0.0f}; + float amount{0.f}; CostPoint(std::string name, const float value, const float amount) : name(std::move(name)), value(value), amount(amount) {} - float total() const { + [[nodiscard]] float total() const { return value * amount; } }; diff --git a/src/services/Surplus.cpp b/src/services/Surplus.cpp index 3bd63cf..c2477a6 100644 --- a/src/services/Surplus.cpp +++ b/src/services/Surplus.cpp @@ -9,22 +9,17 @@ #include void Surplus::CalculateSurplus() { - auto iterateFunc = Community::iterateBuildings(communities); + auto iterateFunc = Community::iterateBuildings(communities); - auto modifyCommunity = [this](Community &c) { + auto modifyCommunity = [this](const Community &c) { this->consumptionAvailable = std::vector(VALUE_COUNT, 0.f); this->generationAvailable = std::vector(VALUE_COUNT, 0.f); assert(!c.name().empty() && "Community name is empty!"); - LOG_DEBUG_INFO("Calculating Surplus for Community: {}", c.name()); }; auto modifyBuildingSurplus = [this](Community &community, Building::Base &building) { - assert(building.metadata() != nullptr && "Building metadata is null!"); - - LOG_DEBUG_INFO("Calculating Surplus for Building: {} in Community: {}", - building.metadata()->name(), community.name()); CalculateBuildingSurplus(community, building); @@ -52,10 +47,10 @@ void Surplus::CalculateSurplus() { } void Surplus::CalculateBuildingSurplus(Community &community, Building::Base &building) { - std::vector ownCoverage = std::vector(VALUE_COUNT, 0.0f); - std::vector neededConsumption = std::vector(VALUE_COUNT, 0.0f); - std::vector neededGeneration = std::vector(VALUE_COUNT, 0.0f); - std::vector ownUsage = std::vector(VALUE_COUNT, 0.0f); + auto ownCoverage = std::vector(VALUE_COUNT, 0.0f); + auto neededConsumption = std::vector(VALUE_COUNT, 0.0f); + auto neededGeneration = std::vector(VALUE_COUNT, 0.0f); + auto ownUsage = std::vector(VALUE_COUNT, 0.0f); assert(building.metadata() != nullptr && "Building metadata is null!"); diff --git a/src/services/Surplus.h b/src/services/Surplus.h index 29ae247..81f3ba5 100644 --- a/src/services/Surplus.h +++ b/src/services/Surplus.h @@ -18,7 +18,7 @@ private: std::vector generationAvailable; public: - Surplus(std::vector > &communities) : communities(communities), + explicit Surplus(std::vector > &communities) : communities(communities), consumptionAvailable(VALUE_COUNT, 0.0f), generationAvailable(VALUE_COUNT, 0.0f) { } @@ -27,7 +27,7 @@ public: void CalculateBuildingSurplus(Community &community, Building::Base &); - void CalculateSurplusCommunity(Community &community, Building::Base &base); + static void CalculateSurplusCommunity(Community &community, Building::Base &base); }; diff --git a/tests/model/Factory.h b/tests/model/Factory.h index 31507dd..71628ac 100644 --- a/tests/model/Factory.h +++ b/tests/model/Factory.h @@ -25,13 +25,13 @@ public: auto neededGen = std::vector(); for (int i = 0; i < 5; i++) { - // Populate with test values - communityCoverage.push_back(i * 10.0f); - ownUsage.push_back(i * 5.0f); - neededCon.push_back(i * 3.0f); - neededGen.push_back(i * 2.0f); + communityCoverage.push_back(static_cast(i) * 10.0f); + ownUsage.push_back(static_cast(i) * 5.0f); + neededCon.push_back(static_cast(i) * 3.0f); + neededGen.push_back(static_cast(i) * 2.0f); } + values->set_community_coverage(std::move(communityCoverage)); values->set_own_usage(std::move(ownUsage)); values->set_needed_con(std::move(neededCon)); @@ -58,8 +58,8 @@ public: for (int i = 0; i < 5; i++) { // Populate with test values - consumptionProfile.push_back(i * 100.0f); - generationProfile.push_back(i * 50.0f); + consumptionProfile.push_back(static_cast(i) * 100.0f); + generationProfile.push_back(static_cast(i) * 50.0f); } metadata->set_consumption_profile(std::move(consumptionProfile)); diff --git a/tests/services/test_Surplus.cpp b/tests/services/test_Surplus.cpp index 8b269fc..80090ec 100644 --- a/tests/services/test_Surplus.cpp +++ b/tests/services/test_Surplus.cpp @@ -2,11 +2,19 @@ // Created by StanislausCichocki on 10.03.2025. // #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include + #include "../../src/services/Surplus.h" +#include "../../src/services/Cost/BillCharge.h" +#include "../../src/services/Cost/CalculateFinalSums.h" #include "../../src/services/Cost/CostPipeline.h" #include "../../src/services/Cost/NetworkProvider.h" +#include "../../src/services/Cost/ServiceCharge.h" +#include "../../src/services/Cost/GridCost/MeasurementServiceFee.h" +#include "../../src/services/Cost/GridCost/NetUsagePerformance.h" #include "doctest/doctest.h" #include "../model/Factory.h" +#include "../../src/Config.h" TEST_CASE("Testing add function") { std::vector > communities; @@ -17,5 +25,13 @@ TEST_CASE("Testing add function") { surplus.CalculateSurplus(); CostPipeline costPipeline(communities); costPipeline.addCostComponent(std::make_shared()); + costPipeline.addCostComponent(std::make_shared()); + costPipeline.addCostComponent(std::make_shared()); + costPipeline.addCostComponent(std::make_shared()); + costPipeline.addCostComponent(std::make_shared()); + costPipeline.addCostComponent(std::make_shared()); + costPipeline.calculateFinalCost(); + + LOG_DEBUG_INFO("{}",communities[0]->total); CHECK(true); }