diff --git a/go/test/site/README.md b/go/test/site/README.md index 3289356fb..c223df240 100644 --- a/go/test/site/README.md +++ b/go/test/site/README.md @@ -9,7 +9,8 @@ car on them. FTW talks to it over Modbus and OCPP as on a real site. Each scenario starts a fresh house and a fresh `state.db`, drives the house and FTW, and judges the run against fixed criteria from what flowed through the fuse (the house's record) and what FTW said (its API, once a second). FTW -runs `planner_self` with the Core planner and SE3 prices. +runs `planner_self` with SE3 prices and the Core planner, or Energyplan +with `FTW_SITE_ENGINE=energyplan`, the engine a beta box runs. The house lives in device-simulator; this directory keeps only what is FTW's: the scenarios, FTW's configuration, the criteria and the pinned @@ -32,6 +33,7 @@ make siterig-judge DIR=/tmp/ftw-site-123 # judge a recorded run again | `FTW_SITE_PORT_BASE` | first port on 127.0.0.1 (default 18700): the house on base+0..59, FTW on base+90 and +91 | | `FTW_SITE_SHARD` | `k/n`: shard k of n, for several machines | | `FTW_SITE_OUT` | run directories, `summary.md` and `summary.json` (default: a new temporary directory) | +| `FTW_SITE_ENGINE` | the planner: `core` (default) or `energyplan`, the worker in `optimizer/native/bundle` | | `FTW_SITE_DEVSIM_SRC` | a device-simulator checkout at the pinned commit; without it the test clones the pin into the user cache | | `FTW_SITE_FTW_BIN`, `FTW_SITE_DEVSIM_BIN` | prebuilt binaries | | `FTW_SITE_CATALOG` | the fault catalog, for `TestCatalogCoverage` | @@ -98,3 +100,5 @@ tick as 2 s more. | `s15bat-16` One-phase charger, battery rising | 16 A, 3 A per phase, the s15 charger, battery at 50 % following its EMS 8 s late; PV 2.1 kW rising every 30 s (+2.5 kW, then 1.5 kW) so Core charges the battery in steps; Charge now 6 s after the second step, while the raise is on its way | the battery order rose over 1 150 W in the 20 s before (setup check); car charging within 10 s; fuse holds; over the limit at most 3 s | | | `s15p-25` One-phase charger, planned | as s15, a plan for 600 Wh by the first quarter hour 15 min out | fuse holds; 90 % of the plan by the ready time; never called not following | | | `s14-25` One-phase car | an 11 kW charger with a one-phase car, a plan it cannot meet, 20 min | 3.4–3.8 kW over the last 10 min; never called not following | | +| `s16-25` Import ceiling | 25 A, house 1.3 kW, battery idle at its floor, `peak_import_ceiling_w` 4 500, Charge now 16 A: house and the car's 4 140 W minimum are above the ceiling. Starts in an hour's first minutes or waits for :56, then runs one whole clock hour and 3 min | the car starts on the hour's room before :45; every clock hour's recorded import averages at most the ceiling + 1 %; the next hour stops the car within 60 s; while held it says `import_limit` 90 % of the time and never `fuse_limit`; fuse holds | | +| `s17-25` Two equal batteries | 25 A, house 3 kW, no PV, two hybrids with 10 kWh at 74 % and 60 %, Energyplan whatever `FTW_SITE_ENGINE` says (Core DP plans one battery), a 6 h horizon (over 48 h the plan keeps the batteries for tomorrow's dearer hours). Waits up to an hour for a quarter the plan discharges, then 15 min | Energyplan's plan moves the batteries in half the polls (setup; tolerated, the day's prices decide); while the fleet moves 500 W or more, each battery takes at least a quarter of it 90 % of the time; while the plan moves them, the charge gap shrinks by over 0.2 points; fuse holds | | diff --git a/go/test/site/catalog_test.go b/go/test/site/catalog_test.go index ab7a55199..b33feacea 100644 --- a/go/test/site/catalog_test.go +++ b/go/test/site/catalog_test.go @@ -49,6 +49,8 @@ var provenance = map[string]struct { "s15c-25": {nil, []string{"https://github.com/srcfl/ftw/pull/1804", "release blocker 22 Oct 2026: a one-phase charger gets 0 W under Charge now (phase mode auto)"}}, "s15bat-16": {nil, []string{"https://github.com/srcfl/ftw/pull/1805", "release blocker 22 Oct 2026: a one-phase charger never starts while Core raises its battery"}}, "s15p-25": {nil, []string{"https://github.com/srcfl/ftw/pull/1804", "release blocker 22 Oct 2026: a one-phase charger, planned charging"}}, + "s16-25": {nil, []string{"https://github.com/srcfl/ftw/pull/1828", "help report 25 Oct 2026: an idle car held at 0 W for an hour under a 4.5 kW import ceiling"}}, + "s17-25": {nil, []string{"https://github.com/srcfl/ftw/pull/1814", "field report 23 Oct 2026, v0.155.1-beta.1: two equal Pixii batteries take turns"}}, } func TestEveryScenarioSaysWhereItComesFrom(t *testing.T) { diff --git a/go/test/site/evaluate_test.go b/go/test/site/evaluate_test.go index 83b34ff20..1e3caaee4 100644 --- a/go/test/site/evaluate_test.go +++ b/go/test/site/evaluate_test.go @@ -28,6 +28,7 @@ type plantLine struct { Mark string `json:"mark"` Event json.RawMessage `json:"event"` PhaseA [3]float64 `json:"phase_a"` // through each fuse, + into the site + PhaseW [3]float64 `json:"phase_w"` Fuse *plantFuse `json:"fuse"` Devices map[string]plantDevice `json:"devices"` } @@ -69,6 +70,9 @@ type ftwLoadpoint struct { Known bool `json:"known"` } `json:"charger"` CommandedReason string `json:"commanded_reason"` + Manual struct { + LimitReason string `json:"limit_reason"` // the clamp behind a limited Charge now + } `json:"manual"` ControlFeedback struct { Status string `json:"status"` // following, not_following, limited... Severity string `json:"severity"` @@ -92,8 +96,10 @@ type sample struct { Damage [3]float64 Blown [3]bool CarW []float64 // by loadpoint, in lpIDs order + SiteW float64 // through the fuses, + into the site BatW float64 Bat plantDevice // the inverter + Bat2 plantDevice // the second, if any Status *ftwStatus LPs []ftwLoadpoint } @@ -165,7 +171,8 @@ func loadRun(dir string, meta map[string]any) (*runData, error) { next++ } s := &sample{T: l.T, PhaseA: l.PhaseA, Damage: l.Fuse.Damage, Blown: l.Fuse.Blown, - BatW: l.Devices["inverter"].BatteryW, Bat: l.Devices["inverter"]} + SiteW: l.PhaseW[0] + l.PhaseW[1] + l.PhaseW[2], + BatW: l.Devices["inverter"].BatteryW, Bat: l.Devices["inverter"], Bat2: l.Devices["inverter2"]} for _, id := range lpIDs { if dev, ok := l.Devices[id]; ok { s.CarW = append(s.CarW, dev.W) diff --git a/go/test/site/scenarios_test.go b/go/test/site/scenarios_test.go index 4c42c9c68..1c1caafc7 100644 --- a/go/test/site/scenarios_test.go +++ b/go/test/site/scenarios_test.go @@ -8,6 +8,7 @@ import ( "math" "os" "path/filepath" + "strings" "time" ) @@ -94,6 +95,11 @@ var scenarios = []scenario{ run: func(c *runCtx) (map[string]any, error) { return s15p(c, 25) }, judge: judgeS15p}, {name: "s14-25", dir: "s14-one-phase-car-25A", minutes: 22, run: func(c *runCtx) (map[string]any, error) { return s14(c, 25) }, judge: judgeS14}, + {name: "s16-25", dir: "s16-import-ceiling-idle-start-25A", minutes: 68, + run: func(c *runCtx) (map[string]any, error) { return s16(c, 25) }, judge: judgeS16}, + {name: "s17-25", dir: "s17-two-equal-batteries-25A", minutes: 30, + run: func(c *runCtx) (map[string]any, error) { return s17(c, 25) }, judge: judgeS17, + tolerate: map[string]string{"Energyplan's plan moves the batteries half the time": "the day's prices: within the hour's wait the plan may keep both batteries idle for later, dearer hours"}}, } // runCtx is where one scenario runs. @@ -1024,3 +1030,238 @@ func judgeS15bat(d *runData) (metrics, []criterion) { crit("over fuse - 1 A at most 3 s", longest <= 3), } } + +// ---- 16. An idle car under the hour's import ceiling ---- + +// localHour is the local clock hour that holds t, the hour Core's import +// ceiling bills. +func localHour(t time.Time) time.Time { + return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()) +} + +// s16 is the help report of 25 Oct 2026 (#1828): a 4.5 kW ceiling on each +// clock hour's mean import, a house of 1.3 kW and Charge now on a car whose +// minimum is 4 140 W. House and minimum are above the ceiling, so the car +// can start only on the room the hour has saved, and the next hour stops it. +// Core counts the part of an hour before its first reading at the ceiling, +// so the run starts in an hour's first minutes or waits for :56 and runs +// one whole clock hour after it. The battery is held idle at its floor so +// that only the house and the car import. +func s16(c *runCtx, fuse float64) (map[string]any, error) { + const ceilingW = 4500 + if m := time.Now().Minute(); m >= 4 && m < 56 { + sleepS(time.Until(localHour(time.Now()).Add(56 * time.Minute)).Seconds()) + } + var hour time.Time + idle := map[string]any{"direction": "idle", "power_w": 0, "hold_s": 1800} + err := with(c, stackSpec{fuseA: fuse, loadsW: [3]float64{433, 433, 434}, socPct: 5}, func(st *stack) error { + if err := st.post("/api/battery/manual_hold", idle, nil); err != nil { + return err + } + if err := st.post("/api/peak_import_ceiling", map[string]any{"peak_import_ceiling_w": ceilingW}, nil); err != nil { + return err + } + if err := st.plug(0); err != nil { + return err + } + st.mark("charge_now") + if err := st.chargeNow("garage", 16, "3p", 0); err != nil { + return err + } + if hour = localHour(time.Now()); time.Now().Minute() >= 30 { + hour = hour.Add(time.Hour) + } + end := hour.Add(63 * time.Minute) + for left := time.Until(end); left > 0; left = time.Until(end) { + sleepS(math.Min(left.Seconds(), 1200)) + if err := st.post("/api/battery/manual_hold", idle, nil); err != nil { + return err + } + } + st.mark("end") + return nil + }) + return map[string]any{"scenario": "s16", "fuse": fuse, "house_w": 1300, "ceiling_w": ceilingW, + "hour": float64(hour.Unix())}, err +} + +func judgeS16(d *runData) (metrics, []criterion) { + ceiling, h1, t1 := d.metaF("ceiling_w"), d.metaF("hour"), d.marks["end"] + h2 := h1 + 3600 + const minW = 4140 + // Each clock hour's import through the fuses, over the part the house + // recorded. Core counts the rest at the ceiling, so the recorded part + // must hold it too. + wh, s := map[float64]float64{}, map[float64]float64{} + w := d.window(d.marks["charge_now"], t1) + for i := 1; i < len(w); i++ { + dt := w[i].T - w[i-1].T + k := h1 + 3600*math.Floor((w[i].T-h1)/3600) + wh[k] += math.Max(0, w[i].SiteW) * dt / 3600 + s[k] += dt + } + means, worst := map[string]float64{}, 0.0 + for k, sec := range s { + if sec < 60 { + continue + } + mean := wh[k] / sec * 3600 + means[time.Unix(int64(k), 0).Format("15:04")] = math.Round(mean) + worst = math.Max(worst, mean) + } + start := d.firstT(h1, func(r *sample) bool { return carW(r, 0) >= 0.9*minW }) + stop := d.firstT(h2, func(r *sample) bool { return carW(r, 0) < 0.1*minW }) + // While the ceiling holds the idle car, from a minute into the hour to + // its start: the reason it gives. + heldEnd := h2 + if start != nil { + heldEnd = h1 + *start - 5 + } + held, says, fuseSays := 0, 0, 0 + for _, r := range d.window(h1+60, heldEnd) { + if r.LPs == nil { + continue + } + lp := firstLP(r) + held++ + switch { + case lp.CommandedReason == "import_limit" || lp.Manual.LimitReason == "import_limit": + says++ + case lp.CommandedReason == "fuse_limit" || lp.Manual.LimitReason == "fuse_limit": + fuseSays++ + } + } + m := metrics{"hour_means_w": means, "worst_hour_mean_w": math.Round(worst), "car_start_s_into_hour": roundp(start, 0), + "car_stop_s_after_hour": roundp(stop, 0), "held_samples": held, "import_limit_samples": says, + "fuse_limit_samples": fuseSays, "max_abs_i": round(maxOf(w, maxAbsI), 1)} + return m, []criterion{ + crit("the car starts on the hour's room before :45", start != nil && *start < 45*60), + crit("every hour's import holds the ceiling", worst <= ceiling*1.01), + crit("the next hour stops the car within 60 s", le(stop, 60)), + crit("while held the car says import_limit, never fuse_limit", held > 0 && says >= held*9/10 && fuseSays == 0), + crit("fuse holds", !anyBlown(w)), + } +} + +// ---- 17. Two equal batteries share the plan's total ---- + +// s17 is Rann's site (#1814): two equal batteries at 74 % and 60 % that took +// turns under the plan, one discharging while the other stood at 0 W. +// Energyplan's plan puts each quarter's discharge on one battery; Core must +// share it. Core DP plans one battery only, so this runs Energyplan, as a +// beta box does. A 3 kW house with no PV. Over 48 h the plan keeps the +// batteries for the dearest hours, often tomorrow's; over 6 h they hold +// more than the house needs, so it discharges now. The run still waits up +// to an hour for a quarter the plan discharges, then runs 15 min. It keeps +// the plan's order for each battery every 30 s in plan.jsonl. +func s17(c *runCtx, fuse float64) (map[string]any, error) { + err := with(c, stackSpec{fuseA: fuse, loadsW: [3]float64{1000, 1000, 1000}, socPct: 74, soc2Pct: 60, + engine: "energyplan", horizonH: 6}, func(st *stack) error { + f, err := os.Create(filepath.Join(st.rundir, "plan.jsonl")) + if err != nil { + return err + } + defer f.Close() + // moving logs the plan's order for each battery now and says + // whether it moves them. + moving := func() bool { + var p struct { + Plan struct { + Actions []struct { + SlotStartMs int64 `json:"slot_start_ms"` + SlotLenMin float64 `json:"slot_len_min"` + StoragePowerW map[string]float64 `json:"storage_power_w"` + } `json:"actions"` + } `json:"plan"` + } + if st.get("/api/mpc/plan", &p) != nil { + return false + } + ms := time.Now().UnixMilli() + for _, a := range p.Plan.Actions { + if a.SlotStartMs <= ms && ms < a.SlotStartMs+int64(a.SlotLenMin*60000) { + b, _ := json.Marshal(map[string]any{"t": now(), "storage_power_w": a.StoragePowerW}) + f.Write(append(b, '\n')) + return planMoves(a.StoragePowerW) + } + } + return false + } + sleepS(60) + for wait := time.Now().Add(time.Hour); !moving() && time.Now().Before(wait); { + sleepS(30) + } + st.mark("start") + for end := time.Now().Add(900 * time.Second); time.Now().Before(end); { + sleepS(30) + moving() + } + st.mark("end") + return nil + }) + return map[string]any{"scenario": "s17", "fuse": fuse, "house_w": 3000, "soc_pct": []float64{74, 60}, "horizon_h": 6}, err +} + +// planMoves is true when a plan's orders move the batteries 300 W or more. +func planMoves(w map[string]float64) bool { + sum := 0.0 + for _, v := range w { + sum += v + } + return math.Abs(sum) >= 300 +} + +func judgeS17(d *runData) (metrics, []criterion) { + t0, t1 := d.marks["start"], d.marks["end"] + w := d.window(t0, t1) + moving, shared := 0, 0 + for _, r := range w { + a, b := r.Bat.BatteryW, r.Bat2.BatteryW + fleet := a + b + if math.Abs(fleet) < 500 { + continue + } + moving++ + // Each takes at least a quarter of the fleet, the same way. + if a/fleet >= 0.25 && b/fleet >= 0.25 { + shared++ + } + } + // The plan's order for each battery over the judged window. + polls, planned := 0, 0 + readLines(filepath.Join(d.dir, "plan.jsonl"), func(b []byte) { + var l struct { + T float64 `json:"t"` + StoragePowerW map[string]float64 `json:"storage_power_w"` + } + if json.Unmarshal(b, &l) != nil || l.T < t0-1 || l.T > t1 { + return + } + polls++ + if planMoves(l.StoragePowerW) { + planned++ + } + }) + energyplan := false + if b, err := os.ReadFile(filepath.Join(d.dir, "ftw.log")); err == nil { + energyplan = strings.Contains(string(b), "Energyplan primary") + } + exercised := energyplan && polls > 0 && planned*2 >= polls + gap := func(r *sample) float64 { return r.Bat.SoCPct - r.Bat2.SoCPct } + var gap0, gap1, wait float64 + if len(w) > 0 { + gap0, gap1, wait = gap(w[0]), gap(w[len(w)-1]), t0-d.samples[0].T + } + m := metrics{"moving_samples": moving, "shared_samples": shared, "samples": len(w), + "plan_polls": polls, "plan_moving_polls": planned, "energyplan": energyplan, + "wait_s": round(wait, 0), "soc_gap_start": round(gap0, 2), "soc_gap_end": round(gap1, 2), + "bat_w_mean": []float64{math.Round(meanOf(w, func(r *sample) float64 { return r.Bat.BatteryW })), + math.Round(meanOf(w, func(r *sample) float64 { return r.Bat2.BatteryW }))}} + return m, []criterion{ + crit("Energyplan's plan moves the batteries half the time", exercised), // the setup + crit("each battery takes a quarter of the fleet's power 90 % of the time", moving > 0 && shared*10 >= moving*9), + // Shared by the energy each can still give, the fuller one gives more. + crit("the charge gap shrinks while the plan moves them", !exercised || gap1 < gap0-0.2), + crit("fuse holds", !anyBlown(w)), + } +} diff --git a/go/test/site/stack_test.go b/go/test/site/stack_test.go index aa4b156bc..e45b6fc54 100644 --- a/go/test/site/stack_test.go +++ b/go/test/site/stack_test.go @@ -265,6 +265,14 @@ type stackSpec struct { // batteryDelayS is how long the battery takes to follow an EMS write; // 0 leaves the model's (2 s for sungrow). batteryDelayS float64 + // soc2Pct adds a second hybrid with an equal 10 kWh battery at this + // charge; 0 leaves one. + soc2Pct float64 + // engine is the planner's: core or energyplan. Empty takes + // FTW_SITE_ENGINE, else core. + engine string + // horizonH is the planner's horizon_hours; 0 leaves Core's 48 h. + horizonH int } type stack struct { @@ -315,6 +323,11 @@ func (st *stack) houseSpec() houseSpec { if st.spec.batteryDelayS > 0 { h.Devices[0].CommandResponseS = f64(st.spec.batteryDelayS) } + if st.spec.soc2Pct > 0 { + second := h.Devices[0] + second.ID, second.SoCPct = "inverter2", f64(st.spec.soc2Pct) + h.Devices = append(h.Devices, second) + } for i, c := range st.spec.chargers { var delay *float64 if st.spec.random == nil { @@ -364,6 +377,17 @@ func (st *stack) up() error { if st.cfg, err = st.ftwConfig(); err != nil { return err } + if st.engine() == "energyplan" { + // FTW looks for the worker beside its binary or under its working + // directory; a box has it in its image. + bundle := filepath.Join(st.rundir, "optimizer", "native") + if err := os.MkdirAll(bundle, 0o755); err != nil { + return err + } + if err := os.Symlink(filepath.Join(st.env.root, "optimizer", "native", "bundle"), filepath.Join(bundle, "bundle")); err != nil { + return err + } + } if err := st.startFTW(); err != nil { return err } @@ -484,6 +508,17 @@ func (r *recorder) run() { var lpIDs = []string{"garage", "carport"} +// engine is the planner this run uses. +func (st *stack) engine() string { + if st.spec.engine != "" { + return st.spec.engine + } + if e := os.Getenv("FTW_SITE_ENGINE"); e != "" { + return e + } + return "core" +} + func (st *stack) ftwConfig() (string, error) { var lps []map[string]any for i := range st.spec.chargers { @@ -525,9 +560,16 @@ func (st *stack) ftwConfig() (string, error) { // A real box plans. A box without a planner runs Self, where the // battery chases the raw meter and covers the car (dispatch.go), // which hides the stress on the fuse. - "planner": map[string]any{"enabled": true, "engine": "core", "mode": "self_consumption"}, + "planner": map[string]any{"enabled": true, "engine": st.engine(), "mode": "self_consumption"}, "price": map[string]any{"provider": "elprisetjustnu", "zone": "SE3"}, } + if st.spec.horizonH > 0 { + cfg["planner"].(map[string]any)["horizon_hours"] = st.spec.horizonH + } + if st.spec.soc2Pct > 0 { + cfg["drivers"] = append(cfg["drivers"].([]map[string]any), + map[string]any{"name": "sungrow2", "lua": "drivers/sungrow.lua", "battery_capacity_wh": 10000, "capabilities": modbus("inverter2")}) + } path := filepath.Join(st.rundir, "config.yaml") b, err := json.MarshalIndent(cfg, "", " ") // JSON is valid YAML if err != nil {