How to read data from external file

Asked by rayolau

Hi mates!

I've made a function which calculates the applyinf force to different particles in function of some variables (shape, mass...). However, appliying this function each time step results in a huge computational cost (time) that i can´t afford. So I've decided to call this function each 100000 time steps and save the resulting forces with their corresponding Ids to be constant applied during the 99999 other iterations.

So, which is the best way to deal with the problem??? I was thinking about saving the particle Ids and their corresponding forces in a external file and call it each one of the 99999 remaining iterations. That is made with a defined function that reads the data and assigns the corresponding force to the corresponding particle Id.

But maybe it would be better to use the addData function... Anyways, I don't know the best way to read (not to save) an external file (I'm a python newbie :S).

I would be very grateful for any kind of help.

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

Hello,

I must say that your problem is not very clear to me. Could you please be
more specific or perhaps post a short script describing your problem more
in detail?
Anyway, if computational costs is an issue for you, do not do file
manipulations each time step.
cheers
Jan

2014-02-18 17:51 GMT+01:00 rayolau <email address hidden>:

> New question #244203 on Yade:
> https://answers.launchpad.net/yade/+question/244203
>
> Hi mates!
>
> I've made a function which calculates the applyinf force to different
> particles in function of some variables (shape, mass...). However,
> appliying this function each time step results in a huge computational cost
> (time) that i can´t afford. So I've decided to call this function each
> 100000 time steps and save the resulting forces with their corresponding
> Ids to be constant applied during the 99999 other iterations.
>
> So, which is the best way to deal with the problem??? I was thinking about
> saving the particle Ids and their corresponding forces in a external file
> and call it each one of the 99999 remaining iterations. That is made with a
> defined function that reads the data and assigns the corresponding force to
> the corresponding particle Id.
>
> But maybe it would be better to use the addData function... Anyways, I
> don't know the best way to read (not to save) an external file (I'm a
> python newbie :S).
>
> I would be very grateful for any kind of help.
>
>
>
> --
> You received this question notification because you are a member of
> yade-users, which is an answer contact for Yade.
>
> _______________________________________________
> Mailing list: https://launchpad.net/~yade-users
> Post to : <email address hidden>
> Unsubscribe : https://launchpad.net/~yade-users
> More help : https://help.launchpad.net/ListHelp
>

Revision history for this message
Jérôme Duriez (jduriez) said :
#2

Hi,

For reading a file from Yade, give a look to the python code of ymport.textExt() function, in https://github.com/yade/trunk/blob/master/py/ymport.py from line 13.

The reverse operation (writing in a file data concerning your force) will be easy if you look to export.textExt() in https://github.com/yade/trunk/blob/master/py/export.py, from line 10

But, if you do not quit your yade session, you can indeed use a python variable, that you set / use when needed.

But probably there is a 3rd method, more straightforward, to keep these forces acting on your bodies : give a look to https://www.yade-dem.org/doc/yade.wrapper.html#yade.wrapper.ForceContainer.permF
(I never used it, so I can not tell you more at the moment)

I guess you will succeed !

Jérôme

Revision history for this message
Bruno Chareyre (bruno-chareyre) said :
#3

It sounds like the use of text files would overkill.
This should be enough:

for b in O.bodies:
   #define your force for body b
   O.forces.addF(b.id,force,permanent=True)

O.run(100000,1)

for b in O.bodies:
   #define new forces
   O.forces.addF(b.id,force,permanent=True)

etc.

Revision history for this message
Klaus Thoeni (klaus.thoeni) said :
#4

Maybe use a PyRunner:

def addForces():
 for b in O.bodies: # if the force is only applied to specific forces use a defined list instead of O.bodies
  f=... # your force vector, you can calculate it here
  O.forces.addF(b.id,f,True)

O.engines=[
 PyRunner(initRun=True,iterPeriod=100000,command='addForces()'), # will be called every 100000 time steps
 ForceResetter(),
 ... # your engines
 ]

etc.

Klaus

Can you help with this problem?

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

To post a message you must log in.