From c50a27948f0fb65c53f0843f05e849127a49d38c Mon Sep 17 00:00:00 2001 From: robin Date: Thu, 9 May 2024 08:34:45 +0200 Subject: [PATCH 1/2] feat: made uvgrid take a path as argument --- .../src/advection/UVGrid.cpp | 8 +-- .../src/advection/UVGrid.h | 2 +- .../src/advection/readdata.cpp | 54 ++++++++++--------- .../src/advection/readdata.h | 6 +-- particle-track-and-trace/src/main.cpp | 7 +-- 5 files changed, 41 insertions(+), 36 deletions(-) diff --git a/particle-track-and-trace/src/advection/UVGrid.cpp b/particle-track-and-trace/src/advection/UVGrid.cpp index 393b507..84e9449 100644 --- a/particle-track-and-trace/src/advection/UVGrid.cpp +++ b/particle-track-and-trace/src/advection/UVGrid.cpp @@ -8,14 +8,14 @@ using namespace std; -UVGrid::UVGrid() { - auto us = readHydrodynamicU(); - auto vs = readHydrodynamicV(); +UVGrid::UVGrid(string path) { + auto us = readHydrodynamicU(path); + auto vs = readHydrodynamicV(path); if (us.size() != vs.size()) { throw domain_error(sizeError2); } - tie(times, lats, lons) = readGrid(); + tie(times, lats, lons) = readGrid(path); timeSize = times.size(); latSize = lats.size(); diff --git a/particle-track-and-trace/src/advection/UVGrid.h b/particle-track-and-trace/src/advection/UVGrid.h index 4d3a99a..ef5a99a 100644 --- a/particle-track-and-trace/src/advection/UVGrid.h +++ b/particle-track-and-trace/src/advection/UVGrid.h @@ -11,7 +11,7 @@ private: */ std::vector uvData; public: - UVGrid(); + UVGrid(std::string path); /** * The matrix has shape (timeSize, latSize, lonSize) diff --git a/particle-track-and-trace/src/advection/readdata.cpp b/particle-track-and-trace/src/advection/readdata.cpp index 3597c5b..23194d2 100644 --- a/particle-track-and-trace/src/advection/readdata.cpp +++ b/particle-track-and-trace/src/advection/readdata.cpp @@ -7,44 +7,48 @@ using namespace std; using namespace netCDF; -template +template vector getVarVector(const NcVar &var) { - int length = 1; - for (NcDim dim : var.getDims()) { - length *= dim.getSize(); - } + int length = 1; + for (NcDim dim: var.getDims()) { + length *= dim.getSize(); + } - vector vec(length); + vector vec(length); - var.getVar(vec.data()); + var.getVar(vec.data()); - return vec; + return vec; } -vector readHydrodynamicU() { - // Vs and Us flipped cause the files are named incorrectly - netCDF::NcFile data("../../../../data/hydrodynamic_V.h5", netCDF::NcFile::read); +vector readHydrodynamicU(string path) { + // Vs and Us flipped cause the files are named incorrectly + string fileName = "hydrodynamic_V.h5"; - multimap< string, NcVar > vars = data.getVars(); + netCDF::NcFile data(path + '/' + fileName, netCDF::NcFile::read); - return getVarVector(vars.find("vo")->second); + multimap vars = data.getVars(); + + return getVarVector(vars.find("vo")->second); } -vector readHydrodynamicV() { - // Vs and Us flipped cause the files are named incorrectly - netCDF::NcFile data("../../../../data/hydrodynamic_U.h5", netCDF::NcFile::read); +vector readHydrodynamicV(string path) { + // Vs and Us flipped cause the files are named incorrectly + string fileName = "hydrodynamic_U.h5"; + netCDF::NcFile data(path + '/' + fileName, netCDF::NcFile::read); - multimap< string, NcVar > vars = data.getVars(); + multimap vars = data.getVars(); - return getVarVector(vars.find("uo")->second); + return getVarVector(vars.find("uo")->second); } -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); +tuple, vector, vector> readGrid(string path) { + string fileName = "grid.h5"; + netCDF::NcFile data(path + '/' + fileName, netCDF::NcFile::read); + multimap 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}; + return {time, latitude, longitude}; } diff --git a/particle-track-and-trace/src/advection/readdata.h b/particle-track-and-trace/src/advection/readdata.h index fea3f99..bd7f15d 100644 --- a/particle-track-and-trace/src/advection/readdata.h +++ b/particle-track-and-trace/src/advection/readdata.h @@ -5,18 +5,18 @@ * reads the file hydrodynamic_U.h5 * @return the data vector of us */ -std::vector readHydrodynamicU(); +std::vector readHydrodynamicU(std::string path); /** * reads the file hydrodynamic_V.h5 * @return the data vector of vs */ -std::vector readHydrodynamicV(); +std::vector readHydrodynamicV(std::string path); /** * Reads the file grid.h5 * @return a tuple of (times, latitude, longitude) */ -std::tuple, std::vector, std::vector> readGrid(); +std::tuple, std::vector, std::vector> readGrid(std::string path); #endif //READDATA_H diff --git a/particle-track-and-trace/src/main.cpp b/particle-track-and-trace/src/main.cpp index 131755c..9a6e834 100644 --- a/particle-track-and-trace/src/main.cpp +++ b/particle-track-and-trace/src/main.cpp @@ -23,16 +23,17 @@ using namespace std; int main() { cout << "Reading data..." << endl; - shared_ptr uvGrid = make_shared(); + string dataPath = "../../../../data"; + shared_ptr uvGrid = make_shared(dataPath); auto kernelRK4 = make_unique(uvGrid); auto kernelRK4BoundaryChecked = make_unique(std::move(kernelRK4), uvGrid); cout << "Starting vtk..." << endl; auto l = new LGlyphLayer(uvGrid, std::move(kernelRK4BoundaryChecked)); - l->spoofPoints(); +// l->spoofPoints(); unique_ptr program = make_unique(DT); - program->addLayer(new BackgroundImage("../../../../data/map_661-661.png")); + program->addLayer(new BackgroundImage(dataPath + "/map_661-661.png")); program->addLayer(new EGlyphLayer(uvGrid)); program->addLayer(l); From 9caf43927708552cdeced1eb9cbee441d71115b0 Mon Sep 17 00:00:00 2001 From: robin Date: Thu, 9 May 2024 08:41:38 +0200 Subject: [PATCH 2/2] docs --- .../src/advection/UVGrid.h | 105 +++++++++--------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/particle-track-and-trace/src/advection/UVGrid.h b/particle-track-and-trace/src/advection/UVGrid.h index ef5a99a..2954d96 100644 --- a/particle-track-and-trace/src/advection/UVGrid.h +++ b/particle-track-and-trace/src/advection/UVGrid.h @@ -6,84 +6,89 @@ class UVGrid { private: - /** - * 1D data vector of all the us and vs - */ - std::vector uvData; + /** + * 1D data vector of all the us and vs + */ + std::vector uvData; public: - UVGrid(std::string path); + /** + * Constructs the UVGrid. + * @param path The data path where the constructor expects to find + * the files hydrodynamic_U.h5, hydrodynamic_V.h5, and grid.h5 + */ + UVGrid(std::string path); - /** - * The matrix has shape (timeSize, latSize, lonSize) - */ - size_t timeSize; - size_t latSize; - size_t lonSize; + /** + * 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 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 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; + /** + * Assuming grid is a regular grid, gives the time spacing of grid. + * @return time spacing + */ + int timeStep() const; /** * Returns the lowest longitudinal value of grid. * @return minimum longitude */ - double lonMin() const; + double lonMin() const; /** * Returns the highest longitudinal value of grid. * @return maximum longitude */ - double lonMax() const; + double lonMax() const; /** * Returns the lowest latitudinal value of grid. * @return minimum latitude */ - double latMin() const; + double latMin() const; /** * Returns the higehst latitudinal value of grid. * @return maximum latitude */ - double latMax() const; + double latMax() 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 times; - std::vector lats; - std::vector lons; + /** + * 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 times; + std::vector lats; + std::vector 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; + /** + * 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); + /** + * 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