export plot figure and data

Asked by Leonard on 2019-08-20

Hello,
I have 2 questions when I use plot to export data and figure. For a simple example:
from yade import pack, plot
import numpy as np
sandR1=1
sandR2=0.8
sand = FrictMat(young=30e9,poisson=0.3,frictionAngle=radians(30),density=2650.0,label='sand')
O.materials.append(sand)
s1=utils.sphere((0,0,0),radius=sandR1,color=[1,1,1],fixed=True,material='sand')
s2=utils.sphere((0,0,1.8),radius=sandR2,color=[1,1,1],material='sand')
O.bodies.append(s1)
O.bodies.append(s2)
s2.state.blockedDOFs='z'
s2.state.vel = Vector3(0,0,-1e1)
Gl1_Sphere.quality=3
O.engines=[
    ForceResetter(),
    InsertionSortCollider([Bo1_Sphere_Aabb()]),
    InteractionLoop(
        [Ig2_Sphere_Sphere_ScGeom()],
        [Ip2_FrictMat_FrictMat_FrictPhys()],
        [Law2_ScGeom_FrictPhys_CundallStrack()]
    ),
    NewtonIntegrator(damping=0.4),
    PyRunner(iterPeriod=100,command='addPlotData()',label='plotData'),
]

def addPlotData():
    plot.addData(displacement=1.8-O.bodies[1].state.pos[2],force=O.forces.f(1)[2])

plot.plots={'displacement':('force',)}
plot.plot()
#plot.saveDataTxt('example.txt')
#plot.plot(noShow=True).savefig("fig1.pdf")
O.run(1000000,True)

I can correctly obtain a line on the figure.
However, when I want to export the figure like this (referring to [1]):

from yade import pack, plot
import numpy as np
sandR1=1
sandR2=0.8
sand = FrictMat(young=30e9,poisson=0.3,frictionAngle=radians(30),density=2650.0,label='sand')
O.materials.append(sand)

s1=utils.sphere((0,0,0),radius=sandR1,color=[1,1,1],fixed=True,material='sand')
s2=utils.sphere((0,0,1.8),radius=sandR2,color=[1,1,1],material='sand')

O.bodies.append(s1)
O.bodies.append(s2)

s2.state.blockedDOFs='z'
s2.state.vel = Vector3(0,0,-1e1)
Gl1_Sphere.quality=3

O.engines=[
    ForceResetter(),
    InsertionSortCollider([Bo1_Sphere_Aabb()]),
    InteractionLoop(
        [Ig2_Sphere_Sphere_ScGeom()],
        [Ip2_FrictMat_FrictMat_FrictPhys()],
        [Law2_ScGeom_FrictPhys_CundallStrack()]
    ),
    NewtonIntegrator(damping=0.4),
    PyRunner(iterPeriod=100,command='addPlotData()',label='plotData'),
]

def addPlotData():
    plot.addData(displacement=1.8-O.bodies[1].state.pos[2],force=O.forces.f(1)[2])

plot.plots={'displacement':('force',)}
#plot.plot()
#plot.saveDataTxt('example.txt')
plot.plot(noShow=True).savefig("fig1.pdf")
O.run(1000000,True)
I can obtain fig1.pdf but with no line on it (just a blank figure), that is the first question.

Further, the 2nd question is when I uncomment:
plot.saveDataTxt('example.txt')
I got an error as:
plot.saveDataTxt('example.txt')
  File "/usr/lib/x86_64-linux-gnu/yade/py/yade/plot.py", line 642, in saveDataTxt
    for i in range(len(data[vars[0]])):
IndexError: list index out of range
Could you please point out the problem?
Thanks!
[1]https://yade-dev.gitlab.io/trunk/yade.plot.html?highlight=plot%20savedatatxt#module-yade.plot

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Robert Caulk
Solved:
2019-08-20
Last query:
2019-08-20
Last reply:
2019-08-20
Best Robert Caulk (rcaulk) said : #1

Looks like you are saving a figure before running the simulation. So the blank figure would be expected. Try moving your savefig command to after O.run(). I guess it is the same problem with your attempt to plot.saveDataTxt().

Leonard (z2521899293) said : #2

Thanks Robert Caulk, that solved my question.