Skip to content

Commit

Permalink
Merge pull request #213 from prabhuramachandran/fix-extract-particles…
Browse files Browse the repository at this point in the history
…-with-strides

Fix bug with extract particles into destination...
  • Loading branch information
prabhuramachandran committed May 17, 2019
2 parents f07ec07 + 6160e1b commit 69965fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pysph/base/device_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ def extra_args(self):

def template(self, i, indices, start_idx):
'''
idx, s_idx, s_i, j = declare('int', 4)
idx, s_idx, s_i, j, start = declare('int', 5)
idx = indices[i]
% for prop in obj.prop_names:
s_idx = stride_${prop} * idx
s_i = stride_${prop} * i
start = stride_${prop} * start_idx
for j in range(stride_${prop}):
dst_${prop}[start_idx + s_i + j] = src_${prop}[s_idx + j]
dst_${prop}[start + s_i + j] = src_${prop}[s_idx + j]
% endfor
'''

Expand Down Expand Up @@ -701,7 +702,9 @@ def empty_clone(self, props=None):
else:
prop_names = props

result_array = pysph.base.particle_array.ParticleArray()
result_array = pysph.base.particle_array.ParticleArray(
backend=self._particle_array.backend
)
result_array.set_name(self._particle_array.name)
result_array.set_device_helper(DeviceHelper(result_array,
backend=self.backend))
Expand Down
2 changes: 1 addition & 1 deletion pysph/base/particle_array.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ cdef class ParticleArray:
dst_prop_array = dest_array.get_carray(prop)
stride = self.stride.get(prop, 1)
src_prop_array.copy_values(index_array, dst_prop_array,
stride, start_idx)
stride, stride*start_idx)

if align:
dest_array.align_particles()
Expand Down
20 changes: 20 additions & 0 deletions pysph/base/tests/test_particle_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,26 @@ def test_extract_particles_works_without_specific_props_with_dest(self):
self.assertEqual(n.x[0], 2.0)
numpy.testing.assert_array_equal(n.A, [2, 3])

def test_extract_particles_works_with_non_empty_dest(self):
# Given
p = particle_array.ParticleArray(name='f', x=[1, 2, 3],
backend=self.backend)
p.add_property('A', data=numpy.arange(6), stride=2)
n = p.empty_clone()
n.extend(2)

# When.
p.extract_particles([1], dest_array=n)
self.pull(n)

# Then.
self.assertEqual(len(p.x), 3)
self.assertEqual(len(p.A), 6)
self.assertEqual(len(n.x), 3)
self.assertEqual(len(n.A), 6)
numpy.testing.assert_array_equal(n.x, [0.0, 0.0, 2.0])
numpy.testing.assert_array_equal(n.A, [0, 0, 0, 0, 2, 3])

def test_extract_particles_works_with_specific_props_with_dest(self):
# Given
p = particle_array.ParticleArray(name='f', x=[1, 2, 3], y=[0, 0, 0],
Expand Down

0 comments on commit 69965fa

Please sign in to comment.