Before we do any coding, it will be helpful to sketch out our tentative strategy for implementing the first version of our app:
- Define the main program. This will define the program name and scope. The main program unit provides a skeleton to hold the declaration of data and the executable code, such as arithmetic, loops, and so on.
- Declare and initialize variables and constants. We need to declare all variables and constants that we intend to use in our program:
- Integer counters
iandn, for space and time, respectively, and corresponding loop dimensionsgrid_sizeandnum_time_steps. The spatial dimension size,grid_size, will determine the length of the arrays, whereas the time dimension size,num_time_steps, will determine for how many iterations we’ll calculate the solution. - Physical constants for background flow speed,
c, time step,dt, and grid spacing,dx. - Arrays with real values for water height,
h, and its finite difference,dh, such thatdh(i)=h(i)-h(i-1)for eachi. The arraydhis necessary for computing the solution without keeping multiple time levels in memory.
- Integer counters
- Calculate the equation solution for a set number of time steps. This step consists of three distinct parts:
- Loop for a set number of time steps (
num_time_steps). - At each step, calculate the new value for water height,
h, based on the value from the previous time step. - Because our domain is limited in size (
grid_size), we need to define the boundary conditions. What happens to the object when it reaches the far right edge of the domain (figure 2.4)?
Figure 2.4 Boundary conditions determine what happens to the object when it reaches an edge of the domain. Should it just leave? Reflect back into the domain like a ball bouncing off a wall? Or perhaps cycle and reappear on the left side?We have a few choices here. The object could be absorbed by the boundary and completely leave the domain without a trace, or it could be reflected back into the domain like a ball bouncing off a wall. Another option is a periodic (or cyclical) boundary condition that connects the right and left edges of the domain. In this case, the object would pass through on the right and reappear on the left. This is a common choice in global atmosphere and ocean prediction because of how our planet is represented in the computational domain. If you go far enough east, you end up in the west! For this reason, we’ll implement the periodic boundary condition in our app. - Loop for a set number of time steps (
- Print the solution to the terminal at each step. To start, we don’t need fancy or specially formatted output. Let’s just output our solution in a default text format to the screen. If we want to store the output in a file for analysis or plotting, we can easily direct the output into a file.
Sound good? Let’s dive in and tackle these one at a time.
Implementation strategy