def get_particle_array( additional_props=None, **props): """ Create and return a particle array with default properties Parameters ---------- props : dict A dictionary of properties requested. Example Usage: -------------- In [1]: import particle In [2]: x = linspace(0,1,10) In [3]: pa = particle.get_particle_array(x=x) In [4]: pa Out[4]: <pysph.base.particle_array.ParticleArray object at 0x9ec302c> """ # handle the name separately if props.has_key('name'): name = props['name'] props.pop('name') else: name = "array" np = 0 nprops = len(props) prop_dict = {} for prop in props.keys(): data = numpy.asarray(props[prop]) np = data.size if prop in ['tag']: prop_dict[prop] = {'data':data, 'type':'int', 'name':prop} if prop in ['pid']: prop_dict[prop] = {'data':data, 'type':'int', 'name':prop} if prop in ['gid']: prop_dict[prop] = {'data':data.astype(numpy.uint32), 'type':'unsigned int', 'name':prop} else: prop_dict[prop] = {'data':data, 'type':'double', 'name':prop} # default properties for an SPH particle default_props = ['x', 'y', 'z', 'u', 'v', 'w', 'm', 'h', 'rho', 'p', 'au', 'av', 'aw', 'gid', 'pid', 'tag'] # add any additional props if additional_props: default_props.extend( additional_props ) default_props = list( set(default_props) ) # Add the default props for prop in default_props: if not prop in prop_dict: if prop in ["pid"]: prop_dict[prop] = {'name':prop, 'type':'int', 'default':0} elif prop in ['gid']: data = numpy.ones(shape=np, dtype=numpy.uint32) data[:] = UINT_MAX prop_dict[prop] = {'name':prop, 'type':'unsigned int', 'data':data, 'default':UINT_MAX} elif prop in ['tag']: prop_dict[prop] = {'name':prop, 'type':'int'} else: prop_dict[prop] = {'name':prop, 'type':'double', 'default':0} # create the particle array pa = ParticleArray(name=name, **prop_dict) # default property arrays to save out. Any reasonable SPH particle # should define these pa.set_output_arrays( ['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'm', 'h', 'pid', 'gid', 'tag'] ) return pa
def get_particle_array(cl_precision="double", **props): """ Create and return a particle array with default properties Parameters ---------- cl_precision : {'single', 'double'} Precision to use in OpenCL (default: 'double'). props : dict A dictionary of properties requested. Example Usage: -------------- In [1]: import particle In [2]: x = linspace(0,1,10) In [3]: pa = particle.get_particle_array(x=x) In [4]: pa Out[4]: <pysph.base.particle_array.ParticleArray object at 0x9ec302c> """ nprops = len(props) prop_dict = {} name = "" particle_type = Fluid default_props = { 'x': 0.0, 'y': 0.0, 'z': 0.0, 'u': 0.0, 'v': 0.0, 'w': 0.0, 'm': 1.0, 'h': 1.0, 'p': 0.0, 'e': 0.0, 'rho': 1.0, 'cs': 0.0 } #Add the properties requested np = 0 for prop in props.keys(): if prop in ['name', 'type']: pass else: np = len(props[prop]) if prop == 'idx': prop_dict[prop] = { 'data': numpy.asarray(props[prop]), 'type': 'int' } else: data = numpy.asarray(props[prop]) prop_dict[prop] = {'data': data, 'type': 'double'} # Add the default props for prop in default_props: if prop not in props.keys(): prop_dict[prop] = { 'name': prop, 'type': 'double', 'default': default_props[prop] } # Add the property idx if not prop_dict.has_key('idx') and np != 0: prop_dict['idx'] = { 'name': 'idx', 'data': numpy.arange(np), 'type': 'long' } #handle the name and particle_type information separately if props.has_key('name'): name = props['name'] if props.has_key("type"): particle_type = props["type"] assert particle_type in [Fluid, Solid, Probe], 'Type not understood!' pa = ParticleArray(name=name, particle_type=particle_type, cl_precision=cl_precision, **prop_dict) return pa
def get_particle_array_wcsph(**props): """Return a particle array for the WCSPH formulation""" # handle the name separately if props.has_key('name'): name = props['name'] props.pop('name') else: name = "" nprops = len(props) np = 0 prop_dict = {} for prop in props.keys(): data = numpy.asarray(props[prop]) np = data.size if prop in ['tag']: prop_dict[prop] = {'data':data, 'type':'int', 'name':prop} if prop in ['pid']: prop_dict[prop] = {'data':data, 'type':'int', 'name':prop} if prop in ['gid']: prop_dict[prop] = {'data':data.astype(numpy.uint32), 'type': 'unsigned int', 'name':prop} else: prop_dict[prop] = {'data':data, 'type':'double', 'name':prop} default_props = ['x', 'y', 'z', 'u', 'v', 'w', 'h', 'rho', 'm', 'p', 'cs', 'ax', 'ay', 'az', 'au', 'av', 'aw', 'x0','y0', 'z0','u0', 'v0','w0', 'arho', 'rho0', 'div', 'gid','pid', 'tag'] for prop in default_props: if not prop in prop_dict: if prop in ["pid"]: prop_dict[prop] = {'name':prop, 'type':'int', 'default':0} elif prop in ['gid']: data = numpy.ones(shape=np, dtype=numpy.uint32) data[:] = UINT_MAX prop_dict[prop] = {'name':prop, 'type':'unsigned int', 'data':data} elif prop in ['tag']: prop_dict[prop] = {'name':prop, 'type':'int',} else: prop_dict[prop] = {'name':prop, 'type':'double', 'default':0} # create the particle array pa = ParticleArray(name=name, **prop_dict) # default property arrays to save out. pa.set_output_arrays( ['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'm', 'h', 'pid', 'gid', 'tag', 'p'] ) return pa