MagMASim (Reconstructing the Magnetic Field of the Milky Way via Astrophysical Techniques and Numerical Simulations)
Magnetic Field Forward/Inverse Modeling and Reconstruction with the Finite Element Method (FEM)
A research-oriented framework for forward and inverse magnetic field reconstruction using FEM and Bayesian inference.
This repository implements forward and inverse problems for magnetic-field-like systems using FEniCSx (dolfinx). It includes constrained formulations enforcing divergence-free fields, synthetic data generation, and workflows reproducing paper-level experiments (1-cut and 4-cut cases).
-
Forward FEM solver (scalar Laplace-based and constrained mixed formulation)
-
Divergence-free formulation via Taylor–Hood elements
-
Synthetic dataset generation with noise and sparsity control
-
Bayesian inverse problem (grid search and dual annealing)
-
Paper reproduction workflows:
- 1-cut case
- 4-cut case with clustering-based priors
-
Automatic plotting and export (JSON)
-
K-means + silhouette analysis for empirical prior estimation
Install required Python packages:
pip install -r requirements.txtContents of requirements.txt:
numpy
matplotlib
scipy
scikit-learn
This project depends on:
- dolfinx (FEniCSx)
- PETSc
- MPI
- petsc4py
- mpi4py
Tested on FEniCSx 0.10 (latest version at time of writing)
Please see here
https://fenicsproject.org/download/
src/
domains.py # mesh generation
fem_solver.py # forward solver (unconstrained)
constrained_solver.py # divergence-free mixed formulation
boundary_conditions.py
paper_boundary_conditions.py
synthetic_data.py
inverse_problem.py
paper_inverse_problem.py
postprocessing.py
io_utils.py
scripts/
run_synthetic_data.py
run_forward_cone.py
run_constrained_analytic.py
run_inverse_theta.py
run_paper_inverse_1cut.py
run_paper_inverse_4cut.py
python scripts/run_synthetic_data.pypython scripts/run_forward_cone.pypython scripts/run_constrained_analytic.pypython scripts/run_inverse_theta.pypython scripts/run_paper_inverse_1cut.pypython scripts/run_paper_inverse_4cut.pyResults are saved in:
outputs/<experiment_name>/run_<timestamp>/
Each run typically includes:
-
JSON datasets:
- full synthetic data
- observed (sparse) data
- reconstructed fields
-
Plots:
- field components (Bx, By, Bz)
- divergence ∇·B
- observation masks
- convergence curves
- parameter recovery plots
This project studies forward and inverse problems for magnetic-field-like systems.
- The forward model solves Laplace-type equations with boundary-driven fields.
- The constrained formulation enforces ∇·B = 0 via a mixed FEM approach.
- The inverse problem estimates boundary parameters from sparse observations using Bayesian optimization.
- The 4-cut case introduces piecewise boundary regions and clustering-based prior estimation.
- Random seeds are explicitly controlled in all scripts.
- Synthetic datasets can be regenerated deterministically.
- Inverse solvers support repeated runs for statistical analysis.
If you use this code, please cite:
G. E. Pavlou, V. Pavlidou, and V. Harmandaris, “Reconstructing the
magnetic field in an arbitrary domain via data-driven bayesian methods
and numerical simulations,” Computation 13 (2025), 10.3390/computa-
tion13020037 https://arxiv.org/abs/2404.15745
This project is licensed under the MIT License.
See the LICENSE file for details.
- Requires FEniCSx installation (not installable via pip alone)
- Mesh quality can affect numerical results