1#pragma once
2
3static void CloudLightningTests()
4{
7 System empty(1234);
9 Check(empty.Generated()==0,"no lightning before convection arrives");
12 Check(empty.Generated()==0,"calm stratus does not generate lightning");
16 Check(empty.Generated()==0,"lightning follows the blended weather, not a newly uploaded endpoint");
18 System fine(3456),coarse(3456);
21 std::cout<<"Planetary lightning rate: "<<fine.Generated()/100.<<" flashes/second\n";
22 Check(fine.Generated()>9500 && fine.Generated()<10500,"global Poisson schedule averages 100 flashes per second");
23 Check(fine.Generated()==coarse.Generated(),"event schedule is independent of frame rate");
24 for(size_t i=0;i<fine.Events().size();++i)
25 {
26 const auto& flash=fine.Events()[i];
27 Check(std::isfinite(flash.Brightness()) && flash.Brightness()>=0,"finite bounded flash pulses");
28 if(flash.animation.inuse)
29 {
34 "scheduled cloud flash ignored shared timing constants");
36 ground.
Start(flash.animation.seed,flash.animation.path);ground.
Update(flash.animation.sequenceAge);
37 Check(std::abs(flash.Brightness()-flash.energy*ground.
flashBrightness)<.00001f,
38 "scheduled cloud flash diverged from the ground-bolt envelope");
39 const Flash* matching=
nullptr;
40 for(const auto& other:coarse.Events())
41 if(other.animation.inuse && other.animation.seed==flash.animation.seed)matching=&other;
44 && std::abs(matching->
Brightness()-flash.Brightness())<.00001f,
45 "cloud flash age or brightness depends on frame rate");
48 Check(altitude<=column.top+.005f && altitude>column.base
50 "active cloud-chain source left its local occupied cloud layer");
51 }
52 }
54 for(const auto& flash:fine.Events()) Check(flash.Brightness()==0,"disconnect clears active lightning");
56 Check(coarse.Generated()<10700,"resume skips a long backlog instead of replaying an hour of lightning");
57
58
59
60 System organic(7654),legacy(7654);
61 auto organicParams=params;organicParams.
organic=
true;
62 unsigned pairedBirths=0;
63 for(unsigned i=0;i<120;++i)
64 {
67
68
69
70 for(const auto& flash:organic.Events())if(flash.animation.inuse && flash.animation.sequenceAge<=1./60+1e-8)
71 {
72 const Flash* match=
nullptr;
73 for(const auto& other:legacy.Events())if(other.animation.seed==flash.animation.seed)match=&other;
74 Check(match && match->
energy==flash.energy && match->
radius==flash.radius
77 "organic placement changed event identity, birth timing or strength");
78 ++pairedBirths;
79 }
80 }
81 Check(organic.Generated()==legacy.Generated(),"organic shape preserves lightning event schedule");
82 Check(pairedBirths>100,"organic/legacy scheduling comparison did not exercise enough event births");
83 unsigned occupied=0;
84 for(const auto& flash:organic.Events())if(flash.animation.inuse)
85 {
86 ++occupied;
87 Check(
CloudVolume::Density(flash.position,organicParams)>=.01f,
"organic flash is embedded in moisture");
89 reference.
Update(flash.animation.sequenceAge);
91 && std::abs(reference.
flashBrightness*flash.energy-flash.Brightness())<.00001f,
92 "organic placement diverged from the shared chain timing");
93 }
94 Check(occupied>0,"organic lightning placement exercised active strikes");
95
98 highResolution.
Build(ConstantWeather(255,256));
99 Check(std::abs(uniform.
total-highResolution.
total)<1e-8,
"weather resolution does not change global distribution mass");
100 std::mt19937 rng(7);
101 const auto random=[&]() { return (double(rng())+.5)/4294967296.; };
102 int polar=0;
103 double zMean=0;
104 int visible[4]{};
105 const double observerAltitudes[]={10,117,500,1e7};
106 for(int i=0;i<40000;++i)
107 {
108 const double choose=random(),longitude=random(),latitude=random();
109 const auto normal=uniform.
Sample(choose,longitude,latitude);
110 polar+=std::abs(normal.z)>.75f; zMean+=normal.
z;
111 Check(std::abs(
CloudVolume::Length(normal)-1)<.00001f,
"spherical samples lie on the unit sphere");
112 for(
int j=0;j<4;++j) visible[j]+=normal.y>(params.
radius+10)/(params.
radius+observerAltitudes[j]);
113 }
114 Check(polar>9500 && polar<10500 && std::abs(zMean/40000)<.01,"area weighting avoids equirectangular polar bias");
115 for(int j=0;j<4;++j)
116 {
117 const double expected=.5*(1-(params.
radius+10)/(params.
radius+observerAltitudes[j]));
118 Check(std::abs(visible[j]/40000.-expected)<.006,"visible flash fraction follows the shrinking horizon cap");
119 std::cout<<observerAltitudes[j]<<" km visible cloud-shell flash rate: "<<visible[j]/400.<<"/second\n";
120 }
121 auto weighted=std::make_shared<CloudWeather::Map>();
123 auto& mip=weighted->mips.front();
124 for(unsigned y=0;y<mip.height;++y) for(unsigned x=0;x<mip.width/2;++x) mip.pixels[y*mip.width+x]=180;
126 int hottest=0;
127 for(int i=0;i<20000;++i)
128 {
129 const double choose=random(),longitude=random(),latitude=random();
130 const auto normal=storms.
Sample(choose,longitude,latitude);
132 }
133 const double expected=1/(1+
Weight(180/255.f));
134 Check(std::abs(hottest/20000.-expected)<.015,"strongest convection receives the cubic-weighted share of flashes");
135
136
137 double durationSum=0,strokeSum=0;
138 constexpr uint32_t population=32768;
139 for(uint32_t seed=0;seed<population;++seed)
140 {
146 for(
int stroke=0;stroke<ground.
strokeCount;++stroke)
147 {
148 const auto& pulse=ground.
strokes[stroke];
149 const double peak=pulse.start+pulse.attack+pulse.hold*.5;
150 cloud.
Start(seed,peak);
151 auto sample=ground;sample.
Update(peak);
153 "cloud and ground peaks differ for the same strike seed");
154 if(stroke)
155 {
156 const double gap=(ground.
strokes[stroke-1].End()+pulse.start)*.5;
157 cloud.
Start(seed,gap);
159 "cloud return-stroke gap lost the shared afterglow");
160 }
161 }
164 }
166 "cloud mean duration diverged from shared tuning");
168 "cloud mean stroke count diverged from shared tuning");
169 std::cout<<"Cloud timing population: "<<population<<" seeds; flashes="<<strokeSum/population
170 <<"; mean="<<durationSum/population<<" s\n";
171}
float Weight(float convection)
constexpr uint32_t DefaultSeed
Column Boundaries(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float Density(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
constexpr double MeanDurationSeconds
constexpr float AfterglowBrightness
constexpr double MaxDurationSeconds
constexpr double MinDurationSeconds
void Build(const CloudWeather::MapPtr &source)
Point Sample(double choose, double longitudeJitter, double latitudeJitter) const
LightningChain::Animation animation
void Start(uint32_t seed, double elapsed=0)
CloudWeather::Frame weather
void Update(double elapsed)
void Start(uint32_t packetSeed, const Path &geometry)
void Start(uint32_t strikeSeed, double durationSeconds=0)
std::array< Stroke, Timing::MaxStrokes > strokes
void Update(double elapsed)