Skip to content

Commit 7d8c94a

Browse files
committed
Particles seem to work withswe
1 parent 181ad07 commit 7d8c94a

6 files changed

Lines changed: 62 additions & 22 deletions

File tree

advection/simulation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ def dovis(self):
127127
if self.particles is not None:
128128
particle_positions = self.particles.get_positions()
129129

130+
# dye particles
131+
colors = self.particles.get_init_positions()[:,0]
132+
130133
# plot particles
131-
ax.scatter(particle_positions[:, 0], particle_positions[:, 1])
134+
ax.scatter(particle_positions[:, 0],
135+
particle_positions[:, 1], c=colors, cmap="Greys")
132136
ax.set_xlim([myg.xmin, myg.xmax])
133137
ax.set_ylim([myg.ymin, myg.ymax])
134138

incompressible/simulation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,15 @@ def dovis(self):
445445

446446
plt.colorbar(img, ax=ax)
447447

448-
ax = axes.flat[0]
449448
if self.particles is not None:
449+
ax = axes.flat[0]
450450
particle_positions = self.particles.get_positions()
451+
# dye particles
452+
colors = self.particles.get_init_positions()[:, 0]
451453

452454
# plot particles
453-
ax.scatter(particle_positions[:, 0], particle_positions[:, 1], s=5)
455+
ax.scatter(particle_positions[:, 0],
456+
particle_positions[:, 1], s=5, c=colors, alpha=0.8, cmap="Greys")
454457
ax.set_xlim([myg.xmin, myg.xmax])
455458
ax.set_ylim([myg.ymin, myg.ymax])
456459

particles/particles.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
"""

particles/tests/test_particles.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_particles_advect():
115115

116116
ps.update_particles(u, v, 1)
117117

118-
positions = set([(p.x, p.y) for p in ps.particles])
118+
positions = set([(p.x, p.y) for p in ps.particles.values()])
119119

120120
xs, step = np.linspace(0, 1, num=7, endpoint=False, retstep=True)
121121
xs += 0.5 * step
@@ -129,7 +129,7 @@ def test_particles_advect():
129129

130130
ps.update_particles(u, v, 1)
131131

132-
positions = set([(p.x, p.y) for p in ps.particles])
132+
positions = set([(p.x, p.y) for p in ps.particles.values()])
133133

134134
correct_positions = set([(x+1, y) for x in xs for y in xs])
135135

@@ -142,7 +142,7 @@ def test_particles_advect():
142142
ps = particles.Particles(sim.cc_data, bc, rp)
143143
ps.update_particles(u, v, 1)
144144

145-
positions = set([(p.x, p.y) for p in ps.particles])
145+
positions = set([(p.x, p.y) for p in ps.particles.values()])
146146

147147
correct_positions = set([(x, y+1) for x in xs for y in xs])
148148

@@ -154,7 +154,7 @@ def test_particles_advect():
154154
ps = particles.Particles(sim.cc_data, bc, rp)
155155
ps.update_particles(u, v, 1)
156156

157-
positions = set([(p.x, p.y) for p in ps.particles])
157+
positions = set([(p.x, p.y) for p in ps.particles.values()])
158158

159159
correct_positions = set([(x+1, y+1) for x in xs for y in xs])
160160

swe/_defaults

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ limiter = 2 ; limiter (0 = none, 1 = 2nd order, 2 = 4th order)
1212
grav = 1.0 ; gravitational acceleration (in y-direction)
1313

1414
riemann = Roe ; HLLC or Roe
15+
16+
17+
[particles]
18+
do_particles = 1
19+
particle_generator = grid

swe/simulation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import mesh.boundary as bnd
1111
from simulation_null import NullSimulation, grid_setup, bc_setup
1212
import util.plot_tools as plot_tools
13+
import particles.particles as particles
1314

1415

1516
class Variables(object):
@@ -129,6 +130,9 @@ def initialize(self, extra_vars=None, ng=4):
129130

130131
self.cc_data = my_data
131132

133+
if self.rp.get_param("particles.do_particles") == 1:
134+
self.particles = particles.Particles(self.cc_data, bc, self.rp)
135+
132136
# some auxillary data that we'll need to fill GC in, but isn't
133137
# really part of the main solution
134138
aux_data = self.data_class(my_grid)
@@ -195,6 +199,13 @@ def evolve(self):
195199
dtdx*(Flux_x.v(n=n) - Flux_x.ip(1, n=n)) + \
196200
dtdy*(Flux_y.v(n=n) - Flux_y.jp(1, n=n))
197201

202+
if self.particles is not None:
203+
limiter = self.rp.get_param("swe.limiter")
204+
u, v = self.cc_data.get_var("velocity")
205+
206+
self.particles.update_particles(u, v, self.dt, limiter)
207+
self.particles.enforce_particle_boundaries()
208+
198209
# increment the time
199210
self.cc_data.t += self.dt
200211
self.n += 1
@@ -262,6 +273,18 @@ def dovis(self):
262273
else:
263274
ax.set_title(field_names[n])
264275

276+
if self.particles is not None:
277+
ax = axes[0]
278+
particle_positions = self.particles.get_positions()
279+
# dye particles
280+
colors = self.particles.get_init_positions()[:, 0]
281+
282+
# plot particles
283+
ax.scatter(particle_positions[:, 0],
284+
particle_positions[:, 1], s=5, c=colors, alpha=0.8, cmap="Greys")
285+
ax.set_xlim([myg.xmin, myg.xmax])
286+
ax.set_ylim([myg.ymin, myg.ymax])
287+
265288
plt.figtext(0.05, 0.0125, "t = {:10.5g}".format(self.cc_data.t))
266289

267290
plt.pause(0.001)

0 commit comments

Comments
 (0)