def exportPeriodicCell(self,comment="comment",numLabel=None): """exports the Cell geometry for periodic simulations. :param string comment: comment to add to vtk file :param int numLabel: number of file (e.g. time step), if unspecified, the last used value + 1 will be used """ if not O.periodic: self._warn("exportPeriodicCell: scene is not periodic, no export...") return hSize = O.cell.hSize fName = self.baseName+'-periCell-%08d'%(numLabel if numLabel else self.intrsSnapCount)+'.vtk' outFile = open(fName, 'w') outFile.write("# vtk DataFile Version 3.0.\n%s\nASCII\n\nDATASET UNSTRUCTURED_GRID\nPOINTS 8 double\n"%(comment)) vertices = [ hSize*Vector3(0,0,1), hSize*Vector3(0,1,1), hSize*Vector3(1,1,1), hSize*Vector3(1,0,1), hSize*Vector3(0,0,0), hSize*Vector3(0,1,0), hSize*Vector3(1,1,0), hSize*Vector3(1,0,0), ] for v in vertices: outFile.write('%g %g %g\n'%(v[0],v[1],v[2])) outFile.write('\nCELLS 1 9\n') outFile.write('8 0 1 2 3 4 5 6 7\n') outFile.write('\nCELL_TYPES 1\n12\n') outFile.close()
def myRandomPeriPack(radius, num, seed=1, memo="", initSizeFactor=3.): if memo and os.path.exists(memo): ret = pack.SpherePack() ret.load(memo) return ret O.switchScene() O.resetThisScene() sp = pack.SpherePack() O.periodic = True O.cell.setBox(num**(1 / 3.) * initSizeFactor * radius * Vector3(1, 1, 1)) sp.makeCloud(Vector3().Zero, O.cell.refSize, radius, 0, num, True, seed=seed) O.engines = [ ForceResetter(), InsertionSortCollider([Bo1_Sphere_Aabb()], verletDist=0), #allowBiggerThanPeriod=True), InteractionLoop([Ig2_Sphere_Sphere_ScGeom()], [Ip2_FrictMat_FrictMat_FrictPhys()], [Law2_ScGeom_FrictPhys_CundallStrack()]), PeriIsoCompressor(charLen=2 * radius, stresses=[-100e9, -1e8], maxUnbalanced=1e-2, doneHook='O.pause();', globalUpdateInt=20, keepProportions=True), NewtonIntegrator(damping=.1) ] O.materials.append( FrictMat(young=30e9, frictionAngle=.0, poisson=.3, density=1e3)) for s in sp: O.bodies.append(utils.sphere(s[0], s[1])) O.dt = .3 * utils.PWaveTimeStep() O.timingEnabled = True O.run() O.wait() p0 = O.bodies[0].state.pos for b in O.bodies: p = b.state.pos - p0 b.state.pos = O.cell.wrapPt(p) ret = pack.SpherePack() ret.fromSimulation() O.switchScene() if memo: ret.save(memo) return ret
def polyhedra(material, size=Vector3(1, 1, 1), seed=None, v=[], mask=1, fixed=False, color=[-1, -1, -1]): """create polyhedra, one can specify vertices directly, or leave it empty for random shape. :param Material material: material of new body :param Vector3 size: size of new body (see Polyhedra docs) :param float seed: seed for random operations :param [Vector3] v: list of body vertices (see Polyhedra docs) """ b = Body() random.seed(seed) b.aspherical = True if len(v) > 0: b.shape = Polyhedra(v=v) else: b.shape = Polyhedra(size=size, seed=random.randint(0, 1E6)) if color[0] == -1: b.shape.color = randomColor(seed=random.randint(0, 1E6)) else: b.shape.color = color b.mat = material b.state.mass = b.mat.density * b.shape.GetVolume() b.state.inertia = b.shape.GetInertia() * b.mat.density b.state.ori = b.shape.GetOri() b.state.pos = b.shape.GetCentroid() b.mask = mask if fixed: b.state.blockedDOFs = 'xyzXYZ' return b
def randomPeriPack(radius, initSize, seed): O.switchScene() O.resetThisScene() sp = pack.SpherePack() O.periodic = True O.cell.setBox(initSize) sp.makeCloud(Vector3().Zero, O.cell.refSize, radius, 0., -1, True, seed=seed) O.engines = [ ForceResetter(), InsertionSortCollider([Bo1_Sphere_Aabb()], verletDist=.05 * radius), InteractionLoop([Ig2_Sphere_Sphere_ScGeom()], [Ip2_FrictMat_FrictMat_FrictPhys()], [Law2_ScGeom_FrictPhys_CundallStrack()]), PeriIsoCompressor(charLen=2 * radius, stresses=[-100e9, -1e8], maxUnbalanced=1e-2, doneHook='O.pause();', globalUpdateInt=20, keepProportions=True), NewtonIntegrator(damping=.8) ] O.materials.append( FrictMat(young=30e9, frictionAngle=.1, poisson=.3, density=1e3)) for s in sp: O.bodies.append(utils.sphere(s[0], s[1])) O.dt = utils.PWaveTimeStep() O.timingEnabled = True O.run() O.wait() for b in O.bodies: b.state.pos = O.cell.wrap(b.state.pos) ret = pack.SpherePack() ret.fromSimulation() O.switchScene() return ret
def textExt(filename, format='x_y_z_r', comment='',mask=-1,attrs=[]): """Save sphere coordinates and other parameters into a text file in specific format. Non-spherical bodies are silently skipped. Users can add here their own specific format, giving meaningful names. The first file row will contain the format name. Be sure to add the same format specification in ymport.textExt. :param string filename: the name of the file, where sphere coordinates will be exported. :param string format: the name of output format. Supported 'x_y_z_r'(default), 'x_y_z_r_matId', 'x_y_z_r_attrs' (use proper comment) :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. With 'x_y_z_r_attrs' format, the last (or only) line should consist of column headers of quantities passed as attrs (1 comment word for scalars, 3 comment words for vectors and 9 comment words for matrices) :param int mask: export only spheres with the corresponding mask export only spheres with the corresponding mask :param [str] attrs: attributes to be exported with 'x_y_z_r_attrs' format. Each str in the list is evaluated for every body exported with body=b (i.e. 'b.state.pos.norm()' would stand for distance of body from coordinate system origin) :return: 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 # TODO use output=[] instrad of ''??? output = '' outputVel='' if (format!='liggghts_in'): output = '#format ' + format + '\n' if (comment): if format=='x_y_z_r_attrs': cmts = comment.split('\n') for cmt in cmts[:-1]: output += cmt output += '# x y z r ' + cmts[-1] + '\n' else: output += '# ' + comment + '\n' minCoord= Vector3.Zero maxCoord= Vector3.Zero maskNumber = [] for b in O.bodies: try: if (isinstance(b.shape,Sphere) and ((mask<0) or ((mask&b.mask)>0))): if (format=='x_y_z_r'): output+=('%g\t%g\t%g\t%g\n'%(b.state.pos[0],b.state.pos[1],b.state.pos[2],b.shape.radius)) elif (format=='x_y_z_r_matId'): output+=('%g\t%g\t%g\t%g\t%d\n'%(b.state.pos[0],b.state.pos[1],b.state.pos[2],b.shape.radius,b.material.id)) elif (format=='x_y_z_r_attrs'): output+=('%g\t%g\t%g\t%g'%(b.state.pos[0],b.state.pos[1],b.state.pos[2],b.shape.radius)) for cmd in attrs: v = eval(cmd) if isinstance(v,(int,float)): output+='\t%g'%v elif isinstance(v,Vector3): output+='\t%g\t%g\t%g'%tuple(v[i] for i in range(3)) elif isinstance(v,Matrix3): output+='\t%g'%tuple(v[i] for i in range(9)) output += '\n' elif (format=='id_x_y_z_r_matId'): output+=('%d\t%g\t%g\t%g\t%g\t%d\n'%(b.id,b.state.pos[0],b.state.pos[1],b.state.pos[2],b.shape.radius,b.material.id)) elif (format=='jointedPM'): output+=('%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n'%(b.id,b.state.onJoint,b.state.joint,b.state.jointNormal1[0],b.state.jointNormal1[1],b.state.jointNormal1[2],b.state.jointNormal2[0],b.state.jointNormal2[1],b.state.jointNormal2[2],b.state.jointNormal3[0],b.state.jointNormal3[1],b.state.jointNormal3[2])) elif (format=='liggghts_in'): output+=('%g %g %g %g %g %g %g\n'%(count+1,b.mask,b.shape.radius,b.material.density,b.state.pos[0],b.state.pos[1],b.state.pos[2])) outputVel+=('%g %g %g %g %g %g %g\n'%(count+1,b.state.vel[0],b.state.vel[1],b.state.vel[2],b.state.angVel[0],b.state.angVel[1],b.state.angVel[2])) else: raise RuntimeError("Please, specify a correct format output!"); count+=1 if (count==1): minCoord = b.state.pos - Vector3(b.shape.radius,b.shape.radius,b.shape.radius) maxCoord = b.state.pos + Vector3(b.shape.radius,b.shape.radius,b.shape.radius) else: minCoord = Vector3(min(minCoord[0], b.state.pos[0]-b.shape.radius),min(minCoord[1], b.state.pos[1]-b.shape.radius),min(minCoord[2], b.state.pos[2]-b.shape.radius)) maxCoord = Vector3(max(maxCoord[0], b.state.pos[0]+b.shape.radius),max(maxCoord[1], b.state.pos[1]+b.shape.radius),max(minCoord[2], b.state.pos[2]+b.shape.radius)) if b.mask not in maskNumber: maskNumber.append(b.mask) except AttributeError: pass if (format=='liggghts_in'): outputHeader = 'LIGGGHTS Description\n\n' outputHeader += '%d atoms\n%d atom types\n\n'%(count,len(maskNumber)) outputHeader += '%g %g xlo xhi\n%g %g ylo yhi\n%g %g zlo zhi\n\n'%(minCoord[0],maxCoord[0],minCoord[1],maxCoord[1],minCoord[2],maxCoord[2]) output=outputHeader + 'Atoms\n\n' + output + '\nVelocities\n\n' + outputVel out.write(output) out.close() return count bodies = [b for b in O.bodies if isinstance(b.shape,Sphere) and (True if mask==-1 else b.msak==mask)] data = [] for b in bodies: pos = b.state.pos d = [pos[i] for i in (0,1,2)] for name,command in what: val = eval(command) if isinstance(val,Matrix3): d.extend((val[0,0],val[0,1],val[0,2],val[1,0],val[1,1],val[1,2],val[2,0],val[2,1],val[2,2])) elif isinstance(val,Vector3): d.extend((v[0],v[1],v[2])) elif isinstance(val,(int,float)): d.append(val) else: print("WARNING: export.text: wrong 'what' parameter, output might be corrupted") return 0 data.append(d) dataw = [' '.join('%e'%v for v in d) for d in data] outFile = open(filename,'w') outFile.writelines(dataw) outFile.close() return len(bodies)
def randomColor(seed=None): random.seed(seed) #Return random Vector3 with each component in interval 0...1 (uniform distribution) return Vector3(random.random(), random.random(), random.random())