Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/fleet-keeps-the-worker-plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"ftw": patch
---

A site Core cannot plan itself, such as one with two home batteries, again
gets Energyplan's plan when the battery covers the house while a car the
battery may not cover charges under the fuse. Since the car-room fix, Core
rejected such a plan and kept the previous one. The plan now goes out with a
warning. The charger still leaves the battery's discharge out of the car's
room, so battery energy does not reach the car.
215 changes: 215 additions & 0 deletions go/internal/mpc/car_room_overrun_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package mpc

import (
"context"
"errors"
"strings"
"testing"
"time"

"github.com/srcfl/ftw/go/internal/loadpoint"
"github.com/srcfl/ftw/go/internal/state"
"github.com/srcfl/ftw/go/internal/telemetry"
)

// carRoomSlot is one hour of the field night (fuseNightParams): a 3,800 W
// house under a 9,250 W limit, the battery at batteryW and the car at carW,
// pulsing at peakW when peakW is set.
func carRoomSlot(batteryW, carW, peakW float64) ([]Slot, Params, Plan) {
slots := []Slot{{StartMs: 1, LenMin: 60, PriceOre: 15, SpotOre: 15, LoadW: 3800,
Limits: PowerLimits{MaxImportW: fuseNightImportW}}}
p := fuseNightParams(ModeArbitrage, true)
p.InitialSoC = .5
p.Loadpoint.TargetSlotIdx = 0
a := Action{SlotStartMs: 1, SlotLenMin: 60, LoadW: 3800, PriceOre: 15, SpotOre: 15,
BatteryW: batteryW, GridW: 3800 + carW + batteryW,
SoC: .5 + loadpoint.BatteryEnergyDeltaWh(batteryW, 1, .95, .95)/p.CapacityWh,
LoadpointW: carW, LoadpointSoC: .3 + carW*.9/84000}
if peakW > 0 {
a.LoadpointPowerW = map[string]float64{"car": carW}
a.LoadpointMaxPowerW = map[string]float64{"car": peakW}
a.LoadpointSoCByID = map[string]float64{"car": a.LoadpointSoC}
}
a.CostOre = reserveSlotCost(slots[0], p, a)
return slots, p, Plan{Mode: p.Mode, HorizonSlots: 1, CapacityWh: p.CapacityWh, InitialSoC: p.InitialSoC,
Actions: []Action{a}, TotalCostOre: a.CostOre}
}

// A plan whose only broken rule is the car's import room is an overrun,
// which Core may publish when it cannot plan the site itself. A battery
// that discharges past the house still feeds the car, which no site allows.
func TestValidatePlanNamesTheCarRoomOverrun(t *testing.T) {
for _, tc := range []struct {
name string
batteryW, carW, peakW float64
overrun bool
}{
// The house uses all 3,000 W; with the battery idle the car could
// have had only 5,450 W.
{"steady car", -3000, 8280, 0, true},
// The mean fits beside the house; the pulse does not.
{"pulsing car", -3000, 4140, 8280, true},
{"battery past the house", -5000, 8280, 0, false},
} {
t.Run(tc.name, func(t *testing.T) {
slots, p, plan := carRoomSlot(tc.batteryW, tc.carW, tc.peakW)
err := ValidatePlan(slots, p, &plan)
var overrun *planOverrun
if err == nil || errors.As(err, &overrun) != tc.overrun {
t.Fatalf("err %v, overrun %v; want overrun %v", err, overrun != nil, tc.overrun)
}
if tc.overrun && (overrun.carRoom == nil || overrun.hours != nil || len(overrun.plan.Actions) != 1) {
t.Fatalf("overrun %+v", overrun)
}
})
}
}

// carRoomOptimizer plans as the fleet worker does on the field night: in
// the first slot where the house draws, the battery covers the house and
// the car charges on the room that frees. Every other rule holds. A
// downside worker plans the caution forecast first, as Energyplan does.
type carRoomOptimizer struct{ downside bool }

func (o carRoomOptimizer) UsesDownsidePV() bool { return o.downside }

