call a class from yade

Asked by JOHN

Good evening,
What i am trying to do is bundle the yade simulation in a class which works

from yade.pack import *
from yade import ymport
import math
class simul():
 def run(self):
  O.bodies.append([
   sphere((0,0,4),1),
   sphere(center=(0,0,0),radius=.5,fixed=True)
   ])

  O.engines=[
   ForceResetter(),
   InsertionSortCollider([Bo1_Sphere_Aabb()]),
   InteractionLoop(
    [Ig2_Sphere_Sphere_ScGeom()],
    [Ip2_FrictMat_FrictMat_FrictPhys()],
    [Law2_ScGeom_FrictPhys_CundallStrack()]
     ),
  NewtonIntegrator(gravity=(0,0,-9.81),damping=0.1),
  ]
  O.dt=.5e-4*PWaveTimeStep()

#simulation=simul()
#simulation.run()

and call it from another file

import math
import operator
import gts
from yade.pack import *
from yade import ymport

from myclass import simul
simulation=simul()
simulation.run()

i get this

yade run.py

Welcome to Yade 1.20.0
TCP python prompt on localhost:9000, auth cookie `ucsdsy'
XMLRPC info provider on http://localhost:21000
Running script run.py
Traceback (most recent call last):
  File "/usr/bin/yade", line 182, in runScript
    execfile(script,globals())
  File "run.py", line 10, in <module>
    from myclass import simul
ImportError: No module named myclass
[[ ^L clears screen, ^U kills line. F12 controller, F11 3d view (use h-key for showing help), F10 both, F9 generator, F8 plot. ]]
[0;34mYade [[1;34m1[0;34m]: [0m

Thank you for the help
(PS, amazing community)

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Hongyang Cheng
Solved:
Last query:
Last reply:

This question was reopened

  • by JOHN
Revision history for this message
Deepak (deepak-kn1990) said :
#1

Hi,

If I understand correctly, you intend to run yade under a ''pure python''
environment? If that's the case you could have a look at these notes in
[1]. You might also want to look at how to instantiate an instance of your
class (having __init()__ method)[2].

An example of python script importing yade as a module is here [3] (this
script is based on one of the example scripts) [4]

[1]
https://yade-dem.org/doc/user.html#importing-yade-in-other-python-applications
[2]https://docs.python.org/2/tutorial/classes.html
[3]https://pastebin.com/J5FsRWQe
[4]
https://github.com/yade/trunk/blob/e4e757f2e98a620e3177b7a36a1d10f69f6a6a28/examples/concrete/triax.py

On Wed, Nov 22, 2017 at 6:08 PM, JOHN <email address hidden>
wrote:

> New question #661017 on Yade:
> https://answers.launchpad.net/yade/+question/661017
>
> Good evening,
> What i am trying to do is bundle the yade simulation in a class which
> works
>
> from yade.pack import *
> from yade import ymport
> import math
> class simul():
> def run(self):
> O.bodies.append([
> sphere((0,0,4),1),
> sphere(center=(0,0,0),radius=.5,fixed=True)
> ])
>
> O.engines=[
> ForceResetter(),
> InsertionSortCollider([Bo1_Sphere_Aabb()]),
> InteractionLoop(
> [Ig2_Sphere_Sphere_ScGeom()],
> [Ip2_FrictMat_FrictMat_FrictPhys()],
> [Law2_ScGeom_FrictPhys_CundallStrack()]
> ),
> NewtonIntegrator(gravity=(0,0,-9.81),damping=0.1),
> ]
> O.dt=.5e-4*PWaveTimeStep()
>
>
>
> #simulation=simul()
> #simulation.run()
>
>
>
>
> and call it from another file
>
>
>
>
>
> import math
> import operator
> import gts
> from yade.pack import *
> from yade import ymport
>
>
> from myclass import simul
> simulation=simul()
> simulation.run()
>
>
>
> i get this
>
> yade run.py
>
> Welcome to Yade 1.20.0
> TCP python prompt on localhost:9000, auth cookie `ucsdsy'
> XMLRPC info provider on http://localhost:21000
> Running script run.py
> Traceback (most recent call last):
> File "/usr/bin/yade", line 182, in runScript
> execfile(script,globals())
> File "run.py", line 10, in <module>
> from myclass import simul
> ImportError: No module named myclass
> [[ ^L clears screen, ^U kills line. F12 controller, F11 3d view (use h-key
> for showing help), F10 both, F9 generator, F8 plot. ]]
> [0;34mYade [[1;34m1[0;34m]: [0m
>
>
> Thank you for the help
> (PS, amazing community)
>
>
>
> --
> You received this question notification because your team yade-users 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
Jan Stránský (honzik) said :
#2

Hello,

> ImportError: No module named myclass

it means that your main script ("another file") does not know where to look for myclass.py . What is location of myclass.py and the main script?
You can also try
###
import sys
sys.path.append('.')
###
before from myclass import simul

cheers
Jan

Revision history for this message
JOHN (washingmachine) said :
#3

hello, and thanks for the quick replies

those two steps solved my problem
best regards
John

Revision history for this message
JOHN (washingmachine) said :
#4

One last tiny thing, is it possible to start the startup yade visualization from python?
Thank you

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

Never tried yade GUI from pure Python.. do you need it?
Not very helpful, sorry :-)
Jan

Revision history for this message
Best Hongyang Cheng (alex-cheng) said :
#6

Maybe you can try from yade import qt and qt.View() and bla, bla.
It worked for me some time ago.

Cheers

Hongyang

Revision history for this message
JOHN (washingmachine) said :
#7

hello all,
I tried importing qt and qt view to no avail,
Still, it is not really important
thanks

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

Hi John,
do you need GUI from Python? if not, do not try it :-) if yes, there are some workaraounds..
cheers
Jan

Revision history for this message
JOHN (washingmachine) said :
#9

Thanks Hongyang Cheng, that solved my question.