Skip to content

Commit 20d8587

Browse files
added embodied carbon (#303)
1 parent 2a0f78c commit 20d8587

File tree

8 files changed

+66
-24
lines changed

8 files changed

+66
-24
lines changed

opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/provisioner/HostsProvisioningStep.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public class HostsProvisioningStep internal constructor(
9191
cluster.battery!!.initialCharge,
9292
cluster.battery!!.name,
9393
cluster.name,
94+
cluster.battery!!.embodiedCarbon,
95+
cluster.battery!!.expectedLifetime,
9496
)
9597
graph.addEdge(battery, batteryDistributor)
9698

opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/telemetry/parquet/DfltBatteryExportColumns.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public object DfltBatteryExportColumns {
8383
field = Types.required(FLOAT).named("energy_usage"),
8484
) { it.energyUsage }
8585

86+
public val EMBODIED_CARBON: ExportColumn<BatteryTableReader> =
87+
ExportColumn(
88+
field = Types.required(FLOAT).named("embodied_carbon_emission"),
89+
) { it.embodiedCarbonEmission }
90+
8691
public val CHARGE: ExportColumn<BatteryTableReader> =
8792
ExportColumn(
8893
field = Types.required(FLOAT).named("charge"),

opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/telemetry/table/battery/BatteryTableReader.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public interface BatteryTableReader : Exportable {
5050
*/
5151
public val timestampAbsolute: Instant
5252

53-
/**
54-
* The number of connected hosts
55-
*/
56-
public val hostsConnected: Int
57-
5853
/**
5954
* The current power draw of the host in W.
6055
*/
@@ -65,9 +60,23 @@ public interface BatteryTableReader : Exportable {
6560
*/
6661
public val energyUsage: Double
6762

63+
/**
64+
* The embodied carbon cost of the Battery in kg
65+
*/
66+
public val embodiedCarbonEmission: Double
67+
68+
/**
69+
* The current state of the battery
70+
*/
6871
public val batteryState: BatteryState
6972

73+
/**
74+
* The current charge of the battery in J
75+
*/
7076
public val charge: Double
7177

78+
/**
79+
* The capacity of the battery in J
80+
*/
7281
public val capacity: Double
7382
}

opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/telemetry/table/battery/BatteryTableReaderImpl.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class BatteryTableReaderImpl(
4848
_timestamp = table.timestamp
4949
_timestampAbsolute = table.timestampAbsolute
5050

51-
_hostsConnected = table.hostsConnected
5251
_powerDraw = table.powerDraw
5352
_energyUsage = table.energyUsage
53+
_embodiedCarbonEmission = table.embodiedCarbonEmission
5454
_charge = table.charge
5555
_capacity = table.capacity
5656
_batteryState = table.batteryState
@@ -72,10 +72,6 @@ public class BatteryTableReaderImpl(
7272
override val timestampAbsolute: Instant
7373
get() = _timestampAbsolute
7474

75-
override val hostsConnected: Int
76-
get() = _hostsConnected
77-
private var _hostsConnected: Int = 0
78-
7975
override val powerDraw: Double
8076
get() = _powerDraw
8177
private var _powerDraw = 0.0
@@ -85,6 +81,11 @@ public class BatteryTableReaderImpl(
8581
private var _energyUsage = 0.0
8682
private var previousEnergyUsage = 0.0
8783

84+
override val embodiedCarbonEmission: Double
85+
get() = _embodiedCarbonEmission - previousEmbodiedCarbonEmission
86+
private var _embodiedCarbonEmission = 0.0
87+
private var previousEmbodiedCarbonEmission = 0.0
88+
8889
override val charge: Double
8990
get() = _charge
9091
private var _charge = 0.0
@@ -104,11 +105,10 @@ public class BatteryTableReaderImpl(
104105
_timestamp = now
105106
_timestampAbsolute = now + startTime
106107

107-
_hostsConnected = 0
108-
109108
battery.updateCounters()
110109
_powerDraw = battery.outgoingSupply
111110
_energyUsage = battery.totalEnergyUsage
111+
_embodiedCarbonEmission = battery.embodiedCarbonEmission
112112

113113
_charge = battery.charge
114114
_capacity = battery.capacity
@@ -120,10 +120,11 @@ public class BatteryTableReaderImpl(
120120
*/
121121
override fun reset() {
122122
previousEnergyUsage = _energyUsage
123+
previousEmbodiedCarbonEmission = _embodiedCarbonEmission
123124

124-
_hostsConnected = 0
125125
_powerDraw = 0.0
126126
_energyUsage = 0.0
127+
_embodiedCarbonEmission = 0.0
127128
_charge = 0.0
128129
_capacity = 0.0
129130
_batteryState = BatteryState.IDLE

opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/TopologyFactories.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ private fun ClusterJSONSpec.toClusterSpec(): ClusterSpec {
143143
this.battery.chargingSpeed,
144144
this.battery.batteryPolicy,
145145
this.battery.initialCharge,
146+
this.battery.embodiedCarbon,
147+
this.battery.expectedLifetime,
146148
)
147149
}
148150

opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/BatterySpec.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ public data class BatterySpec(
2828
val chargingSpeed: Double,
2929
val batteryPolicy: BatteryPolicyJSONSpec,
3030
val initialCharge: Double,
31+
val embodiedCarbon: Double,
32+
val expectedLifetime: Double,
3133
)

opendc-compute/opendc-compute-topology/src/main/kotlin/org/opendc/compute/topology/specs/TopologySpecs.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public data class PowerSourceJSONSpec(
166166
* @property chargingSpeed The charging speed of the battery in J
167167
* @property batteryPolicy The policy used to decide when the battery charges and discharges
168168
* @property initialCharge The initial charge in the battery
169+
* @property embodiedCarbon The embodied carbon needed to create the battery in gram
170+
* @property expectedLifetime The expected lifetime of the battery in years
171+
*
169172
*/
170173
@Serializable
171174
public data class BatteryJSONSpec(
@@ -174,12 +177,9 @@ public data class BatteryJSONSpec(
174177
val chargingSpeed: Double,
175178
val batteryPolicy: BatteryPolicyJSONSpec,
176179
var initialCharge: Double = 0.0,
177-
) {
178-
init {
179-
this.capacity *= 3600000
180-
this.initialCharge *= 3600000
181-
}
182-
}
180+
var embodiedCarbon: Double = 0.0,
181+
var expectedLifetime: Double = 0.0,
182+
)
183183

184184
@Serializable
185185
public data class BatteryPolicyJSONSpec(

opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/SimBattery.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public class SimBattery extends FlowNode implements FlowConsumer, FlowSupplier {
4949

5050
private final String name;
5151
private final String clusterName;
52+
private final Double embodiedCarbonRate; // The rate of carbon emissions per millisecond
53+
private Double embodiedCarbonEmission = 0.0;
54+
55+
public Double getEmbodiedCarbonEmission() {
56+
return embodiedCarbonEmission;
57+
}
5258

5359
public String getName() {
5460
return name;
@@ -112,25 +118,38 @@ public boolean isEmpty() {
112118
}
113119

114120
/**
115-
* Construct a new {@link FlowNode} instance.
121+
* Construct a new {@link SimBattery} instance.
116122
*
117-
* @param parentGraph The {@link FlowGraph} this stage belongs to.
123+
* @param parentGraph The {@link FlowGraph} instance this battery is part of.
124+
* @param capacity The capacity of the battery in kWh.
125+
* @param chargingSpeed The charging speed of the battery in J.
126+
* @param initialCharge The initial charge of the battery in kWh.
127+
* @param name The name of the battery.
128+
* @param clusterName The name of the cluster the battery is part of.
129+
* @param totalEmbodiedCarbon The total embodied carbon used to manufacture the battery in kg.
130+
* @param expectedLifeTime The expected lifetime of the battery in years.
118131
*/
119132
public SimBattery(
120133
FlowGraph parentGraph,
121134
double capacity,
122135
double chargingSpeed,
123136
double initialCharge,
124137
String name,
125-
String clusterName) {
138+
String clusterName,
139+
Double totalEmbodiedCarbon,
140+
Double expectedLifeTime) {
126141

127142
super(parentGraph);
128-
this.capacity = capacity;
143+
this.capacity = capacity * 3600000;
129144
this.chargingSpeed = chargingSpeed;
130145

131-
this.charge = initialCharge;
146+
this.charge = initialCharge * 3600000;
132147
this.name = name;
133148
this.clusterName = clusterName;
149+
150+
// TODO: maybe change this to days instead of years?
151+
this.embodiedCarbonRate =
152+
(totalEmbodiedCarbon * 1000) / (expectedLifeTime * 365.0 * 24.0 * 60.0 * 60.0 * 1000.0);
134153
}
135154

136155
public void close() {
@@ -188,6 +207,8 @@ public void updateCounters(long now) {
188207

189208
long passedTime = now - lastUpdate;
190209

210+
this.embodiedCarbonEmission += this.embodiedCarbonRate * passedTime;
211+
191212
this.updateCharge(passedTime);
192213
if (passedTime > 0) {
193214
double energyUsage = (this.outgoingSupply * passedTime * 0.001);

0 commit comments

Comments
 (0)