-
Notifications
You must be signed in to change notification settings - Fork 473
Chrono Basics
The best way to get into a piece of software is to just start using it. We first need to understand how Chrono computes its simulations. Let's illustrate this with a minimal script that implements Chrono.
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/core/ChCoordsys.h"
#include "chrono/physics/ChBody.h"
int main()
{
chrono::ChCoordsys body_coords(chrono::ChVector3d(0, 0, 0), chrono::ChQuaterniond(1, 0, 0, 0));
auto the_body = std::make_shared<chrono::ChBody>();
the_body->SetCoordsys(body_coords);
the_body->SetMass(1);
the_body->SetLinVel(chrono::ChVector3d(0, 1, 0));
chrono::ChSystemNSC sys;
sys.SetGravitationalAcceleration(chrono::ChVector3d(0, 0, 0));
sys.AddBody(the_body);
double frame_time_interval = 0.1;
for (double frame_time = 0.0; frame_time < 0.5; frame_time += frame_time_interval)
{
std::cout << "Pos: " << the_body->GetPos()
<< " Vel: " << the_body->GetLinVel() << std::endl;
sys.DoStepDynamics(frame_time_interval);
}
return 0;
}
This script does all the basic things that demonstrate a physics simulation:
- Set up a coordinate system at the origin
$(0,0,0)$ with a rotation of$(1,0,0,0)$ , that is, no rotation. - Create a body at the coordinates we just created. The only relevent things about this body are that it has a mass of 1kg and it's moving at 1m/s in the
$y$ direction. This is all that's required to compute a basic distance-velocity calculation. - Create a physics system. This acts as the Universe in which the simulation takes place. We're going to turn off gravity in this system to keep things simple. The body is added the system.
- In order to simulate the passage of time, we set up a
for
loop to iterate from 0s to 0.5s in intervals of 0.1s. We then print out the position of the object as it moves through space.
Let's Start taking apart this fucntionality to understand how chrono performs all of these functions.
The first thing to understand is that a ChSystem
contains an underlying ChAssembly
containing all of the mechanically interacting objects in the system. As shown in the diagram below:
---
classDiagram
---
classDiagram
class ChAssembly {
#vector bodylist
#vector shaftlist
#vector linklist
#vector meshlist
#vector otherphysicslist
#vector batch_to_insert
}
class ChSystem {
m_num_coords_pos int
m_num_coords_vel int
m_num_constr int
}
class ChTimestepper
class ChIntegrable
ChSystem "1" *-- "1" ChAssembly : assembly
ChSystem <--> ChTimestepper : Reference Each Other
ChSystem ..|> ChIntegrable
The ChSystemNSC
object that we created is a ChSystem
object which manages a ChAssembly
of the mechanical objects in a series of lists, each implemented as a C++ std vector. When we ran the AddBody
command, the object the_body was added to the ChAssembly
's list of bodies. The ChSystem
itself is of the type ChIntegrable
, meaning that it can perform the integration necessary to compute the positions of the objects in the ChAssembly
. The ChSystem
is interdependent with a ChTimestepper
that advances time and computes the state of the simulation to pass on to the ChAssembly
When the DoStepDynamics
function is called, a series of events is put in motion to simulate the motion of the object. Let's look at a single timestep iteration to see what happens:
stateDiagram-v2
initial : Initialize Visual system and Collision system if necessary
setup : Set up mechanical objects in the assembly
timestep : Perform a timestep and compute a new system state
scatter : Compute the new positions of the mechanical objects in the assembly
[*] --> initial
initial --> setup
setup --> timestep
timestep --> scatter
scatter --> [*]
When DoStepDynamics
is called, it first runs an initialization function to check if there are any visuals or collisions to calculate. Our exzample is minimal, so there aren't any to consider. After that, a Setup
function is called which sets up each of the bodies, linkages, and other physics objects. After they are set up, a TimeStepper::Advance
function is called which updates the state of the system. There are several different algorithms for different ways to compute this, the default one being used is a ChTimestepperEulerExplIIorder
algorithm. Once the system state is computed, a function called StateScatter
computes the new positions of the mechanical objects in the simulation.
Here are some things to try in order to test your understanding:
- What would happen if we changed
frame_time_interval
? Would this affect our simulation? - What would happen if we changed the mass of
the_body
? Does this make sense wit our understanding of Physics?