flexops.costing

EECO integration

flex-pse does not build its own tariff/cost engine. Tariffs, demand charges, tiered and fixed charges, and both the cost computations at optimization time and after optimization come from the external EECO package (eeco on PyPI), a core runtime dependency. flexops.costing.opex is the thin flex-pse interface around it, and by convention it’s the only module in the codebase that imports eeco, so there’s just one file to fix when EECO’s API moves.

EECO owns all cost math. These wrappers are glue. They marshal inputs, rename EECO’s outputs to stable flex-pse names, and translate EECO/pandas errors into the flex-pse exception hierarchy. A flex-pse tariff object is simply an EECO rate_data DataFrame (EECO 0.4.2 has no tariff loader of its own, so its cost functions consume that frame directly).

Note

EECO is only needed for tariffs. The eeco import is soft, so every carrier given a native energy price builds, solves, and reports without EECO installed, including report_cost. Reaching a tariff path without eeco raises FlexConfigError naming both remedies (install it, or price the carrier natively).

Loaders and CSV conversion

load_tariff

Return an EECO tariff object (a rate_data DataFrame) from source.

merge_tariffs

Load several tariff sources and merge them into one rate_data frame.

load_dr_program

Load a demand-response program (v0: containers-only; None-safe).

tariff_csv_to_dict

Convert an EECO rate_data CSV (or DataFrame) into the dict load_tariff takes.

tariff_currency_units

Return the Pyomo currency unit for a tariff's currency basis.

currency_units

Return the Pyomo currency unit named symbol, registering it on first use.

monthly_scale_factor

Fraction of its starting calendar month that a horizon covers (<= 1).

capital_recovery_factor

Return the annual capital recovery factor.

Several tariff files merge into one frame with merge_tariffs(), which is what FlexCosting(tariff_file=...) uses when given a list or a mapping. Passing a list merges every source as is. Passing a mapping of EECO utility to source assigns each file to a utility and keeps only that utility’s rows, so a sheet carrying stray rows for another utility cannot leak into the bill:

m.costing = fo.FlexCosting(
    time_block=m.time_block,
    tariff_file={"electric": "pge_b20.json", "gas": "socal_gn10.json"},
)

Both forms end at a single rate_data frame. There is no separate tariff object per utility, since the frame’s utility column is what selects rows for each cost leg. Two sources defining the same charge raise rather than silently doubling that line item (EECO sums colliding charge keys).

A tariff may be authored as a JSON records structure or imported from an EECO rate_data CSV. The JSON form is a tariff_data records list, e.g. (an excerpt of the demo flexdemo-b20 time of use tariff):

{
  "tariff_data": [
    {"utility": "electric", "type": "energy", "name": "peak",
     "month_start": 6, "month_end": 9, "weekday_start": 0, "weekday_end": 4,
     "hour_start": 16, "hour_end": 21, "basic_charge_limit (metric)": 0,
     "charge (metric)": 0.18, "units": "$/kWh"},
    {"utility": "electric", "type": "demand", "name": "peak-demand",
     "month_start": 6, "month_end": 9, "weekday_start": 0, "weekday_end": 4,
     "hour_start": 16, "hour_end": 21, "basic_charge_limit (metric)": 0,
     "charge (metric)": 21.5, "units": "$/kW"}
  ]
}

Charge windows are half open on the hour. For example, hour_start=16, hour_end=21 bills hours 16:00 through 20:59 inclusive (21:00 is off peak).

An optional assessed column controls the billing period of a demand charge row. EECO defaults to "monthly" when the column is absent or the row omits it, applying one demand charge epigraph over the whole month_start to month_end window. Set "assessed": "daily" on a demand row to instead apply a separate epigraph per calendar day within that window. That’s the pattern behind daily demand charges on tariffs such as those increasingly common in California.

Tariff signal helpers

Plain pandas signals over a tariff, for writing logic or heuristic constraints. Each is a flex-pse helper built on EECO’s get_charge_dict charge arrays (the source of the price data). EECO 0.4.2 exposes no per-stamp price accessor.

price_series

Base energy price ($/kWh) at each stamp (flex-pse helper over EECO).

is_peak

Boolean mask: True at the highest-price stamps (flex-pse helper over EECO).

peak_windows

Sub-index of the peak (highest-price) stamps (flex-pse helper over EECO).

price_gradient

Δ base energy price between successive stamps (flex-pse helper over EECO).

In-objective cost bridge

add_operating_cost

Build the facility's whole in-objective operating cost — electric and fuel.

add_electricity_cost

Build EECO's convex-relaxed in-objective electricity cost on block.

add_fuel_cost

Build EECO's convex-relaxed in-objective fuel cost on block.

OperatingCostHandles

Stable flex-pse handles for EECO's in-objective cost expressions.

add_operating_cost() is the facility level umbrella. It builds both the electricity and fuel costs onto one opex block (EECO namespaces its Pyomo components by utility, so electric_* and gas_* never collide) and returns a single OperatingCostHandles whose total_operating_cost is the sum across utilities. The facility consumption defaults to the standard series on the block, block.power_electrical and block.fuel_usage, so a caller need not re-declare them each use. Pass electrical_power/fuel_power to override. Every series handed to these builders must carry no units. It should be a bare magnitude in kW (electric) or m³/hr (fuel volumetric flow). EECO applies its own unit conversion as a plain float factor and constrains its own dimensionless Vars to the series it is given. The builders for a single utility, add_electricity_cost() and add_fuel_cost(), remain available for building one leg alone. add_fuel_cost() takes a fuel_type (default "gas", the only value EECO 0.4.2 supports). A hydrogen utility is expected upstream and will add a second value.

Post-optimization evaluators

evaluate_cost

Compute the TRUE (de-relaxed) electricity cost on a fixed realized load.

evaluate_fuel_cost

Compute the TRUE (de-relaxed) fuel cost on a fixed realized usage array.

Demand response (containers-only in v0)

DRConfig

A container/config slot for a demand-response program (v0 no-op).

Note

Demand response is containers only in v0. A DRConfig holds a loaded DR program so the wiring exists, and the internal DR hook is a no-op. Supplying a DR file never changes the objective. Building DR event, curtailment, incentive, and capacity constraints comes after v0. EECO 0.4.2 exposes no DR API, so the DR file format is a flex-pse placeholder loaded into the container only.

In-objective vs. reported cost

EECO is used two ways. add_operating_cost() asks EECO to build the convex relaxed operating cost Expression on a Pyomo block, the tractable proxy the scheduler minimizes. evaluate_cost() evaluates EECO on a fixed, realized aggregate power numpy array to compute the TRUE (no longer relaxed) cost, the bill the user actually sees (see Why the reported cost isn’t the solver’s objective).

The raw solver objective is never reported as the user-facing cost.

Note

Tiered charges and the consumption estimate

EECO prices a top tier (no higher tier above it) at one constant rate exactly. Any other tiered energy or demand charge is dropped from the objective unless EECO is given a consumption estimate (flex-pse logs a warning when one is missing), so the in-objective total then understates the true bill. That includes a base tier, since EECO links the tiers of one charge by their shared name.

Pass consumption_estimate (a mapping of EECO utility, "electric" or "gas", to an estimated total consumption over the horizon in kWh / m^3) to add_operating_cost(), or set CostingConfig.consumption_estimate, to price such a tier. The result is approximate and can land on either side of the true bill; relaxation_gap() reports by how much, given the solver results of an optimal solve.

Tier limits are monthly while the estimate covers the horizon, so on a sub-month horizon a tier only triggers if the horizon alone exceeds its limit. For a tiered demand charge, EECO converts the estimate into an average kW, which can miss a real peak.

Timezones / DST

EECO reasons in naive local wall clock time. Its charge windows are keyed on datetime.month/weekday/hour with no timezone conversion. flex-pse v0 is consistently naive and local (matching TimeBlock). Datetime indices that carry a time zone are rejected at the wrapper boundary with FlexDataError. Strip the timezone (index.tz_localize(None)) before passing an index in.

FlexCosting block

FlexCosting

FlexCostingData

EECO-backed costing block with opex/capex sub-blocks (module docstring).

CostReport

The reported, categorized cost from FlexCostingData.report_cost().

OperatingCostBreakdown

Categorized operating cost from FlexCostingData.report_cost().

CapitalCostBreakdown

Categorized capital cost from FlexCostingData.report_cost().

ScalarCostSpec

A non-energy scalar cost registered on FlexCosting (never billed via EECO).

FlexCosting subclasses IDAES FlowsheetCostingBlockData and delegates all tariff and energy operating cost to EECO, in two ways. It hands EECO the aggregate electrical power (as a bare kW magnitude, via a dimensionless opex.eeco_aggregate_electrical_power normalization Var) plus the tariff to build the convex relaxed in-objective cost (add_electricity_cost()), and after solving calls EECO’s evaluator (evaluate_cost()) for the reported bill. Its own jobs are aggregation, the opex/capex block structure and naming, CapEx plus modes, and report_cost. It writes no tariff cost math.

Every quantity FlexCosting exposes is a decision visible Var defined by an eq_<name> equality Constraint. Aggregate power, each cost line item, the annualized cost, and the totals are all first-class model variables, not bare Expressions.

Every cost lives in one of two sub-blocks built by cost_process().

  • ``opex`` holds all operating cost. That includes electricity_cost and fuel_cost (both from EECO), fixed_operating_cost (a facility cost outside the tariff, covering maintenance, labor, and chemicals, from CostingConfig.fixed_operating_cost), and scalar_cost (flows, supplies, or products with no energy content, below). Their sum, total_operating_cost, is re-exposed as aggregate_operating_cost, the objective used in operations mode. The fixed operating cost is distinct from the tariff’s own fixed_charge, which EECO already folds into electricity_cost.

  • ``capex`` is an empty placeholder in v0 (total_capital_cost == 0, re-exposed as aggregate_capital_cost). Later milestones aggregate the capital costs of individual units into it.

Note

Power aggregation, indexed per carrier.

cost_process() pulls every registered power draw from the model and defines aggregate_power[t, carrier] in kW, where carrier is "electrical" or a thermal label tied to a specific temperature, "thermal@<T>K". Every draw is normalized to kW with pyunits.convert at aggregation, so a draw that is not a power, or does not convert to kW, fails loudly. Thermal duties at different temperatures are never summed together. Each temperature is its own carrier. aggregate_thermal_power is a total that ignores temperature, kept for backward compatibility.

Note

Fuel is a volume, not a power.

A combustible fuel is metered and billed on volume, so it is aggregated separately from power. cost_process pulls every fuel usage flow a unit registered with register_fuel_usage() and defines aggregate_fuel_usage[t, fuel] directly in EECO’s m³/hr (a flow that is not a volumetric rate fails loudly there). Each fuel is billed through add_fuel_cost() against the same tariff loaded at construction, on its own opex.fuel_<name> sub-block so EECO’s gas_* components never collide. That happens via that sub-block’s dimensionless eeco_aggregate_fuel_usage Var, which carries the m³/hr aggregate as a bare magnitude.

Fuels are discovered from the model, so there is nothing to declare on the costing block. A model with no fuel flow builds no gas leg, and opex.fuel_cost is 0. flex-pse applies no heating value and does no fuel type recognition. A fuel priced in the tariff sheet’s rows for the gas utility just works, a tariff missing those rows surfaces EECO’s own validation error, and if a tariff prices gas on an energy basis, converting it is EECO’s job (it applies its own assumption about fuel heating value).

Note

Scalar costs for flows that are not energy are native, never through EECO.

register_scalar_cost() costs an arbitrary rate indexed over time (a flow, supply, or product) as price × Σ_t quantity[t] × dt, for example water withdrawal per m³, chemical dosing per kg, or a credit for product revenue (a negative price). This is built entirely in flex-pse. EECO is not involved. A quantity that does not convert to the declared quantity_units raises, forcing unit consistency.

