A CPU case: a block sliding on a plane

This page takes a complete case, compiles it, runs it and checks the answer. It is the sliding block from tdem/test_01.cpp: a cube is given a horizontal velocity on a rough horizontal plane, friction slows it down, and the distance it travels has an exact formula to compare against. That is what makes it a good first case — it exercises the contact machinery, the friction law and the time integration, and it has an answer you already know.

The mechanics of compiling are on the previous page. Here we go through the case line by line, and then run it.

The case

A block of mass m sliding at speed v on a plane with friction coefficient μ loses speed at a constant rate μg. So it stops after a time v/(μg) and travels

L = v^2 / (2 mu g)
                                

With v = 10, μ = 0.1 and g = 9.8 that is 51.02 metres, after 10.2 seconds. The case below simulates exactly this and prints the distance the block actually moved. It is tdem/test_01.cpp in the tree, with the usual GPL banner on top, which is left out here; copy it wherever you like and rename it as you please, and we will do exactly that when we come to compile it.

The program

/////////////////////// Test 01 the sliding block

// MechSys
#include <mechsys/dem/domain.h>
#include <mechsys/util/fatal.h>
#include <mechsys/util/util.h>

using std::cout;
using std::endl;
using Util::PI;
using Util::FmtErr;
using DEM::Domain;

int main(int argc, char **argv) try
{
    DEM::Domain dom;
    double dt = 1.0e-5;     // time step
    double g = 9.8;         // gravity acceleration
    double velocity = 10.0; // velocity of the sliding block.
    double inipos = -5.0;   // Initial position in the x axis
    double mu = 0.1;        // friction coefficient
    dom.AddCube(/*Tag*/-1,/*initial position*/Vec3_t(inipos,0.0,0.7), /*spheroradius*/0.1,/*side length*/ 1.0,  /*rho*/1.0, /*orientation*/PI/2.0,/*axis of orientation*/&OrthoSys::e0);
    dom.AddPlane(/*Tag*/-2,OrthoSys::O,0.1,100.0,100.0,1.0);
    // Get the position with tag -2 (the plane) and fix it
    DEM::Particle *p = dom.GetParticle(-2);
    p->FixVeloc();
    // Assign gravity and a velocity to particle -1
    p = dom.GetParticle(-1);
    p->v  = velocity , 0.0, 0.0;
    dom.Initialize(dt);
    p->Ff = 0.0,0.0,-p->Props.m*g;
    // setting the tangential viscosity coeffcient Gt to 0 and the friction coefficient to the desired value
    Dict B;
    B.Set(-1,"Gt Kn Kt",0.0,1.0e8,5.0e7);
    B.Set(-2,"Gt Kn Kt",0.0,1.0e8,5.0e7);
    dom.SetProps(B);

    //Assign a different friciton coefficient for different classess of particles
    std::pair<int,int> pr (-1/*tag1*/,-2/*tag2*/);
    dom.FricCoeff[pr] = mu;

    dom.Solve(/*final time*/10.0,/*time step*/dt,/*Output step*/0.1,NULL,NULL,/*file key*/"test_01",/*Render video?*/false);

    //Get the particle final position
    double finpos = p->x(0);

    //Comparing with theoretical formula
    cout << "  Observed L = " << finpos - inipos<< " Theoretical L = " << velocity*velocity/(2*mu*g) << "\n";

    return 0;
}
MECHSYS_CATCH
                                

Step by step

The domain

Everything starts with a DEM::Domain. It owns the particles, finds the pairs that are in contact, applies the contact law to each pair, integrates the motion, and handles the output. You do not manage contacts yourself: you describe the bodies and the material law, and the domain does the rest.

The two bodies

The cube is added with AddCube:

dom.AddCube(-1, Vec3_t(-5.0,0.0,0.7), 0.1, 1.0, 1.0, PI/2.0, &OrthoSys::e0);
                                

The arguments are the tag, the initial position, a spheroradius, the side length, the density, and then an orientation. The spheroradius is the part worth understanding: MechSys represents a polyhedron as a core shape with spheres of that radius at its corners and along its edges, which is what gives it rounded corners and makes contact between two shapes a distance between two spheres. A spheroradius of 0.1 on a cube of side 1 rounds the corners slightly rather than producing a mathematically sharp cube, and it is what makes the contact computation cheap and robust.

The orientation is PI/2 about e0, the x axis. That is a quarter turn, which puts a face down on the plane instead of an edge. The tag -1 means "assign the next free tag", and the same tag is used later to get the particle back.

The plane comes from AddPlane:

dom.AddPlane(-2, OrthoSys::O, 0.1, 100.0, 100.0, 1.0);
                                

a plane of 100 by 100 at the origin, with the same spheroradius and a density. It is a plane in the sense of a flat surface, but it is still a body with a density; what stops it moving is the next line.

Fixing the plane

DEM::Particle *p = dom.GetParticle(-2);
p->FixVeloc();
                                

FixVeloc pins the linear and angular velocity of the plane, so the contact forces do not push it around. Getting the particle back by tag is how you reach any body after adding it, and the pointer stays valid for the life of the domain.

Initial velocity, initialization, gravity

p = dom.GetParticle(-1);
p->v  = velocity , 0.0, 0.0;
dom.Initialize(dt);
p->Ff = 0.0,0.0,-p->Props.m*g;
                                

The cube is given its horizontal velocity, then Initialize(dt) prepares the domain for a run with that time step, and only then is gravity applied. The order matters and is easy to get wrong: Initialize resets the forces on the particles, so anything set in Ff before it is thrown away, and the block falls under no gravity at all.

Ff is the fixed external force on the particle. It is not a push for one step: it stays applied for the whole run, which is exactly what gravity is. There is no global gravity setting, so a case states it per particle, and p->Props.m gives the mass the domain computed from the density and the volume.

The mass is worth a look, because it explains the energy in the output. The cube is 1 by 1 by 1 with a density of 1, so it is 1 kg, and the initial kinetic energy is printed as 50, which is ½·1·10². That is a useful check that the geometry was interpreted as intended.

The contact law

Dict B;
B.Set(-1,"Gt Kn Kt",0.0,1.0e8,5.0e7);
B.Set(-2,"Gt Kn Kt",0.0,1.0e8,5.0e7);
dom.SetProps(B);
                                

A Dict is a small dictionary, and Set takes a tag, the names of the properties, and their values. The three here are:

Gn   normal viscous coefficient
Gt   tangential viscous coefficient
Kn   normal stiffness
Kt   tangential stiffness
                                

Kn and Kt are the spring constants of the contact: how hard the material pushes back when it is compressed normally and sheared tangentially. They are large — 1e8 and 5e7 — so the contact is stiff and the block does not sink visibly into the plane.

Gt is set to zero on purpose. The tangential viscous coefficient is a damping in the tangential direction, and with it set to zero the only tangential effect left is the Coulomb friction set in the next step. That is what makes the block decelerate at exactly μg and lets the simulation be compared with the school formula. Leave a non-zero Gt in and the motion is also damped viscously, which is often what you want physically, but then it is no longer a Coulomb problem and it will not match the formula.

The friction coefficient

std::pair<int,int> pr (-1,-2);
dom.FricCoeff[pr] = mu;
                                

Friction is a property of a pair of materials, not of one body, so FricCoeff is a map from a pair of tags to a coefficient. Here the pair is the cube and the plane. Anything not listed falls back to the microscopic coefficient set through the dict, which is why a case with many materials only names the pairs that differ.

Solving

dom.Solve(10.0, dt, 0.1, NULL, NULL, "test_01", false);
                                

The arguments are the final time, the time step, how often to write output, a setup callback, a report callback, the file key, and whether to render. Both callbacks are unused here and passed as NULL; the GPU page shows what they are for, since boundary conditions are supplied through the setup one.

The "test_01" in the middle is the file key, and it is the stem of every filename the run produces. It is the only thing in the case that decides those names. If you save this code under a name of your own, this is the string to change: leave it alone and a copy called slide.cpp will still write test_01_*.h5.

