Skip to content

Main model refactoring: move calculation logic out of main model#1286

Open
mgovers wants to merge 13 commits intomainfrom
feature/calculation-out-of-main-model
Open

Main model refactoring: move calculation logic out of main model#1286
mgovers wants to merge 13 commits intomainfrom
feature/calculation-out-of-main-model

Conversation

@mgovers
Copy link
Member

@mgovers mgovers commented Jan 28, 2026

This finishes up the cleanup of main model

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
…ut-of-main-model

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
@mgovers mgovers self-assigned this Jan 28, 2026
@mgovers mgovers added improvement Improvement on internal implementation do-not-merge This should not be merged labels Jan 28, 2026
mgovers and others added 4 commits January 28, 2026 11:41
…atforms

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
@mgovers mgovers removed the do-not-merge This should not be merged label Jan 28, 2026
mgovers and others added 2 commits January 28, 2026 13:26
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
Copy link
Member

@figueroa1395 figueroa1395 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current state looks good, I left some minor comments only.

On the other hand, I think calculate and the calculate_ overloads should be gone from the main model as well, and we should only leave the calculator entry point, the others are implementation details that can be in a namespace detail in calculation.hpp. What do you think? Any specific reason to not take them out?

Comment on lines +27 to +28
constexpr auto enumerate(std::ranges::viewable_range auto&& rng) {
return std::views::zip(IdxRange{std::ssize(rng)}, rng);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rng can be easily miss-interpreted. Can you just use range instead?

Suggested change
constexpr auto enumerate(std::ranges::viewable_range auto&& rng) {
return std::views::zip(IdxRange{std::ssize(rng)}, rng);
constexpr auto enumerate(std::ranges::viewable_range auto&& range) {
return std::views::zip(IdxRange{std::ssize(range)}, range);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name conflicts 😢

std::same_as<std::invoke_result_t<SolveFn, MathSolverType&, YBus const&, InputType const&>,
SolverOutputType>
std::vector<SolverOutputType> calculate_(PrepareInputFn&& prepare_input, SolveFn&& solve, Logger& logger) {
std::ranges::range<std::invoke_result_t<PrepareInputFn, Idx /*n_math_solvers*/>> &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were the changes from std::same_as<a, std::vector<b>> to std::ranges::range<a, b> just so it's more general and up to date code? Do you expect to have potential std::spans for instance at some point? Wouldn't it be best to keep it restricted to std::vector` only if we don't foresee this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i needed access to the more generic type, otherwise it was not able to infer the type

Comment on lines +314 to +323
return [this, &comp_coup = state_.comp_coup, &options, &logger](MainModelState const& state,
CalculationMethod calculation_method) {
(void)state; // to avoid unused-lambda-capture when in Release build
assert(&state == &state_);

return calculate_<MathSolverProxy<sym>, YBus<sym>>(Calc::preparer(state, comp_coup, options),
Calc::solver(calculation_method, options, logger),
logger);
};
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not try to get rid of the state and state_ "shenanigans" and do something like (beware of typos)

Suggested change
return [this, &comp_coup = state_.comp_coup, &options, &logger](MainModelState const& state,
CalculationMethod calculation_method) {
(void)state; // to avoid unused-lambda-capture when in Release build
assert(&state == &state_);
return calculate_<MathSolverProxy<sym>, YBus<sym>>(Calc::preparer(state, comp_coup, options),
Calc::solver(calculation_method, options, logger),
logger);
};
};
return [this, &options, &logger](MainModelState const& state,
CalculationMethod calculation_method) {
return calculate_<MathSolverProxy<sym>, YBus<sym>>(Calc::preparer(state, state.comp_coup, options),
Calc::solver(calculation_method, options, logger),
logger);
};
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutable vs immutable. the input state is presumed const, but the comp coup needs to be mutable

calculation_symmetry, std::forward<Functor>(f), std::forward<Args>(args)...);
}

template <typename T, typename sym> struct Calculator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I really like these!

…alculation.hpp


Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>

Co-authored-by: Nitish Bharambe <78108900+nitbharambe@users.noreply.github.com>
Signed-off-by: Martijn Govers <martygovers@hotmail.com>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 4, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improvement on internal implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants