forked from FlorisCalkoen/CoastalCodebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add files via upload (FlorisCalkoen#85)
- Loading branch information
1 parent
7e80b17
commit 54c50d8
Showing
2 changed files
with
1,949 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,351 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "fd978930-bbc5-4c62-9218-96af5c11320f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"import panel as pn\n", | ||
"pn.extension(\"ipywidgets\", 'katex', 'mathjax')\n", | ||
"import ipywidgets as ipw\n", | ||
"from matplotlib.animation import FuncAnimation\n", | ||
"from matplotlib.figure import Figure\n", | ||
"\n", | ||
"from random import shuffle, uniform\n", | ||
"\n", | ||
"import sys\n", | ||
"from inspect import signature\n", | ||
"print(\"Packages successfully loaded\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4a18b5d0-538b-45b9-a552-1a6c4865090b", | ||
"metadata": {}, | ||
"source": [ | ||
"# Functions from cookbook" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "81c22d90-98ad-4367-a297-7cf650f73c13", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"try:\n", | ||
" %run Initialize/init_cookbook.ipynb # valid when running the cookbook in the main file\n", | ||
"except:\n", | ||
" %run init_cookbook.ipynb # valid when running the cookbook from this file." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "add2043c-b3cd-4ef8-9da6-9310aee3f2bc", | ||
"metadata": {}, | ||
"source": [ | ||
"# Commonly used functions" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "74b6ba94-f4a6-46f4-8277-8b2e83d9266d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def wave_length(T, h):\n", | ||
" d = h\n", | ||
"\n", | ||
" # based on waveNumber_Fenton(T,d) from Jaime in computerlab\n", | ||
" g=9.81\n", | ||
" omega = 2*np.pi/T\n", | ||
" k0 = omega*omega/g\n", | ||
" alpha = k0*d\n", | ||
" beta = alpha * (np.tanh(alpha))**-0.5 \n", | ||
" k = (alpha + beta**2 * np.cosh(beta)**-2) / (np.tanh(beta)+ beta*np.cosh(beta)**-2) / d\n", | ||
"\n", | ||
" L = 2*np.pi/k\n", | ||
" \n", | ||
" return L\n", | ||
"\n", | ||
"def group_stats(k1,k2,w1,w2):\n", | ||
" Delta_k = np.abs(k2-k1)\n", | ||
" Delta_w = np.abs(w2-w1)\n", | ||
" L = 2*np.pi/Delta_k\n", | ||
" T = 2*np.pi/Delta_w\n", | ||
" cg = Delta_w/Delta_k\n", | ||
" return L,T, cg" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fba1b418-519b-485f-82ac-88d44eac8c32", | ||
"metadata": {}, | ||
"source": [ | ||
"# Part 1B" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4a9a7a70-b3eb-4b11-915a-1c4ff62091e1", | ||
"metadata": {}, | ||
"source": [ | ||
"## Second order waves" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "4aaf78cd-a30a-421e-99e6-90f298fa0095", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def second_order_waves():# second-order_Stokes_waves\n", | ||
" a1 = ipw.FloatText(value=1, min=0, max=20, step=0.01, description='a1 [m]')\n", | ||
" a2 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='a2 [m]')\n", | ||
" phi1 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='phi [2 pi rad]')\n", | ||
" phi2 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='phi [2 pi rad]')\n", | ||
"\n", | ||
" # Setup widget layout (User Interface)\n", | ||
" vbox1 = ipw.VBox([ipw.Label('Wave 1', layout=ipw.Layout(align_self='center')),a1, phi1])\n", | ||
" vbox2 = ipw.VBox([ipw.Label('Wave 2', layout=ipw.Layout(align_self='center')),a2, phi2])\n", | ||
" ui = ipw.HBox([vbox1, vbox2])\n", | ||
" \n", | ||
" def update_graph(a1, a2, phi1, phi2):\n", | ||
" S_min = -0.25*np.pi\n", | ||
" S_max = 2.25*np.pi\n", | ||
" \n", | ||
" S = np.linspace(S_min, S_max,60)\n", | ||
" S1 = S - phi1*np.pi\n", | ||
" S2 = S*2 - phi2*np.pi\n", | ||
" \n", | ||
" eta1 = a1*np.cos(S1)\n", | ||
" eta2 = a2*np.cos(S2)\n", | ||
" eta = eta1 + eta2\n", | ||
" \n", | ||
" fig, axs = plt.subplots(nrows=1,ncols=1,figsize=(5,4), sharex=False, sharey = False)\n", | ||
" axs.plot(S, eta1, label = '$\\eta_1$', color = 'gray', linestyle = 'dashed')\n", | ||
" axs.plot(S, eta2, label = '$\\eta_2$', color = 'gray', linestyle = 'dashdot')\n", | ||
" axs.plot(S, eta, label = '$\\eta_{1+2}$', color = 'k')\n", | ||
" axs.legend(loc = 'best')\n", | ||
" axs.set_ylabel('$\\eta$ [m]')\n", | ||
" axs.set_xlabel('phase [rad]')\n", | ||
" axs.set_xlim(S_min,S_max)\n", | ||
"\n", | ||
" x_tick_min = S_min//(0.5*np.pi)*0.5*np.pi+0.5*np.pi\n", | ||
" x_ticks = np.arange(x_tick_min, S_max, 0.5*np.pi)\n", | ||
" x_ticks_labels = [f\"{angle/np.pi:.1f}π\" for angle in x_ticks]\n", | ||
" axs.set_xticks(x_ticks, x_ticks_labels)\n", | ||
"\n", | ||
" graph = ipw.interactive_output(update_graph, {'a1': a1,'a2': a2, 'phi1':phi1, 'phi2':phi2})\n", | ||
"\n", | ||
" question1 = ['What are the values of eta1, eta2 and phi2 to make figure 5.13 of the book, if we assume phi1 = 0 and a cosinus function, as in equation 1']\n", | ||
" answer1 = [[1, 0.2, 0]]\n", | ||
" subquestions1 = [['eta1', 'eta2', 'phi2 [pi rad]']]\n", | ||
" Q1_unit = 'm'\n", | ||
"\n", | ||
" question2 = ['What are the values of eta1, eta2 and phi2 to make figure 5.16 of the book, if we assume phi1 = 0 and a cosinus function, as in equation 1']\n", | ||
" answer2 = [[1, 0.2, -0.5]]\n", | ||
" subquestions2 = [['eta1', 'eta2', 'phi2 [pi rad]']]\n", | ||
" Q2_unit = 'm'\n", | ||
" \n", | ||
" Q1 = nummerical_subquestions(question1, answer1, subquestions1, Q1_unit)\n", | ||
" Q2 = nummerical_subquestions(question2, answer2, subquestions2, Q2_unit)\n", | ||
" \n", | ||
" display(ui, graph, *Q1,*Q2)\n", | ||
"\n", | ||
"#second_order_waves()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0b556916-ca5d-44a7-ac17-399b7727ef65", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# wt = 0 and k = 2 pi/L, with k = k1, the first harmonic component is the basis.\n", | ||
"def show_second_order_waves_x_L():\n", | ||
" # define the name of the function that the students will make\n", | ||
" function_name = \"second_order_waves_x_L\"\n", | ||
"\n", | ||
" # define the name of the parameter plotted on the horizontal axis\n", | ||
" parameter_x_axis = 'x_L'\n", | ||
"\n", | ||
" # set the horizontal axis of the graph\n", | ||
" horizontal_axis = np.arange(0,3 +1/30,1/30)\n", | ||
"\n", | ||
" # define the correct function and its values along the x-axis.\n", | ||
" def correct_function(x_L, eta1, eta2, phi1, phi2):\n", | ||
" eta = eta1 * np.cos(-2*np.pi*x_L-phi1) + eta2*np.cos(-4*np.pi*x_L-phi2)\n", | ||
" return eta\n", | ||
"\n", | ||
" # set the acceptable computational error (ratio)\n", | ||
" f_margin = 0.001 # 0.001 = 0.01%\n", | ||
"\n", | ||
" fig = Figure((6,3))\n", | ||
" xlabel = 'x/L'\n", | ||
" ylabel = 'y [m]'\n", | ||
" check_code_function(fig, horizontal_axis, function_name, correct_function, parameter_x_axis, f_margin, xlabel = xlabel, ylabel = ylabel)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e6a88191-8120-40f8-90d4-7cccb0d65f8f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def show_second_order_waves_t_T():\n", | ||
" # define the name of the function that the students will make\n", | ||
" function_name = \"second_order_waves_t_T\"\n", | ||
"\n", | ||
" # define the name of the parameter plotted on the horizontal axis\n", | ||
" parameter_x_axis = 't_T'\n", | ||
"\n", | ||
" # set the horizontal axis of the graph\n", | ||
" horizontal_axis = np.arange(0,3 +1/30,1/30)\n", | ||
"\n", | ||
" # define the correct function and its values along the x-axis.\n", | ||
" def correct_function(t_T, eta1, eta2, phi1, phi2):\n", | ||
" eta = eta1 * np.cos(2*np.pi*t_T-phi1) + eta2*np.cos(4*np.pi*t_T-phi2)\n", | ||
" return eta\n", | ||
"\n", | ||
" # set the acceptable computational error (ratio)\n", | ||
" f_margin = 0.001 # 0.001 = 0.01%\n", | ||
"\n", | ||
" fig = Figure((6,3))\n", | ||
" xlabel = 't/T'\n", | ||
" ylabel = 'y [m]'\n", | ||
" check_code_function(fig, horizontal_axis, function_name, correct_function, parameter_x_axis, f_margin, xlabel = xlabel, ylabel = ylabel)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a3264d4c-6d5c-4d98-b8ff-780ba68f98da", | ||
"metadata": {}, | ||
"source": [ | ||
"## second order waves and powers of eta" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7535b9a3-7603-42c6-8b62-cac16547eeba", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def Skewed_assymetric_waves():\n", | ||
" a1 = ipw.FloatText(value=1, min=0, max=20, step=0.01, description='a1 [m]')\n", | ||
" a2_1 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='a2 [m]')\n", | ||
" a2_2 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='a2 [m]')\n", | ||
" phi1 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='phi [2 pi rad]')\n", | ||
" phi2_1 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='phi [2 pi rad]')\n", | ||
" phi2_2 = ipw.FloatText(value=0, min=0, max=20, step=0.01, description='phi [2 pi rad]')\n", | ||
" \n", | ||
" # Setup widget layout (User Interface)\n", | ||
" vbox1 = ipw.VBox([ipw.Label('Wave 1', layout=ipw.Layout(align_self='center')),a1, phi1])\n", | ||
" vbox2 = ipw.VBox([ipw.Label('Wave 2 (left panel)', layout=ipw.Layout(align_self='center')),a2_1, phi2_1])\n", | ||
" vbox3 = ipw.VBox([ipw.Label('Wave 2 (right panel)', layout=ipw.Layout(align_self='center')),a2_2, phi2_2])\n", | ||
" ui = ipw.HBox([vbox1, vbox2, vbox3])\n", | ||
" \n", | ||
" def update_graph(a1, a2_1, a2_2, phi1, phi2_1, phi2_2):\n", | ||
" S_min = -0.25*np.pi\n", | ||
" S_max = 2.25*np.pi\n", | ||
" S = np.linspace(S_min, S_max,100)\n", | ||
"\n", | ||
" # left panel\n", | ||
" eta_L = a1*np.cos(S-phi1) + a2_1*np.cos(S*2-phi2_1*np.pi)\n", | ||
" eta_3_L = eta_L**3\n", | ||
"\n", | ||
" # right panel\n", | ||
" eta_R = a1*np.cos(S-phi1) + a2_2*np.cos(S*2-phi2_2*np.pi)\n", | ||
" eta_3_R = eta_R**3\n", | ||
" \n", | ||
" # set structure for graph\n", | ||
" fig, axs = plt.subplots(nrows=1,ncols=2,figsize=(10,4), sharex=False, sharey = True)\n", | ||
" fig.subplots_adjust(hspace=0)\n", | ||
" fig.subplots_adjust(wspace=0.06)\n", | ||
"\n", | ||
" # plot lines\n", | ||
" axs[0].plot(S, eta_L, label = '$\\eta$', color = 'gray', linestyle = '--')\n", | ||
" axs[0].plot(S, eta_3_L, label = '$\\eta^3$', color = 'k', linestyle = '-')\n", | ||
" axs[0].plot([S_min, S_max],[0,0], color = 'k', linewidth = 1, linestyle = 'dashed')\n", | ||
" \n", | ||
" axs[1].plot(S, eta_R, label = '$\\eta$', color = 'gray', linestyle = '--')\n", | ||
" axs[1].plot(S, eta_3_R, label = '$\\eta^3$', color = 'k', linestyle = '-')\n", | ||
" axs[1].plot([S_min, S_max],[0,0], color = 'k', linewidth = 1, linestyle = 'dashed')\n", | ||
"\n", | ||
" # set legend and labels\n", | ||
" axs[0].legend(loc = 'best')\n", | ||
" axs[0].set_ylabel('$\\eta$ [m]')\n", | ||
" axs[0].set_xlabel('phase [rad]') \n", | ||
" axs[0].set_xlim(S_min,S_max)\n", | ||
"\n", | ||
" axs[1].legend(loc = 'best')\n", | ||
" axs[1].set_xlabel('phase [rad]') \n", | ||
" axs[1].set_xlim(S_min,S_max)\n", | ||
" \n", | ||
" x_tick_min = S_min//(0.5*np.pi)*0.5*np.pi+0.5*np.pi\n", | ||
" x_ticks = np.arange(x_tick_min, S_max, 0.5*np.pi)\n", | ||
" x_ticks_labels = [f\"{angle/np.pi:.1f}π\" for angle in x_ticks]\n", | ||
" axs[0].set_xticks(x_ticks, x_ticks_labels)\n", | ||
" axs[1].set_xticks(x_ticks, x_ticks_labels)\n", | ||
"\n", | ||
" graph = ipw.interactive_output(update_graph, {'a1': a1, 'a2_1': a2_1, 'a2_2':a2_2, 'phi1': phi1, 'phi2_1':phi2_1, 'phi2_2':phi2_2})\n", | ||
"\n", | ||
" question1 = ['What are the values of eta1, phi1, eta2 (left and right pane), phi2 (left and right pane) to make the skewed waves shown in figure 5.14 of the book?']\n", | ||
" answer1 = [[1, 0, 0, 0, 0.2, 0]]\n", | ||
" subquestions1 = [['eta1 [m]', 'phi1 [phi rad]', 'eta2 (left) [m]', 'phi2 (left) [pi rad]', 'eta2 (right) [m]', 'phi2 (right) [pi rad]']]\n", | ||
" Q1_unit = ' '\n", | ||
"\n", | ||
" question2 = ['What are the values of eta1, phi1, eta2 (left and right pane), phi2 (left and right pane) to make the assymetric waves shown in figure 5.17 of the book?']\n", | ||
" answer2 = [[1, 0, 0.2, 0, 0.2, -0.5]]\n", | ||
" subquestions2 = [['eta1 [m]', 'phi1 [phi rad]', 'eta2 (left) [m]', 'phi2 (left) [pi rad]', 'eta2 (right) [m]', 'phi2 (right) [pi rad]']]\n", | ||
" Q2_unit = ' '\n", | ||
"\n", | ||
" Q1 = nummerical_subquestions(question1, answer1, subquestions1, Q1_unit)\n", | ||
" Q2 = nummerical_subquestions(question2, answer2, subquestions2, Q2_unit)\n", | ||
" \n", | ||
" display(ui, graph, *Q1, *Q2)\n", | ||
"\n", | ||
"#Skewed_assymetric_waves()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "32b7cebf-fa50-4122-816d-a9d7fe7877dc", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.