Note

Native energy prices, no tariff, no EECO.

A carrier does not need a tariff at all. Give energy_prices a price per carrier or fuel and that carrier is billed natively as Σ_t price[t] × quantity[t] × dt, exactly like a scalar cost:

m.costing = fo.FlexCosting(
    time_block=m.time_block,
    energy_prices={
        "electrical":  0.12 * currency_units("USD") / pyunits.kWh,
        "natural_gas": 0.50 * currency_units("USD") / pyunits.m**3,
    },
)

Keys are "electrical" or a registered fuel name. Native prices and an EECO tariff mix freely across carriers, say electricity priced by a tariff alongside a fuel priced natively. Each carrier is billed one way only:

  • A carrier with an energy_prices entry is billed natively and never sent to EECO, even when the tariff also covers its utility (electric for "electrical", gas for any fuel). The native price replaces that carrier’s whole tariff bill, so the utility’s demand, fixed and export charges do not apply to it either.

  • A carrier without an entry is billed by EECO against the tariff, and its utility must have tariff rows, or the build raises FlexConfigError.

Every fuel bills against the tariff’s single gas utility, so a native price for one fuel leaves the gas rows in place for the others.

A price may vary over the horizon. Each entry is one of three things. It can be a single value (flat over the horizon), an array-like with one value per time point, or a Pyomo component indexed over the horizon. That means the price can be a Param you update between solves, or even a Var:

m.day_ahead = pyo.Param(
    m.time_block.time_index, initialize=hourly_prices, mutable=True,
    units=currency_units("USD") / pyunits.kWh,
)
m.costing = fo.FlexCosting(
    time_block=m.time_block,
    energy_prices={"electrical": m.day_ahead},
)

An array-like is read in order against the time index. An indexed component is read in the order of its own index set, so it does not have to live on the TimeBlock’s time_index. It only needs exactly one value per time point. Anything with the wrong number of values raises FlexConfigError at construction, naming both counts. A mapping is rejected outright, since iterating it would cost its keys.

Units are explicit or inferred. A price may carry Pyomo units (0.12 * currency_units("USD") / pyunits.kWh), which must reconcile with the base currency or the check for unit consistency raises. A bare number is read in the currency over the carrier’s metered quantity, kWh for a power carrier and m³ for a fuel. So {"electrical": 0.12} means 0.12 per kWh and {"natural_gas": 0.50} means 0.50 per m³, both in the base currency.

A carrier that has registered draws but is priced by neither a native price nor a tariff covering its utility raises FlexConfigError naming the carrier, rather than contributing a silent 0 to the bill.

Note

Horizons shorter than a month prorate the monthly assessed charges.

A demand charge and a tariff’s fixed (customer) charge are billed per calendar month, so a horizon shorter than a month owes only its share. With prorate_monthly_charges (the default), both are scaled by monthly_scale_factor(), the horizon’s length divided by the real length of the calendar month it starts in. Energy charges are never scaled, and a demand charge the tariff marks "assessed": "daily" is already at horizon granularity so it is left alone. Set the option to False to bill full monthly charges regardless of horizon length.

Prorating is applied to the charge rates before EECO sees them, which leaves EECO’s tiered surcharge arithmetic untouched. flex-pse does this itself, since EECO’s get_charge_dict path exposes no equivalent option. Its calculate_cost adds the customer charge whole, and its demand_scale_factor is suppressed for charges spanning <= 1 day. EECO clips its charge date keys to the horizon, so without that suppression a horizon of one day would silently pick up every monthly charge.

Note

Annualization.

cost_process builds a capital_recovery_factor and an annualized_cost Var (base currency per year). That is the operating cost scaled from the horizon to a year plus capital cost times the CRF. With the empty v0 capex block, the annualized cost is just the operating cost on an annual basis.

