engine refactor
This commit is contained in:
parent
9e663db9dc
commit
81b8e763bd
37 changed files with 3532 additions and 1639 deletions
|
|
@ -8,8 +8,7 @@ import (
|
|||
"predictor-refactored/internal/weather"
|
||||
)
|
||||
|
||||
// noWind is a WindField that always returns zero wind. Lets us test
|
||||
// integration of vertical-only profiles deterministically.
|
||||
// noWind is a WindField that always returns zero wind.
|
||||
type noWind struct{ epoch time.Time }
|
||||
|
||||
func (n noWind) Wind(_ float64, _, _, _ float64) (weather.Sample, error) {
|
||||
|
|
@ -31,19 +30,23 @@ func TestConstantAscentToBurst(t *testing.T) {
|
|||
Name: "ascent",
|
||||
Step: 60,
|
||||
Model: Sum(ConstantRate(rate), WindTransport(noWind{}, nil)),
|
||||
Constraints: []Constraint{MaxAltitude{Limit: burst, On: ActionStop}},
|
||||
Constraints: []Constraint{Altitude{Op: OpGreaterEqual, Limit: burst, On: ActionStop}},
|
||||
}
|
||||
|
||||
prof := Profile{Stages: []*Propagator{ascend}, Direction: Forward}
|
||||
results := prof.Run(0, State{Lat: 0, Lng: 0, Altitude: 0})
|
||||
results := prof.Run(0, State{Lat: 0, Lng: 0, Altitude: 0}, NewEventSink())
|
||||
|
||||
if len(results) != 1 || results[0].Outcome != OutcomeStopped {
|
||||
t.Fatalf("expected one stopped stage, got %+v", results)
|
||||
}
|
||||
if results[0].ConstraintName == "" {
|
||||
t.Errorf("ConstraintName not populated")
|
||||
}
|
||||
if results[0].RefinedState.Altitude == 0 {
|
||||
t.Errorf("RefinedState not populated")
|
||||
}
|
||||
|
||||
last := results[0].Points[len(results[0].Points)-1]
|
||||
// Refinement tolerance is 0.01 in parameter space over a 60s step, so the
|
||||
// returned point sits within ±0.6s × rate ≈ ±3m of the boundary.
|
||||
if math.Abs(last.Altitude-burst) > 5 {
|
||||
t.Errorf("burst altitude = %v, want within 5m of %v", last.Altitude, burst)
|
||||
}
|
||||
|
|
@ -67,12 +70,12 @@ func TestProfileWithFallback(t *testing.T) {
|
|||
Name: "ascent",
|
||||
Step: 60,
|
||||
Model: ConstantRate(rate),
|
||||
Constraints: []Constraint{MaxAltitude{Limit: burst, On: ActionFallback}},
|
||||
Constraints: []Constraint{Altitude{Op: OpGreaterEqual, Limit: burst, On: ActionFallback}},
|
||||
Fallback: descent,
|
||||
}
|
||||
|
||||
prof := Profile{Stages: []*Propagator{ascend}, Direction: Forward}
|
||||
results := prof.Run(0, State{Altitude: 0})
|
||||
results := prof.Run(0, State{Altitude: 0}, NewEventSink())
|
||||
|
||||
if len(results) != 2 {
|
||||
t.Fatalf("expected 2 results (ascent then descent fallback), got %d", len(results))
|
||||
|
|
@ -91,16 +94,14 @@ func TestProfileWithFallback(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestReverseDirection(t *testing.T) {
|
||||
// Start at altitude 100m with downward rate; integrating reverse should
|
||||
// give increasing altitude.
|
||||
desc := &Propagator{
|
||||
Name: "rewind",
|
||||
Step: 1,
|
||||
Model: ConstantRate(-1), // forward: alt decreases at 1 m/s
|
||||
Constraints: []Constraint{MaxAltitude{Limit: 200, On: ActionStop}},
|
||||
Model: ConstantRate(-1),
|
||||
Constraints: []Constraint{Altitude{Op: OpGreaterEqual, Limit: 200, On: ActionStop}},
|
||||
}
|
||||
prof := Profile{Stages: []*Propagator{desc}, Direction: Reverse}
|
||||
results := prof.Run(0, State{Altitude: 100})
|
||||
results := prof.Run(0, State{Altitude: 100}, NewEventSink())
|
||||
|
||||
last := results[0].Points[len(results[0].Points)-1]
|
||||
if math.Abs(last.Altitude-200) > 1 {
|
||||
|
|
@ -129,6 +130,33 @@ func TestPiecewiseRate(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPiecewiseReferenceResolution(t *testing.T) {
|
||||
// Build via the registry with propagator_start segments.
|
||||
spec := ModelSpec{
|
||||
Type: "piecewise",
|
||||
Segments: []PiecewiseSegmentSpec{
|
||||
{Until: 100, Rate: 5, Reference: "propagator_start"},
|
||||
{Until: 200, Rate: 3, Reference: "propagator_start"},
|
||||
},
|
||||
}
|
||||
built, err := BuildModel(spec, BuildDeps{})
|
||||
if err != nil {
|
||||
t.Fatalf("BuildModel: %v", err)
|
||||
}
|
||||
if built.Build == nil {
|
||||
t.Fatalf("expected lazy build for propagator_start references")
|
||||
}
|
||||
ctx := StageContext{ProfileStart: 1000, PropagatorStart: 5000}
|
||||
m := built.Build(ctx)
|
||||
// Until=100 from propagator_start=5000 → absolute 5100.
|
||||
if r := m(5050, State{}); r.Altitude != 5 {
|
||||
t.Errorf("rate at t=5050 = %v, want 5", r.Altitude)
|
||||
}
|
||||
if r := m(5150, State{}); r.Altitude != 3 {
|
||||
t.Errorf("rate at t=5150 = %v, want 3", r.Altitude)
|
||||
}
|
||||
}
|
||||
|
||||
// fixedWind returns a constant wind sample.
|
||||
type fixedWind struct{ u, v float64 }
|
||||
|
||||
|
|
@ -139,12 +167,8 @@ func (fixedWind) Epoch() time.Time { return time.Unix(0, 0) }
|
|||
func (fixedWind) Source() string { return "test-fixed" }
|
||||
|
||||
func TestWindTransportUnitConversion(t *testing.T) {
|
||||
// Pure eastward wind of 10 m/s at the equator at sea level.
|
||||
// Expected dlng/dt = (180/pi) * 10 / (6371009 * cos(0)) ≈ 0.00008991 deg/s.
|
||||
// Expected dlat/dt = 0.
|
||||
wind := WindTransport(fixedWind{u: 10, v: 0}, nil)
|
||||
d := wind(0, State{Lat: 0, Lng: 0, Altitude: 0})
|
||||
|
||||
wantLng := (180.0 / math.Pi) * 10.0 / 6371009.0
|
||||
if math.Abs(d.Lng-wantLng) > 1e-12 {
|
||||
t.Errorf("dlng = %v, want %v", d.Lng, wantLng)
|
||||
|
|
@ -153,7 +177,6 @@ func TestWindTransportUnitConversion(t *testing.T) {
|
|||
t.Errorf("dlat = %v, want 0 for u=10 v=0", d.Lat)
|
||||
}
|
||||
|
||||
// Pure northward at 60° latitude: dlat = (180/pi) * v / R, dlng = 0.
|
||||
wind2 := WindTransport(fixedWind{u: 0, v: 5}, nil)
|
||||
d = wind2(0, State{Lat: 60, Lng: 0, Altitude: 0})
|
||||
wantLat := (180.0 / math.Pi) * 5.0 / 6371009.0
|
||||
|
|
@ -162,8 +185,28 @@ func TestWindTransportUnitConversion(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// aboveModelWind reports AboveModel on every sample. Used to verify event emission.
|
||||
type aboveModelWind struct{}
|
||||
|
||||
func (aboveModelWind) Wind(_ float64, _, _, _ float64) (weather.Sample, error) {
|
||||
return weather.Sample{AboveModel: true}, nil
|
||||
}
|
||||
func (aboveModelWind) Epoch() time.Time { return time.Unix(0, 0) }
|
||||
func (aboveModelWind) Source() string { return "above" }
|
||||
|
||||
func TestWindTransportEmitsAboveModel(t *testing.T) {
|
||||
sink := NewEventSink()
|
||||
wind := WindTransport(aboveModelWind{}, sink)
|
||||
for range 3 {
|
||||
_ = wind(0, State{})
|
||||
}
|
||||
events := sink.Snapshot()
|
||||
if len(events) != 1 || events[0].Type != "above_model" || events[0].Count != 3 {
|
||||
t.Errorf("expected one above_model event with count=3, got %+v", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateAddWrapsLongitude(t *testing.T) {
|
||||
// Demonstrates state algebra used by the integrator and refinement.
|
||||
s := stateAdd(State{Lat: 0, Lng: 350, Altitude: 0}, 1, State{Lng: 20})
|
||||
if math.Abs(s.Lng-10) > 1e-9 {
|
||||
t.Errorf("addState wrap: lng = %v, want 10", s.Lng)
|
||||
|
|
@ -174,3 +217,39 @@ func TestStateAddWrapsLongitude(t *testing.T) {
|
|||
t.Errorf("lerpState lng wrap: %v, want 0 or 360", mid.Lng)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPolygonInside(t *testing.T) {
|
||||
// Unit square at the equator.
|
||||
square := []PolygonVertex{
|
||||
{Lat: -1, Lng: -1},
|
||||
{Lat: -1, Lng: 1},
|
||||
{Lat: 1, Lng: 1},
|
||||
{Lat: 1, Lng: -1},
|
||||
}
|
||||
c := Polygon{Vertices: square, Mode: PolygonInside, On: ActionStop}
|
||||
if !c.Violated(0, State{Lat: 0, Lng: 0}) {
|
||||
t.Errorf("origin should be inside the square")
|
||||
}
|
||||
if c.Violated(0, State{Lat: 5, Lng: 0}) {
|
||||
t.Errorf("(5, 0) should be outside the square")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPolygonOutsideAntimeridian(t *testing.T) {
|
||||
// A polygon centred near the antimeridian, spanning lng 170..-170
|
||||
// (i.e. lng 170..190 in [0, 360) form).
|
||||
poly := []PolygonVertex{
|
||||
{Lat: -10, Lng: 170},
|
||||
{Lat: -10, Lng: 190},
|
||||
{Lat: 10, Lng: 190},
|
||||
{Lat: 10, Lng: 170},
|
||||
}
|
||||
c := Polygon{Vertices: poly, Mode: PolygonInside, On: ActionStop}
|
||||
// A point at the antimeridian.
|
||||
if !c.Violated(0, State{Lat: 0, Lng: 180}) {
|
||||
t.Errorf("(0, 180) should be inside the antimeridian polygon")
|
||||
}
|
||||
if c.Violated(0, State{Lat: 0, Lng: 0}) {
|
||||
t.Errorf("(0, 0) should be outside")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue