code cleanup:
consistenly use getter and setter methods commenting etc
This commit is contained in:
@@ -33,6 +33,8 @@ class Druckrohrleitung_class:
|
||||
self.density = rho # density of the liquid in the pipeline
|
||||
self.g = g # gravitational acceleration
|
||||
|
||||
self.A = (diameter/2)**2*np.pi
|
||||
|
||||
self.dx = total_length/number_segments # length of each segment
|
||||
self.l_vec = np.arange(0,(number_segments+1),1)*self.dx # vector giving the distance from each node to the start of the pipeline
|
||||
|
||||
@@ -98,23 +100,22 @@ class Druckrohrleitung_class:
|
||||
p_old_res = self.p_old[1] # @ second node (the one after the reservoir)
|
||||
v_old_res = self.v_old[1] # @ second node (the one after the reservoir)
|
||||
# set the boundary conditions derived from reservoir and turbine
|
||||
self.v_boundary_tur = v_turbine # at new timestep
|
||||
self.p_boundary_res = p_reservoir # at new timestep
|
||||
v_boundary_tur = v_turbine # at new timestep
|
||||
p_boundary_res = p_reservoir # at new timestep
|
||||
# calculate the missing boundary conditions
|
||||
self.v_boundary_res = v_old_res+1/(rho*c)*(p_reservoir-p_old_res)+dt*g*np.sin(alpha)-f_D*dt/(2*D)*abs(v_old_res)*v_old_res
|
||||
self.p_boundary_tur = p_old_tur-rho*c*(v_turbine-v_old_tur)+rho*c*dt*g*np.sin(alpha)-f_D*rho*c*dt/(2*D)*abs(v_old_tur)*v_old_tur
|
||||
v_boundary_res = v_old_res+1/(rho*c)*(p_boundary_res-p_old_res)+dt*g*np.sin(alpha)-f_D*dt/(2*D)*abs(v_old_res)*v_old_res
|
||||
p_boundary_tur = p_old_tur-rho*c*(v_boundary_tur-v_old_tur)+rho*c*dt*g*np.sin(alpha)-f_D*rho*c*dt/(2*D)*abs(v_old_tur)*v_old_tur
|
||||
|
||||
# write boundary conditions to the velocity/pressure vectors of the next timestep
|
||||
self.v[0] = self.v_boundary_res.copy()
|
||||
self.v[-1] = self.v_boundary_tur.copy()
|
||||
self.p[0] = self.p_boundary_res.copy()
|
||||
self.p[-1] = self.p_boundary_tur.copy()
|
||||
|
||||
self.v[0] = v_boundary_res
|
||||
self.v[-1] = v_boundary_tur
|
||||
self.p[0] = p_boundary_res
|
||||
self.p[-1] = p_boundary_tur
|
||||
|
||||
def set_steady_state(self,ss_flux,ss_level_reservoir,pl_vec,h_vec):
|
||||
# set the pressure and velocity distributions, that allow a constant flow of water from the (steady-state) reservoir to the (steady-state) turbine
|
||||
# the flow velocity is given by the constant flow through the pipe
|
||||
ss_v0 = np.full(self.n_seg+1,ss_flux/(self.dia**2/4*np.pi))
|
||||
ss_v0 = np.full(self.n_seg+1,ss_flux/self.A)
|
||||
# the static pressure is given by the hydrostatic pressure, corrected for friction losses and dynamic pressure
|
||||
ss_pressure = (self.density*self.g*(ss_level_reservoir+h_vec)-ss_v0**2*self.density/2)-(self.f_D*pl_vec/self.dia*self.density/2*ss_v0**2)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -16,64 +16,98 @@
|
||||
"current = os.path.dirname(os.path.realpath('Main_Programm.ipynb'))\n",
|
||||
"parent = os.path.dirname(current)\n",
|
||||
"sys.path.append(parent)\n",
|
||||
"from functions.pressure_conversion import pressure_conversion"
|
||||
"from functions.pressure_conversion import pressure_conversion\n",
|
||||
"from Ausgleichsbecken.Ausgleichsbecken_class_file import Ausgleichsbecken_class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib qt5\n",
|
||||
"#define constants\n",
|
||||
"#define constants pipe\n",
|
||||
"\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",
|
||||
"L = 1000 # length of pipeline [m]\n",
|
||||
"D = 1 # pipe diameter [m]\n",
|
||||
"Q0 = 2 # initial flow in whole pipe [m³/s]\n",
|
||||
"h_res = 20 # water level in upstream reservoir [m]\n",
|
||||
"n = 10 # number of pipe segments in discretization\n",
|
||||
"nt = 100 # number of time steps after initial conditions\n",
|
||||
"f_D = 0.01 # Darcy friction factor\n",
|
||||
"c = 400 # propagation velocity of the pressure wave [m/s]\n",
|
||||
"h_pipe = 200 # hydraulic head without reservoir [m] \n",
|
||||
"alpha = np.arcsin(h_pipe/L) # Höhenwinkel der Druckrohrleitung \n",
|
||||
"L = 1000. # length of pipeline [m]\n",
|
||||
"D = 0.9 # pipe diameter [m]\n",
|
||||
"h_res = 10. # water level in upstream reservoir [m]\n",
|
||||
"n = 50 # number of pipe segments in discretization\n",
|
||||
"nt = 5000 # number of time steps after initial conditions\n",
|
||||
"f_D = 0.01 # Darcy friction factor\n",
|
||||
"c = 400. # propagation velocity of the pressure wave [m/s]\n",
|
||||
"h_pipe = 105. # hydraulic head without reservoir [m] \n",
|
||||
"alpha = np.arcsin(h_pipe/L) # Höhenwinkel der Druckrohrleitung \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# preparing the discretization and initial conditions\n",
|
||||
"initial_influx = 2. # m³/s\n",
|
||||
"initial_level = 10. # m\n",
|
||||
"initial_flux = 0.8 # m³/s\n",
|
||||
"initial_level = h_res # m\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",
|
||||
"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,h_pipe+h_pipe/n,h_pipe/n) # hydraulic head of pipeline at each node\n"
|
||||
"h_vec = np.arange(0,h_pipe+h_pipe/n,h_pipe/n) # hydraulic head of pipeline at each node\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# define constants reservoir\n",
|
||||
"conversion_pressure_unit = 'mWS'\n",
|
||||
"\n",
|
||||
"area_base = 75. # m²\n",
|
||||
"area_pipe = (D/2)**2*np.pi # m²\n",
|
||||
"critical_level_low = 0. # m\n",
|
||||
"critical_level_high = 100. # m\n",
|
||||
"\n",
|
||||
"# make sure e-RK4 method of reservoir has a small enough timestep to avoid runaway numerical error\n",
|
||||
"nt_eRK4 = 1 # number of simulation steps of reservoir in between timesteps of pipeline \n",
|
||||
"simulation_timestep = dt/nt_eRK4"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"V = Ausgleichsbecken_class(area_base, area_pipe, critical_level_low, critical_level_high,simulation_timestep)\n",
|
||||
"V.set_steady_state(initial_flux,initial_level,conversion_pressure_unit)\n",
|
||||
"\n",
|
||||
"pipe = Druckrohrleitung_class(L,D,n,alpha,f_D)\n",
|
||||
"pipe.set_pressure_propagation_velocity(c)\n",
|
||||
"pipe.set_number_of_timesteps(nt)\n",
|
||||
"pipe.set_steady_state(initial_influx,initial_level,pl_vec,h_vec)"
|
||||
"pipe.set_steady_state(initial_flux,initial_level,pl_vec,h_vec)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(V.get_current_influx())\n",
|
||||
"print(V.get_current_outflux())\n",
|
||||
"print(V.get_current_level())\n",
|
||||
"print(V.get_current_pressure())\n",
|
||||
"print(pipe.get_current_pressure_distribution()[0])\n",
|
||||
"print(pipe.get_current_velocity_distribution()*area_pipe)\n",
|
||||
"print(pipe.get_current_velocity_distribution())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# initialization for timeloop\n",
|
||||
"\n",
|
||||
"level_vec = np.zeros_like(t_vec)\n",
|
||||
"level_vec[0] = V.get_current_level()\n",
|
||||
"\n",
|
||||
"# prepare the vectors in which the pressure and velocity distribution in the pipeline from the previous timestep are stored\n",
|
||||
"v_old = pipe.get_current_velocity_distribution()\n",
|
||||
"p_old = pipe.get_current_pressure_distribution()\n",
|
||||
@@ -94,80 +128,110 @@
|
||||
"p_boundary_tur[0] = p_old[-1]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig2,axs2 = plt.subplots(2,1)\n",
|
||||
"axs2[0].set_title('Pressure distribution in pipeline')\n",
|
||||
"axs2[0].set_xlabel(r'$x$ [$\\mathrm{m}$]')\n",
|
||||
"axs2[0].set_ylabel(r'$p$ [mWS]')\n",
|
||||
"lo_00, = axs2[0].plot(pl_vec,pressure_conversion(p_old,'Pa','mWS'),marker='.')\n",
|
||||
"axs2[0].set_ylim([0.9*np.min(pressure_conversion(p_old,'Pa','mWS')),1.1*np.max(pressure_conversion(p_old,'Pa','mWS'))])\n",
|
||||
"\n",
|
||||
"axs2[1].set_title('Velocity distribution in pipeline')\n",
|
||||
"axs2[1].set_xlabel(r'$x$ [$\\mathrm{m}$]')\n",
|
||||
"axs2[1].set_ylabel(r'$p$ [mWS]')\n",
|
||||
"lo_01, = axs2[1].plot(pl_vec,v_old,marker='.')\n",
|
||||
"axs2[1].set_ylim([0.9*np.min(v_old),1.1*np.max(v_boundary_res)])\n",
|
||||
"\n",
|
||||
"fig2.tight_layout()\n",
|
||||
"plt.pause(5)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"for it in range(1,pipe.nt):\n",
|
||||
" pipe.set_boundary_conditions_next_timestep(p_boundary_res[0],v_boundary_tur[0])\n",
|
||||
" pipe.timestep_characteristic_method()\n",
|
||||
" lo_00.set_ydata(pressure_conversion(pipe.get_current_pressure_distribution(),'Pa','mWS'))\n",
|
||||
" lo_01.set_ydata(pipe.get_current_velocity_distribution())\n",
|
||||
"\n",
|
||||
" v_boundary_res[it] = pipe.get_current_velocity_distribution()[0]\n",
|
||||
" v_boundary_tur[it] = pipe.get_current_velocity_distribution()[-1]\n",
|
||||
" p_boundary_res[it] = pipe.get_current_pressure_distribution()[0]\n",
|
||||
" p_boundary_tur[it] = pipe.get_current_pressure_distribution()[-1]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" fig2.suptitle(str(it))\n",
|
||||
" fig2.canvas.draw()\n",
|
||||
" fig2.tight_layout()\n",
|
||||
" plt.pause(0.2)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig3,axs3 = plt.subplots(2,2)\n",
|
||||
"axs3[0,0].set_title('Pressure Reservoir')\n",
|
||||
"axs3[0,0].plot(t_vec,pressure_conversion(p_boundary_res,'Pa','mWS'))\n",
|
||||
"axs3[0,0].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs3[0,0].set_ylabel(r'$p$ [mWS]')\n",
|
||||
"axs3[0,0].set_ylim([0.9*np.min(pressure_conversion(p_boundary_res,'Pa','mWS')),1.1*np.max(pressure_conversion(p_boundary_res,'Pa','mWS'))])\n",
|
||||
"fig1,axs1 = plt.subplots(2,1)\n",
|
||||
"axs1[0].set_title('Pressure distribution in pipeline')\n",
|
||||
"axs1[0].set_xlabel(r'$x$ [$\\mathrm{m}$]')\n",
|
||||
"axs1[0].set_ylabel(r'$p$ [mWS]')\n",
|
||||
"lo_00, = axs1[0].plot(pl_vec,pressure_conversion(p_old,'Pa',conversion_pressure_unit),marker='.')\n",
|
||||
"axs1[0].set_ylim([0.9*np.min(pressure_conversion(p_old,'Pa',conversion_pressure_unit)),1.1*np.max(pressure_conversion(p_old,'Pa',conversion_pressure_unit))])\n",
|
||||
"\n",
|
||||
"axs3[0,1].set_title('Velocity Reservoir')\n",
|
||||
"axs3[0,1].plot(t_vec,v_boundary_res)\n",
|
||||
"axs3[0,1].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs3[0,1].set_ylabel(r'$v$ [$\\mathrm{m}/\\mathrm{s}$]')\n",
|
||||
"axs3[0,1].set_ylim([0.9*np.min(v_boundary_res),1.1*np.max(v_boundary_res)])\n",
|
||||
"axs1[1].set_title('Velocity distribution in pipeline')\n",
|
||||
"axs1[1].set_xlabel(r'$x$ [$\\mathrm{m}$]')\n",
|
||||
"axs1[1].set_ylabel(r'$v$ [m/s]')\n",
|
||||
"lo_01, = axs1[1].plot(pl_vec,v_old,marker='.')\n",
|
||||
"# axs1[1].set_ylim([0.9*np.min(v_old),1.1*np.max(v_boundary_res)])\n",
|
||||
"\n",
|
||||
"axs3[1,0].set_title('Pressure Turbine')\n",
|
||||
"axs3[1,0].plot(t_vec,pressure_conversion(p_boundary_tur,'Pa','mWS'))\n",
|
||||
"axs3[1,0].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs3[1,0].set_ylabel(r'$p$ [mWS]')\n",
|
||||
"axs3[1,0].set_ylim([0.9*np.min(pressure_conversion(p_boundary_tur,'Pa','mWS')),1.1*np.max(pressure_conversion(p_boundary_tur,'Pa','mWS'))])\n",
|
||||
"fig1.tight_layout()\n",
|
||||
"plt.pause(1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"axs3[1,1].set_title('Velocity Turbine')\n",
|
||||
"axs3[1,1].plot(t_vec,v_boundary_tur)\n",
|
||||
"axs3[1,1].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs3[1,1].set_ylabel(r'$v$ [$\\mathrm{m}/\\mathrm{s}$]')\n",
|
||||
"axs3[1,1].set_ylim([0.9*np.min(v_boundary_tur),1.1*np.max(v_boundary_tur)])\n",
|
||||
"for it_pipe in range(1,nt):\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",
|
||||
" V.set_pressure = p_old[0]\n",
|
||||
" V.set_outflux = v_old[0]*area_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.timestep_reservoir_evolution() \n",
|
||||
" level_vec[it_pipe] = V.get_current_level() \n",
|
||||
"\n",
|
||||
"fig3.tight_layout()\n",
|
||||
" \n",
|
||||
" # set boundary conditions for the next timestep of the characteristic method\n",
|
||||
" p_boundary_res[it_pipe] = V.get_current_pressure()\n",
|
||||
" v_boundary_tur[it_pipe] = initial_flux/area_pipe\n",
|
||||
"\n",
|
||||
" # the the boundary conditions in the pipe.object and thereby calculate boundary pressure at turbine\n",
|
||||
" pipe.set_boundary_conditions_next_timestep(p_boundary_res[it_pipe],v_boundary_tur[it_pipe])\n",
|
||||
" p_boundary_tur[it_pipe] = pipe.get_current_pressure_distribution()[-1]\n",
|
||||
" v_boundary_res[it_pipe] = pipe.get_current_velocity_distribution()[0]\n",
|
||||
"\n",
|
||||
" # perform the next timestep via the characteristic method\n",
|
||||
" pipe.timestep_characteristic_method()\n",
|
||||
"\n",
|
||||
" # prepare for next loop\n",
|
||||
" p_old = pipe.get_current_pressure_distribution()\n",
|
||||
" v_old = pipe.get_current_velocity_distribution()\n",
|
||||
"\n",
|
||||
" # plot some stuff\n",
|
||||
" # remove line-objects to autoscale axes (there is definetly a better way, but this works ¯\\_(ツ)_/¯ )\n",
|
||||
" lo_00.remove()\n",
|
||||
" lo_01.remove()\n",
|
||||
" # lo_02.remove()\n",
|
||||
" # plot new pressure and velocity distribution in the pipeline\n",
|
||||
" lo_00, = axs1[0].plot(pl_vec,pressure_conversion(p_old,'Pa', conversion_pressure_unit),marker='.',c='blue')\n",
|
||||
" lo_01, = axs1[1].plot(pl_vec,v_old,marker='.',c='blue')\n",
|
||||
" \n",
|
||||
" fig1.suptitle(str(round(t_vec[it_pipe],2)) + '/' + str(t_vec[-1]))\n",
|
||||
" fig1.canvas.draw()\n",
|
||||
" fig1.tight_layout()\n",
|
||||
" plt.pause(0.00001)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig2,axs2 = plt.subplots(2,2)\n",
|
||||
"axs2[0,0].set_title('Pressure Reservoir')\n",
|
||||
"axs2[0,0].plot(t_vec,pressure_conversion(p_boundary_res,'Pa',conversion_pressure_unit))\n",
|
||||
"axs2[0,0].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs2[0,0].set_ylabel(r'$p$ [mWS]')\n",
|
||||
"axs2[0,0].set_ylim([0.9*np.min(pressure_conversion(p_boundary_res,'Pa',conversion_pressure_unit)),1.1*np.max(pressure_conversion(p_boundary_res,'Pa',conversion_pressure_unit))])\n",
|
||||
"\n",
|
||||
"axs2[0,1].set_title('Velocity Reservoir')\n",
|
||||
"axs2[0,1].plot(t_vec,v_boundary_res)\n",
|
||||
"axs2[0,1].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs2[0,1].set_ylabel(r'$v$ [$\\mathrm{m}/\\mathrm{s}$]')\n",
|
||||
"axs2[0,1].set_ylim([0.9*np.min(v_boundary_res),1.1*np.max(v_boundary_res)])\n",
|
||||
"\n",
|
||||
"axs2[1,0].set_title('Pressure Turbine')\n",
|
||||
"axs2[1,0].plot(t_vec,pressure_conversion(p_boundary_tur,'Pa',conversion_pressure_unit))\n",
|
||||
"axs2[1,0].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs2[1,0].set_ylabel(r'$p$ [mWS]')\n",
|
||||
"axs2[1,0].set_ylim([0.9*np.min(pressure_conversion(p_boundary_tur,'Pa',conversion_pressure_unit)),1.1*np.max(pressure_conversion(p_boundary_tur,'Pa',conversion_pressure_unit))])\n",
|
||||
"\n",
|
||||
"axs2[1,1].set_title('Velocity Turbine')\n",
|
||||
"axs2[1,1].plot(t_vec,v_boundary_tur)\n",
|
||||
"axs2[1,1].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
|
||||
"axs2[1,1].set_ylabel(r'$v$ [$\\mathrm{m}/\\mathrm{s}$]')\n",
|
||||
"axs2[1,1].set_ylim([0.9*np.min(v_boundary_tur),1.1*np.max(v_boundary_tur)])\n",
|
||||
"\n",
|
||||
"fig2.tight_layout()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user