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)
[2]:
network = pypsa.examples.ac_dc_meshed(from_master=True)
[3]:
out = network.lopf()

Now turn on infos just for OPF module.

[4]:
pypsa.opf.logger.setLevel(logging.INFO)
[5]:
out = network.lopf()
INFO:pypsa.opf:Performed preliminary steps
INFO:pypsa.opf:Building pyomo model using `kirchhoff` formulation
INFO:pypsa.opf:Solving model using glpk
INFO:pypsa.opf:Optimization successful
# ==========================================================
# = Solver Results                                         =
# ==========================================================
# ----------------------------------------------------------
#   Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
  Lower bound: -3474256.04054994
  Upper bound: -3474256.04054994
  Number of objectives: 1
  Number of constraints: 452
  Number of variables: 188
  Number of nonzeros: 991
  Sense: minimize
# ----------------------------------------------------------
#   Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
  Termination condition: optimal
  Statistics:
    Branch and bound:
      Number of bounded subproblems: 0
      Number of created subproblems: 0
  Error rc: 0
  Time: 0.010411739349365234
# ----------------------------------------------------------
#   Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
  number of solutions displayed: 0

Now turn on warnings just for OPF module

[6]:
pypsa.opf.logger.setLevel(logging.WARNING)
[7]:
out = network.lopf()

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: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()