The CRF is built on a single effective_rate. Supplying only discount_rate uses it directly. Supplying interest_rate as well (the nominal cost of capital) deflates it into a real rate, (1 + interest_rate) / (1 + discount_rate) - 1. An effective rate of 0 falls back to a straight line calculation, 1/lifetime_years, and an effective rate <= -1 or a lifetime that is not positive raises.

Note

Currency basis.

The costing block’s base_currency, which sets the units on every operating cost and capital cost expression it builds, is the tariff sheet’s currency basis, read from the charge units column by tariff_currency_units() (EECO tariffs are based on the dollar, so "$" maps to USD). With no tariff it is the configured currency (default USD), available as currency_units() for writing flat prices. EECO’s own cost expressions are dimensionless dollars, so FlexCosting casts them to this currency. The report_cost numbers are magnitudes in that currency, and CostReport.currency names it (str(base_currency), e.g. "USD"), so a report describes itself independent of the block that produced it.

Note

Capital cost enters the objective only in design mode.

The objective in operations mode is aggregate_operating_cost alone. The objective in design mode is total_cost (operating + capital). set_operations_mode() fixes the registered sizing Vars and deactivates their capex constraints. set_design_mode() unfixes and activates them. Both are idempotent toggles on a single model. Merging representative months and linking sizing Vars across them is the job of flexdesign.

Note

Export prices.

By default exports are credited at the import price, because an export is a negative net series (net metering). To sell for less than you buy, give export_prices, keyed like energy_prices, for any natively priced carrier: electricity or a fuel the site sells:

m.costing = fo.FlexCosting(
    time_block=m.time_block,
    energy_prices={"electrical": pool_price, "biogas": 0.50},
    export_prices={"electrical": 0.5 * pool_price, "biogas": 0.20},
)

Each export price takes the same forms and units as its energy_prices entry. The carrier’s net series is then split into non-negative opex.import_<carrier> and opex.export_<carrier>, with net[t] == import[t] - export[t], and the carrier costs Σ_t (import_price[t] × import[t] − export_price[t] × export[t]) × dt. Because an export price may not exceed its import price, the optimizer never imports and exports in the same period, so the model stays an LP with no binaries. An export price above the import price at any time point raises FlexConfigError (the check skips a price component that has no value yet), as does an export_prices key with no energy_prices entry. The split Vars can carry import and export limits directly.

Note

Exports on the tariff path.

An EECO tariff can carry export charges, but EECO only applies them once net power is split into imports and exports. Pass decomposition_type="linear" (or any type EECO supports) to FlexCosting and it is handed to EECO for the electric and gas legs, in both the in-objective cost and the post-solve bill (report_cost). The default None skips the split. A tariff with an electric or gas export charge requires it: with None EECO bills the net series as both imports and exports, so FlexCosting raises FlexConfigError instead of misbilling. EECO owns the available types and whether a type keeps the problem linear: "linear" stays an LP and is exact when no export charge exceeds its energy charge, while "absolute_value" is nonlinear.

Note

Reporting rule.

report_cost() returns a categorized CostReport, capital versus operating, each itemized, recomputed after solving, never read off the solver objective (a relaxed or scalarized proxy). Operating electricity and fuel are EECO evaluations computed after the fact on the realized dispatch (fuel on the realized aggregate_fuel_usage, 0 when the model burns none), and fixed is the config constant. In v0 dr_revenue and the whole capital breakdown are zero placeholders, so the structure is stable as those features land. Pass split_imports_exports=True to report_cost to also get operating.imports (import cost) and operating.exports (export revenue, a positive magnitude), each keyed by carrier like energy_prices, alongside the net electricity and fuel. Only natively priced carriers appear; a tariff bill is EECO’s single net figure. See Why the reported cost isn’t the solver’s objective for why this number, and not the solver’s internal objective, is the one a user should trust.

Construction-order invariant

FlexCosting may be constructed before any units exist. The API-freeze script builds m.costing before m.waterfacility.tank, since all aggregation and the EECO call are deferred to cost_process(), which pulls every unit’s registered power and fuel usage from the model. Building costing first, last, or between units gives the identical result.