The ParticleArray class itself is documented as below.
A ParticleArray represents a collection of particles.
Bases: object
ParticleArray(str name=’‘, default_particle_tag=Local, constants=None, **props)
Class to represent a collection of particles.
There are many ways to create a ParticleArray:
>>> p = ParticleArray(name='fluid', x=[1.,2., 3., 4.])
>>> p.name
'fluid'
>>> p.x, p.tag, p.pid, p.gid
For a full specification of properties with their type etc.:
>>> p = ParticleArray(name='fluid',
... x=dict(data=[1,2], type='int', default=1))
>>> p.get_carray('x').get_c_type()
'int'
The default value is what is used to set the default value when a new particle is added and the arrays auto-resized.
To create constants that are not resized with added/removed particles:
>>> p = ParticleArray(name='f', x=[1,2], constants={'c':[0.,0.,0.]})
Constructor
Add a constant property to the particle array.
A constant property is an array but has a fixed size in that it is never resized as particles are added or removed. These properties are always stored internally as CArrays.
An example of where this is useful is if you need to calculate the center of mass of a solid body or the net force on the body.
Append props to the existing list of output arrays
Add particles in particle_array to self.
- all properties should have same length arrays.
- all properties should already be present in this particles array. if new properties are seen, an exception will be raised. properties.
Add a new property to the particle array.
If a default is not supplied 0 is assumed.
If there are no particles currently in the particle array, and a new property with some particles is added, all the remaining properties will be resized to the size of the newly added array.
If there are some particles in the particle array, and a new property is added without any particles, then this new property will be resized according to the current size.
If there are some particles in the particle array and a new property is added with a different number of particles, then an error will be raised.
- it is best not to add properties with data when you already have particles in the particle array. Reason is that, the particles in the particle array will be stored so that the ‘Real’ particles are in the top of the list and later the dummy ones. The data in your property array should be matched to the particles appropriately. This may not always be possible when there are particles of different type in the particle array.
- Properties without any values can be added anytime.
- While initializing particle arrays only using the add_property function, you will have to call align_particles manually to make sure particles are aligned properly.
Moves all ‘Local’ particles to the beginning of the array
This makes retrieving numpy slices of properties of ‘Local’ particles possible. This facility will be required frequently.
Pseudo-code:
index_arr = LongArray(n)
next_insert = 0
for i from 0 to n
p <- ith particle
if p is Local
if i != next_insert
tmp = index_arr[next_insert]
index_arr[next_insert] = i
index_arr[i] = tmp
next_insert += 1
else
index_arr[i] = i
next_insert += 1
else
index_arr[i] = i
# we now have the new index assignment.
# swap the required values as needed.
for every property array:
for i from 0 to n:
if index_arr[i] != i:
tmp = prop[i]
prop[i] = prop[index_arr[i]]
prop[index_arr[i]] = tmp
Add particles from a particle array
properties that are not there in self will be added
cl_properties: dict
cl_setup_done: ‘bool’
Clear all data held by this array
constants: dict
Copy the properties from one set to another.
To save the properties ‘x’ and ‘y’ to say ‘x0’ and ‘y0’:
>>> pa.copy_over_properties(props = {'x':'x0', 'y':'y0'}
Copy properties from source to self
default_values: dict
Increase the total number of particles by the requested amount
New particles are added at the end of the list, you may have to manually call align_particles later.
Create new particle array for particles with indices in index_array
The algorithm is as follows:
- create a new particle array with the required properties.
- resize the new array to the desired length (index_array.length)
- copy the properties from the existing array to the new array.
Return the numpy array/constant for the property names in the arguments.
indicates if properties of only real particles need to be returned or all particles to be returned. By default only real particles will be returned.
The returned numpy array does NOT own its data. Other operations may be performed.
Numpy array.
Return the c-array for the property or constant.
Return the properties that are to be load balanced. If none are explicitly set by the user, return all of the properties.
Return the number of particles
Return a dictionary of arrays held by the ParticleArray container.
This does not include the constants.
The dictionary returned is keyed on the property name and the value is the NumPy array representing the data. If all is set to False, the list of arrays is determined by the output_property_arrays data attribute.
Get the index of the property in the property array
Returns true if the array arr_name is present
indices_invalid: ‘bool’
is_dirty: ‘bool’
name: str
num_real_particles: ‘long’
output_property_arrays: list
properties: dict
property_arrays: list
Remove particles whose indices are given in index_list.
We repeatedly interchange the values of the last element and values from the index_list and reduce the size of the array by one. This is done for every property that is being maintained.
Pseudo-code for the implementation:
if index_list.length > number of particles
raise ValueError
sorted_indices <- index_list sorted in ascending order.
for every every array in property_array
array.remove(sorted_indices)
Removes property prop_name from the particle array
Remove particles that have the given tag.
Resize all arrays to the new size
Set properties from numpy arrays like objects
- the properties being set must already be present in the properties dict.
- the size of the data should match the array already present.
Set the is_dirty variable to given value
Set the indices_invalid to the given value
Set the list of output arrays for this ParticleArray
In PySPH, the solver obtains the list of property arrays to output by calling the ParticleArray.get_property_arrays method. If detailed output is not requested, the output_property_arrays attribute is used to determine the arrays that will be written to file
Set the processor id for all particles
Set value of tag to tag_value for the particles in indices
time: ‘double’
Update the min,max values of all properties
Update an array from another array and a scalar.
This situation arises often when we want to update an array from computed accelrations. The scalar in this case will be the the time step.
There are several convenience functions that provide a particle array with a requisite set of particle properties that are documented below.
Creates a LongArray working same as builtin range with upto 2 arguments both expected to be positive
Returns a replica (empty) of a list of particles
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’]
>>> 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']
Return a particle array for a Gas Dynamics problem.
get_particle_array
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'
]
get_particle_array
Return a particle array for a rigid body motion.
get_particle_array
Return a particle array for the TVF formulation for a fluid.
get_particle_array
Return a particle array for the TVF formulation for a solid.
get_particle_array
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']
get_particle_array