How to detect the coordinates of each particle?
How do I get the coordinates of each particle? For example, in my simulation, I apply gravity in the z-axis direction, and after stabilization, how do I know the z-value of the uppermost particle?
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
|
#1 |
Hello,
> How do I get the coordinates of each particle?
it depends on the definition of "get".
You can print them in a for loop:
for b in O.bodies: print(b.state.pos)
You can get the coordinates e.g. as a list:
coords = [b.state.pos for b in O.bodies]
You can save them as a file:
yade.export.
You can ...
> how do I know the z-value of the uppermost particle?
zMax = max(b.state.pos[2] for b in O.bodies)
Cheers
Jan
Revision history for this message
|
#2 |
I tried it and found that the code had no errors, but no z-value output for the uppermost particles.
My code is as follows:
from yade import pack,plot
O.bodies.
sp = pack.SpherePack()
sp.makeCloud(
sp.toSimulation()
(xdim,ydim,zdim)= aabbDim()
print("Height is ",zdim)
for b in O.bodies:
if isinstance(
circleRadius=1.5
circleCenter = Vector3(0.05,6,6)
circleRadius1=1.5
circleCenter1 = Vector3(0.05,12,6)
#
O.engines = [
# handle sphere+sphere and facet+sphere collisions
),
# call the checkUnbalanced function (defined below) every 2 seconds
# call the addPlotData function every 200 steps
]
O.dt = 0.5 * PWaveTimeStep()
# enable energy tracking; any simulation parts supporting it
# can create and update arbitrary energy types, which can be
# accessed as O.energy[
O.trackEnergy = True
# if the unbalanced forces goes below .05, the packing
# is considered stabilized, therefore we stop collected
# data history and stop
def checkUnbalanced():
if unbalancedForce() < .05:
O.pause()
plot.
for b in O.bodies:
if isinstance(
#coords = [b.state.pos for b in O.bodies]
#zMax = max(b.state.pos[2] for b in O.bodies)
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
O.bodies.
zMax = max(b.state.pos[2] for b in O.bodies)
(xdim,ydim,zdim)= aabbDim()
print("Height is ",zdim)
# collect history of data which will be plotted
def addPlotData():
# each item is given a names, by which it can be the unsed in plot.plots
# the **O.energy converts dictionary-like O.energy to plot.addData arguments
plot.addData(
# define how to plot data: 'i' (step number) on the x-axis, unbalanced force
# on the left y-axis, all energies on the right y-axis
# (O.energy.keys is function which will be called to get all defined energies)
# None separates left and right y-axis
plot.plots = {'i': ('unbalanced', None, O.energy.keys)}
# show the plot on the screen, and update while the simulation runs
plot.plot()
O.saveTmp()
# to see it
from yade import qt
qt.Controller()
qt.View()
qt.View()
Cheers
Revision history for this message
|
#3 |
Hello,
> My code is as follows:
please make your code MWE [1]:
W = working. This code throw IndentationError: unexpected indent
M = minimal. For the problem, there is no need of so many O.bodies.append, no qt is needed, plot is not needed, plenty of commented lines are not needed, etc. etc.
> I tried it and found that the code had no errors, but no z-value output for the uppermost particles.
> zMax = max(b.state.pos[2] for b in O.bodies)
what do you mean by "no z-value output"?
zMax is not used anywhere (e.g. not printed), is this the problem?
Cheers
Jan
Revision history for this message
|
#4 |
My code is as follows:
from yade import pack,plot
O.bodies.
sp = pack.SpherePack()
sp.makeCloud(
sp.toSimulation()
(xdim,ydim,zdim)= aabbDim()
print("Height is ",zdim)
for b in O.bodies:
if isinstance(
O.engines = [
),
]
O.dt = 0.5 * PWaveTimeStep()
O.trackEnergy = True
def checkUnbalanced():
if unbalancedForce() < .05:
O.pause()
#coords = [b.state.pos for b in O.bodies]
#zMax = max(b.state.pos[2] for b in O.bodies)
zMax = max(b.state.pos[2] for b in O.bodies)
print("Z ",zMax)
(xdim,ydim,zdim)= aabbDim()
print("Height is ",zdim)
def addPlotData():
plot.addData(
O.saveTmp()
from yade import qt
qt.Controller()
#qt.View()
>This code throw IndentationError: unexpected indent
This may be due to an indentation error caused by copying over.
>what do you mean by "no z-value output"?
Yes, I found this problem, I did not set the print for output.
But the zMax = max(b.state.pos[2] for b in O.bodies) string of code didn't solve my problem. It can only get the z-value of the highest particle before the run, and I need to get the z-value of the uppermost particles after gravity.
Revision history for this message
|
#5 |
Thanks for the MWE.
> But the zMax = max(b.state.pos[2] for b in O.bodies) string of code didn't solve my problem. It can only get the z-value of the highest particle before the run, and I need to get the z-value of the uppermost particles after gravity.
the command gets the zMax value at the time it is executed.
If you execute it before the run, it is evaluated before the run.
If you execute it "after gravity", it is evaluated "after gravity".
Here in your code, it is evaluated inside checkUnbalanced() function if unbalancedForce() < .05 is fulfilled.
Also, max(b.state.pos[2] for b in O.bodies) takes into account all bodies, including facets.
If you are interested only in spheres, you can modify it as:
zMax = max(b.state.pos[2] for b in O.bodies if isinstance(
Cheers
Jan
Revision history for this message
|
#6 |
Yes, I added zMax = max(b.state.pos[2] for b in O.body if isinstance(b.shape, Sphere)) to the checkUnbalanced() function, z-values can get the output, but only one zMax output, zMAX is the highest z-value, and I need to output the z-value of each particle in the uppermost layer.
Revision history for this message
|
#7 |
> ... coordinates of each particle?
> ... the z-value of the uppermost particle?
> ... the z-value of each particle in the uppermost layer.
please be consistent.
> in the uppermost layer.
first, define rigorously "the uppermost layer", i.e. how mathematically determine if particle belongs to this layer or not.
Then just:
###
def isUpperMostLaye
... # your definition
return False # or True
zValsUpperMostLayer = [b.state.pos[2] for b in O.bodies if isinstance(b.shape, Sphere) and isUpperMostLaye
print(zValsUppe
###
Cheers
Jan
Revision history for this message
|
#8 |
I see what you mean. But how can I strictly define particles as belonging to the top layer, I don't seem to have a train of thought.
My first thought was to use coordinates to define the uppermost particles, but the coordinates of these particles are also unknown quantities, and I can't use unknown quantities to solve for unknown quantities.
Cheers
Revision history for this message
|
#9 |
> But how can I strictly define particles as belonging to the top layer, I don't seem to have a train of thought.
This is up to you.
There are a lot of reasonable approaches how to define it.
> My first thought was to use coordinates to define the uppermost particles
yes, this is one of the most easiest and straightforward approach, get the highest coordinate zMax and say all particles with z coodinate larger than zMax - someLayerThickness is the layer.
Or you can define number of particles in the layer, sort particles w.r.t. z coordinate and define the layer as first N particles.
Or you can define "numerical blanket", let it fall to the packing and define touching particles as the top layer.
Or ........
It really depends on definition, purposes, available / needed time to use / develop a method, ...
> but the coordinates of these particles are also unknown quantities,
coordinates of particles are of course known quantities (all this thread is about it).
Cheers
Jan
Revision history for this message
|
#10 |
Thanks Jan Stránský, that solved my question.