Skip to main content

from django_oemof.tests.test_oemof_parameters import OEMOF_DATAPACKAGE

Django-Oemof

Django-Oemof is a Django app to provide an API to build and optimize oemof.solph models and deliver results via JSON response.

Requirements

  • oemof.tabular has to be installed
  • CBC solver has to be installed. Install it via (conda):
conda install -c conda-forge coincbc

Django project must use celery and automatically detect celery tasks. (follow https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html to setup celery)

Quick start

  1. Add "oemof" to your INSTALLED_APPS setting like this::

        INSTALLED_APPS = [
            ...
            'rest_framework'
            'django_oemof',
        ]
    
  2. Include the oemof URLconf in your project urls.py like this::

    path('oemof/', include('django_oemof.urls')),

  3. Run python manage.py migrate to create the oemof models.

  4. Now you can call /oemof/simulate?scenario=<oemof_datapackage> in the browser in order to start simulation of an Oemof datapackage. See section OEMOF Datapackages on how to prepare datapackages.

Configuration

You can set following configs via environment:

  • DJANGO_OEMOF_IGNORE_SIMULATION_PARAMETERS list of parameter keys which shall be ignored when initializing a simulation
  • DJANGO_OEMOF_TIMELIMIT timelimit for cbc solver, default is 600s = 10min

OEMOF Datapackages

Have to be put in folder oemof within Django's MEDIA_ROOT folder. Name of datapackage folder is used in request for building ES. Once simulation of a scenario is started, the related datapackage will be build using oemof.tabular's Energysystem.from_datapackage(). Afterwards, components and constraints of resulting oemof.solph.Energysystem and oemof.solph.Model can be adapted/added/deleted using so-called hooks.

Hooks

Hooks can be used to adapt energysystem and model of the oemof simulation. This is done by defining custom functions which can be registered in django_oemof and are applied at certain stages while building the ES and optimizing the resulting model. See the following flow chart for the order of hooks:

Hook Flow Chart

Every hook is scenario-dependent to allow different hooks per scenario, but you can use hooks.ALL_SCENARIO as the scenario key to apply the hook to all scenarios.

