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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.
    """
    # Create a temporary file for the MATPOWER case
    with tempfile.NamedTemporaryFile(mode="w", suffix=".m", delete=False) as temp_file:
        temp_filename = temp_file.name

    try:
        # Save network to temporary file
        net.to_mpc(temp_filename)

        result = jl.run_opf(temp_filename)

        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}")
    finally:
        # Clean up temporary file
        if os.path.exists(temp_filename):
            os.unlink(temp_filename)

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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.
    """

    # Create a temporary file for the MATPOWER case
    with tempfile.NamedTemporaryFile(mode="w", suffix=".m", delete=False) as temp_file:
        temp_filename = temp_file.name

    try:
        # Save network to temporary file
        net.to_mpc(temp_filename)

        # Run PF
        result = jl.run_pf_fast(temp_filename) if fast else jl.run_pf(temp_filename)
        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}")
    finally:
        # Clean up temporary file
        if os.path.exists(temp_filename):
            os.unlink(temp_filename)

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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.
    """

    # Create a temporary file for the MATPOWER case
    with tempfile.NamedTemporaryFile(mode="w", suffix=".m", delete=False) as temp_file:
        temp_filename = temp_file.name

    try:
        # Save network to temporary file
        net.to_mpc(temp_filename)

        # Run DCPF (fast or standard)
        result = jl.run_dcpf_fast(temp_filename) if fast else jl.run_dcpf(temp_filename)

        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}")
    finally:
        # Clean up temporary file
        if os.path.exists(temp_filename):
            os.unlink(temp_filename)