export the sphere in specific position with txt format

Asked by zhao dejin

Hi, everyone
 I am doing a powder bed simulation ,and I need to export the sphere in specific position with VTK format,.Can this be realized in yade?

Best wishes

ZDJ

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
Jérôme Duriez (jduriez) said :
#1

Hello,

Did you try using (e.g. including it in your O.engines) VTKRecorder [*] ?
It seems to me this is exactly what you want

[*] https://yade-dem.org/doc/yade.wrapper.html#yade.wrapper.VTKRecorder, including 'spheres' in recorders attributes

Revision history for this message
zhao dejin (515jingege) said :
#2

Hello,
 Thanks ,Jérôme , I have tried the VTKRecorder,like this :
O.engines=[
                 ForceResetter(),
                 ......
                 ..... ( omit)
                 VTKRecorder (fileName='test',recorders=['sphere',iterPeriod=200])
]

but all sphere are exported through this method ,not in specific position ,like sphere in a box [ 0 ≦ x ≦ 1, 0 ≦ y ≦ 1, 0 ≦ z ≦ 1] .

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

Sorry, I misunderstood this part of your question.

Then VTKExporter.exportSpheres function from export module (and its ids attribute, picking a list of ids that satisfy your position criteria) would help :

https://yade-dem.org/doc/yade.export.html?highlight=vtkexport#yade.export.VTKExporter.exportSpheres

Revision history for this message
zhao dejin (515jingege) said :
#4

Hi ,Jérôme
  I think it's difficult to make it true through this method , I plan to export the txt format ,here is my code , but it's failed, can you guide me ? thanks a lot .

###########code ##############

# !/usr/bin/python
# -*- coding: utf-8 -*-

###########################
# IMPORT MODULES
###########################

from yade import export
from yade import pack
from yade import qt

###########################
# DEFINE MATERIALS
###########################

steel=FrictMat(young=2.2e11,poisson=0.3,density=7800,frictionAngle=0.3,label='steel')
O.materials.append(steel)

###########################
###########################

plank1=O.bodies.append(geom.facetBox(center=(0.00075,0.00025,-0.00005),extents=(0.00075,0.00025,0.00005),wire=True))

plank2=O.bodies.append(geom.facetBox(center=(0.00075,0.0006,0.00005),extents=(0.00075,0.0001,0.00005),wire=False))

plank2=O.bodies.append(geom.facetBox(center=(0.00075,-0.0001,0.00005),extents=(0.00075,0.0001,0.00005),wire=False))

sp=pack.SpherePack()
sp.makeCloud((-0.0005,-0.0002,0.0001),(0,0.0007,0.0006),psdSizes=[0.000017, 0.000025, 0.000032,0.000038, 0.000044, 0.000053],psdCumm=[0, 0.4, 0.56,0.65, 0.85, 1])
ids=sp.toSimulation(color=(0.8,0.8,0.8),material='steel')

......(omit)

###########################
# ENGINES
###########################
O.engines=[
        ForceResetter(),
        InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Facet_Aabb(),Bo1_Box_Aabb()]),
        InteractionLoop(

               [Ig2_Sphere_Sphere_ScGeom(),Ig2_Facet_Sphere_ScGeom(),Ig2_Box_Sphere_ScGeom()],
  [Ip2_FrictMat_FrictMat_FrictPhys()],
  [Law2_ScGeom_FrictPhys_CundallStrack()]

 ),
 GlobalStiffnessTimeStepper(active=1,timeStepUpdateInterval=5,timestepSafetyCoefficient=0.8,defaultDt=PWaveTimeStep()),

 NewtonIntegrator(gravity=(0,0,-9.81),damping=0.5),
 PyRunner(command="exportSelectedSpheres()",iterPeriod=5000),

]

###########################
# DEFINE FUNCATIONS
###########################
def exportSelectedSpheres():
 xCor = O.bodies[i].state.pos[0]
 yCor = O.bodies[i].state.pos[1]
 zCor = O.bodies[i].state.pos[2]

 for i in sp:
  if (0 <= xCor <= 0.000150) & (0 <= yCor <= 0.00050) & (-0.00003 <= zCor <= 0.00007):
   export.text('zdj.txt')
###########################

#set an optimal timestep
O.dt=utils.PWaveTimeStep()
O.usesTimeStepper=True

qt.View()
qt.Controller()

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

Hello,

> export the sphere in specific position with VTK format
> I plan to export the txt format

please be consistent in your requirements :-)

> but it's failed

please always be as specific as possible (e.g. it gives this and that error, it does not save what it should...). In this case, I got
variable 'i' referenced before assignment
which is understandable, because in your function exportSelectedSpheres, "i" is used:
> xCor = O.bodies[i].state.pos[0]
but python don't know what it is..

> I think it's difficult to make it true through this method

this should do what you want (although in the given setting, no sphere satisfy your condition and therefore no vtk file is saved)
###
vtk = export.VTKExporter("zdj")
def exportSelectedSpheres():
   def check(b):
      x,y,z = b.state.pos
      return isinstance(b.shape,Sphere) and 0<=x and x<=0.000150 and 0<=y and y<=0.00050 and -0.00003<=z and z<=0.00007
   ids = [b.id for b in O.bodies if check(b)]
   vtk.exportSpheres(ids=ids)
###

cheers
Jan

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

In the case of a txt format, the approach is very similar:

########################
def exportSelectedSpheres():
   def isOK(b):
      x,y,z = b.state.pos
      return isinstance(b.shape,Sphere) and 0<=x and x<=0.000150 and 0<=y and y<=0.00050 and -0.00003<=z and z<=0.00007
   bodies = filter(isOK, O.bodies)
   def toStr(b):
      x,y,z = b.state.pos
      r = b.shape.radius
      return "{} {} {} {}\n".format(x,y,z,r)
   lines = map(toStr, bodies)
   with open('zdj.txt','w') as f:
      f.writelines(lines)

def exportSelectedSpheres2():
   bodies = []
   for b in O.bodies:
      x,y,z = b.state.pos
      if isinstance(b.shape,Sphere) and 0<=x and x<=0.000150 and 0<=y and y<=0.00050 and -0.00003<=z and z<=0.00007:
         bodies.append(b)
   with open('zdj2.txt','w') as f:
      for b in bodies:
         x,y,z = b.state.pos
         r = b.shape.radius
         f.write("{} {} {} {}\n".format(x,y,z,r))
########################

cheers
Jan

Revision history for this message
zhao dejin (515jingege) said :
#7

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