code cleanup:

consistenly use getter and setter methods
commenting etc
This commit is contained in:
Brantegger Georg
2022-07-27 11:40:58 +02:00
parent ac8bfdb7c6
commit d1c15090dc
13 changed files with 956 additions and 584 deletions

View File

@@ -1,3 +1,4 @@
from time import time
import numpy as np
#importing pressure conversion function
import sys
@@ -8,35 +9,117 @@ sys.path.append(parent)
from functions.pressure_conversion import pressure_conversion
class Francis_Turbine:
def __init__(self, Q_nenn,p_nenn):
self.Q_n = Q_nenn
self.p_n = p_nenn
self.LA_n = 1. # 100%
h = pressure_conversion(p_nenn,'Pa','MWs')
self.A = Q_nenn/(np.sqrt(2*9.81*h)*0.98)
# 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()
density_unit = r'$\mathrm{kg}/\mathrm{m}^3$'
flux_unit = r'$\mathrm{m}^3/\mathrm{s}$'
LA_unit = '%'
pressure_unit = 'Pa'
time_unit = 's'
velocity_unit = r'$\mathrm{m}/\mathrm{s}$'
volume_unit = r'$\mathrm{m}^3$'
def set_LA(self,LA):
density_unit_print = 'kg/m³'
flux_unit_print = 'm³/s'
LA_unit_print = '%'
pressure_unit_print = 'mWS'
time_unit_print = 's'
velocity_unit_print = 'm/s'
volume_unit_print = ''
g = 9.81 # m/s² gravitational acceleration
# init
def __init__(self, Q_nenn,p_nenn,t_closing=-1.,timestep=-1.):
self.Q_n = Q_nenn # nominal flux
self.p_n = p_nenn # nominal pressure
self.LA_n = 1. # 100% # nominal Leitapparatöffnung
h = pressure_conversion(p_nenn,'Pa','MWs') # nominal pressure in terms of hydraulic head
self.A = Q_nenn/(np.sqrt(2*self.g*h)*0.98) # Ersatzfläche
self.dt = timestep # simulation timestep
self.t_c = t_closing # closing time
self.d_LA_max_dt = 1/t_closing # maximal change of LA per second
# initialize for get_info() - parameters will be converted to display -1 if not overwritten
self.p = pressure_conversion(-1,self.pressure_unit_print,self.pressure_unit)
self.Q = -1.
self.LA = -0.01
# setter
def set_LA(self,LA,display_warning=True):
# set Leitapparatöffnung
self.LA = LA
# warn user, that the .set_LA() method should not be used ot set LA manually
if display_warning == True:
print('Consider using the .update_LA() method instead of setting LA manually')
def set_timestep(self,timestep,display_warning=True):
# set Leitapparatöffnung
self.dt = time
# warn user, that the .set_LA() method should not be used ot set LA manually
if display_warning == True:
print('WARNING: You are changing the timestep of the turbine simulation. This has implications on the simulated closing speed!')
def set_pressure(self,pressure):
# set pressure in front of the turbine
self.p = pressure
def get_Q(self):
#getter
def get_current_Q(self):
# return the flux through the turbine, based on the current pressure in front
# of the turbine and the Leitapparatöffnung
self.Q = self.Q_n*(self.LA/self.LA_n)*np.sqrt(self.p/self.p_n)
return self.Q
def set_closing_time(self,t_closing):
self.t_c = t_closing
self.d_LA_max_dt = 1/t_closing
def get_current_LA(self):
return self.LA
def change_LA(self,LA_soll,timestep):
LA_diff = self.LA-LA_soll
LA_diff_max = self.d_LA_max_dt*timestep
if abs(LA_diff) > LA_diff_max:
LA_diff = np.sign(LA_diff)*LA_diff_max
self.LA = self.LA-LA_diff
def get_info(self, full = False):
new_line = '\n'
p = pressure_conversion(self.p,self.pressure_unit,self.pressure_unit_print)
p_n = pressure_conversion(self.p_n,self.pressure_unit,self.pressure_unit_print)
if full == True:
# :<10 pads the self.value to be 10 characters wide
print_str = (f"Turbine has the following attributes: {new_line}"
f"----------------------------- {new_line}"
f"Type = Francis {new_line}"
f"Nominal flux = {self.Q_n:<10} {self.flux_unit_print} {new_line}"
f"Nominal pressure = {round(p_n,3):<10} {self.pressure_unit_print}{new_line}"
f"Nominal LA = {self.LA_n*100:<10} {self.LA_unit_print} {new_line}"
f"Closing time = {self.t_c:<10} {self.time_unit_print} {new_line}"
f"Current flux = {self.Q:<10} {self.flux_unit_print} {new_line}"
f"Current pipe pressure = {round(p,3):<10} {self.pressure_unit_print} {new_line}"
f"Current LA = {self.LA*100:<10} {self.LA_unit_print} {new_line}"
f"Simulation timestep = {self.dt:<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 flux = {self.Q:<10} {self.flux_unit_print} {new_line}"
f"Current pipe pressure = {round(p,3):<10} {self.pressure_unit_print} {new_line}"
f"Current LA = {self.LA*100:<10} {self.LA_unit_print} {new_line}"
f"----------------------------- {new_line}")
print(print_str)
# methods
def update_LA(self,LA_soll):
# update the Leitappartöffnung and consider the restrictions of the closing time of the turbine
LA_diff = self.LA-LA_soll # calculate the difference to the target LA
LA_diff_max = self.d_LA_max_dt*self.dt # calculate the maximum change in LA based on the given timestep
LA_diff = np.sign(LA_diff)*np.min(np.abs([LA_diff,LA_diff_max])) # calulate the correct change in LA
self.set_LA(self.LA-LA_diff,display_warning=False) # set new LA
def set_steady_state(self,ss_flux,ss_pressure):
# calculate and set steady state LA, that allows the flow of ss_flux at ss_pressure through the
# turbine at the steady state LA
ss_LA = self.LA_n*ss_flux/self.Q_n*np.sqrt(self.p_n/ss_pressure)
self.set_LA(ss_LA)
if ss_LA < 0 or ss_LA > 1:
print('LA out of range')
raise Exception('LA out of range [0;1]')
self.set_LA(ss_LA,display_warning=False)

