From 2864e70d39797f64f69bfe1412b3a74596febc50 Mon Sep 17 00:00:00 2001 From: robin Date: Thu, 25 Apr 2024 11:07:35 +0200 Subject: [PATCH] euler integration maybe working --- advection/README.md | 21 +-------------------- advection/src/AdvectionKernel.h | 15 +++++++++++++++ advection/src/CMakeLists.txt | 3 +++ advection/src/EulerAdvectionKernel.cpp | 11 +++++++++++ advection/src/EulerAdvectionKernel.h | 17 +++++++++++++++++ advection/src/interpolate.cpp | 6 +++--- advection/src/interpolate.h | 4 ++-- advection/src/main.cpp | 7 ++++--- 8 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 advection/src/AdvectionKernel.h create mode 100644 advection/src/EulerAdvectionKernel.cpp create mode 100644 advection/src/EulerAdvectionKernel.h diff --git a/advection/README.md b/advection/README.md index ec2a268..4774b34 100644 --- a/advection/README.md +++ b/advection/README.md @@ -17,23 +17,4 @@ mkdir build cd build cmake .. make -``` - -### Building with Linux -Makes use of `mdspan` which is not supported by glibc++ at time of writing. See [compiler support](https://en.cppreference.com/w/cpp/compiler_support/23) for `mdspan`. The solution to this is to use Clang and libc++; this is configured in our CMake setup, however the default installation of the `netcdf-cxx` package on at least Arch linux (and suspectedly Debian derivatives as well) specifically builds for the glibc implementation. To get the netcdf C++ bindings functional with the libc++ implementation, one needs to build from source. On Linux, this requires a few changes to the CMake file included with the netcdf-cxx source code, which are detailed below. - -Step-by-step to build the program using clang++ and libc++ on linux: - 1. Download the source code of netcdf-cxx, found at 'https://github.com/Unidata/netcdf-cxx4/releases/tag/v4.3.1' (make sure to download the release source code, as the master branch contains non-compilable code). - 2. Edit the CMakeLists.txt file, by appending '-stdlib=libc++' to the `CMAKE_CXX_FLAGS` variable in line 430. This means line 430 should read: - ```cmake - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wno-unused-variable -Wno-unused-parameter -stdlib=libc++") - ``` - 2. Build the source code with the following: - ```sh - mkdir build && cd build - cmake .. -DCMAKE_CXX_COMPILER=/usr/bin/clang++ - make - ctest - sudo make install - ``` - 3. Now the code should compile through the standard steps described in the Compiling section. +``` \ No newline at end of file diff --git a/advection/src/AdvectionKernel.h b/advection/src/AdvectionKernel.h new file mode 100644 index 0000000..ade97ec --- /dev/null +++ b/advection/src/AdvectionKernel.h @@ -0,0 +1,15 @@ +#ifndef ADVECTION_ADVECTIONKERNEL_H +#define ADVECTION_ADVECTIONKERNEL_H + +#include + +#include "Vel.h" + +#define DT 4000 + +class AdvectionKernel { +public: + virtual std::pair advect(int time, double latitude, double longitude) const = 0; +}; + +#endif //ADVECTION_ADVECTIONKERNEL_H diff --git a/advection/src/CMakeLists.txt b/advection/src/CMakeLists.txt index 79e2a61..ed215aa 100644 --- a/advection/src/CMakeLists.txt +++ b/advection/src/CMakeLists.txt @@ -17,6 +17,9 @@ add_executable(Advection main.cpp UVGrid.h Vel.h Vel.cpp + AdvectionKernel.h + EulerAdvectionKernel.cpp + EulerAdvectionKernel.h ) execute_process( diff --git a/advection/src/EulerAdvectionKernel.cpp b/advection/src/EulerAdvectionKernel.cpp new file mode 100644 index 0000000..83210db --- /dev/null +++ b/advection/src/EulerAdvectionKernel.cpp @@ -0,0 +1,11 @@ + +#include "EulerAdvectionKernel.h" + +using namespace std; + +EulerAdvectionKernel::EulerAdvectionKernel(std::shared_ptr grid) { } + +std::pair EulerAdvectionKernel::advect(int time, double latitude, double longitude) const { + auto [u,v] = (*grid)[time, latitude, longitude]; + return {latitude+u*DT, longitude+v*DT}; +} diff --git a/advection/src/EulerAdvectionKernel.h b/advection/src/EulerAdvectionKernel.h new file mode 100644 index 0000000..f8570f0 --- /dev/null +++ b/advection/src/EulerAdvectionKernel.h @@ -0,0 +1,17 @@ +#ifndef ADVECTION_EULERADVECTIONKERNEL_H +#define ADVECTION_EULERADVECTIONKERNEL_H + +#include "AdvectionKernel.h" +#include "UVGrid.h" + +class EulerAdvectionKernel: public AdvectionKernel { +private: + std::shared_ptr grid; +public: + explicit EulerAdvectionKernel(std::shared_ptr grid); + std::pair advect(int time, double latitude, double longitude) const override; + +}; + + +#endif //ADVECTION_EULERADVECTIONKERNEL_H diff --git a/advection/src/interpolate.cpp b/advection/src/interpolate.cpp index 92b8ac1..70eb19e 100644 --- a/advection/src/interpolate.cpp +++ b/advection/src/interpolate.cpp @@ -2,7 +2,7 @@ using namespace std; -Vel biadvection(const UVGrid &uvGrid, int time, double lat, double lon) { +Vel bilinearinterpolation(const UVGrid &uvGrid, int time, double lat, double lon) { double latStep = uvGrid.latStep(); double lonStep = uvGrid.lonStep(); int timeStep = uvGrid.timeStep(); @@ -36,11 +36,11 @@ Vel biadvection(const UVGrid &uvGrid, int time, double lat, double lon) { return point; } -vector biadvection(const UVGrid &uvGrid, vector> points) { +vector bilinearinterpolation(const UVGrid &uvGrid, vector> points) { vector result; result.reserve(points.size()); for (auto [time, lat, lon]: points) { - result.push_back(biadvection(uvGrid, time, lat, lon)); + result.push_back(bilinearinterpolation(uvGrid, time, lat, lon)); } return result; diff --git a/advection/src/interpolate.h b/advection/src/interpolate.h index 8cda2c3..f7e2924 100644 --- a/advection/src/interpolate.h +++ b/advection/src/interpolate.h @@ -15,7 +15,7 @@ * @param lon longitude of point * @return interpolated velocity */ -Vel biadvection(const UVGrid &uvGrid, int time, double lat, double lon); +Vel bilinearinterpolation(const UVGrid &uvGrid, int time, double lat, double lon); /** * Helper function for bilnearly interpolating a vector of points @@ -23,6 +23,6 @@ Vel biadvection(const UVGrid &uvGrid, int time, double lat, double lon); * @param points vector of points * @return interpolated velocities */ -std::vector biadvection(const UVGrid &uvGrid, std::vector> points); +std::vector bilinearinterpolation(const UVGrid &uvGrid, std::vector> points); #endif //ADVECTION_INTERPOLATE_H diff --git a/advection/src/main.cpp b/advection/src/main.cpp index 258f056..4cd077e 100644 --- a/advection/src/main.cpp +++ b/advection/src/main.cpp @@ -6,8 +6,9 @@ using namespace std; int main() { - UVGrid uvGrid; - uvGrid.streamSlice(cout, 100); +// UVGrid uvGrid; + std::shared_ptr uvGrid = std::make_shared(); + uvGrid->streamSlice(cout, 100); int N = 10000000; // Number of points @@ -32,7 +33,7 @@ int main() { auto start = chrono::high_resolution_clock::now(); - auto x = biadvection(uvGrid, points); + auto x = bilinearinterpolation(*uvGrid, points); auto stop = chrono::high_resolution_clock::now();