| 1 |
-- vim:syntax=lua tabstop=4 |
| 2 |
|
| 3 |
load("functions.lua") |
| 4 |
load("constants.lua") |
| 5 |
load("physics.lua") |
| 6 |
|
| 7 |
-- this spawn script is "proper physics ready". |
| 8 |
|
| 9 |
|
| 10 |
function describe() |
| 11 |
log("some merging galaxies") |
| 12 |
end |
| 13 |
|
| 14 |
-- ---------------------------------------------------------------- |
| 15 |
-- ---------------------------------------------------------------- |
| 16 |
|
| 17 |
function rotatevect3(pos, alpha, beta) |
| 18 |
local result=rotatevector(pos, alpha, v(1, 0, 0)) |
| 19 |
return rotatevector(result, beta, v(0, 0, 1)) |
| 20 |
end |
| 21 |
|
| 22 |
-- ---------------------------------------------------------------- |
| 23 |
function makeball_orbit(galpos, galvel, galradius, startradius, startmass, massmin, massmax, mass_sign, firstparticle, particles) |
| 24 |
local massrange = math.abs(massmax - massmin) |
| 25 |
local estmass = startmass + ((massmin + massrange/2 ) * particles) |
| 26 |
|
| 27 |
local totalmass=0.0 |
| 28 |
local pos |
| 29 |
local vel |
| 30 |
local mass |
| 31 |
local radius |
| 32 |
local speed |
| 33 |
local angle |
| 34 |
local innermass |
| 35 |
local tocenter |
| 36 |
local xmass_sign=mass_sign |
| 37 |
local endparticle = firstparticle+particles-1 |
| 38 |
|
| 39 |
-- ball volume = 4/3 * math.pi * r^3 ; ball surface=4*math.pi*r^2 |
| 40 |
-- desity = totalmass/totalvolume |
| 41 |
local density= (estmass-startmass) / (4/3 * math.pi * (galradius^3 - startradius^3)) |
| 42 |
|
| 43 |
for i=firstparticle, endparticle do |
| 44 |
radius = randomfloat(0.1,galradius-startradius) |
| 45 |
|
| 46 |
if gravit_physics < PH_PROPER then |
| 47 |
mass = randomfloat(massmin,massmax) |
| 48 |
else |
| 49 |
mass = (randomfloat(massmin,massmax)+randomfloat(massmin,massmax))/2.0 |
| 50 |
end |
| 51 |
totalmass=totalmass+mass |
| 52 |
|
| 53 |
-- makes ball with uniform mass distribution -- and correct by 1/sqrt(2), to include "outbound" orbits |
| 54 |
if gravit_physics < PH_PROPER then |
| 55 |
pos = randomrange((galradius-startradius) * 0.9) |
| 56 |
else |
| 57 |
pos = randomrange((galradius-startradius) * 0.707) |
| 58 |
end |
| 59 |
|
| 60 |
-- move pos so ball starts at "startradius" instead of (0,0,0) |
| 61 |
radius=distance(v(0,0,0), pos) |
| 62 |
tocenter=pos * (1/radius) * startradius |
| 63 |
pos=pos+tocenter |
| 64 |
radius=distance(v(0,0,0), pos) |
| 65 |
|
| 66 |
if gravit_physics < PH_PROPER then |
| 67 |
-- estimate enclosed mass; uniform mass distribution |
| 68 |
innermass = startmass + ((radius-startradius)^3/(galradius-startradius)^3)*(estmass-startmass) |
| 69 |
else |
| 70 |
-- 2nd try: estimate enclosed mass; uniform mass distribution |
| 71 |
-- proper mass estimation would be: |
| 72 |
-- mass of enclosed CIRCLE (2dim) = 2* pi * r^2 * |
| 73 |
-- raduis * |
| 74 |
-- (-1/r^3 - -1/1^3) = (-1/r^3 + 1) = (1 - 1/r^3) |
| 75 |
|
| 76 |
innermass= 2 * math.pi * (radius^2 - startradius^2) * density |
| 77 |
innermass= startmass + innermass * radius * math.pi/2.1 * (1 - 1/(radius-startradius)^3) |
| 78 |
end |
| 79 |
|
| 80 |
|
| 81 |
if (startmass > 0) then |
| 82 |
speed = orbit_velocity(innermass, radius) * randomfloat(0.9, 1.1) |
| 83 |
if gravit_physics == PH_CLASSIC then |
| 84 |
speed = speed * math.sqrt(mass * 0.5) |
| 85 |
end |
| 86 |
tocenter=pos * (1/radius) * speed |
| 87 |
vel=randomortho(pos, speed) |
| 88 |
else |
| 89 |
speed = - ((massmin+massmax)/2)/24 |
| 90 |
if gravit_physics == PH_CLASSIC then |
| 91 |
speed = speed * math.sqrt(mass * 0.5) |
| 92 |
end |
| 93 |
tocenter=pos * (1/radius) * speed |
| 94 |
vel=tocenter |
| 95 |
end |
| 96 |
|
| 97 |
if (mass_sign ==2) then |
| 98 |
xmass_sign= 1 - 2 * randomint(0,1) |
| 99 |
end |
| 100 |
|
| 101 |
particle(i, galpos + pos, galvel + vel, mass * xmass_sign) |
| 102 |
|
| 103 |
-- enhance stability by enforcing symetry |
| 104 |
if (gravit_physics == PH_PROPER) and (i<endparticle-1) then |
| 105 |
i=i+1 |
| 106 |
pos = v(-pos.x, -pos.y, -pos.z) |
| 107 |
vel = v(-vel.x, -vel.y, -vel.z) |
| 108 |
particle(i, galpos + pos, galvel + vel, mass * xmass_sign) |
| 109 |
end |
| 110 |
end |
| 111 |
|
| 112 |
return totalmass |
| 113 |
end |
| 114 |
|
| 115 |
-- ---------------------------------------------------------------- |
| 116 |
|
| 117 |
function makespiral_orbit_2(galpos, galvel, galradius, startradius, startmass, massmin, massmax, mass_sign, firstparticle, particles, ang_end) |
| 118 |
local totalmass=0.0 |
| 119 |
local massrange = math.abs(massmax - massmin) |
| 120 |
local estmass = startmass + ((massmin + massrange/2 ) * particles) |
| 121 |
|
| 122 |
local galaxyalpha = randomfloat(0, ang_end * math.pi) |
| 123 |
local galaxybeta = randomfloat(0, ang_end * math.pi) |
| 124 |
|
| 125 |
local hotv=0.25 |
| 126 |
local hotmax=math.max(galradius*0.05, startradius*randomfloat(0.25,1.1)) |
| 127 |
|
| 128 |
local hotp= 0.0 |
| 129 |
local pos |
| 130 |
local vel |
| 131 |
local pos2 |
| 132 |
local vel2 |
| 133 |
local mass |
| 134 |
local radius |
| 135 |
local speed |
| 136 |
local angle |
| 137 |
local innermass |
| 138 |
local tocenter |
| 139 |
local endparticle = firstparticle+particles-1 |
| 140 |
|
| 141 |
local density = (estmass-startmass) / (galradius-startradius) |
| 142 |
local xmass_sign=mass_sign |
| 143 |
local antirotation_mode=randomint(0,1) |
| 144 |
|
| 145 |
for i=firstparticle, endparticle do |
| 146 |
radius = randomfloat(startradius, galradius) |
| 147 |
angle = randomfloat(0,2*math.pi) |
| 148 |
|
| 149 |
if gravit_physics < PH_PROPER then |
| 150 |
mass = randomfloat(massmin,massmax) |
| 151 |
else |
| 152 |
mass = (randomfloat(massmin,massmax)+randomfloat(massmin,massmax))/2.0 |
| 153 |
end |
| 154 |
|
| 155 |
totalmass=totalmass+mass |
| 156 |
hotp = hotmax / galradius * (galradius - radius) |
| 157 |
pos = v(math.cos(angle)*radius, math.sin(angle)*radius, randomfloat(-hotp,hotp)) |
| 158 |
|
| 159 |
-- estimate enclosed mass; linear mass distribution over all radii |
| 160 |
innermass = startmass + ((radius-startradius)/(galradius-startradius))*(estmass-startmass) |
| 161 |
radius=distance(v(0,0,0), pos) |
| 162 |
|
| 163 |
speed = orbit_velocity(innermass, radius) * randomfloat(0.9, 1.1) |
| 164 |
if (gravit_physics == PH_PROPER) then |
| 165 |
speed = speed / math.sqrt(radius * 0.8 ) |
| 166 |
end |
| 167 |
if gravit_physics == PH_CLASSIC then |
| 168 |
speed = speed * math.sqrt(mass * 0.5) |
| 169 |
end |
| 170 |
|
| 171 |
tocenter=pos * (1/radius) * speed |
| 172 |
vel = v(-tocenter.y, tocenter.x, -tocenter.z + randomfloat(-(speed*hotv), speed*hotv) ) |
| 173 |
|
| 174 |
if (antirotation_mode == 1) then |
| 175 |
-- if (randomint(0,1)==0) then |
| 176 |
vel = vel * -1 |
| 177 |
pos = v(pos.x,pos.y,-pos.z) |
| 178 |
-- end |
| 179 |
end |
| 180 |
|
| 181 |
|
| 182 |
pos2 = rotatevect3(pos, galaxyalpha, galaxybeta) |
| 183 |
vel2 = rotatevect3(vel, galaxyalpha, galaxybeta) |
| 184 |
|
| 185 |
if (mass_sign ==2) then |
| 186 |
xmass_sign= 1 - 2 * randomint(0,1) |
| 187 |
end |
| 188 |
|
| 189 |
particle(i, galpos + pos2, galvel + vel2, mass * xmass_sign) |
| 190 |
|
| 191 |
if (gravit_physics == PH_PROPER) and (i<endparticle-1) then |
| 192 |
i=i+1 |
| 193 |
pos = v(-pos.x, -pos.y, -pos.z) |
| 194 |
vel = v(-vel.x, -vel.y, -vel.z) |
| 195 |
pos2 = rotatevect3(pos, galaxyalpha, galaxybeta) |
| 196 |
vel2 = rotatevect3(vel, galaxyalpha, galaxybeta) |
| 197 |
particle(i, galpos + pos2, galvel + vel2, mass * xmass_sign) |
| 198 |
|
| 199 |
end |
| 200 |
|
| 201 |
end |
| 202 |
return totalmass |
| 203 |
end |
| 204 |
|
| 205 |
-- ---------------------------------------------------------------- |
| 206 |
function makegalaxy_orbit_2(galpos, galvel, galradius, massmin, massmax, firstparticle, particles, ang_end) |
| 207 |
local kind=randomint(1, 3) |
| 208 |
local mass_sign = 1 |
| 209 |
if randomint(0,1) == 1 then kind = 2 end |
| 210 |
if (massmin > massmax) then |
| 211 |
massmin = -massmin |
| 212 |
massmax = -massmax |
| 213 |
mass_sign = -1 |
| 214 |
end |
| 215 |
local startmass |
| 216 |
|
| 217 |
-- central supermassive black hole = 1% of total mass |
| 218 |
if gravit_physics < PH_PROPER then |
| 219 |
startmass = (massmax - massmin) * particles * randomfloat(0.005, 0.05) |
| 220 |
else |
| 221 |
startmass = (massmax - massmin) * particles * randomfloat(0.001, 0.02) |
| 222 |
end |
| 223 |
|
| 224 |
local ball_particles=math.floor(particles/(randomfloat(2,8))) |
| 225 |
local ball_radius = galradius / randomfloat(3,9) |
| 226 |
|
| 227 |
local angular_vel=48 |
| 228 |
local startradius=math.pow(gravit_g * 2 * startmass, 1/3) * math.pow(angular_vel, 2/3) / math.pow (2*math.pi, 2/3) |
| 229 |
|
| 230 |
-- allow the center bulge to have different masses |
| 231 |
local factor_center=randomfloat(0.6, 2) |
| 232 |
|
| 233 |
local haloparticles=0 |
| 234 |
local halofactor=1 |
| 235 |
local halostart =galradius * 1.3 |
| 236 |
local haloend =galradius * 2 |
| 237 |
|
| 238 |
if randomint(0, 2) == 0 then |
| 239 |
-- no central SMBH |
| 240 |
startmass=randomfloat(massmin,massmax) |
| 241 |
end |
| 242 |
|
| 243 |
if (gravit_physics == PH_CLASSIC) then |
| 244 |
-- no central SMBH |
| 245 |
startmass=randomfloat(massmin,massmax) |
| 246 |
-- no surrounding massive particles |
| 247 |
end |
| 248 |
|
| 249 |
|
| 250 |
particle(firstparticle, galpos, galvel, startmass * mass_sign) |
| 251 |
|
| 252 |
if kind == 1 then |
| 253 |
makespiral_orbit_2(galpos, galvel, galradius, startradius, startmass, massmin, massmax, mass_sign, firstparticle+1, particles-1, ang_end) |
| 254 |
else |
| 255 |
local do_halo=randomint(0, 3) |
| 256 |
if (gravit_physics == PH_CLASSIC) then |
| 257 |
-- no surrounding massive particles |
| 258 |
do_halo=randomint(0, 2) |
| 259 |
end |
| 260 |
if randomint(0,1) == 1 then do_halo = 0 end |
| 261 |
if kind == 3 then do_halo = 0 end |
| 262 |
|
| 263 |
|
| 264 |
if do_halo == 3 then |
| 265 |
-- type 1 : a few massive objects at long distances |
| 266 |
halofactor =randomfloat(1,10) |
| 267 |
haloparticles = math.floor((particles-ball_particles)/(halofactor+4)) |
| 268 |
halostart = galradius * randomfloat(0.8,1.1) |
| 269 |
haloend = galradius * randomfloat(1.2,2) |
| 270 |
end |
| 271 |
if do_halo == 2 then |
| 272 |
-- type 3 : cloud of dust |
| 273 |
halofactor =randomfloat(0.1,0.6) |
| 274 |
haloparticles= randomint(math.floor((particles-ball_particles)/4), math.floor((particles-ball_particles)/1.5)) |
| 275 |
halostart = galradius * randomfloat(0.5,0.7) |
| 276 |
haloend = galradius * randomfloat(0.9,1.5) |
| 277 |
end |
| 278 |
|
| 279 |
-- bulge |
| 280 |
startmass = startmass + makeball_orbit(galpos, galvel, ball_radius, startradius, startmass, massmin*factor_center, massmax*factor_center, mass_sign, firstparticle+1, ball_particles) |
| 281 |
|
| 282 |
-- disk |
| 283 |
if gravit_physics < PH_PROPER then |
| 284 |
startmass = startmass + 1.1 * makespiral_orbit_2(galpos, galvel, galradius, startradius+ball_radius, startmass, massmin, massmax, mass_sign, firstparticle+ball_particles+haloparticles+1, particles-ball_particles-haloparticles-1, ang_end) |
| 285 |
else |
| 286 |
startmass = startmass + 1.1 * makespiral_orbit_3(galpos, galvel, galradius, startradius+ball_radius, startmass, massmin, massmax, mass_sign, firstparticle+ball_particles+haloparticles+1, particles-ball_particles-haloparticles-1, ang_end) |
| 287 |
end |
| 288 |
|
| 289 |
-- halo |
| 290 |
if (do_halo > 1) then |
| 291 |
makeball_orbit(galpos, galvel, haloend, halostart, startmass, massmin*halofactor, massmax*halofactor, mass_sign, firstparticle+ball_particles+1, haloparticles) |
| 292 |
end |
| 293 |
end |
| 294 |
end |
| 295 |
|
| 296 |
|
| 297 |
function makegalaxy_orbit(galpos, galvel, galradius, massmin, massmax, firstparticle, particles) |
| 298 |
makegalaxy_orbit_2(galpos, galvel, galradius, massmin, massmax, firstparticle, particles,2) |
| 299 |
end |
| 300 |
|
| 301 |
-- ---------------------------------------------------------------- |
| 302 |
-- ---------------------------------------------------------------- |
| 303 |
|
| 304 |
|
| 305 |
function spawn() |
| 306 |
-- work out a number of galaxies based on spawnparticles |
| 307 |
local biggalaxysize = randomint(spawnparticles/4,spawnparticles/1.5) |
| 308 |
local num_small_objects=randomint(1,3) |
| 309 |
|
| 310 |
local smallgalaxysize = spawnparticles-biggalaxysize |
| 311 |
smallgalaxysize = math.floor(smallgalaxysize / num_small_objects) |
| 312 |
biggalaxysize = spawnparticles - (smallgalaxysize * num_small_objects); |
| 313 |
|
| 314 |
local faster=2.0 |
| 315 |
local maxangle = 0.17 |
| 316 |
|
| 317 |
-- first galaxy |
| 318 |
local rad = 10.0+randomfloat(350, 1200) |
| 319 |
local massmin = faster * randomfloat(0.1,10) |
| 320 |
local massmax = faster * randomfloat(massmin+2, massmin+20) |
| 321 |
|
| 322 |
if gravit_physics < PH_PROPER then |
| 323 |
rad= 10.0+randomfloat(200, 700) |
| 324 |
massmin = faster * 0.7 * randomfloat(1,45) |
| 325 |
massmax = faster * 0.7 * randomfloat(massmin+5, massmin*2+10) |
| 326 |
maxangle = 0.36 |
| 327 |
end |
| 328 |
|
| 329 |
if (gravit_physics == PH_CLASSIC) then |
| 330 |
massmin=massmin*5 |
| 331 |
massmax=massmax*10 |
| 332 |
rad=rad * 2.2 |
| 333 |
maxangle = 0.6 |
| 334 |
end |
| 335 |
|
| 336 |
makegalaxy_orbit_2(v(0,0,0), v(0,0,0), rad, massmin, massmax, 0, biggalaxysize, maxangle) |
| 337 |
|
| 338 |
-- more galaxies |
| 339 |
for i=1, num_small_objects do |
| 340 |
massmin = faster * randomfloat(0.1,10) |
| 341 |
massmax = faster * randomfloat(massmin+2, massmin+20) |
| 342 |
local speed2 = randomfloat(2,8) |
| 343 |
local target=randomrange(rad/4) |
| 344 |
|
| 345 |
if gravit_physics < PH_PROPER then |
| 346 |
massmin = faster * 0.7 * randomfloat(1,45) |
| 347 |
massmax = faster * 0.7 * randomfloat(massmin+5, massmin*2+10) |
| 348 |
target=randomrange(rad/3) |
| 349 |
end |
| 350 |
if (gravit_physics == PH_CLASSIC) then |
| 351 |
massmin=massmin*5 |
| 352 |
massmax=massmax*10 |
| 353 |
target=randomrange(rad * 2) |
| 354 |
speed2=speed2 * 2 |
| 355 |
end |
| 356 |
|
| 357 |
-- local rad2=randomfloat(rad*0.6, rad*1.5) |
| 358 |
local rad2=randomfloat(rad*0.4, rad*1.2) |
| 359 |
local y_offset=randomfloat(-300,300) |
| 360 |
local pos2=randomrange(-200,200) |
| 361 |
pos2.x=pos2.x * 1.5 |
| 362 |
|
| 363 |
if (gravit_physics == PH_CLASSIC) then |
| 364 |
rad2 = rad2 * 1.3 |
| 365 |
y_offset=y_offset * 4 |
| 366 |
pos2=pos2 * 14 |
| 367 |
end |
| 368 |
|
| 369 |
if gravit_physics == PH_PROPER then |
| 370 |
pos2.y=pos2.y + y_offset |
| 371 |
pos2.x = pos2.x + rad2 + rad |
| 372 |
else |
| 373 |
pos2.y=pos2.y + y_offset |
| 374 |
pos2.x = pos2.x + rad2 + rad/2 |
| 375 |
end |
| 376 |
|
| 377 |
target.y = target.y * 1.5 |
| 378 |
local direction=speed2/distance(target, pos2) |
| 379 |
local vel2= (pos2 - target) * direction |
| 380 |
makegalaxy_orbit_2(pos2*1.4, vel2 * -1.3, rad2, massmin, massmax, biggalaxysize+((i-1)*smallgalaxysize), smallgalaxysize, maxangle) |
| 381 |
end |
| 382 |
end |
| 383 |
|