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)
/tmp/ipykernel_2184/992264737.py:1: DeprecationWarning:
The 'update' 'from_master' and 'remove_link_p_set' parameters are deprecated and do not have any effect. Example networks are always updated and retrieved for the current version.Deprecated in version 0.35 and will be removed in version 1.0.
[3]:
out = network.optimize()
Running HiGHS 1.11.0 (git hash: 364c83a): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-fa9n7ite 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-fa9n7ite
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-491w2cny.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.11.0 (git hash: 364c83a): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-3hdhjp8_ 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-3hdhjp8_
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-ly8bkdka.sol
Now turn on warnings just for optimization module
[6]:
pypsa.optimization.optimize.logger.setLevel(logging.WARNING)
[7]:
out = network.optimize()
Running HiGHS 1.11.0 (git hash: 364c83a): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-ylmvm0id 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-ylmvm0id
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-2y06geer.sol
Now turn on all messages for the PF module
[8]:
pypsa.network.power_flow.logger.setLevel(logging.DEBUG)
[9]:
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.networks.SubNetwork object at 0x7f8134f92850> 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.networks.SubNetwork object at 0x7f813396cc80> 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.networks.SubNetwork object at 0x7f813396ce60> 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.networks.SubNetwork object at 0x7f813382f690> 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.network.power_flow.logger.setLevel(logging.ERROR)
[11]:
out = network.lpf()