Feat: Add 1D Wave convergence example (Matlab & C++) ref Mathews & Fi…#270
Feat: Add 1D Wave convergence example (Matlab & C++) ref Mathews & Fi…#270tin7 wants to merge 2 commits intocsrc-sdsu:mainfrom
Conversation
jbrzensk
left a comment
There was a problem hiding this comment.
Thanks for contributing @tin7 . It will be good to have this comparison.
For the code, I am going to reference the MATLAB version, but the same suggestion may apply to the C++ code.
In the MATLAB folder, you have div, grad, lap. Please use the operators in the MOLE src/matlab_octave folder.
Also, you do not need the pictures in the PR. Those are generated by the user. You can include them in the pull request, so we can see what the results look like, but leave them out of the PR.
To check the convergence of the operator in space, you FIX the time discretization (dt) and vary the space discretization (dx). You have both varying.
Also, for the FD method, you have a staggered grid (line 34). To do a correct comparison, you need m+2 equally spaced points. The finite difference does not work on a staggered grid. Currently, this introduces additional error in the FD method. When done correctly, both of these measurements (FD and MD methods) should be really close to 2.
|
Thank you for the detailed review and the suggestions regarding the grid setup and time stepping.
Ready for re-review. |
jbrzensk
left a comment
There was a problem hiding this comment.
Getting better. I put some notes about the paths and the error. The mimetic is order 2, you should get order 2, like really close to 2, (1.99->2.01). Same for the finite difference. If you get something different, you did something wrong.
Some other notes about adding paths much simpler, and using the dot operator for scalar times matrix.
Again, thanks for this. I will probably have notes about naming the files and their folder, but thats minor. Key is to get the error to show around order 2. Once it does, change the order of the mimetic operator and ensure that it is order 4. Once yuou fix the matlab, I just assume you will fix the cpp code.
| clear; clc; close all; | ||
|
|
||
| % --- 1. Path Configuration --- | ||
| current_file = mfilename('fullpath'); |
There was a problem hiding this comment.
Files in the current folder are automatically in the path for both Matlab and Octave.
You can add the src files to the path like other matlab examples, with
addpath('../../../src/matlab_octave');
| mesh_sizes = [20, 40, 80, 160, 320]; | ||
| n_sims = length(mesh_sizes); | ||
| c = 2; | ||
| m_finest = max(mesh_sizes); |
There was a problem hiding this comment.
Just set a small dt. No need to to complicated logic to pick a value. You already picked the numbers of cells, pick an appropriate dt. Something that devides evenly into 0.5
| fprintf('| :--- | :--- | :--- | :--- | :--- |\n'); | ||
|
|
||
| for i = 1:n_sims | ||
| r_mim = '-'; r_fd = '-'; |
There was a problem hiding this comment.
Use '----' to make the table print out nicer
|
|
||
| % Define the Force Function (RHS) | ||
| % F = c^2 * Laplacian * u | ||
| F_op = @(u) (c^2) * (L * u); |
There was a problem hiding this comment.
scalar times matrix should have '.*' as the operator. MATLAB usually handles this OK, but its also a clue that its a scalar and matrix in the equation
|
|
||
| % Compute discrete L2 Error Norm | ||
| diff = u_num_centers - u_exact_centers; | ||
| L2_error = sqrt(sum(diff.^2) * dx); |
There was a problem hiding this comment.
L2 error has N, but I think you want relative error here, for testing convergence. sqrt((Uh-U)^2) / sqrt(U^2).
| % 1. Physical Parameters and Grid Configuration | ||
| % ========================================================================= | ||
| a = 0; b = 1; % Domain boundaries | ||
| dx = (b-a)/m; % Grid spacing |
There was a problem hiding this comment.
dx set here, and changed later. Just use the later one.
| % 0: Main diagonal | ||
| % +1: Upper diagonal | ||
| L = spdiags([e -2*e e], -1:1, N, N); | ||
| L = L / dx^2; |
There was a problem hiding this comment.
./ for division. For sanity.
| L(end, :) = 0; L(end, end) = 0; | ||
|
|
||
| % Define the Force Function (RHS) | ||
| F_op = @(u) (c^2) * (L * u); |
| u_exact_internal = u_exact_all(2:end-1); | ||
|
|
||
| diff = u_num_internal - u_exact_internal; | ||
| L2_error = sqrt(sum(diff.^2) * dx); |
There was a problem hiding this comment.
Same here, I think you want relative error.

…nk Ex 10.1
What type of PR is this? (check all applicable)
Description
Academic example comparing Mimetic Finite Differences vs Standard Finite Differences for the 1D Wave Equation (Hyperbolic).
Context & Reference
Mathematical Formulation
Implementation Details
C++ (
examples/cpp/wave1D_convergence.cpp):MATLAB/Octave (
examples/matlab_octave/wave1D_convergence/):run_convergence_test.m: Driver script.Numerical Verification
Mimetic Scheme Convergence Rates (C++ Output):
Note: The observed rates > 2.0 indicate superconvergence effects typical of Mimetic operators on staggered grids with smooth initial conditions.
Related Issues & Documents
QA Instructions, Screenshots, Recordings
Visual validation of the implementation:
(1) Convergence: Error vs Grid Spacing

(2) Convergence: Error vs cells

(3) Wave Profile Comparison (Coarse Grid m=20)

Visual validation of phase and amplitude accuracy on a low-resolution grid. It is noteworthy that the Mimetic method closely tracks the analytical solution even on this coarse mesh ($m=20$), exhibiting significantly less numerical dispersion than the standard Finite Difference scheme.
How to test:
cd build && make && ./examples/cpp/wave1D_convergenceexamples/matlab_octave/wave1D_convergence/run_convergence_test.mAdded/updated tests?
_We encourage you to test all code included with MOLE, including examples.
have not been included
Read Contributing Guide and Code of Conduct
[optional] Are there any post deployment tasks we need to perform?
[optional] What gif best describes this PR or how it makes you feel?