From e9a04d9ec3561b05998053d148607f1b7abf67ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A9vis=20Morvany?= Date: Tue, 21 Jan 2025 14:21:56 +0100 Subject: [PATCH] Correctly pad filenames in the wave project --- projects/01_wave/openmp/main.cpp | 20 ++++++++++++++------ projects/01_wave/sequential/main.cpp | 20 ++++++++++++++------ projects/01_wave/solution/main.cpp | 20 ++++++++++++++------ 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/projects/01_wave/openmp/main.cpp b/projects/01_wave/openmp/main.cpp index cf1e3f9..3aefd8d 100644 --- a/projects/01_wave/openmp/main.cpp +++ b/projects/01_wave/openmp/main.cpp @@ -24,7 +24,8 @@ void write_grid(const std::vector& U, unsigned int Ny, double dx, double dy, - unsigned int it); + unsigned int it, + unsigned int padding); // __________________________________________________________________________ // @@ -154,6 +155,12 @@ int main(int argc, char* argv[]) { system("rm -rf diags"); system("mkdir -p diags"); + // Maximum number of characters for the iteration number + const unsigned int iteration_number_padding = + number_of_iteration == 0 + ? 1 + : static_cast(std::log10(number_of_iteration)) + 1; + // __________________________________________________________________________ // Print summary of parameters @@ -218,7 +225,7 @@ int main(int argc, char* argv[]) { } } - write_grid(U, Nx, Ny, dx,dy, 0); + write_grid(U, Nx, Ny, dx,dy, 0, iteration_number_padding); // __________________________________________________________________________ @@ -290,7 +297,7 @@ int main(int argc, char* argv[]) { // write the grid to a file if (it % output_period == 0) { - write_grid(U, Nx, Ny, dx,dy, it); + write_grid(U, Nx, Ny, dx,dy, it, iteration_number_padding); } // Print iteration information in the terminal @@ -341,11 +348,12 @@ void write_grid(const std::vector& U, unsigned int Ny, double dx, double dy, - unsigned int it) { + unsigned int it, + unsigned int padding) { // File name of the form grid_0000.json std::stringstream filename(""); - filename << "diags/grid_" << std::setfill('0') << std::setw(4) << it << ".json"; + filename << "diags/grid_" << std::setfill('0') << std::setw(padding) << it << ".json"; std::ofstream file(filename.str()); file << "{\n"; @@ -369,4 +377,4 @@ void write_grid(const std::vector& U, file << "}\n"; file.close(); -} \ No newline at end of file +} diff --git a/projects/01_wave/sequential/main.cpp b/projects/01_wave/sequential/main.cpp index 17d9b78..fc70f90 100644 --- a/projects/01_wave/sequential/main.cpp +++ b/projects/01_wave/sequential/main.cpp @@ -23,7 +23,8 @@ void write_grid(const std::vector& U, unsigned int Ny, double dx, double dy, - unsigned int it); + unsigned int it, + unsigned int padding); // __________________________________________________________________________ // @@ -152,6 +153,12 @@ int main(int argc, char* argv[]) { system("rm -rf diags"); system("mkdir -p diags"); + // Maximum number of characters for the iteration number + const unsigned int iteration_number_padding = + number_of_iteration == 0 + ? 1 + : static_cast(std::log10(number_of_iteration)) + 1; + // __________________________________________________________________________ // Print summary of parameters @@ -210,7 +217,7 @@ int main(int argc, char* argv[]) { } } - write_grid(U, Nx, Ny, dx,dy, 0); + write_grid(U, Nx, Ny, dx,dy, 0, iteration_number_padding); // __________________________________________________________________________ @@ -273,7 +280,7 @@ int main(int argc, char* argv[]) { // write the grid to a file if (it % output_period == 0) { - write_grid(U, Nx, Ny, dx,dy, it); + write_grid(U, Nx, Ny, dx,dy, it, iteration_number_padding); } // Print iteration information in the terminal @@ -322,11 +329,12 @@ void write_grid(const std::vector& U, unsigned int Ny, double dx, double dy, - unsigned int it) { + unsigned int it, + unsigned int padding) { // File name of the form grid_0000.json std::stringstream filename(""); - filename << "diags/grid_" << std::setfill('0') << std::setw(4) << it << ".json"; + filename << "diags/grid_" << std::setfill('0') << std::setw(padding) << it << ".json"; std::ofstream file(filename.str()); file << "{\n"; @@ -350,4 +358,4 @@ void write_grid(const std::vector& U, file << "}\n"; file.close(); -} \ No newline at end of file +} diff --git a/projects/01_wave/solution/main.cpp b/projects/01_wave/solution/main.cpp index fb2b66f..a91d01d 100644 --- a/projects/01_wave/solution/main.cpp +++ b/projects/01_wave/solution/main.cpp @@ -22,7 +22,8 @@ void write_grid(const Kokkos::View::HostMirror& U, unsigned int Ny, double dx, double dy, - unsigned int it); + unsigned int it, + unsigned int padding); // __________________________________________________________________________ // @@ -152,6 +153,12 @@ int main(int argc, char* argv[]) { system("rm -rf diags"); system("mkdir -p diags"); + // Maximum number of characters for the iteration number + const unsigned int iteration_number_padding = + number_of_iteration == 0 + ? 1 + : static_cast(std::log10(number_of_iteration)) + 1; + // __________________________________________________________________________ // Print summary of parameters @@ -227,7 +234,7 @@ int main(int argc, char* argv[]) { Kokkos::fence(); - write_grid(U_host, Nx, Ny, dx,dy, 0); + write_grid(U_host, Nx, Ny, dx,dy, 0, iteration_number_padding); Kokkos::deep_copy(U, U_host); Kokkos::deep_copy(U_prev, U_prev_host); @@ -319,7 +326,7 @@ int main(int argc, char* argv[]) { // write the grid to a file if (it % output_period == 0) { Kokkos::deep_copy(U_host, U); - write_grid(U_host, Nx, Ny, dx,dy, it); + write_grid(U_host, Nx, Ny, dx,dy, it, iteration_number_padding); } // Print iteration information in the terminal @@ -372,11 +379,12 @@ void write_grid(const Kokkos::View::HostMirror& U, unsigned int Ny, double dx, double dy, - unsigned int it) { + unsigned int it, + unsigned int padding) { // File name of the form grid_0000.json std::stringstream filename(""); - filename << "diags/grid_" << std::setfill('0') << std::setw(4) << it << ".json"; + filename << "diags/grid_" << std::setfill('0') << std::setw(padding) << it << ".json"; std::ofstream file(filename.str()); file << "{\n"; @@ -400,4 +408,4 @@ void write_grid(const Kokkos::View::HostMirror& U, file << "}\n"; file.close(); -} \ No newline at end of file +}