Elastic collision of bodies
In this example, we will calculate the motion of two bodies upon impact. The model takes into account masses and velocities, as well as elasticity, stiffness, and the model of their increase upon impact.
Description of the system
The model consists of two blocks that simulate point bodies with mass, with the ability to move along a 1D trajectory. In this experiment, the trajectory will be considered rectilinear.
The "Incoming stops" block is responsible for the collision, the lower limit of which is set to "minus infinity" (-Inf), thus from the side R Nothing restricts the movement of the connected object in this block, and if the object moves away from the stop, nothing will stop it. In oncoming traffic, the object transmits synetic energy to the stop, which transmits it to the second object.

The position of the upper stop limit sets * the distance that a moving object must travel before colliding* with a stationary one.
To visualize the movement, we use "Absolute motion Sensors", one of which is set to the initial position of the mass equal to -1 m.
Launching calculations
Let's run the model in program control mode:
engee.open( "$(@__DIR__)/" * "mass_collision_model.engee");
data = engee.run( "mass_collision_model" );
Based on the data obtained, we can build graphs:
mass1_P = collect(data["Mass movement 1.p_out"]);
mass1_V = collect(data["Mass movement 1.v_out"]);
mass2_P = collect(data["Movement of the mass 2.p_out"]);
mass2_V = collect(data["Mass movement 2.v_out"]);
gr()
plot( mass2_P.value, mass2_P.value, c=:red, lw=3, xlabel="Position of bodies, m", ylabel="Position of bodies, m", label="Weight 2", marker=(:o,8))
plot!( mass1_P.value, mass1_P.value, lw=3, xlabel="Position of bodies, m", ylabel="Position of bodies, m", label="Weight 1", marker=(:o),
mc = [(v > 0) ? :blue : :lightblue for v in mass1_V.value],
ms = [(v > 0) ? 4 : 3 for v in mass1_V.value])
plot!(guidefont=font(8), legend=:bottomright, title="Body positions", titlefont=font(11))
The red markers show the movement of the first body before the collision.
After the collision, the second body began to move (blue markers), and the first one changed the direction of movement (the collision is quite elastic, and the second body is twice as massive as the first).
plot(mass1_V.time, mass1_V.value, lw=3, xlabel="Position of bodies, m", ylabel="Position of bodies, m", label="Weight 1", marker=(:o,5))
plot!(mass2_V.time, mass2_V.value, lw=3, xlabel="Position of bodies, m", ylabel="Position of bodies, m", label="Weight 2", marker=(:o,4))
plot!(guidefont=font(8), legend=:bottomright, title="The speed of movement of bodies", titlefont=font(11))
If you change the collision parameters, you can get a different movement pattern.
# Let's make the impact less elastic
engee.set_param!("mass_collision_model/Соударение", "k_upper_bound"=>Dict("value" => 1e2, "unit" => "N/m"))
data = engee.run( "mass_collision_model" );
# Let's return the old value of the elasticity parameter
engee.set_param!("mass_collision_model/Соударение", "k_upper_bound"=>Dict("value" => 1e6, "unit" => "N/m"))
mass1_P = collect(data["Mass movement 1.p_out"]);
mass1_V = collect(data["Mass movement 1.v_out"]);
mass2_P = collect(data["Movement of the mass 2.p_out"]);
mass2_V = collect(data["Mass movement 2.v_out"]);
plot( mass2_P.value, mass2_P.value, lw=3, xlabel="Position of bodies, m", ylabel="Position of bodies, m", label="Weight 2", marker=(:o,8))
plot!( mass1_P.value, mass1_P.value, lw=3, xlabel="Position of bodies, m", ylabel="Position of bodies, m", label="Weight 1", marker=(:o,4))
plot!(guidefont=font(8), legend=:bottomright, title="Body position", titlefont=font(11))
With this parameter of impact elasticity, the first body continued to move in the same direction, but acquired a slight lag.
Conclusion
In the 1D formulation, it is possible to model quite complex mechanical systems, including the free movement of objects in space, taking into account collisions, while selecting the most appropriate parameters for each element of the mathematical model.


