Skip to content

gepavlou/MagMASim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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).


Features

  • 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


Requirements

Python dependencies

Install required Python packages:

pip install -r requirements.txt

Contents of requirements.txt:

numpy
matplotlib
scipy
scikit-learn

FEniCSx (required)

This project depends on:

  • dolfinx (FEniCSx)
  • PETSc
  • MPI
  • petsc4py
  • mpi4py

Tested on FEniCSx 0.10 (latest version at time of writing)

Recommended installation methods

Please see here

https://fenicsproject.org/download/


Repository Structure

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

How to Run

1. Generate synthetic dataset

python scripts/run_synthetic_data.py

2. Forward solve

python scripts/run_forward_cone.py

3. Constrained (divergence-free) solve

python scripts/run_constrained_analytic.py

4. Inverse problem (deterministic θ)

python scripts/run_inverse_theta.py

5. Paper experiments

1-cut case

python scripts/run_paper_inverse_1cut.py

4-cut case

python scripts/run_paper_inverse_4cut.py

Outputs

Results 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

Scientific Context

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.

Reproducibility

  • Random seeds are explicitly controlled in all scripts.
  • Synthetic datasets can be regenerated deterministically.
  • Inverse solvers support repeated runs for statistical analysis.

Citation

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

License

This project is licensed under the MIT License. See the LICENSE file for details.


Notes

  • Requires FEniCSx installation (not installable via pip alone)
  • Mesh quality can affect numerical results

About

A research-oriented framework for forward and inverse magnetic field reconstruction using FEM and Bayesian inference.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages