← Back to team overview

yade-dev team mailing list archive

[svn] r1874 - in trunk: py scripts

 

Author: eudoxos
Date: 2009-07-16 18:59:11 +0200 (Thu, 16 Jul 2009)
New Revision: 1874

Modified:
   trunk/py/plot.py
   trunk/scripts/simple-scene-graph.py
Log:
1. Adapt simple-scene-graph.py to new instance syntax
2. Add support to y2axis to saved gnuplot plots
3. Change interface for yade.plot.addData to take **kw instead of straight dict (still backwards-compatible)


Modified: trunk/py/plot.py
===================================================================
--- trunk/py/plot.py	2009-07-16 15:25:19 UTC (rev 1873)
+++ trunk/py/plot.py	2009-07-16 16:59:11 UTC (rev 1874)
@@ -59,8 +59,9 @@
 def reverseData():
 	for k in data: data[k].reverse()
 
-def addData(d):
-	"""Add data from argument {'name':value,...} to yade.plot.data.
+def addData(*d_in,**kw):
+	"""Add data from arguments name1=value1,name2=value2 to yade.plot.data.
+	(the old {'name1':value1,'name2':value2} is deprecated, but still supported)
 
 	New data will be left-padded with nan's, unspecified data will be nan.
 	This way, equal length of all data is assured so that they can be plotted one against any other.
@@ -71,6 +72,8 @@
 	else: numSamples=0
 	reduceData(numSamples)
 	nan=float('nan')
+	d=(d_in[0] if len(d_in)>0 else {})
+	d.update(**kw)
 	for name in d:
 		if not name in data.keys():
 			data[name]=[nan for i in range(numSamples)] #numpy.array([nan for i in range(numSamples)])
@@ -105,9 +108,13 @@
 		pylab.legend([_p[0] for _p in plots_p_y1],loc=('upper left' if len(plots_p_y2)>0 else 'best'))
 		pylab.ylabel(','.join([_p[0] for _p in plots_p_y1]))
 		if len(plots_p_y2)>0:
+			# try to move in the color palette a little further (magenta is 5th): r,g,b,c,m,y,k
+			origLinesColor=pylab.rcParams['lines.color']; pylab.rcParams['lines.color']='m'
+			# create the y2 axis
 			pylab.twinx()
 			plotLines[p]+=[pylab.plot(*sum([[data[p],data[d[0]],d[1]] for d in plots_p_y2],[]))]
 			pylab.legend([_p[0] for _p in plots_p_y2],loc='upper right')
+			pylab.rcParams['lines.color']=origLinesColor
 			pylab.ylabel(','.join([_p[0] for _p in plots_p_y2]))
 		pylab.xlabel(p)
 		if 'title' in O.tags.keys(): pylab.title(O.tags['title'])
@@ -166,14 +173,27 @@
 	i=0
 	for p in plots:
 		# print p
-		plots_p=[fillNonSequence(o) for o in plots[p]]
+		plots_p=[addPointTypeSpecifier(o) for o in tuplifyYAxis(plots[p])]
 		if term in ['wxt','x11']: fPlot.write("set term %s %d persist\n"%(term,i))
 		else: fPlot.write("set term %s; set output '%s.%d.%s'\n"%(term,baseNameNoPath,i,extension))
 		fPlot.write("set xlabel '%s'\n"%p)
 		fPlot.write("set grid\n")
 		fPlot.write("set datafile missing 'nan'\n")
 		if title: fPlot.write("set title '%s'\n"%title)
-		fPlot.write("plot "+",".join([" %s using %d:%d title '%s(%s)' with lines"%(dataFile,vars.index(p)+1,vars.index(pp[0])+1,pp[0],p) for pp in plots_p])+"\n")
+		y1=True; plots_y1,plots_y2=[],[]
+		for d in plots_p:
+			if d[0]=='|||':
+				y1=False; continue
+			if y1: plots_y1.append(d)
+			else: plots_y2.append(d)
+		fPlot.write("set ylabel '%s'\n"%(','.join([_p[0] for _p in plots_y1]))) 
+		if len(plots_y2)>0:
+			fPlot.write("set y2label '%s'\n"%(','.join([_p[0] for _p in plots_y2])))
+			fPlot.write("set y2tics\n")
+		ppp=[]
+		for pp in plots_y1: ppp.append(" %s using %d:%d title '← %s(%s)' with lines"%(dataFile,vars.index(p)+1,vars.index(pp[0])+1,pp[0],p,))
+		for pp in plots_y2: ppp.append(" %s using %d:%d title '%s(%s) →' with lines axes x1y2"%(dataFile,vars.index(p)+1,vars.index(pp[0])+1,pp[0],p,))
+		fPlot.write("plot "+",".join(ppp)+"\n")
 		i+=1
 	fPlot.close()
 	return baseName+'.gnuplot'

Modified: trunk/scripts/simple-scene-graph.py
===================================================================
--- trunk/scripts/simple-scene-graph.py	2009-07-16 15:25:19 UTC (rev 1873)
+++ trunk/scripts/simple-scene-graph.py	2009-07-16 16:59:11 UTC (rev 1874)
@@ -1,35 +1,20 @@
 #!/usr/local/bin/yade-trunk -x
 # -*- encoding=utf-8 -*-
 
