# yade-users team mailing list archive

## Re: [Question #689434]: How to generate many polyhedra randomly without specific data in Potential Blocks code？

```Question #689434 on Yade changed:

Vasileios Angelidakis proposed the following answer:
Hi,

To convert all the polyhedra into PBs, you have to define the generation
of PBs inside the loop. You are currently creating only the last
polyhedron. Then, for the newly created PBs, you have to assign the same
orientation and position as the one of the polyhedral particles.

Below I generate the PBs shifted by Vector3(lengthOfBase,0,0), so that
you can compare the polyhedra (on the left) and the PBs (on the right).
Also, I choose different "r=chosenR" for each body, depending on their
individual dd values.

I see some faces are still not rendered properly in the PBs, but this is
only a visualisation issue.

#Best Regards,
#Vasileios

#################################
import random
import numpy as np

#-------------------------------------------
#Material
n = PolyhedraMat(young=7.2e7,poisson=.2,density=2.5e3)
#-------------------------------------------
#Dimensions

meanSize = 0.05
wallThickness = 0.5*meanSize
distanceToCentre = 0.01
lengthOfBase = 5*meanSize
heightOfBase = 12*meanSize

#-------------------------------------------
#Make Cloud

sp=pack.SpherePack()
mn,mx=Vector3(-0.5*(lengthOfBase-wallThickness),0.5*meanSize,-0.5*(lengthOfBase-wallThickness)),Vector3(0.5*(lengthOfBase-wallThickness),0.5*heightOfBase,0.5*(lengthOfBase-wallThickness))
R=sqrt(3.0)*distanceToCentre
sp.makeCloud(mn,mx,R,0,-1,False)

def dvalue(vecn1,pp1):
dd1=1*(vecn1*pp1+vecn1*pp1+vecn1*pp1)
return dd1

for s in sp:
# Generate polyhedra
color=Vector3(random.random(),random.random(),random.random())
b=polyhedra_utils.polyhedra(material=n,size=(2*distanceToCentre,distanceToCentre,distanceToCentre),color=color)
b.state.pos = s #s stores center
b.state.ori = Quaternion((random.random(),random.random(),random.random()),random.random()) #s
O.bodies.append(b)

# Generate PBs
aa=[]
bb=[]
cc=[]
dd=[]
vs=b.shape.v
face2=b.shape.GetSurfaces()

id1=0
while id1<len(face2):
face11=face2[id1]
if len(face11)>2:
vec1=vs[face11]-vs[face11]; vec1.normalize()
vec2=vs[face11]-vs[face11]; vec2.normalize() #Normalize this object in-place.
vects=vec1.cross(vec2); vects.normalize()
dvalue2=dvalue(vects,vs[face11])
aa.append(vects)
bb.append(vects)
cc.append(vects)
dd.append(dvalue2)
id1=id1+1

chosenR=min(dd)/2

bbb=Body()
bbb.aspherical=True
wire=False
highlight=True
bbb.shape=PotentialBlock(k=0.0, r=chosenR, R=0.0, a=aa, b=bb, c=cc, d=np.array(dd)-chosenR, color=b.shape.color)
utils._commonBodySetup(bbb, bbb.shape.volume, bbb.shape.inertia,material='frictionless', pos=bbb.shape.position, fixed=False)
bbb.state.ori= b.state.ori
bbb.state.pos = b.state.pos+Vector3(lengthOfBase,0,0)
O.bodies.append(bbb)

# Count number of bodies with b.shape=Polyhedra
countPol=0
for b in O.bodies:
if isinstance(b.shape,Polyhedra):
countPol=countPol+1
print("number of Polyhedra       =   ", countPol)

# Count number of bodies with b.shape=PotentialBlock
countPBs=0
for b in O.bodies:
if isinstance(b.shape,PotentialBlock):
countPBs=countPBs+1
print("number of PotentialBlocks =   ", countPBs)