← Back to team overview

yade-dev team mailing list archive

[Branch ~yade-pkg/yade/git-trunk] Rev 3774: Add functions to save and load clumps.

 

------------------------------------------------------------
revno: 3774
committer: Anton Gladky <gladky.anton@xxxxxxxxx>
timestamp: Mon 2013-12-09 13:32:37 +0100
message:
  Add functions to save and load clumps.
added:
  examples/clumps/save-load-clumps.py
modified:
  py/export.py
  py/ymport.py


--
lp:yade
https://code.launchpad.net/~yade-pkg/yade/git-trunk

Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-pkg/yade/git-trunk/+edit-subscription
=== added file 'examples/clumps/save-load-clumps.py'
--- examples/clumps/save-load-clumps.py	1970-01-01 00:00:00 +0000
+++ examples/clumps/save-load-clumps.py	2013-12-09 12:32:37 +0000
@@ -0,0 +1,83 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+'''This example shows usage of save and load clumps.'''
+
+from yade import pack,export,qt,ymport
+
+#define material for all bodies:
+id_Mat=O.materials.append(FrictMat(young=1e6,poisson=0.3,density=1000,frictionAngle=1))
+Mat=O.materials[id_Mat]
+
+#define engines:
+O.engines=[
+	ForceResetter(),
+	InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Box_Aabb()]),
+	InteractionLoop(
+		[Ig2_Sphere_Sphere_ScGeom(),Ig2_Box_Sphere_ScGeom()],
+		[Ip2_FrictMat_FrictMat_FrictPhys()],
+		[Law2_ScGeom_FrictPhys_CundallStrack()]
+	),
+	NewtonIntegrator(damping=0.7,gravity=[0,0,-10])
+]
+
+#create a box:
+id_box = O.bodies.append(box((0,0,0),(2,2,.1),fixed=True,material=Mat))
+
+
+#### show how to use appendClumped():
+
+
+#create 2 clumps:
+clump1=O.bodies.appendClumped([\
+sphere([0,0,1], material=Mat, radius=0.5),\
+sphere([0.2,0,1], material=Mat, radius=0.5)\
+])
+clump2=O.bodies.appendClumped([\
+sphere([3,1,2], material=Mat, radius=0.5),\
+sphere([3.2,1,2], material=Mat, radius=0.5)\
+])
+
+#get clump ids:
+id_clump1 = clump1[0]
+id_clump2 = clump2[0]
+
+#definition for getting informations from all clumps:
+def getClumpInfo():
+	for b in O.bodies:
+		if b.isClump:
+			print 'Clump ',b.id,' has following members:'
+			keys = b.shape.members.keys()
+			for ii in range(0,len(keys)):
+				print '- Body ',keys[ii]
+			print 'inertia:',b.state.inertia
+			print 'mass:',b.state.mass,'\n'
+
+
+#### show how to use addToClump():
+
+#create a new sphere:
+id_new=O.bodies.append(sphere([0,0.2,1], material=Mat, radius=0.5))
+
+#add a sphere to the clump:
+O.bodies.addToClump([id_new],id_clump1)
+
+#add a clump to a clump:
+O.bodies.addToClump([id_clump2],id_clump1)
+
+print '\nSTATE after adding the second clump to clump ------------'
+getClumpInfo()
+
+print "Save clumps"
+export.textClumps("savedClumps.txt")
+print "Load clumps"
+ymport.textClumps("savedClumps.txt", shift=Vector3(0,0,1.5))
+
+print "After saving"
+getClumpInfo()
+
+O.dt=1e-6
+
+print '\nPress Play button ... '
+renderer = qt.Renderer()
+qt.View()

=== modified file 'py/export.py'
--- py/export.py	2013-11-04 14:20:24 +0000
+++ py/export.py	2013-12-09 12:32:37 +0000
@@ -75,6 +75,51 @@
 	out.write(output)
 	out.close()
 	return count
+  
+#textExt===============================================================
+def textClumps(filename, format='x_y_z_r_clumpId', comment='',mask=-1):
+	"""Save clumps-members into a text file. Non-clumps members are bodies are silently skipped.
+
+	:param string filename: the name of the file, where sphere coordinates will be exported.
+	:param string comment: the text, which will be added as a comment at the top of file. If you want to create several lines of text, please use '\\\\n#' for next lines.
+	:param int mask: export only spheres with the corresponding mask export only spheres with the corresponding mask
+	:return: number of clumps, number of spheres which were written.
+	:rtype: int
+	"""
+	O=Omega()
+	
+	try:
+		out=open(filename,'w')
+	except:
+		raise RuntimeError("Problem to write into the file")
+	
+	count=0
+	countClumps=0
+	output = ''
+	output = '#format x_y_z_r_clumpId\n'
+	if (comment):
+		output += '# ' + comment + '\n'
+	
+	minCoord= Vector3.Zero
+	maxCoord= Vector3.Zero
+	maskNumber = []
+	
+	for bC in O.bodies:
+		if bC.isClump:
+			keys = bC.shape.members.keys()
+			countClumps+=1
+			for ii in keys:
+				try:
+					b = O.bodies[ii]
+					if (isinstance(b.shape,Sphere) and ((mask<0) or ((mask&b.mask)>0))):
+						output+=('%g\t%g\t%g\t%g\t%g\n'%(b.state.pos[0],b.state.pos[1],b.state.pos[2],b.shape.radius,bC.id))
+						count+=1
+				except AttributeError:
+					pass
+			
+	out.write(output)
+	out.close()
+	return countClumps,count
 
 #VTKWriter===============================================================
 class VTKWriter:

=== modified file 'py/ymport.py'
--- py/ymport.py	2013-08-14 08:59:42 +0000
+++ py/ymport.py	2013-12-09 12:32:37 +0000
@@ -47,6 +47,46 @@
 		else:
 			raise RuntimeError("Please, specify a correct format output!");
 	return ret
+  
+def textClumps(fileName,shift=Vector3.Zero,scale=1.0,**kw):
+	"""Load clumps-members from file, insert them to the simulation.
+	
+	:param str filename: file name
+	:param str format: the name of output format. Supported `x_y_z_r`(default), `x_y_z_r_matId`
+	:param [float,float,float] shift: [X,Y,Z] parameter moves the specimen.
+	:param float scale: factor scales the given data.
+	:param \*\*kw: (unused keyword arguments) is passed to :yref:`yade.utils.sphere`
+	:returns: list of spheres.
+
+	Lines starting with # are skipped
+	"""
+	infile = open(fileName,"r")
+	lines = infile.readlines()
+	infile.close()
+	ret=[]
+	
+	curClump=[]
+	newClumpId = -1
+	
+	for line in lines:
+		data = line.split()
+		if (data[0][0] == "#"): continue
+		pos = Vector3(float(data[0]),float(data[1]),float(data[2]))
+	
+		if (newClumpId<0 or newClumpId==int(data[4])):
+			idD = curClump.append(utils.sphere(shift+scale*pos,scale*float(data[3]),**kw))
+			newClumpId = int(data[4])
+			ret.append(idD)
+		else:
+			newClumpId = int(data[4])
+			O.bodies.appendClumped(curClump)
+			curClump=[]
+			idD = curClump.append(utils.sphere(shift+scale*pos,scale*float(data[3]),**kw))
+			ret.append(idD)
+	
+	if (len(curClump)<>0):
+		O.bodies.appendClumped(curClump)
+	return ret
 
 def text(fileName,shift=Vector3.Zero,scale=1.0,**kw):
 	"""Load sphere coordinates from file, create spheres, insert them to the simulation.