All data of the simulation are accessible from python; when you open the Inspector, blue labels of various data can be clicked – left button for getting to the documentation, middle click to copy the name of the object (use Ctrl-V or middle-click to paste elsewhere). The interesting objects are among others (see Omega for a full list):
Engines are accessed by their index (position) in the simulation loop:
O.engines[0] # first engine
O.engines[-1] # last engine
Note
The index can change if O.engines is modified. Labeling introduced below is a better solution for reliable access to a particular engine.
Bodies are identified by their id, which is guaranteed to not change during the whole simulation:
O.bodies[0] # first body
[b.shape.radius in O.bodies if isinstance(b.shape,Sphere)] # list of radii of all spherical bodies
sum([b.state.mass for b in O.bodies]) # sum of masses of all bodies
Generalized forces (forces, torques) acting on each particle. They are (usually) reset at the beginning of each step with ForceResetter, subsequently forces from individual interactions are accumulated in InteractionLoop. To access the data, use:
O.forces.f(0) # force on #0
O.forces.t(1) # torque on #1
Interactions are identified by ids of the respective interacting particles (they are created and deleted automatically during the simulation):
O.interactions[0,1] # interactions of #0 with #1
O.interactions[1,0] # the same object
O.bodies[0].intrs # all interactions of body #0
Engines and functors can be labeled, which means that python variable of that name is automatically created.
Yade [1]: O.engines=[
...: NewtonIntegrator(damping=.2,label='newton')
...: ]
...:
Yade [2]: newton.damping=.4
Yade [3]: O.engines[0].damping # O.engines[0] and newton are the same objects
Out[3]: 0.4
Exercises
Find meaning of this expression:
max([b.state.vel.norm() for b in O.bodies])
Run the gravity deposition script, pause after a few seconds of simulation. Write expressions that compute
Useful measures of what happens in the simulation globally:
Evaluating energy data for all components in the simulation (such as gravity work, kinetic energy, plastic dissipation, damping dissipation) can be enabled with
O.trackEnergy=True
Subsequently, energy values are accessible in the O.energy; it is a dictionary where its entries can be retrived with keys() and their values with O.energy[key].
To save data that we just learned to access, we need to call Python from within the simulation loop. PyRunner is created just for that; it inherits periodicy control from PeriodicEngine and takes the code to run as text (must be quoted, i.e. inside '...') attributed called command. For instance, adding this to O.engines will print the current step number every second:
O.engines=O.engines+[ PyRunner(command='print O.iter',realPeriod=1) ]
Writing complicated code inside command is awkward; in such case, we define a function that will be called:
def myFunction():
'''Print step number, and pause the simulation is unbalanced force is smaller than 0.05.'''
print O.iter
if utils.unbalancedForce()<0.05:
print 'Unbalanced force is smaller than 0.05, pausing.'
O.pause()
O.engines=[
# ...
PyRunner(command='myFunction()',iterPeriod=100) # call myFunction every 100 steps
]
Exercises
Yade provides the plot module used for storing and plotting variables (plotting itself will be discussed later). Periodic storing of data is done with PyRunner and the plot.addData function, for instance:
from yade import plot
O.engines=[ # ...,
PyRunner(command='addPlotData()',realPeriod=2) # call the addPlotData function every 2 seconds of human time
]
def addPlotData():
# this function adds current values to the history of data, under the names specified
plot.addData(i=O.iter,t=O.time,Ek=utils.kineticEnergy(),coordNum=utils.avgNumInteractions(),unForce=utils.unbalancedForce())
History is stored in plot.data, and can be accessed using the variable name, e.g. plot.data['Ek'], and saved to text file (for post-processing outside yade) with plot.saveTxt.
plot provides facilities for plotting history saved with plot.addData as 2d plots. Data to be plotted are specified using dictionary plot.plots
plot.plots={'t':('coordNum','unForce',None,'Ek')}
History of all values is given as the name used for plot.addData; keys of the dictionary are -axis values, and values are sequence of data on the
axis; the None separates data on the left and right axes (they are scaled independently). The plot itself is created with
plot.plot() # on the command line, F8 can be used as shorthand
While the plot is open, it will be updated periodically, so that simulation evolution can be seen in real-time.
Plotting all energy contributions would be difficult, since names of all energies might not be known in advance. Fortunately, there is a way to handle that in Yade. It consists in two parts:
plot.addData is given all the energies that are currently defined:
plot.addData(i=O.iter,total=O.energy.total(),**O.energy)
The O.energy.total functions, which sums all energies together. The **O.energy is special python syntax for converting dictionary (remember that O.energy is a dictionary) to named functions arguments, so that the following two commands are identical:
function(a=3,b=34) # give arguments as arguments
function(**{'a':3,'b':34}) # create arguments from dictionary
Data to plot are specified using a function that gives names of data to plot, rather than providing the data names directly:
plot.plots={'i':['total',O.energy.keys()]}
where total is the name we gave to O.energy.total() above, while O.energy.keys() will always return list of currently defined energies.
Exercises
See also
Most Examples use plotting facilities of Yade, some of them also track energy of the simulation.