From a88d3f0b98051b4fff9d04e7fd2dbb54c5fadb0a Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:03:36 +0000 Subject: [PATCH 1/9] Create MountainRangeBasic.hpp by removing extra items from MountainRange.hpp --- src/MountainRangeBasic.hpp | 113 +++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/MountainRangeBasic.hpp diff --git a/src/MountainRangeBasic.hpp b/src/MountainRangeBasic.hpp new file mode 100644 index 0000000..e26c4d8 --- /dev/null +++ b/src/MountainRangeBasic.hpp @@ -0,0 +1,113 @@ +#pragma once +#include +#include +#include +#include +#include +#include + + + +// This header containes the Basic MountainRange class. It is a simplification of the MountainRange class. + + +// Basic MountainRange. Derived classes can override write, dsteepness, and step. +class MountainRangeBasic { +public: + using size_type = size_t; + using value_type = double; + + +protected: + // Parameters and members + static constexpr const value_type default_dt = 0.01; + static constexpr const size_t header_size = sizeof(size_type) * 2 + sizeof(value_type); + const size_type ndims, cells; + value_type t; + std::vector r, h, g; + + + +public: + // Accessors + auto size() const { return cells; } + auto sim_time() const { return t; } + auto &uplift_rate() const { return r; } + auto &height() const { return h; } + + + +protected: + // Basic constructor + MountainRange(auto ndims, auto cells, auto t, const auto &r, const auto &h): ndims{ndims}, cells{cells}, t{t}, + r(r), h(h), g(h) { + if (ndims != 1) handle_wrong_dimensions(); + step(0); // initialize g + } + + // Error handlers for I/O constructors + static void handle_wrong_dimensions() { + throw std::logic_error("Input file is corrupt or multi-dimensional, which this implementation doesn't support"); + } + + +public: + // Build a MountainRange from an uplift rate and a current height + MountainRange(const auto &r, const auto &h): MountainRange(1ul, r.size(), 0.0, r, h) {} + + +protected: + // Helpers for step and dsteepness + constexpr void update_g_cell(auto i) { + auto L = (h[i-1] + h[i+1]) / 2 - h[i]; + g[i] = r[i] - pow(h[i], 3) + L; + } + + constexpr void update_h_cell(auto i, auto dt) { + h[i] += g[i] * dt; + } + + constexpr value_type ds_cell(auto i) const { + return ((h[i-1] - h[i+1]) * (g[i-1] - g[i+1])) / 2 / (cells - 2); + } + + +public: + // Calculate the steepness derivative + virtual value_type dsteepness() { + value_type ds = 0; + for (size_t i=1; i std::numeric_limits::epsilon()) { + step(dt); + } + + // Return total simulation time + return t; + } +}; From 1f8baa9e01e04e4a4334c23c0ec0bf38471a2844 Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:10:43 +0000 Subject: [PATCH 2/9] Combine constructors --- src/MountainRangeBasic.hpp | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/MountainRangeBasic.hpp b/src/MountainRangeBasic.hpp index e26c4d8..56e9a04 100644 --- a/src/MountainRangeBasic.hpp +++ b/src/MountainRangeBasic.hpp @@ -27,7 +27,6 @@ class MountainRangeBasic { std::vector r, h, g; - public: // Accessors auto size() const { return cells; } @@ -35,25 +34,10 @@ class MountainRangeBasic { auto &uplift_rate() const { return r; } auto &height() const { return h; } - - -protected: - // Basic constructor - MountainRange(auto ndims, auto cells, auto t, const auto &r, const auto &h): ndims{ndims}, cells{cells}, t{t}, - r(r), h(h), g(h) { - if (ndims != 1) handle_wrong_dimensions(); - step(0); // initialize g - } - - // Error handlers for I/O constructors - static void handle_wrong_dimensions() { - throw std::logic_error("Input file is corrupt or multi-dimensional, which this implementation doesn't support"); - } - - -public: // Build a MountainRange from an uplift rate and a current height - MountainRange(const auto &r, const auto &h): MountainRange(1ul, r.size(), 0.0, r, h) {} + MountainRange(const auto &r, const auto &h) : ndims{1ul}, cells{r.size()}, t{0.0}, r{r}, h{h} { + step(0); + } protected: From ee2bb98a6c7477bbda613174ffe7a37962042049 Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:13:28 +0000 Subject: [PATCH 3/9] Remove unnecessary headers --- src/MountainRangeBasic.hpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/MountainRangeBasic.hpp b/src/MountainRangeBasic.hpp index 56e9a04..072244a 100644 --- a/src/MountainRangeBasic.hpp +++ b/src/MountainRangeBasic.hpp @@ -1,11 +1,6 @@ #pragma once #include -#include -#include -#include #include -#include - // This header containes the Basic MountainRange class. It is a simplification of the MountainRange class. From 4b7e44971a075333386eedb918d89232fa70d197 Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:19:15 +0000 Subject: [PATCH 4/9] Update driver code to use MountainRangeBasic class --- src/initial.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/initial.cpp b/src/initial.cpp index 7718710..ebc6265 100644 --- a/src/initial.cpp +++ b/src/initial.cpp @@ -1,13 +1,10 @@ #include #include -#include "MountainRange.hpp" - +#include "MountainRangeBasic.hpp" // Run a basic mountain range simulation and print its simulation time - - int main() { // Simulation parameters size_t len = 1000; @@ -18,7 +15,7 @@ int main() { std::vector r(len), h(len); std::fill(r.begin()+plateau_start, r.begin()+plateau_end, value); h[0] = 1; - auto m = MountainRange(r, h); + auto m = MountainRangeBasic(r, h); // Solve and return std::cout << m.solve() << std::endl; From c5f0e22c98b36ea050de8f7bf65851e06ae241d8 Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:23:14 +0000 Subject: [PATCH 5/9] Update CMakeLists and README with new references --- CMakeLists.txt | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d055fe..bcaf674 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ endif() add_executable(mountaindiff src/mountaindiff.cpp ${COMMON_INCLUDES}) # initial -add_executable(initial src/initial.cpp ${COMMON_INCLUDES}) +add_executable(initial src/initial.cpp src/MountainRangeBasic.hpp) # mountainsolve_serial add_executable(mountainsolve_serial src/mountainsolve.cpp ${COMMON_INCLUDES}) diff --git a/README.md b/README.md index 58a3e14..a6cfcd2 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ The other binaries mirror those that will be built for the C++ phases of the pro | Corresponding phase | Binary | Source files | | --- | --- | --- | -| [Phase 1](https://byuhpc.github.io/sci-comp-course/project/phase1) | `initial` | [initial](src/initial.cpp), [MountainRange](src/MountainRange.hpp) | +| [Phase 1](https://byuhpc.github.io/sci-comp-course/project/phase1) | `initial` | [initial](src/initial.cpp), [MountainRangeBasic](src/MountainRangeBasic.hpp) | | [Phase 2](https://byuhpc.github.io/sci-comp-course/project/phase2) | `mountainsolve_serial`* | [MountainRange](src/MountainRange.hpp) | | [Phase 3](https://byuhpc.github.io/sci-comp-course/project/phase3) | `mountainsolve_openmp` | [MountainRange](src/MountainRange.hpp) | | [Phase 6](https://byuhpc.github.io/sci-comp-course/project/phase6) | `mountainsolve_thread` | [MountainRangeThreaded](src/MountainRangeThreaded.hpp) | From 3f5a4b74dac071e43772c4807b0f5e9abdd17eb0 Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:25:04 +0000 Subject: [PATCH 6/9] Fix compilation errors --- src/MountainRangeBasic.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/MountainRangeBasic.hpp b/src/MountainRangeBasic.hpp index 072244a..3505b28 100644 --- a/src/MountainRangeBasic.hpp +++ b/src/MountainRangeBasic.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include @@ -30,7 +31,7 @@ class MountainRangeBasic { auto &height() const { return h; } // Build a MountainRange from an uplift rate and a current height - MountainRange(const auto &r, const auto &h) : ndims{1ul}, cells{r.size()}, t{0.0}, r{r}, h{h} { + MountainRangeBasic(const auto &r, const auto &h): ndims{1ul}, cells{r.size()}, t{0.0}, r{r}, h{h} { step(0); } From f539c5db5dfc798e4e40b1bd5a93391cf5fd8232 Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:29:45 +0000 Subject: [PATCH 7/9] Further simplifications... - Remove header_size calculations - Remove ability to configure default_dt --- src/MountainRangeBasic.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/MountainRangeBasic.hpp b/src/MountainRangeBasic.hpp index 3505b28..df03c84 100644 --- a/src/MountainRangeBasic.hpp +++ b/src/MountainRangeBasic.hpp @@ -17,7 +17,6 @@ class MountainRangeBasic { protected: // Parameters and members static constexpr const value_type default_dt = 0.01; - static constexpr const size_t header_size = sizeof(size_type) * 2 + sizeof(value_type); const size_type ndims, cells; value_type t; std::vector r, h, g; @@ -80,11 +79,11 @@ class MountainRangeBasic { // Step until dsteepness() falls below 0, checkpointing along the way - value_type solve(value_type dt=default_dt) { + value_type solve() { // Solve loop while (dsteepness() > std::numeric_limits::epsilon()) { - step(dt); + step(default_dt); } // Return total simulation time From 99657fe7b522f91748b21c201c254684d80d5770 Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:45:35 +0000 Subject: [PATCH 8/9] Convert MountainRange SD to represent MountainRangeBasic --- ....md => MountainRangeBasic-sequence-diagram.md} | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) rename docs/{MountainRange-sequence-diagram.md => MountainRangeBasic-sequence-diagram.md} (78%) diff --git a/docs/MountainRange-sequence-diagram.md b/docs/MountainRangeBasic-sequence-diagram.md similarity index 78% rename from docs/MountainRange-sequence-diagram.md rename to docs/MountainRangeBasic-sequence-diagram.md index d128a9e..38ae439 100644 --- a/docs/MountainRange-sequence-diagram.md +++ b/docs/MountainRangeBasic-sequence-diagram.md @@ -1,21 +1,18 @@ -# Mountain Range Threaded — Sequence Diagram +# Mountain Range Basic — Sequence Diagram > [!IMPORTANT] > This diagram relies on [Mermaid diagrams](https://mermaid.js.org/) which display properly when rendered within GitHub. > -> It may not work properly when rendered within other websites. [Click here to view the source](https://github.com/BYUHPC/sci-comp-course-example-cxx/blob/main/docs/MountainRange-sequence-diagram.md). +> It may not work properly when rendered within other websites. [Click here to view the source](https://github.com/BYUHPC/sci-comp-course-example-cxx/blob/main/docs/MountainRangeBasic-sequence-diagram.md). ## Intro -This [sequence diagram](https://mermaid.js.org/syntax/sequenceDiagram.html#sequence-diagrams) written with Mermaid visually represents -the calls and work being performed in the `MountainRange` example. +This [sequence diagram](https://mermaid.js.org/syntax/sequenceDiagram.html#sequence-diagrams) written with Mermaid visually represents the calls and work being performed in the `MountainRange` example. -It is designed to help visualize the relationships between -the various entities involved in running the program. The close reader will observe stacked activation functions representing calls -to methods on the base or subclass of the `MountainRange` object. +It is designed to help visualize the relationships between the various entities involved in running the program. The code covered by this diagram exists in two separate example files: -* [MountainRange.hpp](../src/MountainRange.hpp) (base class) +* [MountainRangeBasic.hpp](../src/MountainRangeBasic.hpp) (base class) * [initial.cpp](../src/initial.cpp) (driver code) ## Diagram @@ -28,7 +25,7 @@ title: Mountain Range — Sequence Diagram sequenceDiagram participant main -participant MR as MountainRange +participant MR as MountainRangeBasic participant cout as std::cout note left of main: Program starts From cca06696aecbb30fc3314cda4fbde7df34afbd4d Mon Sep 17 00:00:00 2001 From: James Finlinson Date: Fri, 10 Jan 2025 18:57:04 +0000 Subject: [PATCH 9/9] Add temporary symlink so original filename continues to have content --- docs/MountainRange-sequence-diagram.md | 1 + 1 file changed, 1 insertion(+) create mode 120000 docs/MountainRange-sequence-diagram.md diff --git a/docs/MountainRange-sequence-diagram.md b/docs/MountainRange-sequence-diagram.md new file mode 120000 index 0000000..8657d74 --- /dev/null +++ b/docs/MountainRange-sequence-diagram.md @@ -0,0 +1 @@ +MountainRangeBasic-sequence-diagram.md \ No newline at end of file