simple-scene-plot.py 2.02 KB
Newer Older
Anton Gladky's avatar
Anton Gladky committed
1 2
#!/usr/bin/python
# -*- coding: utf-8 -*-
3
from __future__ import print_function
4 5
import matplotlib
matplotlib.use('TkAgg')
6

7
O.engines=[
8
	ForceResetter(),
9
	InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Box_Aabb()]),
10
	InteractionLoop(
11
		[Ig2_Sphere_Sphere_ScGeom(),Ig2_Box_Sphere_ScGeom()],
12
		[Ip2_FrictMat_FrictMat_FrictPhys()],
13
		[Law2_ScGeom_FrictPhys_CundallStrack()]
14
	),
15
	NewtonIntegrator(damping=.2,gravity=(0,0,-9.81)),
16
	###
Václav Šmilauer's avatar
Václav Šmilauer committed
17
	### NOTE this extra engine:
18
	###
Václav Šmilauer's avatar
Václav Šmilauer committed
19 20
	### You want snapshot to be taken every 1 sec (realTimeLim) or every 50 iterations (iterLim),
	### whichever comes soones. virtTimeLim attribute is unset, hence virtual time period is not taken into account.
21
	PyRunner(iterPeriod=20,command='myAddPlotData()')
22
]
23 24 25
O.bodies.append(box(center=[0,0,0],extents=[.5,.5,.5],fixed=True,color=[1,0,0]))
O.bodies.append(sphere([0,0,2],1,color=[0,1,0]))
O.dt=.002*PWaveTimeStep()
26 27 28 29 30 31


############################################
##### now the part pertaining to plots #####
############################################

Václav Šmilauer's avatar
Václav Šmilauer committed
32
from yade import plot
33 34
## we will have 2 plots:
## 1. t as function of i (joke test function)
35
## 2. i as function of t on left y-axis ('|||' makes the separation) and z_sph, v_sph (as green circles connected with line) and z_sph_half again as function of t
36
plot.plots={'i':('t'),'t':('z_sph',None,('v_sph','go-'),'z_sph_half')}
37

38
## this function is called by plotDataCollector
39 40 41
## it should add data with the labels that we will plot
## if a datum is not specified (but exists), it will be NaN and will not be plotted
def myAddPlotData():
42
	sph=O.bodies[1]
43
	## store some numbers under some labels
Anton Gladky's avatar
Anton Gladky committed
44
	plot.addData(t=O.time,i=O.iter,z_sph=sph.state.pos[2],z_sph_half=.5*sph.state.pos[2],v_sph=sph.state.vel.norm())
45
print("Now calling plot.plot() to show the figures. The timestep is artificially low so that you can watch graphs being updated live.")
46
plot.liveInterval=.2
47
plot.plot(subPlots=False)
48 49
O.run(int(2./O.dt));
#plot.saveGnuplot('/tmp/a')
50
## you can also access the data in plot.data['i'], plot.data['t'] etc, under the labels they were saved.