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

用 WebGL2 在 0.75 cm 網格上模擬 WiFi 波,並校正粗網格誤差

Solving WiFi waves on 0.75 cm cells in WebGL2 (FDTD, and correcting for a coarse grid)

AI 導讀

作者以 WebGL2 在 GPU 上直接求解電磁波方程,製作可調整路由器位置及加畫牆壁的 WiFi 模擬頁面,並説明如何校正粗網格造成的過大訊號衰減。網格大小為 0.75 cm;校正後,5 種材料在 2 個頻率下的傳輸損耗與解析值相差不超過 0.01 dB。

正文

How much do walls weaken WiFi, and where in a home is the signal hard to reach? I made a page that solves the electromagnetic wave equation directly on the GPU and shows the waves spreading and the received strength. Plain WebGL2, no libraries.

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

You can move the router around a two-bedroom apartment and draw extra walls. This post covers how it is solved on the GPU, and how I fixed walls weakening the signal far too much because the grid was too coarse.

The equation

A 2D wave equation for only the electric field Ez perpendicular to the floor (TM mode; in 2D, Ez alone describes it exactly):

∂²E/∂t² = (1/με) ∇²E − (σ/ε) ∂E/∂t
  • cell size DX = 0.75 cm, 1280×800 cells (9.6 m × 6 m)
  • Courant number 0.5 → one step is 12.5 ps

The wavelength at 2.4 GHz is about 12.5 cm, so one wavelength is about 17 cells.

GPU: ping-pong between two textures

State lives in two RGBA32F textures, swapping read and write each step:

Channel Contents
r current Ez (u(n))
g previous Ez (u(n−1))
b exponential moving average of u² (for signal strength)

Rendering to floating-point textures needs EXT_color_buffer_float.

Materials are stored as IDs 0–7 in an R8 texture. The shader looks up coefficients from uniform arrays by ID:

  • uK2[id] = KC²/εr (metal is −1, forcing Ez = 0)
  • uA[id] = σ·DT/2ε (loss)

The outer 40 cells are a sponge with damping growing as the cube, to remove reflections.

Converting 2D falloff to 3D

In 2D, wave strength falls off as 1/r with distance r. In real 3D free space it is 1/r².

So at startup, the average of "strength × r" in an empty field is measured as C, and received strength is:

received [dBm] = 20 dBm − FSPL_3D(d) + 10·log10(mean u² · r / C)

Distance loss uses the 3D formula (free-space path loss), and only walls, reflections and interference come from the simulation.

Stuck: glass at −14 dB at 5 GHz

Material values (relative permittivity εr and conductivity σ) come from ITU-R P.2040. Used as they are, walls weakened 5 GHz far too much:

Material (5.2 GHz) Simulation without correction Analytic value
Glass 14.4 dB about 3 dB
Concrete (18 cm) 59.1 dB 23.0 dB

The cause is grid coarseness. On a 0.75 cm grid, one wavelength inside glass at 5 GHz is only about 3 cells. Wave propagation (numerical dispersion) then drifts from reality.

But a finer grid costs cells × steps (in 2D, halving the cell size costs 8×).

The fix: bisect for "values giving the same transmission loss"

So, for each material and frequency, find the εr and σ that give the analytic transmission loss on this grid, and use those:

  1. a 1D strip test that hits a slab with a plane wave, stripDb(f, id, er, sig)
  2. the analytic transmission through the slab with complex numbers, slabDb() (normal incidence)
  3. bisection for the εr and σ where stripDb equals slabDb
  4. store them in the material table as eff: [[er, σ at 2.4GHz], [er, σ at 5GHz]]

Checking, all 5 materials × 2 frequencies agree within 0.01 dB (e.g. concrete 13.37 dB at 2.437 GHz and 23.01 dB at 5.2 GHz).

This is not "bending the physical values". It cancels the coarse grid's error using a measurable quantity, transmission loss. The page says so as a "grid correction". Adding materials or changing thicknesses means regenerating them.

The gap experiment

One scene has an 8 cm-thick metal plate with gaps of 20, 8, 4 and 2 cm. When a gap is narrower than half a wavelength (6.2 cm at 2.4 GHz, 2.9 cm at 5 GHz), waves can hardly pass.

In the simulation, the 4 cm gap gives about −30 dB at 2.4 GHz and about −14 dB at 5 GHz, and nothing gets through the 2 cm gap at either frequency.

Checking for flicker

The wave display has fine moving stripes, so a single point changes brightness 8–29 times per second. Against the photosensitive-seizure guideline (WCAG), I measured brightness changes averaged over 32 px squares. Areas changing a lot more than 3 times per second covered at most 5.9% of the screen, under the guideline (about 10% of the screen).

Main sources

  • ITU-R P.2040-3 (2023) (electrical properties of building materials)
  • ITU-R P.525 (free-space attenuation)
  • IEEE 802.11 (WiFi frequencies)
  • A. Taflove, S. C. Hagness (2005) Computational Electrodynamics: The Finite-Difference Time-Domain Method, 3rd ed.
  • C. Gabriel (1996) (electrical properties of the human body)

Signal thresholds, router output and body values are rough values. Nothing here indicates effects on the body.


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