yade-users team mailing list archive
-
yade-users team
-
Mailing list archive
-
Message #18811
Re: [Question #677994]: save the height sphere in file
Question #677994 on Yade changed:
https://answers.launchpad.net/yade/+question/677994
Status: Open => Answered
Jan Stránský proposed the following answer:
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
--
You received this question notification because your team yade-users is
an answer contact for Yade.