Skip to content

slrodini/cl_exercises_template

Repository files navigation

Setup

  1. Ensure that you have a C++ compiler that supports C++20
  2. Ensure that you have installed cmake (on linux: sudo apt-get install cmake)
  3. Ensure that you have installed either make or ninja build tools (sudo apt-get install make or sudo apt-get install ninja-build)
  4. Modify the CMakeUserPresets.json file to point to your C++ compiler and to choose whether to use make or ninja:
    {
       "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"
             }
          }
       ]
    }
  5. Now create the usual cmake initialization:
    mkdir build
    cd build
    cmake --preset=default ..
    This set up for you the build tool files
  6. To compile: ninja of make

Your exercises

Harmonic oscillator

It is already implemented for you, but I want you to make few modifications:

  1. 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).
  2. 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).
  3. Import the generated files, let us say you called them NonAdaptiveResults.dat and AdaptiveResults.dat into Mathematica, modifying the correct lines in the notebook Comparison.nb.
  4. Do some plots to compare the analytical solution with the numerical one, play around a bit with the data.

Quartic Oscillator

  1. Create a new file quartic_oscillator.cc
  2. Modify the CMakeLists.txt file, by adding a line at the end add_executable(quartic_oscillator quartic_oscillator.cc)
  3. 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.
  4. Generate the files, using for the sake of comparison with Mathematica the setup $k=m=\lambda=1$.
  5. Import the files in Mathematica and compare the results with the analytical solution of Mathematica.

Extras

  1. 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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published