O.load Error

Asked by jamespaul

Hi,
Thank you Jan, you have helped me a lot.I successfully loaded the simulation according to the method you taught.

However, an error will be reported at the beginning of loading:

NameError Traceback (most recent call last)
/usr/bin/yade in <module>()
NameError: name 'triaxFinished' is not defined

In my first program, even though it had stopped 'O.pause()' , why did this function appear?I want to correct this error.

Thanks,
James

############################
The first script:

# encoding: utf-8
from yade import pack, qt, plot

sigmaIso=-25000

O.periodic=True

frictionAngle1= 2.862405226
frictionAngle2= 14.03624347

sphere=O.materials.append(FrictMat(young=1e7,poisson=0.3,density=1000,frictionAngle=radians(frictionAngle1),label='sphere'))

multiple=.01
length=1.5

psdSizes=[.18*multiple,.2*multiple,.21*multiple,.25*multiple,.27*multiple,.30*multiple,.31*multiple,.34*multiple,.38*multiple]
psdCumm=[0.11,0.27,0.45,0.73,0.88,0.95,0.98,0.99,1]

sp=pack.SpherePack()
sp.makeCloud((0,0,0),(length*multiple,length*multiple,length*multiple),psdSizes=psdSizes,psdCumm=psdCumm,distributeMass=True,periodic=True)
n=sp.toSimulation()

O.engines=[
 ForceResetter(),
 InsertionSortCollider([Bo1_Sphere_Aabb()]),
 InteractionLoop(
  [Ig2_Sphere_Sphere_ScGeom()],
  [Ip2_FrictMat_FrictMat_FrictPhys()],
  [Law2_ScGeom_FrictPhys_CundallStrack()]
 ),
 PeriTriaxController(label='triax',
  goal=(sigmaIso,sigmaIso,sigmaIso),stressMask=7,
  dynCell=True,maxStrainRate=(1000,1000,1000),
  maxUnbalanced=1e-3,relStressTol=1e-3,
  doneHook='changeMaterial()'
 ),
 NewtonIntegrator(damping=.1),
 PyRunner(command='num()',initRun=1,nDo=1),
]
O.dt=.5*PWaveTimeStep()

def changeMaterial():
 O.cell.trsf=Matrix3.Identity
 for i in n:
         O.bodies[i].material.frictionAngle=radians(frictionAngle2)
 triax.doneHook='triaxFinished()'

def triaxFinished():
    print 'Finished'
    O.pause()

qt.Controller()
O.run()
O.wait()
O.save('four.yade.gz')
O.saveTmp()

#############
My second script:

#!/usr/bin/python
from yade import pack, qt, plot

O.load('four.yade.gz')
#O.run()
O.dt=1e-9

O.engines += [PeriTriaxController(
 goal=(0,0,-1000000),stressMask=4,maxStrainRate=(0,0,.1),
 maxUnbalanced=1e-8,doneHook='functionToRunWhenFinished()')]

O.engines += [PyRunner(command='addPlotData2()',iterPeriod=100000)];

def functionToRunWhenFinished():
    print 'Finished'
    O.pause()

def addPlotData2():
 plot.addData(unbalanced=unbalancedForce(),i=O.iter,
  sxx=-triax.stress[0],syy=-triax.stress[1],szz=-triax.stress[2],
  exx=-triax.strain[0],eyy=-triax.strain[1],ezz=-triax.strain[2],
  q=-triax.stress[2]+0.5*(triax.stress[0]+triax.stress[1]),
  p=-(triax.stress[0]+triax.stress[1]+triax.stress[2])/3,
 )

plot.plots={'i':('unbalanced',),'i ':('sxx','syy','szz'),' i':('ezzs'),' p ':('q'),}
plot.plot()

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,
this is a "classical" problem with O.save / O.load. You define a python function as triax.doneHook. The name of the function is saved and loaded. However, it is not defined in the second script, so when it is about to be called, it results in this error.

Either define the function in the second script or change the doneHook

please provide a WORKING example. I got "NameError: name 'num' is not defined"

cheers
Jan

Revision history for this message
jamespaul (jamespauljames) said :
#2

I am so sorry about that,Jan. I've modified my code and now it runs.

############################
The first script:

# encoding: utf-8
from yade import pack, qt, plot

sigmaIso=-25000

O.periodic=True

frictionAngle1= 2.862405226
frictionAngle2= 14.03624347

sphere=O.materials.append(FrictMat(young=1e7,poisson=0.3,density=1000,frictionAngle=radians(frictionAngle1),label='sphere'))

multiple=.01
length=1.5

psdSizes=[.18*multiple,.2*multiple,.21*multiple,.25*multiple,.27*multiple,.30*multiple,.31*multiple,.34*multiple,.38*multiple]
psdCumm=[0.11,0.27,0.45,0.73,0.88,0.95,0.98,0.99,1]

sp=pack.SpherePack()
sp.makeCloud((0,0,0),(length*multiple,length*multiple,length*multiple),psdSizes=psdSizes,psdCumm=psdCumm,distributeMass=True,periodic=True)
n=sp.toSimulation()

O.engines=[
 ForceResetter(),
 InsertionSortCollider([Bo1_Sphere_Aabb()]),
 InteractionLoop(
  [Ig2_Sphere_Sphere_ScGeom()],
  [Ip2_FrictMat_FrictMat_FrictPhys()],
  [Law2_ScGeom_FrictPhys_CundallStrack()]
 ),
 PeriTriaxController(label='triax',
  goal=(sigmaIso,sigmaIso,sigmaIso),stressMask=7,
  dynCell=True,maxStrainRate=(1000,1000,1000),
  maxUnbalanced=1e-3,relStressTol=1e-3,
  doneHook='changeMaterial()'
 ),
 NewtonIntegrator(damping=.1),
]
O.dt=.5*PWaveTimeStep()

def changeMaterial():
 O.cell.trsf=Matrix3.Identity
 for i in n:
         O.bodies[i].material.frictionAngle=radians(frictionAngle2)
 triax.doneHook='triaxFinished()'

def triaxFinished():
    print 'Finished'
    O.pause()

qt.Controller()
O.run()
O.wait()
O.save('four.yade.gz')
O.saveTmp()

#############
My second script:

#!/usr/bin/python
from yade import pack, qt, plot

O.load('four.yade.gz')
#O.run()
O.dt=1e-9

O.engines += [PeriTriaxController(
 goal=(0,0,-1000000),stressMask=4,maxStrainRate=(0,0,.1),
 maxUnbalanced=1e-8,doneHook='functionToRunWhenFinished()')]

O.engines += [PyRunner(command='addPlotData2()',iterPeriod=100000)];

def functionToRunWhenFinished():
    print 'Finished'
    O.pause()

def addPlotData2():
 plot.addData(unbalanced=unbalancedForce(),i=O.iter,
  sxx=-triax.stress[0],syy=-triax.stress[1],szz=-triax.stress[2],
  exx=-triax.strain[0],eyy=-triax.strain[1],ezz=-triax.strain[2],
  q=-triax.stress[2]+0.5*(triax.stress[0]+triax.stress[1]),
  p=-(triax.stress[0]+triax.stress[1]+triax.stress[2])/3,
 )

plot.plots={'i':('unbalanced',),'i ':('sxx','syy','szz'),' i':('ezzs'),' p ':('q'),}
plot.plot()

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

Thanks for the code

> However, an error will be reported at the beginning of loading:

the loading is ok, actually too ok :-) see my previous answer. The problem is missing triaxFinished in the second script. Solutions:
1) define triaxFinished in the second script, e.g.
###
def triaxFinished():
   O.pause()
###
2) change the doneHook of triax
###
def someNewDomeHook():
   print "STOP"
   O.pause()
O.engines[3].doneHook = "someNewDomeHook()"
###

> O.engines += [PeriTriaxController(...)]

Are you aware that now you have 2 PeriTriaxController simultaneously in the simulation? Probably you should do something like:
###
O.engines = O.engines[:3] + [PeriTriaxController(...)] + O.engines[4:]
###

cheers
Jan

Revision history for this message
jamespaul (jamespauljames) said :
#4

Thanks Jan so much.Your tutorial is very detailed.

> Are you aware that now you have 2 PeriTriaxController simultaneously in the simulation?

 What is the effect of 2 PeriTriaxController?

>I changed my second script. I define triaxFinished again.Is there only one PeriTriaxController?

#!/usr/bin/python
from yade import pack, qt, plot

O.load('four.yade.gz')
O.run()
O.dt=1e-13

O.engines += [PyRunner(command='addPlotData2()',iterPeriod=100000)];

def triaxFinished():
 O.cell.trsf=Matrix3.Identity
 triax.goal=(0,0,-1000000)
 triax.stressMask=4
 triax.maxStrainRate=(0,0,.01)
 triax.maxUnbalanced=1e-8
 triax.doneHook='functionToRunWhenFinished()'

def functionToRunWhenFinished():
    print 'Finished'
    O.pause()

def addPlotData2():
 plot.addData(unbalanced=unbalancedForce(),i=O.iter,
  sxx=-triax.stress[0],syy=-triax.stress[1],szz=-triax.stress[2],
  exx=-triax.strain[0],eyy=-triax.strain[1],ezz=-triax.strain[2],
  q=-triax.stress[2]+0.5*(triax.stress[0]+triax.stress[1]),
  p=-(triax.stress[0]+triax.stress[1]+triax.stress[2])/3,
 )

plot.plots={'i':('unbalanced',),'i ':('sxx','syy','szz'),' i':('ezz'),' p ':('q'),}
plot.plot()

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

> What is the effect of 2 PeriTriaxController?

no idea, I have never tried it :-D **just guessing**, I would say the second one overwrites values set by the first one. But again, this is only guessing

> Is there only one PeriTriaxController?

it seems yes. You can try yoursef:
print O.engines

Jan

Revision history for this message
jamespaul (jamespauljames) said :
#6

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