The time step deserves a thought. The domain reports a suggested time step in its output, derived from the stiffness of the contacts and the smallest particle, and 1.0e-5 is what it suggests here. Taking a step much larger than the suggestion is the classic way to make a DEM simulation explode: the contact springs are stiff, and a step that is too long overshoots the force and the energy grows instead of being conserved.

Compiling and running

Put the code in a directory of your own, under any name you like. Nothing here needs the MechSys tree, and nothing needs to be called test_01:

user@mach: ~ $ mkdir myproject
user@mach: ~ $ cd myproject
user@mach: ~/myproject $ cp ~/mechsys/tdem/test_01.cpp slide.cpp   # or paste it in by hand
user@mach: ~/myproject $ ~/mechsys/mechsyscc slide.cpp -o slide
user@mach: ~/myproject $ ./slide
                                

mechsyscc takes the path to your file and does not care where it is or what it is called; the .cpp extension is what sends it to the C++ compiler rather than to nvcc. Without -o the executable would be named after the source, so slide.cpp alone would have given you ./slide anyway, and the executable always lands in the directory you run the command from.

One thing to change in the code if you rename it: the file key in Solve. It is still the string "test_01" above, so this renamed copy writes test_01_0000.h5 and its siblings, not slide_*.h5. Editing that one string is the whole of it.

The run takes a couple of seconds — a million steps, but only two particles, so there is almost nothing to do per step. The output is a report of the state at the start and at the end:

--- Initializing particles ------------------------------------------------------
  Kinematic energy   = 50
  Potential energy   = 0
  Total energy       = 50

--- Solving ---------------------------------------------------------------------
  Total mass   of free particles            =  1
  Total volume of free particles            =  1
  Total number of particles                 =  2
  Time step                                 =  1e-05
  Simulated Time                            =  10
  Verlet distance                           =  0.05
  Suggested Time Step                       =  1e-05
  Total number of iterations                                = 1000001
  Kinematic energy                                          = 0.020037
  Potential energy                                          = 2.48429e-07
  Total energy                                              = 0.0200372
  Elapsed time       = 2.269538s  CPU 18.101173s  (16:27:03 => 16:27:05)
  Observed L = 51.0012 Theoretical L = 51.0204
                                

Checking the answer

The last line is the point of the case, and it is worth reading carefully because the two numbers are not quite the same quantity.

Theoretical L = 51.0204 is v²/(2μg), the total distance to a stop. It is reached at v/(μg) = 10.204 seconds. But the run stops at 10 seconds, which is slightly before the block stops.

So the honest comparison is against the position at t = 10 s:

x(10) = v t - (1/2) mu g t^2 = 100 - 49 = 51.0000
                                

and the simulation reports 51.0012. That is a difference of about a millimetre in 51 metres, consistent with the spheroradius rounding the cube's corners slightly and with the contact being a stiff spring rather than a rigid constraint. The agreement with the formula the case prints, 51.0204, is also good, simply because at t = 10 s the block is nearly stopped and the remaining 0.02 m of travel is still to come.

The energy line tells the same story from the other side. The block started with kinematic energy 50 and ends with 0.02, so it has been brought very nearly to rest, and the small residual potential energy is it sitting slightly sunk into the plane, held by the contact spring.

Things that bite

Gravity set before Initialize. The forces are reset, and the block will not fall. This is the most common mistake with this case.

No FixVeloc on the plane. The plane has a mass and will be pushed along by the cube. The case will still run, and the answer will be wrong.

A time step larger than the suggested one. The simulation gains energy instead of losing it. The suggested value is printed on every run; watch it.

Expecting a sharp cube. The spheroradius is part of the geometry. Setting it very small makes the contacts stiffer and the required time step smaller, and setting it large visibly rounds the shape.

Forgetting how much output there is. The output step of 0.1 over 10 seconds writes test_01_0000 to test_01_0099, each an .h5 with a matching .xmf, and alongside them a test_01_bf_0000 to test_01_bf_0098 pair — 199 files of each kind, into the current directory. ParaView opens the .xmf; the .h5 holds the data. For a long production run it is worth choosing that output step deliberately, because these files are large and nothing prunes them.