Skip to content

Convert XML scenes to Python scripts and integrate FreeFem simulations with SOFA comparison#3

Open
Fimache wants to merge 3 commits intomain-freefem-sofafrom
python-script
Open

Convert XML scenes to Python scripts and integrate FreeFem simulations with SOFA comparison#3
Fimache wants to merge 3 commits intomain-freefem-sofafrom
python-script

Conversation

@Fimache
Copy link
Copy Markdown

@Fimache Fimache commented Mar 27, 2026

This PR adds Python scripts to convert XML scenes to Python and integrate FreeFem simulations with SOFA framework.

  • Convert XML scenes to Python format
  • Reimplement FreeFem code as PyFreeFem
  • Compare simulation results using percentage-based analysis
  • Use consistent interpolation methods for fair comparison

The comparison script analyzes both simulation outputs and provides:

  • Percentage differences between SOFA and FreeFem results

@th-skam
Copy link
Copy Markdown
Collaborator

th-skam commented Mar 27, 2026

You will need to base this on the main-freefem-sofa branch.

@Fimache Fimache changed the base branch from sofa-freefem to main-freefem-sofa March 27, 2026 16:14
Copy link
Copy Markdown
Collaborator

@th-skam th-skam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes need to be done. My recommendation for this PR is that it only covers the conversion of xml files to python. Let's do the comparison as a next step.

  1. It is better if we don't dump the new files at the top of the repository. Move barre_1d_pyfreefem.py and the others inside examples/FreeFEM/.

  2. beam_2d_sofa.py runs a simulation replicating examples/FreeFEM/beam-2d.scn. barre_1d_sofa.py does not. It does not in fact load nor run any SOFA scene.

  3. All the files in examples/FreeFEM/*.edp are duplicates of examples/FreeFEM/edp. So we should only keep one of them.

For me, this PR should only contain 2 new files: the xml files in examples/FreeFEM/sofa/ converted to SofaPython scripts. So,

  1. examples/FreeFEM/sofa/barre-traction.scn -> examples/FreeFEM/sofa/barre-traction.py
  2. examples/FreeFEM/sofa/beam-2d.scn -> examples/FreeFEM/sofa/beam-2d.py
    I suggest you keep the same names between the files. It becomes straightforward to understand the conversion.

Use the information here: https://sofapython3.readthedocs.io/en/latest/content/FirstSteps.html#executing-a-simulation
to help you

comparaison.py Outdated
Comment on lines +757 to +796
"""
POURQUOI ON PEUT COMPARER LES DEUX CODES MALGRÉ DES MAILLAGES DIFFÉRENTS ?
===========================================================================

PROBLÈME DE DÉPART :
• FreeFEM génère un maillage triangulaire NON-STRUCTURÉ avec ~4800 nœuds.
Les nœuds sont placés de façon irrégulière, denses là où le gradient est
fort (encastrement gauche), plus rares au centre.

• SOFA utilise une RegularGridTopology 11×4 = 44 nœuds seulement,
placés sur une grille parfaitement régulière.
Les lignes y sont : 0.0, 0.67, 1.33, 2.0
→ aucun nœud à y = 1.0 exactement !

SOLUTION — LA GRILLE COMMUNE :
1. On crée une grille uniforme 300×80 = 24 000 points couvrant [0,10]×[0,2].
2. On interpole chaque champ (ux, uy) sur cette grille depuis les nœuds bruts :
- FreeFEM : scipy.griddata(..., method='linear')
→ triangulation Delaunay + interpolation linéaire par morceaux (P1)
→ très fidèle car maillage dense
- SOFA : scipy.griddata(..., method='cubic')
→ interpolation cubique car 44 points sont trop peu pour 'linear'
→ lisse le champ entre les nœuds réguliers

3. Une fois les deux champs sur la même grille, on peut :
diff[i,j] = SOFA_interpolé[i,j] - FreeFEM_interpolé[i,j]
et calculer RMSE, erreur relative, histogrammes, etc.

POURQUOI LA COMPARAISON A DU SENS ?
Les deux codes résolvent la MÊME équation (élasticité linéaire plane,
même E, nu, gravité, encastrement gauche). La différence vient de :
a) La formulation : plane_strain (Vec3, λ=Eν/((1+ν)(1-2ν))) vs
les coefficients Lamé 3D de FreeFEM (identiques à plane_strain)
b) La discrétisation : triangles P1 vs Q1 subdivisé en triangles
c) Le nombre de DDL : 44 nœuds SOFA << 4814 nœuds FreeFEM
→ SOFA est moins précis, mais donne la bonne tendance physique.

Le tableau récapitulatif vous donne l'écart % entre les deux pour
chaque quantité d'intérêt (flèche maximale, RMSE, erreur médiane...).
""" No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I want us to avoid doing. For this reason, we will use a mesh that is shared among the two scripts (SOFA vs FreeFEM). We are introducing another factor with the interpolation, the effect of which we are not quantifying.

Will be added in subsequent PRs.

comparaison.py Outdated
Comment on lines +16 to +19
Usage :
python compare_freefem_sofa_complet.py

Fichiers requis (même dossier) :
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole file simply compares the results of two simulations. We don't want that.
Also, the usage instruction references another file.

ax.set_title("Déplacement axial — Barre 1D (charge uniforme)")
ax.legend()
ax.grid(True, alpha=0.3)

Copy link
Copy Markdown
Collaborator

@th-skam th-skam Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is just a copy of barre_1d_pyfreefem.py with some added lines to load some sofa results. It is not a converted file, from xml to python.

- Convert beam-2d.scn to beam-2d.py
- Convert barre-traction.scn to barre-traction.py
- Place scripts in examples/Freefem/sofa/
- Remove comparison scripts and duplicate .edp files (to be added in next PR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants