In this homework we will solve the wave equation in 1-D through a finite difference approach using Euler's method. The purpose of the assignment is to allow students to compare and benchmark language choice and its impact on time to science using an easy to describe and read, mostly memory coherent application.
By the end of this assignment, you should be able to:
The 1D wave equation can be thought of as a wave propagating along a string.
The wave equation is an second-order linear partial differential equation for the description of waves as they occur in classical physics (for example: water waves, sound waves and seismic waves, light waves). The basic algorithm splits the wave into a grid of points and calculates the point's position, velocity and acceleration. The key incite to making the wave equations work that an individuals point will lead/follow the behavior of that points neighbors.
In the 1D case we will model a line of points in the x-direction such that thy can only move in the y direction. The position for each point $y$ can be calculated by the particle's previous position and the particle's velocity multiplied by the change in time:
$$y_{i+1} = y_i + \dot{y}_idt$$Notation: $$\frac{dy}{dt} = \dot{y} = v = \text{velocity of particle in y direction}$$
We calculation the velocity using the acceleration:
$$\dot{y}_{i+1} = \dot{y}_i + \ddot{y}_idt$$Notation: $$\frac{d^2y}{dt^2} = \frac{dv}{dt} = \ddot{y} = a = \text{acceleration of particle in y direction}$$
Given the above equations, if we know the starting values $y_0$, $\dot{y}_0$, $\ddot{y}_0$ then the only unknown is the equation for how the acceleration changes.
Wave Equation: We will estimate acceleration in time by using acceleration in space. Intuitively we can think about this as any point can estimate where it will be in the future by looking at it's neighbors. Mathematically we show this equation as follows:
$$\frac{d^2y}{dt^2} = \gamma\frac{d^2y}{dx^2}$$We know the acceleration in space using the Euler's finite difference of the particle's position:
To get the above equation we estimate the velocity to the Left and Right (Before and After) the point of interest using Finite Difference:
$$\dot{y}[i]_L = \frac{y[i]-y[i-1]}{dx}$$$$\dot{y}[i]_R = \frac{y[i+1]-y[i]}{dx}$$Second, using the Left and Right Velocity, we use finite difference again to estimate the acceleration:
$$\ddot{y}[i] = \frac{\dot{y}[i]_R - \dot{y}[i]_L}{dx}$$Putting it all together and simplifying we get the followign:
Given the above we can calculate a points position at time step $t+1$ by using it's position/velocity at timestep $t$ and it's neighbors position at time step $t-1$.
✅ DO THIS: Write a 1D wave equation in your program language of choice using the following pseudocode as a guide.
Pseudocode and settings for 1D Wave Equation
Divide simulation into grid in the x direction¶
$xmin = 0; xmax=10; nx=512$
$dx = \frac{xmax-xmin}{nx}$
$x = \text{linspace}(xmin, xmax, nx)$ #Returns a row vector of nx evenly spaced points between xmin and xmax.
Divide time into discrete units¶
$tmin = 0; tmax=10; nt=1000000$
$dt = \frac{tmax-tmin}{nt}$
$times = \text{linespace}(tmin,tmax, nt)$ #Returns a row vector of nt evenly spaced points between tmin and tmax.
Initialize starting position as a simple pulse¶
$y_i = e^{-(x_i-5)^2}$ for all $i \in [0,nx)$
Initialize velocity and acceleration to zero¶
$\dot{y}_i = 0$ for all $i \in [0,nx)$
$\ddot{y}_i = 0$ for all $i \in [0,nx)$
$\gamma = 1$
Run the simulation of t timesteps¶
Loop over index $t$ in $times$:
$\ddot{y}_0 = 0$ # Keep acceleration to zero on ends
$\ddot{y}_{nx-1} = 0$ # Keep acceleration to zero on ends
$\ddot{y}_i \approx \gamma \frac{y_{(i+1)} + y_{(i-1)}-2y_i}{dx^2}$ for all $i \in (0,nx-1)$ # Estimate acceleration using position
$y_i = y_i + \dot{y}_i dt$ for all $i \in [0,nx)$ # Update position
$\dot{y}_{i} = \dot{y}_i+ \ddot{y}_idt$ for all $i \in [0,nx)$ # Update velocity
✅ DO THIS: Conduct a timing study of your 1D wave equation. Make sure you have run it enough times to get accurate results. (You are encouraged to compare timing results if you have different versions of the code and/or if you run your code on different machines).
Write a short (approx. 1-page) report for this homework. Include the report and all of your code/instructions when you turn in the project (ex. Makefiles, library install instructions, etc). Make sure the report answers the following questions:
✅ QUESTION: On average how long did it take to execute the simulation above?
✅ QUESTION: What programming language/version did you use to implement your 1D wave equation?
✅ QUESTION: What instructions are needed for the instructor to reproduce your results? i.e. what commands are needed to compile your code? What libraries (if any) Libraries are needed? Do not assume that the instructor is familiar with your language. Make sure instructions are complete. Write them as if you were giving them to another student in this course or maybe CMSE202.
✅ QUESTION: Describe the computer which you conducted the timing study (CPU speed, RAM, Operating System, software version, Cores used, etc.).
✅ QUESTION: Describe ways you could make your program go faster.
Using your code, generate a visualization of the simulation similar to the animated gif at the top of this notebook. Include this visualization as part of your report.
Create a folder based on your MSU netID and the homework code (For example, mine would be called colbrydi_HW1). Place all of your source code files in the directory. Make sure you include instructions for the instructors to use the code. Do not include compiled code or libraries that can be installed by the instructor.
We are going to use a program call tar
which is used to "Archive" files. Basically tar
will take a group of files and combine them into one file. The command can also be used to zip
or compress the result into a single, compact file that makes it easy to share. We often call these files a tarzip. To tarzip a folder you need to run the following command in the directory that contains your folder:
tar -czvf NAME.tgz FOLDER
The options are as follows:
c
- compress the files into one filez
- zip up the compressed filev
- verbose mode (show us what the program is doing)f NAME.tgz
- file NAME we want to useFOLDER
- The folder with all of our files.For my folder (colbrydi_HW1) the command would look like the following.
tar -czvf colbrydi_HW1.tgz colbrydi_HW1
If you are running in Windows you may or may not have tar
installed. I recommend that you either install tar or upload your files to jupyterhub or the HPCC and tar the files there.
To test if the tar
file works, may a copy of the file in a temporary directory and run the following command to "untar" the file:
tar -xzvf NAME.tgz
The options are as follows:
x
- xtract the file into it's original filesz
- unzip up the compressed filev
- verbose mode (show us what the program is doing)f NAME.tgz
- file NAME we want to useIf everything works, you should see a folder with all of our files. For my folder (colbrydi_HW1) the command would look like the following:
tar -xzvf colbrydi_HW1.tgz
Submit your tgz file to the course Desire2Learn page in the HW1 assignment.
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.