/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/base/utils.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
import numpy
from .particle_array import ParticleArray, \
get_local_tag, get_remote_tag, get_ghost_tag
from pyzoltan.core.carray import LongArray
UINT_MAX = (1<<32) - 1
# Internal tags used in PySPH (defined in particle_array.pxd)
class ParticleTAGS:
Local = get_local_tag()
Remote = get_remote_tag()
Ghost = get_ghost_tag()
def arange_long(start, stop=-1):
""" Creates a LongArray working same as builtin range with upto 2 arguments
both expected to be positive
"""
if stop == -1:
arange = LongArray(start)
for i in range(start):
arange.data[i] = i
return arange
else:
size = stop-start
arange = LongArray(size)
for i in range(size):
arange.data[i] = start + i
return arange
# A collection of default properties for all SPH arrays.
DEFAULT_PROPS = set(
('x', 'y', 'z', 'u', 'v', 'w', 'm', 'h', 'rho', 'p',
'au', 'av', 'aw', 'gid', 'pid', 'tag')
)
def get_particle_array(additional_props=None, constants=None, **props):
"""Create and return a particle array with default properties.
The default properties are ['x', 'y', 'z', 'u', 'v', 'w', 'm', 'h', 'rho',
'p', 'au', 'av', 'aw', 'gid', 'pid', 'tag'], this set is available in
`DEFAULT_PROPS`.
Parameters
----------
additional_props : list
If specified, add these properties.
constants : dict
Any constants to be added to the particle array.
Other Parameters
----------------
props : dict
Additional keywords passed are set as the property arrays.
Examples
--------
>>> x = linspace(0,1,10)
>>> pa = get_particle_array(name='fluid', x=x)
>>> pa.properties.keys()
['x', 'z', 'rho', 'pid', 'v', 'tag', 'm', 'p', 'gid', 'au',
'aw', 'av', 'y', 'u', 'w', 'h']
>>> pa1 = get_particle_array(name='fluid', additional_props=['xx', 'yy'])
>>> pa = get_particle_array(name='fluid', x=x, constants={'alpha': 1.0})
>>> pa.constants.keys()
['alpha']
"""
# handle the name separately
if 'name' in props:
name = props['name']
props.pop('name')
else:
name = "array"
# default properties for an SPH particle
default_props = set(DEFAULT_PROPS)
# add any additional props to the default_props
if additional_props:
default_props = default_props.union(additional_props)
np = 0
prop_dict = {}
for prop in props.keys():
data = numpy.asarray(props[prop])
np = data.size
if prop in ['tag', 'pid']:
prop_dict[prop] = {'data':data,
'type':'int',
'name':prop}
elif 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}
# 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 ['tag']:
prop_dict[prop] = {'name':prop, 'type':'int',
'default':ParticleTAGS.Local}
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}
else:
prop_dict[prop] = {'name':prop, 'type':'double',
'default':0}
# create the particle array
pa = ParticleArray(name=name, constants=constants, **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_wcsph(constants=None, **props):
"""Return a particle array for the WCSPH formulation.
This sets the default properties to be::
['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']
Parameters
----------
constants : dict
Dictionary of constants
Other Parameters
----------------
props : dict
Additional keywords passed are set as the property arrays.
See Also
--------
get_particle_array
"""
wcsph_props = ['cs', 'ax', 'ay', 'az', 'arho', 'x0','y0', 'z0',
'u0', 'v0','w0', 'rho0', 'div']
pa = get_particle_array(
constants=constants, additional_props=wcsph_props, **props
)
# 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
def get_particle_array_iisph(constants=None, **props):
"""Get a particle array for the IISPH formulation.
The default properties are::
['x', 'y', 'z', 'u', 'v', 'w', 'm', 'h', 'rho', 'p', 'au', 'av', 'aw',
'gid', 'pid', 'tag' 'uadv', 'vadv', 'wadv', 'rho_adv', 'au', 'av',
'aw','ax', 'ay', 'az', 'dii0', 'dii1', 'dii2', 'V', 'aii', 'dijpj0',
'dijpj1', 'dijpj2', 'p', 'p0', 'piter', 'compression'
]
Parameters
----------
constants : dict
Dictionary of constants
Other Parameters
----------------
props : dict
Additional keywords passed are set as the property arrays.
See Also
--------
get_particle_array
"""
iisph_props = ['uadv', 'vadv', 'wadv', 'rho_adv',
'au', 'av', 'aw','ax', 'ay', 'az',
'dii0', 'dii1', 'dii2', 'V',
'aii', 'dijpj0', 'dijpj1', 'dijpj2', 'p', 'p0', 'piter',
'compression'
]
# Used to calculate the total compression first index is count and second
# the compression.
consts = {'tmp_comp': [0.0, 0.0]}
if constants:
consts.update(constants)
pa = get_particle_array(
constants=consts, additional_props=iisph_props, **props
)
pa.set_output_arrays( ['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'h', 'm',
'p', 'pid', 'au', 'av', 'aw', 'tag', 'gid', 'V'] )
return pa
def get_particle_array_rigid_body(constants=None, **props):
"""Return a particle array for a rigid body motion.
For multiple bodies, add a body_id property starting at index 0 with each
index denoting the body to which the particle corresponds to.
Parameters
----------
constants : dict
Dictionary of constants
Other Parameters
----------------
props : dict
Additional keywords passed are set as the property arrays.
See Also
--------
get_particle_array
"""
extra_props = ['au', 'av', 'aw', 'V', 'fx', 'fy', 'fz', 'x0', 'y0', 'z0']
body_id = props.pop('body_id', None)
nb = 1 if body_id is None else numpy.max(body_id) + 1
consts = {'total_mass':numpy.zeros(nb, dtype=float),
'num_body': numpy.asarray(nb, dtype=int),
'cm': numpy.zeros(3*nb, dtype=float),
# The mi are also used to temporarily reduce mass (1), center of
# mass (3) and the interia components (6), total force (3), total
# torque (3).
'mi': numpy.zeros(16*nb, dtype=float),
'force': numpy.zeros(3*nb, dtype=float),
'torque': numpy.zeros(3*nb, dtype=float),
# velocity, acceleration of CM.
'vc': numpy.zeros(3*nb, dtype=float),
'ac': numpy.zeros(3*nb, dtype=float),
'vc0': numpy.zeros(3*nb, dtype=float),
# angular velocity, acceleration of body.
'omega': numpy.zeros(3*nb, dtype=float),
'omega0': numpy.zeros(3*nb, dtype=float),
'omega_dot': numpy.zeros(3*nb, dtype=float)
}
if constants:
consts.update(constants)
pa = get_particle_array(constants=consts, additional_props=extra_props,
**props)
pa.add_property('body_id', type='int', data=body_id)
pa.set_output_arrays( ['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'h', 'm',
'p', 'pid', 'au', 'av', 'aw', 'tag', 'gid', 'V',
'fx', 'fy', 'fz', 'body_id'] )
return pa
def get_particle_array_tvf_fluid(constants=None, **props):
"""Return a particle array for the TVF formulation for a fluid.
Parameters
----------
constants : dict
Dictionary of constants
Other Parameters
----------------
props : dict
Additional keywords passed are set as the property arrays.
See Also
--------
get_particle_array
"""
tv_props = ['uhat', 'vhat', 'what',
'auhat', 'avhat', 'awhat', 'vmag2', 'V']
pa = get_particle_array(
constants=constants, additional_props=tv_props, **props
)
pa.set_output_arrays( ['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'p', 'h',
'm', 'au', 'av', 'aw', 'V', 'vmag2', 'pid', 'gid',
'tag'] )
return pa
def get_particle_array_tvf_solid(constants=None, **props):
"""Return a particle array for the TVF formulation for a solid.
Parameters
----------
constants : dict
Dictionary of constants
Other Parameters
----------------
props : dict
Additional keywords passed are set as the property arrays.
See Also
--------
get_particle_array
"""
tv_props = ['u0', 'v0', 'w0', 'V', 'wij', 'ax', 'ay', 'az',
'uf', 'vf', 'wf', 'ug', 'vg', 'wg']
return get_particle_array(
constants=constants, additional_props=tv_props, **props
)
def get_particle_array_gasd(constants=None, **props):
"""Return a particle array for a Gas Dynamics problem.
Parameters
----------
constants : dict
Dictionary of constants
Other Parameters
----------------
props : dict
Additional keywords passed are set as the property arrays.
See Also
--------
get_particle_array
"""
required_props = [
'x', 'y', 'z', 'u', 'v', 'w', 'rho', 'h', 'm', 'cs', 'p', 'e',
'au', 'av', 'aw', 'arho', 'ae', 'am', 'ah', 'x0', 'y0', 'z0', 'u0', 'v0', 'w0',
'rho0', 'e0', 'h0', 'div', 'grhox', 'grhoy', 'grhoz', 'dwdh', 'omega',
'converged', 'alpha1', 'alpha10', 'aalpha1', 'alpha2', 'alpha20', 'aalpha2',
'del2e']
pa = get_particle_array(
constants=constants, additional_props=required_props, **props
)
# set the intial smoothing length h0 to the particle smoothing
# length. This can result in an annoying error in the density
# iterations which require the h0 array
pa.h0[:] = pa.h[:]
pa.set_output_arrays(['x', 'y', 'u', 'v', 'rho', 'm', 'h', 'cs', 'p', 'e',
'au', 'av', 'ae', 'pid', 'gid', 'tag', 'dwdh',
'alpha1', 'alpha2'] )
return pa
def get_particles_info(particles):
"""Return the array information for a list of particles.
Returns
-------
An OrderedDict containing the property information for a list of
particles. This dict can be used for example to set-up dummy/empty
particle arrays.
"""
info = OrderedDict()
for parray in particles:
prop_info = {}
for prop_name, prop in parray.properties.items():
prop_info[prop_name] = {
'name':prop_name, 'type':prop.get_c_type(),
'default':parray.default_values[prop_name],
'data':None}
const_info = {}
for c_name, value in parray.constants.items():
const_info[c_name] = value.get_npy_array()
info[ parray.name ] = dict(
properties=prop_info, constants=const_info,
output_property_arrays=parray.output_property_arrays
)
return info
def create_dummy_particles(info):
"""Returns a replica (empty) of a list of particles"""
particles = []
for name, pa_data in info.items():
prop_dict = pa_data['properties']
constants = pa_data['constants']
pa = ParticleArray(name=name, constants=constants, **prop_dict)
pa.set_output_arrays(pa_data['output_property_arrays'])
particles.append(pa)
return particles
|