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
- Open the terminal, navigate to your home directory
- Create a new empty file and save it in
~/first.py
- Change directory to
/tmp
; delete the file~/first.py
- Run program
xeyes
- Look at the help of Yade.
- Look at the manual page of Yade
- 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):
- Create Body
- Set Body.shape to be an instance of Sphere with some given radius
- Set Body.material (last-defined material is used, otherwise a default material is created)
- 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 0x378acc0>
Yade [10]: s.shape.radius # we already know the Sphere class