I01_par_distribute Test
测试目标
本页说明 tests/006_initializer/I01_par_distribute 中的参考测试。
测试验证
I01_par_distribute
提供的 sub_I01_par_distribute_equilibrium(par, nppc, il, iu, vt, vd)——
这个函数负责在 3D 网格上均匀铺粒子并按 Maxwell 分布赋初速度。
覆盖以下四个方面:
粒子数:实际填充数是否等于 \(\prod(\text{iu}-\text{il}+1)\times\prod\text{nppc}\)。
空间位置:每个粒子坐标是否严格满足 \(x = (i-1) + (i_p + 0.5)/\text{nppc}_x\)。
零热速度退化:
vt=0时所有粒子速度是否精确等于漂移速度vd(浮点精确,误差为 0)。Maxwell 速度分布统计:大样本(N=125000)下均值是否趋近
vd、标准差是否趋近vt,容差 5%。
程序结构
source_f90/main.f90依次运行四个 case。source_f90/sub_case01_count.f90:哨兵法(sentinel)验证粒子数边界。source_f90/sub_case02_positions.f90:逐粒子比对坐标解析公式,容差 1e-6。source_f90/sub_case03_vt0.f90:vt=0时速度精确等于vd,误差严格为 0。source_f90/sub_case04_maxwellian.f90:统计均值/标准差并输出case04_maxwellian.dat。source_py/plot_savefig.py读取build/case04_maxwellian.dat,绘制速度分布直方图。make.sh使用gfortran -cpp -O3 -fdefault-real-8编译(无 OpenMP 依赖)。
运行方式
cd tests/006_initializer/I01_par_distribute
bash clean.sh && bash make.sh && bash run.sh && bash plot.sh
run.sh 在 build/ 中生成 case04_maxwellian.dat;
plot.sh 在 figs/ 中生成 case04_maxwellian_hist.png。
测试算例
Case |
配置 |
验证重点 |
输出 |
|---|---|---|---|
|
A: 3×3×3 格 × 2×3×4 ppc = 648;B: 1×1×1 格 × 1×1×1 ppc = 1;C: 5×4×3 格 × 3×2×4 ppc = 1440 |
粒子数精确等于 |
终端输出 |
|
A: 单格 il=iu=[2,3,4],nppc=[4,3,2],24 粒子;B: 2×2×2 格,nppc=[2,2,2],64 粒子 |
每个粒子位置正确:按循环顺序(k,j,i 外层;kp,jp,ip 内层)逐粒子比对解析坐标,最大误差 < 1e-6。 |
终端输出 |
|
A: 4×4×4 格,nppc=[3,3,3],vt=0,vd=(1.5,−2.0,3.7);B: 2×2×2 格,vt=vd=0 |
|
终端输出 |
|
10×10×10 格,nppc=[5,5,5],N=125000;vt=(1,2,3),vd=(0.5,−1,2) |
\(|\bar{v}_\alpha - v_{d,\alpha}| < 0.05\,v_{t,\alpha}\) 且 \(|\sigma_\alpha - v_{t,\alpha}| < 0.05\,v_{t,\alpha}\);5% 容差约 17–25 个统计标准误,几乎不会随机失败。 |
|
参考图
Case 04 速度分布直方图(N=125000)。三个分量分别以对应的高斯曲线 \(\mathcal{N}(v_d, v_t^2)\) 叠加验证 Box-Muller 采样的正确性。
Test Goal
This page documents the reference test under
tests/006_initializer/I01_par_distribute. The test verifies
sub_I01_par_distribute_equilibrium(par, nppc, il, iu, vt, vd) from
I01_par_distribute —
this routine fills a 3D grid with uniformly spaced particles and samples
velocities from a Maxwellian distribution.
Four aspects are checked:
Particle count: the number of filled particles equals \(\prod(\text{iu}-\text{il}+1)\times\prod\text{nppc}\).
Spatial positions: every particle coordinate satisfies \(x = (i-1) + (i_p + 0.5)/\text{nppc}_x\) exactly.
Zero thermal speed: when
vt=0all particle velocities equalvdwith floating-point exactness (error strictly zero, not just small).Maxwellian statistics: for a large sample (N=125000) the mean converges to
vdand the standard deviation tovt, within 5% tolerance.
Program Structure
source_f90/main.f90runs the four cases in sequence.source_f90/sub_case01_count.f90: sentinel method — allocates np+1 slots, pre-fills with a sentinel value, calls the routine, then checks the boundary.source_f90/sub_case02_positions.f90: compares each particle coordinate against the analytic formula, tolerance 1e-6.source_f90/sub_case03_vt0.f90: verifies thatvt=0gives velocities exactly equal tovd(error strictly zero).source_f90/sub_case04_maxwellian.f90: checks mean/std statistics and writescase04_maxwellian.dat.source_py/plot_savefig.pyreadsbuild/case04_maxwellian.datand saves a velocity histogram.make.shcompiles withgfortran -cpp -O3 -fdefault-real-8(no OpenMP required).
Run Command
cd tests/006_initializer/I01_par_distribute
bash clean.sh && bash make.sh && bash run.sh && bash plot.sh
run.sh writes case04_maxwellian.dat in build/.
plot.sh writes case04_maxwellian_hist.png in figs/.
Test Cases
Case |
Setup |
Check |
Output |
|---|---|---|---|
|
A: 3×3×3 cells × 2×3×4 ppc = 648; B: 1×1×1 × 1×1×1 = 1; C: 5×4×3 × 3×2×4 = 1440 |
Particle count is exactly |
Terminal output |
|
A: single cell il=iu=[2,3,4], nppc=[4,3,2], 24 particles; B: 2×2×2 cells, nppc=[2,2,2], 64 particles |
Every particle lands at the correct position: loop order (outer k,j,i; inner kp,jp,ip), compared particle-by-particle against the analytic formula, max error < 1e-6. |
Terminal output |
|
A: 4×4×4 cells, nppc=[3,3,3], vt=0, vd=(1.5,−2.0,3.7); B: 2×2×2 cells, vt=vd=0 |
Every particle velocity equals |
Terminal output |
|
10×10×10 cells, nppc=[5,5,5], N=125000; vt=(1,2,3), vd=(0.5,−1,2) |
\(|\bar{v}_\alpha - v_{d,\alpha}| < 0.05\,v_{t,\alpha}\) and \(|\sigma_\alpha - v_{t,\alpha}| < 0.05\,v_{t,\alpha}\); 5% tolerance ≈ 17–25 statistical standard errors, essentially never fails randomly. |
|
Reference Figure
Case 04 velocity histograms (N=125000). Each component is overlaid with the target Gaussian \(\mathcal{N}(v_d, v_t^2)\) to verify the Box-Muller sampler.