-o=Omega()
-o.initializers=[
+O.initializers=[
 	MetaEngine('BoundingVolumeMetaEngine',[EngineUnit('InteractingSphere2AABB'),EngineUnit('InteractingBox2AABB'),EngineUnit('MetaInteractingGeometry2AABB')])
 	]
-o.engines=[
-	StandAloneEngine('PhysicalActionContainerReseter'),
-	MetaEngine('BoundingVolumeMetaEngine',[
-		EngineUnit('InteractingSphere2AABB'),
-		EngineUnit('InteractingBox2AABB'),
-		EngineUnit('MetaInteractingGeometry2AABB')
-	]),
-	StandAloneEngine('PersistentSAPCollider'),
-	MetaEngine('InteractionGeometryMetaEngine',[
-		EngineUnit('InteractingSphere2InteractingSphere4SpheresContactGeometry'),
-		EngineUnit('InteractingBox2InteractingSphere4SpheresContactGeometry')
-	]),
-	MetaEngine('InteractionPhysicsMetaEngine',[EngineUnit('SimpleElasticRelationships')]),
-	StandAloneEngine('ElasticContactLaw'),
-	DeusExMachina('GravityEngine',{'gravity':[0,0,-9.81]}),
-	MetaEngine('PhysicalActionDamper',[
-		EngineUnit('CundallNonViscousForceDamping',{'damping':0.2}),
-		EngineUnit('CundallNonViscousMomentumDamping',{'damping':0.2})
-	]),
-	MetaEngine('PhysicalActionApplier',[
-		EngineUnit('NewtonsForceLaw'),
-		EngineUnit('NewtonsMomentumLaw'),
-	]),
-	MetaEngine('PhysicalParametersMetaEngine',[EngineUnit('LeapFrogPositionIntegrator')]),
-	MetaEngine('PhysicalParametersMetaEngine',[EngineUnit('LeapFrogOrientationIntegrator')]),
+O.engines=[
+	BexResetter(),
+	BoundingVolumeMetaEngine([InteractingSphere2AABB(),InteractingBox2AABB(),MetaInteractingGeometry2AABB()]),
+	InsertionSortCollider(),
+	InteractionDispatchers(
+		[InteractingSphere2InteractingSphere4SpheresContactGeometry(),InteractingBox2InteractingSphere4SpheresContactGeometry()],
+		[SimpleElasticRelationships()],
+		[ef2_Spheres_Elastic_ElasticLaw()]
+	),
+	GravityEngine(gravity=(0,0,-9.81)),
+	NewtonsDampedLaw(damping=.2),
 	###
 	### NOTE this extra engine:
 	###
@@ -40,12 +25,12 @@
 	### 
 	### The engine _must_ be labeled 'plotDataCollector', so that the reducer may find it and adjust its periods if necessary.
 	###
-	StandAloneEngine('PeriodicPythonRunner',{'realPeriod':1,'realLim':10,'iterPeriod':1000,'iterLim':10000,'command':'myAddPlotData()','label':'plotDataCollector'})
+	PeriodicPythonRunner(iterPeriod=200,command='myAddPlotData()',label='plotDataCollector')
 ]
 from yade import utils
-o.bodies.append(utils.box(center=[0,0,0],extents=[.5,.5,.5],dynamic=False,color=[1,0,0],young=30e9,poisson=.3,density=2400))
-o.bodies.append(utils.sphere([0,0,2],1,color=[0,1,0],young=30e9,poisson=.3,density=2400))
-o.dt=.2*utils.PWaveTimeStep()
+O.bodies.append(utils.box(center=[0,0,0],extents=[.5,.5,.5],dynamic=False,color=[1,0,0],young=30e9,poisson=.3,density=2400))
+O.bodies.append(utils.sphere([0,0,2],1,color=[0,1,0],young=30e9,poisson=.3,density=2400))
+O.dt=.2*utils.PWaveTimeStep()
 
 
 ############################################
@@ -56,22 +41,18 @@
 import yade.plot
 ## we will have 2 plots:
 ## 1. t as function of i (joke test function)
-## 2. z_sph and v_sph (rendered as green triangles, 'g^') as function of t
-yade.plot.plots={'i':('t'),'t':('z_sph','|||',('v_sph','go-'))}
+## 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
+yade.plot.plots={'i':('t'),'t':('z_sph','|||',('v_sph','go-'),'z_sph_half')}
 
-## static var to specify max number of samples we want to have (1000 is default, no change)
-# yade.plot.maxDataLen=1000
-## this function will be called by PlotDataGetter
+## this function is called by plotDataCollector
 ## 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():
 	## store some numbers under some labels
-	sph=o.bodies[1]
-	yade.plot.addData({'t':o.time,'i':o.iter,'z_sph':sph.phys['se3'][2],'v_sph':sqrt(sum([v**2 for v in sph.phys['velocity']]))})
+	sph=O.bodies[1]
+	yade.plot.addData(t=O.time,i=O.iter,z_sph=sph.phys.pos[2],z_sph_half=.5*sph.phys.pos[2],v_sph=sqrt(sum([v**2 for v in sph.phys['velocity']])))
 
-
-
-o.run(int(2./o.dt));
+O.run(int(2./O.dt),True);
 print """Now, you can say
 
  yade.plot.plot()
@@ -79,4 +60,4 @@
 to see figures.
 """
 import yade.plot as yp
-yp.plot()
+yp.saveGnuplot('/tmp/a')