Skip to content

Commit 6c9605a

Browse files
authored
Merge pull request #1115 from PowerGridModel/feature/log-less-main-model
Clean-up main model: pass logger by reference to main model
2 parents 7615d7e + 4064067 commit 6c9605a

File tree

2 files changed

+71
-126
lines changed

2 files changed

+71
-126
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/job_adapter.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
// Adapter that connects the JobDispatch to the MainModelImpl
88

9-
#include "auxiliary/dataset.hpp"
109
#include "job_interface.hpp"
1110
#include "main_model_fwd.hpp"
1211

12+
#include "auxiliary/dataset.hpp"
13+
#include "common/dummy_logging.hpp"
1314
#include "main_core/update.hpp"
1415

1516
namespace power_grid_model {
@@ -99,7 +100,7 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
99100

100101
void calculate_impl(MutableDataset const& result_data, Idx scenario_idx) const {
101102
MainModel::calculator(options_.get(), model_reference_.get(), result_data.get_individual_scenario(scenario_idx),
102-
false);
103+
false, logger());
103104
}
104105

105106
void cache_calculate_impl() const {
@@ -112,7 +113,7 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
112113
"sym_output",
113114
model_reference_.get().meta_data(),
114115
},
115-
true);
116+
true, logger());
116117
} catch (SparseMatrixError const&) { // NOLINT(bugprone-empty-catch) // NOSONAR
117118
// missing entries are provided in the update data
118119
} catch (NotObservableError const&) { // NOLINT(bugprone-empty-catch) // NOSONAR
@@ -157,13 +158,12 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
157158
});
158159
}
159160

160-
void reset_logger_impl() {
161-
log_ = nullptr;
162-
model_reference_.get().reset_logger();
163-
}
164-
void set_logger_impl(Logger& log) {
165-
log_ = &log;
166-
model_reference_.get().set_logger(*log_);
161+
void reset_logger_impl() { log_ = nullptr; }
162+
void set_logger_impl(Logger& log) { log_ = &log; }
163+
164+
Logger& logger() const {
165+
static common::logging::NoLogger no_log{};
166+
return log_ != nullptr ? *log_ : no_log;
167167
}
168168
};
169169
} // namespace power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 61 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
// common
1717
#include "common/common.hpp"
18-
#include "common/dummy_logging.hpp"
1918
#include "common/exception.hpp"
2019
#include "common/timer.hpp"
2120

@@ -158,57 +157,6 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
158157
meta_data_{&meta_data},
159158
math_solver_dispatcher_{&math_solver_dispatcher} {}
160159

