From b1696f696cb87c162b0b7e292a3284745417439d Mon Sep 17 00:00:00 2001 From: Brantegger Georg Date: Thu, 7 Jul 2022 14:33:16 +0200 Subject: [PATCH] small formatting and labeling changes --- .gitignore | 1 + Main_Programm.ipynb | 65 ++++++++++++------------ Main_Programm_demo.ipynb | 107 +++++++++++++++++++++------------------ 3 files changed, 91 insertions(+), 82 deletions(-) diff --git a/.gitignore b/.gitignore index c4e6357..9fc0ff3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *__pycache__/ .vscode/settings.json *.pyc +Messing Around/ \ No newline at end of file diff --git a/Main_Programm.ipynb b/Main_Programm.ipynb index 9e54cdf..d64c4a0 100644 --- a/Main_Programm.ipynb +++ b/Main_Programm.ipynb @@ -23,39 +23,38 @@ "#define constants\n", "\n", "# physics\n", - "g = 9.81 # gravitational acceleration [m/s²]\n", - "rho = 1000. # density of water [kg/m³]\n", + "g = 9.81 # gravitational acceleration [m/s²]\n", + "rho = 1000. # density of water [kg/m³]\n", "\n", "# pipeline\n", - "L = 1000. # length of pipeline [m]\n", - "D = 1. # pipe diameter [m]\n", - "A_pipe = D**2/4*np.pi # pipeline area\n", - "h_pipe = 200 # hydraulic head without reservoir [m] \n", - "alpha = np.arcsin(h_pipe/L) # Höhenwinkel der Druckrohrleitung \n", - "n = 10 # number of pipe segments in discretization\n", + "L = 1000. # length of pipeline [m]\n", + "D = 1. # pipe diameter [m]\n", + "A_pipe = D**2/4*np.pi # pipeline area\n", + "h_pipe = 200 # hydraulic head without reservoir [m] \n", + "alpha = np.arcsin(h_pipe/L) # Höhenwinkel der Druckrohrleitung \n", + "n = 10 # number of pipe segments in discretization\n", "# consider replacing Q0 with a vector be be more flexible in initial conditions\n", - "Q0 = 2. # initial flow in whole pipe [m³/s]\n", - "v0 = Q0/A_pipe # initial flow velocity [m/s]\n", - "f_D = 0.1 # Darcy friction factor\n", - "c = 400. # propagation velocity of the pressure wave [m/s]\n", + "Q0 = 2. # initial flow in whole pipe [m³/s]\n", + "v0 = Q0/A_pipe # initial flow velocity [m/s]\n", + "f_D = 0.1 # Darcy friction factor\n", + "c = 400. # propagation velocity of the pressure wave [m/s]\n", "# consider prescribing a total simulation time and deducting the number of timesteps from that\n", - "nt = 100 # number of time steps after initial conditions\n", + "nt = 100 # number of time steps after initial conditions\n", "\n", "# derivatives of the pipeline constants\n", - "dx = L/n # length of each pipe segment\n", - "dt = dx/c # timestep according to method of characterisitics\n", - "nn = n+1 # number of nodes\n", - "h_res = 20. # water level in upstream reservoir [m]\n", - "p0 = rho*g*h_res-v0**2*rho/2\n", - "pl_vec = np.arange(0,nn*dx,dx) # pl = pipe-length. position of the nodes on the pipeline\n", - "t_vec = np.arange(0,nt*dt,dt) # time vector\n", - "h_vec = np.arange(0,n+1)*h_pipe/n # hydraulic head of pipeline at each node np.arange(0,0) does not yield the intended result\n", - "v_init = np.full(nn,Q0/(D**2/4*np.pi)) # initial velocity distribution in pipeline\n", - "p_init = (rho*g*(h_res+h_vec)-v_init**2*rho/2)-(f_D*pl_vec/D*rho/2*v_init**2) # ref Wikipedia: Darcy Weisbach\n", + "dx = L/n # length of each pipe segment\n", + "dt = dx/c # timestep according to method of characterisitics\n", + "nn = n+1 # number of nodes\n", + "initial_level = 20. # water level in upstream reservoir [m]\n", + "p0 = rho*g*initial_level-v0**2*rho/2\n", + "pl_vec = np.arange(0,nn*dx,dx) # pl = pipe-length. position of the nodes on the pipeline\n", + "t_vec = np.arange(0,nt+1)*dt # time vector\n", + "h_vec = np.arange(0,n+1)*h_pipe/n # hydraulic head of pipeline at each node \n", + "v_init = np.full(nn,Q0/(D**2/4*np.pi)) # initial velocity distribution in pipeline\n", + "p_init = (rho*g*(initial_level+h_vec)-v_init**2*rho/2)-(f_D*pl_vec/D*rho/2*v_init**2) # ref Wikipedia: Darcy Weisbach\n", "\n", "\n", "# reservoir\n", - "initial_level = h_res # water level in upstream reservoir [m]\n", "# replace influx by vector\n", "initial_influx = 0. # initial influx of volume to the reservoir [m³/s]\n", "initial_outflux = Q0 # initial outflux of volume from the reservoir to the pipeline [m³/s]\n", @@ -137,16 +136,16 @@ "p_boundary_tur = np.empty_like(t_vec)\n", "\n", "# prepare the vectors that store the temporal evolution of the level in the reservoir\n", - "level_vec = np.full_like(t_vec,initial_level) # level at the end of each pipeline timestep\n", + "level_vec = np.full(nt+1,initial_level) # level at the end of each pipeline timestep\n", "level_vec_2 = np.empty([nt_eRK4]) # level throughout each reservoir timestep-used for plotting and overwritten afterwards\n", "\n", "# set the boudary conditions for the first timestep\n", - "v_boundary_res[0] = v_old[0]\n", - "v_boundary_tur[0] = v_old[-1] \n", - "v_boundary_tur[1:] = 0 # instantaneous closing\n", + "v_boundary_res[0] = v_old[0]\n", + "v_boundary_tur[0] = v_old[-1] \n", + "v_boundary_tur[1:] = 0 # instantaneous closing\n", "# v_boundary_tur[0:20] = np.linspace(v_old[-1],0,20) # overwrite for finite closing time - linear case\n", "const = int(np.min([100,round(nt/1.1)]))\n", - "v_boundary_tur[0:const] = v_old[1]*np.cos(t_vec[0:const]*2*np.pi/5)**2\n", + "v_boundary_tur[0:const] = v_old[1]*np.cos(t_vec[0:const]*2*np.pi/5)**2\n", "p_boundary_res[0] = p_old[0]\n", "p_boundary_tur[0] = p_old[-1]\n", "\n" @@ -163,6 +162,7 @@ "\n", "# create a figure and subplots to display the velocity and pressure distribution across the pipeline in each pipeline step\n", "fig1,axs1 = plt.subplots(2,1)\n", + "fig1.suptitle(str(0) +' s / '+str(round(t_vec[-1],2)) + ' s' )\n", "axs1[0].set_title('Pressure distribution in pipeline')\n", "axs1[1].set_title('Velocity distribution in pipeline')\n", "axs1[0].set_xlabel(r'$x$ [$\\mathrm{m}$]')\n", @@ -180,11 +180,11 @@ "# lo_02, = axs1[2].plot(level_vec_2)\n", "# axs1[2].autoscale()\n", "fig1.tight_layout()\n", - "plt.show()\n", + "fig1.show()\n", "plt.pause(1)\n", "\n", "# loop through time steps of the pipeline\n", - "for it_pipe in range(1,pipe.nt):\n", + "for it_pipe in range(1,pipe.nt+1):\n", "\n", "# for each pipeline timestep, execute nt_eRK4 timesteps of the reservoir code\n", " # set initial conditions for the reservoir time evolution calculted with e-RK4\n", @@ -222,9 +222,10 @@ " lo_00, = axs1[0].plot(pl_vec,pressure_conversion(pipe.p_old,initial_pressure_unit, conversion_pressure_unit)[0],marker='.',c='blue')\n", " lo_01, = axs1[1].plot(pl_vec,pipe.v_old,marker='.',c='blue')\n", " # lo_02, = axs1[2].plot(level_vec_2,c='blue')\n", - " fig1.suptitle(str(it_pipe))\n", + " fig1.suptitle(str(round(t_vec[it_pipe],2))+ ' s / '+str(round(t_vec[-1],2)) + ' s' )\n", " fig1.canvas.draw()\n", " fig1.tight_layout()\n", + " fig1.show()\n", " plt.pause(0.00001) \n", "\n", " # prepare for next loop\n", diff --git a/Main_Programm_demo.ipynb b/Main_Programm_demo.ipynb index ecf05fe..b62e13c 100644 --- a/Main_Programm_demo.ipynb +++ b/Main_Programm_demo.ipynb @@ -2,10 +2,11 @@ "cells": [ { "cell_type": "code", - "execution_count": 8, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ + "# imports\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", @@ -16,60 +17,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# for demoing I\n", "# pipeline\n", - "L = 1000. # length of pipeline [m]\n", - "D = 1. # pipe diameter [m]\n", - "h_pipe = 200 # hydraulic head without reservoir [m] \n", - "Q0 = 2. # initial flow in whole pipe [m³/s]\n", - "f_D = 0.1 # Darcy friction factor\n", - "c = 400. # propagation velocity of the pressure wave [m/s]\n", + "L = 1000. # length of pipeline [m]\n", + "D = 1. # pipe diameter [m]\n", + "h_pipe = 0 # hydraulic head without reservoir [m] \n", + "Q0 = 2. # initial flow in whole pipe [m³/s]\n", + "f_D = 0.05 # Darcy friction factor\n", + "c = 400. # propagation velocity of the pressure wave [m/s]\n", + "n = 100 # number of pipe segments in discretization\n", "\n", + "# consider prescribing a total simulation time and deducting the number of timesteps from that\n", + "nt = 1000 # number of time steps after initial conditions\n", "\n", "# reservoir\n", - "area_base = 20. # total base are of the cuboid reservoir [m²] \n" + "area_base = 1. # total base are of the cuboid reservoir [m²] \n" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#define constants\n", "\n", "# physics\n", - "g = 9.81 # gravitational acceleration [m/s²]\n", - "rho = 1000. # density of water [kg/m³]\n", + "g = 9.81 # gravitational acceleration [m/s²]\n", + "rho = 1000. # density of water [kg/m³]\n", "\n", "\n", - "A_pipe = D**2/4*np.pi # pipeline area\n", - "alpha = np.arcsin(h_pipe/L) # Höhenwinkel der Druckrohrleitung \n", - "n = 10 # number of pipe segments in discretization\n", + "A_pipe = D**2/4*np.pi # pipeline area\n", + "alpha = np.arcsin(h_pipe/L) # Höhenwinkel der Druckrohrleitung \n", "# consider replacing Q0 with a vector be be more flexible in initial conditions\n", - "v0 = Q0/A_pipe # initial flow velocity [m/s]\n", - "# consider prescribing a total simulation time and deducting the number of timesteps from that\n", - "nt = 100 # number of time steps after initial conditions\n", + "v0 = Q0/A_pipe # initial flow velocity [m/s]\n", + "\n", "\n", "# derivatives of the pipeline constants\n", - "dx = L/n # length of each pipe segment\n", - "dt = dx/c # timestep according to method of characterisitics\n", - "nn = n+1 # number of nodes\n", - "h_res = 20. # water level in upstream reservoir [m]\n", - "p0 = rho*g*h_res-v0**2*rho/2\n", - "pl_vec = np.arange(0,nn*dx,dx) # pl = pipe-length. position of the nodes on the pipeline\n", - "t_vec = np.arange(0,nt*dt,dt) # time vector\n", - "h_vec = np.arange(0,n+1)*h_pipe/n # hydraulic head of pipeline at each node np.arange(0,0) does not yield the intended result\n", - "v_init = np.full(nn,Q0/(D**2/4*np.pi)) # initial velocity distribution in pipeline\n", - "p_init = (rho*g*(h_res+h_vec)-v_init**2*rho/2)-(f_D*pl_vec/D*rho/2*v_init**2) # ref Wikipedia: Darcy Weisbach\n", + "dx = L/n # length of each pipe segment\n", + "dt = dx/c # timestep according to method of characterisitics\n", + "nn = n+1 # number of nodes\n", + "initial_level = 20. # water level in upstream reservoir [m]\n", + "p0 = rho*g*initial_level-v0**2*rho/2\n", + "pl_vec = np.arange(0,nn*dx,dx) # pl = pipe-length. position of the nodes on the pipeline\n", + "t_vec = np.arange(0,nt+1)*dt # time vector\n", + "h_vec = np.arange(0,n+1)*h_pipe/n # hydraulic head of pipeline at each node \n", + "v_init = np.full(nn,Q0/A_pipe) # initial velocity distribution in pipeline\n", + "p_init = (rho*g*(initial_level+h_vec)-v_init**2*rho/2)-(f_D*pl_vec/D*rho/2*v_init**2) # ref Wikipedia: Darcy Weisbach\n", "\n", "\n", "# reservoir\n", - "initial_level = h_res # water level in upstream reservoir [m]\n", "# replace influx by vector\n", "initial_influx = 0. # initial influx of volume to the reservoir [m³/s]\n", "initial_outflux = Q0 # initial outflux of volume from the reservoir to the pipeline [m³/s]\n", @@ -88,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -113,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -133,41 +134,43 @@ "p_boundary_tur = np.empty_like(t_vec)\n", "\n", "# prepare the vectors that store the temporal evolution of the level in the reservoir\n", - "level_vec = np.full_like(t_vec,initial_level) # level at the end of each pipeline timestep\n", + "level_vec = np.full(nt+1,initial_level) # level at the end of each pipeline timestep\n", "level_vec_2 = np.empty([nt_eRK4]) # level throughout each reservoir timestep-used for plotting and overwritten afterwards\n", "\n", "# set the boudary conditions for the first timestep\n", - "v_boundary_res[0] = v_old[0]\n", - "p_boundary_res[0] = p_old[0]\n", - "p_boundary_tur[0] = p_old[-1]\n", + "v_boundary_res[0] = v_old[0]\n", + "p_boundary_res[0] = p_old[0]\n", + "p_boundary_tur[0] = p_old[-1]\n", "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# for demoing II\n", - "v_boundary_tur[0] = v_old[-1] \n", - "v_boundary_tur[1:] = 0 # instantaneous closing\n", - "# v_boundary_tur[0:20] = np.linspace(v_old[-1],0,20) # overwrite for finite closing time - linear case\n", - "const = int(np.min([100,round(nt/1.1)]))\n", - "v_boundary_tur[0:const] = v_old[1]*np.cos(t_vec[0:const]*2*np.pi/5)**2" + "v_boundary_tur[0] = v_old[-1] \n", + "v_boundary_tur[1:] = 0 # instantaneous closing\n", + "\n", + "const = int(np.min([1000,round(nt/1.1)])) \n", + "# v_boundary_tur[0:const] = np.linspace(v_old[-1],0,const) # linear closing\n", + "# v_boundary_tur[0:const] = v_old[1]*np.cos(t_vec[0:const]*2*np.pi)**2 # oscillating" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "%matplotlib qt5\n", "# time loop\n", + "%matplotlib qt5\n", "\n", "# create a figure and subplots to display the velocity and pressure distribution across the pipeline in each pipeline step\n", "fig1,axs1 = plt.subplots(2,1)\n", + "fig1.suptitle(str(0) +' s / '+str(round(t_vec[-1],2)) + ' s' )\n", "axs1[0].set_title('Pressure distribution in pipeline')\n", "axs1[1].set_title('Velocity distribution in pipeline')\n", "axs1[0].set_xlabel(r'$x$ [$\\mathrm{m}$]')\n", @@ -185,16 +188,17 @@ "# lo_02, = axs1[2].plot(level_vec_2)\n", "# axs1[2].autoscale()\n", "fig1.tight_layout()\n", - "plt.show()\n", - "plt.pause(1)\n", + "fig1.show()\n", + "plt.pause(2)\n", "\n", "# loop through time steps of the pipeline\n", - "for it_pipe in range(1,pipe.nt):\n", + "for it_pipe in range(1,pipe.nt+1):\n", "\n", "# for each pipeline timestep, execute nt_eRK4 timesteps of the reservoir code\n", - " # set initial conditions for the reservoir time evolution calculted with e-RK4\n", + " # set initial conditions for the reservoir time evolution calculated with e-RK4\n", " V.pressure = p_old[0]\n", " V.outflux = v_old[0]\n", + " # V.influx = v_boundary_tur[it_pipe]\n", " # calculate the time evolution of the reservoir level within each pipeline timestep to avoid runaway numerical error\n", " for it_res in range(nt_eRK4):\n", " V.e_RK_4() # call e-RK4 to update outflux\n", @@ -204,12 +208,14 @@ " if (V.level < critical_level_low) or (V.level > critical_level_high): # make sure to never exceed critical levels\n", " i_max = it_pipe # for plotting only calculated values\n", " break \n", - " level_vec[it_pipe] = V.level \n", + " level_vec[it_pipe] = V.level \n", + "\n", "\n", " # set boundary conditions for the next timestep of the characteristic method\n", " p_boundary_res[it_pipe] = rho*g*V.level-v_old[1]**2*rho/2\n", " v_boundary_res[it_pipe] = v_old[1]+1/(rho*c)*(p_boundary_res[it_pipe]-p_old[1])-f_D*dt/(2*D)*abs(v_old[1])*v_old[1] \\\n", " +dt*g*np.sin(alpha)\n", + " \n", "\n", " # the the boundary conditions in the pipe.object and thereby calculate boundary pressure at turbine\n", " pipe.set_boundary_conditions_next_timestep(v_boundary_res[it_pipe],p_boundary_res[it_pipe],v_boundary_tur[it_pipe])\n", @@ -227,9 +233,10 @@ " lo_00, = axs1[0].plot(pl_vec,pressure_conversion(pipe.p_old,initial_pressure_unit, conversion_pressure_unit)[0],marker='.',c='blue')\n", " lo_01, = axs1[1].plot(pl_vec,pipe.v_old,marker='.',c='blue')\n", " # lo_02, = axs1[2].plot(level_vec_2,c='blue')\n", - " fig1.suptitle(str(it_pipe))\n", + " fig1.suptitle(str(round(t_vec[it_pipe],2))+ ' s / '+str(round(t_vec[-1],2)) + ' s' )\n", " fig1.canvas.draw()\n", " fig1.tight_layout()\n", + " fig1.show()\n", " plt.pause(0.00001) \n", "\n", " # prepare for next loop\n", @@ -242,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [