gtopt::OutputContext class

Public types

struct ClassFieldNameHash
struct FieldEntry
struct PreludeUids
using ClassFieldName = std::array<std::string_view, 3>
using FieldVector = std::vector<FieldEntry>
using FieldVectorMap = std::unordered_map<ClassFieldName, FieldVector, ClassFieldNameHash>

Constructors, destructors, conversion operators

OutputContext(const SystemContext& psc, LinearInterface& linear_interface, SceneUid scene_uid = make_uid<Scene>(0), PhaseUid phase_uid = make_uid<Phase>(0), bool is_continuous_phase = false) explicit

Public functions

void add_col_cost(std::string_view cname, std::string_view col_name, const Id& id, const GSTBIndexHolder<ColIndex>& holder)
void add_col_cost(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder)
void add_col_cost(std::string_view cname, std::string_view col_name, const Id& id, const STIndexHolder<ColIndex>& holder)
void add_col_cost(std::string_view cname, std::string_view col_name, const Id& id, const TIndexHolder<ColIndex>& holder)
void add_col_cost_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder)
void add_col_cost_values(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)
void add_col_cost_values_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)
void add_col_sol(std::string_view cname, std::string_view col_name, const Id& id, const GSTBIndexHolder<ColIndex>& holder)
void add_col_sol(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder)
void add_col_sol(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<std::vector<ColIndex>>& holder)
void add_col_sol(std::string_view cname, std::string_view col_name, const Id& id, const STIndexHolder<ColIndex>& holder)
void add_col_sol(std::string_view cname, std::string_view col_name, const Id& id, const TIndexHolder<ColIndex>& holder)
void add_col_sol_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder)
void add_col_sol_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<std::vector<ColIndex>>& holder)
void add_col_sol_integer(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder, bool relaxed = false)
void add_col_sol_values(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)
void add_col_sol_values_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)
void add_dual_derived_values(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)
void add_row_dual(std::string_view cname, std::string_view row_name, const Id& id, const GSTBIndexHolder<RowIndex>& holder)
void add_row_dual(std::string_view cname, std::string_view row_name, const Id& id, const STBIndexHolder<RowIndex>& holder)
void add_row_dual(std::string_view cname, std::string_view row_name, const Id& id, const STBIndexHolder<RowIndex>& holder, const STIndexHolder<double>& st_scale)
void add_row_dual(std::string_view cname, std::string_view row_name, const Id& id, const STIndexHolder<RowIndex>& holder)
void add_row_dual(std::string_view cname, std::string_view row_name, const Id& id, const TIndexHolder<RowIndex>& holder)
void add_row_dual_extras(std::string_view cname, std::string_view row_name, const Id& id, const STBIndexHolder<RowIndex>& holder)
void add_row_dual_raw(std::string_view cname, std::string_view row_name, const Id& id, const STBIndexHolder<RowIndex>& holder)
add_row_dual using discount-only scaling (scale_obj / discount[t]).
void add_row_dual_values(std::string_view cname, std::string_view row_name, const Id& id, const STBIndexHolder<double>& holder)
auto cost(ColIndex col) const -> double noexcept
auto dual(RowIndex row) const -> double noexcept
auto emit_dual(std::string_view cname) const -> bool noexcept
auto emit_extras(std::string_view cname) const -> bool noexcept
auto emit_reduced_cost(std::string_view cname) const -> bool noexcept
auto emit_solution(std::string_view cname) const -> bool noexcept
auto fields() const -> const auto& noexcept
auto has_col_costs() const -> bool noexcept
auto has_row_duals() const -> bool noexcept
Row-dual counterpart of has_col_costs().
auto options() const -> auto&& noexcept
auto output_flags() const -> OutputFlags noexcept
Which output fields were requested for this context.
auto output_selection() const -> const OutputSelection& noexcept
auto primal(ColIndex col) const -> double noexcept
auto system_context() const -> const SystemContext& noexcept
void write() const

Typedef documentation

using gtopt::OutputContext::ClassFieldName = std::array<std::string_view, 3>

Map key for field_vector_map: (cname, fname, sname). All three are static string_view literals (class_name / field-name constants / fixed suffix tag), so this is a zero-allocation key — replaces the former pair<string_view, Name> whose Name (std::string) was freshly heap-allocated via as_label(fname, sname) on every call.

Function documentation

void gtopt::OutputContext::add_col_cost_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder)

Extras-gated variant of add_col_cost(..., STBIndexHolder<ColIndex>). Used by GeneratorLP for heat-rate slack reduced costs and by LineLP for overload-slack reduced costs. Both streams are retained for future audit use but read by no current consumer.

void gtopt::OutputContext::add_col_cost_values_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)

Extras-gated variant of add_col_cost_values. Same on-disk shape (<col_name>_cost.parquet) and same factor pipeline; gated on OutputFlags::extras instead of reduced_cost so the caller can keep the stream around for opt-in audits without bloating the default footprint. Used by LineLP::add_to_output for the inactive-direction reduced cost flown_cost that complements the default-emitted signed combined flow_cost.

void gtopt::OutputContext::add_col_sol(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<std::vector<ColIndex>>& holder)

Sum-of-cols solution overload: writes Σ col_sol[col] for each block. Used by piecewise_direct line-loss mode to emit Line.flowp:sol / Line.flown:sol as the per-block segment-sum even though no aggregator LP column was created. The output scalar is the same value the AMPL resolver computes for line.flowp via the multi-col registration (see AmplVariable::block_cols_sum and LineLP::add_to_lp), so downstream consumers — solution.csv, the line.flow derivation, PAMPL user-constraints — see a consistent flow scalar across LP / AMPL / output regardless of whether the line is aggregator-mode or direct-mode.

void gtopt::OutputContext::add_col_sol_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder)

Extras-gated variant of add_col_sol(..., STBIndexHolder<ColIndex>). File name stays <col_name>_sol.parquet; only the gate moves to OutputFlags::extras. Used by LineLP for piecewise-segment slices, line losses, and overload slacks — none of which any current consumer reads.

void gtopt::OutputContext::add_col_sol_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<std::vector<ColIndex>>& holder)

Extras-gated variant of add_col_sol(..., STBIndexHolder<std::vector<ColIndex>>). Used by LineLP to publish the consolidated Line/loss_sol.parquet from a merged lossp ∪ lossn holder: per-(line, scenario, stage, block) value is Σ col_sol[col_k] over whichever direction-specific loss columns were populated on that cell (at most one of A→B / B→A has loss at any block; merging gives one schema-stable file instead of paired direction parquets). Empty-skip semantics mirror the non-extras overload.

void gtopt::OutputContext::add_col_sol_integer(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<ColIndex>& holder, bool relaxed = false)

Integer-snapping variant of add_col_sol for binary / integer LP columns (commitment status / startup / shutdown). The MIP solver returns these within a small feasibility tolerance (~1e-6) of 0 or 1, but in practice the float32 grid + per-column decimal rounding leaves a small tail of fractional reports (e.g. 0.058, 0.629) even on a MIP solved to a zero gap. Snap each value to int(round(v)) after the standard add_field collection so the output parquet carries clean 0/1 integers, matching the LP variable's declared type. Calling site for CommitmentLP.

LP-relax gate: when the phase is solved as a continuous LP (via model_options.continuous_phases or per-phase continuous: true), the binary variable is genuinely fractional and the fractional reading is the meaningful output — snapping would corrupt it. The dual-extraction pass also re-solves LP-relaxed, but with the binaries fixed at their MIP 0/1 values, so col_sol is already integer-valued there and snapping would be a no-op; we still skip it to keep the policy "snap iff the solver was asked to enforce integrality on this phase".

Element-level relax gate: relaxed is the element's own LP-relax flag (Commitment.relax, LineCommitment.relax). A relaxed element's columns are continuous in [0, 1] even in a MIP phase, so its fractional LP value IS the solution and is written unrounded (rounding would, e.g., report a unit at u = 0.6 as fully on).

void gtopt::OutputContext::add_col_sol_values_extras(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)

