{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Time Series Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A first impression for the execution of a time series simulation has already been given by the [example](https://pandapipes.readthedocs.io/en/latest/timeseries/example.html) in the documentation. Here, a new pandapipes network was created. In the following, an already existing net is loaded from a json file. The prerequisites for a successful execution of a time series simulation are:\n", "\n", "- a pandapipes network\n", "- predefined time series for the sinks and sources in the network (e.g. 2 csv files)\n", "\n", "First, the following imports must be carried out for a general time series simulation:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "import pandapower.control as control\n", "from pandapower.timeseries import DFData\n", "from pandapower.timeseries import OutputWriter\n", "from pandapipes.timeseries import run_timeseries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since in this example an already existing network is loaded, the upcoming code must be added:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pandapipes import networks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the pandapipes network can be created/ loaded:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "net = networks.simple_gas_networks.gas_meshed_delta()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the delta STANET network from the chapter [Meshed Networks](https://pandapipes.readthedocs.io/en/latest/networks/meshed/meshed_networks.html) with the medium gas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Furthermore, the network can be prepared. For this purpose the time series for sinks and sources have to be defined. Since there is only one sink and one source, the respective csv file only consists of one column. The number of lines corresponds to the desired number of time steps, in this case 10." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the code" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "profiles_sink = pd.read_csv(os.path.join('files',\n", " 'simple_time_series_example_sink_profiles.csv'),\n", " index_col=0)\n", "profiles_source = pd.read_csv(os.path.join('files',\n", " 'simple_time_series_example_source_profiles.csv'),\n", " index_col=0)\n", "ds_sink = DFData(profiles_sink)\n", "ds_source = DFData(profiles_source)\n", "\n", "const_sink = control.ConstControl(net, element='sink', variable='mdot_kg_per_s',\n", " element_index=net.sink.index.values, data_source=ds_sink,\n", " profile_name=net.sink.index.values.astype(str))\n", "const_source = control.ConstControl(net, element='source', variable='mdot_kg_per_s',\n", " element_index=net.source.index.values,\n", " data_source=ds_source,\n", " profile_name=net.source.index.values.astype(str))\n", "del const_sink.initial_powerflow\n", "const_sink.initial_pipeflow = False\n", "del const_source.initial_powerflow\n", "const_source.initial_pipeflow = False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "the corresponding csv-file is read out, prepared as data sources (ds) for the controllers and the resulting DataFrames are written into the network with the help of the controller [ConstControl](https://pandapower.readthedocs.io/en/v2.2.2/control/controller.html#constcontrol). Then the desired time steps for which the network is to be calculated must be specified. In the present case, these can be defined from 1 to 10 at most, since only 10 time steps are specified for the sink and source. Likewise, for example, a range of 3 to 6 can be specified. Here the time steps from 1 to 10 are defined with" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "time_steps = range(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Furthermore an [OuputWriter](https://pandapower.readthedocs.io/en/v2.2.2/timeseries/output_writer.html) must be created, which is defined as `ow` using" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "log_variables = [('res_junction', 'p_bar'), ('res_pipe', 'v_mean_m_per_s'),\n", " ('res_pipe', 'reynolds'), ('res_pipe', 'lambda'),\n", " ('res_sink', 'mdot_kg_per_s'), ('res_source', 'mdot_kg_per_s'),\n", " ('res_ext_grid', 'mdot_kg_per_s')]\n", "ow = OutputWriter(net, time_steps, output_path=None, log_variables=log_variables)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the output writer the results of the simulation are stored. Finally, the main function can be called to run the time series simulation:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "run_timeseries(net, time_steps, output_writer=ow)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The complete script is shown below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "import pandapower.control as control\n", "from pandapower.timeseries import DFData\n", "from pandapower.timeseries import OutputWriter\n", "from pandapipes.timeseries import run_timeseries\n", "from pandapipes import networks\n", "\n", "# 1. create/ load the pandapipes network\n", "net = networks.simple_gas_networks.gas_meshed_delta()\n", "\n", "# 2. preparing the network for simulation\n", "profiles_sink = pd.read_csv(os.path.join('files',\n", " 'simple_time_series_example_sink_profiles.csv'),\n", " index_col=0)\n", "profiles_source = pd.read_csv(os.path.join('files',\n", " 'simple_time_series_example_source_profiles.csv'),\n", " index_col=0)\n", "ds_sink = DFData(profiles_sink)\n", "ds_source = DFData(profiles_source)\n", "\n", "const_sink = control.ConstControl(net, element='sink', variable='mdot_kg_per_s',\n", " element_index=net.sink.index.values, data_source=ds_sink,\n", " profile_name=net.sink.index.values.astype(str))\n", "const_source = control.ConstControl(net, element='source', variable='mdot_kg_per_s',\n", " element_index=net.source.index.values,\n", " data_source=ds_source,\n", " profile_name=net.source.index.values.astype(str))\n", "del const_sink.initial_powerflow\n", "const_sink.initial_pipeflow = False\n", "del const_source.initial_powerflow\n", "const_source.initial_pipeflow = False\n", "\n", "# 3. define the time steps to be calculated\n", "time_steps = range(10)\n", "\n", "# 4. creating an output writer\n", "log_variables = [('res_junction', 'p_bar'), ('res_pipe', 'v_mean_m_per_s'),\n", " ('res_pipe', 'reynolds'), ('res_pipe', 'lambda'),\n", " ('res_sink', 'mdot_kg_per_s'), ('res_source', 'mdot_kg_per_s'),\n", " ('res_ext_grid', 'mdot_kg_per_s')]\n", "ow = OutputWriter(net, time_steps, output_path=None, log_variables=log_variables)\n", "\n", "# 5. run simulation\n", "run_timeseries(net, time_steps, output_writer=ow)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The results of the simulation can be accessed via the OutputWriter as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"pressure:\")\n", "print(ow.np_results[\"res_junction.p_bar\"])\n", "print(\"mean velocity:\")\n", "print(ow.np_results[\"res_pipe.v_mean_m_per_s\"])\n", "print(\"reynolds number:\")\n", "print(ow.np_results[\"res_pipe.reynolds\"])\n", "print(\"lambda:\")\n", "print(ow.np_results[\"res_pipe.lambda\"])\n", "print(\"mass flow sink:\")\n", "print(ow.np_results[\"res_sink.mdot_kg_per_s\"])\n", "print(\"mass flow source:\")\n", "print(ow.np_results[\"res_source.mdot_kg_per_s\"])\n", "print(\"mass flow ext. grid:\")\n", "print(ow.np_results[\"res_ext_grid.mdot_kg_per_s\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To view the results more clearly, they can be displayed with `matplotlib.pyplot` in a diagram with the time steps as x-axis.The time curve for the mass flow in the External Grid looks as follows:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The figure can be created by the following lines:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "x = time_steps\n", "y = ow.np_results[\"res_ext_grid.mdot_kg_per_s\"]\n", "\n", "plt.xlabel(\"time step\")\n", "plt.ylabel(\"mass flow [kg/s]\")\n", "plt.title(\"External Grid Mass Flow Profile\")\n", "plt.plot(x, y, \"r-o\")\n", "plt.grid()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition, the pressure curves for the individual junctions can be plotted with this code block:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "x = time_steps\n", "y = ow.np_results[\"res_junction.p_bar\"]\n", "\n", "plt.xlabel(\"time step\")\n", "plt.ylabel(\"pressure [bar]\")\n", "plt.title(\"Pressure Profile\")\n", "plt.plot(x, y[:,0], \"g-o\")\n", "plt.plot(x, y[:,1], \"r-o\")\n", "plt.plot(x, y[:,2], \"y-o\")\n", "plt.legend([\"Source\", \"External Grid\", \"Sink\"], loc='lower left')\n", "plt.grid()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a final example to illustrate the results, the flow velocities of the three pipes are plotted:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this purpose, the following code must be executed:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "x = time_steps\n", "y = ow.np_results[\"res_pipe.v_mean_m_per_s\"]\n", "\n", "plt.xlabel(\"time step\")\n", "plt.ylabel(\"v_mean [m/s]\")\n", "plt.title(\"Velocity Profile\")\n", "plt.plot(x, y[:,0], \"g-o\")\n", "plt.plot(x, y[:,1], \"r-o\")\n", "plt.plot(x, y[:,2], \"y-o\")\n", "plt.legend([\"Pipe 1\", \"Pipe 2\", \"Pipe 3\"], loc='lower left')\n", "plt.grid()\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }