yade-users team mailing list archive
-
yade-users team
-
Mailing list archive
-
Message #05405
Re: [Question #182459]: wrap a simulation to a class
Question #182459 on Yade changed:
https://answers.launchpad.net/yade/+question/182459
Status: Answered => Open
ceguo is still having a problem:
Hi,
I made the yade library callable from python and now I can run a script
using `python test.py`. The script for a biaxial test (first isotropic
consolidation then undrained shear) is like this:
from yade.wrapper import *
from yade import *
from yade import pack,plot,utils
from miniEigen import Matrix3
O=Omega()
O.materials.append(FrictMat(young=100e6,poisson=.3,frictionAngle=.5,density=2600))
sp = pack.SpherePack()
sp.makeCloud(minCorner=(0,0,0),maxCorner=(3,3,0),rMean=0.03,rRelFuzz=.25,periodic=True,seed=12345)
sp.toSimulation()
O.cell.hSize = Matrix3(3,0,0, 0,3,0, 0,0,1)
print len(O.bodies)
for p in O.bodies:
p.state.blockedDOFs = 'zXY'#['z','rx','ry']
#plot.plots={'ey':('sdev',),'sp':('sdev',),'ey_':('ratio',)}
def saveAddData():
plot.addData(
stress_xx = biax.stress[0],
stress_yy = biax.stress[1],
stress_zz = biax.stress[2],
strain_xx = biax.strain[0],
strain_yy = biax.strain[1]
)
# print O.cell.hSize
O.dt = .5*utils.PWaveTimeStep()
O.engines = [
ForceResetter(),
InsertionSortCollider([Bo1_Sphere_Aabb()]), # detect new collisions
InteractionLoop(
[Ig2_Sphere_Sphere_Dem3DofGeom()],
[Ip2_FrictMat_FrictMat_FrictPhys()],
[Law2_Dem3DofGeom_FrictPhys_CundallStrack()]
),
Peri3dController(
goal=(-5.e4,-5.e4,0,0,0,0),
stressMask=3,
nSteps=20000,
doneHook='print "Consolidation finished."; O.pause()',
maxStrain=.5,
label='biax'
),
NewtonIntegrator()
]
O.run()
O.wait()
#O.materials.append(FrictMat(young=100e6,poisson=1.,frictionAngle=.5,density=2600))
O.engines = [
ForceResetter(),
InsertionSortCollider([Bo1_Sphere_Aabb()]), # detect new collisions
InteractionLoop(
[Ig2_Sphere_Sphere_Dem3DofGeom()],
[Ip2_FrictMat_FrictMat_FrictPhys()],
[Law2_Dem3DofGeom_FrictPhys_CundallStrack()]
),
Peri3dController(
goal=(2.e-2,-2.e-2,0,0,0,0),
stressMask=0,
nSteps=50000,
doneHook='print "Shear finished."; O.pause()',
maxStrain=.5,
label='biax'
),
NewtonIntegrator(),
PyRunner(command='saveAddData()',iterPeriod=500)
]
O.run(); O.wait()
plot.saveDataTxt('result.txt',vars=('stress_xx','stress_yy','stress_zz','strain_xx','strain_yy'))
O.exitNoBacktrace()
This works. Now I want to wrap it to a class named simDEM. I wrote like
this:
from yade.wrapper import *
from yade import *
from yade import pack,plot,utils
from miniEigen import Matrix3
class simDEM(object):
def __init__(self):
self.__O=Omega()
self.__O.materials.append(FrictMat(young=1.e8,poisson=.3,frictionAngle=.5,density=2600))
self.__sp=pack.SpherePack()
self.__sp.makeCloud(minCorner=(0,0,0),maxCorner=(3,3,0),rMean=.03,rRelFuzz=.25,periodic=True,seed=12345)
self.__sp.toSimulation()
self.__O.cell.hSize=Matrix3(3,0,0, 0,3,0, 0,0,1)
for p in self.__O.bodies:
p.state.blockedDOFs='zXY'
self.__O.dt=.5*utils.PWaveTimeStep()
def countParticle(self):
return len(self.__O.bodies)
def saveAddData(self):
plot.addData(
stress_xx = biax.stress[0],
stress_yy = biax.stress[1],
stress_zz = biax.stress[2],
strain_xx = biax.strain[0],
strain_yy = biax.strain[1]
)
def consolidation(self, stress=-5.e4):
self.__O.eigines=[
ForceResetter(),
InsertionSortCollider([Bo1_Sphere_Aabb()]),
InteractionLoop(
[Ig2_Sphere_Sphere_Dem3DofGeom()],
[Ip2_FrictMat_FrictMat_FrictPhys()],
[Law2_Dem3DofGeom_FrictPhys_CundallStrack()]
),
Peri3dController(
goal=(stress,stress,0,0,0,0),
stressMask=3,
nSteps=20000,
doneHook='print "Consolidation finished!"; self.__O.pause()',
maxStrain=.5,
label='biax'
),
NewtonIntegrator()
]
self.__O.run(20000,True)
def shear(self, strain=-1.e-2):
self.__O.eigines=[
ForceResetter(),
InsertionSortCollider([Bo1_Sphere_Aabb()]),
InteractionLoop(
[Ig2_Sphere_Sphere_Dem3DofGeom()],
[Ip2_FrictMat_FrictMat_FrictPhys()],
[Law2_Dem3DofGeom_FrictPhys_CundallStrack()]
),
Peri3dController(
goal=(-1.*strain,strain,0,0,0,0),
stressMask=0,
nSteps=50000,
doneHook='print "Shear finished!"; self.__O.pause()',
maxStrain=.5,
label='biax'
),
NewtonIntegrator(),
PyRunner(command='self.saveAddData()',iterPeriod=500)
]
self.__O.run(50000,True)
def outputResult(self):
plot.saveDataTxt(
'result.txt',
vars=('stress_xx','stress_yy','stress_zz','strain_xx','strain_yy')
)
def finalization(self):
self.__O.exitNoBacktrace()
This doesn't work smoothly within the function consolidation. But it's
very close to what I want. I hope anyone who's more familiar with the
code can help me to modify it. Below is what I got:
$ python
>>> from multiscaleDEM import simDEM
>>> a = simDEM()
>>> a.countParticle()
1342
>>> a.consolidation() # no response, doneHook not executed!
>>> a.shear() # no response, doneHook not executed! Neither PyRunner()
>>> a.outputResult()
Error! vars=('stress_xx','stress_yy','stress_zz','strain_xx','strain_yy')
plot.py in saveDataTxt for i in range(len(data[vars[0]])):
KeyError: 'stress_xx'
--
You received this question notification because you are a member of
yade-users, which is an answer contact for Yade.