\( \usepackage{euler} % must be loaded before fontspec for the whole doc (below); this must be kept for pngmath, however \usepackage{hyperref} \usepackage{amsmath} \usepackage{amsbsy} %\usepackage{mathabx} \usepackage{underscore} \usepackage[all]{xy} % Metadata of the pdf output \hypersetup{pdftitle={Yade Documentation}} \hypersetup{pdfauthor={Smilauer, V., et al.}} \hypersetup{pdfkeywords={Discrete element method, granular materials, contact law, hydromechanical coupling}} % symbols \renewcommand{\mat}{\boldsymbol} \renewcommand{\vec}{\boldsymbol} \renewcommand{\tens}{\boldsymbol} %normalized and locfram here \def\normalized#1{\widehat{#1}} \def\locframe#1{\widetilde{#1}} % timestep \def\Dt{\Delta t} \def\Dtcr{\Dt_{\rm cr}} % algorithm complexity \def\bigO#1{{\mathcal{O}(#1)}} % variants for greek symbols \let\epsilon\varepsilon \let\theta\vartheta \let\phi\varphi % shorthands \let\sig\sigma \let\eps\epsilon % variables at different points of time \def\prev#1{#1^-} \def\pprev#1{#1^\ominus} \def\curr#1{#1^{\circ}} \def\nnext#1{#1^\oplus} \def\next#1{#1^+} % shorthands for geometry \def\currn{\curr{\vec{n}}} \def\currC{\curr{\vec{C}}} \def\uT{\vec{u}_T} \def\curruT{\curr{\vec{u}}_T} \def\prevuT{\prev{\vec{u}}_T} \def\currn{\curr{\vec{n}}} \def\prevn{\prev{\vec{n}}} % motion \def\pprevvel{\pprev{\dot{\vec{u}}}} \def\nnextvel{\nnext{\dot{\vec{u}}}} \def\curraccel{\curr{\ddot{\vec{u}}}} \def\prevpos{\prev{\vec{u}}} \def\currpos{\curr{\vec{u}}} \def\nextpos{\next{\vec{u}}} \def\curraaccel{\curr{\dot{\vec{\omega}}}} \def\pprevangvel{\pprev{\vec{\omega}}} \def\nnextangvel{\nnext{\vec{\omega}}} \def\loccurr#1{\curr{\locframe{#1}}} \def\numCPU{n_{\rm cpu}} \DeclareMathOperator{\Align}{Align} \DeclareMathOperator{\sign}{sgn} % sorting algorithms \def\isleq#1{\currelem{#1}\ar@/^/[ll]^{\leq}} \def\isnleq#1{\currelem{#1}\ar@/^/[ll]^{\not\leq}} \def\currelem#1{\fbox{$#1$}} \def\sortSep{||} \def\sortInv{\hbox{\phantom{||}}} \def\sortlines#1{\xymatrix@=3pt{#1}} \def\crossBound{||\mkern-18mu<} \)

# Introduction to Bash and Python

## Terminal

The terminal is a shell designed to let us interact with the computer and its filing system with a basic set of commands:

    user@machine:~$                         # user operating at machine, in the directory ~ (= user's home directory)
    user@machine:~$ ls .                    # list contents of the current directory
    user@machine:~$ ls foo                  # list contents of directory foo, relative to the dcurrent directory ~ (= ls ~/foo = ls /home/user/foo)
    user@machine:~$ ls /tmp                 # list contents of /tmp
    user@machine:~$ cd foo                  # change directory to foo
    user@machine:~/foo$ ls ~                # list home directory (= ls /home/user)
    user@machine:~/foo$ cd bar              # change to bar (= cd ~/foo/bar)
    user@machine:~/foo/bar$ cd ../../foo2   # go to the parent directory twice, then to foo2 (cd ~/foo/bar/../../foo2 = cd ~/foo2 = cd /home/user/foo2)
    user@machine:~/foo2$ cd                 # go to the home directory (= ls ~ = ls /home/user)
    user@machine:~$

## Keys

Useful keys on the command-line are:

<tab> | show possible completions of what is being typed (use abundantly!)
^C (=Ctrl+C) | delete current line
^D | exit the shell
↑↓ | move up and down in the command history
^C | interrupt currently running program
^\ | kill currently running program
Shift-PgUp | scroll the screen up (show past output)
Shift-PgDown | scroll the screen down (show future output; works only on quantum computers)

## Starting yade

If yade is installed on the machine, it can be (roughly speaking) run as any other program; without any arguments, it runs in the “dialog mode”, where a command-line is presented:

    user@machine:~$ yade
    Welcome to Yade 2022.01a
    TCP python prompt on localhost:9002, auth cookie `adcusk'
    XMLRPC info provider on http://localhost:21002
    [[ ^L clears screen, ^U kills line. F12 controller, F11 3d view, F10 both, F9 generator, F8 plot. ]]
    Yade [1]:                                            #### hit ^D to exit
    Do you really want to exit ([y]/n)?
    Yade: normal exit.

The command-line is in fact `python`, enriched with some yade-specific features. (Pure python interpreter can be run with `python` or `ipython` commands).

Instead of typing commands one-by-one on the command line, they can be be written in a file (with the .py extension) and given as argument to Yade:

    user@machine:~$ yade simulation.py

For a complete help, see `man yade`

Exercises

1. Open the terminal, navigate to your home directory

2. Create a new empty file and save it in `~/first.py`

3. Change directory to `/tmp`; delete the file `~/first.py`

4. Run program `xeyes`

5. Look at the help of Yade.

6. Look at the manual page of Yade

7. Run Yade, exit and run it again.

## Yade basics

Yade objects are constructed in the following manner (this process is also called “instantiation”, since we create concrete instances of abstract classes: one individual sphere is an instance of the abstract Sphere, like Socrates is an instance of “man”):

    Yade [1]: Sphere           # try also Sphere?
    Out[1]: yade.wrapper.Sphere

    Yade [2]: s=Sphere()       # create a Sphere, without specifying any attributes

    Yade [3]: s.radius         # 'nan' is a special value meaning "not a number" (i.e. not defined)
    Out[3]: nan

    Yade [4]: s.radius=2       # set radius of an existing object

    Yade [5]: s.radius
    Out[5]: 2.0

    Yade [6]: ss=Sphere(radius=3)   # create Sphere, giving radius directly

    Yade [7]: s.radius, ss.radius     # also try typing s.<tab> to see defined attributes
    Out[7]: (2.0, 3.0)

## Particles

Particles are the “data” component of simulation; they are the objects that will undergo some processes, though do not define those processes yet.

## Singles

There is a number of pre-defined functions to create particles of certain type; in order to create a sphere, one has to (see the source of utils.sphere for instance):

1. Create Body

2. Set Body.shape to be an instance of Sphere with some given radius

3. Set Body.material (last-defined material is used, otherwise a default material is created)

4. Set position and orientation in Body.state, compute mass and moment of inertia based on Material and Shape

In order to avoid such tasks, shorthand functions are defined in the utils module; to mention a few of them, they are utils.sphere, utils.facet, utils.wall.

    Yade [8]: s=utils.sphere((0,0,0),radius=1)    # create sphere particle centered at (0,0,0) with radius=1

    Yade [9]: s.shape                       # s.shape describes the geometry of the particle
    Out[9]: <Sphere instance at 0x2593d950>

    Yade [10]: s.shape.radius                # we already know the Sphere class
    Out[10]: 1.0

    Yade [11]: s.state.mass, s.state.inertia # inertia is computed from density and geometry
    Out[11]:
    (4188.790204786391,
     Vector3(1675.516081914556253,1675.516081914556253,1675.516081914556253))

    Yade [12]: s.state.pos                   # position is the one we prescribed
    Out[12]: Vector3(0,0,0)

    Yade [13]: s2=utils.sphere((-2,0,0),radius=1,fixed=True)     # explanation below

In the last example, the particle was fixed in space by the `fixed=True` parameter to utils.sphere; such a particle will not move, creating a primitive boundary condition.

A particle object is not yet part of the simulation; in order to do so, a special function O.bodies.append (also see Omega::bodies and Scene) is called:

    Yade [14]: O.bodies.append(s)            # adds particle s to the simulation; returns id of the particle(s) added
    Out[14]: 0

## Packs

There are functions to generate a specific arrangement of particles in the pack module; for instance, cloud (random loose packing) of spheres can be generated with the pack.SpherePack class:

    Yade [15]: from yade import pack

    Yade [16]: sp=pack.SpherePack()                   # create an empty cloud; SpherePack contains only geometrical information

    Yade [17]: sp.makeCloud((1,1,1),(2,2,2),rMean=.2) # put spheres with defined radius inside box given by corners (1,1,1) and (2,2,2)
    Out[17]: 7

    Yade [18]: for c,r in sp: print(c,r)               # print center and radius of all particles (SpherePack is a sequence which can be iterated over)
       ....:
    Vector3(1.790396227341059632,1.371598959954730024,1.376954780275094992) 0.2
    Vector3(1.669691514113762931,1.744153229864833232,1.784337277969396673) 0.2
    Vector3(1.215235809703947734,1.668231783346398656,1.728206835377136708) 0.2
    Vector3(1.515178103742450988,1.712244858531946523,1.221794362008554069) 0.2
    Vector3(1.576813171342277942,1.346503835772326685,1.794132325786250259) 0.2
    Vector3(1.302393576449079937,1.229741232515965565,1.268386044282871161) 0.2
    Vector3(1.207640446928896161,1.22500480454610372,1.669115496529985698) 0.2

    Yade [19]: sp.toSimulation()                      # create particles and add them to the simulation
    Out[19]: [1, 2, 3, 4, 5, 6, 7]

## Boundaries

utils.facet (triangle Facet) and utils.wall (infinite axes-aligned plane Wall) geometries are typically used to define boundaries. For instance, a “floor” for the simulation can be created like this:

    Yade [20]: O.bodies.append(utils.wall(-1,axis=2))
    Out[20]: 8

There are other conveinence functions (like utils.facetBox for creating closed or open rectangular box, or family of ymport functions)

## Look inside

The simulation can be inspected in several ways. All data can be accessed from python directly:

    Yade [21]: len(O.bodies)
    Out[21]: 9

    Yade [22]: O.bodies[10].shape.radius   # radius of body #10 (will give error if not sphere, since only spheres have radius defined)
    [31m---------------------------------------------------------------------------[39m
    [31mIndexError[39m                                Traceback (most recent call last)
    [36mCell[39m[36m [39m[32mIn[22][39m[32m, line 1[39m
    [32m----> [39m[32m1[39m [43mO[49m[43m.[49m[43mbodies[49m[43m[[49m[32;43m10[39;49m[43m][49m.shape.radius   [38;5;66;03m# radius of body #10 (will give error if not sphere, since only spheres have radius defined)[39;00m

    [31mIndexError[39m: Body id out of range.

    Yade [23]: O.bodies[12].state.pos      # position of body #12
    [31m---------------------------------------------------------------------------[39m
    [31mIndexError[39m                                Traceback (most recent call last)
    [36mCell[39m[36m [39m[32mIn[23][39m[32m, line 1[39m
    [32m----> [39m[32m1[39m [43mO[49m[43m.[49m[43mbodies[49m[43m[[49m[32;43m12[39;49m[43m][49m.state.pos      [38;5;66;03m# position of body #12[39;00m

    [31mIndexError[39m: Body id out of range.

Besides that, Yade says this at startup (the line preceding the command-line):

    [[ ^L clears screen, ^U kills line. F12 controller, F11 3d view, F10 both, F9 generator, F8 plot. ]]

Controller

  Pressing `F12` brings up a window for controlling the simulation. Although typically no human intervention is done in large simulations (which run “headless”, without any graphical interaction), it can be handy in small examples. There are basic information on the simulation (will be used later).

3d view

  The 3d view can be opened with F11 (or by clicking on button in the Controller – see below). There is a number of keyboard shortcuts to manipulate it (press `h` to get basic help), and it can be moved, rotated and zoomed using mouse. Display-related settings can be set in the “Display” tab of the controller (such as whether particles are drawn).

Inspector

  Inspector is opened by clicking on the appropriate button in the Controller. It shows (and updates) internal data of the current simulation. In particular, one can have a look at engines, particles (Bodies) and interactions (Interactions). Clicking at each of the attribute names links to the appropriate section in the documentation.

## Engines

Engines define processes undertaken by particles. As we know from the theoretical introduction, the sequence of engines is called simulation loop. Let us define a simple interaction loop:

    Yade [24]: O.engines=[                   # newlines and indentations are not important until the brace is closed
       ....:    ForceResetter(),
       ....:    InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Wall_Aabb()]),
       ....:    InteractionLoop(           # dtto for the parenthesis here
       ....:        [Ig2_Sphere_Sphere_ScGeom(),Ig2_Wall_Sphere_ScGeom()],
       ....:        [Ip2_FrictMat_FrictMat_FrictPhys()],
       ....:        [Law2_ScGeom_FrictPhys_CundallStrack()]
       ....:    ),
       ....:    NewtonIntegrator(damping=.2,label='newtonCustomLabel')      # define a label newtonCustomLabel under which we can access this engine easily
       ....: ]
       ....:

    Yade [25]: O.engines
    Out[25]:
    [<ForceResetter instance at 0x242d7d10>,
     <InsertionSortCollider instance at 0x24f32820>,
     <InteractionLoop instance at 0x25906c70>,
     <NewtonIntegrator instance at 0x24df4490>]

    Yade [26]: O.engines[-1]==newtonCustomLabel    # is it the same object?
    Out[26]: True

    Yade [27]: newtonCustomLabel.damping
    Out[27]: 0.2

Instead of typing everything into the command-line, one can describe simulation in a file (script) and then run yade with that file as an argument. We will therefore no longer show the command-line unless necessary; instead, only the script part will be shown. Like this:

    O.engines=[                   # newlines and indentations are not important until the brace is closed
            ForceResetter(),
            InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Wall_Aabb()]),
            InteractionLoop(           # dtto for the parenthesis here
                     [Ig2_Sphere_Sphere_ScGeom(),Ig2_Wall_Sphere_ScGeom()],
                     [Ip2_FrictMat_FrictMat_FrictPhys()],
                     [Law2_ScGeom_FrictPhys_CundallStrack()]
            ),
            GravityEngine(gravity=(0,0,-9.81)),                    # 9.81 is the gravity acceleration, and we say that
            NewtonIntegrator(damping=.2,label='newtonCustomLabel') # define a label under which we can access this engine easily
    ]

Besides engines being run, it is likewise important to define how often they will run. Some engines can run only sometimes (we will see this later), while most of them will run always; the time between two successive runs of engines is timestep (\(\Dt\)). There is a mathematical limit on the timestep value, called critical timestep, which is computed from properties of particles. Since there is a function for that, we can just set timestep using utils.PWaveTimeStep:

    O.dt=utils.PWaveTimeStep()

Each time when the simulation loop finishes, time `O.time` is advanced by the timestep `O.dt`:

    Yade [28]: O.dt=0.01

    Yade [29]: O.time
    Out[29]: 0.0

    Yade [30]: O.step()

    Yade [31]: O.time
    Out[31]: 0.01

For experimenting with a single simulations, it is handy to save it to memory; this can be achieved, once everything is defined, with:

    O.saveTmp()