Problem with the combined engine

Asked by Przemek on 2019-11-15


I'm new with Yade, and I have problem with combined engine. I want to change the translationAxis and the velocity after a few iterations but I still have an issue about the global variables.
Could you help me?

# -*- coding: utf-8 -*-

from __future__ import print_function
from builtins import range
import random
from yade import ymport
from yade import pack, qt

## PhysicalParameters

## Import geometry
rod = O.bodies.append(ymport.stl('tool.stl',wire=False,color=[0.31,0.47,0.47]))

# Plate dimension
thickness = 1.0 # cm
width = 5.0 # cm
length = 11.0 # cm
boxMax = (2,2.5,0)

# Spheres
sphereRadius = 0.05 # cm
ndiv_x = length/(2*sphereRadius)
ndiv_y = width/(2*sphereRadius)
ndiv_z = thickness/(2*sphereRadius)
#nbSpheres = (ndiv_x,ndiv_y,ndiv_z)
nbSpheres = (40,40,10)#(110,50,10)
print("Creating %d spheres..."%(nbSpheres[0]*nbSpheres[1]*nbSpheres[2]), end=' ')
for i in range(nbSpheres[0]):
 for j in range(nbSpheres[1]):
  for k in range(nbSpheres[2]):
   x = boxMax[0]-2.0*sphereRadius*(i)
   y = boxMax[1]-2.0*sphereRadius*(j)
   z = boxMax[2]-2.0*sphereRadius*(k)
   r = random.uniform(sphereRadius,sphereRadius)
   fixed = False
   if (i==0 or i==nbSpheres[0]-1 or k==nbSpheres[2]-1 or j==0 or j==nbSpheres[1]-1):
    fixed = True

## Estimate time step

def updateParameters():
 aTime = (O.iter*O.dt)
 if aTime>0.005: # fist part
  velVec = Vector3(-1,0,0)
  tranVel = 1.0
 transEngine.translationAxis = velVec
 transEngine.velocity = tranVel

## Engines
 ForceResetter(), # 0
 InsertionSortCollider([ # 1
 InteractionLoop( # 2
 CombinedKinematicEngine(ids=rod,label='combEngine') + TranslationEngine(translationAxis=(0,0,-1),velocity=0.6) + RotationEngine(rotationAxis=(0,0,-1), angularVelocity=20, rotateAroundZero=True, zeroPoint=(0,0,0)),
 NewtonIntegrator(damping=2.0,gravity=[0,0,-9.81]), # 3
 PyRunner(iterPeriod=1, command='updateParameters()'),
 # save data from Yade's own 3d view

import sys,time

print("Start simulation: ")

from yade import qt
v.eyePosition=(6,2,2); v.upVector=(-.4,-.4,.8); v.viewDir=(-.9,-.25,-.3); v.axes=False; v.sceneRadius=20


# this function is called when the simulation is finished
def finish():
 # snapshot is label of qt.SnapshotEngine
 # the 'snapshots' attribute contains list of all saved files

#for t in xrange(2):
# start=time.time();;O.wait();finish=time.time()
# speed=nbIter/(finish-start); print '%g iter/sec\n'%speed
#print "FINISH"


Question information

English Edit question
Yade Edit question
No assignee Edit question
Last query:
Last reply:
Jérôme Duriez (jduriez) said : #1

What is your "issue about the global variables" ? (the error message)

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


> I'm new with Yade

welcome :-)

> I still have an issue about the global variables

please read [1] and next time try:
- to be more specific (like including the error message in the question)
- include a MWE (W=working). If external files (like too.stl file) is involved, try to include it in the question, or preferably not use it and e.g. create a few facets manually instead (if the problem is not related to the stl file). Without it, we cannot try the code ourselves..

I guess the problem is that transEngine is not defined in updateParameters() function. You call it, but you do not define it anywhere in the script and Python interpreter has no clue what it is..

Try one of these:

a) set label to TranslationEngine when it is created
... TranslationEngine(..., label='transEngine')

b) create TranslationEngine before O.engines and then add the instance to CombinedKinematicEngine
transEngine = TranslationEngine(...)
O.engines = [
   CombinedKinematicEngine(...) + transEngine + ...

c) define transEngine in updateParameters() from combEngine content
transEngine = combEngine.comb[0]
transEngine.translationAxis = velVec
transEngine.velocity = tranVel



Przemek (przemekn) said : #3

Sorry, next time I'll adjust to guidelines.

Here is the error:

UnboundLocalError Traceback (most recent call last)
/usr/bin/yade in <module>()

/usr/bin/yade in updateParameters()
     52 tranVel = 1.0
     53 transEngine = combEngine.comb[0]
---> 54 transEngine.translationAxis = velVec
     55 transEngine.velocity = tranVel

UnboundLocalError: local variable 'velVec' referenced before assignment

I tried to modify the script by yous suggestions but it still doesn't work.

STL file:

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


velVec is assigned a value inside an if condition (if aTime>0.005).
This condition is false (you can easily check, O.dt=0.0001, iterPeriod=1) and velVec therefore has no value.
Then you try to assign it to translation engine, but it is currently just nothing, so Python comes with this error.


PS: have look at [1], section "Please, no external links!" ;-)

PPS: aTime = (O.iter*O.dt) ... you can use directly aTime = O.time


Przemek (przemekn) said : #5


now it's working :) Thanks a lot!

Can you help with this problem?

Provide an answer of your own, or ask Przemek for more information if necessary.

To post a message you must log in.