summaryrefslogtreecommitdiff
path: root/opendc-compute/opendc-compute-topology/src
diff options
context:
space:
mode:
authorDante Niewenhuis <d.niewenhuis@hotmail.com>2025-03-14 15:33:42 +0100
committerGitHub <noreply@github.com>2025-03-14 15:33:42 +0100
commit7dc2639a7fcdf51ef789f4af2e3afff11438be6e (patch)
tree9a67f432f1aa31e57b20471b6cca61e01ccdea70 /opendc-compute/opendc-compute-topology/src
parent5ec41d49b497010783d25bf13bc042d3e76824e3 (diff)
Added more battery policies (#312)
* some updates * Updates * Added comments and renamed variables * Ran Spotless
Diffstat (limited to 'opendc-compute/opendc-compute-topology/src')
-rw-r--r--opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/TopologyFactories.kt6
-rw-r--r--opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/ClusterSpec.kt2
-rw-r--r--opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/TopologySpecs.kt94
3 files changed, 96 insertions, 6 deletions
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<HostSpec>,
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")
+ }
+}