Question about FEM and DEM coupling

Asked by Kun Zeng

I change the code of the example1-yade.py file in [1] to the following code:

from libyade import yade
from yade import polyhedra_utils
from yade import ymport
from yade import *

widthl = .3
widthr = .3
widthc = .3
height = .3
thick = .5
deep = -.2
sizeMin = 40e-3
sizeMax = 60e-3

frictionAngle = .5
young = 1e8 # stiffness

dt = 1e-3 # time step

nGravityDeposition = 250 # how long to run initial gravity deposition
nCycles = 3 # how many jumps to run afterwards
nStepsBetweenCycles = 200 # number of time steps between jumps
dspl = 20e-3

width = widthl+widthc+widthr

matS = O.materials.append(FrictMat(young=1e5,density=5000))

mat = PolyhedraMat(young=young,poisson=10,frictionAngle=frictionAngle)
O.materials.append(mat)
O.engines = [
 ForceResetter(),
 InsertionSortCollider([Bo1_Polyhedra_Aabb(),Bo1_Facet_Aabb()]),
 InteractionLoop(
  [Ig2_Polyhedra_Polyhedra_PolyhedraGeom(), Ig2_Facet_Polyhedra_PolyhedraGeom()],
  [Ip2_FrictMat_FrictMat_FrictPhys(),Ip2_PolyhedraMat_PolyhedraMat_PolyhedraPhys()],
  [Law2_PolyhedraGeom_PolyhedraPhys_Volumetric(), Law2_ScGeom_FrictPhys_CundallStrack()]),
 NewtonIntegrator(gravity=(0,0,-9.8)),
]

# box
p000 = Vector3(0,0,0)
p100 = Vector3(widthl,0,0)
p200 = Vector3(widthl+widthc,0,0)
p300 = Vector3(widthl+widthc+widthr,0,0)
pxs = (p000,p100,p200,p300)
p001,p101,p201,p301 = [p+Vector3(0,0,height) for p in pxs]
p010,p110,p210,p310 = [p+Vector3(0,thick,0) for p in pxs]
p011,p111,p211,p311 = [p+Vector3(0,thick,height) for p in pxs]
p00b,p10b,p20b,p30b = [p+Vector3(0,0,deep) for p in pxs]
p01b,p11b,p21b,p31b = [p+Vector3(0,thick,deep) for p in pxs]
def rect(vs,**kw):
 v1,v2,v3,v4 = vs
 return [
  facet((v1,v2,v3),**kw),
  facet((v1,v3,v4),**kw),
 ]
movables = rect((p100,p200,p210,p110)) # bottom center
rects = (
 (p000,p100,p110,p010), # bottom left
 (p200,p300,p310,p210), # bottom left
 (p000,p010,p011,p001), # left
 (p300,p310,p311,p301), # right
 (p000,p100,p101,p001), # front left
 (p100,p200,p201,p101), # front center
 (p200,p300,p301,p201), # front right
 (p010,p110,p111,p011), # back left
 (p110,p210,p211,p111), # back center
 (p210,p310,p311,p211), # back right
 (p100,p200,p20b,p10b), # front center below
 (p110,p210,p21b,p11b), # back center below
 (p100,p110,p11b,p10b), # left below
 (p200,p210,p21b,p20b), # right below
)
rects = movables + sum((rect(r) for r in rects),[])
O.bodies.append(rects)

O.bodies.append(yade.ymport.textPolyhedra('try2.txt', mat,scale=1.0))

And I run the example1.sh it shows:

Traceback (most recent call last):
  File "example1.py", line 90, in <module>
    dem = YADE_API(demName)
  File "/home/kun/femdem/include/mupif/api/yade/yade_interface.py", line 153, in __init__
    execfile(inputfile)
  File "example1-yade.py", line 56, in <module>
    p000 = vector(0,0,0)
NameError: name 'vector' is not defined

and also :

