Can I add properties and color into export text file?

Asked by Huan

Hi,
I am trying to save the position, properties, and color. Can I export it into one text file for future importing uses for example I will import the spherer packing from text when I implement gravity deposition?

##My Code##
import random
import math
from yade import geom, pack, utils, plot, ymport, export
import numpy as np

# Materials Properties
gravel = CohFrictMat(young = 1e7, poisson = 0.25, density = 2700, label = 'gravel')
asphalt_binder = CohFrictMat(young = 1e7, poisson = 0.25, density = 1060, frictionAngle = radians(40), normalCohesion = 2e5, shearCohesion = 2e5, label = 'asphalt_binder')

# add properties
O.materials.append(gravel)
O.materials.append(asphalt_binder)

# Define cylinder with funnel parameters
center = (0, 0, 0)
diameter = 0.102
height = 0.18

# create cylindrical body with radius 0.102 m and height 0.064 m
cylinder = geom.facetCylinder(center=center, radius=diameter/2, height=height, segmentsNumber=80, wallMask=6)

# add cylinder to simulation
O.bodies.append(cylinder)

# Define cylinder with funnel parameters
center1 = (0,0,height/2)
dBunker = 0.4
dOutput = 0.102
hBunker = 0
hOutput = 0.15
hPipe = 0

# create funnel as a bunker with diameter 0.102 m, height 0.064 m
funnel = geom.facetBunker(center=center1, dBunker=dBunker, dOutput=dOutput, hBunker=hBunker,hOutput=hOutput, hPipe=hPipe, segmentsNumber=80, wallMask=4)

# add funnel to simulation
O.bodies.append(funnel)

def makeRandomBlocks(diameter, gridsize, minz, numblocks):
    bunkerLimit = diameter/2
    #
    # Make grid blocks inside a bunker
    #
    grid = np.arange(-bunkerLimit, bunkerLimit, gridsize)
    inGrid = []
    for yi in grid:
        for xi in grid:
            c = [[xi, yi], [xi+gridsize, yi], [xi+gridsize, yi+gridsize], [xi, yi+gridsize]]
            #
            # Check if the block corners are outside of the bunker or not
            #
            out = False
            for p in c:
                r = (p[0]**2 + p[1]**2)**0.5
                if r > bunkerLimit:
                    out = True
            #
            # If the block is inside the bunker, keep it
            #
            if not out:
                inGrid.append(c)
    layer = math.ceil(numblocks / len(inGrid))
    blocks = []
    for l in range(layer):
        for g in inGrid:
            zi = minz + l*gridsize
            p1 = g[0].copy()
            p1.append(zi)
            p2 = g[2].copy()
            p2.append(zi+gridsize)
            blocks.append([p1, p2])
    random.shuffle(blocks)
    return blocks

minZ = 2.35
dbunker = 0.4
gridSize = dbunker/8
numblocks = 51
blockList = makeRandomBlocks(dbunker, gridSize, minZ, numblocks)

for i in range(numblocks):
    print("Making cloud block", i+1, "/", numblocks)
    corner = blockList.pop()
    sp = pack.SpherePack()

    # 15.75 mm 13 particles
    if (i < 13):
        n1 = sp.makeCloud(minCorner=corner[0], maxCorner=corner[1], rMean=0.01575/2, num=1)

    # 11 mm 51 particles
    n2 = sp.makeCloud(minCorner=corner[0], maxCorner=corner[1], rMean=0.011/2, num=1)

    # 7.125 mm 51x11 = 561 particles
    n3 = sp.makeCloud(minCorner=corner[0], maxCorner=corner[1], rMean=0.007125/2, num=11)

    # 3.555 mm 51x100 = 5,100 particles
    n4 = sp.makeCloud(minCorner=corner[0], maxCorner=corner[1], rMean=0.003555/2, num=100)

    # 1.77 mm 51x360 = 18,360 particles
    n5 = sp.makeCloud(minCorner=corner[0], maxCorner=corner[1], rMean=0.00177/2, num=360)

    # 0.6 mm 51x19000 = 969,000 particles
    n6 = sp.makeCloud(minCorner=corner[0], maxCorner=corner[1], rMean=0.0006/2, num=300)

    # 0.3 mm 51x78510 = 4,004,010 particles
    n7 = sp.makeCloud(minCorner=corner[0], maxCorner=corner[1], rMean=0.0003/2, num=400)

    sp.toSimulation()

for body in O.bodies:
   if not isinstance(body.shape, Sphere):
       continue
   if body.shape.radius == 0.01575/2 :
       body.shape.color = (0,0,1) #blue
   if body.shape.radius == 0.011/2:
       body.shape.color = (1,0,0) #red
   if body.shape.radius == 0.007125/2:
       body.shape.color = (0,1,0) #green
   if body.shape.radius == 0.003555/2:
       body.shape.color = (1,1,0) #yellow
   if body.shape.radius == 0.00177/2 :
       body.shape.color = (1,0,1) #magenta
   if body.shape.radius == 0.0006/2 :
       body.shape.color = (1,1,1) #magenta
   if body.shape.radius == 0.0003/2 :
       body.shape.color = (0,0,0) #black

# give properties to sphere
for i in range(n1,n6):
    O.bodies[i].mat = gravel
for i in range(n6, n7):
    O.bodies[i].mat = asphalt_binder

# Save body IDs and materials to a text file
with open("body_properties.txt", "w") as file:
    for b in O.bodies:
        file.write(f"{b.id} {b.mat}\n")

export.text("testCloud.txt")

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Huan
Solved:
Last query:
Last reply:
Revision history for this message
Huan (huan-liu) said :
#1

Nevermind I ended up only save the position of the sphere. Then, in the second python file I import the position and give it properties in the second file.

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

Hello,

> ##My Code##

please try to use MWE [1], M = minimal.
For this case, a few "hardcoded" spheres is enough for the export / import purposes (see below what I mean)

Option 1:
it is python script, so you can "manually" export anything anyhow you like.

Option2:
using yade's predefined export.textExt function [2]:
###
from yade import export
mat1 = FrictMat(label="mat1")
mat2 = FrictMat(label="mat2")
mat1id = O.materials.append(mat1)
mat2id = O.materials.append(mat2)
s1 = sphere((0,0,0),1,material=mat1,color=(0.9,0.2,0.1))
s2 = sphere((2,0,0),1,material=mat2,color=(0.3,0.4,0.8))
O.bodies.append([s1,s2])
export.textExt("test.txt",format="x_y_z_r_attrs",comment="color_r color_g color_b mat_id",attrs=["b.shape.color","b.mat.id"])
# note: b.shape.color "vector" is split into 3 numbers
###

For import you can use ymport.textExt [3]:
###
from yade import ymport
attrs = []
spheres = ymport.textExt("test.txt",format="x_y_z_r_attrs",attrs=attrs)
print(spheres)
print(attrs)
###

Cheers
Jan

[1] https://www.yade-dem.org/wiki/Howtoask
[2] https://yade-dem.org/doc/yade.export.html#yade.export.textExt
[3] https://yade-dem.org/doc/yade.ymport.html#yade.ymport.textExt