tips: - If everything was doing what you think it is, your program would work. Do not just assume that any line of code does what you intended for it to do. You must find other ways to check if it is doing what you meant. You will very probably not find your own mistakes by reading your code. - Compile with all warnings turned on. Fix all of the warnings. This takes a little extra time to do, but will save you time later. - Be systematic. Only change one parameter at a time or check one idea at a time. - Always check energy conservation. - Reduce the parameters to a simple (limiting) case where you know exactly what will happen: one-dimensional, only 2 particles, particular initial conditions (such as those that will lead to head on collisions), very large box size, etc. This way, you can see more easily what is going wrong. - If it works for the simple cases, introduce more typical parameter values one at a time. Start the particles with different velocities, make a shearing instead of a head-on collision, go to three particles, etc. - Print lots of different things (time, positions, velocities, etc.) to check that they make sense and plot lots of things. This will help you identify the symptoms of the bug. If you are comparing the output for different parameters, look at it side by side. - Narrow down where in your code the problem is by checking small parts of it manually numerically. - Try to narrow down what function or subroutine the problem is in. - Check if your problem depends on the time-step size. - Use programs like valgrind or cachegrind to search for errors. - If you are truly stuck, come to me for help. Make sure to investigate as much as possible of the symptoms of your bugs beforehand, plot things, try things. But do ask for help. Do not allow yourself to be stuck for a long time. This is frustrating and unproductive. - Do not code or debug while extremely tired (or intoxicated). You will just make it worse. You can easily spend 3 hours in the middle of the night not finding a bug that would take 5 minutes to identify in the morning after a good night's sleep. common bugs: - missing/extra minus signs (for example when applying a force to a particle) - messing up the order of the indices of a high-dimensional array - typo: something*2 instead of something**2 - typo: "=" instead of "+=", "-=", or "*=" - typo: "*" instead of "/", etc - forgetting to divide forces by the mass - implementing the nearest image convention only for the force, not the potential energy (or the other way around, or only for one direction) - mixed-up units - forgetting to free allocated memory - forgotting type declarations (this should produce compiler errors) - neglecting to set the box size correctly - reading/writing beyond the end of an array - setting a sum to zero at the wrong point in a (double) loop, or not at all - incorrect or inappropriate use of fabs() or fmod() other problems that could lead to strange results: - not enough statistics: not enough particles or only taking statistics once, not a few times during the simulation - funny initial conditions, such as particles placed on top of eachother (at the periodic boundary for instance) - simulation box too small for the length scales you are looking at - system not equilibrated properly yet