| 1 |
-- vim:syntax=lua tabstop=4 |
| 2 |
|
| 3 |
load("functions.lua") |
| 4 |
load("constants.lua") |
| 5 |
load("physics.lua") |
| 6 |
|
| 7 |
function describe() |
| 8 |
log("some globular clusters.") |
| 9 |
end |
| 10 |
|
| 11 |
|
| 12 |
-- this spawn script is "proper physics ready". |
| 13 |
|
| 14 |
|
| 15 |
-- ---------------------------------------------------------------- |
| 16 |
-- ---------------------------------------------------------------- |
| 17 |
-- adoption of the "plummer" formula (Aarseth, Henon and Wielen) from |
| 18 |
-- http://www.artcompsci.org/kali/vol/plummer/title.html |
| 19 |
|
| 20 |
-- the support for "almost_proper" physics is an educated guess, |
| 21 |
-- but it seems to work |
| 22 |
|
| 23 |
-- This function also includes scaling from |
| 24 |
-- the "standard simulation units" system (total_mass = radius = G = 1) |
| 25 |
-- to a system with arbitrary G, mass and radius |
| 26 |
|
| 27 |
function do_plummer(center, groupvel, target_radius, target_mass , start, particles) |
| 28 |
local real_mass=0 |
| 29 |
|
| 30 |
-- compute physical units scale factors |
| 31 |
local rfak = target_radius |
| 32 |
local mfak = target_mass |
| 33 |
local vfak = math.sqrt((mfak * gravit_g)/rfak) |
| 34 |
if gravit_physics < PH_PROPER then |
| 35 |
vfak = vfak * math.sqrt(rfak) |
| 36 |
end |
| 37 |
|
| 38 |
mfak=mfak * 1.01 |
| 39 |
|
| 40 |
-- (plummer model scaling to rad = mass = g = 1 ) |
| 41 |
local scalefactor_r = (3.0 * math.pi) / 16 |
| 42 |
local scalefactor_v = math.sqrt(1.0 / scalefactor_r) |
| 43 |
|
| 44 |
-- build plummer model |
| 45 |
local avg_mass=0.94 / particles |
| 46 |
local cumulative_mass_min = 0 |
| 47 |
local cumulative_mass_max = avg_mass |
| 48 |
for i=start, start+particles-1 do |
| 49 |
--local mass = avg_mass |
| 50 |
local mass = randomfloat(avg_mass * 0.8, avg_mass * 1.2) |
| 51 |
cumulative_mass = randomfloat(cumulative_mass_min, cumulative_mass_max) |
| 52 |
cumulative_mass_min = cumulative_mass_max |
| 53 |
cumulative_mass_max = cumulative_mass_max + avg_mass |
| 54 |
|
| 55 |
local radius = 1.0 / math.sqrt( cumulative_mass^(-2.0/3.0) - 1.0) |
| 56 |
local pos = randomshell(radius) * scalefactor_r |
| 57 |
|
| 58 |
local x = 0.0 |
| 59 |
local y = 0.1 |
| 60 |
while (y > (x*x*(1.0-x*x)^3.5)) do |
| 61 |
x = randomfloat(0,1) |
| 62 |
y = randomfloat(0,0.1) |
| 63 |
end |
| 64 |
|
| 65 |
velocity = x * math.sqrt(2.0 / math.sqrt(1 + radius*radius)); |
| 66 |
|
| 67 |
if gravit_physics < PH_PROPER then |
| 68 |
velocity = velocity * math.sqrt(radius) |
| 69 |
end |
| 70 |
if gravit_physics == PH_CLASSIC then |
| 71 |
velocity = velocity * mass |
| 72 |
end |
| 73 |
|
| 74 |
|
| 75 |
vel = randomshell(velocity) * scalefactor_v |
| 76 |
|
| 77 |
-- scale pos,vel,mass to physical units, then allocate particle |
| 78 |
pos = pos * rfak |
| 79 |
vel = vel * vfak |
| 80 |
mass = mass * mfak |
| 81 |
particle(i, center + pos, groupvel + vel, mass) |
| 82 |
-- sum real mass |
| 83 |
real_mass=real_mass + mass |
| 84 |
end |
| 85 |
return real_mass |
| 86 |
end |
| 87 |
|
| 88 |
-- ---------------------------------------------------------------- |
| 89 |
-- ---------------------------------------------------------------- |
| 90 |
|
| 91 |
|
| 92 |
function place_object_plummer(num, center, vel, ratio, start, particles) |
| 93 |
local radius=randomfloat(50, 150) |
| 94 |
local mass=4* math.sqrt(radius^3) * ratio |
| 95 |
|
| 96 |
if gravit_physics < PH_PROPER then |
| 97 |
mass=mass*randomfloat(4,12) |
| 98 |
end |
| 99 |
|
| 100 |
if gravit_physics == PH_CLASSIC then |
| 101 |
mass=mass * 6 |
| 102 |
radius=radius * 6 |
| 103 |
end |
| 104 |
|
| 105 |
return do_plummer(center, vel, radius, mass, start, particles) |
| 106 |
end |
| 107 |
|
| 108 |
|
| 109 |
function spawn() |
| 110 |
|
| 111 |
local num_objects=randomint(1,6) |
| 112 |
local mass_unit=1.0 |
| 113 |
local min_distance=1500 |
| 114 |
|
| 115 |
local part=math.floor(spawnparticles / num_objects) |
| 116 |
|
| 117 |
if gravit_physics == PH_CLASSIC then |
| 118 |
min_distance= min_distance * 4 |
| 119 |
mass_unit = mass_unit * 2 |
| 120 |
end |
| 121 |
|
| 122 |
-- first postion |
| 123 |
local pos = randomrange(400) |
| 124 |
local mainvel = v(0,0,0) |
| 125 |
local first = 0 |
| 126 |
|
| 127 |
-- i want to be dirty, lets spawn some very massive objects |
| 128 |
if (randomint(0,2) > 1) then |
| 129 |
local smbhmass = 256 + (2 - gravit_physics) * 512 |
| 130 |
-- let a smbh cross the scene |
| 131 |
for k=1 ,randomint(3,12) do |
| 132 |
particle(first, v(2000,2000,0) + randomshell(800) + randomrange(800), v(-4,-4,0), smbhmass) |
| 133 |
first=first +1 |
| 134 |
end |
| 135 |
end |
| 136 |
|
| 137 |
-- first sphere |
| 138 |
local mass = place_object_plummer(0, pos, mainvel, mass_unit, first, part-first) |
| 139 |
local center=pos |
| 140 |
|
| 141 |
-- spawn some more spheres |
| 142 |
local pstart=part |
| 143 |
for i=1, num_objects-1 do |
| 144 |
if i==(num_objects-1) then |
| 145 |
parts = spawnparticles - pstart |
| 146 |
end |
| 147 |
|
| 148 |
-- pick position |
| 149 |
pos = center + randomshell(min_distance) + randomrange(min_distance/3.0) |
| 150 |
local dist=distance(pos, center) |
| 151 |
|
| 152 |
-- compute 2-body eliptic orbit velocity |
| 153 |
local ratio=mass_unit/randomfloat(0.9,4) |
| 154 |
local eccentricity = randomfloat(0.2, 0.8) |
| 155 |
local smalldist = dist * math.sqrt(1- eccentricity*eccentricity) |
| 156 |
|
| 157 |
local orbitVel=math.sqrt(gravit_g * mass *(1+ratio) * ((2/dist) - (1/smalldist))) |
| 158 |
|
| 159 |
if (gravit_physics < PH_PROPER) or (randomint(0,1)>0) then |
| 160 |
orbitVel = orbit_velocity(mass, dist) |
| 161 |
end |
| 162 |
if gravit_physics == PH_MODIFIED then |
| 163 |
orbitVel = orbitVel * 0.5 * math.sqrt(mass/2); |
| 164 |
end |
| 165 |
mainvel=randomortho(pos - center, orbitVel * randomfloat(0.5, 0.94)) |
| 166 |
|
| 167 |
-- create plummer sphere |
| 168 |
mass = place_object_plummer(i, pos, mainvel, ratio, pstart, part) |
| 169 |
center=pos |
| 170 |
pstart=pstart+part |
| 171 |
end |
| 172 |
end |