Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/WeatherRadarTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Modules/WeatherRadarModel.h"
2#include "../Modules/WeatherRadarSource.h"
3#include <iostream>
4#include <limits>
5#include <stdexcept>
6#include <chrono>
7
8using namespace WeatherRadarModel;
9static int checks=0;
10static void Check(bool value,const char* reason)
11{ ++checks; if(!value) throw std::runtime_error(reason); }
12static Input Flight()
13{
14 Input in; in.equipped=in.power=in.dataAvailable=true; in.vehicle=1;
15 in.radiusKm=2939.6; in.position={0,-2940.6,0}; in.forward={0,0,1}; in.right={1,0,0};
16 in.rangeKm=64; in.generation=1; in.revision=1; return in;
17}
18template<class Sampler> static void Advance(Scanner& scanner,Input& in,Display& display,double seconds,Sampler sample)
19{ for(int i=0;i<int(std::round(seconds*60));++i) scanner.Update(in,1./60,sample,display); }
20static int Count(const Scanner& scanner)
21{ int n=0; for(const auto& beam:scanner.Beams()) n+=beam.valid; return n; }
22#include "WeatherRadarLimitsTests.h"
23#include "WeatherRadarSignalTests.h"
24#include "WeatherRadarGroundTests.h"
25#include "WeatherRadarAltitudeTests.h"
26#include "WeatherRadarAutoTiltTests.h"
27int main(int argc,char** argv)
28{
29 try
30 {
31 if(argc==2 && std::string(argv[1])=="--ground-replay")
32 { WriteWeatherRadarGroundReplay(std::cout); return 0; }
33 if(argc==2 && std::string(argv[1])=="--benchmark")
34 { BenchmarkWeatherRadarAutoTilt(); return 0; }
35 Check(Classify(2)==Echo::Clear && Classify(2.4999)==Echo::Clear,"clear threshold");
36 Check(Classify(2.5)==Echo::Green && Classify(6.9999)==Echo::Green,"green threshold");
37 Check(Classify(7)==Echo::Yellow && Classify(10.9999)==Echo::Yellow,"yellow threshold");
38 Check(Classify(11)==Echo::Red && Classify(14.9999)==Echo::Red,"red threshold");
39 Check(Classify(15)==Echo::Purple && Classify(16)==Echo::Purple,"purple threshold");
40 Scanner scanner; Display display; auto in=Flight(); int samples=0;
41 auto clear=[&](Point) { ++samples; return Sample{true,2,-20,10}; }; // isolated clear air, ground outside beam
42 in.equipped=false; scanner.Update(in,.1,clear,display);
43 Check(!display.enabled && samples==0,"unequipped performs no sampling");
44 in.equipped=true; in.power=false; scanner.Update(in,.1,clear,display);
45 Check(!display.enabled && samples==0,"power off performs no sampling");
46 in.power=true; scanner.Update(in,0,clear,display);
47 Check(Count(scanner)==1 && display.scanning,"startup acquires only the first bearing");
48 Advance(scanner,in,display,2,clear);
49 Check(scanner.Bearing()==60 && Count(scanner)==61,"half sweep updates only crossed bearings");
50 Advance(scanner,in,display,2,clear);
51 Check(scanner.Bearing()==120 && !display.scanning && Count(scanner)==121,"one four-second traverse");
52 Advance(scanner,in,display,2,clear);
53 Check(scanner.Bearing()==60 && scanner.Direction()==-1,"antenna reverses at right edge");
54 Advance(scanner,in,display,2,clear);
55 Check(scanner.Bearing()==0 && !display.limited && display.available,"round trip retains valid clear coverage");
56
57 // Seven modules at 60/30/10/5 FPS, 1 Hz, and fifty modules at 10 FPS.
58 // Accumulated time must retain the same four-second antenna traversal.
59 for(double cadence : {7./60,7./30,7./10,7./5,1.,5.})
60 {
61 in.maxFrameSeconds=cadence==5 ? .1 : cadence/7;
62 scanner.Reset(); scanner.Update(in,0,clear,display);
63 double time=0;
64 while(time<4-1e-9)
65 { const double dt=(std::min)(cadence,4-time); scanner.Update(in,dt,clear,display); time+=dt; }
66 Check(scanner.Bearing()==120 && Count(scanner)==121 && !display.scanning,
67 "scheduled low-frame-rate sweep completes in four elapsed seconds");
68 while(time<8-1e-9)
69 { const double dt=(std::min)(cadence,8-time); scanner.Update(in,dt,clear,display); time+=dt; }
70 Check(scanner.Bearing()==0 && display.available && !display.scanning,
71 "scheduled sweep reverses without repeatedly resetting history");
72 }
73 scanner.Reset(); in.maxFrameSeconds=.1; scanner.Update(in,5,clear,display);
74 Check(Count(scanner)==121 && scanner.Bearing()==90 && scanner.Direction()==-1,
75 "five-second scheduled turn advances and reverses without a frame pause");
76 in.maxFrameSeconds=.51; scanner.Update(in,1,clear,display);
77 Check(Count(scanner)==1 && display.scanning,"actual frame pause restarts without catch-up");
78 in.maxFrameSeconds=std::numeric_limits<double>::quiet_NaN(); scanner.Update(in,.1,clear,display);
79 Check(!display.available,"invalid scheduler frame timing clears output");
80 in.maxFrameSeconds=0;
81
82 // Two separate storms: a deep intense core must block the farther
83 // return. An adjacent unobstructed beam remains able to see that storm.
84 const auto origin=in.position;
85 auto storms=[&](Point p) {
86 const double range=Length(p-origin);
87 const bool core=range<18 && p.x<0;
88 return Sample{true,core || (range>30 && range<32) ? 16. : 2.,0,10};
89 };
90 scanner.Reset(); Advance(scanner,in,display,4.1,storms);
91 Check(scanner.Beams()[20].echoes[62]==Echo::Shadow,"near storm shadows distant storm");
92 Check(scanner.Beams()[100].echoes[62]==Echo::Red || scanner.Beams()[100].echoes[62]==Echo::Purple,
93 "unobstructed distant storm is still detected");
94 Check(display.attenuated,"shadow annunciation");
95 Check(scanner.Beams()[20].echoes[0]==Echo::Purple,"near intense return is purple");
96 Check(scanner.Beams()[20].echoes[20]==Echo::Purple,"usable intense return retains its compensated color");
97 const auto oldBeam=scanner.Beams()[100];
98 Advance(scanner,in,display,.05,clear);
99 Check(scanner.Beams()[100].echoes==oldBeam.echoes,"unswept echo memory is unchanged");
100 in.position.x+=.5; Advance(scanner,in,display,.05,clear);
101 Check(scanner.Beams()[100].origin.x==oldBeam.origin.x,"stored echoes stay at acquisition world position");
102 in.forward=Unit(Point{.3,0,1}); in.right=Unit(Cross(in.forward,Unit(in.position)));
103 Advance(scanner,in,display,.05,clear);
104 Check(scanner.Beams()[100].left.x==oldBeam.left.x,"stored echoes do not rotate with aircraft heading");
105
106 scanner.Reset(); in=Flight();
107 auto edge=[&](Point p) { return Sample{Length(p-in.position)<20,16,0,10}; };
108 Advance(scanner,in,display,4.1,edge);
109 // Use calm weather for the missing-data test so shadow cannot mask the edge.
110 auto missing=[&](Point p) { return Sample{Length(p-in.position)<20,2,0,10}; };
111 scanner.Reset(); Advance(scanner,in,display,4.1,missing);
112 Check(scanner.Beams()[60].echoes[50]==Echo::Missing && display.limited,"unknown path is never clear");
113 auto mountain=[](Point) { return Sample{true,16,2,10}; };
114 scanner.Reset(); scanner.Update(in,.1,mountain,display);
115 Check(scanner.Beams()[0].echoes[1]==Echo::TerrainShadow && !display.attenuated,
116 "terrain blocks beam without inventing precip or a weather attenuation warning");
117 auto wet=[](Point) { return Sample{true,16,0,10}; };
118 scanner.Reset(); in.position={0,-2951.6,0}; scanner.Update(in,.1,wet,display);
119 Check(scanner.Beams()[0].echoes[0]==Echo::Clear,"horizontal beam above column has no precipitation return");
120
121 // Ground and VTOL have no speed or autopilot exclusion; geometric MSL
122 // determines intersection. High ascent/coast/orbit use feed availability.
123 for(double height : {.005,.1,3.,9.4})
124 {
125 in=Flight(); in.position.y=-in.radiusKm-height; scanner.Reset(); scanner.Update(in,.1,wet,display);
126 Check(display.available,"ground hover cruise and high atmospheric flight scan");
127 }
128 for(double height : {11.,63.,200.})
129 {
130 in.position.y=-in.radiusKm-height; in.dataAvailable=false; scanner.Update(in,.1,wet,display);
131 Check(display.enabled && !display.available && display.echoes.empty(),"ascent coast and orbit clear stale data");
132 }
133 in=Flight(); scanner.Update(in,.1,wet,display);
134 Check(display.available && Count(scanner)<Bearings,"re-entry re-arms a fresh sweep");
135 for(int frame=0;frame<120;++frame)
136 { in.position.x+=8./60; scanner.Update(in,1./60,wet,display); }
137 Check(display.available,"8 km/s descent has bounded scan work without IAS exclusion");
138 in.simulator=true; scanner.Update(in,.1,wet,display);
139 Check(!display.enabled && display.echoes.empty() && Count(scanner)==0,"simulator entry resets scanner");
140 in.simulator=false; scanner.Update(in,.1,clear,display);
141 Check(display.echoes.empty() && display.scanning,"simulator exit cannot replay real-flight storm");
142 in.power=false; scanner.Update(in,.1,wet,display); in.power=true; scanner.Update(in,.1,clear,display);
143 Check(display.echoes.empty() && display.scanning,"power cycle clears memory");
144 Advance(scanner,in,display,4,wet); ++in.vehicle; scanner.Update(in,0,clear,display);
145 Check(Count(scanner)==1 && display.echoes.empty(),"vehicle switch clears old antenna history");
146 Advance(scanner,in,display,4,wet); ++in.generation; scanner.Update(in,0,clear,display);
147 Check(Count(scanner)==1 && display.echoes.empty(),"new feed token or face clears old footprint");
148 Advance(scanner,in,display,4,wet); in.position.x+=100; scanner.Update(in,0,clear,display);
149 Check(Count(scanner)==1,"teleport clears echoes");
150 in.rangeKm=1000; scanner.Update(in,.1,clear,display);
151 Check(display.rangeKm==64,"hard 64 km cap");
152 double farthest=0;
153 scanner.Reset(); scanner.Update(in,.1,[&](Point p) { farthest=(std::max)(farthest,Length(p-in.position)); return Sample{true,2,0,10}; },display);
154 Check(farthest<=64,"no sampling beyond 64 km");
155 in.rangeKm=4; scanner.Update(in,0,wet,display);
156 Check(display.rangeKm==4 && Count(scanner)==1,"map range change re-acquires at new scale");
157 scanner.Update(in,20,clear,display);
158 Check(Count(scanner)==1,"long pause cannot synthesize a full sweep");
159 in.position.x=std::numeric_limits<double>::quiet_NaN(); scanner.Update(in,.1,wet,display);
160 Check(!display.available && display.echoes.empty(),"invalid position clears output");
161 in=Flight(); scanner.Reset(); Advance(scanner,in,display,361,clear);
162 Check(!display.available && display.echoes.empty(),"stale source expires");
163 ++in.revision; Advance(scanner,in,display,.1,clear);
164 Check(display.available && display.scanning,"new source revision recovers after timeout");
165 Check(DisplaySelected(true,true,false,false,false),"weather selects map fill");
166 Check(!DisplaySelected(true,true,false,true,false),"GPWS has display priority");
167 Check(!DisplaySelected(true,true,false,false,true),"TCAS keeps unobstructed display");
168 Check(!DisplaySelected(true,true,true,false,false),"simulator keeps baseline map");
169 SPlotWeatherGrid packet{}; packet.header.cube=0; packet.header.tcp={1019,1019};
170 packet.header.revision=1; packet.header.requestToken=3;
171 for(auto& column:packet.samples) for(auto& cell:column)
172 { cell.precipitation=160; cell.ceiling=1500; cell.visibility=42000; }
173 PlotWeather::Store store;
174 Check(store.Accept(reinterpret_cast<const unsigned char*>(&packet),sizeof packet,3)==PlotWeather::Result::Full,
175 "production weather wire grid accepted");
176 auto ground=[](unsigned face,double u,double v,float& meters) {
177 Check(face==0 && u>=988 && u<=1051 && v>=988 && v<=1051,"terrain joined using weather coordinates");
178 meters=300; return true;
179 };
180 const auto decoded=SampleGrid(store,{0,-2940.6,0},10,ground);
181 Check(decoded.available && decoded.level==16 && std::fabs(decoded.surfaceKm-.3)<1e-6,
182 "wire scale and terrain meters decoded correctly");
183 Check(!SampleGrid(store,{0,2940.6,0},10,ground).available,"wrong hemisphere never reuses weather grid");
184 Check(!SampleGrid(store,{0,-2940.6,500},10,ground).available,"outside footprint is unavailable before terrain lookup");
185 Check(!SampleGrid(store,{0,-2940.6,0},10,[](unsigned,double,double,float&){return false;}).available,
186 "missing independent terrain cannot become zero elevation");
187 packet.header.revision=2; packet.samples[31][31].visibility=0;
188 store.Accept(reinterpret_cast<const unsigned char*>(&packet),sizeof packet,3);
189 Check(!SampleGrid(store,{0,-2940.6,0},10,ground).available,"masked grid point cannot become clear sky");
190 CheckWeatherRadarLimits();
191 CheckWeatherRadarSignal();
192 CheckWeatherRadarGround();
193 CheckWeatherRadarAltitude();
194 CheckWeatherRadarAutoTilt();
195 std::cout<<"PASS "<<checks<<" WeatherRadar checks\n";
196 return 0;
197 }
198 catch(const std::exception& error) { std::cerr<<"FAIL: "<<error.what()<<'\n'; return 1; }
199}
SCRIPT_CONTROL SCRIPT_STATE const void int
Result Accept(const unsigned char *p, size_t length, uint16_t expectedToken)
Step Advance(float previous, float target, float baseline, float maximum, float dt, bool dead, float wakeFactor)
Point Cross(Point a, Point b)
constexpr int Bearings
bool DisplaySelected(bool equipped, bool power, bool simulator, bool terrainPriority, bool tcasPriority)
double Length(Point a)
Echo Classify(double level)
Point Unit(Point a)
Sample SampleGrid(const PlotWeather::Store &source, Point position, double topKm, Elevation elevation, const GridFootprint *terrainFootprint=nullptr)
SRenderWeather samples[64][64]
SPlotWeatherHeader header
uint16_t visibility