Skip to content

Add SOFAUnits, a utilitary lib for unit conversion#622

Open
bakpaul wants to merge 3 commits into
sofa-framework:masterfrom
bakpaul:26_07_add_unit_converter
Open

Add SOFAUnits, a utilitary lib for unit conversion#622
bakpaul wants to merge 3 commits into
sofa-framework:masterfrom
bakpaul:26_07_add_unit_converter

Conversation

@bakpaul

@bakpaul bakpaul commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

It has been a looooong time issue for the community : how to deal with units in SOFA. "If I use mm instead of m, what appends ton my Young modulus ?" : heard too many times.

This is a simple utilitary lib that provide a class where you can define the system of parameter that you use, given a set of "primary" units (length, mass and time).

Then, when you need to specify a value that has a certain unit, you just specify the unit you want and the conversion is done automatically for you. For instance :

    SceneUnit = SOFAParameters(s, mm, g) #I know my scene is in mm and in g
    ...
    root.gravity=[0, SceneUnit.convert(-9.81, N/kg), 0]
    ...
    liver.addObject('DiagonalMass', name="Mass", massDensity=SceneUnit.convert(1.0,g/(cm**3)))
    ...
    liver.addObject('TetrahedralCorotationalFEMForceField', youngModulus=SceneUnit.convert(3,kPa),  template="Vec3d", name="FEM", method="large", poissonRatio="0.3",computeGlobalMatrix="0")

And you can also use the 'call' overload

    SceneUnit = SOFAParameters(s, mm, g) #I know my scene is in mm and in g
    ...
    root.gravity=[0, SceneUnit(-9.81, N/kg), 0]
    ...

The usual units for mechanics are given (see units.py). It is really easy to add more, even for "primary" units, e.g. if we want to deal with electricity at one point

A = PrimaryUnit("A")   #ampere

### Derived units
V = kg*m**2*/(s**3*A)  # Volt
C = s*A                # Coulomb
F = C/V                # Farad


## Scaled primary units
mV = ScaledUnit(V, 1e-3)
....

👨 Human made ! The tests have been generated by AI, the rest I did it myself for fun.

@bakpaul bakpaul changed the title Add SOFAUnits, a utilitary lib for unit convertion Add SOFAUnits, a utilitary lib for unit conversion Jul 1, 2026
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.

1 participant