This is a C++ rewrite of the original Fortran function FSM_WENO3_PS_2d, using the Eigen library for vectorized operations. The solver implements the Fast Sweeping Method combined with WENO3 (Weighted Essentially Non-Oscillatory) scheme to solve the 2D Eikonal equation.
The original FSM_WENO3_PS_2d function is a numerical solver for the Eikonal equation in 2D anisotropic media:
Where:
Tis the traveltime fieldfis the slowness field (reciprocal of velocity)- The equation in anisotropic media is more complex, involving coefficient matrices a, b, c
- WENO3 Scheme: Uses third-order WENO scheme for derivative calculation, providing high accuracy and stability
- Fast Sweeping Method: Four-directional sweeping ensures causality of the numerical solution
- Anisotropic Support: Supports anisotropic media through coefficient matrices a, b, c
- Boundary Condition Handling: Automatic boundary condition processing
- Eigen: For matrix and vector operations, providing optimized linear algebra computations
- STL: Standard C++ library for basic functionality
- Matrix Operations: Uses Eigen's matrix types instead of primitive arrays
- Batch Computation: Uses Eigen's vectorized operations where possible
- Memory Management: Uses Eigen's automatic memory management
class EikonSolver2D {
public:
static void FSM_WENO3_PS_2d(...); // Main solver function
private:
// Helper functions
static void computeT0AndDerivatives(...);
static void initializeTauAndChange(...);
static void computeWENODerivatives(...);
static void updateBoundaryConditions(...);
static void computeConvergenceMetrics(...);
};xx: x-coordinate vector (nx elements)yy: y-coordinate vector (ny elements)a, b, c: Anisotropic coefficient matrices (nx × ny)fun: Slowness field (nx × ny)x0, y0: Source coordinatesu: Reference solution (nx × ny) - only used for error calculation
T: Traveltime field (nx × ny)
mamba create -n EikonPP
mamba activate EikonPP
mamba install -c conda-forge eigen cmake c++_compiler hdf5mkdir build
cd build
cmake ..
make#include "eikon_solver_2d.hpp"
// Create grid and parameters
Eigen::VectorXd xx = Eigen::VectorXd::LinSpaced(nx, x_min, x_max);
Eigen::VectorXd yy = Eigen::VectorXd::LinSpaced(ny, y_min, y_max);
// Initialize coefficient matrices
Eigen::MatrixXd a(nx, ny), b(nx, ny), c(nx, ny);
Eigen::MatrixXd fun(nx, ny); // Slowness field
Eigen::MatrixXd u(nx, ny); // Reference solution
// Set parameters...
// Solve
Eigen::MatrixXd T(nx, ny);
EikonSolver2D::FSM_WENO3_PS_2d(xx, yy, a, b, c, fun, x0, y0, T);- Calculate grid spacing
- Perform bilinear interpolation at source location to obtain local parameters
- Compute initial traveltime field T0 and its derivatives
- Initialize tau array and change flag array
- Four-directional sweeping (forward and backward in both directions)
- For each point to be updated:
- Compute WENO3 derivatives
- Calculate Hamiltonian Htau
- Update tau values
- Update boundary conditions
- Check convergence
Uses third-order Weighted Essentially Non-Oscillatory scheme:
- Calculate weights based on local smoothness indicators
- Combine different finite difference schemes
- Ensure stability and accuracy of the numerical solution
- Memory Management: Eigen provides efficient memory management
- Vectorization: Modern compilers can better vectorize C++ code
- Template Optimization: Compile-time optimization
- SIMD Support: Eigen automatically uses SIMD instructions
- Use
-O3 -march=nativecompilation flags - Consider OpenMP parallelization (can be added in loops)
- Use appropriate data types (double vs float)
- Type Safety: C++'s strong type system
- Memory Safety: Automatic memory management
- Interface Simplification: Clear class interface
- Extensibility: Easy to add new features
- Maintains the same numerical precision as the original Fortran version
- Same convergence criteria and algorithm logic
Recommended test cases:
- Analytical solution verification in uniform media
- Anisotropic media testing
- Complex topography testing
- Performance benchmarking
- Parallelization: OpenMP or CUDA implementation
- 3D Extension: Extend to three-dimensional problems
- Adaptive Mesh: Implement adaptive mesh refinement
- Other Schemes: Implement other high-order schemes (WENO5, etc.)
Common issues:
- Compilation Errors: Check Eigen installation
- Numerical Instability: Check grid resolution and parameter settings
- Convergence Problems: Adjust tolerance parameters
This C++ implementation maintains the numerical precision and algorithm logic of the original Fortran code while providing the advantages of modern C++ and the performance optimizations of the Eigen library.