Note
You can download this example as a Jupyter notebook or start it in interactive mode.
Demonstrate usage of logging#
PyPSA uses the Python standard library logging. This notebook shows how to use it and control the logging messages from different modules.
One can set the logging level to different values like ERROR, WARNING, INFO, DEBUG. This works independently for separate module.
We start by setting the basic logging level to ERROR.
[1]:
import logging
import pypsa
logging.basicConfig(level=logging.ERROR)
[2]:
network = pypsa.examples.ac_dc_meshed(from_master=True)
[3]:
out = network.optimize()
Running HiGHS 1.10.0 (git hash: fd86653): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-shww2r1j 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-shww2r1j
Model status : Optimal
Simplex iterations: 126
Objective value : -3.4742560406e+06
Relative P-D gap : 1.0722555433e-15
HiGHS run time : 0.00
Writing the solution to /tmp/linopy-solve-9gq_tvqh.sol
Now turn on infos just for optimization module.
[4]:
pypsa.optimization.optimize.logger.setLevel(logging.INFO)
[5]:
out = network.optimize()
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.10.0 (git hash: fd86653): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-ww9foufj 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-ww9foufj
Model status : Optimal
Simplex iterations: 126
Objective value : -3.4742560406e+06
Relative P-D gap : 1.0722555433e-15
HiGHS run time : 0.00
Writing the solution to /tmp/linopy-solve-smgxg6q5.sol
Now turn on warnings just for optimization module
[6]:
pypsa.optimization.optimize.logger.setLevel(logging.WARNING)
[7]:
out = network.optimize()
Running HiGHS 1.10.0 (git hash: fd86653): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-6neuvu59 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-6neuvu59
Model status : Optimal
Simplex iterations: 126
Objective value : -3.4742560406e+06
Relative P-D gap : 1.0722555433e-15
HiGHS run time : 0.00
Writing the solution to /tmp/linopy-solve-it282kk0.sol
Now turn on all messages for the PF module
[8]:
pypsa.pf.logger.setLevel(logging.DEBUG)
[9]:
out = network.lpf()
DEBUG:pypsa.pf:Slack bus for sub-network 0 is Manchester
DEBUG:pypsa.pf:Slack bus for sub-network 1 is Norwich DC
DEBUG:pypsa.pf:Slack bus for sub-network 2 is Frankfurt
DEBUG:pypsa.pf:No slack generator found in sub-network 3, using Norway Wind as the slack generator
DEBUG:pypsa.pf:Slack bus for sub-network 3 is Norway
DEBUG:pypsa.pf:Slack bus for sub-network 0 is Manchester
INFO:pypsa.pf:Performing linear load-flow on AC sub-network <pypsa.networks.SubNetwork object at 0x7e0da1ac4250> 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.pf:Slack bus for sub-network 1 is Norwich DC
INFO:pypsa.pf:Performing linear load-flow on DC sub-network <pypsa.networks.SubNetwork object at 0x7e0da1b6a8a0> 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.pf:Slack bus for sub-network 2 is Frankfurt
INFO:pypsa.pf:Performing linear load-flow on AC sub-network <pypsa.networks.SubNetwork object at 0x7e0da1b6b3e0> 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.pf:Slack bus for sub-network 3 is Norway
INFO:pypsa.pf:Performing linear load-flow on AC sub-network <pypsa.networks.SubNetwork object at 0x7e0da1bf33f0> 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)
Now turn off all messages for the PF module again
[10]:
pypsa.pf.logger.setLevel(logging.ERROR)
[11]:
out = network.lpf()