From 1643157fe83b8c2dba72b2ffe7821766a94ea321 Mon Sep 17 00:00:00 2001 From: robin Date: Sun, 5 May 2024 20:41:59 +0200 Subject: [PATCH] connected kind of --- advection/src/CMakeLists.txt | 42 ------------ advection/src/UVGrid.cpp | 66 ------------------ vtk/src/CMakeLists.txt | 16 +++++ .../src/advection}/AdvectionKernel.h | 2 +- .../src/advection}/EulerAdvectionKernel.cpp | 0 .../src/advection}/EulerAdvectionKernel.h | 0 .../src/advection}/RK4AdvectionKernel.cpp | 0 .../src/advection}/RK4AdvectionKernel.h | 0 vtk/src/advection/UVGrid.cpp | 67 +++++++++++++++++++ {advection/src => vtk/src/advection}/UVGrid.h | 0 {advection/src => vtk/src/advection}/Vel.cpp | 0 {advection/src => vtk/src/advection}/Vel.h | 0 .../src => vtk/src/advection}/interpolate.cpp | 0 .../src => vtk/src/advection}/interpolate.h | 0 {advection/src => vtk/src/advection}/main.cpp | 0 .../src => vtk/src/advection}/readdata.cpp | 0 .../src => vtk/src/advection}/readdata.h | 0 vtk/src/layers/EGlyphLayer.cpp | 26 +------ vtk/src/layers/LGlyphLayer.cpp | 6 +- vtk/src/layers/LGlyphLayer.h | 4 +- vtk/src/main.cpp | 10 ++- 21 files changed, 100 insertions(+), 139 deletions(-) delete mode 100644 advection/src/CMakeLists.txt delete mode 100644 advection/src/UVGrid.cpp rename {advection/src => vtk/src/advection}/AdvectionKernel.h (94%) rename {advection/src => vtk/src/advection}/EulerAdvectionKernel.cpp (100%) rename {advection/src => vtk/src/advection}/EulerAdvectionKernel.h (100%) rename {advection/src => vtk/src/advection}/RK4AdvectionKernel.cpp (100%) rename {advection/src => vtk/src/advection}/RK4AdvectionKernel.h (100%) create mode 100644 vtk/src/advection/UVGrid.cpp rename {advection/src => vtk/src/advection}/UVGrid.h (100%) rename {advection/src => vtk/src/advection}/Vel.cpp (100%) rename {advection/src => vtk/src/advection}/Vel.h (100%) rename {advection/src => vtk/src/advection}/interpolate.cpp (100%) rename {advection/src => vtk/src/advection}/interpolate.h (100%) rename {advection/src => vtk/src/advection}/main.cpp (100%) rename {advection/src => vtk/src/advection}/readdata.cpp (100%) rename {advection/src => vtk/src/advection}/readdata.h (100%) diff --git a/advection/src/CMakeLists.txt b/advection/src/CMakeLists.txt deleted file mode 100644 index b8c8758..0000000 --- a/advection/src/CMakeLists.txt +++ /dev/null @@ -1,42 +0,0 @@ -cmake_minimum_required (VERSION 3.28) -project (Advection) - -set(CMAKE_CXX_STANDARD 23) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -find_package(netCDF REQUIRED) - -add_executable(Advection main.cpp - readdata.cpp - readdata.h - interpolate.cpp - interpolate.h - UVGrid.cpp - UVGrid.h - Vel.h - Vel.cpp - AdvectionKernel.h - EulerAdvectionKernel.cpp - EulerAdvectionKernel.h - RK4AdvectionKernel.cpp - RK4AdvectionKernel.h -) - -execute_process( - COMMAND nc-config --includedir - OUTPUT_VARIABLE NETCDF_INCLUDE_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -execute_process( - COMMAND ncxx4-config --libdir - OUTPUT_VARIABLE NETCDFCXX_LIB_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -target_include_directories(Advection PUBLIC ${netCDF_INCLUDE_DIR}) - -find_library(NETCDF_LIB NAMES netcdf-cxx4 netcdf_c++4 PATHS ${NETCDFCXX_LIB_DIR} NO_DEFAULT_PATH) -target_link_libraries(Advection ${NETCDF_LIB}) diff --git a/advection/src/UVGrid.cpp b/advection/src/UVGrid.cpp deleted file mode 100644 index 1febbfe..0000000 --- a/advection/src/UVGrid.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include - -#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; - } -} \ No newline at end of file diff --git a/vtk/src/CMakeLists.txt b/vtk/src/CMakeLists.txt index ef2a3aa..cf0b9d3 100644 --- a/vtk/src/CMakeLists.txt +++ b/vtk/src/CMakeLists.txt @@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.12 FATAL_ERROR) project(VtkBase) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) find_package(VTK COMPONENTS @@ -47,6 +50,19 @@ add_executable(VtkBase MACOSX_BUNDLE main.cpp commands/SpawnPointCallback.h commands/SpawnPointCallback.cpp CartographicTransformation.cpp + advection/AdvectionKernel.h + advection/EulerAdvectionKernel.cpp + advection/EulerAdvectionKernel.h + advection/interpolate.cpp + advection/interpolate.h + advection/readdata.cpp + advection/readdata.h + advection/RK4AdvectionKernel.cpp + advection/RK4AdvectionKernel.h + advection/UVGrid.cpp + advection/UVGrid.h + advection/Vel.cpp + advection/Vel.h ) execute_process( diff --git a/advection/src/AdvectionKernel.h b/vtk/src/advection/AdvectionKernel.h similarity index 94% rename from advection/src/AdvectionKernel.h rename to vtk/src/advection/AdvectionKernel.h index 0170c79..d8d7674 100644 --- a/advection/src/AdvectionKernel.h +++ b/vtk/src/advection/AdvectionKernel.h @@ -11,7 +11,7 @@ */ class AdvectionKernel { public: - const static int DT = 60 * 15; // 60 sec/min * 15 mins + const static int DT = 15 * 60 * 15; // 60 sec/min * 15 mins /** * 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. diff --git a/advection/src/EulerAdvectionKernel.cpp b/vtk/src/advection/EulerAdvectionKernel.cpp similarity index 100% rename from advection/src/EulerAdvectionKernel.cpp rename to vtk/src/advection/EulerAdvectionKernel.cpp diff --git a/advection/src/EulerAdvectionKernel.h b/vtk/src/advection/EulerAdvectionKernel.h similarity index 100% rename from advection/src/EulerAdvectionKernel.h rename to vtk/src/advection/EulerAdvectionKernel.h diff --git a/advection/src/RK4AdvectionKernel.cpp b/vtk/src/advection/RK4AdvectionKernel.cpp similarity index 100% rename from advection/src/RK4AdvectionKernel.cpp rename to vtk/src/advection/RK4AdvectionKernel.cpp diff --git a/advection/src/RK4AdvectionKernel.h b/vtk/src/advection/RK4AdvectionKernel.h similarity index 100% rename from advection/src/RK4AdvectionKernel.h rename to vtk/src/advection/RK4AdvectionKernel.h diff --git a/vtk/src/advection/UVGrid.cpp b/vtk/src/advection/UVGrid.cpp new file mode 100644 index 0000000..3be14d9 --- /dev/null +++ b/vtk/src/advection/UVGrid.cpp @@ -0,0 +1,67 @@ +#include + +#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 { + cout << "t=" << timeIndex << "latIndex=" << latIndex << "lonIndex=" << lonIndex << endl; + 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; + } +} \ No newline at end of file diff --git a/advection/src/UVGrid.h b/vtk/src/advection/UVGrid.h similarity index 100% rename from advection/src/UVGrid.h rename to vtk/src/advection/UVGrid.h diff --git a/advection/src/Vel.cpp b/vtk/src/advection/Vel.cpp similarity index 100% rename from advection/src/Vel.cpp rename to vtk/src/advection/Vel.cpp diff --git a/advection/src/Vel.h b/vtk/src/advection/Vel.h similarity index 100% rename from advection/src/Vel.h rename to vtk/src/advection/Vel.h diff --git a/advection/src/interpolate.cpp b/vtk/src/advection/interpolate.cpp similarity index 100% rename from advection/src/interpolate.cpp rename to vtk/src/advection/interpolate.cpp diff --git a/advection/src/interpolate.h b/vtk/src/advection/interpolate.h similarity index 100% rename from advection/src/interpolate.h rename to vtk/src/advection/interpolate.h diff --git a/advection/src/main.cpp b/vtk/src/advection/main.cpp similarity index 100% rename from advection/src/main.cpp rename to vtk/src/advection/main.cpp diff --git a/advection/src/readdata.cpp b/vtk/src/advection/readdata.cpp similarity index 100% rename from advection/src/readdata.cpp rename to vtk/src/advection/readdata.cpp diff --git a/advection/src/readdata.h b/vtk/src/advection/readdata.h similarity index 100% rename from advection/src/readdata.h rename to vtk/src/advection/readdata.h diff --git a/vtk/src/layers/EGlyphLayer.cpp b/vtk/src/layers/EGlyphLayer.cpp index cb91825..b591b54 100644 --- a/vtk/src/layers/EGlyphLayer.cpp +++ b/vtk/src/layers/EGlyphLayer.cpp @@ -11,36 +11,12 @@ #include #include #include -#include #include #include "../CartographicTransformation.h" +#include "../advection/readdata.h" -using namespace netCDF; using namespace std; -template -vector getVarVector(const NcVar &var) { - int length = 1; - for (NcDim dim : var.getDims()) { - length *= dim.getSize(); - } - - vector vec(length); - - var.getVar(vec.data()); - - return vec; -} - -tuple, vector, vector> readGrid() { - netCDF::NcFile data("../../../../data/grid.h5", netCDF::NcFile::read); - multimap< string, NcVar > vars = data.getVars(); - vector time = getVarVector(vars.find("times")->second); - vector longitude = getVarVector(vars.find("longitude")->second); - vector latitude = getVarVector(vars.find("latitude")->second); - - return {time, latitude, longitude}; -} EGlyphLayer::EGlyphLayer() { diff --git a/vtk/src/layers/LGlyphLayer.cpp b/vtk/src/layers/LGlyphLayer.cpp index 845d628..9b0840a 100644 --- a/vtk/src/layers/LGlyphLayer.cpp +++ b/vtk/src/layers/LGlyphLayer.cpp @@ -31,7 +31,7 @@ vtkSmartPointer LGlyphLayer::createSpawnPointCallback() { // // TODO: modelling all this in vtkClasses is workable, but ideally i would want to work with a native C++ class. See if this is doable and feasible. -LGlyphLayer::LGlyphLayer() { +LGlyphLayer::LGlyphLayer(std::unique_ptr advectionKernel) { this->ren = vtkSmartPointer::New(); this->ren->SetLayer(2); @@ -39,6 +39,8 @@ LGlyphLayer::LGlyphLayer() { this->data = vtkSmartPointer::New(); this->data->SetPoints(this->points); + advector = std::move(advectionKernel); + auto camera = createNormalisedCamera(); ren->SetActiveCamera(camera); @@ -88,7 +90,7 @@ void LGlyphLayer::updateData(int t) { double point[3]; for (vtkIdType n = 0; n < this->points->GetNumberOfPoints(); n++) { this->points->GetPoint(n, point); - auto [xNew, yNew] = advect(n, point[0], point[1]); + auto [yNew, xNew] = advector->advect(t, point[1], point[0]); this->points->SetPoint(n, xNew, yNew, 0); } this->points->Modified(); diff --git a/vtk/src/layers/LGlyphLayer.h b/vtk/src/layers/LGlyphLayer.h index 22993b4..1a8dc0f 100644 --- a/vtk/src/layers/LGlyphLayer.h +++ b/vtk/src/layers/LGlyphLayer.h @@ -2,6 +2,7 @@ #define LGLYPHLAYER_H #include "Layer.h" +#include "../advection/AdvectionKernel.h" #include "../commands/SpawnPointCallback.h" #include #include @@ -13,13 +14,14 @@ class LGlyphLayer : public Layer { private: vtkSmartPointer points; vtkSmartPointer data; + std::unique_ptr advector; public: /** Constructor. */ - LGlyphLayer(); + LGlyphLayer(std::unique_ptr advectionKernel); /** This function spoofs a few points in the dataset. Mostly used for testing. */ diff --git a/vtk/src/main.cpp b/vtk/src/main.cpp index fb89ea4..88c9bfa 100644 --- a/vtk/src/main.cpp +++ b/vtk/src/main.cpp @@ -7,18 +7,24 @@ #include #include #include +#include #include "layers/BackgroundImage.h" #include "layers/EGlyphLayer.h" #include "layers/LGlyphLayer.h" #include "Program.h" +#include "advection/UVGrid.h" +#include "advection/RK4AdvectionKernel.h" using namespace std; int main() { - auto l = new LGlyphLayer(); - l->spoofPoints(); + shared_ptr uvGrid = std::make_shared(); + auto kernelRK4 = make_unique(uvGrid); + + auto l = new LGlyphLayer(move(kernelRK4)); +// l->spoofPoints(); Program *program = new Program(); program->addLayer(new BackgroundImage("../../../../data/map_661-661.png"));