Select bodies under post2d.extractor function

Asked by Yuxuan Wang on 2020-08-17

Hello yade programmers and fellow users,
Hope all is well! I have been trying to mimic the uniax-post.py example to produce a velocity contour plot of a slice of particles in the simulation. Following Jan’s advice in post [1], I tried searching for ways to construct an extractor function to output “2-tuple velocity” for the bodies I want and “None” for the rest. I was only able to think of two ways to attack this being fairly new to python. Both of them did not work out so it would be greatly appreciated if you could offer me some hints!!

Below are the two methods I attempted:
1. I tried defining a new extractor function vExtract() that could take the body instance outputs (b) looped by post2d.data() as its own argument. That way I would be able to use the following structure to accomplish my goal.

def vExtract(b):
if -0.15< b.state.pos[2] <= 0.15:return flattener.normal(b,b.state.vel)
else:return None

# raw velocity (vector field) plot
pylab.figure(); post2d.plot(post2d.data(vExtract(),flattener))

However, this would yield an error due to the fact that vExtract is expecting exactly one argument while I passed zero to post2d.data. If there is a way to write the vExtract() function to use arguments (body b) not specified in its own definitions, but in the higher level post2d.data function it would work.

2. I also looked for ways to modify the extractor extractVelocity() used by the original uniax-post.py. I thought of adding the if-else condition

if -0.15< b.state.pos[2] <= 0.15:return flattener.normal(b,b.state.vel)
else:return None

to the lambda expression “extractVelocity=lambda b: flattener.normal(b,b.state.vel)”,
but seems like lambda expressions cannot take conditional statements.

Thank you in advance! My Yade version is 2018.02b.

[1] https://answers.launchpad.net/yade/+question/404193

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Yuxuan Wang
Solved:
2020-08-21
Last query:
2020-08-21
Last reply:
2020-08-18
Jan Stránský (honzik) said : #1

Hello,

> I was only able to think of two ways

it is actually one way, see below

> but seems like lambda expressions cannot take conditional statements.

it can:
extractVelocity=lambda b: flattener.normal(b,b.state.vel) if -0.15< b.state.pos[2] <= 0.15 else None

Please provide a MWE [1], something like a few predefined spheres with predefined positions and velocities and describe expected result.

cheers
Jan

[1] https://www.yade-dem.org/wiki/Howtoask

Yuxuan Wang (ywang1) said : #2

Thank you so much Jan, I used
extractVelocity=lambda b: flattener.normal(b,b.state.vel) if -0.15< b.state.pos[2] <= 0.15 else None
and it worked perfectly.

Best,
Yuxuan