- Ensure that you have a
C++compiler that supportsC++20 - Ensure that you have installed
cmake(on linux:sudo apt-get install cmake) - Ensure that you have installed either
makeorninjabuild tools (sudo apt-get install makeorsudo apt-get install ninja-build) - Modify the
CMakeUserPresets.jsonfile to point to your C++ compiler and to choose whether to usemakeorninja:{ "version": 3, "cmakeMinimumRequired": { "major": 3, "minor": 19, "patch": 0 }, "configurePresets": [ { "name": "default", "generator": "", // Either "Ninja" or "Unix Makefiles" "cacheVariables": { "CMAKE_CXX_COMPILER": "", // The path to your compiler, for instance "/usr/bin/clang++-19" "CMAKE_EXPORT_COMPILE_COMMANDS": "ON" } } ] } - Now create the usual
cmakeinitialization:This set up for you the build tool filesmkdir build cd build cmake --preset=default .. - To compile:
ninjaofmake
It is already implemented for you, but I want you to make few modifications:
- Automatize the creation of the vector that is given to the TimeInfo structure so that it has points that are multiples of
$\frac{\omega}{6}= \frac{1}{6}\sqrt{\frac{k}{m}}$ , so that we are guaranteed to have always a good enough number of samples. The initial point should always be$0$ , the final point, let us say,$10\omega$ . (Right now, it is sampling at points spaced by$\omega$ , which is not good enough). - Once you have it, modify the callback so that it prints to file: the time, the exact solution, the numerical solution each time is called. (Pay attention that the callback is re-used in the
AdaptiveRungeKutta, hence you will need to close the file after the first solver, and open a new one, with the same file handler, so that you will have two files, one for the non-adaptive and one for the adaptive Runge Kutta). - Import the generated files, let us say you called them
NonAdaptiveResults.datandAdaptiveResults.datinto Mathematica, modifying the correct lines in the notebookComparison.nb. - Do some plots to compare the analytical solution with the numerical one, play around a bit with the data.
- Create a new file
quartic_oscillator.cc - Modify the
CMakeLists.txtfile, by adding a line at the endadd_executable(quartic_oscillator quartic_oscillator.cc) - Create a similar structure as for the harmonic oscillator, but this time you will not have the analytical solution, and you need to add the quartic term to the equation of motion.
- Generate the files, using for the sake of comparison with Mathematica the setup
$k=m=\lambda=1$ . - Import the files in Mathematica and compare the results with the analytical solution of Mathematica.
- Did you finish already? Very good! Now you can take a problem that we saw together, maybe the pendulum or the double pendulum or the second exercise of the self-evaluation test, and try to simulate it!