from app.plant import Plant # 海斯特站规模:单泵铭牌 Q≈850 m³/h @50Hz,5 台(三用二备) def test_optimize_meets_target_and_saves_energy(): plant = Plant() Q_target = 700.0 res = plant.optimizer.optimize(Q_target=Q_target) assert res["success"] # 变频泵可 trim 到接近目标(软约束) assert abs(res["wp"]["Q_total"] - Q_target) < 150 # 相对全泵 50Hz 基线应节能 base = plant.baseline_all_on_50hz(Q_target) assert res["spec_energy"] <= base.spec_energy + 1e-6 def test_freqs_within_bounds(): plant = Plant() res = plant.optimizer.optimize(Q_target=1200.0) for f in res["freqs"].values(): assert 20.0 - 1e-6 <= f <= 50.0 + 1e-6 def test_start_stop_penalty_prefers_continuity(): plant = Plant() # 当前 1、2 号在运,需求恰好 2 泵满足,应保持不乱切 res = plant.optimizer.optimize( Q_target=1350.0, current_active=[1, 2], current_freqs={1: 50.0, 2: 50.0}, ) assert res["success"] # 真实厂家曲线下1号泵较小;当前1+2无法达到1350,允许切换为可行组合。 assert abs(res["wp"]["Q_total"] - 1350.0) <= max(50.0, 1350.0 * 0.03) assert res["wp"]["feasible"] def test_rotation_prefers_least_run_hours(): plant = Plant() # 低需求单泵场景:变频泵(1/2/5)中累计运行 h 最少的 5# 应被优先 res = plant.optimizer.optimize( Q_target=700.0, run_hours={1: 9000.0, 2: 9000.0, 3: 9000.0, 4: 9000.0, 5: 100.0}, ) assert res["success"] if len(res["active"]) == 1: assert res["active"][0] == 5 def test_higher_demand_more_pumps(): plant = Plant() low = plant.optimizer.optimize(Q_target=700.0) high = plant.optimizer.optimize(Q_target=1700.0) assert high["success"] assert len(high["active"]) >= len(low["active"]) assert len(high["active"]) >= 3 # 大需求需多台 def test_infeasible_target_is_not_recommended(): plant = Plant() res = plant.optimizer.optimize(Q_target=10000.0, level=5.5) assert not res["success"] assert res["status"] == "infeasible_target"