func (carRoomOptimizer) Optimize(_ context.Context, slots []Slot, p Params) (Plan, error) {
lp := p.activeLoadpoints()[0]
plan := Plan{Mode: p.Mode, HorizonSlots: len(slots), CapacityWh: p.CapacityWh, InitialSoC: p.InitialSoC,
Solver: &SolverInfo{Engine: "native", Backend: "car-room-test", Status: "feasible"}}
energy := make(map[string]float64, len(p.Storages))
for _, s := range p.Storages {
energy[s.ID] = s.InitialEnergyWh
}
soc, carSoC, placed := p.InitialSoC, lp.InitialSoC, false
for _, slot := range slots {
a := Action{SlotStartMs: slot.StartMs, SlotLenMin: slot.LenMin, ExecutionStartMs: slot.ExecutionStartMs,
PriceOre: slot.PriceOre, SpotOre: slot.SpotOre, LoadW: slot.LoadW, PVW: slot.PVW}
house, limit := slot.LoadW+slot.PVW, slot.Limits.MaxImportW
if !placed && house > 100 && limit > 0 {
for _, step := range lp.normalizedSteps() {
if house+step > limit+50 && step <= limit-50 {
a.BatteryW, a.LoadpointW, placed = -house, step, true
break
}
}
}
dtH := slot.DurationHours()
soc += loadpoint.BatteryEnergyDeltaWh(a.BatteryW, dtH, p.ChargeEfficiency, p.DischargeEfficiency) / p.CapacityWh
if len(p.Storages) > 0 {
a.StoragePowerW = map[string]float64{}
a.StorageEnergyWh = map[string]float64{}
total := 0.0
for i, s := range p.Storages {
w := 0.0
if i == 0 {
w = a.BatteryW
}
energy[s.ID] += loadpoint.BatteryEnergyDeltaWh(w, dtH, s.ChargeEfficiency, s.DischargeEfficiency)
a.StoragePowerW[s.ID], a.StorageEnergyWh[s.ID] = w, energy[s.ID]
total += energy[s.ID]
}
soc = total / p.CapacityWh
}
carSoC += a.LoadpointW * dtH * lp.ChargeEfficiency / lp.CapacityWh
a.SoC, a.LoadpointSoC = soc, carSoC
a.GridW = loadpoint.GridW(slot.LoadW, slot.PVW, a.BatteryW, a.LoadpointW)
a.CostOre = reserveSlotCost(slot, p, a)
plan.TotalCostOre += a.CostOre
plan.Actions = append(plan.Actions, a)
}
if !placed {
return Plan{}, errors.New("test site has no slot where the battery makes the car's room")
}
if err := ValidatePlan(slots, p, &plan); err != nil {
return Plan{}, errors.Join(errors.New("optimizer plan rejected"), err)
}
return plan, nil
}

func (carRoomOptimizer) Close() error { return nil }

// carRoomFieldService is the import-ceiling field site on a 9,250 W fuse,
// with a car the battery may not cover and storages home batteries.
func carRoomFieldService(t *testing.T, optimizer PlanOptimizer, storages int) *Service {
t.Helper()
svc, _ := importCeilingFieldService(t, optimizer, false)
svc.UpdateSiteEconomics(SiteEconomics{FuseMaxW: fuseNightImportW, Timezone: "Europe/Stockholm"})
car := svc.Loadpoint
svc.Loadpoint = func(i int) *LoadpointSpec {
lp := car(i)
lp.NoBatteryToEV = true
return lp
}
if storages == 2 {
soc := .5
svc.Tele.Update("pixii", telemetry.DerBattery, 0, &soc, nil)
svc.Tele.DriverHealthMut("pixii").RecordSuccess()
svc.UpdateBatteryFleet([]BatteryFleetMember{
{Driver: "sungrow", CapacityWh: 9600, MaxChargeW: 5000, MaxDischargeW: 5000},
{Driver: "pixii", CapacityWh: 9600, MaxChargeW: 5000, MaxDischargeW: 5000},
}, 19200, 10000, 10000)
}
return svc
}

// A worker plan in which the battery makes the car's import room falls back
// to Core's own plan. With two batteries Core cannot plan the site itself:
// the worker's plan is published with a warning rather than no new plan,
// and the charger's live room still leaves the battery's discharge out.
func TestCarRoomWorkerPlanWhenCoreCannotPlan(t *testing.T) {
for _, storages := range []int{1, 2} {
svc := carRoomFieldService(t, carRoomOptimizer{}, storages)
if svc.Replan(context.Background()) == nil {
t.Fatalf("%d storages: no plan published: %+v", storages, svc.PlanSnapshot())
}
snap := svc.PlanSnapshot()
warned := strings.Contains(strings.Join(snap.Warnings, " "), "lets the battery make room for the car")
if storages == 1 {
if !snap.Plan.Solver.Fallback || warned {
t.Fatalf("1 storage: solver %+v, warnings %q; want Core's own plan", snap.Plan.Solver, snap.Warnings)
}
continue
}
if snap.Plan.Solver.Fallback || snap.Plan.Solver.Backend != "car-room-test" || !warned {
t.Fatalf("2 storages: solver %+v, warnings %q; want the worker's plan with a warning", snap.Plan.Solver, snap.Warnings)
}
}
}

