From 7dc2639a7fcdf51ef789f4af2e3afff11438be6e Mon Sep 17 00:00:00 2001 From: Dante Niewenhuis Date: Fri, 14 Mar 2025 15:33:42 +0100 Subject: Added more battery policies (#312) * some updates * Updates * Added comments and renamed variables * Ran Spotless --- .../opendc/compute/topology/TopologyFactories.kt | 6 +- .../opendc/compute/topology/specs/ClusterSpec.kt | 2 +- .../opendc/compute/topology/specs/TopologySpecs.kt | 94 +++++++++++++++++++++- 3 files changed, 96 insertions(+), 6 deletions(-) (limited to 'opendc-compute/opendc-compute-topology') diff --git a/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/TopologyFactories.kt b/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/TopologyFactories.kt index b62c457c..a20bc2c2 100644 --- a/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/TopologyFactories.kt +++ b/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/TopologyFactories.kt @@ -24,7 +24,7 @@ package org.opendc.compute.topology -import org.opendc.compute.topology.specs.BatterySpec +import org.opendc.compute.topology.specs.BatteryJSONSpec import org.opendc.compute.topology.specs.ClusterJSONSpec import org.opendc.compute.topology.specs.ClusterSpec import org.opendc.compute.topology.specs.HostJSONSpec @@ -134,10 +134,10 @@ private fun ClusterJSONSpec.toClusterSpec(): ClusterSpec { carbonTracePath = this.powerSource.carbonTracePath, ) - var batterySpec: BatterySpec? = null + var batterySpec: BatteryJSONSpec? = null if (this.battery != null) { batterySpec = - BatterySpec( + BatteryJSONSpec( createUniqueName(this.battery.name, batteryNames), this.battery.capacity, this.battery.chargingSpeed, diff --git a/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/ClusterSpec.kt b/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/ClusterSpec.kt index 6e7c8dfa..97e12637 100644 --- a/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/ClusterSpec.kt +++ b/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/ClusterSpec.kt @@ -26,5 +26,5 @@ public data class ClusterSpec( val name: String, val hostSpecs: List, val powerSource: PowerSourceSpec, - val battery: BatterySpec? = null, + val battery: BatteryJSONSpec? = null, ) diff --git a/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/TopologySpecs.kt b/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/TopologySpecs.kt index 7b309bc3..3d8b63dc 100644 --- a/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/TopologySpecs.kt +++ b/opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/TopologySpecs.kt @@ -22,10 +22,19 @@ package org.opendc.compute.topology.specs +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.opendc.common.units.DataSize import org.opendc.common.units.Frequency import org.opendc.common.units.Power +import org.opendc.simulator.compute.power.batteries.BatteryAggregator +import org.opendc.simulator.compute.power.batteries.SimBattery +import org.opendc.simulator.compute.power.batteries.policy.BatteryPolicy +import org.opendc.simulator.compute.power.batteries.policy.DoubleThresholdBatteryPolicy +import org.opendc.simulator.compute.power.batteries.policy.RunningMeanBatteryPolicy +import org.opendc.simulator.compute.power.batteries.policy.RunningMeanPlusBatteryPolicy +import org.opendc.simulator.compute.power.batteries.policy.SingleThresholdBatteryPolicy +import org.opendc.simulator.engine.graph.FlowGraph /** * Definition of a Topology modeled in the simulation. @@ -182,6 +191,87 @@ public data class BatteryJSONSpec( ) @Serializable -public data class BatteryPolicyJSONSpec( +public sealed interface BatteryPolicyJSONSpec + +@Serializable +@SerialName("single") +public data class SingleBatteryPolicyJSONSpec( val carbonThreshold: Double, -) +) : BatteryPolicyJSONSpec + +@Serializable +@SerialName("double") +public data class DoubleBatteryPolicyJSONSpec( + val lowerThreshold: Double, + val upperThreshold: Double, +) : BatteryPolicyJSONSpec + +@Serializable +@SerialName("runningMean") +public data class RunningMeanPolicyJSONSpec( + val startingThreshold: Double, + val windowSize: Int, +) : BatteryPolicyJSONSpec + +@Serializable +@SerialName("runningMeanPlus") +public data class RunningMeanPlusPolicyJSONSpec( + val startingThreshold: Double, + val windowSize: Int, +) : BatteryPolicyJSONSpec + +@Serializable +@SerialName("runningMedian") +public data class RunningMedianPolicyJSONSpec( + val startingThreshold: Double, + val windowSize: Int, +) : BatteryPolicyJSONSpec + +@Serializable +@SerialName("runningQuartiles") +public data class RunningQuartilesPolicyJSONSpec( + val startingThreshold: Double, + val windowSize: Int, +) : BatteryPolicyJSONSpec + +public fun createSimBatteryPolicy( + batterySpec: BatteryPolicyJSONSpec, + graph: FlowGraph, + battery: SimBattery, + batteryAggregator: BatteryAggregator, +): BatteryPolicy { + return when (batterySpec) { + is SingleBatteryPolicyJSONSpec -> + SingleThresholdBatteryPolicy( + graph, + battery, + batteryAggregator, + batterySpec.carbonThreshold, + ) + is DoubleBatteryPolicyJSONSpec -> + DoubleThresholdBatteryPolicy( + graph, + battery, + batteryAggregator, + batterySpec.lowerThreshold, + batterySpec.upperThreshold, + ) + is RunningMeanPolicyJSONSpec -> + RunningMeanBatteryPolicy( + graph, + battery, + batteryAggregator, + batterySpec.startingThreshold, + batterySpec.windowSize, + ) + is RunningMeanPlusPolicyJSONSpec -> + RunningMeanPlusBatteryPolicy( + graph, + battery, + batteryAggregator, + batterySpec.startingThreshold, + batterySpec.windowSize, + ) + else -> throw IllegalArgumentException("Unknown battery policy") + } +} -- cgit v1.2.3