{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Control 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": [ "[EPyT-Flow](https://github.com/WaterFutures/EPyT-Flow) is available on [PyPI](https://pypi.org/project/epyt-flow/) and can be installed via `pip install epyt-flow`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install epyt-flow --quiet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from epyt_flow.data.networks import load_net1\n", "from epyt_flow.simulation import ScenarioSimulator, EpanetConstants\n", "from epyt_flow.simulation import SimpleControlModule\n", "from epyt_flow.utils import to_seconds\n", "from epyt_flow.simulation.events import ActuatorConstants" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create two simple control modules (i.e. entries in the \"CONTROL\" section of an .inp file) for controlling pump \"9\" based on the water leven in tank \"2\":\n", "\n", "- LINK 9 OPEN IF NODE 2 BELOW 110\n", "- LINK 9 CLOSED IF NODE 2 ABOVE 140\n", "\n", "Note that all simple control modules must be instances of [SimpleControlModule](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.simulation.scada.html#epyt_flow.simulation.scada.simple_control.SimpleControlModule)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# LINK 9 OPEN IF NODE 2 BELOW 110\n", "my_control_1 = SimpleControlModule(link_id=\"9\",\n", " link_status=ActuatorConstants.EN_OPEN,\n", " cond_type=EpanetConstants.EN_LOWLEVEL,\n", " cond_var_value=\"2\",\n", " cond_comp_value=110.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# LINK 9 CLOSED IF NODE 2 ABOVE 140\n", "my_control_2 = SimpleControlModule(link_id=\"9\",\n", " link_status=ActuatorConstants.EN_CLOSED,\n", " cond_type=EpanetConstants.EN_HILEVEL,\n", " cond_var_value=\"2\",\n", " cond_comp_value=140.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create new simulation based on Net1:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim = ScenarioSimulator(scenario_config=load_net1(verbose=False))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set simulation duration to two days:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.set_general_parameters(simulation_duration=to_seconds(days=2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Monitor states of tank \"2\" and pump \"9\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.set_tank_sensors(sensor_locations=[\"2\"])\n", "sim.set_pump_state_sensors(sensor_locations=[\"9\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that Net1.inp contains some simple controls. Remove all of them by calling [remove_all_simple_controls()](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.simulation.html#epyt_flow.simulation.scenario_simulator.ScenarioSimulator.remove_all_simple_controls):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.remove_all_simple_controls()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add our simple control modules by calling [add_simple_control()](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.simulation.html#epyt_flow.simulation.scenario_simulator.ScenarioSimulator.add_simple_control):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.add_simple_control(my_control_1)\n", "sim.add_simple_control(my_control_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the simulation and show sensor readings over time:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scada_data = sim.run_simulation()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scada_data.plot_pumps_state()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scada_data.plot_tanks_water_volume()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do not forget to close the simulation!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.close()" ] } ], "metadata": { "kernelspec": { "display_name": "epytflow2", "language": "python", "name": "python3" }, "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.20" } }, "nbformat": 4, "nbformat_minor": 2 }