SimulationLP class
#include <gtopt/simulation_lp.hpp>
Linear programming representation of a power system simulation.
Encapsulates the LP transformation of a power system simulation model, providing access to all components in their LP form. Maintains references to the original simulation and options objects.
Public types
- struct AmplLpCell
- using ampl_lp_registry_t = StrongIndexVector<SceneIndex, StrongIndexVector<PhaseIndex, AmplLpCell>>
-
using global_integer_variable_map_t = StrongIndexVector<SceneIndex, StrongIndexVector<PhaseIndex, integer_
variable_ map_ t>> -
using global_variable_map_t = StrongIndexVector<SceneIndex, StrongIndexVector<PhaseIndex, state_
variable_ map_ t>> -
using integer_variable_key_t = IntegerVariable::
Key -
using integer_variable_map_t = flat_
map<integer_ variable_ key_ t, IntegerVariable> -
using lp_key_t = StateVariable::
LPKey -
using state_variable_key_t = StateVariable::
Key -
using state_variable_map_t = flat_
map<state_ variable_ key_ t, StateVariable>
Constructors, destructors, conversion operators
- SimulationLP(const Simulation& simulation, const PlanningOptionsLP& options, std::shared_ptr<ArrowIndexCache> shared_index_cache = {}, std::optional<SceneMerge> scene_merge = {}) explicit
- Constructs a SimulationLP from a Simulation.
- SimulationLP(const SimulationLP&) deleted
- SimulationLP(Simulation&& simulation, const PlanningOptionsLP& options, std::shared_ptr<ArrowIndexCache> shared_index_cache = {}, std::optional<SceneMerge> scene_merge = {}) deleted
- SimulationLP(SimulationLP&&) deleted
- ~SimulationLP() defaulted noexcept
Public functions
- void add_ampl_compound(std::string_view class_name, std::string_view compound_name, std::vector<AmplCompoundLeg> legs)
- void add_ampl_scalar(std::string_view class_name, std::string_view attribute, double value)
- void add_ampl_variable(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, ColIndex stage_col)
- void add_ampl_variable(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, const BIndexHolder<ColIndex>& block_cols)
- void add_ampl_variable(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, const BIndexHolder<ColIndex>& block_cols, const BIndexHolder<double>& block_offsets)
- void add_ampl_variable(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, const BIndexHolder<std::vector<ColIndex>>& block_cols_sum)
- void add_ampl_variable(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, const BIndexHolder<std::vector<std::pair<ColIndex, double>>>& block_cols_weighted_sum, const BIndexHolder<double>& block_offsets = {})
-
template<typename Key = integer_auto add_integer_variable(Key&& key, ColIndex col, IntegerDomain domain, IntegerScope scope, GroupUid group_uid, std::span<const BlockUid> blocks) -> const IntegerVariable&
variable_ key_ t> -
template<typename Key = state_auto add_state_variable(Key&& key, ColIndex col, double scost, double var_scale, LpContext context) -> const StateVariable&
variable_ key_ t> - auto alpha_offset(SceneIndex si) const -> double noexcept
- auto ampl_attribute_candidates(std::string_view class_name, Uid element_uid, std::size_t max_attrs = 12) const -> std::vector<std::string_view>
- auto ampl_element_name_candidates(std::string_view class_name, std::string_view target, std::size_t max_names = 5) const -> std::vector<std::string_view>
- auto ampl_params() const -> const AmplParamMap& noexcept
- auto ampl_registry_flag() const -> std::once_flag& noexcept
- auto apertures() const -> const auto& noexcept
- Gets the aperture definitions from the underlying Simulation.
- auto array_index_mutex() const -> std::mutex& noexcept
- auto arrow_index_cache() const -> ArrowIndexCache&
- auto arrow_index_cache_ptr() const -> std::shared_ptr<ArrowIndexCache>
- auto blocks() const -> const auto& noexcept
- auto dropped_term_counts() const -> auto
- Snapshot of the drop counts (tests).
- void finalize_ampl_element_names()
- auto find_ampl_active(std::string_view class_name) const -> AmplActiveFn noexcept
- Look up the activity probe of a class; nullptr when unregistered.
- auto find_ampl_class_attribute(std::string_view class_name, std::string_view attribute) const -> bool
- auto find_ampl_col(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, BlockUid block_uid) const -> std::optional<ColIndex>
- auto find_ampl_cols(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, BlockUid block_uid) const -> std::span<const ColIndex>
- auto find_ampl_compound(std::string_view class_name, std::string_view compound_name) const -> const std::vector<AmplCompoundLeg>* noexcept
- auto find_ampl_element_metadata(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid) const -> const AmplElementMetadata* noexcept
- auto find_ampl_iter(std::string_view class_name) const -> AmplIterFn noexcept
- auto find_ampl_offset(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, BlockUid block_uid) const -> double
- auto find_ampl_param(std::string_view class_name, std::string_view attribute) const -> AmplParamFn noexcept
- auto find_ampl_scalar(std::string_view class_name, std::string_view attribute) const -> std::optional<double> noexcept
- auto find_ampl_scoped_element(std::string_view class_name, Uid element_uid) const -> const std::string* noexcept
- auto find_ampl_suppression(std::string_view class_name, std::string_view attribute) const -> std::optional<std::string_view> noexcept
- auto find_ampl_variable(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid) const -> const AmplVariable*
- auto find_ampl_variable_for_element(std::string_view class_name, Uid element_uid, std::string_view attribute) const -> bool
- auto find_ampl_weighted_cols(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, std::string_view attribute, ScenarioUid scenario_uid, StageUid stage_uid, BlockUid block_uid) const -> std::span<const std::pair<ColIndex, double>>
-
template<typename Self, typename Key = integer_auto integer_variable(this Self&& self, Key&& key) -> auto noexcept
variable_ key_ t> -
template<typename Self>auto integer_variable_map(this Self&& self, SystemKind kind) -> auto&& noexcept
- auto integer_variables() -> const auto& noexcept
-
template<typename Self>auto integer_variables(this Self&& self, SceneIndex scene_index, PhaseIndex phase_index, SystemKind kind = SystemKind::
forward) -> auto&& noexcept - auto last_phase_index() const -> PhaseIndex noexcept
- Gets the index of the last phase.
- auto last_scene_index() const -> SceneIndex noexcept
- Gets the index of the last scene.
- void log_dropped_term_summary() const
- auto lookup_ampl_element_name(const LPClassName& class_name, Uid element_uid) const -> std::string_view noexcept
- auto lookup_ampl_element_uid(std::string_view class_name, std::string_view element_name) const -> std::optional<Uid>
- auto need_ampl_variables() const -> bool noexcept
- auto next_stage_in_phase(const StageLP& stage) const -> const StageLP* noexcept
- Next stage of the SAME phase, or nullptr at the phase's last stage.
- auto operator=(const SimulationLP&) -> SimulationLP& deleted
- auto operator=(SimulationLP&&) -> SimulationLP& deleted
- auto options() const -> const PlanningOptionsLP& noexcept
- Gets the LP solver options.
- auto owning_lp_of(ScenarioUid scenario_uid, StageUid stage_uid) const -> std::optional<std::pair<SceneIndex, PhaseIndex>> noexcept
- auto phase_count() const -> Index noexcept
- Number of phases as a signed
Index. - auto phase_of(StageUid stage_uid) const -> std::optional<PhaseIndex> noexcept
- auto phases() const -> const auto& noexcept
- Gets all phase LP representations.
- auto prev_at_phase_start_counts() const -> auto
- Snapshot of the later-phase-start
_prevcounts (tests). - auto prev_stage(const StageLP& stage) const -> std::pair<const StageLP*, const PhaseLP*> noexcept
- void record_dropped_term(std::string_view constraint_name, std::string_view reason) const
- void record_prev_at_phase_start(std::string_view constraint_name, std::string_view term) const
- void register_ampl_active(std::string_view class_name, AmplActiveFn fn)
- void register_ampl_element(std::string_view class_name, std::string_view element_name, Uid element_uid)
- void register_ampl_element_metadata(SceneIndex scene_index, PhaseIndex phase_index, std::string_view class_name, Uid element_uid, AmplElementMetadata metadata)
- void register_ampl_iter(std::string_view class_name, AmplIterFn fn)
- void register_ampl_param(std::string_view class_name, std::string_view attribute, AmplParamFn fn)
- void register_ampl_scoped_element(std::string_view class_name, Uid element_uid, std::string description)
- void release_ampl_cell(SceneIndex scene_index, PhaseIndex phase_index) noexcept
- auto scenarios() const -> const auto& noexcept
- Gets all scenario LP representations.
- auto scene_count() const -> Index noexcept
- Number of scenes as a signed
Index. - auto scene_merge() const -> const std::optional<SceneMerge>& noexcept
- auto scene_of(ScenarioUid scenario_uid) const -> std::optional<SceneIndex> noexcept
- auto scenes() const -> const auto& noexcept
- Gets all scene LP representations.
- void set_alpha_offsets(StrongIndexVector<SceneIndex, double> offsets)
- void set_need_ampl_variables(bool v) noexcept
-
template<typename Self>auto simulation(this Self& self) -> auto&& noexcept
- Gets the underlying simulation model.
- auto stages() const -> const auto& noexcept
-
template<typename Self, typename Key = state_auto state_variable(this Self&& self, Key&& key) -> auto noexcept
variable_ key_ t> - Retrieves a state variable by its key.
- auto state_variables() -> const auto& noexcept
-
template<typename Self>auto state_variables(this Self&& self, SceneIndex scene_index, PhaseIndex phase_index, SystemKind kind = SystemKind::
forward) -> auto&& noexcept - void suppress_ampl_attribute(std::string_view class_name, std::string_view attribute, std::string_view reason)
- void suppress_ampl_class(std::string_view class_name, std::string_view reason)
- auto uid_of(PhaseIndex phase_index) const -> PhaseUid noexcept
- Convert a
PhaseIndex(0-based) to the matchingPhaseUid(1-based). Mirror ofuid_of(SceneIndex)— see that helper for rationale. - auto uid_of(SceneIndex scene_index) const -> SceneUid noexcept
- Convert a
SceneIndex(0-based array position) to the matchingSceneUid(1-based identity from JSON input). - auto unique_owning_lp_of(std::span<const std::pair<ScenarioUid, StageUid>> refs) const -> std::optional<std::pair<SceneIndex, PhaseIndex>> noexcept
-
template<typename Self>auto variable_map(this Self&& self, SystemKind kind) -> auto&& noexcept
Function documentation
gtopt:: SimulationLP:: SimulationLP(const Simulation& simulation,
const PlanningOptionsLP& options,
std::shared_ptr<ArrowIndexCache> shared_index_cache = {},
std::optional<SceneMerge> scene_merge = {}) explicit
Constructs a SimulationLP from a Simulation.
| Parameters | |
|---|---|
| simulation | Reference to the base simulation model |
| options | Reference to LP solver options |
| shared_index_cache | Optional Arrow input-index cache to share with other SimulationLP instances over the same inputs; when empty the instance keeps its own private cache |
| scene_merge | |
| Exceptions | |
| std::runtime_error | If component validation fails |
| std::bad_alloc | If memory allocation fails |
gtopt:: SimulationLP:: SimulationLP(Simulation&& simulation,
const PlanningOptionsLP& options,
std::shared_ptr<ArrowIndexCache> shared_index_cache = {},
std::optional<SceneMerge> scene_merge = {}) deleted
SimulationLP stores a reference to simulation; reject temporaries at compile time (they dangle as soon as the full expression ends).
void gtopt:: SimulationLP:: add_ampl_compound(std::string_view class_name,
std::string_view compound_name,
std::vector<AmplCompoundLeg> legs)
Register a compound PAMPL attribute for a class. The compound name resolves to the linear combination Σ leg.coefficient * <leg.source_attribute> using the already-registered per-element AMPL variables. Registration is class-level (once per class definition, not per element) — the compound "line.flow" is the same recipe for every LineLP.
Called exclusively from system_lp.cpp inside the same std::call_once block that registers element names; no synchronization needed.
void gtopt:: SimulationLP:: add_ampl_scalar(std::string_view class_name,
std::string_view attribute,
double value)
Register a class-level scalar (e.g. options.annual_discount_rate). class_name and attribute must point to storage that lives at least as long as SimulationLP (in practice: std::string for class_name, constexpr literal for attribute).
Called exclusively from system_lp.cpp inside the AMPL globals std::call_once; no synchronization needed.
void gtopt:: SimulationLP:: add_ampl_variable(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
ColIndex stage_col)
Register a stage-level scalar column (e.g., eini, efin, capainst). Returns the same value for every block in the stage.
void gtopt:: SimulationLP:: add_ampl_variable(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
const BIndexHolder<ColIndex>& block_cols)
Register a per-block variable map (e.g., generator.generation). No-op when need_ampl_variables() is false.
void gtopt:: SimulationLP:: add_ampl_variable(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
const BIndexHolder<ColIndex>& block_cols,
const BIndexHolder<double>& block_offsets)
Register a per-block variable map together with per-block additive offsets. Used by shifted-variable encodings (e.g., demand's Option C neg_fail = load − lmax substitution) so user constraints referencing demand.load resolve to (col + offset) physically, with the offset shifted onto the row's RHS by the resolver. No-op when need_ampl_variables() is false.
void gtopt:: SimulationLP:: add_ampl_variable(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
const BIndexHolder<std::vector<ColIndex>>& block_cols_sum)
Register a per-block sum of columns for a virtual aggregator attribute (e.g., line.flowp = Σ flowp_seg_k under piecewise_direct line-loss mode — no aggregator LP col, the segments are summed at resolution time). When the user-constraint resolver hits this registration it stamps each col in the list with the leg coefficient: row[col] += base · coef per col.
Like the other overloads, no-op when need_ampl_variables() is false. The BIndexHolder<std::vector<ColIndex>> is copied by value for the same lifetime reasons as the single-col overload (the source map may move when later add_to_lp calls grow the owner's STBIndexHolder).
void gtopt:: SimulationLP:: add_ampl_variable(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
const BIndexHolder<std::vector<std::pair<ColIndex, double>>>& block_cols_weighted_sum,
const BIndexHolder<double>& block_offsets = {})
Register a per-block weighted sum of columns for a virtual aggregator attribute whose legs each carry a coefficient. Used by FuelLP::add_to_lp to expose fuel("X").offtake = Σ_g heat_rate_g · dur_b · generation_g[b] without creating an LP Y_f[b] column + equality binding row (the legacy substitute-out anti-pattern, mirror of the EmissionZone.production removal). Resolver stamps row[col] += base_coef · weight per leg. No-op when need_ampl_variables() is false. Copied by value for the same lifetime reasons as the other sum-of-cols overload.
template<typename Key = integer_ variable_ key_ t>
const IntegerVariable& gtopt:: SimulationLP:: add_integer_variable(Key&& key,
ColIndex col,
IntegerDomain domain,
IntegerScope scope,
GroupUid group_uid,
std::span<const BlockUid> blocks)
Register one integer LP column. Idempotent — re-registering the same key with the same column index is a no-op; re-registering with a different column index is a hard error (same contract as add_state_variable).
double gtopt:: SimulationLP:: alpha_offset(SceneIndex si) const noexcept
Per-scene α-rebase offset c̄ ($). Returns 0 when the scene index is out of range or no offsets were published (mean-shift disabled / no cuts).
std::vector<std::string_view> gtopt:: SimulationLP:: ampl_attribute_candidates(std::string_view class_name,
Uid element_uid,
std::size_t max_attrs = 12) const
Collect up to max_attrs distinct attribute names registered as LP variables for the (class_name, element_uid) pair, across every (scene, phase, scenario, stage). Diagnostic-only: used to build a "valid attributes are ..." hint when a user constraint references a known element with an unknown attribute. Scans the per-cell variable registry — only called on the strict-mode error path.
std::vector<std::string_view> gtopt:: SimulationLP:: ampl_element_name_candidates(std::string_view class_name,
std::string_view target,
std::size_t max_names = 5) const
Collect up to max_names registered element names for class_name, sorted by ascending edit-distance to target so the closest candidate appears first. Diagnostic-only: used to build a "did you mean ...?" hint when a user constraint references an unknown element name. Linear scan over the name registry — only ever called on the strict-mode error path, never on the hot path.
const AmplParamMap& gtopt:: SimulationLP:: ampl_params() const noexcept
The whole (class, attribute) -> resolver table, for diagnostics and for tests that pin the registered set.
std::once_flag& gtopt:: SimulationLP:: ampl_registry_flag() const noexcept
Exposed so that SystemLP's constructor can wrap the one-shot AMPL-registry population in std::call_once. Not part of the public user API — friend class SystemLP would be purer but pulls in a circular include. Returning the flag by reference keeps the policy decision (what to register) in system_lp.cpp where all the LP element headers are already visible.
const auto& gtopt:: SimulationLP:: apertures() const noexcept
Gets the aperture definitions from the underlying Simulation.
| Returns | Const reference to the aperture_array |
|---|
std::mutex& gtopt:: SimulationLP:: array_index_mutex() const noexcept
Mutex guarding arrow_index_cache() against the concurrent per-(scene, phase) SystemLP builds.
ArrowIndexCache& gtopt:: SimulationLP:: arrow_index_cache() const
Shared arrow input-index cache (see the member docs). Used by make_array_index so the scene/phase-invariant (Stage, Block) row index is built once across all per-cell builds rather than rebuilt per InputContext. Lazily creates the cache on first use; callers MUST hold array_index_mutex() (make_array_index does), which also serialises that creation. Defined out-of-line in simulation_lp.cpp because ArrowIndexCache is only forward-declared here (pimpl).
std::shared_ptr<ArrowIndexCache> gtopt:: SimulationLP:: arrow_index_cache_ptr() const
Returns the shared cache pointer so callers can propagate it across SimulationLP instances. The cascade passes it to each per-level PlanningLP so the (cname, fname, uid) -> (Stage, Block) index stays warm across levels instead of being rebuilt cold each level (a fresh PlanningLP per level owns a fresh SimulationLP by value, so without this the index-cache fix would be scoped to a single level). Lazily creates on first use. Defined out-of-line (ArrowIndexCache is forward-declared).
void gtopt:: SimulationLP:: finalize_ampl_element_names()
Bulk-load the AMPL element-name registry from the pending buffer filled by register_ampl_element. Sorting once and inserting in ascending key order makes every insert_or_assign land at the end of the flat_map's backing vector (O(1) amortised) rather than shifting it (O(n)), turning the former O(n^2) build into O(n log n). Call exactly once, after all register_ampl_element calls (see register_all_ampl_element_names).
bool gtopt:: SimulationLP:: find_ampl_class_attribute(std::string_view class_name,
std::string_view attribute) const
Is the (class, attribute) pair registered as an LP variable for any element of the class, somewhere in the simulation? This is the LP-attr-dormant leniency check used by the user-constraint resolver for the "element is known and the class supports the attribute, but THIS particular element has zero capacity for the entire horizon and therefore got no LP column" case.
Example: PANGUE_U1 has pmax = 0 in every block → GeneratorLP::add_to_lp never creates a generation column for it, and add_ampl_variable is gated on a non-empty column map (if (!gcols.empty())). So find_ampl_variable_for_element("generator", PANGUE_U1, "generation") returns false. But ANY other non-zero-pmax generator in the system registers generation, so this method returns true — telling the resolver that the attribute is a valid LP variable on the class (just not materialised for this particular element) and the term should silently contribute 0.
Critically, this still rejects:
- Unknown class (no element of that class registers anything).
- Unknown attribute on a class (no element of the class registers this attribute — typo on
generation→generattionreturns false).
Answered from a thread-safe (class, attribute) index filled by every add_ampl_variable.
std::optional<ColIndex> gtopt:: SimulationLP:: find_ampl_col(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
BlockUid block_uid) const
Look up a registered variable. Returns nullopt if the (class, uid, attribute, scenario, stage, block) combination was not registered — e.g., when the column is conditional (soft_emin, drain) and was never created.
std::span<const ColIndex> gtopt:: SimulationLP:: find_ampl_cols(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
BlockUid block_uid) const
Look up a registered sum-of-cols attribute. Returns an empty span when the attribute is not registered or is registered as a single col (callers should fall through to find_ampl_col in that case). Used by the user-constraint resolver to handle virtual aggregators like line.flowp under piecewise_direct mode where the attribute expands to a sum of segment LP cols.
const std::vector<AmplCompoundLeg>* gtopt:: SimulationLP:: find_ampl_compound(std::string_view class_name,
std::string_view compound_name) const noexcept
Look up a compound attribute by (class, compound_name). Returns nullptr when the attribute is not a registered compound (most attributes are ordinary single-column variables). Read-only during the parallel scene-build loop — safe without synchronization.
const AmplElementMetadata* gtopt:: SimulationLP:: find_ampl_element_metadata(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid) const noexcept
Look up an element's metadata bundle. Returns nullptr when the element hasn't registered any (e.g. a type without filter-relevant fields).
AmplIterFn gtopt:: SimulationLP:: find_ampl_iter(std::string_view class_name) const noexcept
Look up the iterator for a class. Returns nullptr when the class is not registered.
double gtopt:: SimulationLP:: find_ampl_offset(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
BlockUid block_uid) const
Look up the per-block additive offset for a registered variable. Returns 0.0 when the variable was not registered with offsets (the common case) or when no offset is recorded for this block. Used by the user-constraint resolver to fold shifted-variable offsets into the row RHS via the existing param_shift mechanism.
AmplParamFn gtopt:: SimulationLP:: find_ampl_param(std::string_view class_name,
std::string_view attribute) const noexcept
Look up the resolver for (class, attribute). Returns nullptr when the pair is not registered — the caller falls back to the legacy path or returns nullopt.
std::optional<double> gtopt:: SimulationLP:: find_ampl_scalar(std::string_view class_name,
std::string_view attribute) const noexcept
Look up a class-level scalar. Returns nullopt when the (class, attribute) pair is not in the allow-list. Read-only during the parallel scene-build loop — safe without synchronization.
const std::string* gtopt:: SimulationLP:: find_ampl_scoped_element(std::string_view class_name,
Uid element_uid) const noexcept
Return the scope description when (class, uid) was registered via register_ampl_scoped_element; nullptr otherwise.
std::optional<std::string_view> gtopt:: SimulationLP:: find_ampl_suppression(std::string_view class_name,
std::string_view attribute) const noexcept
Return the suppression reason if either the class or the (class, attribute) pair is suppressed; nullopt otherwise. Class-level suppression takes precedence over attribute-level.
const AmplVariable* gtopt:: SimulationLP:: find_ampl_variable(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid) const
Look up a registered AMPL variable by its full key (no block). Returns a pointer to the registry entry (node-stable while the registry lives — the resolver only uses it within the same build, before release_ampl_cell), or nullptr when unregistered. Lets the user-constraint resolver hash the key ONCE per (term, block) and then read every per-block shape (col_at / cols_at / weighted_cols_at / offset_at) directly off the entry, instead of repeating the hash+find for each of those four lookups.
bool gtopt:: SimulationLP:: find_ampl_variable_for_element(std::string_view class_name,
Uid element_uid,
std::string_view attribute) const
Is the (class, element_uid, attribute) triple registered as an LP variable somewhere — any (scene, phase, scenario, stage)? Used by the user-constraint resolver to distinguish "attribute is real but the element has no column in THIS specific (scene, phase, scenario, stage, block)" (treat-as-zero) from "attribute is a typo or not supported on this element" (strict-mode error).
Answered from a thread-safe (class, uid, attribute) index filled by every add_ampl_variable (independent of scenario/stage and of release_ampl_cell).
std::span<const std::pair<ColIndex, double>> gtopt:: SimulationLP:: find_ampl_weighted_cols(SceneIndex scene_index,
PhaseIndex phase_index,
std::string_view class_name,
Uid element_uid,
std::string_view attribute,
ScenarioUid scenario_uid,
StageUid stage_uid,
BlockUid block_uid) const
Look up a registered weighted sum-of-cols attribute. Returns an empty span when the attribute is not registered with weighted legs (callers fall through to single-col / unweighted-sum paths in that case). Used by the user-constraint resolver to stamp row[col] += base_coef · weight for every leg of attributes like fuel("X").offtake (registered by FuelLP::add_to_lp as Σ heat_rate_g · dur_b · generation_g[b]).
template<typename Self>
auto&& gtopt:: SimulationLP:: integer_variable_map(this Self&& self,
SystemKind kind) noexcept
Select the forward / aperture integer-variable registry, mirroring variable_map(SystemKind) for state variables.
PhaseIndex gtopt:: SimulationLP:: last_phase_index() const noexcept
Gets the index of the last phase.
| Returns | previous(PhaseIndex{phases().size()}). |
|---|
SceneIndex gtopt:: SimulationLP:: last_scene_index() const noexcept
Gets the index of the last scene.
void gtopt:: SimulationLP:: log_dropped_term_summary() const
Log (INFO) the per-constraint drop summary, one line per constraint, and (WARN) the later-phase-start _prev terms.
std::string_view gtopt:: SimulationLP:: lookup_ampl_element_name(const LPClassName& class_name,
Uid element_uid) const noexcept
Reverse lookup: element name by (LPClassName, uid). Linear scan — only use on rare / diagnostic paths (e.g. SDDP fcut logging). The registry stores snake_case class_name keys; class_name carries both forms precomputed, so we compare against class_name.snake_case() directly — zero runtime conversion. Returns a string_view into the same stable storage as the registry key (element Id::name, owned by System), or empty view when no match is found.
std::optional<Uid> gtopt:: SimulationLP:: lookup_ampl_element_uid(std::string_view class_name,
std::string_view element_name) const
Look up an element Uid by (class_name, name). Read-only during the parallel scene-build loop — safe without synchronization.
const PlanningOptionsLP& gtopt:: SimulationLP:: options() const noexcept
Gets the LP solver options.
| Returns | Const reference to the options object |
|---|
std::optional<std::pair<SceneIndex, PhaseIndex>> gtopt:: SimulationLP:: owning_lp_of(ScenarioUid scenario_uid,
StageUid stage_uid) const noexcept
Look up the unique LP that owns the (scenario, stage) tuple. Returns nullopt if either uid is unknown.
Index gtopt:: SimulationLP:: phase_count() const noexcept
Number of phases as a signed Index.
Convenience wrapper that avoids the repeated static_cast<Index>(sim.phases().size()) idiom at SDDP call sites.
std::optional<PhaseIndex> gtopt:: SimulationLP:: phase_of(StageUid stage_uid) const noexcept
Look up the phase that owns stages bearing the given uid. Returns nullopt when the uid is unknown (e.g. inactive stage).
const auto& gtopt:: SimulationLP:: phases() const noexcept
Gets all phase LP representations.
| Returns | Const reference to vector of PhaseLP objects |
|---|
std::pair<const StageLP*, const PhaseLP*> gtopt:: SimulationLP:: prev_stage(const StageLP& stage) const noexcept
Chronological predecessor of stage.
For a phase stage (stage.phase_stage_index() known):
- same-phase predecessor ->
{prev_stage, nullptr}taken from the phase's OWN stage list (addressed byPhaseStageIndex); - first stage of a later phase ->
{last stage of the previous phase, previous phase}; - first stage of the first phase ->
{nullptr, nullptr}. For a flat global copy (no phase position) the predecessor comes from the global list (StageIndex) and the phase pointer is null.
The two positions are distinct types: indexing the global array with the phase-local position (the pre-fix bug, free water in every non-first stage of a non-first phase) no longer compiles.
void gtopt:: SimulationLP:: record_prev_at_phase_start(std::string_view constraint_name,
std::string_view term) const
Count a user-constraint <attr>_prev term read at the FIRST block of a LATER phase. Its predecessor lives in another phase LP and a plain attribute carries no cross-phase state, so the term is the constant 0 there — a known limitation, reported as a WARN per constraint by log_dropped_term_summary.
void gtopt:: SimulationLP:: register_ampl_active(std::string_view class_name,
AmplActiveFn fn)
Register the class-level activity probe (see AmplActiveFn). Called once per class from system_lp.cpp's AMPL call_once block.
void gtopt:: SimulationLP:: register_ampl_element(std::string_view class_name,
std::string_view element_name,
Uid element_uid)
Register an element's name so that user-expressions like generator("G1") resolve to its Uid.
Called exclusively from system_lp.cpp inside a std::call_once keyed on ampl_registry_flag(). This guarantees the registry is populated exactly once per SimulationLP regardless of whether construction goes through PlanningLP::create_systems (parallel over scenes) or directly via tests that build a single SystemLP. During the parallel scene-build loop the map is read-only, so no mutex is needed.
void gtopt:: SimulationLP:: register_ampl_iter(std::string_view class_name,
AmplIterFn fn)
Register a class-level iterator function. Called once per class from system_lp.cpp's AMPL call_once block.
void gtopt:: SimulationLP:: register_ampl_param(std::string_view class_name,
std::string_view attribute,
AmplParamFn fn)
Register one (class, attribute) -> resolver entry. Called once per attribute from system_lp.cpp's AMPL call_once block.
void gtopt:: SimulationLP:: register_ampl_scoped_element(std::string_view class_name,
Uid element_uid,
std::string description)
Mark (class, uid) as scope-restricted. description is a short human-readable phrase ("scope 'global'", "single-block selector
(block 3)") surfaced in the diagnostic.
void gtopt:: SimulationLP:: release_ampl_cell(SceneIndex scene_index,
PhaseIndex phase_index) noexcept
Release the per-(scene, phase) AMPL variable + metadata maps once the cell's LP has been flattened. Gated by LpMatrixOptions::release_ampl_after_flatten (set on the production solve path) so direct PlanningLP/SystemLP tests that inspect the registry post-build keep it. The maps are only read to resolve user constraints during the build and never after flatten, so on user-constraint-heavy cases (CEN PLEXOS, irrigation) this reclaims the registry's memory — hundreds of MB at CEN scale — for the solve. Move-assigning fresh empty maps (rather than clear()) returns the node storage to the allocator. Safe from the owning build thread: each cell touches only its own [scene][phase] slot, and a later rebuild re-populates it via add_ampl_variable.
const auto& gtopt:: SimulationLP:: scenarios() const noexcept
Gets all scenario LP representations.
| Returns | Const reference to vector of ScenarioLP objects |
|---|
Index gtopt:: SimulationLP:: scene_count() const noexcept
Number of scenes as a signed Index.
Convenience wrapper that avoids the repeated static_cast<Index>(sim.scenes().size()) idiom at SDDP call sites.
const std::optional<SceneMerge>& gtopt:: SimulationLP:: scene_merge() const noexcept
The scene merge applied for nonanticipative DecisionVariables (see scene_merge.hpp): when set, scenes() holds ONE LP scene spanning the original active scenes, and the output writer splits each cell's shards back into original_scenes. nullopt ⇒ no merge.
std::optional<SceneIndex> gtopt:: SimulationLP:: scene_of(ScenarioUid scenario_uid) const noexcept
Look up the scene that owns scenarios bearing the given uid. Returns nullopt when the uid is unknown (e.g. inactive scenario).
const auto& gtopt:: SimulationLP:: scenes() const noexcept
Gets all scene LP representations.
| Returns | Const reference to vector of SceneLP objects |
|---|
void gtopt:: SimulationLP:: set_alpha_offsets(StrongIndexVector<SceneIndex, double> offsets)
Publish the per-scene α-rebase offsets (copied from the SDDP method's m_scene_alpha_offsets_). Read-only afterwards.
void gtopt:: SimulationLP:: set_need_ampl_variables(bool v) noexcept
Enable or disable AMPL variable registration. When false (the default), add_ampl_variable is a no-op and the per-cell variable maps stay empty — avoiding allocation overhead when no consumer (user constraints, PAMPL expressions) needs them.
The flag is set by SystemLP when user constraints exist or by PlanningLP when PAMPL evaluation is requested.
template<typename Self>
auto&& gtopt:: SimulationLP:: simulation(this Self& self) noexcept
Gets the underlying simulation model.
| Returns | Reference to the simulation object |
|---|
template<typename Self, typename Key = state_ variable_ key_ t>
auto gtopt:: SimulationLP:: state_variable(this Self&& self,
Key&& key) noexcept
Retrieves a state variable by its key.
| Parameters | |
|---|---|
| self | The object instance (deduced via explicit object parameter) |
| key | The key to search for |
| Returns | Optional reference to the state variable if found (const or non-const) |
void gtopt:: SimulationLP:: suppress_ampl_attribute(std::string_view class_name,
std::string_view attribute,
std::string_view reason)
Mark a specific attribute as suppressed (e.g. bus.theta when Kirchhoff is disabled). Same lifetime requirements as suppress_ampl_class.
void gtopt:: SimulationLP:: suppress_ampl_class(std::string_view class_name,
std::string_view reason)
Mark a class as suppressed (e.g. line under use_single_bus). reason should outlive SimulationLP — a constexpr literal is fine, a caller-owned std::string is not.
SceneUid gtopt:: SimulationLP:: uid_of(SceneIndex scene_index) const noexcept
Convert a SceneIndex (0-based array position) to the matching SceneUid (1-based identity from JSON input).
Convenience wrapper that replaces the repeated sim.scenes()[scene_index].uid() idiom at SDDP call sites (notably the cut-loader paths in source/sddp_cut_io.cpp, where the uid is fed straight into make_iteration_context).
The name mirrors the free-function gtopt::uid_of(IterationIndex) in <gtopt/iteration.hpp> — one idiom (uid_of) for every dimension's Index-to-Uid conversion. Method overload resolves on the index type: sim.uid_of(SceneIndex{k}) → SceneUid, sim.uid_of(PhaseIndex{k}) → PhaseUid.
std::optional<std::pair<SceneIndex, PhaseIndex>> gtopt:: SimulationLP:: unique_owning_lp_of(std::span<const std::pair<ScenarioUid, StageUid>> refs) const noexcept
Returns the unique owning LP iff every tuple in refs maps to the same (scene, phase). Used by row dispatch to detect constraints that would cross LPs. An empty span returns nullopt (no constraint to assign).
template<typename Self>
auto&& gtopt:: SimulationLP:: variable_map(this Self&& self,
SystemKind kind) noexcept
Select the forward or aperture state-variable registry. The two maps have identical [scene][phase] shape but hold the columns of two physically distinct LPs (see SystemKind). Routing by key.lp_key.kind keeps every forward consumer on m_global_variable_map_ untouched while the aperture-system build/lookup uses the second map.