renamed and stuff

This commit is contained in:
robin
2024-05-06 16:45:56 +02:00
parent 3c64364482
commit 59bd2f5a73
35 changed files with 35 additions and 35 deletions

View File

@@ -0,0 +1,31 @@
#ifndef ADVECTIONKERNEL_H
#define ADVECTIONKERNEL_H
#include <tuple>
#include "Vel.h"
/*
* Implement this class for every integration method.
*/
class AdvectionKernel {
public:
/**
* This function must take a time, latitude and longitude of a particle and must output
* a new latitude and longitude after being advected once for AdvectionKernel::DT time as defined above.
* @param time Time since the beginning of the data
* @param latitude Latitude of particle
* @param longitude Longitude of particle
* @return A pair of latitude and longitude of particle.
*/
virtual std::pair<double, double> advect(int time, double latitude, double longitude, int dt) const = 0;
// Taken from Parcels https://github.com/OceanParcels/parcels/blob/daa4b062ed8ae0b2be3d87367d6b45599d6f95db/parcels/tools/converters.py#L155
const static double metreToDegrees(double metre) {
return metre / 1000. / 1.852 / 60.;
}
virtual ~AdvectionKernel() = default; // Apparently I need this, idk why
};
#endif //ADVECTIONKERNEL_H

View File

@@ -0,0 +1,13 @@
#include "EulerAdvectionKernel.h"
#include "interpolate.h"
using namespace std;
EulerAdvectionKernel::EulerAdvectionKernel(std::shared_ptr<UVGrid> grid) : grid(grid) {}
std::pair<double, double> EulerAdvectionKernel::advect(int time, double latitude, double longitude, int dt) const {
auto [u, v] = bilinearinterpolate(*grid, time, latitude, longitude);
return {latitude + metreToDegrees(v * dt), longitude + metreToDegrees(u * dt)};
}

View File

@@ -0,0 +1,25 @@
#ifndef EULERADVECTIONKERNEL_H
#define EULERADVECTIONKERNEL_H
#include "AdvectionKernel.h"
#include "UVGrid.h"
/**
* Implementation of AdvectionKernel.
* The basic equation is:
* new_latitude = latitude + v*DT
* new_longitude = longitude + u*DT
*
* Uses bilinear interpolation as implemented in interpolate.h
*/
class EulerAdvectionKernel: public AdvectionKernel {
private:
std::shared_ptr<UVGrid> grid;
public:
explicit EulerAdvectionKernel(std::shared_ptr<UVGrid> grid);
std::pair<double, double> advect(int time, double latitude, double longitude, int dt) const override;
};
#endif //EULERADVECTIONKERNEL_H

View File

@@ -0,0 +1,35 @@
#include "RK4AdvectionKernel.h"
#include "interpolate.h"
using namespace std;
RK4AdvectionKernel::RK4AdvectionKernel(std::shared_ptr<UVGrid> grid): grid(grid) { }
std::pair<double, double> RK4AdvectionKernel::advect(int time, double latitude, double longitude, int dt) const {
auto [u1, v1] = bilinearinterpolate(*grid, time, latitude, longitude);
// lon1, lat1 = (particle.lon + u1*.5*particle.dt, particle.lat + v1*.5*particle.dt);
double lon1 = longitude + metreToDegrees(u1 * 0.5*dt);
double lat1 = latitude + metreToDegrees(v1 * 0.5*dt);
// (u2, v2) = fieldset.UV[time + .5 * particle.dt, particle.depth, lat1, lon1, particle]
auto [u2, v2] = bilinearinterpolate(*grid, time + 0.5 * dt, lat1, lon1);
// lon2, lat2 = (particle.lon + u2*.5*particle.dt, particle.lat + v2*.5*particle.dt)
double lon2 = longitude + metreToDegrees(u2 * 0.5 * dt);
double lat2 = latitude + metreToDegrees(v2 * 0.5 * dt);
// (u3, v3) = fieldset.UV[time + .5 * particle.dt, particle.depth, lat2, lon2, particle]
auto [u3, v3] = bilinearinterpolate(*grid, time + 0.5 * dt, lat2, lon2);
// lon3, lat3 = (particle.lon + u3*particle.dt, particle.lat + v3*particle.dt)
double lon3 = longitude + metreToDegrees(u3 * dt);
double lat3 = latitude + metreToDegrees(v3 * dt);
// (u4, v4) = fieldset.UV[time + particle.dt, particle.depth, lat3, lon3, particle]
auto [u4, v4] = bilinearinterpolate(*grid, time + dt, lat3, lon3);
double lonFinal = longitude + metreToDegrees((u1 + 2 * u2 + 2 * u3 + u4) / 6.0 * dt);
double latFinal = latitude + metreToDegrees((v1 + 2 * v2 + 2 * v3 + v4) / 6.0 * dt);
return {latFinal, lonFinal};
}

View File

@@ -0,0 +1,22 @@
#ifndef RK4ADVECTIONKERNEL_H
#define RK4ADVECTIONKERNEL_H
#include "AdvectionKernel.h"
#include "UVGrid.h"
/**
* Implementation of Advection kernel using RK4 integration
* See https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods for more details.
* Uses bilinear interpolation as implemented in interpolate.h
*/
class RK4AdvectionKernel: public AdvectionKernel {
private:
std::shared_ptr<UVGrid> grid;
public:
explicit RK4AdvectionKernel(std::shared_ptr<UVGrid> grid);
std::pair<double, double> advect(int time, double latitude, double longitude, int dt) const override;
};
#endif //RK4ADVECTIONKERNEL_H

View File

@@ -0,0 +1,66 @@
#include <ranges>
#include "UVGrid.h"
#include "readdata.h"
#define sizeError2 "The sizes of the hydrodynamic data files are different"
#define sizeError "The sizes of the hydrodynamicU or -V files does not correspond with the sizes of the grid file"
using namespace std;
UVGrid::UVGrid() {
auto us = readHydrodynamicU();
auto vs = readHydrodynamicV();
if (us.size() != vs.size()) {
throw domain_error(sizeError2);
}
tie(times, lats, lons) = readGrid();
timeSize = times.size();
latSize = lats.size();
lonSize = lons.size();
size_t gridSize = timeSize * latSize * lonSize;
if (gridSize != us.size()) {
throw domain_error(sizeError);
}
uvData.reserve(gridSize);
for (auto vel: views::zip(us, vs)) {
uvData.push_back(Vel(vel));
}
}
const Vel &UVGrid::operator[](size_t timeIndex, size_t latIndex, size_t lonIndex) const {
if (timeIndex < 0 or timeIndex >= timeSize
or latIndex < 0 or latIndex >= latSize
or lonIndex < 0 or lonIndex >= lonSize) {
throw std::out_of_range("Index out of bounds");
}
size_t index = timeIndex * (latSize * lonSize) + latIndex * lonSize + lonIndex;
return uvData[index];
}
double UVGrid::lonStep() const {
return lons[1] - lons[0];
}
double UVGrid::latStep() const {
return lats[1] - lats[0];
}
int UVGrid::timeStep() const {
return times[1] - times[0];
}
void UVGrid::streamSlice(ostream &os, size_t t) {
for (int x = 0; x < latSize; x++) {
for (int y = 0; y < lonSize; y++) {
auto vel = (*this)[t, x, y];
os << vel << " ";
}
os << endl;
}
}

View File

@@ -0,0 +1,65 @@
#ifndef UVGRID_H
#define UVGRID_H
#include <vector>
#include "Vel.h"
class UVGrid {
private:
/**
* 1D data vector of all the us and vs
*/
std::vector<Vel> uvData;
public:
UVGrid();
/**
* The matrix has shape (timeSize, latSize, lonSize)
*/
size_t timeSize;
size_t latSize;
size_t lonSize;
/**
* Assuming grid is a regular grid, gives the longitudinal spacing of grid.
* @return longitudinal spacing
*/
double lonStep() const;
/**
* Assuming grid is a regular grid, gives the latitudinal spacing of grid.
* @return latitudinal spacing
*/
double latStep() const;
/**
* Assuming grid is a regular grid, gives the time spacing of grid.
* @return time spacing
*/
int timeStep() const;
/**
* times, lats, lons are vector of length timeSize, latSize, lonSize respectively.
* The maintain the following invariant:
* grid[timeIndex,latIndex,lonIndex] gives the u,v at the point with latitude at lats[latIndex],
* with longitude at lons[lonIndex], and with time at times[timeIndex].
*/
std::vector<int> times;
std::vector<double> lats;
std::vector<double> lons;
/**
* The 3D index into the data. The array is sized by [8761][67][116]
* @return Velocity at that index
*/
const Vel& operator[](size_t timeIndex, size_t latIndex, size_t lonIndex) const;
/**
* Streams a slice at timeIndex t of the matrix to the outstream given by os
* @param os outstream
* @param t index with which to slice matrix
*/
void streamSlice(std::ostream &os, size_t t);
};
#endif //UVGRID_H

View File

@@ -0,0 +1,40 @@
#include "Vel.h"
#include <stdexcept>
#include <iomanip>
using namespace std;
Vel::Vel(double u, double v) : u(u), v(v) {}
Vel::Vel(const std::pair<double, double>& p) : u(p.first), v(p.second) {}
Vel& Vel::operator=(const std::pair<double, double>& p) {
u = p.first;
v = p.second;
return *this;
}
Vel Vel::operator+(const Vel& other) const {
return Vel(u + other.u, v + other.v);
}
Vel& Vel::operator+=(const Vel& other) {
u += other.u;
v += other.v;
return *this;
}
template<typename Scalar>
Vel Vel::operator/(Scalar scalar) const {
if (scalar == 0) throw std::runtime_error("Division by zero");
return Vel(u / scalar, v / scalar);
}
std::ostream& operator<<(ostream& os, const Vel& vel) {
os << "(";
os << fixed << setprecision(2) << setw(5) << vel.u;
os << ", ";
os << fixed << setprecision(2) << setw(5) << vel.v;
os << ")";
return os;
}

View File

@@ -0,0 +1,44 @@
#ifndef VEL_H
#define VEL_H
#include <utility>
#include <stdexcept>
#include <iostream>
#include <format>
class Vel {
public:
double u; // Eastward Current Velocity in the Water Column
double v; // Northward Current Velocity in the Water Column
Vel(double u, double v);
Vel(const std::pair<double, double>& p); // Conversion constructor
Vel& operator=(const std::pair<double, double>& p);
// Operator + to add two Vel objects
Vel operator+(const Vel& other) const;
// Operator += to add another Vel object to this object
Vel& operator+=(const Vel& other);
// Operator * to multiply Vel by a scalar, defined as a member template
template<typename Scalar>
Vel operator*(Scalar scalar) const {
return Vel(u * scalar, v * scalar);
}
// Operator / to divide Vel by a scalar, defined as a member template
template<typename Scalar>
Vel operator/(Scalar scalar) const;
// Friend declaration for the stream insertion operator
friend std::ostream& operator<<(std::ostream& os, const Vel& vel);
};
// Non-member function for scalar multiplication on the left
template<typename Scalar>
Vel operator*(Scalar scalar, const Vel& p) {
return Vel(p.u * scalar, p.v * scalar);
}
#endif //VEL_H

View File

@@ -0,0 +1,47 @@
#include "interpolate.h"
using namespace std;
Vel bilinearinterpolate(const UVGrid &uvGrid, int time, double lat, double lon) {
double latStep = uvGrid.latStep();
double lonStep = uvGrid.lonStep();
int timeStep = uvGrid.timeStep();
int latIndex = (lat - uvGrid.lats[0]) / latStep;
int lonIndex = (lon - uvGrid.lons[0]) / lonStep;
int timeIndex = (time - uvGrid.times[0]) / timeStep;
double timeRatio = (static_cast<double>(time) - uvGrid.times[timeIndex]) / timeStep;
double latRatio = (lat - uvGrid.lats[latIndex]) / latStep;
double lonRatio = (lon - uvGrid.lons[lonIndex]) / lonStep;
Vel point = {0, 0};
for (int timeOffset = 0; timeOffset <= 1; timeOffset++) {
for (int latOffset = 0; latOffset <= 1; latOffset++) {
for (int lonOffset = 0; lonOffset <= 1; lonOffset++) {
auto vertex = uvGrid[
timeIndex + 1 < uvGrid.timeSize ? timeIndex + timeOffset : timeIndex,
latIndex + 1 < uvGrid.latSize ? latIndex + latOffset : latIndex,
lonIndex + 1 < uvGrid.lonSize ? lonIndex + lonOffset : lonIndex
];
double timeRation = (1 - timeOffset) * (1 - timeRatio) + timeOffset * timeRatio;
double latRation = (1 - latOffset) * (1 - latRatio) + latOffset * latRatio;
double lonRation = (1 - lonOffset) * (1 - lonRatio) + lonOffset * lonRatio;
point += timeRation * latRation * lonRation * vertex;
}
}
}
return point;
}
vector<Vel> bilinearinterpolation(const UVGrid &uvGrid, vector<tuple<int, double, double>> points) {
vector<Vel> result;
result.reserve(points.size());
for (auto [time, lat, lon]: points) {
result.push_back(bilinearinterpolate(uvGrid, time, lat, lon));
}
return result;
}

View File

@@ -0,0 +1,28 @@
#ifndef INTERPOLATE_H
#define INTERPOLATE_H
#include <vector>
#include "UVGrid.h"
/**
* Bilinearly interpolate the point (time, lat, lon) to produce the interpolated velocity.
* Since it is in 3D, this means that it interpolates against 8 points (excluding edges).
* As described in https://numerical.recipes/book.html Chapter 3.6
* @param uvGrid velocity grid
* @param time time of point
* @param lat latitude of point
* @param lon longitude of point
* @return interpolated velocity
*/
Vel bilinearinterpolate(const UVGrid &uvGrid, int time, double lat, double lon);
/**
* Helper function for bilnearly interpolating a vector of points
* @param uvGrid velocity grid
* @param points vector of points
* @return interpolated velocities
*/
std::vector<Vel> bilinearinterpolation(const UVGrid &uvGrid, std::vector<std::tuple<int, double, double>> points);
#endif //INTERPOLATE_H

View File

@@ -0,0 +1,50 @@
#include <stdexcept>
#include <netcdf>
#include "readdata.h"
using namespace std;
using namespace netCDF;
template <typename T>
vector<T> getVarVector(const NcVar &var) {
int length = 1;
for (NcDim dim : var.getDims()) {
length *= dim.getSize();
}
vector<T> vec(length);
var.getVar(vec.data());
return vec;
}
vector<double> readHydrodynamicU() {
// Vs and Us flipped cause the files are named incorrectly
netCDF::NcFile data("../../../../data/hydrodynamic_V.h5", netCDF::NcFile::read);
multimap< string, NcVar > vars = data.getVars();
return getVarVector<double>(vars.find("vo")->second);
}
vector<double> readHydrodynamicV() {
// Vs and Us flipped cause the files are named incorrectly
netCDF::NcFile data("../../../../data/hydrodynamic_U.h5", netCDF::NcFile::read);
multimap< string, NcVar > vars = data.getVars();
return getVarVector<double>(vars.find("uo")->second);
}
tuple<vector<int>, vector<double>, vector<double>> readGrid() {
netCDF::NcFile data("../../../../data/grid.h5", netCDF::NcFile::read);
multimap< string, NcVar > vars = data.getVars();
vector<int> time = getVarVector<int>(vars.find("times")->second);
vector<double> longitude = getVarVector<double>(vars.find("longitude")->second);
vector<double> latitude = getVarVector<double>(vars.find("latitude")->second);
return {time, latitude, longitude};
}

View File

@@ -0,0 +1,22 @@
#ifndef READDATA_H
#define READDATA_H
/**
* reads the file hydrodynamic_U.h5
* @return the data vector of us
*/
std::vector<double> readHydrodynamicU();
/**
* reads the file hydrodynamic_V.h5
* @return the data vector of vs
*/
std::vector<double> readHydrodynamicV();
/**
* Reads the file grid.h5
* @return a tuple of (times, latitude, longitude)
*/
std::tuple<std::vector<int>, std::vector<double>, std::vector<double>> readGrid();
#endif //READDATA_H