跳到正文
DEV Community· sishiya (しし屋)·· 2 小時前AI 評分38

用手指撕開地球:在 Web Worker 中模擬 16,000 個 SPH 粒子

Tearing the Earth with your finger: 16,000-particle SPH in a Web Worker

AI 導讀

「God's-Eye Map」以 CesiumJS 展示地球,並用 Web Worker 執行 16,000 個粒子的 SPH 模擬,讓用戶從太空視角撕開地球,呈現岩石和海水的變化。每步計算在 16 核心電腦上約需 30 ms;作者亦調整手部移動速度及裂縫判定方式,以改善模擬效果。

正文

"God's-Eye Map" shows the Earth with CesiumJS and lets you travel from the ground to space using public data that needs no API keys, such as Japan's GSI maps and the PLATEAU 3D city models.

https://sishiya.github.io/manabi-lab/god-view/?lang=en

Inside it there is a button that appears only while you look at Earth from space: "✋ Tear off a piece of Earth". Pinch part of the Earth and pull, and a hole opens, hot rock comes out and seawater boils.

This post covers how that calculation (SPH, a particle method for fluids) is built and where I got stuck.

At Earth's scale, gravity is more than 100 times the strength of rock, so rock can be treated as a fluid. Still, small fragments would really keep their shape better, and other things are simplified (the app labels them "estimate").

Building an Earth of particles

The Earth is about 16,000 particles. Spacing s is about 408 km, the smoothing length h = 1.1 s, and each particle has about 45 neighbors within 2h.

Layer boundaries (inner core 1221 km, core–mantle boundary 3480 km) follow PREM, the standard Earth model.

Density uses an equation of state for each layer (Murnaghan), solved so that gravity and pressure balance:

Layer ρ0 (kg/m³) K0 (GPa) K′
Mantle 3500 160 4
Outer core 6100 90 4
Inner core 7800 170 4.5

The result: central pressure 340 GPa, mass 5.66×10²⁴ kg (5% lighter than the real Earth). Densities are close to PREM but not the same.

Particles on a lattice get a mass of "density × s³", and the planet is relaxed for 300 steps (multiplying speed by 0.9 each step to stop the shaking) before starting.

Inside the SPH

  • density is tracked with the continuity equation; summing particle masses gave too little density at the surface and wrong pressure
  • pressure is never below 0 (no tension), so things tear easily
  • artificial viscosity (α = 1, β = 2), with its work turned into heat
  • one step is about 10 seconds, set by the speed of sound (about 11 km/s) and particle spacing

Summing gravity over every pair is heavy, so it is computed as spherically symmetric. For each body (Earth, fragment), the space is cut into 256 shells from its center, and each particle is pulled only by the mass inside its radius.

Temperature is tracked too. Released pressure cools by adiabatic expansion, and the energy of material collapsing inward becomes heat. Compared with the melting onset temperature (solidus), it also gives a "partly molten fraction".

Running it in a Web Worker

The calculation runs in one Web Worker. One step takes about 30 ms (on a 16-core PC). Drawing happens on the main thread, with particles drawn as round point sprites in a shader.

window does not exist inside a Worker, so shared code lives on self.GV.

Stuck: how fast the "hand" moves

The hardest part was not the calculation but how to move the pinching hand.

Particles inside a sphere of radius Rg around the pinched point are pulled toward the hand with a spring (critically damped).

1. The hand was too fast
Using the finger's motion directly as the hand position stirred the Earth with the fragment, reaching 44 km/s and +350,000 °C. Now the hand approaches the pointed spot with a speed limit.

2. The hand was too slow
With a limit of 3 km/s, the hole filled in before it tore off, and you could not see a deep hole. Now it is 8 km/s while pulling and 15 km/s while carrying.

3. Carrying in a straight line scraped the ground
Carrying the fragment in a straight line skimmed the surface and blasted away the ground (+100,000 °C). Now it first moves out to 14,000 km from the center, then swings around.

While you pull (until it tears), the calculation runs more than 300× faster.

Stuck: deciding what "cracked"

I wanted the cracked surface around the hole to glow as hot rock.

At first, "cracked" was decided by how far a particle moved from its starting place. Then the whole Earth wobbling made a wide area count as cracked.

So it now uses spacing to neighboring particles. If the spacing to surface particles that were neighbors at the start (up to 6) changes by 35% or more, it is "cracked". When the whole Earth wobbles, neighbor spacing barely changes.

Oceans and steam

The ocean averages 3.7 km, much thinner than a particle, so it is a separate set of light particles (about 3600). Ocean locations come from checking an Earth photo (NASA's Blue Marble) at 7000 points on the sphere and keeping only blue spots.

At first, water particles slipped into gaps between surface particles, touched the inner rock below, and half the ocean was "boiling". The fix was to treat only rock that is truly exposed at the surface as hot.

Main sources

  • A. M. Dziewonski, D. L. Anderson (1981) Preliminary reference Earth model, Physics of the Earth and Planetary Interiors 25
  • J. J. Monaghan (1992) Smoothed particle hydrodynamics, Annual Review of Astronomy and Astrophysics 30
  • T. Katsura et al. (2010) Physics of the Earth and Planetary Interiors 183 (adiabatic temperature of the mantle)
  • T. Lay, J. Hernlund, B. A. Buffett (2008) Nature Geoscience 1 (heat at the core–mantle boundary)
  • M. M. Hirschmann (2000) Geochemistry, Geophysics, Geosystems 1 / G. Fiquet et al. (2010) Science 329 (mantle melting onset)

Sources and terms of use for maps, photos and 3D city models are shown in the app.


This page is part of Manabi Lab, a series of small learning apps that run in the browser (English and Japanese). The code and this post were written together with AI (Claude).

https://sishiya.github.io/manabi-lab/?lang=en

來源:DEV Community · dev.to