Skip to content

fluxopt.model

Classes:

Name Description
FlowSystem

FlowSystem

FlowSystem(data: ModelData)

Initialize the flow system optimization model.

Parameters:

Name Type Description Default

data

ModelData

Pre-built model data.

required

Methods:

Name Description
build

Build all variables, constraints, and the objective.

optimize

Build, optionally customize, and solve the model.

solve

Solve the built model and return results.

Source code in src/fluxopt/model.py
def __init__(self, data: ModelData) -> None:
    """Initialize the flow system optimization model.

    Args:
        data: Pre-built model data.
    """
    self.data = data
    self.m = Model()
    self._objective_effects: list[str] = []
    self._piecewise: dict[str, Any] = {}  # conv_id -> linopy.PiecewiseFormulation

build

build() -> None

Build all variables, constraints, and the objective.

Source code in src/fluxopt/model.py
def build(self) -> None:
    """Build all variables, constraints, and the objective."""
    # Phase 1: Decision variables
    self._create_flow_variables()
    self._create_sizing_variables()
    self._create_investment_variables()
    self._create_status_variables()
    self._create_component_status_variables()
    # Phase 2: Flow rate constraints
    self._constrain_flow_rates_plain()
    self._constrain_flow_rates_sizing()
    self._constrain_flow_rates_status()
    self._constrain_flow_rates_component_status()
    # Phase 3: Feature constraints
    self._constrain_sizing()
    self._constrain_investment()
    self._constrain_status()
    self._constrain_component_status()
    # Phase 4: System
    self._create_balance()
    self._create_converter_constraints()
    self._create_piecewise_constraints()
    self._create_storage()
    self._create_effects()
    self._set_objective()
    self._builtin_var_names: frozenset[str] = frozenset(self.m.variables)

optimize

optimize(
    objective_effects: str | list[str],
    customize: Callable[[FlowSystem], None] | None = None,
    *,
    solver: str = 'highs',
    **kwargs: Any,
) -> Result

Build, optionally customize, and solve the model.

Parameters:

Name Type Description Default

objective_effects

str | list[str]

Effect name(s) to minimize. Sum of named effect totals.

required

customize

Callable[[FlowSystem], None] | None

Optional callback to modify the linopy model between build and solve. Receives self; use model.m to add variables/constraints.

None

solver

str

Solver backend name.

'highs'

**kwargs

Any

Passed through to linopy.Model.solve().

{}
Source code in src/fluxopt/model.py
def optimize(
    self,
    objective_effects: str | list[str],
    customize: Callable[[FlowSystem], None] | None = None,
    *,
    solver: str = 'highs',
    **kwargs: Any,
) -> Result:
    """Build, optionally customize, and solve the model.

    Args:
        objective_effects: Effect name(s) to minimize. Sum of named effect totals.
        customize: Optional callback to modify the linopy model between build and solve.
            Receives ``self``; use ``model.m`` to add variables/constraints.
        solver: Solver backend name.
        **kwargs: Passed through to ``linopy.Model.solve()``.
    """
    self._objective_effects = [objective_effects] if isinstance(objective_effects, str) else objective_effects
    self.build()
    if customize is not None:
        customize(self)
    return self.solve(solver_name=solver, **kwargs)

solve

solve(**kwargs: Any) -> Result

Solve the built model and return results.

Thin wrapper around linopy.Model.solve(). Call :meth:build first.

Parameters:

Name Type Description Default

**kwargs

Any

Passed through to linopy.Model.solve().

{}
Source code in src/fluxopt/model.py
def solve(self, **kwargs: Any) -> Result:
    """Solve the built model and return results.

    Thin wrapper around ``linopy.Model.solve()``. Call :meth:`build` first.

    Args:
        **kwargs: Passed through to ``linopy.Model.solve()``.
    """
    self.m.solve(**kwargs)
    return Result.from_model(self)