@@ -59,7 +59,7 @@ def __init__(self, sim_data, bc, rp):
5959
6060 self .sim_data = sim_data
6161 self .bc = bc
62- self .particles = set ()
62+ self .particles = dict ()
6363 self .rp = rp
6464
6565 # TODO: read something from rp here to determine how to
@@ -94,7 +94,8 @@ def randomly_generate_particles(self, n_particles):
9494 positions [:, 1 ] = positions [:, 1 ] * (myg .ymax - myg .ymin ) + \
9595 myg .ymin
9696
97- self .particles = set ([Particle (x , y ) for (x , y ) in positions ])
97+ for (x , y ) in positions :
98+ self .particles [(x , y )] = Particle (x , y )
9899
99100 def grid_generate_particles (self , n_particles ):
100101 """
@@ -105,8 +106,6 @@ def grid_generate_particles(self, n_particles):
105106 """
106107 sq_n_particles = int (round (np .sqrt (n_particles )))
107108
108- print (f'sq_n_particles = { sq_n_particles } ' )
109-
110109 if sq_n_particles ** 2 != n_particles :
111110 msg .warning ("WARNING: Changing number of particles from {} to {}" .format (n_particles , sq_n_particles ** 2 ))
112111
@@ -116,10 +115,9 @@ def grid_generate_particles(self, n_particles):
116115 xs += 0.5 * step
117116 ys , step = np .linspace (myg .ymin , myg .ymax , num = sq_n_particles , endpoint = False , retstep = True )
118117 ys += 0.5 * step
119-
120- print (f'xs = { xs } ' )
121-
122- self .particles = set ([Particle (x , y ) for x in xs for y in ys ])
118+ for x in xs :
119+ for y in ys :
120+ self .particles [(x , y )] = Particle (x , y )
123121
124122 def update_particles (self , u , v , dt , limiter = 0 ):
125123 """
@@ -145,7 +143,7 @@ def update_particles(self, u, v, dt, limiter=0):
145143 ldelta_vx = reconstruction .limit (v , myg , 1 , limiter )
146144 ldelta_vy = reconstruction .limit (v , myg , 2 , limiter )
147145
148- for p in self .particles :
146+ for k , p in self .particles . items () :
149147 # find what cell it lives in
150148 x_idx = (p .x - myg .xmin ) / myg .dx - 0.5
151149 y_idx = (p .y - myg .ymin ) / myg .dy - 0.5
@@ -199,8 +197,8 @@ def enforce_particle_boundaries(self):
199197 TODO: copying the set and adding everything back again is messy
200198 - think of a better way to do this?
201199 """
202- new_particles = self .particles .copy ()
203- self .particles = set ()
200+ old_particles = self .particles .copy ()
201+ self .particles = dict ()
204202
205203 myg = self .sim_data .grid
206204
@@ -209,8 +207,8 @@ def enforce_particle_boundaries(self):
209207 ylb = self .bc .ylb
210208 yrb = self .bc .yrb
211209
212- while new_particles :
213- p = new_particles . pop ()
210+ while old_particles :
211+ k , p = old_particles . popitem ()
214212
215213 # -x boundary
216214 if p .x < myg .xmin :
@@ -256,15 +254,22 @@ def enforce_particle_boundaries(self):
256254 else :
257255 msg .fail ("ERROR: yrb = %s invalid BC" % (yrb ))
258256
259- self .particles . add ( p )
257+ self .particles [ k ] = p
260258
261259 self .n_particles = len (self .particles )
262260
263261 def get_positions (self ):
264262 """
265263 Return an array of particle positions.
266264 """
267- return np .array ([[p .x , p .y ] for p in self .particles ])
265+ return np .array ([[p .x , p .y ] for p in self .particles .values ()])
266+
267+ def get_init_positions (self ):
268+ """
269+ We defined the particles as a dictionary with their initial positions
270+ as the keys, so this just becomes a restructuring operation.
271+ """
272+ return np .array ([[x , y ] for (x ,y ) in self .particles .keys ()])
268273
269274 def write_particles (self , filename ):
270275 """
0 commit comments