Code a simple flow solver
Computational analysis of flow problems has become an important piece of equipment in the toolbox for science and engineering. The problem of finding approximate solutions to the equations for fluid flow may be reduced to evaluating a numerical recipe, following from a suitable discretization for the problem. These pages aim to provide a tutorial-styled course on numerical solvers for flow problems. It is not only a step-by-step guide on how to write a flow solver in the C-programming language, also, a discussion on the various solver-design choices is included. C is chosen as it is the perfect didactic language for learning how to program due to its explicit and strict syntax.
Pre-requisites
Apart from time, interest and a good spirit, a few easier-to-come-by assets are needed to follow along with this course. First, you will need to have a computer that provides you with a C compiler. On a Linux system you may open a terminal-emulator (terminal) and type
$ gcc -v
and see something like,
Using built-in specs.
...
gcc version 8.3.0 (Debian 8.3.0-6)
Otherwise you can install it on a Debian-based distro,
$ sudo apt install gcc
If you do not know what the above means, please consult the internet
and search for “Install C compiler on your OS”, where you
replace the italicized text (your OS) with the name of your
operating system. From hereon, I will assume the C compiler is called
gcc
, although (like always) worthwhile alternatives exists.
Furthermore, a so-called text editor is needed. This is a program that
lets you write a plain text file. Examples include
atom, vim
and emacs
. The course will mainly go
between writing in the editor and typing commands in a terminal. If you
prefer a different workflow (e.g. using an IDE), you may need to
translate some instructions.
Chapter 0: systems check
This zeroth chapter introduces (and tests) the workflow. You may skip
it if you are already familiar with C programming. Open your text editor
and create a new file that we will call hello.c
. Type,
#include <stdio.h>
int main() {
("This is not a flow solver");
puts }
The program code consists of an #include
directive which
tells the C compiler to load the standard input-output header file
(<stdio.h>
), a function declaration
(main()
), a corresponding code block that is marked by
curly braces ({ .. }
) and a function call to the “put
string” function (puts
), that takes a text string as an
argument between the round braces ((...)
). Altough the C
compiler knows that puts
only takes a string as an
argument, we need to mark our input string with the double quotation
marks ("..."
). Now we can save and exit the editor and come
back to the terminal, where we compile our code. Make sure you are in
the same folder as where you keep hello.c
,
$ gcc hello.c
The absence of error messages marks a successfully run command. If you do get an error, read it carefully and try to fix it. The generated executable can be run from the command line as well,
$ ./a.out
Any C executable starts with executing the main()
function, so pretty soon you should see appear,
This is not a flow solver
on your terminal screen. Great! Lets move on …
A pdf version exists
Although the web version should give you the best experience, you may also download a pdf version of these pages here.