The different hooks are explained in the following:

  1. Setup Hook

    Gets executed before simulation is started. After all related setup hooks have been run, the resulting parameter set will be used to start/lookup simulation. Thus, the setup hook can modify the parameters which will be stored together with simulation results in the database. As these parameters must be JSON-able, no complex parameters (like pandas.DataFrame) can be used here. Normally the setup hook is used to handle request data, especially to clean user input using forms.

  2. Parameter Hook

    This hook gets executed right after building ES from datapackage. As a special hook, the resulting parameters dictionary is used to adapt component attributes within the ES. The first key is used to identify the ES component. The value, which consists of another dictionary, is used to adapt attributes of the related component (i.e. regarding the dispatch example in oemof.tabular, the dictionary {"demand1": {"amount": 2000}} would increase the load to value 2000 (instead of 1000)). This hook can also adapt profiles. To do so, a profile given as pandas.Series must be set as value instead (i.e. {"demand0": {"profile": pandas.Series(...)}}).

    Note: Changing variable expandable might cause problems as Investment might already be initialized in oemof (this is the case for storages). To avoid this, you can try to set "investment": None for the related component instead or additionally (i.e. {"battery": {"capacity": 50, "storage_capacity": 100, "investment": None})

  3. Energysystem Hook

    This hook gets executed after adapting ES from parameter hooks. The underlying function gets access to the oemof.solph.Energysystem and can adapt, add or delete components directly within the ES.

  4. Model Hook

    This hook gets executed after building the optimization model from ES. By this hook, constraints of the model can be adapted, added or deleted.

  5. Postprocessing Hook

    This hook gets executed after model is solved and is intended to change meta results or add additional meta results from model. Hook is parametrized with meta_results as data and model as additional_data.

An example hook (from digiplan, adapting electricity demand) could be set up as follows:

from django_oemof import hooks


def adapt_electricity_demand(scenario: str, data: dict, additional_data: Any) -> dict:  # noqa: ARG001
    for sector, slider in (("hh", "s_v_3"), ("cts", "s_v_4"), ("ind", "s_v_5")):
        demand = datapackage.get_power_demand(sector)[sector]
        logging.info(f"Adapting electricity demand at {sector=}.")
        data[f"ABW-electricity-demand_{sector}"] = {"amount": float(demand["2022"].sum()) * data.pop(slider) / 100}
    return data


electricity_demand_hook = hooks.Hook(scenario="dispatch", function=adapt_electricity_demand)
hooks.register_hook(hooks.HookType.PARAMETER, electricity_demand_hook)

Visualization

You can get a sankey from result flows by visiting /oemof/flows?simulation_id=<simuulation_id>.

Note: Unfortunately, all storage components are removed (currently hardcoded), as eChart sankeys do not allow cyclic flows.

This will give you a sankey like this: Sankey made from oemof results

Tests

Run tests for standalone app via python runtests.py

Standalone

This section is about using django-oemof without necessity to set up a django webserver. You can store/restore simulated oemof.tabular datapackages using djangos ORM. Additionally, hooks from django-oemof to

  • change parameters before simulation,
  • change ES after building from datapackage or
  • changing model before simulating

are available.

Usage

Steps to run simulation:

  1. Install this package via pip install django-oemof or locally via pip install .
  2. Additionally, django and oemof.tabular (chose your version freely) must be installed manually
  3. Set up database url as DATABASE_URL in .env file in working directory
  4. Migrate django models via python -m django_oemof.standalone migrate
  5. Download or create a valid oemof.tabular datapackage and store it in folder media/oemof (Media folder can be changed via MEDIA_ROOT in .env file)
  6. Create script which imports init_django from django_oemof.standalone
  7. Now, you can save/restore oemof results to/from DB using:
# Example with some hooks
from django_oemof.standalone import init_django
init_django()
from django_oemof import simulation, hooks

OEMOF_DATAPACKAGE = "dispatch"

# Hook functions must be defined beforehand
ph = hooks.Hook(OEMOF_DATAPACKAGE, test_parameter_hook)
esh = hooks.Hook(OEMOF_DATAPACKAGE, test_es_hook)
mh = hooks.Hook(OEMOF_DATAPACKAGE, test_model_hook)

hooks.register_hook(hook_type=hooks.HookType.PARAMETER, hook=ph)
hooks.register_hook(hook_type=hooks.HookType.ENERGYSYSTEM, hook=esh)
hooks.register_hook(hook_type=hooks.HookType.MODEL, hook=mh)

parameters = {}
simulation_id = simulation.simulate_scenario(scenario=OEMOF_DATAPACKAGE, parameters=parameters)
print("Simulation ID:", simulation_id)

# Restore oemof results from DB
from django_oemof import models
sim = models.Simulation.objects.get(id=simulation_id)
inputs, outputs = sim.dataset.restore_results()

Note: django_oemof.models must be loaded AFTER init_django() call. Thus, import of django.models might look unusual and linter might complain - but otherwise django models are not ready yet and a django error will occur!

Metadata

Release files for django-oemof 2.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-oemof 2.2.2
File Size Uploaded Provenance Actions
django_oemof-2.2.2.tar.gz 425.7 kB Without provenance Details for django_oemof-2.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-oemof 2.2.2
File Interpreter ABI Platform Actions
django_oemof-2.2.2-py3-none-any.whl Python 3 none any Details for django_oemof-2.2.2-py3-none-any.whl

Total release size: 898.1 kB

Release files / django_oemof-2.2.2.tar.gz

Download URL django_oemof-2.2.2.tar.gz
Size 425.7 kB
Tags Source
SHA-256 checksum
How to use checksums
96c0c837437cb95ed549cc790a5bf3c03df1c8b8845a81c8c4466214088a8a2a
BLAKE2b-256 checksum
How to use checksums
1b19faebba01e44fb5d90d0746b7fd680296c033ef582552b89c26e458482360
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-101-generic

Release files / django_oemof-2.2.2-py3-none-any.whl

Download URL django_oemof-2.2.2-py3-none-any.whl
Size 472.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c5f6bead04582f1b324e1db0fa4123690e7a7adb07d8968d67f8a8c34090a9e
BLAKE2b-256 checksum
How to use checksums
6906b56288b38554843353a165e2854a5666bfa5278720c43c18dd0c34516599
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-101-generic

Security for django-oemof 2.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page