save the height sphere in file

Asked by hafsa

hi,
i have wrote a program of bouncing ball on the soil and i want to save the height of sphere in each time step in a file 'height.txt'
the problem is that the script does not calculate the heights and the time step .
my code is:

#!/usr/bin/python
# coding: utf-8

from numpy import*
from yade import plot

""" add the particule to simulation """
O.bodies.append([utils.sphere((0,0,2),.5)])

""" add a wall to simulation """
O.bodies.append([utils.wall(-1,axis=2)])

O.engines=[
   ForceResetter(),
   InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Wall_Aabb()]),
   InteractionLoop(
       [Ig2_Sphere_Sphere_ScGeom(),Ig2_Wall_Sphere_ScGeom()], # collision geometry
       [Ip2_FrictMat_FrictMat_FrictPhys()], # collision "physics"
       [Law2_ScGeom_FrictPhys_CundallStrack()] # contact law -- apply forces
   ),
   # Apply gravity force to particles. damping: numerical dissipation of energy.
   NewtonIntegrator(gravity=(0,0,-9.81),damping=0.1)
 ]

#fixing step time
O.dt=.5e-4*utils.PWaveTimeStep()

# save the simulation
O.saveTmp()

""" open file"""
fil=open('height.txt', 'w')
fil.write('%e %e\n' % (t/T, height))

""" print the sphere's height """
def height_sphr():
    h = [O.bodies.state.pos[2]]
    fil.write('%d, %.4f\n' % (O.iter,h))

fil.close()

Any ideas why it doesn't work?

Thanks in advance,

Question information

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

Hello,

1)
> Any ideas why it doesn't work?

Your script has errors.
I have tried your code with
NameError: name 't' is not defined
Yade/Python console usually tells you where in the script the problem is

I also don't see any definition of 'T' and 'height'..

O.bodies.state.pos[2] would also result in error..

2)
Yade reads python script line by line and executes what is read. In your case, it would open the file just once

There are several approaches to achieve what you want, e.g. using plot. module [1]
###
O.engines = [
   ...
   PyRunner(iterPeriod=1,command="plotAddData()") # maybe also initRun=True
]

def plotAddData():
   plot.addData(
      t = O.time, # or whatever you want
      height = O.bodies[0].state.pos[2],
   )
   # plot.saveDataTxt("height.txt")
   # you can also save the file each time step, but I don't like the idea :-)
   # alternativelly you can define another PyRunner saveing data e.g. each 100 iterations...

O.run(1000,True) # or something like that
plot.saveDataTxt("height.txt")
###

or, e.g., "manually"
###
with open("height.txt") as f: # I **personally** prefer this syntax to f=open(...)/f.close
   for i in range(1000):
      O.step()
      f.write("{:e} {:e}\n".format(O.time,O.bodies[0].state.pos[2]) # I **personally** prefer .format over % operator
###

cheers
Jan

[1] https://yade-dem.org/doc/user.html#tracking-variables

Revision history for this message
Robert Caulk (rcaulk) said :
#2

Also helpful for Yade newcomers [1]

[1]https://yade-dem.org/doc/tutorial.html

Revision history for this message
hafsa (sebbah.hafsa) said :
#3

thank you Jan , I really appreciate for answer me .
you are right , saving height in each time step is not a good idea .

Revision history for this message
Launchpad Janitor (janitor) said :
#4

This question was expired because it remained in the 'Open' state without activity for the last 15 days.