summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-power/src/main
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-09-30 16:00:05 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-10-03 17:17:40 +0200
commit94783ff9d8cd81275fefd5804ac99f98e2dee3a4 (patch)
tree9668a4370fa49aa7bae4decb4b358681dd3031b7 /opendc-simulator/opendc-simulator-power/src/main
parenta2ce07026bf3ef17326e72f395dfa2dd9d9b17be (diff)
fix(simulator): Fix loss computation for UPS and PDU
This change fixes the loss computation for both the UPS and PDU implementation that was broken due to the new pushing mechanism. We implement a new class FlowMapper that can be used to map the flow pushed by a `FlowSource` using a user-specified method.
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.kt16
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt2
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt2
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt18
4 files changed, 13 insertions, 25 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 c33f5186..d536f22d 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
@@ -57,17 +57,9 @@ public class SimPdu(
mux.addOutput(forwarder)
}
- override fun createConsumer(): FlowSource = object : FlowSource by forwarder {
- override fun onPull(conn: FlowConnection, now: Long, delta: Long): Long {
- val duration = forwarder.onPull(conn, now, delta)
- val loss = computePowerLoss(conn.demand)
- val newLimit = conn.demand + loss
-
- conn.push(newLimit)
- return duration
- }
-
- override fun toString(): String = "SimPdu.Consumer"
+ override fun createSource(): FlowSource = FlowMapper(forwarder) { _, rate ->
+ val loss = computePowerLoss(rate)
+ rate + loss
}
override fun toString(): String = "SimPdu"
@@ -85,7 +77,7 @@ public class SimPdu(
*/
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) {
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 851b28a5..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
@@ -44,5 +44,5 @@ public abstract class SimPowerInlet {
/**
* Create a [FlowSource] which represents the consumption of electricity from the power outlet.
*/
- public abstract fun createConsumer(): FlowSource
+ 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 7faebd75..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
@@ -43,7 +43,7 @@ public class SimPowerSource(engine: FlowEngine, public val capacity: Double) : S
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 5eaa91af..312f1d0f 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
@@ -59,17 +59,13 @@ public class SimUps(
}
override fun onConnect(inlet: SimPowerInlet) {
- val consumer = inlet.createConsumer()
- provider.startConsumer(object : FlowSource by consumer {
- override fun onPull(conn: FlowConnection, now: Long, delta: Long): Long {
- val duration = consumer.onPull(conn, now, delta)
- val loss = computePowerLoss(conn.demand)
- val newLimit = conn.demand + loss
+ val source = inlet.createSource()
+ val mapper = FlowMapper(source) { _, rate ->
+ val loss = computePowerLoss(rate)
+ rate + loss
+ }
- conn.push(newLimit)
- return duration
- }
- })
+ provider.startConsumer(mapper)
}
override fun onDisconnect(inlet: SimPowerInlet) {
@@ -88,7 +84,7 @@ public class SimUps(
* A UPS inlet.
*/
public inner class Inlet(private val forwarder: FlowForwarder) : SimPowerInlet(), AutoCloseable {
- override fun createConsumer(): FlowSource = forwarder
+ override fun createSource(): FlowSource = forwarder
/**
* Remove the inlet from the PSU.