-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the MultiPurposeSimulator wiki!
This was originally a coursework assignment for an undergraduate physics course. Most of the useful info can be found in README.md.
The original remit was to write a gravity simulator for 2 free bodies by the 4th order Runge-Kutta method. By splitting the problem into four modules, we obtain powerful multifunctional code whereby each module can be swapped out to alter the behaviour without having to rewrite anything. The four modules are as follows:
Simply takes the starting values, copies them into two buffers, previous and next. These are then passed into the interface module which is specified by a function pointer in one of the iterator's arguments. On each iteration, the iterator asks the interface module whether the terminating condition has been reached. If it has not, it swaps the buffers and writes the previous set of values to the specified file. Otherwise it terminates.
This is the function which simply calls whichever solving method is required and passes into it a function pointer to the system of equations' function set. It is also responsible for checking the terminating condition, which is returned to the iterator as either 0 to continue or 1 to stop. Other useful variables can also be calculated here and added to the variable buffer (such as total energy of the system, useful for testing precision).
A single C function representing all of the different functions in the system of differential equations. It takes as its argument a temporary buffer with every variable in the system already transformed by the solution-method function. The other argument specifies the function reference. It returns the computed value.
This is the implementation of the solving method required. It has a function pointer pointing to the function set as one of its arguments, as well as 'in' and 'out' variable buffers. All problems solved in this exercise use the exact same solution-method function, which is an implementation of the 4th order Runge-Kutta method for any number of variables and constants.
Different problems can be solved simply by swapping out modules. In the case of all the problems in this exercise, only the function set and interface modules had to be written. The rest of the code works as-is. There are many benefits to this, and the result is a suite of functions which can be reused bit-for- bit to solve a wide variety of problems.
For efficiency reasons, all of the variables and constants for any given iteration, including temporary variables for passing into the solution-method function, are allocated into the same buffer so that malloc need only be called once before starting a simulation and then freed at the end.