Yade Class not find in a user defined python class

Asked by gaoxuesong on 2019-05-17

I want to write a data-post class for the result from yade. In my class i used the Sphere class. But when i invoke my class in yade console, the Sphere class is not recognised. I have looked up the information about Importing Yade in other Python applications. It says as following,
    $ cd /path/where/you/want/yadeimport
    $ ln -s /path/to/yade/executable/yade-trunk yadeimport.py
But when i do the second step, the error comes as "operation is not supported".
Here is my code,

for my class file named “yadedatapost.py”
import numpy as np
import os
from yadeimport import *

class yadeDataPost:
    def __init__(self):
        pass
def erase_outbox_sphere(self):
        for eb in O.bodies:
            if isinstance(eb.shape, Sphere):
                    O.bodies.erase(eb.id)

Then i start yade from terminal, input the following code,
import sys
sys.path.append('/mnt/hgfs/virpc/backup')
from yadedatapost import yadeDataPost

dp1 = yadeDataPost()
dp1.erase_outbox_sphere()

The error comes, No Sphere defined. I know the critical step of creating a symbolic link is missing.
Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
gaoxuesong
Solved:
2019-05-20
Last query:
2019-05-20
Last reply:
2019-05-20
gaoxuesong (260582472-9) said : #1

The line i used to create symbolic link is,
ln -s /usr/bin/yade yadeimport.py

Jan Stránský (honzik) said : #2

Hello,

1)
please, post a complete error you get.

2)
> Then i start yade from terminal, input the following code,
> import sys
> sys.path.append('/mnt/hgfs/virpc/backup')
> from yadedatapost import yadeDataPost

I am a bit confused.. if you start yade form terminal, you need no "ln -s"..

3)
Your code is not consistent. According to the indentation, erase_outbox_sphere is a standalone function, but then you call it as a yadeDataPost instance method..

thanks for clarification
Jan

gaoxuesong (260582472-9) said : #3

Sorry, Jan. I make it confucius. I want to write a class for data post-process. In my defined class, the Sphere class from yade is used. When i invoke my class from yade terminal, the error comes.

The code in my defined class yadeDatapost is the following,
import numpy as np
import os
class yadeDataPost:
    def __init__(self):
        pass

    def erase_outbox_sphere(self, box):
        for eb in O.bodies:
            if isinstance(eb.shape, Sphere):
                xi = eb.state.pos[0]
                yi = eb.state.pos[1]
                zi = eb.state.pos[2]
                if (xi<box[0] or xi>box[1] or yi<box[2] or yi>box[3] or zi<box[4] or zi>box[5]):
                    O.bodies.erase(eb.id)
    def __str__(self):
        return 'this class is execute successfully'

The code inputting in the yade terminal,
yade
O.load('./svdir/time-3.5')
import sys
sys.path.append('/mnt/hgfs/virpc/backup')
from yadedatapost import yadeDataPost
dp1 = yadeDataPost()
box = [2.5e-3, 3.0e-3, 0e-3, 1.0e-3, 0.9e-3, 1.5e-3]
dp1.erase_outbox_sphere(box)

The following is the errors,

In [8]: dp1 = yadeDataPost()
   ...:
   ...: box = [2.5e-3, 3.0e-3, 0e-3, 1.0e-3, 0.9e-3, 1.5e-3]
   ...:
   ...: dp1.erase_outbox_sphere(box)
   ...:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/usr/bin/yade in <module>()
      3 box = [2.5e-3, 3.0e-3, 0e-3, 1.0e-3, 0.9e-3, 1.5e-3]
      4
----> 5 dp1.erase_outbox_sphere(box)

/mnt/hgfs/virpc/backup/yadedatapost.py in erase_outbox_sphere(self, box)
     21 def erase_outbox_sphere(self, box):
     22 for eb in O.bodies:
---> 23 if isinstance(eb.shape, Sphere):
     24 xi = eb.state.pos[0]
     25 yi = eb.state.pos[1]

NameError: global name 'Sphere' is not defined

Much thanks.

Jan Stránský (honzik) said : #4

Hello,

thanks for making everything much more clear :-)

solution 1:
###
...
    def erase_outbox_sphere(self, box):
        import yade # even used inside yade, yadedatapost.py module knows nothing about yade, you have to import it
        for eb in O.bodies:
            if isinstance(eb.shape, yade.Sphere): # now yade.Sphere does the trick
...
###

you need either "ln -s" trick or (using Ubuntu or Debian operating system) "sudo apt install python-yade"

solution 2:
###
# yadedatapost.py
Sphere = None # a global module variable, initially set to something irrelevant
...

# yade
...
import yadedatapost
yadedatapost.Sphere = Sphere # before using, set the global module variable to the correct value
...
###

cheers
Jan

gaoxuesong (260582472-9) said : #5

It works. Thanks.