How to save Data in a txt or Gnuplot file

Asked by Katja

Dear all,

I am new to YADE and still have some difficulties. I have written a very simple script. However, I would like to save my plot data into a txt-file (I only wanna save the information about the Penetration Depth (PD) and the normal Stiffness (Kn2), so I can plot them later). I tried working with a PyRunner, but somehow it's not working. Can anybody please help me with that? Below you find my working example.

Thanks a lot,
Soje

!/usr/bin/env python
# encoding: utf-8

from yade import utils, plot, qt
o = Omega()

# Physical parameters
fr = 0.5;rho=2000
tc = 0.0001;
en = 0.7; et = 0.4;
cn = 0.186; cs = 0.186
kn =1000; ks =0.849e-3
tc = 0.0001;
o.dt = 0.1*tc
Rad = 2.0e-3

# Add material
mat1 = O.materials.append(ViscElMat(frictionAngle=fr,cn=cn,cs=cs,kn=kn,ks=ks))
mat2 = O.materials.append(ViscElMat(frictionAngle=fr,cn=cn,cs=cs,kn=10*kn,ks=ks))

# Add spheres
id1 = O.bodies.append(sphere(center=[0,0,0],radius=Rad,material=mat1,fixed=True))
id2 = O.bodies.append(sphere(center=[0,0,2*Rad],radius=Rad,material=mat1,fixed=False))
bodyList = []
for b in O.bodies:
 bodyList.append(b.id)
id3 = O.bodies.append(sphere(center=[0,0.5*Rad,-1e-2+Rad],radius=Rad,material=mat2,fixed=True))

# Import box's geometry
O.bodies.append(geom.facetBox(center=(0,0,0), extents=(1e-2,1e-2,1e-2), wallMask=31, material=mat1))

# Add engines
o.engines = [
  ForceResetter(),
  InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Wall_Aabb(),Bo1_Facet_Aabb()]),
  InteractionLoop(
    [Ig2_Sphere_Sphere_ScGeom(), Ig2_Facet_Sphere_ScGeom(), Ig2_Wall_Sphere_ScGeom()],
    [Ip2_ViscElMat_ViscElMat_ViscElPhys(label='myPhysics')],
    [Law2_ScGeom_ViscElPhys_Basic()],
  ),
  NewtonIntegrator(damping=0,gravity=[0,0,-9.81]),
  DeformControl(),
  PyRunner(command='addPlotData()',iterPeriod=1),
  PyRunner(command='ChangeKn()', virtPeriod=0.1,dead=False,label='changeK'),
  PyRunner(command='CreateClumps()',iterPeriod=1000)
  PyRunner(command='ExportPara()',iterPeriod=1000)

]

#Function change Kn
def ChangeKn():
  pairs = [(i.id1,i.id2) for i in O.interactions]
  for i in range(0,2,1):
    kn2 = O.bodies[i].material.kn * 0.9
    O.bodies[i].mat = ViscElMat(frictionAngle=fr,cn=cn,cs=cs,kn=kn2,ks=ks)
    if kn2 <= 0.3:
      changeK.dead = True

  O.interactions.clear()
  for id1,id2 in pairs: utils.createInteraction(id1,id2)

#Funtion create Clumps
def CreateClumps():
  kn2 = O.bodies[0].mat.kn
  if kn2 <= 0.3:
    if not O.bodies[0].isClumpMember:
      idClump = O.bodies.clump(bodyList)

# Data History and save for Gnuplot
def ExportPara():
 plot.addData(PD=O.interactions[0,1].geom.penetrationDepth)
 plot.addData(kn2 = O.bodies[0,1].mat.kn)
 plot.saveDataTxt("Parameter.txt.bz2")

# Function to add data to plot
def addPlotData():
  pos1 = (O.bodies[id2].state.pos[2]-O.bodies[id1].state.pos[2])-(Rad)
  PD=O.interactions[0,1].geom.penetrationDepth
  kn2 = O.bodies[0].mat.kn
  plot.addData(PD=PD, kn2=-kn2)

plot.plots={'kn2':('PD')}; plot.plot()
plot.plot()

O.run(1)
qt.View()

Question information

Language:
English Edit question
Status:
Answered
For:
Yade Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Jan Stránský (honzik) said :
#1

Hi Soje,

please provide a working example. I have tried this, but got SyntaxError at O.engines (which is easy to fix) but then got
NameError: name 'DeformControl' is not defined

cheers
Jan

Revision history for this message
Katja (kadebro) said :
#2

Hi Jan,

thank you for your fast reply. I'm sorry I forgot to comment out the DeformControl() command. You can just ignore this command. It doesn't effect the Penetration Depth or Stiffness.

The code again (without the wrong PyRunner(command='ExportPara()',iterPeriod=1000) command)

from yade import utils, plot, qt
o = Omega()

# Physical parameters
fr = 0.5;rho=2000
tc = 0.0001;
en = 0.7; et = 0.4;
cn = 0.186; cs = 0.186
kn =1000; ks =0.849e-3
tc = 0.0001;
o.dt = 0.1*tc
Rad = 2.0e-3

# Add material
#mat1 = O.materials.append(ViscElMat(frictionAngle=fr,tc=tc,en=en,et=et,label='myMat'))
mat1 = O.materials.append(ViscElMat(frictionAngle=fr,cn=cn,cs=cs,kn=kn,ks=ks))
mat2 = O.materials.append(ViscElMat(frictionAngle=fr,cn=cn,cs=cs,kn=10*kn,ks=ks))

# Add spheres
id1 = O.bodies.append(sphere(center=[0,0,0],radius=Rad,material=mat1,fixed=True))
id2 = O.bodies.append(sphere(center=[0,0,2*Rad],radius=Rad,material=mat1,fixed=False))
bodyList = []
for b in O.bodies:
 bodyList.append(b.id)
id3 = O.bodies.append(sphere(center=[0,0.5*Rad,-1e-2+Rad],radius=Rad,material=mat2,fixed=True))

# Import box's geometry
O.bodies.append(geom.facetBox(center=(0,0,0), extents=(1e-2,1e-2,1e-2), wallMask=31, material=mat1))

# Add engines
O.engines = [
  ForceResetter(),
  InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Wall_Aabb(),Bo1_Facet_Aabb()]),
  InteractionLoop(
    [Ig2_Sphere_Sphere_ScGeom(), Ig2_Facet_Sphere_ScGeom(), Ig2_Wall_Sphere_ScGeom()],
    [Ip2_ViscElMat_ViscElMat_ViscElPhys(label='myPhysics')],
    [Law2_ScGeom_ViscElPhys_Basic()],
  ),
  NewtonIntegrator(damping=0,gravity=[0,0,-9.81]),
  #DeformControl(),
  PyRunner(command='addPlotData()',iterPeriod=1),
  PyRunner(command='ChangeKn()', virtPeriod=0.1,dead=False,label='changeK'),
  PyRunner(command='CreateClumps()',iterPeriod=1000)
]

#Function change Kn
def ChangeKn():
  pairs = [(i.id1,i.id2) for i in O.interactions]
  for i in range(0,2,1):
    kn2 = O.bodies[i].material.kn * 0.9
    O.bodies[i].mat = ViscElMat(frictionAngle=fr,cn=cn,cs=cs,kn=kn2,ks=ks)
    if kn2 <= 0.3:
      changeK.dead = True

  O.interactions.clear()
  for id1,id2 in pairs: utils.createInteraction(id1,id2)

#Funtion create Clumps
def CreateClumps():
  kn2 = O.bodies[0].mat.kn
  if kn2 <= 0.3:
    if not O.bodies[0].isClumpMember:
      idClump = O.bodies.clump(bodyList)

# Function to add data to plot
def addPlotData():
  pos1 = (O.bodies[id2].state.pos[2]-O.bodies[id1].state.pos[2])-(Rad)
  PD=O.interactions[0,1].geom.penetrationDepth
  kn2 = O.bodies[0].mat.kn
  plot.addData(PD=PD, kn2=-kn2)

#plot.plots={'time':('PD')}; plot.plot()
plot.plots={'kn2':('PD')}; plot.plot()
plot.plot()

O.run(1)
qt.View()

Revision history for this message
Katja (kadebro) said :
#3

Dear Jan,

Did it work now? Do you have any idea how I can save my PD-data and my Kn2-data from every loop in a seperate .txt-file? Schould I work with a PyRunner or is there any other possibility? I am sorry that I'm so helpless.

Warm regards
Sophie

Revision history for this message
Jan Stránský (honzik) said :
#4

yes, it works, thanks.

> Do you have any idea how I can save my PD-data and my Kn2-data from every loop in a seperate .txt-file? Schould I work with a PyRunner or is there any other possibility?

I don't quite understand the formulation "from every loop in a seperate .txt-file". Anyway, anything is possible :-) so please specify more your needs (ideally with an example, after 10 iterations I expect to have these files, after 20 iterations these files with this conent...)
thanks
Jan

Revision history for this message
Luc Scholtès (luc) said :
#5

Hello Sophie,

I think that something is missing somewhere in your script.

You need to call for the data to be saved with, for example [1]:

plot.saveDataTxt('nameOfTextFile')

If you put that at the end of your addPlotData() function, the file will be created every time the function is called.

If you put that at the end of your script, the file will be created at the end of your simulation.

plot.saveGnuplot() should work too [2]

[1] https://yade-dem.org/doc/yade.plot.html?highlight=save#yade.plot.saveDataTxt
[2] https://yade-dem.org/doc/yade.plot.html?highlight=gnuplot#yade.plot.saveGnuplot

Can you help with this problem?

Provide an answer of your own, or ask Katja for more information if necessary.

To post a message you must log in.