consolidated the getter methods of the classes

This commit is contained in:
Georg ´Brantegger
2022-07-01 11:28:21 +02:00
parent b948ab39cb
commit 28d38e8bb4
3 changed files with 108 additions and 86 deletions

View File

@@ -1,15 +1,18 @@
import numpy as np
from Ausgleichsbecken_functions import FODE_function, get_h_halfstep, get_p_halfstep from Ausgleichsbecken_functions import FODE_function, get_h_halfstep, get_p_halfstep
#importing pressure conversion function #importing pressure conversion function
import sys import sys
import os import os
current = os.path.dirname(os.path.realpath('Main_Programm.ipynb')) current = os.path.dirname(os.path.realpath(__file__))
parent = os.path.dirname(current) parent = os.path.dirname(current)
sys.path.append(parent) sys.path.append(parent)
from functions.pressure_conversion import pressure_conversion from functions.pressure_conversion import pressure_conversion
class Ausgleichsbecken_class: class Ausgleichsbecken_class:
# units # units
# make sure that units and print units are the same
# units are used to label graphs and print units are used to have a bearable format when using pythons print()
area_unit = r'$\mathrm{m}^2$' area_unit = r'$\mathrm{m}^2$'
area_outflux_unit = r'$\mathrm{m}^2$' area_outflux_unit = r'$\mathrm{m}^2$'
flux_unit = r'$\mathrm{m}^3/\mathrm{s}$' flux_unit = r'$\mathrm{m}^3/\mathrm{s}$'
@@ -18,13 +21,28 @@ class Ausgleichsbecken_class:
time_unit = 's' time_unit = 's'
volume_unit = r'$\mathrm{m}^3$' volume_unit = r'$\mathrm{m}^3$'
area_unit_print = ''
area_outflux_unit_print = ''
flux_unit_print = 'm³/s'
level_unit_print = 'm'
pressure_unit_print = 'Pa'
time_unit_print = 's'
volume_unit_print = ''
# init # init
def __init__(self,area,outflux_area,level_min,level_max,timestep = 1): def __init__(self,area,outflux_area,level_min = 0,level_max = np.inf ,timestep = 1):
self.area = area # base area of the rectangular structure self.area = area # base area of the rectangular structure
self.area_outflux = outflux_area # area of the outlet towards the pipeline self.area_outflux = outflux_area # area of the outlet towards the pipeline
self.level_min = level_min # lowest allowed water level self.level_min = level_min # lowest allowed water level
self.level_max = level_max # highest allowed water level self.level_max = level_max # highest allowed water level
self.timestep = timestep # timestep of the simulation self.timestep = timestep # timestep of the simulation
# initialize for get_info
self.level = "--"
self.influx = "--"
self.outflux = "--"
self.volume = "--"
# setter # setter
def set_volume(self): def set_volume(self):
@@ -41,32 +59,35 @@ class Ausgleichsbecken_class:
self.outflux = outflux self.outflux = outflux
# getter # getter
def get_area(self): def get_info(self, full = False):
print('The base area of the cuboid reservoir is', self.area, self.area_unit) new_line = '\n'
if full == True:
# :<10 pads the self.value to be 10 characters wide
print_str = (f"The cuboid reservoir has the following attributes: {new_line}"
f"----------------------------- {new_line}"
f"Base area = {self.area:<10} {self.area_unit_print} {new_line}"
f"Outflux area = {self.area_outflux:<10} {self.area_outflux_unit_print} {new_line}"
f"Current level = {self.level:<10} {self.level_unit_print}{new_line}"
f"Critical level low = {self.level_min:<10} {self.level_unit_print} {new_line}"
f"Critical level high = {self.level_max:<10} {self.level_unit_print} {new_line}"
f"Volume in reservoir = {self.volume:<10} {self.volume_unit_print} {new_line}"
f"Current influx = {self.influx:<10} {self.flux_unit_print} {new_line}"
f"Current outflux = {self.outflux:<10} {self.flux_unit_print} {new_line}"
f"Simulation timestep = {self.timestep:<10} {self.time_unit_print} {new_line}"
f"----------------------------- {new_line}")
else:
# :<10 pads the self.value to be 10 characters wide
print_str = (f"The current attributes are: {new_line}"
f"----------------------------- {new_line}"
f"Current level = {self.level:<10} {self.level_unit_print}{new_line}"
f"Volume in reservoir = {self.volume:<10} {self.volume_unit_print} {new_line}"
f"Current influx = {self.influx:<10} {self.flux_unit_print} {new_line}"
f"Current outflux = {self.outflux:<10} {self.flux_unit_print} {new_line}"
f"----------------------------- {new_line}")
def get_outflux_area(self): print(print_str)
print('The outflux area from the cuboid reservoir to the pipeline is', \
self.area_outflux, self.area_outflux_unit)
def get_level(self):
print('The current level in the reservoir is', self.level , self.level_unit)
def get_crit_levels(self):
print('The critical water levels in the reservoir are: \n',\
' Minimum:', self.level_min , self.level_unit , '\n',\
' Maximum:', self.level_max , self.level_unit )
def get_volume(self):
print('The current water volume in the reservoir is', self.volume, self.volume_unit)
def get_timestep(self):
print('The timestep for the simulation is' , self.timestep, self.time_unit)
def get_influx(self):
print('The current influx is', self.influx, self.flux_unit)
def get_outflux(self):
print('The current outflux is', self.outflux, self.flux_unit)
# methods # methods
def update_level(self,timestep): def update_level(self,timestep):
@@ -92,4 +113,4 @@ class Ausgleichsbecken_class:
ynp1 = yn + dt/6*(FODE_function(Y1, h, alpha, p)+2*FODE_function(Y2, h_hs, alpha, p_hs)+ \ ynp1 = yn + dt/6*(FODE_function(Y1, h, alpha, p)+2*FODE_function(Y2, h_hs, alpha, p_hs)+ \
2*FODE_function(Y3, h_hs, alpha, p_hs)+ FODE_function(Y4, h, alpha, p)) 2*FODE_function(Y3, h_hs, alpha, p_hs)+ FODE_function(Y4, h, alpha, p))
self.outflux = ynp1*self.area_outflux self.outflux = ynp1*self.area_outflux

View File

@@ -21,6 +21,17 @@ class Druckrohrleitung_class:
time_unit = 's' time_unit = 's'
velocity_unit = r'$\mathrm{m}/\mathrm{s}$' # for flux and pressure propagation velocity_unit = r'$\mathrm{m}/\mathrm{s}$' # for flux and pressure propagation
volume_unit = r'$\mathrm{m}^3$' volume_unit = r'$\mathrm{m}^3$'
acceleration_unit_print = 'm/s²'
angle_unit_print = '°'
area_unit_print = ''
density_unit_print = 'kg/m³'
flux_unit_print = 'm³/s'
length_unit_print = 'm'
pressure_unit_print = 'Pa'
time_unit_print = 's'
velocity_unit_print = 'm/s' # for flux and pressure propagation
volume_unit_print = ''
# init # init
@@ -36,8 +47,9 @@ class Druckrohrleitung_class:
self.dx = total_length/number_segments self.dx = total_length/number_segments
self.l_vec = np.arange(0,(number_segments+1)*self.dx,self.dx) self.l_vec = np.arange(0,(number_segments+1)*self.dx,self.dx)
# workaround for try-except construct in set_number_of_timesteps # initialize for get_info method
self.c = 0 self.c = '--'
self.dt = '--'
# setter # setter
def set_pressure_propagation_velocity(self,c): def set_pressure_propagation_velocity(self,c):
@@ -46,7 +58,7 @@ class Druckrohrleitung_class:
def set_number_of_timesteps(self,number_timesteps): def set_number_of_timesteps(self,number_timesteps):
self.nt = number_timesteps self.nt = number_timesteps
if self.c == 0: if self.c == '--':
raise Exception('Please set the pressure propagation velocity before setting the number of timesteps.') raise Exception('Please set the pressure propagation velocity before setting the number of timesteps.')
else: else:
self.t_vec = np.arange(0,self.nt*self.dt,self.dt) self.t_vec = np.arange(0,self.nt*self.dt,self.dt)
@@ -62,7 +74,7 @@ class Druckrohrleitung_class:
#initialize the vectors in which the old and new pressures are stored for the method of characteristics #initialize the vectors in which the old and new pressures are stored for the method of characteristics
self.p_old = self.p0.copy() self.p_old = self.p0.copy()
self.p_new = np.empty_like(self.p_old) self.p = np.empty_like(self.p_old)
def set_initial_flow_velocity(self,velocity): def set_initial_flow_velocity(self,velocity):
if np.size(velocity) == 1: if np.size(velocity) == 1:
@@ -74,7 +86,7 @@ class Druckrohrleitung_class:
#initialize the vectors in which the old and new velocities are stored for the method of characteristics #initialize the vectors in which the old and new velocities are stored for the method of characteristics
self.v_old = self.v0.copy() self.v_old = self.v0.copy()
self.v_new = np.empty_like(self.v_old) self.v = np.empty_like(self.v_old)
def set_boundary_conditions_next_timestep(self,v_reservoir,p_reservoir,v_turbine,input_unit_pressure = 'Pa'): def set_boundary_conditions_next_timestep(self,v_reservoir,p_reservoir,v_turbine,input_unit_pressure = 'Pa'):
rho = self.density rho = self.density
@@ -88,53 +100,42 @@ class Druckrohrleitung_class:
self.v_boundary_tur = v_turbine self.v_boundary_tur = v_turbine
self.p_boundary_res,_ = pressure_conversion(p_reservoir,input_unit_pressure,target_unit=self.pressure_unit) self.p_boundary_res,_ = pressure_conversion(p_reservoir,input_unit_pressure,target_unit=self.pressure_unit)
self.p_boundary_tur = p_old+rho*c*v_old-rho*c*f_D*dt/(2*D)*abs(v_old)*v_old self.p_boundary_tur = p_old+rho*c*v_old-rho*c*f_D*dt/(2*D)*abs(v_old)*v_old
self.v_new[0] = self.v_boundary_res.copy() self.v[0] = self.v_boundary_res.copy()
self.v_new[-1] = self.v_boundary_tur.copy() self.v[-1] = self.v_boundary_tur.copy()
self.p_new[0] = self.p_boundary_res.copy() self.p[0] = self.p_boundary_res.copy()
self.p_new[-1] = self.p_boundary_tur.copy() self.p[-1] = self.p_boundary_tur.copy()
# getter # getter
def get_pipeline_geometry(self): def get_info(self):
print('The total length of the pipeline is', '\n', \ new_line = '\n'
self.length, self.length_unit, '\n', \
'The diameter of the pipeline is', '\n', \
self.dia, self.length_unit, '\n', \
'The pipeline is divided into', self.n_seg , 'segments of length', '\n', \
round(self.dx,1), self.length_unit, '\n', \
'The pipeline has an inclination angle of', '\n', \
self.angle, self.angle_unit)
def get_other_pipeline_info(self):
print('The Darcy-friction factor of the pipeline is', '\n', \
self.f_D, '\n', \
'The pipeline is filled with a liquid with density', '\n', \
self.density, self.density_unit, '\n', \
'The gravitational acceleration is set to', '\n', \
self.g, self.acceleration_unit)
def get_pressure_propagation_velocity(self):
print('The pressure propagation velocity in the pipeline is', '\n', \
self.c, self.velocity_unit)
def get_number_of_timesteps(self): # :<10 pads the self.value to be 10 characters wide
print(self.nt, 'timesteps are performed in the simulation') print_str = (f"The pipeline has the following attributes: {new_line}"
f"----------------------------- {new_line}"
f"Length = {self.length:<10} {self.length_unit_print} {new_line}"
f"Diameter = {self.dia:<10} {self.length_unit_print} {new_line}"
f"Number of segemnts = {self.n_seg:<10} {new_line}"
f"Number of nodes = {self.n_seg+1:<10} {new_line}"
f"Length per segment = {self.dx:<10} {self.length_unit_print} {new_line}"
f"Pipeline angle = {self.angle:<10} {self.angle_unit_print} {new_line}"
f"Darcy friction factor = {self.f_D:<10} {new_line}"
f"Density of liquid = {self.density:<10} {self.density_unit_print} {new_line}"
f"Pressure wave vel. = {self.c:<10} {self.velocity_unit_print} {new_line}"
f"Simulation timesteps = {self.dt:<10} {self.time_unit_print } {new_line}"
f"Number of timesteps = {self.nt:<10} {new_line}"
f"----------------------------- {new_line}"
f"Velocity and pressure distribution are vectors and are accessible by the .v and .p attribute of the pipeline object")
print(print_str)
def get_initial_pressure(self,target_unit='bar'):
print('The inital pressure distribution in is', '\n', \
pressure_conversion(self.p0,self.pressure_unit,target_unit))
def get_initial_flow_velocity(self):
print('The inital velocity distribution is', '\n', \
self.v0, self.velocity_unit)
def get_boundary_conditions_next_timestep(self,target_unit_pressure ='bar'): def get_boundary_conditions_next_timestep(self,target_unit_pressure ='bar'):
print('The pressure at the reservoir for the next timestep is', '\n', \ print('The pressure at the reservoir for the next timestep is', '\n', \
pressure_conversion(self.p_boundary_res,self.pressure_unit,target_unit_pressure), '\n', \ pressure_conversion(self.p_boundary_res,self.pressure_unit_print,target_unit_pressure), '\n', \
'The velocity at the reservoir for the next timestep is', '\n', \ 'The velocity at the reservoir for the next timestep is', '\n', \
self.v_boundary_res, self.velocity_unit, '\n', \ self.v_boundary_res, self.velocity_unit, '\n', \
'The pressure at the turbine for the next timestep is', '\n', \ 'The pressure at the turbine for the next timestep is', '\n', \
pressure_conversion(self.p_boundary_tur,self.pressure_unit,target_unit_pressure), '\n', \ pressure_conversion(self.p_boundary_tur,self.pressure_unit_print,target_unit_pressure), '\n', \
'The velocity at the turbine for the next timestep is', '\n', \ 'The velocity at the turbine for the next timestep is', '\n', \
self.v_boundary_tur, self.velocity_unit) self.v_boundary_tur, self.velocity_unit)
@@ -149,14 +150,14 @@ class Druckrohrleitung_class:
D = self.dia D = self.dia
for i in range(1,nn-1): for i in range(1,nn-1):
self.v_new[i] = 0.5*(self.v_old[i-1]+self.v_old[i+1])+0.5/(rho*c)*(self.p_old[i-1]-self.p_old[i+1]) \ self.v[i] = 0.5*(self.v_old[i-1]+self.v_old[i+1])+0.5/(rho*c)*(self.p_old[i-1]-self.p_old[i+1]) \
-f_D*dt/(4*D)*(abs(self.v_old[i-1])*self.v_old[i-1]+abs(self.v_old[i+1])*self.v_old[i+1]) -f_D*dt/(4*D)*(abs(self.v_old[i-1])*self.v_old[i-1]+abs(self.v_old[i+1])*self.v_old[i+1])
self.p_new[i] = 0.5*rho*c*(self.v_old[i-1]-self.v_old[i+1])+0.5*(self.p_old[i-1]+self.p_old[i+1]) \ self.p[i] = 0.5*rho*c*(self.v_old[i-1]-self.v_old[i+1])+0.5*(self.p_old[i-1]+self.p_old[i+1]) \
-rho*c*f_D*dt/(4*D)*(abs(self.v_old[i-1])*self.v_old[i-1]-abs(self.v_old[i+1])*self.v_old[i+1]) -rho*c*f_D*dt/(4*D)*(abs(self.v_old[i-1])*self.v_old[i-1]-abs(self.v_old[i+1])*self.v_old[i+1])
self.p_old = self.p_new.copy() self.p_old = self.p.copy()
self.v_old = self.v_new.copy() self.v_old = self.v.copy()

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -21,7 +21,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -105,7 +105,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -133,7 +133,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -171,15 +171,15 @@
"for it in range(1,pipe.nt):\n", "for it in range(1,pipe.nt):\n",
" pipe.set_boundary_conditions_next_timestep(v_1[it],p_1[it],v_np1[it])\n", " pipe.set_boundary_conditions_next_timestep(v_1[it],p_1[it],v_np1[it])\n",
" pipe.timestep_characteristic_method()\n", " pipe.timestep_characteristic_method()\n",
" lo_00.set_ydata(pipe.p_new)\n", " lo_00.set_ydata(pipe.p)\n",
" lo_01.set_ydata(pipe.v_new)\n", " lo_01.set_ydata(pipe.v)\n",
"\n", "\n",
" # store parameters of node 1 (at reservoir)\n", " # store parameters of node 1 (at reservoir)\n",
" pipe.p_1[it] = pipe.p_new[0]\n", " pipe.p_1[it] = pipe.p[0]\n",
" pipe.v_1[it] = pipe.v_new[0]\n", " pipe.v_1[it] = pipe.v[0]\n",
" # store parameters of node N+1 (at reservoir)\n", " # store parameters of node N+1 (at reservoir)\n",
" pipe.p_np1[it] = pipe.p_new[-1]\n", " pipe.p_np1[it] = pipe.p[-1]\n",
" pipe.v_np1[it] = pipe.v_new[-1]\n", " pipe.v_np1[it] = pipe.v[-1]\n",
" \n", " \n",
" fig2.suptitle(str(it))\n", " fig2.suptitle(str(it))\n",
" fig2.canvas.draw()\n", " fig2.canvas.draw()\n",
@@ -189,7 +189,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 9,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [