Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/enum.hpp"
#include "common/grouped_index_vector.hpp"
#include "common/maybe_owning_view.hpp"
#include "common/small_vector.hpp"
#include "common/statistics.hpp"
#include "common/three_phase_tensor.hpp"

Expand Down Expand Up @@ -410,10 +411,14 @@ struct OptimizerOutput {

template <typename T> struct SupernodeOutput;

// One entry per user node of a topological node. Only link-merged supernodes hold more than one, so
// inline room for a single element keeps the ordinary node from allocating at all.
template <symmetry_tag sym> using UserNodeValueVector = SmallVector<ComplexValue<sym>, 1>;

template <steady_state_solver_output_type SolverOutputType> struct SupernodeOutput<SolverOutputType> {
using sym = decode_symmetry_v<SolverOutputType>;

ComplexValueVector<sym> bus_injection; // user bus output
UserNodeValueVector<sym> bus_injection; // user bus output
std::vector<BranchSolverOutput<sym>> link; // user link
};
template <short_circuit_solver_output_type SolverOutputType> struct SupernodeOutput<SolverOutputType> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
//
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include "common.hpp"

#include <boost/container/small_vector.hpp>

#include <cstddef>

namespace power_grid_model {

// A vector that keeps room for the first N elements inside the object itself and only allocates
// beyond that. Use it where a container is almost always short but occasionally is not. N is a
// per-use-site decision: too large inflates every object for slots that go unused.
template <class T, std::size_t N> using SmallVector = boost::container::small_vector<T, N>;

} // namespace power_grid_model
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace power_grid_model::main_core {
namespace detail {
template <symmetry_tag sym> struct SuperNodeSolverInput {
std::span<BranchIdx const> links;
ComplexValueVector<sym> node_injection;
ComplexValueVector<sym> node_flow_from_branch;
UserNodeValueVector<sym> node_injection;
UserNodeValueVector<sym> node_flow_from_branch;

ComplexValueVector<sym> get_total_injection_per_node() const {
assert(node_injection.size() == node_flow_from_branch.size());
Expand Down Expand Up @@ -225,8 +225,8 @@ solve_topological_nodes(LinkSolver link_solver, State const& state,
std::views::transform([](auto const& topo_node) -> SuperNodeSolverInput<sym> {
auto const node_number = topo_node.user_nodes.size();
return {.links = std::span{topo_node.user_links},
.node_injection = ComplexValueVector<sym>(node_number),
.node_flow_from_branch = ComplexValueVector<sym>(node_number)};
.node_injection = UserNodeValueVector<sym>(node_number),
.node_flow_from_branch = UserNodeValueVector<sym>(node_number)};
}) |
std::ranges::to<std::vector>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,10 @@ TEST_CASE("Test topological node output") {
CHECK(mock.recorded_loads[1] == ComplexVector{DoubleComplex{}});

REQUIRE(result.size() == 2);
CHECK(result[0].bus_injection ==
ComplexVector{dummy_complex_value_sym(), dummy_complex_value_sym(), dummy_complex_value_sym()});
CHECK(result[1].bus_injection == ComplexVector{DoubleComplex{}});
CHECK(result[0].bus_injection == UserNodeValueVector<symmetric_t>{dummy_complex_value_sym(),
dummy_complex_value_sym(),
dummy_complex_value_sym()});
CHECK(result[1].bus_injection == UserNodeValueVector<symmetric_t>{DoubleComplex{}});
REQUIRE(result[0].link.size() == 2);
check_close(result[0].link[0].s_f, 2.0 * dummy_complex_value_sym());
check_close(result[0].link[0].s_t, -2.0 * dummy_complex_value_sym());
Expand Down
Loading