Skip to content

Solvers

This module provides functions for running power flow calculations.

run_opf

Run Optimal Power Flow (OPF) calculation using Julia interface.

Parameters:

Name Type Description Default
net Network

A Network object containing the power system model.

required
jl Any

Julia interface object for running OPF.

required

Returns:

Type Description
Dict[str, Any]

OPF result containing termination status and solution data.

Raises:

Type Description
RuntimeError

If OPF fails to converge or encounters an error.

Source code in gridfm_datakit/process/solvers.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def run_opf(net: Network, jl: Any) -> Dict[str, Any]:
    """Run Optimal Power Flow (OPF) calculation using Julia interface.

    Args:
        net: A Network object containing the power system model.
        jl: Julia interface object for running OPF.

    Returns:
        OPF result containing termination status and solution data.

    Raises:
        RuntimeError: If OPF fails to converge or encounters an error.
    """
    try:
        data = _julia_pm_data(net, jl)

        with solver_capture("opf"):
            result = jl.run_opf(data)

        if str(result["termination_status"]) != "LOCALLY_SOLVED":
            raise RuntimeError(f"OPF did not converge: {result['termination_status']}")

        return result

    except Exception as e:
        raise RuntimeError(f"Error running OPF: {e}")

run_pf

Run Power Flow (PF) calculation using Julia interface.

This function runs the power flow calculation using the Julia interface and returns the result with termination status.

Parameters:

Name Type Description Default
net Network

A network object containing the power system model.

required
jl Any

Julia interface object for running power flow.

required
fast Union[bool, None]

If True, use the direct (non-optimizer) computation. If None, defaults to False (uses optimizer-based solver).

None

Returns:

Type Description
Dict[str, Any]

Power flow result containing termination status and solution data.

Raises:

Type Description
RuntimeError

If power flow fails to converge or encounters an error.

Source code in gridfm_datakit/process/solvers.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def run_pf(net: Network, jl: Any, fast: Union[bool, None] = None) -> Dict[str, Any]:
    """Run Power Flow (PF) calculation using Julia interface.

    This function runs the power flow calculation using the Julia interface
    and returns the result with termination status.

    Args:
        net: A network object containing the power system model.
        jl: Julia interface object for running power flow.
        fast: If True, use the direct (non-optimizer) computation. If None, defaults to False (uses optimizer-based solver).

    Returns:
        Power flow result containing termination status and solution data.

    Raises:
        RuntimeError: If power flow fails to converge or encounters an error.
    """
    try:
        data = _julia_pm_data(net, jl)

        # Run PF
        with solver_capture("pf"):
            result = jl.run_pf_fast_data(data) if fast else jl.run_pf_data(data)
        if (
            fast
            and str(result["termination_status"]) != "True"
            or (not fast and str(result["termination_status"]) != "LOCALLY_SOLVED")
        ):
            raise RuntimeError(
                f"PF did not converge: {result['termination_status']}, fast={fast}",
            )

        return result

    except Exception as e:
        raise RuntimeError(f"Error running PF: {e}")

run_dcpf

Run DC Power Flow (DCPF) calculation using Julia interface.

This function runs the DC power flow calculation using the Julia interface and returns the result with termination status.

Parameters:

Name Type Description Default
net Network

A network object containing the power system model.

required
jl Any

Julia interface object for running DC power flow.

required
fast Union[bool, None]

If True, use the direct (non-optimizer) computation. If None, defaults to False (uses optimizer-based solver).

None

Returns:

Type Description
Dict[str, Any]

DC power flow result containing termination status and solution data.

Raises:

Type Description
RuntimeError

If DC power flow fails to converge or encounters an error.

Source code in gridfm_datakit/process/solvers.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
def run_dcpf(net: Network, jl: Any, fast: Union[bool, None] = None) -> Dict[str, Any]:
    """Run DC Power Flow (DCPF) calculation using Julia interface.

    This function runs the DC power flow calculation using the Julia interface
    and returns the result with termination status.

    Args:
        net: A network object containing the power system model.
        jl: Julia interface object for running DC power flow.
        fast: If True, use the direct (non-optimizer) computation. If None, defaults to False (uses optimizer-based solver).

    Returns:
        DC power flow result containing termination status and solution data.

    Raises:
        RuntimeError: If DC power flow fails to converge or encounters an error.
    """
    try:
        data = _julia_pm_data(net, jl)

        # Run DCPF (fast or standard)
        with solver_capture("dcpf"):
            result = jl.run_dcpf_fast_data(data) if fast else jl.run_dcpf_data(data)

        if (
            fast
            and str(result["termination_status"]) != "True"
            or (not fast and str(result["termination_status"]) != "LOCALLY_SOLVED")
        ):
            raise RuntimeError(
                f"DC PF did not converge: {result['termination_status']}, fast={fast}",
            )

        return result

    except Exception as e:
        raise RuntimeError(f"Error running DC PF: {e}")