Traceback (most recent call last):
  File "example1.py", line 90, in <module>
    dem = YADE_API(demName)
  File "/home/kun/femdem/include/mupif/api/yade/yade_interface.py", line 153, in __init__
    execfile(inputfile)
  File "example1-yade.py", line 72, in <module>
    movables = rect((p100,p200,p210,p110)) # bottom center
  File "example1-yade.py", line 69, in rect
    facet((v1,v2,v3),**kw),
NameError: global name 'facet' is not defined

What errors here?

[1] https://github.com/stranskyjan/dem-fem-coupling

Question information

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

Hello,

> NameError: global name 'facet' is not defined

if you use standalone yade, it imports a lot of stuff (like function facet) at startup to make things easier.
However, if you use yade as a Python module (from libyade import yade), not everything is imported.

e.e. to use facet, you need first import the name:
from yade.utils import facet
f = facet(...)

or

f = yade.utils.facet(...)

> NameError: name 'vector' is not defined

did you mean Vector3?

cheers
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#2

Hi Jan,

Yes, I use Vector3 in my code. But the error shows ' name 'vector' is not defined ' like this: p000 = Vector3(0,0,0).

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

Sorry, have not checked your code in detail, there you have indeed Vector3.. Does the error really says 'vector' is not defined? According to the error, it considered
p000 = vector(0,0,0)
to be in the file.. Are you sure you saved the file to the correct place?
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#4

I run my code again and it still shows :

sh example1.sh
Traceback (most recent call last):
  File "example1.py", line 90, in <module>
    dem = YADE_API(demName)
  File "/home/kun/femdem/include/mupif/api/yade/yade_interface.py", line 153, in __init__
    execfile(inputfile)
  File "example1-yade.py", line 57, in <module>
    p000 = Vector3(0,0,0)
NameError: name 'Vector3' is not defined

Here is my code in the google drive, can you help me to see where is the error in my code? Thanks.

https://drive.google.com/open?id=0BxKuPWLwx3a-b1hNZUhFMl80bWc

Revision history for this message
Kun Zeng (zkbread) said :
#5

Sorry Jan, It is "NameError: name 'Vector3' is not defined" ...

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

Similar to facet, use either
from yade import Vector3
or
yade.Vector3
cheers
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#7

Thanks Jan. I add from yade import Vector3 at the top of my code. it works. However, I changed the facet to:

def rect(vs,**kw):
   v1,v2,v3,v4 = vs
   return [
    f = yade.utiles.facet((v1,v2,v3),**kw),
 ]

Then it shows:

Traceback (most recent call last):
  File "example1.py", line 90, in <module>
    dem = YADE_API(demName)
  File "/home/kun/femdem/include/mupif/api/yade/yade_interface.py", line 153, in __init__
    execfile(inputfile)
  File "example1-yade.py", line 74
    f = yade.utils.facet((v1,v2,v3),**kw),
      ^
SyntaxError: invalid syntax

Is there somethings I still have mistake?

Revision history for this message
Kun Zeng (zkbread) said :
#8

Hi Jan, sorry for post so much questions here.

I thought i figure the facet problem by changing the code to: yade.utils.facet((v1,v2,v3),**kw)

And I run my code it shows:

  File "example1-yade.py", line 94, in <genexpr>
    rects = movables + sum((rect(r) for r in rects),[])
NameError: global name 'rect' is not defined

And I add :

from yade.utils import facet

but it shows:

ImportError: cannot import name rect

What's the problem here? Thank you very much.

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

>NameError: global name 'rect' is not defined
> ...
> from yade.utils import facet
> but it shows:
> ImportError: cannot import name rect

rect is not part of yade, it is user-defined function (see the original script of this thread). Maybe you deleted it by accident?

concerning SyntaxError, the problem itself might have source at different place than the reported line itself (e.g. missed parenthesis), so please post a complete code in that case

cheers
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#10

The complete code of example1-yade.py is as below:

from libyade import yade
from yade import *
from yade import polyhedra_utils
from yade import ymport
from yade import Vector3
from yade.utils import facet

# basic material

# box dimensions
widthl = .3
widthr = .3
widthc = .3
height = .3
thick = .5
deep = -.2

# size of grains
sizeMin = 40e-3
sizeMax = 60e-3

frictionAngle = .5
young = 1e8 # stiffness

dt = 1e-3 # time step

nGravityDeposition = 250 # how long to run initial gravity deposition
nCycles = 3 # how many jumps to run afterwards
nStepsBetweenCycles = 200 # number of time steps between jumps
dspl = 20e-3

# how much larger the initial make box should be
fillBoxHFactor = 3
width = widthl+widthc+widthr

matS = O.materials.append(FrictMat(young=1e5,density=5000))

mat = PolyhedraMat(young=young,poisson=10,frictionAngle=frictionAngle)
O.materials.append(mat)
O.engines = [
 ForceResetter(),
 InsertionSortCollider([Bo1_Polyhedra_Aabb(),Bo1_Facet_Aabb()]),
 InteractionLoop(
  [Ig2_Polyhedra_Polyhedra_PolyhedraGeom(), Ig2_Facet_Polyhedra_PolyhedraGeom()],
  [Ip2_FrictMat_FrictMat_FrictPhys(),Ip2_PolyhedraMat_PolyhedraMat_PolyhedraPhys()],
  [Law2_PolyhedraGeom_PolyhedraPhys_Volumetric(), Law2_ScGeom_FrictPhys_CundallStrack()]),
 NewtonIntegrator(gravity=(0,0,-9.8)),
]

# box
p000 = Vector3(0,0,0)
p100 = Vector3(widthl,0,0)
p200 = Vector3(widthl+widthc,0,0)
p300 = Vector3(widthl+widthc+widthr,0,0)
pxs = (p000,p100,p200,p300)
p001,p101,p201,p301 = [p+Vector3(0,0,height) for p in pxs]
p010,p110,p210,p310 = [p+Vector3(0,thick,0) for p in pxs]
p011,p111,p211,p311 = [p+Vector3(0,thick,height) for p in pxs]
p00b,p10b,p20b,p30b = [p+Vector3(0,0,deep) for p in pxs]
p01b,p11b,p21b,p31b = [p+Vector3(0,thick,deep) for p in pxs]

def rect(vs,**kw):
 v1,v2,v3,v4 = vs
 return [
  yade.utils.facet((v1,v2,v3),**kw),
  yade.utils.facet((v1,v3,v4),**kw),
 ]
movables = rect((p100,p200,p210,p110)) # bottom center
rects = (
 (p000,p100,p110,p010), # bottom left
 (p200,p300,p310,p210), # bottom left
 (p000,p010,p011,p001), # left
 (p300,p310,p311,p301), # right
 (p000,p100,p101,p001), # front left
 (p100,p200,p201,p101), # front center
 (p200,p300,p301,p201), # front right
 (p010,p110,p111,p011), # back left
 (p110,p210,p211,p111), # back center
 (p210,p310,p311,p211), # back right
 (p100,p200,p20b,p10b), # front center below
 (p110,p210,p21b,p11b), # back center below
 (p100,p110,p11b,p10b), # left below
 (p200,p210,p21b,p20b), # right below
)
rects = movables + sum((rect(r) for r in rects),[])
O.bodies.append(rects)

#O.periodic = True
O.bodies.append(yade.ymport.textPolyhedra('try2.txt', mat,scale=1.0))

And the whole files : https://drive.google.com/open?id=0BxKuPWLwx3a-b1hNZUhFMl80bWc

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

Thanks for the files, what is the problem now (I did not face any)?
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#12

Hi Jan,

I run the .sh file and it still shows:

Traceback (most recent call last):
  File "example1.py", line 90, in <module>
    dem = YADE_API(demName)
  File "/home/kun/femdem/include/mupif/api/yade/yade_interface.py", line 153, in __init__
    execfile(inputfile)
  File "example1-yade.py", line 94, in <module>
    rects = movables + sum((rect(r) for r in rects),[])
  File "example1-yade.py", line 94, in <genexpr>
    rects = movables + sum((rect(r) for r in rects),[])
NameError: global name 'rect' is not defined

Thanks.

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

Hi Kun,
so there must be some inconsistency. In the last code you posted there is
###
def rect(vs,**kw):
 v1,v2,v3,v4 = vs
 return [
  yade.utils.facet((v1,v2,v3),**kw),
  yade.utils.facet((v1,v3,v4),**kw),
 ]
###

so rect IS defined..

Sorry for late question, but what is your goal of this approach? The main idea in the examples are that you do not create facets in Yade manually, but rather they are created as a copy of surface of given FEM mesh..

cheers
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#14

I would like to generate a beam by FEM and falling down on the Polyhedra particles layers which I generated by Yade before this coupling(ymport into this coupling). And when I run this model, It shows : NameError: global name 'rect' is not defined.

And if i remove the #box part from this code, after running this code, see from paraview, there is no boundary to restrict this particles and all this particles fall down from the beginning.

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

As I wrote, I have tried your code and had no problem and I don't see where this NameError could come from, since rect is defined in the code..
please post here once more exactly the code that produces the problem.
Thanks
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#16

Hi Jan,

I do used this code in my example1-yade.py file and it still same error: NameError: global name 'rect' is not defined...

from libyade import yade
from yade import *
from yade import polyhedra_utils
from yade import ymport
from yade import Vector3
from yade.utils import facet

# basic material

# box dimensions
widthl = .3
widthr = .3
widthc = .3
height = .3
thick = .5
deep = -.2

# size of grains
sizeMin = 40e-3
sizeMax = 60e-3

frictionAngle = .5
young = 1e8 # stiffness

dt = 1e-3 # time step

nGravityDeposition = 250 # how long to run initial gravity deposition
nCycles = 3 # how many jumps to run afterwards
nStepsBetweenCycles = 200 # number of time steps between jumps
dspl = 20e-3

# how much larger the initial make box should be
fillBoxHFactor = 3
width = widthl+widthc+widthr

matS = O.materials.append(FrictMat(young=1e5,density=5000))

mat = PolyhedraMat(young=young,poisson=10,frictionAngle=frictionAngle)
O.materials.append(mat)
O.engines = [
 ForceResetter(),
 InsertionSortCollider([Bo1_Polyhedra_Aabb(),Bo1_Facet_Aabb()]),
 InteractionLoop(
  [Ig2_Polyhedra_Polyhedra_PolyhedraGeom(), Ig2_Facet_Polyhedra_PolyhedraGeom()],
  [Ip2_FrictMat_FrictMat_FrictPhys(),Ip2_PolyhedraMat_PolyhedraMat_PolyhedraPhys()],
  [Law2_PolyhedraGeom_PolyhedraPhys_Volumetric(), Law2_ScGeom_FrictPhys_CundallStrack()]),
 NewtonIntegrator(gravity=(0,0,-9.8)),
]

# box
p000 = Vector3(0,0,0)
p100 = Vector3(widthl,0,0)
p200 = Vector3(widthl+widthc,0,0)
p300 = Vector3(widthl+widthc+widthr,0,0)
pxs = (p000,p100,p200,p300)
p001,p101,p201,p301 = [p+Vector3(0,0,height) for p in pxs]
p010,p110,p210,p310 = [p+Vector3(0,thick,0) for p in pxs]
p011,p111,p211,p311 = [p+Vector3(0,thick,height) for p in pxs]
p00b,p10b,p20b,p30b = [p+Vector3(0,0,deep) for p in pxs]
p01b,p11b,p21b,p31b = [p+Vector3(0,thick,deep) for p in pxs]

def rect(vs,**kw):
 v1,v2,v3,v4 = vs
 return [
  yade.utils.facet((v1,v2,v3),**kw),
  yade.utils.facet((v1,v3,v4),**kw),
 ]
movables = rect((p100,p200,p210,p110)) # bottom center
rects = (
 (p000,p100,p110,p010), # bottom left
 (p200,p300,p310,p210), # bottom left
 (p000,p010,p011,p001), # left
 (p300,p310,p311,p301), # right
 (p000,p100,p101,p001), # front left
 (p100,p200,p201,p101), # front center
 (p200,p300,p301,p201), # front right
 (p010,p110,p111,p011), # back left
 (p110,p210,p211,p111), # back center
 (p210,p310,p311,p211), # back right
 (p100,p200,p20b,p10b), # front center below
 (p110,p210,p21b,p11b), # back center below
 (p100,p110,p11b,p10b), # left below
 (p200,p210,p21b,p20b), # right below
)
rects = movables + sum((rect(r) for r in rects),[])
O.bodies.append(rects)

#O.periodic = True
O.bodies.append(yade.ymport.textPolyhedra('try2.txt', mat,scale=1.0))

Revision history for this message
Kun Zeng (zkbread) said :
#17

The whole outcome is this:

Traceback (most recent call last):
  File "example1.py", line 90, in <module>
    dem = YADE_API(demName)
  File "/home/kun/femdem/include/mupif/api/yade/yade_interface.py", line 153, in __init__
    execfile(inputfile)
  File "example1-yade.py", line 94, in <module>
    rects = movables + sum((rect(r) for r in rects),[])
  File "example1-yade.py", line 94, in <genexpr>
    rects = movables + sum((rect(r) for r in rects),[])
NameError: global name 'rect' is not defined

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

Ok, I have read the previous pssages in detail

> I would like to generate a beam by FEM and falling down on the Polyhedra particles layers which I generated by Yade before this coupling(ymport into this coupling)

In this case, you should run the "preprocessing" phase in advance with standalone yade, like it is done in example 2. Then you should not use facets directly in Yade when running the coupling (should be generated automatically based on given FEM mesh).

cheers
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#19

Hi Jan,

You mean the file named " example2-yade-prepoc.py" ? In my simualtion, I use this code to do preparation of the polyhedara particle layers:

https://drive.google.com/open?id=0BxKuPWLwx3a-LVdwSnlpd2htOXc

This is a code in the example in yade polyhedra and I changed a little bit and export it by using "yade.export.textpolyhedra()"

And In my coupling code, and the file "example1-yade.py", I used the ymport to import the polyhedra particles layer and this no boundary to confine this particle as the similar condition in the final condition in the try2.py simulation. Since this is a error in the "def of rect" part. Since it shows: NameError: global name 'rect' is not defined in the sentence "rects = movables + sum((rect(r) for r in rects),[])". Is there something wrong with the rect(r) here? Thanks.

Kun

Revision history for this message
Kun Zeng (zkbread) said :
#20

Hi Jan,

Sorry to post so many comments and questions above. I changed the NameError part in my code into this:

rects = (movables + rect((p000,p100,p110,p010)) + rect((p200,p300,p310,p210)) +rect((p000,p010,p011,p001)) + rect((p300,p310,p311,p301)) + rect((p000,p100,p101,p001)) + rect((p100,p200,p201,p101)) + rect((p200,p300,p301,p201)) + rect((p010,p110,p111,p011)) + rect((p110,p210,p211,p111)) + rect((p210,p310,p311,p211)) + rect((p100,p200,p20b,p10b)) + rect((p110,p210,p21b,p11b)) + rect((p100,p110,p11b,p10b)) + rect((p200,p210,p21b,p20b)))
#rects = movables + sum((rect(r) for r in rects),[])
#config.permutations = [[1] + list(x) for x in config.permutations]
O.bodies.append(rects)

(1) This works and could run successfully. However I am not sure if it is correct way?

(2) And I have a question to assign the material properties and boundary conditions(like gravity) of the FEM element(I use the salome to generate and mesh a cylinder). But I don't know how to apply the gravity to this cylinder. I know the -unv2oofem.py should work for this purpose but how can I change the example1-unv2oofem.py file to apply the gravity on the cylinder? (Should I open an another question for this one?)

Thank you very much.

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

(1) yes, the two pieces of code are equivalent, but still you use rect(...), so I see no reason why this eorks and the previous did not :-D

(2) as FEM part is not related to YADE, please do not open a new question here, but rather write me to personal email or open an issue on the github project.

cheers
Jan

Revision history for this message
Kun Zeng (zkbread) said :
#22

Thanks Jan

Revision history for this message
Kun Zeng (zkbread) said :
#23

Thanks Jan Stránský, that solved my question.