Logging¶
PyPSA uses the Python standard library logging module to provide informative messages during execution.
You can set the logging level to different values like ERROR, WARNING, INFO, or DEBUG. This can be configured independently for different modules.
We'll start by setting the basic logging level to INFO to see informational messages.
In [2]:
Copied!
import logging
import pypsa
logging.basicConfig(level=logging.INFO)
network = pypsa.examples.ac_dc_meshed()
out = network.optimize()
import logging
import pypsa
logging.basicConfig(level=logging.INFO)
network = pypsa.examples.ac_dc_meshed()
out = network.optimize()
INFO:pypsa.network.io:Retrieving network data from https://github.com/PyPSA/PyPSA/raw/v1.0.5/examples/networks/ac-dc-meshed/ac-dc-meshed.nc.
INFO:pypsa.network.io:Imported network 'AC-DC-Meshed' has buses, carriers, generators, global_constraints, lines, links, loads
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow: Index(['2', '3', '4'], dtype='object', name='name')
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow: Index(['0', '1', '5', '6'], dtype='object', name='name')
INFO:linopy.model: Solve problem using Highs solver
INFO:linopy.io: Writing time: 0.04s
INFO:linopy.constants: Optimization successful: Status: ok Termination condition: optimal Solution: 188 primals, 468 duals Objective: -3.47e+06 Solver model: available Solver message: Optimal
INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-ext-p-lower, Generator-ext-p-upper, Line-ext-s-lower, Line-ext-s-upper, Link-ext-p-lower, Link-ext-p-upper, Kirchhoff-Voltage-Law were not assigned to the network.
Running HiGHS 1.11.0 (git hash: 364c83a): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-a_gam0eq has 468 rows; 188 cols; 1007 nonzeros
Coefficient ranges:
Matrix [1e-02, 1e+00]
Cost [9e-03, 3e+03]
Bound [2e+07, 2e+07]
RHS [9e-01, 1e+03]
Presolving model
391 rows, 187 cols, 930 nonzeros 0s
305 rows, 101 cols, 1042 nonzeros 0s
Dependent equations search running on 22 equations with time limit of 1000.00s
Dependent equations search removed 0 rows and 0 nonzeros in 0.00s (limit = 1000.00s)
303 rows, 99 cols, 1058 nonzeros 0s
Presolve : Reductions: rows 303(-165); columns 99(-89); elements 1058(+51)
Solving the presolved LP
Using EKK dual simplex solver - serial
Iteration Objective Infeasibilities num(sum)
0 -2.1204510016e+07 Pr: 102(98953); Du: 0(4.73182e-11) 0s
126 -3.4742560406e+06 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model name : linopy-problem-a_gam0eq
Model status : Optimal
Simplex iterations: 126
Objective value : -3.4742560406e+06
P-D objective error : 1.6083830835e-15
HiGHS run time : 0.00
Writing the solution to /tmp/linopy-solve-6te7g9cl.sol
Now let's turn on INFO level messages specifically for the optimization module to see detailed optimization progress.
In [3]:
Copied!
pypsa.optimization.optimize.logger.setLevel(logging.INFO)
out = network.optimize()
pypsa.optimization.optimize.logger.setLevel(logging.INFO)
out = network.optimize()
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow: Index(['2', '3', '4'], dtype='object', name='name')
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow: Index(['0', '1', '5', '6'], dtype='object', name='name')
INFO:linopy.model: Solve problem using Highs solver
INFO:linopy.io: Writing time: 0.04s
INFO:linopy.constants: Optimization successful: Status: ok Termination condition: optimal Solution: 188 primals, 468 duals Objective: -3.47e+06 Solver model: available Solver message: Optimal
INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-ext-p-lower, Generator-ext-p-upper, Line-ext-s-lower, Line-ext-s-upper, Link-ext-p-lower, Link-ext-p-upper, Kirchhoff-Voltage-Law were not assigned to the network.
Running HiGHS 1.11.0 (git hash: 364c83a): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-ufte6j7i has 468 rows; 188 cols; 1007 nonzeros
Coefficient ranges:
Matrix [1e-02, 1e+00]
Cost [9e-03, 3e+03]
Bound [2e+07, 2e+07]
RHS [9e-01, 1e+03]
Presolving model
391 rows, 187 cols, 930 nonzeros 0s
305 rows, 101 cols, 1042 nonzeros 0s
Dependent equations search running on 22 equations with time limit of 1000.00s
Dependent equations search removed 0 rows and 0 nonzeros in 0.00s (limit = 1000.00s)
303 rows, 99 cols, 1058 nonzeros 0s
Presolve : Reductions: rows 303(-165); columns 99(-89); elements 1058(+51)
Solving the presolved LP
Using EKK dual simplex solver - serial
Iteration Objective Infeasibilities num(sum)
0 -2.1204510016e+07 Pr: 102(98953); Du: 0(4.73182e-11) 0s
126 -3.4742560406e+06 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model name : linopy-problem-ufte6j7i
Model status : Optimal
Simplex iterations: 126
Objective value : -3.4742560406e+06
P-D objective error : 1.6083830835e-15
HiGHS run time : 0.00
Writing the solution to /tmp/linopy-solve-4h54mw9j.sol
Now let's reduce verbosity by setting the optimization module to only show WARNING level messages.
In [4]:
Copied!
pypsa.optimization.optimize.logger.setLevel(logging.WARNING)
out = network.optimize()
pypsa.optimization.optimize.logger.setLevel(logging.WARNING)
out = network.optimize()
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow: Index(['2', '3', '4'], dtype='object', name='name')
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow: Index(['0', '1', '5', '6'], dtype='object', name='name')
INFO:linopy.model: Solve problem using Highs solver
INFO:linopy.io: Writing time: 0.04s
INFO:linopy.constants: Optimization successful: Status: ok Termination condition: optimal Solution: 188 primals, 468 duals Objective: -3.47e+06 Solver model: available Solver message: Optimal
Running HiGHS 1.11.0 (git hash: 364c83a): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-8yii54b2 has 468 rows; 188 cols; 1007 nonzeros
Coefficient ranges:
Matrix [1e-02, 1e+00]
Cost [9e-03, 3e+03]
Bound [2e+07, 2e+07]
RHS [9e-01, 1e+03]
Presolving model
391 rows, 187 cols, 930 nonzeros 0s
305 rows, 101 cols, 1042 nonzeros 0s
Dependent equations search running on 22 equations with time limit of 1000.00s
Dependent equations search removed 0 rows and 0 nonzeros in 0.00s (limit = 1000.00s)
303 rows, 99 cols, 1058 nonzeros 0s
Presolve : Reductions: rows 303(-165); columns 99(-89); elements 1058(+51)
Solving the presolved LP
Using EKK dual simplex solver - serial
Iteration Objective Infeasibilities num(sum)
0 -2.1204510016e+07 Pr: 102(98953); Du: 0(4.73182e-11) 0s
126 -3.4742560406e+06 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model name : linopy-problem-8yii54b2
Model status : Optimal
Simplex iterations: 126
Objective value : -3.4742560406e+06
P-D objective error : 1.6083830835e-15
HiGHS run time : 0.00
Writing the solution to /tmp/linopy-solve-odaiuayc.sol
Now let's turn on DEBUG level messages for the power flow module to see the most detailed output.
In [5]:
Copied!
pypsa.network.power_flow.logger.setLevel(logging.DEBUG)
out = network.lpf()
pypsa.network.power_flow.logger.setLevel(logging.DEBUG)
out = network.lpf()
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 0 is Manchester
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 1 is Norwich DC
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 2 is Frankfurt
DEBUG:pypsa.network.power_flow:No slack generator found in sub-network 3, using Norway Wind as the slack generator
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 3 is Norway
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 0 is Manchester
INFO:pypsa.network.power_flow:Performing linear load-flow on AC sub-network <pypsa.SubNetwork object at 0x7ac48233fa50> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
'2015-01-01 02:00:00', '2015-01-01 03:00:00',
'2015-01-01 04:00:00', '2015-01-01 05:00:00',
'2015-01-01 06:00:00', '2015-01-01 07:00:00',
'2015-01-01 08:00:00', '2015-01-01 09:00:00'],
dtype='datetime64[ns]', name='snapshot', freq=None)
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 1 is Norwich DC
INFO:pypsa.network.power_flow:Performing linear load-flow on DC sub-network <pypsa.SubNetwork object at 0x7ac4701afc50> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
'2015-01-01 02:00:00', '2015-01-01 03:00:00',
'2015-01-01 04:00:00', '2015-01-01 05:00:00',
'2015-01-01 06:00:00', '2015-01-01 07:00:00',
'2015-01-01 08:00:00', '2015-01-01 09:00:00'],
dtype='datetime64[ns]', name='snapshot', freq=None)
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 2 is Frankfurt
INFO:pypsa.network.power_flow:Performing linear load-flow on AC sub-network <pypsa.SubNetwork object at 0x7ac48f6bc320> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
'2015-01-01 02:00:00', '2015-01-01 03:00:00',
'2015-01-01 04:00:00', '2015-01-01 05:00:00',
'2015-01-01 06:00:00', '2015-01-01 07:00:00',
'2015-01-01 08:00:00', '2015-01-01 09:00:00'],
dtype='datetime64[ns]', name='snapshot', freq=None)
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 3 is Norway
INFO:pypsa.network.power_flow:Performing linear load-flow on AC sub-network <pypsa.SubNetwork object at 0x7ac48f76f4d0> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
'2015-01-01 02:00:00', '2015-01-01 03:00:00',
'2015-01-01 04:00:00', '2015-01-01 05:00:00',
'2015-01-01 06:00:00', '2015-01-01 07:00:00',
'2015-01-01 08:00:00', '2015-01-01 09:00:00'],
dtype='datetime64[ns]', name='snapshot', freq=None)
Finally, let's turn off messages for the power flow module by setting it back to ERROR level.
In [6]:
Copied!
pypsa.network.power_flow.logger.setLevel(logging.ERROR)
out = network.lpf()
pypsa.network.power_flow.logger.setLevel(logging.ERROR)
out = network.lpf()
In [8]:
Copied!
# Access a deprecated feature to see a warning
network.components.generators.component_names
# Access a deprecated feature to see a warning
network.components.generators.component_names
/tmp/ipykernel_5893/1732202595.py:2: DeprecationWarning: c.component_names is deprecated, use c.names instead
Out[8]:
Index(['Manchester Wind', 'Manchester Gas', 'Norway Wind', 'Norway Gas',
'Frankfurt Wind', 'Frankfurt Gas'],
dtype='object', name='name')
In [9]:
Copied!
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
network.components.generators.component_names
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
network.components.generators.component_names
Out[9]:
Index(['Manchester Wind', 'Manchester Gas', 'Norway Wind', 'Norway Gas',
'Frankfurt Wind', 'Frankfurt Gas'],
dtype='object', name='name')