Lab1: Real time simulation


In this lab, we will use the SOFA software that is installed on the machines.

Installation and visualization

Launch the command runSofa in the terminal from any location. Sofa starts with a default example:

You can move around the scene using the wheel to move forward / backward. Right click allows to go up / down and left click to rotate the scene. Click Animate to start the simulation and see the collisions and deformations. With Shift + Left Shift, you can apply external forces on the deformable model to pull and deform it.

The View tab allows you to change the view mode. For example, you can visualize the mechanical mesh or collision.

Display the mechanical mesh by clicking on Force Fields, a set of hexahedra appears which represent the elements on which the mechanics is calculated. You can also display the collision mesh. For that click on Collision Model then disable the Visual Models visual model . The meshes displayed are simpler meshes which are used for the computation of the collisions. Finally, you can view other information such as contacts (Interactions) or wireframe mode .

The launch of the software via the graphical interface does not make it possible to visualize the errors. In this lab, it will be better to launch the software from the terminal.

Scene files


Sofa is based on a component graph system. Each component is specialized for a specific task in simulation (solver, visualization, topology, …). The principle is then to describe a simulation by a hierarchy of components that will interact with each other to form the simulation. These components are described in a format similar to XML which is called a scene file (.scn) in the Sofa nomination.

The Modeler is a graphical tool for creating and editing scene files. It is located in the Sofa / build / bin directory. On the left is a list of all Sofa components, in the center a visualization of the scene file and on the right the parameters of each component.

Download and open the the file beam_explicit.scn. In the central tab, you can see a hierarchical description of the components of the scene. The main node is named scene. By clicking on these nodes, you will be able to specify the global parameters of the simulation in the tab on the right. For example, we can change the gravity and the time step.

The node scene contains a sub node named beam This sub node contains a list of components that will constitute the simulation of a deformable beam. Each of these components accepts several parameters (sometimes called attributes), of which here is a description:

  • EulerSolver: explicit temporal integration scheme
  • RegularGrid: generates a regular hexahedral grid in a procedural way. Parameter n describes the number of elements in the three dimensions X, Y and Z. The parameters min and max give the dimensions of the beam
  • MechanicalObject: Contains position, speed and acceleration variables. This component is essential to calculate the mechanics of deformable objects.
  • UniformMass: Adds Mass to the System
  • TetrahedronFEMForceField: Adds a co-rotational behavior to the system. The parameter youngModulus allows to adjust the rigidity of the material, and the fishRatio regulates the incompressibility.
  • BoxROI: Sets the boundary conditions. The box parameter describes a box in 3D in which all the points of the deformable model will be considered fixed. The boxes are declared using 6 scalars, successively describing two 3D vectors: x_min y_min z_min x_max y_max z_max . The box is visible by ticking behavior model in the graphical interface.
  • FixedConstraint: sets the points selected by BoxROI

From the Modeler, run this file in Sofa (RunSofa-> Run in Sofa) . Click on Animate to animate the model. The beam falls under the effect of gravity. We will define the values ​​of x_min y_min z_min x_max y_max z_max , from so to fix the last row of points located at the right end of the beam as on the following image:

To do this, reset the scene (ctrl + R) (do not click Animate to not move the beam ) . Click the Graph tab (in the left window). This tab allows you to view and edit the components of the scene during the simulation. Notice that you find the hierarchical structure of the components as described in the Modeler. Double-click on the BoxROI component . A window appears that allows you to edit the attributes of the component (Note that some parameters can not be edited during the simulation). Edit the box attribute which contains the 6 values x_min y_min z_min x_max y_max z_max . Change the values ​​to select only the points on the last row to the right of the beam and click update at the bottom left of the window. To help you you can press R to display the marker.

Save the values ​​of x_min y_min z_min x_max y_max z_max you have found.

Scene files

In practice, the use of the Modeler remains limited. Indeed, formatting after the backup makes it difficult to read and edit the scene file. It is generally preferred to edit scene files using a text editor. Open the beam_explicit.scn file in a text editor.

In this file we find the hierarchy of components and their names as they were described in the Modeler . The hierarchical structure is done using the <Node> tag. This tag is opened in the first line by <Node> and closed to the last line by </ Node> which represents the general node of the scene .

Sub nodes are then declared to define the deformable beam. Inside, there is a declarative list of components that are opened using the < and closed with /> character . The attributes are then given by specifying name = “value” .

Edit the beam_explicit.scn file using a text editor and copy the values ​​of x_min y_min z_min x_max y_max z_max into the box field of the BoxROI compostant . Save the file and restart the scene.

Explicit / implicit simulation

Now that the beam is fixed, open the beam_explicit.scn file in Sofa (preferably from a terminal). Click A nimate , then vary the time step from 0.001 to 0.00001 by dividing it successively by 10 (be careful never to type 0). Observe that the number of FPS does not change, but that the simulation becomes more and more slow until it appears static.

Indeed, the simulated time is equal to the dt * FPS . With a dt of 0.00001, it is necessary to achieve a performance of 10,000 FPS so that a second simulation is simulated in one second, which is simply not possible. However, the stability of an explicit simulation is guaranteed only under certain conditions. In particular, the time step must be relatively small, especially if the simulated object is rigid.

Set the time step to 0.01 and find that the beam becomes unstable. Similarly, increase the modulus of young to 500,000 in the TetraHedronFEMForceField (it is necessary to restart sofa with each modification), and find that even with a dt at 0.001 the simulation is also unstable. However, with a dt equal to 0.0001 the simulation remains stable but it is no longer real time.

An alternative is to use an implicit integration that is unconditionally stable. To do this, create a beam_implicit.scn file from the beam_explicit.scn file. To change the scene to an implicit version just replace the ODE solver EulerSolver for an implicit version EulerImplicitSolver . Nevertheless, it must be known that an implicit integration involves solving a linear system at each time step. Thus, to use the EulerImplicitSolver component it is necessary to add a component to solve the linear system.

There are several linear solvers in Sofa (iterative, direct, …). In this TP we will use a Conjugate Gradient which is an iterative algorithm. Add the CGLinearSolver component below the Euler integration. In the Modeler, or in Sofa, view the parameters of this component, and define in the scene file the number of iterations at 25 and the tolerance at 1e-5.

Launch the implicit scene and notice that the FPS is less than the explicit version. Nevertheless, the stability of the simulation is much better, including with rigid materials and a big time step.