// A worker that plans the caution forecast first returns only its point
// plan's error once caution fails. Core publishes that point plan, so the
// plan reads as one without the extra forecast margin.
func TestCarRoomPointPlanIsWithoutCaution(t *testing.T) {
svc := carRoomFieldService(t, carRoomOptimizer{downside: true}, 2)
svc.PVForecastSafetyK = .3
svc.ForecastSnapshot = func(time.Time, []state.ForecastPoint) ForecastInputs {
return ForecastInputs{Load: func(time.Time) float64 { return 800 },
Risk: func(_, risk []Slot, _ float64) {
for i := range risk {
risk[i].LoadW = 900
}
}}
}
plan := svc.Replan(context.Background())
if plan == nil || plan.Solver == nil || plan.Solver.Backend != "car-room-test" {
t.Fatalf("no worker plan published: %+v", svc.PlanSnapshot())
}
if !plan.CautionOff || !strings.Contains(strings.Join(svc.PlanSnapshot().Warnings, " "), "caution") {
t.Fatalf("caution off = %v, warnings %q; want the point plan marked without caution", plan.CautionOff, svc.PlanSnapshot().Warnings)
}
}

// The bundled fleet worker plans a two-battery site with the field night's
// house and fuse. Core cannot plan the site itself, so it publishes the
// worker's plan rather than no new plan.
func TestNativeFleetCarRoomWhenCoreCannotPlan(t *testing.T) {
worker := nativeWorker(t, 1500*time.Millisecond)
t.Cleanup(func() { _ = worker.Close() })
svc := carRoomFieldService(t, worker, 2)
svc.BaseLoad = 3800
if svc.Replan(context.Background()) == nil {
t.Fatalf("no plan published: %+v", svc.PlanSnapshot())
}
t.Logf("solver %+v, warnings %q", svc.PlanSnapshot().Plan.Solver, svc.PlanSnapshot().Warnings)
}
18 changes: 15 additions & 3 deletions go/internal/mpc/ev_reserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mpc

import (
"context"
"errors"
"fmt"
"math"
"slices"
Expand Down Expand Up @@ -278,6 +279,7 @@ func validateEVPulse(slot Slot, p Params, a Action, pvW float64, vehicleRules bo
return fmt.Errorf("EV pulse must retain every loadpoint")
}
minEV, maxEV, solarPeak := 0.0, 0.0, 0.0
var carRoom error
for i, lp := range loads {
peak, ok := a.LoadpointMaxPowerW[lp.ID]
average, hasAverage := a.LoadpointPowerW[lp.ID]
Expand All @@ -303,8 +305,13 @@ func validateEVPulse(slot Slot, p Params, a Action, pvW float64, vehicleRules bo
if lp.SurplusOnly {
solarPeak += peak
}
if vehicleRules && peak > 0 && lp.blocksBatteryToEV() && loadpoint.BatteryDischargeFeedsEVUnderImport(a.BatteryW, peak, slot.LoadW, pvW, slot.Limits.MaxImportW) {
return fmt.Errorf("battery discharge feeds EV pulse")
if vehicleRules && peak > 0 && lp.blocksBatteryToEV() {
if loadpoint.BatteryDischargeFeedsEV(a.BatteryW, peak, slot.LoadW, pvW) {
return fmt.Errorf("battery discharge feeds EV pulse")
}
if loadpoint.BatteryDischargeFeedsEVUnderImport(a.BatteryW, peak, slot.LoadW, pvW, slot.Limits.MaxImportW) {
carRoom = errPulseCarRoom
}
}
}
if surplusOnlyExceedsHousePV(solarPeak, slot.LoadW, pvW) {
Expand All @@ -323,5 +330,10 @@ func validateEVPulse(slot Slot, p Params, a Action, pvW float64, vehicleRules bo
return fmt.Errorf("EV pulse charges during battery export")
}
}
return nil
return carRoom
}

// errPulseCarRoom is an EV pulse that keeps every rule but one: the battery
// covers only the house, yet the pulse fits the import limit only beside
// that discharge. validatePlan counts it as a planOverrun.
var errPulseCarRoom = errors.New("battery discharge makes the import room for an EV pulse")
20 changes: 14 additions & 6 deletions go/internal/mpc/external_optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ func validatePlan(slots []Slot, p Params, plan *Plan, vehicleRules bool) error {
for _, lp := range activeLoadpoints {
evSoC[lp.ID] = lp.InitialSoC
}
// The first slot where the battery makes a car's import room. The plan
// is still checked against every other rule (planOverrun).
var carRoom error
totalCost := 0.0
for i, slot := range slots {
a := plan.Actions[i]
Expand Down Expand Up @@ -673,9 +676,9 @@ func validatePlan(slots []Slot, p Params, plan *Plan, vehicleRules bool) error {
}
// The cars together must fit the import limit beside the house
// with the battery idle, or the discharge made their room.
if vehicleRules && lp.blocksBatteryToEV() && powerW > solverZeroPowerToleranceW &&
if vehicleRules && carRoom == nil && lp.blocksBatteryToEV() && powerW > solverZeroPowerToleranceW &&
loadpoint.BatteryDischargeFeedsEVUnderImport(a.BatteryW, totalLoadpointW, slot.LoadW, effectivePVW, slot.Limits.MaxImportW) {
return fmt.Errorf("slot %d battery discharge makes the import room for loadpoint %s", i, lp.ID)
carRoom = fmt.Errorf("slot %d battery discharge makes the import room for loadpoint %s", i, lp.ID)
}
}
if surplusOnlyExceedsHousePV(totalSurplusOnlyW, slot.LoadW, effectivePVW) {
Expand All @@ -690,7 +693,11 @@ func validatePlan(slots []Slot, p Params, plan *Plan, vehicleRules bool) error {
(slot.Limits.MaxExportW > 0 && a.GridW < -slot.Limits.MaxExportW-solverGridLimitToleranceW) {
return fmt.Errorf("slot %d grid_w %.3f violates grid limits", i, a.GridW)
}
if err := validateEVPulse(slot, p, a, effectivePVW, vehicleRules); err != nil {
if err := validateEVPulse(slot, p, a, effectivePVW, vehicleRules); errors.Is(err, errPulseCarRoom) {
if carRoom == nil {
carRoom = fmt.Errorf("slot %d: %w", i, err)
}
} else if err != nil {
return fmt.Errorf("slot %d: %w", i, err)
}
wantCost := reserveSlotCost(slot, p, a)
Expand All @@ -704,9 +711,10 @@ func validatePlan(slots []Slot, p Params, plan *Plan, vehicleRules bool) error {
if math.Abs(plan.TotalCostOre-totalCost) > 0.1 {
return fmt.Errorf("total cost %.4f, want %.4f", plan.TotalCostOre, totalCost)
}
// Last, so an overrun hour is the only rule the plan breaks.
if err := validateImportHours(slots, p, plan.Actions); err != nil {
return &importHourOverrun{plan: *plan, err: err}
// Last, so an overrun hour and the car's room are the only rules the
// plan breaks.
if hours := validateImportHours(slots, p, plan.Actions); hours != nil || carRoom != nil {
return &planOverrun{plan: *plan, hours: hours, carRoom: carRoom}
}
return nil
}
32 changes: 25 additions & 7 deletions go/internal/mpc/import_hours.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mpc

import (
"errors"
"fmt"
"math"
"time"
Expand Down Expand Up @@ -151,15 +152,32 @@ func evAverageRoomW(capW, baseW, peakW float64) float64 {
return max(0, min(peakW, (capW-off)*peakW/(on-off)))
}

// importHourOverrun is a plan that keeps every rule but a clock hour's
// import. Core plans the site itself instead; when it cannot, it publishes
// this plan, and the live limit still holds each hour.
type importHourOverrun struct {
plan Plan
err error
// planOverrun is a plan that keeps every rule but ones live control holds
// by itself: a clock hour's import (hours), which the live limit holds, and
// a battery that makes a car's import room (carRoom), which the charger's
// room leaves out (control.evBatteryDischargeW). Core plans the site itself
// instead; when it cannot, it publishes this plan.
type planOverrun struct {
plan Plan
hours, carRoom error
}

func (e *importHourOverrun) Error() string { return e.err.Error() }
func (e *planOverrun) Error() string {
switch {
case e.carRoom == nil:
return e.hours.Error()
case e.hours == nil:
return e.carRoom.Error()
}
return e.carRoom.Error() + "; " + e.hours.Error()
}

// onlyCarRoom reports whether err is a planOverrun that breaks only the
// car's room.
func onlyCarRoom(err error) bool {
var overrun *planOverrun
return errors.As(err, &overrun) && overrun.hours == nil && overrun.carRoom != nil
}

func actionChargesCar(a Action) bool {
if a.LoadpointW > solverZeroPowerToleranceW {
Expand Down
Loading
Loading