summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-power/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-simulator/opendc-simulator-power/src/main')
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt50
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt6
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt14
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt51
4 files changed, 50 insertions, 71 deletions
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
index 3ce85d02..9f88fecc 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
@@ -22,52 +22,40 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.*
+import org.opendc.simulator.flow.*
+import org.opendc.simulator.flow.mux.FlowMultiplexer
+import org.opendc.simulator.flow.mux.MaxMinFlowMultiplexer
/**
* A model of a Power Distribution Unit (PDU).
*
- * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param engine The underlying [FlowEngine] to drive the simulation under the hood.
* @param idlePower The idle power consumption of the PDU independent of the load on the PDU.
* @param lossCoefficient The coefficient for the power loss of the PDU proportional to the square load.
*/
public class SimPdu(
- interpreter: SimResourceInterpreter,
+ engine: FlowEngine,
private val idlePower: Double = 0.0,
private val lossCoefficient: Double = 0.0,
) : SimPowerInlet() {
/**
- * The [SimResourceDistributor] that distributes the electricity over the PDU outlets.
+ * The [FlowMultiplexer] that distributes the electricity over the PDU outlets.
*/
- private val distributor = SimResourceDistributorMaxMin(interpreter)
+ private val mux = MaxMinFlowMultiplexer(engine)
/**
- * Create a new PDU outlet.
+ * The [FlowForwarder] that represents the input of the PDU.
*/
- public fun newOutlet(): Outlet = Outlet(distributor.newOutput())
-
- override fun createConsumer(): SimResourceConsumer = object : SimResourceConsumer by distributor {
- override fun onNext(ctx: SimResourceContext): SimResourceCommand {
- return when (val cmd = distributor.onNext(ctx)) {
- is SimResourceCommand.Consume -> {
- val duration = cmd.work / cmd.limit
- val loss = computePowerLoss(cmd.limit)
- val newLimit = cmd.limit + loss
+ private val output = mux.newOutput()
- SimResourceCommand.Consume(duration * newLimit, newLimit, cmd.deadline)
- }
- is SimResourceCommand.Idle -> {
- val loss = computePowerLoss(0.0)
- if (loss > 0.0)
- SimResourceCommand.Consume(Double.POSITIVE_INFINITY, loss, cmd.deadline)
- else
- cmd
- }
- else -> cmd
- }
- }
+ /**
+ * Create a new PDU outlet.
+ */
+ public fun newOutlet(): Outlet = Outlet(mux, mux.newInput())
- override fun toString(): String = "SimPdu.Consumer"
+ override fun createSource(): FlowSource = FlowMapper(output) { _, rate ->
+ val loss = computePowerLoss(rate)
+ rate + loss
}
override fun toString(): String = "SimPdu"
@@ -83,9 +71,9 @@ public class SimPdu(
/**
* A PDU outlet.
*/
- public class Outlet(private val provider: SimResourceCloseableProvider) : SimPowerOutlet(), AutoCloseable {
+ public class Outlet(private val switch: FlowMultiplexer, private val provider: FlowConsumer) : SimPowerOutlet(), AutoCloseable {
override fun onConnect(inlet: SimPowerInlet) {
- provider.startConsumer(inlet.createConsumer())
+ provider.startConsumer(inlet.createSource())
}
override fun onDisconnect(inlet: SimPowerInlet) {
@@ -96,7 +84,7 @@ public class SimPdu(
* Remove the outlet from the PDU.
*/
override fun close() {
- provider.close()
+ switch.removeInput(provider)
}
override fun toString(): String = "SimPdu.Outlet"
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
index 0ac1f199..de587b7f 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
@@ -22,7 +22,7 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.SimResourceConsumer
+import org.opendc.simulator.flow.FlowSource
/**
* An abstract inlet that consumes electricity from a power outlet.
@@ -42,7 +42,7 @@ public abstract class SimPowerInlet {
internal var _outlet: SimPowerOutlet? = null
/**
- * Create a [SimResourceConsumer] which represents the consumption of electricity from the power outlet.
+ * Create a [FlowSource] which represents the consumption of electricity from the power outlet.
*/
- public abstract fun createConsumer(): SimResourceConsumer
+ public abstract fun createSource(): FlowSource
}
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt
index 3ef8ccc6..07e9f52e 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt
@@ -22,28 +22,28 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.SimResourceInterpreter
-import org.opendc.simulator.resources.SimResourceSource
+import org.opendc.simulator.flow.FlowEngine
+import org.opendc.simulator.flow.FlowSink
/**
* A [SimPowerOutlet] that represents a source of electricity.
*
- * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param engine The underlying [FlowEngine] to drive the simulation under the hood.
*/
-public class SimPowerSource(interpreter: SimResourceInterpreter, public val capacity: Double) : SimPowerOutlet() {
+public class SimPowerSource(engine: FlowEngine, public val capacity: Double) : SimPowerOutlet() {
/**
* The resource source that drives this power source.
*/
- private val source = SimResourceSource(capacity, interpreter)
+ private val source = FlowSink(engine, capacity)
/**
* The power draw at this instant.
*/
public val powerDraw: Double
- get() = source.speed
+ get() = source.rate
override fun onConnect(inlet: SimPowerInlet) {
- source.startConsumer(inlet.createConsumer())
+ source.startConsumer(inlet.createSource())
}
override fun onDisconnect(inlet: SimPowerInlet) {
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
index f9431d21..46d659f8 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
@@ -22,63 +22,54 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.*
+import org.opendc.simulator.flow.*
+import org.opendc.simulator.flow.mux.MaxMinFlowMultiplexer
/**
* A model of an Uninterruptible Power Supply (UPS).
*
* This model aggregates multiple power sources into a single source in order to ensure that power is always available.
*
- * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param engine The underlying [FlowEngine] to drive the simulation under the hood.
* @param idlePower The idle power consumption of the UPS independent of the load.
* @param lossCoefficient The coefficient for the power loss of the UPS proportional to the load.
*/
public class SimUps(
- interpreter: SimResourceInterpreter,
+ private val engine: FlowEngine,
private val idlePower: Double = 0.0,
private val lossCoefficient: Double = 0.0,
) : SimPowerOutlet() {
/**
* The resource aggregator used to combine the input sources.
*/
- private val aggregator = SimResourceAggregatorMaxMin(interpreter)
+ private val mux = MaxMinFlowMultiplexer(engine)
+
+ /**
+ * The [FlowConsumer] that represents the output of the UPS.
+ */
+ private val provider = mux.newInput()
/**
* Create a new UPS outlet.
*/
public fun newInlet(): SimPowerInlet {
- val forward = SimResourceForwarder(isCoupled = true)
- aggregator.addInput(forward)
+ val forward = FlowForwarder(engine, isCoupled = true)
+ forward.startConsumer(mux.newOutput())
return Inlet(forward)
}
override fun onConnect(inlet: SimPowerInlet) {
- val consumer = inlet.createConsumer()
- aggregator.startConsumer(object : SimResourceConsumer by consumer {
- override fun onNext(ctx: SimResourceContext): SimResourceCommand {
- return when (val cmd = consumer.onNext(ctx)) {
- is SimResourceCommand.Consume -> {
- val duration = cmd.work / cmd.limit
- val loss = computePowerLoss(cmd.limit)
- val newLimit = cmd.limit + loss
+ val source = inlet.createSource()
+ val mapper = FlowMapper(source) { _, rate ->
+ val loss = computePowerLoss(rate)
+ rate + loss
+ }
- SimResourceCommand.Consume(duration * newLimit, newLimit, cmd.deadline)
- }
- is SimResourceCommand.Idle -> {
- val loss = computePowerLoss(0.0)
- if (loss > 0.0)
- SimResourceCommand.Consume(Double.POSITIVE_INFINITY, loss, cmd.deadline)
- else
- cmd
- }
- else -> cmd
- }
- }
- })
+ provider.startConsumer(mapper)
}
override fun onDisconnect(inlet: SimPowerInlet) {
- aggregator.cancel()
+ provider.cancel()
}
/**
@@ -92,8 +83,8 @@ public class SimUps(
/**
* A UPS inlet.
*/
- public inner class Inlet(private val forwarder: SimResourceTransformer) : SimPowerInlet(), AutoCloseable {
- override fun createConsumer(): SimResourceConsumer = forwarder
+ public inner class Inlet(private val forwarder: FlowForwarder) : SimPowerInlet(), AutoCloseable {
+ override fun createSource(): FlowSource = forwarder
/**
* Remove the inlet from the PSU.