Extras-gated variant of add_col_sol_values. Same on-disk shape (<col_name>_sol.parquet) and same encoding; gated on OutputFlags::extras instead of solution so the caller can keep the stream around for opt-in audits without bloating the default footprint. Used by GeneratorLP::add_to_output for the per-block VOM / fuel cost decomposition (no current consumer reads these — srmc_sol already covers the marginal-cost use case).

void gtopt::OutputContext::add_dual_derived_values(std::string_view cname, std::string_view col_name, const Id& id, const STBIndexHolder<double>& holder)

Emit <col_name>_sol values derived from row duals (see dual): the holder carries linear combinations of folded duals and receives the Power-family inverse cost factors, so the written value is on the same $/MW(h) scale as the plane / bus duals.

void gtopt::OutputContext::add_row_dual(std::string_view cname, std::string_view row_name, const Id& id, const STBIndexHolder<RowIndex>& holder, const STIndexHolder<double>& st_scale)

add_row_dual with an additional per-(scenario,stage) back-scale factor. Used by StorageLP::add_to_output for daily-cycle dual correction.

void gtopt::OutputContext::add_row_dual_extras(std::string_view cname, std::string_view row_name, const Id& id, const STBIndexHolder<RowIndex>& holder)

Extras-gated variant of add_row_dual(..., STBIndexHolder<RowIndex>). Used by GeneratorLP for the capacity-row dual. The dual is the shadow price on the per-block gen <= pmax constraint — informative for capacity-expansion studies, but unused by the dispatch / marginal-unit pipelines.

double gtopt::OutputContext::cost(ColIndex col) const noexcept

Physical LP reduced cost (in $/[unit]) at the given LP column. Counterpart to :func:primal that reads the already-cached col_cost_span. Used by LineLP::add_to_output (and any other reconstruction site) that combines per-direction LP columns into a single output stem and needs the rc of each piece to choose a signed combined rc.

double gtopt::OutputContext::dual(RowIndex row) const noexcept

Physical row dual at the given LP row. Row counterpart of :func:cost; used by reconstruction sites that report the reduced cost of a substituted slack as -dual of the row it was folded into (e.g. the slack-free GeneratorProfile form). Callers MUST check has_row_duals() first. The value is BEFORE the per-block cost-factor unfolding (÷ probability · discount · duration) that add_row_dual applies: combine such values linearly and emit them through add_dual_derived_values, which applies the same unfolding.

bool gtopt::OutputContext::emit_extras(std::string_view cname) const noexcept

Opt-in gate for streams that no current consumer reads (heat-rate slacks, per-block cost decomposition, line piecewise / loss / overload slack columns, capacity row duals). *LPadd_to_output routes those calls through add_*_extras overloads which check this gate instead of the default sol/dual/rc gate.

const auto& gtopt::OutputContext::fields() const noexcept

Read-only view of the accumulated field map, keyed by (class_name, field_name, suffix). Each entry is a FieldVector of sparse FieldEntry streams (see above). Exposed for regression tests that need to assert on the schema (column presence, naming) or the collected values without going end-to-end through the Parquet writer. Not part of the production code path — write consumes the map directly.

bool gtopt::OutputContext::has_col_costs() const noexcept

True when the solve published reduced costs. False under the dual-recovery bail (MIP primal preserved, duals unavailable — see fix_mip_and_resolve_duals): reconstruction sites that read cost() per column MUST check this and skip their :cost streams instead of indexing the empty span (production SIGABRT, 2026-07-13, span:302 hardening assert).

double gtopt::OutputContext::primal(ColIndex col) const noexcept

Physical primal value at the given LP column. Equivalent to linear_interface().get_col_sol()[col] but reads from the already-cached col_sol_span so callers in add_to_output don't need to re-fetch the LP-solution view. Used by the reconstruction sites (e.g. the planned P0 demand-failure rewrite) that need to read a surviving variable's primal value in order to compute the substituted variable's output.

const SystemContext& gtopt::OutputContext::system_context() const noexcept

Borrow the owning SystemContext so output sites can reach the persistent registries (e.g. simulation(), system()) that survive per-cell LP rebuilds under low_memory = compress. Used by FutureCostLP::add_to_output to SELF-FIND its α columns + rebase offset at write time rather than relying on a per-cell stash.