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 pypsa, os
import logging

logging.basicConfig(level=logging.ERROR)
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/v0.24.0/lib/python3.11/site-packages/pypsa/networkclustering.py:16: UserWarning: The namespace `pypsa.networkclustering` is deprecated and will be removed in PyPSA v0.24. Please use `pypsa.clustering.spatial instead`.
  warnings.warn(
[2]:
network = pypsa.examples.ac_dc_meshed(from_master=True)
[3]:
out = network.optimize()
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
 --lp /tmp/linopy-problem-2z4dccf4.lp --output /tmp/linopy-solve-46j6rg6a.sol
Reading problem data from '/tmp/linopy-problem-2z4dccf4.lp'...
468 rows, 188 columns, 1007 non-zeros
2678 lines were read
GLPK Simplex Optimizer 5.0
468 rows, 188 columns, 1007 non-zeros
Preprocessing...
391 rows, 187 columns, 930 non-zeros
Scaling...
 A: min|aij| =  9.693e-03  max|aij| =  1.215e+00  ratio =  1.254e+02
GM: min|aij| =  5.786e-01  max|aij| =  1.728e+00  ratio =  2.987e+00
EQ: min|aij| =  3.377e-01  max|aij| =  1.000e+00  ratio =  2.961e+00
Constructing initial basis...
Size of triangular part is 391
      0: obj =  -2.104321118e+07 inf =   9.486e+04 (101)
    162: obj =   1.210828068e+07 inf =   1.864e-11 (0) 1
*   239: obj =  -3.474256041e+06 inf =   1.755e-12 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.6 Mb (656771 bytes)
Writing basic solution to '/tmp/linopy-solve-46j6rg6a.sol'...

Now turn on infos just for OPF module.

[4]:
pypsa.opf.logger.setLevel(logging.INFO)
[5]:
out = network.optimize()
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
 --lp /tmp/linopy-problem-hqvkgvg_.lp --output /tmp/linopy-solve-hcp2iplq.sol
Reading problem data from '/tmp/linopy-problem-hqvkgvg_.lp'...
468 rows, 188 columns, 1007 non-zeros
2678 lines were read
GLPK Simplex Optimizer 5.0
468 rows, 188 columns, 1007 non-zeros
Preprocessing...
391 rows, 187 columns, 930 non-zeros
Scaling...
 A: min|aij| =  9.693e-03  max|aij| =  1.215e+00  ratio =  1.254e+02
GM: min|aij| =  5.786e-01  max|aij| =  1.728e+00  ratio =  2.987e+00
EQ: min|aij| =  3.377e-01  max|aij| =  1.000e+00  ratio =  2.961e+00
Constructing initial basis...
Size of triangular part is 391
      0: obj =  -2.104321118e+07 inf =   9.486e+04 (101)
    162: obj =   1.210828068e+07 inf =   1.864e-11 (0) 1
*   239: obj =  -3.474256041e+06 inf =   1.755e-12 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.6 Mb (656771 bytes)
Writing basic solution to '/tmp/linopy-solve-hcp2iplq.sol'...

Now turn on warnings just for OPF module

[6]:
pypsa.opf.logger.setLevel(logging.WARNING)
[7]:
out = network.optimize()
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
 --lp /tmp/linopy-problem-r0l1bvtq.lp --output /tmp/linopy-solve-cuij5wys.sol
Reading problem data from '/tmp/linopy-problem-r0l1bvtq.lp'...
468 rows, 188 columns, 1007 non-zeros
2678 lines were read
GLPK Simplex Optimizer 5.0
468 rows, 188 columns, 1007 non-zeros
Preprocessing...
391 rows, 187 columns, 930 non-zeros
Scaling...
 A: min|aij| =  9.693e-03  max|aij| =  1.215e+00  ratio =  1.254e+02
GM: min|aij| =  5.786e-01  max|aij| =  1.728e+00  ratio =  2.987e+00
EQ: min|aij| =  3.377e-01  max|aij| =  1.000e+00  ratio =  2.961e+00
Constructing initial basis...
Size of triangular part is 391
      0: obj =  -2.104321118e+07 inf =   9.486e+04 (101)
    162: obj =   1.210828068e+07 inf =   1.864e-11 (0) 1
*   239: obj =  -3.474256041e+06 inf =   1.755e-12 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.6 Mb (656771 bytes)
Writing basic solution to '/tmp/linopy-solve-cuij5wys.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 SubNetwork 0 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 SubNetwork 1 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 SubNetwork 2 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 SubNetwork 3 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()