{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Uncertainties Example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import display, HTML\n", "display(HTML('\"Open'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example demonstrates how apply uncertainties to model parameters such as demand patterns." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install epyt-flow --quiet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from epyt_flow.data.networks import load_ltown\n", "from epyt_flow.simulation import ScenarioSimulator, ModelUncertainty, \\\n", " RelativeUniformUncertainty\n", "from epyt_flow.utils import to_seconds, plot_timeseries_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the [L-Town network](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.data.html#epyt_flow.data.networks.load_ltown) with realistic demands:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "network_config = load_ltown(include_default_sensor_placement=True,\n", " use_realistic_demands=True, verbose=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a new [simulation](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.simulation.html#epyt_flow.simulation.scenario_simulator.ScenarioSimulator):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim = ScenarioSimulator(scenario_config=network_config)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set simulation duration to two hours:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.set_general_parameters(simulation_duration=to_seconds(hours=2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add [uncertainty](https://epyt-flow.readthedocs.io/en/stable/tut.uncertainty.html) (i.e. randomness) with respect to the demand pattern -- i.e. demand pattern values can deviate up to 25% from their original value.\n", "Consequently, the simulation is no longer deterministic and the results vary from run to run.\n", "\n", "Here, we use [RelativeUniformUncertainty](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.uncertainty.html#epyt_flow.uncertainty.uncertainties.RelativeUniformUncertainty) for the demand pattern uncertainty and set it by calling [set_model_uncertainty()](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.simulation.html#epyt_flow.simulation.scenario_simulator.ScenarioSimulator.set_model_uncertainty):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "uc = RelativeUniformUncertainty(low=0.75, high=1.25)\n", "\n", "sim.set_model_uncertainty(ModelUncertainty(global_demand_pattern_uncertainty=uc))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run simulation three times and retrieve sensor readings at node \"n105\" -- do not forget to set `reapply_uncertainties=True` to get reset the uncertainties before each simulation run:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "measurements = []\n", "for _ in range(3):\n", " scada_data = sim.run_simulation(reapply_uncertainties=True)\n", " measurements.append(scada_data.get_data_pressures(sensor_locations=[\"n105\"]).\n", " flatten().tolist())\n", "\n", "plot_timeseries_data(np.array(measurements),\n", " labels=[f\"Scenario {s_id}\" for s_id in range(len(measurements))],\n", " x_axis_label=\"Time (5min steps)\", y_axis_label=\"Pressure in $m$\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do not forget to close the simulation!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.close()" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 2 }