Skip to content

Commit cece3d2

Browse files
Update icbuilder.py
1 parent a52d0a1 commit cece3d2

1 file changed

Lines changed: 35 additions & 42 deletions

File tree

tools/icbuilder.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -347,45 +347,37 @@ def random_solar_system(
347347

348348
def spiral_ic(N, A=1.0, B=1.0, arms=2, mass=1.0, noise=0.02):
349349
"""
350-
Generates particles along the logarithmic spiral:
350+
Generates baryons along the Ringermacher-Mead spiral:
351351
r(phi) = A * log( B * tan(phi / (2*arms)) )
352352
- N: number of particles
353353
- A, B: spiral parameters
354-
- arms: number of spiral arms (N in your formula)
354+
- arms: number of spiral arms
355355
- mass: total mass
356356
- noise: random positional jitter
357357
type = 0 (baryons)
358358
"""
359359
import math, random
360360
particles = []
361-
362-
for i in range(N):
363-
# angle along spiral
364-
phi = random.uniform(0.1, math.pi * 2 * arms - 0.1)
365-
366-
# radius from your formula
367-
r = A * math.log(B * math.tan(phi / (2 * arms)))
368-
369-
# convert to Cartesian
361+
count = 0
362+
while count < N:
363+
phi = random.uniform(0.001, math.pi * arms - 0.001)
364+
val = B * math.tan(phi / (2 * arms))
365+
if val <= 0:
366+
continue # skip invalid domain
367+
r = A * math.log(val)
370368
x = r * math.cos(phi) + noise * (random.random() - 0.5)
371369
y = r * math.sin(phi) + noise * (random.random() - 0.5)
372370
z = noise * (random.random() - 0.5)
373-
374-
# no initial velocity (cold start)
375-
vx = vy = vz = 0.0
376-
377-
particles.append((x, y, z, vx, vy, vz, mass / N, 0))
378-
371+
particles.append((x, y, z, 0, 0, 0, mass / N, 0))
372+
count += 1
379373
return particles
380374

381375
def spiral_dm_ic(N, A=1.0, B=1.0, arms=2,
382-
mass=1.0, dm_fraction=0.85,
383-
noise=0.02, dm_scale=1.5):
376+
mass=1.0, dm_fraction=0.85,
377+
noise=0.02, dm_scale=1.5):
384378
"""
385-
Logarithmic spiral ICs with baryons (type=0) + DM (type=1).
386-
387-
r(phi) = A * log( B * tan(phi / (2*arms)) )
388-
379+
Spiral ICs with baryons (type=0) and DM (type=1) using:
380+
r(phi) = A * log( B * tan(phi / (2*arms)) )
389381
- N: number of baryon particles (DM will also be N)
390382
- A, B: spiral parameters
391383
- arms: number of spiral arms
@@ -394,37 +386,38 @@ def spiral_dm_ic(N, A=1.0, B=1.0, arms=2,
394386
- noise: positional jitter
395387
- dm_scale: DM radius multiplier (DM more extended)
396388
"""
397-
398389
import math, random
399390
particles = []
400-
401391
M_b = mass * (1 - dm_fraction)
402392
M_dm = mass * dm_fraction
403393

404-
# -----------------------
405-
# BARYONS (type = 0)
406-
# -----------------------
407-
for _ in range(N):
408-
phi = random.uniform(0.1, math.pi * 2 * arms - 0.1)
409-
r = A * math.log(B * math.tan(phi / (2 * arms)))
410-
394+
# Baryons
395+
count = 0
396+
while count < N:
397+
phi = random.uniform(0.001, math.pi * arms - 0.001)
398+
val = B * math.tan(phi / (2 * arms))
399+
if val <= 0:
400+
continue
401+
r = A * math.log(val)
411402
x = r * math.cos(phi) + noise * (random.random() - 0.5)
412403
y = r * math.sin(phi) + noise * (random.random() - 0.5)
413404
z = noise * (random.random() - 0.5)
414-
415405
particles.append((x, y, z, 0, 0, 0, M_b / N, 0))
416-
417-
# -----------------------
418-
# DARK MATTER (type = 1)
419-
# -----------------------
420-
for _ in range(N):
421-
phi = random.uniform(0.1, math.pi * 2 * arms - 0.1)
422-
r = dm_scale * A * math.log(B * math.tan(phi / (2 * arms)))
423-
406+
count += 1
407+
408+
# Dark Matter
409+
count = 0
410+
while count < N:
411+
phi = random.uniform(0.001, math.pi * arms - 0.001)
412+
val = B * math.tan(phi / (2 * arms))
413+
if val <= 0:
414+
continue
415+
r = dm_scale * A * math.log(val)
424416
x = r * math.cos(phi) + noise * (random.random() - 0.5)
425417
y = r * math.sin(phi) + noise * (random.random() - 0.5)
426418
z = noise * (random.random() - 0.5)
427-
428419
particles.append((x, y, z, 0, 0, 0, M_dm / N, 1))
420+
count += 1
429421

430422
return particles
423+

0 commit comments

Comments
 (0)