Write a program that solves the following unidirectional flow problem for the unknown velocity v(x,t):
P.D.E.:
![]()
Boundary conditions:
![]()
Initial condition:
![]()
You first need to create an input file ftcs.in for your program with the data for the problem to be solved. In the file, initially insert the following values of the variables followed by their descriptions:
0.3 coefficient of viscosity nu 2.0 length l 3.0 magnitude of the parabolic part of the initial velocity V_0 2.0 velocity at the left end V_1 1.0 velocity at the right end V_2 4 maximum value of spatial index j (i.e. number of mesh cells) j_max 0.5 nondimensional mesh viscosity number Nm = nu dt / dx^2 0.5 time interval between doing output t_out
As far as
is concerned, your program
will use this value to compute the time step. The scheme is stable if
.
As far as
is concerned, your program should output
the velocity distribution v as a function of x at times
and
. Then use the software of your choice to plot these
5 velocity curves in a single v,x-plot.
Now write the program. Give the program file the name ftcs.c, ftcs.cpp, or ftcs.f, depending on what language you are using. The structure of the program should be as follows, in order of appearance:
In Fortran, the first lines should be a program statement, as well as an implicit none statement, followed by a declaration of an integer j_dim followed by a parameter statement making j_dim a parameter with a value equal to the maximum number of mesh cells that you would ever want to use, say 1,000.
You also want to declare a variable Nm to contain the value
of the nondimensional group
.
You also need to declare storage for the velocities to be computed. In C or C++, declare the storage as float v[j_dim][2], while in Fortran, declare it as real v(0:j_dim,0:1). Note from this that only two time planes are stored. In particular, any velocity vnj must be stored as v[j][n%2] in C/C++ and as v(j,mod(n,2)) in Fortran during the computations. When computing new times, the new velocities overwrite the older values.
You may also want to declare an array x of dimension j_dim to hold the mesh x-values and a variable t to hold time.
You also need to declare a variable n_out which is the number of time steps to do before doing output and a corresponding time interval t_out
Each declaration must be preceded by a comment line clearly describing what the variable respresents to anyone who might read the program.
For the remaining parts of the program, precede every small thing you do by a comment line explaining what you are doing.
Print each value out to the output file, with its description, as it is read in from the input file. Check each value read in for acceptability. Is nu greater then zero? Is j_max greater than 1? Is it less than j_dim, so that we have enough storage to store all values computed? Etcetera. If an unacceptable value is found, print out a message what precisely is wrong and terminate program execution.
You probably also want to open some plot file for the plot of the velocities v at the requested 5 times against x.
Write these initial velocities also to your plot file.
To your output file, write a single line saying that the current time t=0, followed by the minimum and maximum initial temperatures.
The subparts inside this n-loop will be
![]()
Run your program and plot the results using your favorite plotting program. Plot the computed velocities as individual points, not as a line. Best is to use a different plot symbol for each of the five times.
Increase the value of j_max by a factor 2 and see what happens to the computed solution. Increase it by another factor 2 and see.
Now change back to the original j_max and make the value of Nm equal to 0.25. What happens to the solution?
Now make Nm equal to 1. What happens to the solution? Try increasing j_max to get more accuracy. Does it help?
Comment on all results.