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 7 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');
There was a problem hiding this comment.
Use addpath instead of the four lines of code here.
examples/matlab_octave/wave1D_convergence/run_convergence_test.m
Outdated
Show resolved
Hide resolved
examples/matlab_octave/wave1D_convergence/run_convergence_test.m
Outdated
Show resolved
Hide resolved
Signed-off-by: Martin Armoa <87256238+tin7@users.noreply.github.com>
|
I think that found the main reason I was still getting an apparent order higher than 2: I was measuring the error at (T = 0.5), which is a special time for this test case. For the exact solution sin(2\pi \cdot 0.5)=\sin(\pi)=0 I will update the MATLAB test accordingly (and then mirror the same fix in the C++ version). Thanks |
|
Implemented latest review feedback and pushed commit 9ff5b39 (fixed convergence setup, updated paths, and aligned MATLAB/C++ naming) |
jbrzensk
left a comment
There was a problem hiding this comment.
Thanks for the update. Looks better with the convergence.
I put some nitpick things about formatting and spacing and some extra bits not needed in the code.
Also about the naming. The wave1d_solver... should be named wave1Dsolver..., and just change the name of the "run_convergence_test.m" to wave1D_convergence.m. No need for the extra file.
| clear; clc; close all; | ||
|
|
||
| % --- 1. Path Configuration --- | ||
| current_file = mfilename('fullpath'); |
There was a problem hiding this comment.
Use addpath instead of the four lines of code here.
examples/matlab_octave/wave1D_convergence/run_convergence_test.m
Outdated
Show resolved
Hide resolved
examples/matlab_octave/wave1D_convergence/run_convergence_test.m
Outdated
Show resolved
Hide resolved
examples/matlab_octave/wave1D_convergence/run_convergence_test.m
Outdated
Show resolved
Hide resolved
examples/matlab_octave/wave1D_convergence/run_convergence_test.m
Outdated
Show resolved
Hide resolved
|
As requested, I also successfully verified the 4th-order behavior locally. By changing the mimetic operator to k=4 and drastically reducing the time step to | (m) | Mimetic Err | Rate | FD Error | Rate | |

…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?