{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A12" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ How does logging behave when you rerun the script without restarting the kernel? What happens if you delete the log-file before you rerun?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Play around with the `datefmt` keyword when configuring the logger to omit displaying year and months." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__3__ Below you find a script that propagates a particle in a harmonic potential numerically (using the euler integrator). You do not need to know about details of the scientific background yet. Just note so much: The script monitors the total energy of the particle at every propagation step. Whenever the change in energy is larger than 0.3, the velocity should be rescaled (multiplied) by a factor of 0.9. This part is not yet included in the script below. If this happens a message should be written to a log file, stating the old and the new speed. Your task is it, to setup a logger and insert a logging statement at the right position in the code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Advanced:** If you are already a bit familar with the background of task 3. Try to implement the euler integrator yourself (and ignore the definition of the `propagate()` function below.\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__4__ Plot the result of the simulation as velocity vs. position." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Example code for task 3\n", "\n", "def force(x):\n", " \"\"\"Compute the instantaneous force acting on the particle\n", " \n", " Args:\n", " x: Current position\n", " x0 (global): Position of the minimum of the potential\n", " k (global): Force constant\n", " \"\"\"\n", " \n", " return -k * (x - x0)\n", "\n", "\n", "def e_pot(x):\n", " \"\"\"Compute the potential energy of the particle\n", " \n", " Args:\n", " x: Current position\n", " x0 (global): Position of the minimum of the potential\n", " k (global): Force constant\n", " \"\"\"\n", "\n", " return 0.5 * k * (x - x0)**2\n", "\n", "\n", "def e_kin(v):\n", " \"\"\"Compute the kinetic energy of the particle\n", " \n", " Args:\n", " v: Particle velocity\n", " m (global): Particle mass\n", " \"\"\"\n", "\n", " return 0.5 * m * v**2\n", "\n", "\n", "def propagate(x, v):\n", " \"\"\"Compute the position of the particle one timestep further\n", " \n", " Args:\n", " x: Current position\n", " v: Current velocity\n", " dt (global): Propagation timestep\n", " m (global): Particle mass\n", " \n", " Returns:\n", " New position and velocity\n", " \"\"\"\n", " \n", " x_new = x + v*dt + force(x)/(2*m) * dt**2\n", " v_new = v + force(x)/m * dt\n", "\n", " return x_new, v_new\n", "\n", "\n", "# Preparing lists to store the computed trajectories\n", "x_t = [] # Position\n", "v_t = [] # Velocity\n", "e_t = [] # Energy\n", "\n", "# Particle/Simulation parameters\n", "x0 = 0 # Position of the potential minimum\n", "k = 1. # Force constant\n", "m = 1. # Particle mass\n", "\n", "dt = 1e-1 # Simulation timestep\n", "T = 10000 # Total number of steps\n", "\n", "x = 1 # starting position\n", "v = -2 # starting velocity\n", "\n", "# Put them in the output lists \n", "x_t.append(x)\n", "v_t.append(v)\n", "e_t.append(e_pot(x) + e_kin(v))\n", "\n", "# Simulate\n", "for i in range(T):\n", " # Update position and velocity\n", " x, v = propagate(x, v)\n", " x_t.append(x)\n", " v_t.append(v)\n", " \n", " # Update total energy\n", " e_t.append(e_pot(x) + e_kin(v)) " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }