Found the reason why pressure_surge didn't work

as expected -> unintentional overwrite memory address
This commit is contained in:
Brantegger Georg
2022-06-28 14:58:22 +02:00
parent 16bc55d47a
commit fe6b7c0e6b

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -14,7 +14,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -27,16 +27,16 @@
"D = 1 # pipe diameter [m]\n", "D = 1 # pipe diameter [m]\n",
"Q0 = 2 # initial flow in whole pipe [m³/s]\n", "Q0 = 2 # initial flow in whole pipe [m³/s]\n",
"h = 20 # water level in upstream reservoir [m]\n", "h = 20 # water level in upstream reservoir [m]\n",
"n = 10 # number of pipe segments in discretization\n", "n = 10 # number of pipe segments in discretization\n",
"nt = 11 # number of time steps\n", "nt = 1500 # number of time steps after initial conditions\n",
"f_D = 0.01 # Darcy friction factor\n", "f_D = 0.05 # Darcy friction factor\n",
"c = 400 # propagation velocity of the pressure wave [m/s]\n", "c = 400 # propagation velocity of the pressure wave [m/s]\n",
"\n" "\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -46,14 +46,14 @@
"dt = dx/c # timestep according to method of characterisitics\n", "dt = dx/c # timestep according to method of characterisitics\n",
"nn = n+1 # number of nodes\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", "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", "t_vec = np.arange(0,nt*dt,dt) # time vector\n",
"\n", "\n",
"v0 = Q0/(D**2/4*np.pi)\n", "v0 = Q0/(D**2/4*np.pi)\n",
"p0 = (rho*g*h-v0**2*rho/2)\n", "p0 = (rho*g*h-v0**2*rho/2)\n",
"\n", "\n",
"# storage vectors for old parameters\n", "# storage vectors for old parameters\n",
"v_old = np.full(nn,v0)\n", "v_old = np.full(nn,v0)\n",
"p_old = p0-(f_D*pl_vec/D*rho/2*v0**2) # ref Wikipedia: Rohrreibungszahls\n", "p_old = p0-(f_D*pl_vec/D*rho/2*v0**2) # ref Wikipedia: Darcy Weisbach\n",
"\n", "\n",
"# storage vectors for new parameters\n", "# storage vectors for new parameters\n",
"v_new = np.zeros_like(v_old)\n", "v_new = np.zeros_like(v_old)\n",
@@ -71,20 +71,9 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 9,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"(-5.092958178940651, 5.092958178940651)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"%matplotlib qt\n", "%matplotlib qt\n",
"# plotting preparation\n", "# plotting preparation\n",
@@ -95,34 +84,20 @@
"\n", "\n",
"lo_00, = axs1[0].plot(pl_vec,p_old,marker='.')\n", "lo_00, = axs1[0].plot(pl_vec,p_old,marker='.')\n",
"lo_01, = axs1[1].plot(pl_vec,v_old,marker='.')\n", "lo_01, = axs1[1].plot(pl_vec,v_old,marker='.')\n",
"\n",
"axs1[0].set_ylim([-20*p0,20*p0])\n", "axs1[0].set_ylim([-20*p0,20*p0])\n",
"axs1[1].set_ylim([-2*v0,2*v0])" "axs1[1].set_ylim([-2*v0,2*v0])\n",
"fig1.tight_layout()\n",
"plt.pause(1)\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"[2.54647909 2.54647909 0.03242134 0.02836835 0.02431541 0.02026254\n",
" 0.01620977 0.01215711 0.00810458 0.0040522 0. ]\n",
"9\n",
"[2.54647909 0.03647353 0.03242052 0.02836756 0.02431467 0.02026188\n",
" 0.01620919 0.01215664 0.00810425 0.00405203 0. ]\n",
"10\n",
"[-2.46542799 -2.46568104 -2.46593345 -2.46618518 -2.4664362 -2.46668647\n",
" -2.46693595 -2.46718459 -2.46743236 -2.46767923 0. ]\n"
]
}
],
"source": [ "source": [
"for it in range(nt):\n", "for it in range(1,nt):\n",
"\n",
" # set boundary conditions\n", " # set boundary conditions\n",
" v_new[-1] = 0 # in front of the instantaneously closing valve, the velocity is 0\n", " v_new[-1] = 0 # in front of the instantaneously closing valve, the velocity is 0\n",
" p_new[0] = p0 # hydrostatic pressure from the reservoir\n", " p_new[0] = p0 # hydrostatic pressure from the reservoir\n",
@@ -142,37 +117,33 @@
" -rho*c*f_D*dt/(4*D)*(abs(v_old[i-1])*v_old[i-1]-abs(v_old[i+1])*v_old[i+1])\n", " -rho*c*f_D*dt/(4*D)*(abs(v_old[i-1])*v_old[i-1]-abs(v_old[i+1])*v_old[i+1])\n",
" \n", " \n",
"\n", "\n",
" # prepare for next loop\n",
" # use .copy() to avoid that memory address is overwritten and hell breaks loose :D\n",
" #https://www.geeksforgeeks.org/array-copying-in-python/\n",
" p_old = p_new.copy()\n",
" v_old = v_new.copy()\n",
"\n",
"\n",
" lo_00.set_ydata(p_new)\n", " lo_00.set_ydata(p_new)\n",
" lo_01.set_ydata(v_new)\n", " lo_01.set_ydata(v_new)\n",
" \n", " \n",
" fig1.suptitle(str(it))\n", " fig1.suptitle(str(it))\n",
" fig1.canvas.draw()\n", " fig1.canvas.draw()\n",
" fig1.tight_layout()\n", " fig1.tight_layout()\n",
" plt.pause(0.2)\n", " plt.pause(0.1)\n",
"\n", "\n",
" # store parameters of node 1 (at reservoir)\n", " # store parameters of node 1 (at reservoir)\n",
" p_1[it] = p_old[0]\n", " p_1[it] = p_new[0]\n",
" v_1[it] = v_old[0]\n", " v_1[it] = v_new[0]\n",
" # store parameters of node N+1 (at reservoir)\n", " # store parameters of node N+1 (at reservoir)\n",
" p_np1[it] = p_old[-1]\n", " p_np1[it] = p_new[-1]\n",
" v_np1[it] = v_old[-1]\n", " v_np1[it] = v_new[-1]"
"\n",
" # prepare for next loop\n",
" p_old = p_new\n",
" v_old = v_new\n",
" if it > 7:\n",
" print(it)\n",
" #print(pressure_conversion(p_new, input_unit= 'Pa', target_unit='Bar'))\n",
" print(v_new)\n",
"\n",
"\n",
"\n"
] ]
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3.8.13 ('Georg_DT_Slot3')", "display_name": "Python 3.9.7 ('base')",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },
@@ -186,12 +157,12 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.13" "version": "3.9.7"
}, },
"orig_nbformat": 4, "orig_nbformat": 4,
"vscode": { "vscode": {
"interpreter": { "interpreter": {
"hash": "84fb123bdc47ab647d3782661abcbe80fbb79236dd2f8adf4cef30e8755eb2cd" "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
} }
} }
}, },