**Explicit / implicit simulation**

Download and open the the file *beam_explicit.scn*. This file describes a beam falling under the effect of gravity with an explicit time integration. The beam falls under the effect of gravity when you press Animate. We will first attach one side of the beam as illustrated in the following image:

The scene is composed of the following components:

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

**FixedConstraint**: sets the points selected by BoxROI

In order to fix one side of the beam, we will define the values of *x_min y_min z_min x_max y_max z_max*, such that the **BoxROI **contains the last row of points located at the right end of the beam. Note that the box is visible by ticking *behavior model* in the graphical interface.

Double-click on the **BoxROI** component **.** A window appears that allows you to edit the attributes of the component. 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** Shith + R** to display the frame (Wraning the size of the arrows depends on the size of the scene, theferore it is not necessarily 1).

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**.** To do this, reset the scene (ctrl + R).

Now that the beam is fixed, open the *beam_explicit.scn* file in Sofa. Click *Animate* , then vary the time step from 0.002 to 0.00001 by dividing successively by 10. Observe that the number of FPS does not change, but the simulation becomes more and more slower 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 10000 FPS so that one second simulation is simulated in one second, which is not possible. In addition, 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 in the **TetraHedronFEMForceField** (it is necessary to restart sofa after each modification), and find that even with a *dt* at 0.002 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 to increase the stability is to use an implicit integration that is unconditionally stable. Copy/Paste the *beam_explicit.scn* file and name it *beam_implicit.scn* file from. In order to change the scene to an implicit version replace the ODE solver **EulerSolver** for an implicit version **EulerImplicitSolver**. Run the scene and observe the error in the terminal.

An implicit integration involves requiers 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 Lab 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, see 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 important than with the explicit version. Nevertheless, the stability of the simulation is much better, including with rigid materials and large time steps.