View File

@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@@ -23,18 +23,39 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Turbine has the following attributes: \n",
"----------------------------- \n",
"Type = Francis \n",
"Nominal flux = 0.85 m³/s \n",
"Nominal pressure = 108.09 mWS\n",
"Nominal LA = 100.0 % \n",
"Closing time = -1 s \n",
"Current flux = -1 m³/s \n",
"Current pipe pressure = -1.0 mWS \n",
"Current LA = -1.0 % \n",
"Simulation timestep = -1 s \n",
"----------------------------- \n",
"\n"
]
}
],
"source": [
"Q_nenn = 0.85\n",
"p_nenn = pressure_conversion(10.6,'bar','Pa')\n",
"Untertweng1 = Francis_Turbine(Q_nenn,p_nenn)"
"Untertweng1 = Francis_Turbine(Q_nenn,p_nenn)\n",
"Untertweng1.get_info(full=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@@ -47,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -56,14 +77,14 @@
"Text(0.5, 0, 'Q [m³/s]')"
]
},
"execution_count": 20,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7650cd2a1f9a4b87b98e70add29f11a7",
"model_id": "75adb3cb47e642e3a5606cb41efedf72",
"version_major": 2,
"version_minor": 0
},
@@ -94,8 +115,8 @@
"for i in range(n_p):\n",
" for j in range(n_LA):\n",
" Untertweng1.set_pressure(pp[i,j])\n",
" Untertweng1.set_LA(ll[i,j])\n",
" Q_mat[i,j] = Untertweng1.get_Q()\n",
" Untertweng1.set_LA(ll[i,j],display_warning=False)\n",
" Q_mat[i,j] = Untertweng1.get_current_Q()\n",
"\n",
"fig1 = plt.figure()\n",
"ax1 = plt.axes(projection='3d')\n",
@@ -108,31 +129,23 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\BRANT\\AppData\\Local\\Temp\\9\\ipykernel_7508\\1599598770.py:5: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).\n",
" fig = plt.figure()\n"
]
},
{
"data": {
"text/plain": [
"(0.0, 1.275)"
]
},
"execution_count": 27,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "52152d0b96c74a4ebfb18041a22d8d0e",
"model_id": "224f00f9bf85446b845685a08ed27c68",
"version_major": 2,
"version_minor": 0
},
@@ -170,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -179,14 +192,14 @@
"(0.0, 1.275)"
]
},
"execution_count": 30,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bf6b0fe84d264693813f6e991600ece1",
"model_id": "92741b823c9749c9820ee7b5ba47a6bc",
"version_major": 2,
"version_minor": 0
},
@@ -221,23 +234,6 @@
"plt.title('P = '+ str(p_test2) + ' [Pa]')\n",
"plt.ylim([0,1.5*Q_nenn])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"53000.0\n"
]
}
],
"source": [
"print(pp[10,5])"
]
}
],
"metadata": {

90
Turbinen/messy.ipynb Normal file
View File

@@ -0,0 +1,90 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from Turbinen_class_file import Francis_Turbine\n",
"from mpl_toolkits import mplot3d\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib widget\n",
"\n",
"#importing pressure conversion function\n",
"import sys\n",
"import os\n",
"current = os.path.dirname(os.path.realpath('messy.ipynb'))\n",
"parent = os.path.dirname(current)\n",
"sys.path.append(parent)\n",
"from functions.pressure_conversion import pressure_conversion"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The current attributes are: \n",
"----------------------------- \n",
"Current flux = -1.0 m³/s \n",
"Current pipe pressure = -1.0 mWS \n",
"Current LA = -1.0 % \n",
"----------------------------- \n",
"\n",
"The current attributes are: \n",
"----------------------------- \n",
"Current flux = -1.0 m³/s \n",
"Current pipe pressure = -1.0 mWS \n",
"Current LA = -1.0 % \n",
"----------------------------- \n",
"\n"
]
}
],
"source": [
"Q_nenn = 0.85\n",
"p_nenn = pressure_conversion(10.6,'bar','Pa')\n",
"Untertweng1 = Francis_Turbine(Q_nenn,p_nenn)\n",
"Untertweng2 = Francis_Turbine(Q_nenn,p_nenn)\n",
"\n",
"\n",
"turbines = [Untertweng1,Untertweng2]\n",
"for turbine in turbines:\n",
" turbine.get_info()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.13 ('Georg_DT_Slot3')",
"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.8.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "84fb123bdc47ab647d3782661abcbe80fbb79236dd2f8adf4cef30e8755eb2cd"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}