From ea23ed9d68210e2d0132f3dad5e4b8b1d93404bf Mon Sep 17 00:00:00 2001 From: robin Date: Tue, 23 Apr 2024 17:41:02 +0200 Subject: [PATCH] removed some functional stuff --- linear-interpolation/src/CMakeLists.txt | 5 ----- linear-interpolation/src/UVGrid.cpp | 20 +++++++++----------- linear-interpolation/src/UVGrid.h | 6 +----- linear-interpolation/src/Vel.cpp | 13 ++++++++++--- linear-interpolation/src/interpolate.cpp | 16 +++++++--------- linear-interpolation/src/main.cpp | 13 ++++++------- linear-interpolation/src/to_vector.h | 24 ------------------------ 7 files changed, 33 insertions(+), 64 deletions(-) delete mode 100644 linear-interpolation/src/to_vector.h diff --git a/linear-interpolation/src/CMakeLists.txt b/linear-interpolation/src/CMakeLists.txt index faeeaa7..9428e41 100644 --- a/linear-interpolation/src/CMakeLists.txt +++ b/linear-interpolation/src/CMakeLists.txt @@ -6,10 +6,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -# Make flags for release compilation -# set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall -DNDEBUG") - - find_package(netCDF REQUIRED) add_executable(LinearInterpolate main.cpp @@ -20,7 +16,6 @@ add_executable(LinearInterpolate main.cpp UVGrid.cpp UVGrid.h Vel.h - to_vector.h Vel.cpp) execute_process( diff --git a/linear-interpolation/src/UVGrid.cpp b/linear-interpolation/src/UVGrid.cpp index f209a12..6673237 100644 --- a/linear-interpolation/src/UVGrid.cpp +++ b/linear-interpolation/src/UVGrid.cpp @@ -2,7 +2,6 @@ #include "UVGrid.h" #include "readdata.h" -#include "to_vector.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" @@ -27,12 +26,11 @@ UVGrid::UVGrid() { throw domain_error(sizeError); } - uvData = to_vector(views::transform(views::zip(us, vs), [](auto pair){return Vel(pair);})); -// uvData = views::zip(us, vs) -// | views::transform([](auto pair) { -// return Vel(pair); -// }) -// | ranges::to(); + 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 { @@ -52,12 +50,12 @@ int UVGrid::timeStep() const { return times[1] - times[0]; } -void UVGrid::printSlice(size_t t) { +void UVGrid::streamSlice(ostream &os, size_t t) { for (int x = 0; x < latSize; x++) { for (int y = 0; y < lonSize; y++) { - auto [u,v] = (*this)[t,x,y]; - printf("%7.4f, %7.4f", u, v); + auto vel = (*this)[t,x,y]; + os << vel << " "; } - cout << endl; + os << endl; } } diff --git a/linear-interpolation/src/UVGrid.h b/linear-interpolation/src/UVGrid.h index 613bd77..6c3f8f6 100644 --- a/linear-interpolation/src/UVGrid.h +++ b/linear-interpolation/src/UVGrid.h @@ -45,11 +45,7 @@ public: */ const Vel& operator[](size_t timeIndex, size_t latIndex, size_t lonIndex) const; - // Friend declaration for the stream insertion operator - friend std::ostream& operator<<(std::ostream& os, const UVGrid& vel); - - void printSlice(size_t t); + void streamSlice(std::ostream &os, size_t t); }; - #endif //LINEARINTERPOLATE_UVGRID_H diff --git a/linear-interpolation/src/Vel.cpp b/linear-interpolation/src/Vel.cpp index 803e8f2..6bb5990 100644 --- a/linear-interpolation/src/Vel.cpp +++ b/linear-interpolation/src/Vel.cpp @@ -1,5 +1,8 @@ #include "Vel.h" #include +#include + +using namespace std; Vel::Vel(double u, double v) : u(u), v(v) {} @@ -27,7 +30,11 @@ Vel Vel::operator/(Scalar scalar) const { return Vel(u / scalar, v / scalar); } -std::ostream& operator<<(std::ostream& os, const Vel& vel) { - os << "(" << vel.u << ", " << vel.v << ")"; +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; -} +} \ No newline at end of file diff --git a/linear-interpolation/src/interpolate.cpp b/linear-interpolation/src/interpolate.cpp index 26c46b3..98fe42d 100644 --- a/linear-interpolation/src/interpolate.cpp +++ b/linear-interpolation/src/interpolate.cpp @@ -1,8 +1,4 @@ -#include -#include - #include "interpolate.h" -#include "to_vector.h" using namespace std; @@ -41,9 +37,11 @@ Vel bilinearInterpolate(const UVGrid &uvGrid, int time, double lat, double lon) } vector bilinearInterpolate(const UVGrid &uvGrid, vector> points) { - auto results = points | std::views::transform([&uvGrid](const auto &point) { - auto [time, lat, lon] = point; - return bilinearInterpolate(uvGrid, time, lat, lon); - }); - return to_vector(results); + vector result; + result.reserve(points.size()); + for (auto [time, lat, lon]: points) { + result.push_back(bilinearInterpolate(uvGrid, time, lat, lon)); + } + + return result; } diff --git a/linear-interpolation/src/main.cpp b/linear-interpolation/src/main.cpp index ebcd487..77f68ff 100644 --- a/linear-interpolation/src/main.cpp +++ b/linear-interpolation/src/main.cpp @@ -7,7 +7,7 @@ using namespace std; int main() { UVGrid uvGrid; - uvGrid.printSlice(100); + uvGrid.streamSlice(cout, 100); int N = 10000000; // Number of points @@ -20,7 +20,6 @@ int main() { double lon_start = -15.875; double lon_end = 12.875; - // Calculate increments double lat_step = (lat_end - lat_start) / (N - 1); double lon_step = (lon_end - lon_start) / (N - 1); int time_step = (time_end - time_start) / (N - 1); @@ -31,22 +30,22 @@ int main() { points.push_back({time_start+time_step*i, lat_start+lat_step*i, lon_start+lon_step*i}); } - auto start = std::chrono::high_resolution_clock::now(); + auto start = chrono::high_resolution_clock::now(); auto x = bilinearInterpolate(uvGrid, points); - auto stop = std::chrono::high_resolution_clock::now(); + auto stop = chrono::high_resolution_clock::now(); - auto duration = std::chrono::duration_cast(stop - start); + auto duration = chrono::duration_cast(stop - start); - printf("Time taken for %d points was %lf seconds\n", N, duration.count()/1000.); + cout << "Time taken for " << N << " points was " << duration.count()/1000. << " seconds\n"; // Do something with result in case of optimisation double sum = 0; for (auto [u,v]: x) { sum += u + v; } - printf("Sum = %lf\n", sum); + cout << "Sum = " << sum << endl; return 0; } diff --git a/linear-interpolation/src/to_vector.h b/linear-interpolation/src/to_vector.h deleted file mode 100644 index 989085b..0000000 --- a/linear-interpolation/src/to_vector.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef TO_VECTOR_H -#define TO_VECTOR_H - -#include -#include - -template -auto to_vector(R&& r) { - std::vector> v; - - // if we can get a size, reserve that much - if constexpr (requires { std::ranges::size(r); }) { - v.reserve(std::ranges::size(r)); - } - - // push all the elements - for (auto&& e : r) { - v.push_back(static_cast(e)); - } - - return v; -} - -#endif