Skip to content

Commit df20373

Browse files
authored
Merge pull request #24 from elbeejay/faster-generation
Faster "exact" generation
2 parents f1ec04e + d42e00f commit df20373

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

docs/source/userguide/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Defining a :obj:`dorado.particle_track.Particles` class is a key step in using `
6464
Particle Generation and Routing
6565
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6666

67-
* Particles can be generated using the `particle_generator` function, this allows for random and exact placement of particles
67+
* Particles can be generated using the :obj:`dorado.particle_track.Particles.generate_particles` function, this allows for random and exact placement of particles
6868

6969
* Particles can be routed using either the High-Level or Low-Level API functionalities described below
7070

dorado/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "2.2.0"
1+
__version__ = "2.3.0"
22

33
from . import lagrangian_walker
44
from . import parallel_routing

dorado/particle_track.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -459,21 +459,16 @@ def generate_particles(self,
459459
in list(range(Np_tracer))]
460460

461461
elif method == 'exact':
462-
# step through seed lists to create exact list of start locations
463-
# if number of particles exceeds length of seed lists, loop back
464-
# to beginning of the seed lists and continue from there
465-
Np_to_seed = Np_tracer
466-
new_start_xindices = []
467-
new_start_yindices = []
468-
while Np_to_seed > 0:
469-
for i in range(0, len(seed_xloc)):
470-
# condition if we run out of particles within this loop
471-
if Np_to_seed <= 0:
472-
break
473-
# otherwise keep on seeding
474-
new_start_xindices = new_start_xindices + [seed_xloc[i]]
475-
new_start_yindices = new_start_yindices + [seed_yloc[i]]
476-
Np_to_seed -= 1 # reduce number of particles left to seed
462+
# create exact start indices based on x, y locations and np_tracer
463+
# get number of particles per location and remainder left out
464+
Np_divy = divmod(Np_tracer, len(seed_xloc))
465+
# assign the start locations for the evenly divided particles
466+
new_start_xindices = seed_xloc * Np_divy[0]
467+
new_start_yindices = seed_yloc * Np_divy[0]
468+
# add the remainder ones to the list one by one via looping
469+
for i in range(0, Np_divy[1]):
470+
new_start_xindices = new_start_xindices + [seed_xloc[i]]
471+
new_start_yindices = new_start_yindices + [seed_yloc[i]]
477472

478473
# Now initialize vectors that will create the structured list
479474
new_xinds = [[new_start_xindices[i]] for i in

tests/test_particle_track.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ def test_exact_locations_overflow():
133133
assert len(all_walk_data['xinds']) == num_ps
134134
assert len(all_walk_data['yinds']) == num_ps
135135

136+
def test_exact_locations_multidims():
137+
"""Test case where seed locs given as multi-dimensional arrays."""
138+
num_ps = 5
139+
particle = particle_track.Particles(params)
140+
x_seed = np.array(((0, 0), (1, 1)))
141+
y_seed = np.array(((0, 0), (1, 1)))
142+
particle.generate_particles(num_ps, x_seed, y_seed, method='exact')
143+
all_walk_data = particle.run_iteration()
144+
assert len(all_walk_data['xinds']) == num_ps
145+
assert len(all_walk_data['yinds']) == num_ps
146+
136147
def test_no_explicit_generation():
137148
"""Test reading of Np_tracer from self if some walk_data exists."""
138149
# create some walk data (would exist from a previous run)

0 commit comments

Comments
 (0)