-
Notifications
You must be signed in to change notification settings - Fork 63
Burgers1D example in C++ #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mdumett
merged 25 commits into
csrc-sdsu:master
from
arrowguy234:Poisson2D-and-burgers1D_
Apr 28, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
abccdf3
Add files via upload
arrowguy234 4356439
Delete examples/cpp/pois2D (2).cpp
arrowguy234 4d4e651
Add files via upload
arrowguy234 f72172c
Update Makefile
arrowguy234 3070ffe
Update Makefile
arrowguy234 7c3b77b
Update Burgers1D.cpp
arrowguy234 e348092
Update Burgers1D.cpp
arrowguy234 ca8090d
Update pois2D.cpp
arrowguy234 cd4cb4d
Delete examples/cpp/Makefile
arrowguy234 e9bd8ea
Merge branch 'csrc-sdsu:master' into Poisson2D-and-burgers1D_
arrowguy234 d441aae
Update Burgers1D.cpp
arrowguy234 aa430a5
Delete examples/cpp/pois2D.cpp
arrowguy234 00d61a0
Add files via upload
arrowguy234 570e908
Update Burgers1D.cpp
arrowguy234 3a8b89b
Delete examples/cpp/Poisson2D.cpp
arrowguy234 2ebe837
Update utils.cpp
arrowguy234 5e85509
Update utils.h
arrowguy234 c51675f
Update Burgers1D.cpp
arrowguy234 982b559
Update utils.cpp
arrowguy234 a587a33
Update utils.h
arrowguy234 92485cc
Merge branch 'csrc-sdsu:master' into Poisson2D-and-burgers1D_
arrowguy234 85ff8ce
Update Burgers1D.cpp
arrowguy234 1f891fc
Merge branch 'csrc-sdsu:master' into Poisson2D-and-burgers1D_
arrowguy234 0ede582
Update Burgers1D.cpp
arrowguy234 220cdc2
Update Burgers1D.cpp
arrowguy234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
|
|
||
| /** | ||
| * Solving the 1D Advection Equation using a Mimetic Finite Difference Scheme | ||
| * | ||
| * Equation: ∂U/∂t + ∂(U²)/∂x = 0 (Nonlinear Burgers' Equation in conservative form) | ||
| * Domain: x ∈ [-15, 15] with m = 300 grid cells | ||
| * Time: Simulated until t = 10.0 with time step dt = dx (CFL condition) | ||
| * Initial Condition: U(x,0) = exp(-x² / 50) | ||
| * Boundary Conditions: Mimetic divergence and interpolation operators applied (implicit treatment) | ||
| * | ||
| * Solution is computed using a staggered grid approach, explicit time-stepping, | ||
| * and mimetic finite difference operators for divergence and interpolation. | ||
| */ | ||
| #include <armadillo> | ||
| #include <cmath> | ||
| #include <cstdlib> // for EXIT_SUCCESS / EXIT_FAILURE | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <iomanip> | ||
| #include "mole.h" | ||
| #include "utils.h" | ||
|
|
||
| int main() { | ||
| constexpr double west = -15.0; | ||
| constexpr double east = 15.0; | ||
| constexpr int k = 2; | ||
| constexpr int m = 300; | ||
| constexpr double t = 10.0; | ||
|
|
||
| const double dx = (east - west) / m; | ||
| const double dt = dx; | ||
|
|
||
| Divergence D(k, m, dx); | ||
| Interpol I(m, 1.0); | ||
|
|
||
| // Spatial grid (including ghost cells) | ||
| arma::vec xgrid(m + 2); | ||
| xgrid(0) = west; | ||
| xgrid(m + 1) = east; | ||
| for (int i = 1; i <= m; ++i) { | ||
| xgrid(i) = west + (i - 0.5) * dx; | ||
| } | ||
|
|
||
| // Initial condition | ||
| arma::vec U = arma::exp(-arma::square(xgrid) / 50.0); | ||
|
|
||
| // Sanity check: matrix dimensions | ||
| if (D.n_cols != I.n_rows || I.n_cols != U.n_rows) { | ||
| std::cerr << "Error: Incompatible matrix dimensions!" << std::endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| int total_steps = static_cast<int>(t / dt); | ||
| int plot_interval = total_steps / 5; | ||
|
|
||
| for (int step = 0; step <= total_steps; ++step) { | ||
| double time = step * dt; | ||
|
|
||
| // Explicit update | ||
| U += (-dt / 2.0) * (D * (I * arma::square(U))); | ||
|
|
||
| if (step % plot_interval == 0) { | ||
| double area = Utils::trapz(xgrid, U); | ||
| std::cout << "Time step: " << step | ||
| << ", Time: " << time | ||
| << ", Trapz Area: " << area | ||
| << ", U_min: " << U.min() | ||
| << ", U_max: " << U.max() | ||
| << ", U_center: " << U(U.n_elem / 2) | ||
| << std::endl; | ||
|
|
||
| std::string filename = "output_step_" + std::to_string(step) + ".dat"; | ||
| std::ofstream outfile(filename); | ||
| if (!outfile) { | ||
| std::cerr << "Error: Could not open file for writing: " << filename << std::endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| outfile << "# x U(x)\n"; | ||
| for (arma::uword i = 0; i < xgrid.n_elem; ++i) { | ||
| outfile << std::setw(12) << xgrid(i) << " " | ||
| << std::setw(12) << U(i) << "\n"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return EXIT_SUCCESS; | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.