161-
MainModelImpl(MainModelImpl const& other)
162-
: log_{nullptr}, // logger should not be copied, because it may result in race conditions
163-
system_frequency_{other.system_frequency_},
164-
meta_data_{other.meta_data_},
165-
math_solver_dispatcher_{other.math_solver_dispatcher_},
166-
state_{other.state_},
167-
math_state_{other.math_state_},
168-
n_math_solvers_{other.n_math_solvers_},
169-
is_topology_up_to_date_{other.is_topology_up_to_date_},
170-
is_sym_parameter_up_to_date_{other.is_sym_parameter_up_to_date_},
171-
is_asym_parameter_up_to_date_{other.is_asym_parameter_up_to_date_},
172-
is_accumulated_component_updated_{other.is_accumulated_component_updated_},
173-
last_updated_calculation_symmetry_mode_{other.last_updated_calculation_symmetry_mode_},
174-
cached_inverse_update_{other.cached_inverse_update_},
175-
cached_state_changes_{other.cached_state_changes_},
176-
parameter_changed_components_{other.parameter_changed_components_}
177-
#ifndef NDEBUG
178-
,
179-
// construction_complete is used for debug assertions only
180-
construction_complete_{other.construction_complete_}
181-
#endif // !NDEBUG
182-
{
183-
}
184-
MainModelImpl& operator=(MainModelImpl const& other) {
185-
if (this != &other) {
186-
log_ = nullptr; // logger should be reset, because it may result in race conditions
187-
system_frequency_ = other.system_frequency_;
188-
meta_data_ = other.meta_data_;
189-
math_solver_dispatcher_ = other.math_solver_dispatcher_;
190-
state_ = other.state_;
191-
math_state_ = other.math_state_;
192-
n_math_solvers_ = other.n_math_solvers_;
193-
is_topology_up_to_date_ = other.is_topology_up_to_date_;
194-
is_sym_parameter_up_to_date_ = other.is_sym_parameter_up_to_date_;
195-
is_asym_parameter_up_to_date_ = other.is_asym_parameter_up_to_date_;
196-
is_accumulated_component_updated_ = other.is_accumulated_component_updated_;
197-
last_updated_calculation_symmetry_mode_ = other.last_updated_calculation_symmetry_mode_;
198-
cached_inverse_update_ = other.cached_inverse_update_;
199-
cached_state_changes_ = other.cached_state_changes_;
200-
parameter_changed_components_ = other.parameter_changed_components_;
201-
#ifndef NDEBUG
202-
// construction_complete is used for debug assertions only
203-
construction_complete_ = other.construction_complete_;
204-
#endif // !NDEBUG
205-
}
206-
return *this;
207-
}
208-
MainModelImpl(MainModelImpl&& /*other*/) noexcept = default;
209-
MainModelImpl& operator=(MainModelImpl&& /*other*/) noexcept = default;
210-
~MainModelImpl() = default;
211-
212160
// helper function to get what components are present in the update data
213161
std::array<bool, main_core::utils::n_types<ComponentType...>>
214162
get_components_to_update(ConstDataset const& update_data) const {
@@ -402,20 +350,20 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
402350
std::same_as<std::invoke_result_t<PrepareInputFn, Idx /*n_math_solvers*/>, std::vector<InputType>> &&
403351
std::same_as<std::invoke_result_t<SolveFn, MathSolverType&, YBus const&, InputType const&>,
404352
SolverOutputType>
405-
std::vector<SolverOutputType> calculate_(PrepareInputFn&& prepare_input, SolveFn&& solve) {
353+
std::vector<SolverOutputType> calculate_(PrepareInputFn&& prepare_input, SolveFn&& solve, Logger& logger) {
406354
using sym = typename SolverOutputType::sym;
407355

408356
assert(construction_complete_);
409357
// prepare
410-
auto const& input = [this, prepare_input_ = std::forward<PrepareInputFn>(prepare_input)] {
411-
Timer const timer{logger(), LogEvent::prepare};
358+
auto const& input = [this, &logger, prepare_input_ = std::forward<PrepareInputFn>(prepare_input)] {
359+
Timer const timer{logger, LogEvent::prepare};
412360
prepare_solvers<sym>();
413361
assert(is_topology_up_to_date_ && is_parameter_up_to_date<sym>());
414362
return prepare_input_(n_math_solvers_);
415363
}();
416364
// calculate
417-
return [this, &input, solve_ = std::forward<SolveFn>(solve)] {
418-
Timer const timer{logger(), LogEvent::math_calculation};
365+
return [this, &logger, &input, solve_ = std::forward<SolveFn>(solve)] {
366+
Timer const timer{logger, LogEvent::math_calculation};
419367
auto& solvers = main_core::get_solvers<sym>(math_state_);
420368
auto& y_bus_vec = main_core::get_y_bus<sym>(math_state_);
421369
std::vector<SolverOutputType> solver_output;
@@ -427,39 +375,44 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
427375
}();
428376
}
429377

430-
template <symmetry_tag sym> auto calculate_power_flow_(double err_tol, Idx max_iter) {
431-
return [this, err_tol, max_iter](MainModelState const& state,
432-
CalculationMethod calculation_method) -> std::vector<SolverOutput<sym>> {
433-
return calculate_<SolverOutput<sym>, MathSolverProxy<sym>, YBus<sym>, PowerFlowInput<sym>>(
434-
[&state](Idx n_math_solvers) {
435-
return main_core::prepare_power_flow_input<sym>(state, n_math_solvers);
436-
},
437-
[this, err_tol, max_iter, calculation_method](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus,
438-
PowerFlowInput<sym> const& input) {
439-
return solver.get().run_power_flow(input, err_tol, max_iter, logger(), calculation_method, y_bus);
440-
});
441-
};
378+
template <symmetry_tag sym> auto calculate_power_flow_(double err_tol, Idx max_iter, Logger& logger) {
379+
return
380+
[this, err_tol, max_iter, &logger](MainModelState const& state,
381+
CalculationMethod calculation_method) -> std::vector<SolverOutput<sym>> {
382+
return calculate_<SolverOutput<sym>, MathSolverProxy<sym>, YBus<sym>, PowerFlowInput<sym>>(
383+
[&state](Idx n_math_solvers) {
384+
return main_core::prepare_power_flow_input<sym>(state, n_math_solvers);
385+
},
386+
[err_tol, max_iter, calculation_method,
387+
&logger](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus, PowerFlowInput<sym> const& input) {
388+
return solver.get().run_power_flow(input, err_tol, max_iter, logger, calculation_method, y_bus);
389+
},
390+
logger);
391+
};
442392
}
443393

444-
template <symmetry_tag sym> auto calculate_state_estimation_(double err_tol, Idx max_iter) {
445-
return [this, err_tol, max_iter](MainModelState const& state,
446-
CalculationMethod calculation_method) -> std::vector<SolverOutput<sym>> {
447-
return calculate_<SolverOutput<sym>, MathSolverProxy<sym>, YBus<sym>, StateEstimationInput<sym>>(
448-
[&state](Idx n_math_solvers) {
449-
return main_core::prepare_state_estimation_input<sym>(state, n_math_solvers);
450-
},
451-
[this, err_tol, max_iter, calculation_method](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus,
452-
StateEstimationInput<sym> const& input) {
453-
return solver.get().run_state_estimation(input, err_tol, max_iter, logger(), calculation_method,
454-
y_bus);
455-
});
456-
};
394+
template <symmetry_tag sym> auto calculate_state_estimation_(double err_tol, Idx max_iter, Logger& logger) {
395+
return
396+
[this, err_tol, max_iter, &logger](MainModelState const& state,
397+
CalculationMethod calculation_method) -> std::vector<SolverOutput<sym>> {
398+
return calculate_<SolverOutput<sym>, MathSolverProxy<sym>, YBus<sym>, StateEstimationInput<sym>>(
399+
[&state](Idx n_math_solvers) {
400+
return main_core::prepare_state_estimation_input<sym>(state, n_math_solvers);
401+
},
402+
[err_tol, max_iter, calculation_method, &logger](
403+
MathSolverProxy<sym>& solver, YBus<sym> const& y_bus, StateEstimationInput<sym> const& input) {
404+
return solver.get().run_state_estimation(input, err_tol, max_iter, logger, calculation_method,
405+
y_bus);
406+
},
407+
logger);
408+
};
457409
}
458410

459-
template <symmetry_tag sym> auto calculate_short_circuit_(ShortCircuitVoltageScaling voltage_scaling) {
460-
return [this,
461-
voltage_scaling](MainModelState const& state,
462-
CalculationMethod calculation_method) -> std::vector<ShortCircuitSolverOutput<sym>> {
411+
template <symmetry_tag sym>
412+
auto calculate_short_circuit_(ShortCircuitVoltageScaling voltage_scaling, Logger& logger) {
413+
return [this, voltage_scaling,
414+
&logger](MainModelState const& state,
415+
CalculationMethod calculation_method) -> std::vector<ShortCircuitSolverOutput<sym>> {
463416
(void)state; // to avoid unused-lambda-capture when in Release build
464417
assert(&state == &state_);
465418

@@ -469,25 +422,27 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
469422
return main_core::prepare_short_circuit_input<sym>(state_, state_.comp_coup, n_math_solvers,
470423
voltage_scaling);
471424
},
472-
[this, calculation_method](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus,
473-
ShortCircuitInput const& input) {
474-
return solver.get().run_short_circuit(input, logger(), calculation_method, y_bus);
475-
});
425+
[calculation_method, &logger](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus,
426+
ShortCircuitInput const& input) {
427+
return solver.get().run_short_circuit(input, logger, calculation_method, y_bus);
428+
},
429+
logger);
476430
};
477431
}
478432

479433
// Calculate with optimization, e.g., automatic tap changer
480-
template <calculation_type_tag calculation_type, symmetry_tag sym> auto calculate(Options const& options) {
481-
auto const calculator = [this, &options] {
434+
template <calculation_type_tag calculation_type, symmetry_tag sym>
435+
auto calculate(Options const& options, Logger& logger) {
436+
auto const calculator = [this, &options, &logger] {
482437
if constexpr (std::derived_from<calculation_type, power_flow_t>) {
483-
return calculate_power_flow_<sym>(options.err_tol, options.max_iter);
438+
return calculate_power_flow_<sym>(options.err_tol, options.max_iter, logger);
484439
}
485440
assert(options.optimizer_type == OptimizerType::no_optimization);
486441
if constexpr (std::derived_from<calculation_type, state_estimation_t>) {
487-
return calculate_state_estimation_<sym>(options.err_tol, options.max_iter);
442+
return calculate_state_estimation_<sym>(options.err_tol, options.max_iter, logger);
488443
}
489444
if constexpr (std::derived_from<calculation_type, short_circuit_t>) {
490-
return calculate_short_circuit_<sym>(options.short_circuit_voltage_scaling);
445+
return calculate_short_circuit_<sym>(options.short_circuit_voltage_scaling, logger);
491446
}
492447
throw UnreachableHit{"MainModelImpl::calculate", "Unknown calculation type"};
493448
}();
@@ -506,7 +461,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
506461
}
507462

508463
// Single calculation, propagating the results to result_data
509-
void calculate(Options options, MutableDataset const& result_data) {
464+
void calculate(Options options, MutableDataset const& result_data, Logger& logger) {
510465
assert(construction_complete_);
511466

512467
if (options.calculation_type == CalculationType::short_circuit) {
@@ -520,26 +475,24 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
520475
calculation_type_symmetry_func_selector(
521476
options.calculation_type, options.calculation_symmetry,
522477
[]<calculation_type_tag calculation_type, symmetry_tag sym>(
523-
MainModelImpl& main_model_, Options const& options_, MutableDataset const& result_data_) {
524-
auto const math_output = main_model_.calculate<calculation_type, sym>(options_);
525-
main_model_.output_result(math_output, result_data_);
478+
MainModelImpl& main_model_, Options const& options_, MutableDataset const& result_data_,
479+
Logger& logger) {
480+
auto const math_output = main_model_.calculate<calculation_type, sym>(options_, logger);
481+
main_model_.output_result(math_output, result_data_, logger);
526482
},
527-
*this, options, result_data);
483+
*this, options, result_data, logger);
528484
}
529485

530486
public:
531487
static auto calculator(Options const& options, MainModelImpl& model, MutableDataset const& target_data,
532-
bool cache_run) {
488+
bool cache_run, Logger& logger) {
533489
auto sub_opt = options; // copy
534490
sub_opt.err_tol = cache_run ? std::numeric_limits<double>::max() : options.err_tol;
535491
sub_opt.max_iter = cache_run ? 1 : options.max_iter;
536492

537-
model.calculate(sub_opt, target_data);
493+
model.calculate(sub_opt, target_data, logger);
538494
}
539495

540-
void reset_logger() { log_ = nullptr; }
541-
void set_logger(Logger& logger) { log_ = &logger; }
542-
543496
auto const& state() const {
544497
assert(construction_complete_);
545498
return state_;
@@ -554,8 +507,8 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
554507

555508
private:
556509
template <solver_output_type SolverOutputType>
557-
void output_result(MathOutput<std::vector<SolverOutputType>> const& math_output,
558-
MutableDataset const& result_data) const {
510+
void output_result(MathOutput<std::vector<SolverOutputType>> const& math_output, MutableDataset const& result_data,
511+
Logger& logger) const {
559512
assert(!result_data.is_batch());
560513

561514
auto const output_func = [this, &math_output, &result_data]<typename CT>() {
@@ -577,13 +530,10 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
577530
}
578531
};
579532

580-
Timer const t_output{logger(), LogEvent::produce_output};
533+
Timer const t_output{logger, LogEvent::produce_output};
581534
main_core::utils::run_functor_with_all_types_return_void<ComponentType...>(output_func);
582535
}
583536

584-
Logger* log_; // needs to be first due to padding override
585-
// may be changed in const functions for metrics
586-
587537
double system_frequency_;
588538
MetaData const* meta_data_;
589539
MathSolverDispatcher const* math_solver_dispatcher_;
@@ -606,11 +556,6 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
606556
bool construction_complete_{false};
607557
#endif // !NDEBUG
608558

609-
Logger& logger() const {
610-
static common::logging::NoLogger no_log{};
611-
return log_ != nullptr ? *log_ : no_log;
612-
}
613-
614559
template <symmetry_tag sym> bool& is_parameter_up_to_date() {
615560
if constexpr (is_symmetric_v<sym>) {
616561
return is_sym_parameter_up_to_date_;

0 commit comments

Comments
 (0)