lab6: initial commit with some working code
This commit is contained in:
parent
58becbe55e
commit
bd6a285f8f
|
@ -0,0 +1,33 @@
|
|||
public class Appliance {
|
||||
private boolean powerConnected;
|
||||
|
||||
public Appliance(boolean connected) {
|
||||
this.powerConnected = connected;
|
||||
}
|
||||
|
||||
public boolean getPowerState() {
|
||||
return powerConnected;
|
||||
}
|
||||
|
||||
public void plug() {
|
||||
powerConnected = true;
|
||||
}
|
||||
|
||||
public void unplug() {
|
||||
powerConnected = false;
|
||||
}
|
||||
|
||||
public float min(float v1, float v2) {
|
||||
return v1 <= v2 ? v1 : v2;
|
||||
}
|
||||
|
||||
public float max(float v1, float v2) {
|
||||
return v1 >= v2 ? v1 : v2;
|
||||
}
|
||||
|
||||
public float getPowerConsumption() {
|
||||
return 0f;
|
||||
}
|
||||
|
||||
public void step(float seconds, float ventRPM) {}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
public class Dishwasher extends Appliance {
|
||||
private float basePower = 160.0f;
|
||||
private float[] powerStates = {400f, 60f, 130f, 350f};
|
||||
private float nextPowerStateIn = 20f;
|
||||
private int currentState = -1;
|
||||
|
||||
public Dishwasher(boolean plugged) {
|
||||
super(plugged);
|
||||
}
|
||||
|
||||
public void step(float seconds, float ventRPM) {
|
||||
if (!super.getPowerState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
nextPowerStateIn -= seconds;
|
||||
|
||||
if (nextPowerStateIn <= 0) {
|
||||
nextPowerStateIn += 20f;
|
||||
currentState++;
|
||||
}
|
||||
|
||||
if (currentState > 3) {
|
||||
unplug();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void plug() {
|
||||
super.plug();
|
||||
|
||||
currentState = 0;
|
||||
nextPowerStateIn = 20f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unplug() {
|
||||
super.unplug();
|
||||
|
||||
currentState = -1;
|
||||
}
|
||||
|
||||
public float getPowerConsumption() {
|
||||
if (super.getPowerState()) {
|
||||
return powerStates[currentState];
|
||||
} else {
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Vent vent = new Vent(true);
|
||||
|
||||
Appliance[] devices = {
|
||||
new ServerRack(false),
|
||||
new ServerRack(false),
|
||||
new RPI(false),
|
||||
new Dishwasher(false)
|
||||
};
|
||||
|
||||
devices[0].plug();
|
||||
devices[2].plug();
|
||||
|
||||
int i = 0;
|
||||
float ventConsumed = 0f;
|
||||
float totalPowerConsumed = 0f;
|
||||
//for (int i = 0; i < 180*10; i++) {
|
||||
while (true) {
|
||||
step(vent, devices, 0.1f);
|
||||
ventConsumed += vent.getPowerConsumption() * 0.1;
|
||||
totalPowerConsumed += getTotalPowerConsumption(vent, devices) * 0.1 / 3600;
|
||||
//System.out.print(String.format("Time: %02.1f; ", (float) i/10)
|
||||
System.out.print("Time: " + floatFormat((float)i/10, 2, 1) + "; "
|
||||
+ getStats(vent, devices) + "; vent avg = "
|
||||
+ floatFormat(ventConsumed*10/i, 3, 1)
|
||||
+ "W; total = "
|
||||
+ floatFormat(totalPowerConsumed, 3, 3) + "W\r");
|
||||
if (devices[0].getPowerConsumption() > 205) {
|
||||
vent.plug();
|
||||
}
|
||||
|
||||
if (devices[0].getPowerConsumption() < 190) {
|
||||
vent.unplug();
|
||||
}
|
||||
|
||||
if (i == 150) {
|
||||
devices[1].plug();
|
||||
} else if (i == 300) {
|
||||
devices[0].unplug();
|
||||
} else if (i == 400) {
|
||||
devices[3].plug();
|
||||
} else if (i == 700) {
|
||||
devices[0].plug();
|
||||
}
|
||||
|
||||
try {
|
||||
//TimeUnit.SECONDS.sleep(1);
|
||||
Thread.sleep(2);
|
||||
} catch (Exception e) {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void step(Vent vent, Appliance[] devices, float seconds) {
|
||||
vent.step(seconds);
|
||||
for (Appliance i : devices) {
|
||||
i.step(seconds, vent.getRPM());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getStats(Vent vent, Appliance[] devices) {
|
||||
float[] powerConsumption = new float[devices.length];
|
||||
float totalPowerConsumption = 0;
|
||||
|
||||
for (int i = 0; i < devices.length; i++) {
|
||||
powerConsumption[i] = devices[i].getPowerConsumption();
|
||||
totalPowerConsumption += devices[i].getPowerConsumption();
|
||||
}
|
||||
|
||||
String result = "PPD: ";
|
||||
|
||||
for (float i : powerConsumption) {
|
||||
//result += String.format("%03.1fW ", i);
|
||||
result += String.format(floatFormat(i, 3, 1) + "W ");
|
||||
|
||||
}
|
||||
|
||||
float powerLinesDraw = totalPowerConsumption
|
||||
+ vent.getPowerConsumption();
|
||||
|
||||
//result += String.format("; Vent: %03.1fW, %05.0f RPM; Total power: %04.1fW",
|
||||
result += "; Vent: " + floatFormat(vent.getPowerConsumption(), 3, 1)
|
||||
+ "W, " + floatFormat(vent.getRPM(), 5, 0)
|
||||
+ " RPM; Total power: " + floatFormat(powerLinesDraw, 4, 1)
|
||||
+ "W";
|
||||
//vent.getPowerConsumption(), vent.getRPM(),
|
||||
//totalPowerConsumption + vent.getPowerConsumption());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static float getTotalPowerConsumption(Vent vent,
|
||||
Appliance[] devices) {
|
||||
float result = vent.getPowerConsumption();
|
||||
|
||||
for (Appliance a : devices) {
|
||||
result += a.getPowerConsumption();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String floatFormat(float num, int leading, int trailing) {
|
||||
String newNum = String.format("%0" + leading + "." + trailing + "f", num);
|
||||
|
||||
int targetLength = leading + trailing + 1;
|
||||
|
||||
for (int i = newNum.length(); i < targetLength; i++) {
|
||||
newNum = "0" + newNum;
|
||||
}
|
||||
|
||||
return newNum;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
public class RPI extends Appliance {
|
||||
private float power = 15.0f;
|
||||
private float postBootDecreaseIn = 10.0f;
|
||||
|
||||
public RPI(boolean plugged) {
|
||||
super(plugged);
|
||||
}
|
||||
|
||||
public void step(float seconds, float ventRPM) {
|
||||
postBootDecreaseIn -= seconds;
|
||||
|
||||
if ((postBootDecreaseIn <= 0) && (power >= 5.0)) {
|
||||
power -= seconds;
|
||||
}
|
||||
|
||||
if (power < 5.0) {
|
||||
power = 5.0f;
|
||||
}
|
||||
}
|
||||
|
||||
public float getPowerConsumption() {
|
||||
if (super.getPowerState()) {
|
||||
return power;
|
||||
} else {
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
public class ServerRack extends Appliance {
|
||||
private String type = "ServerRack";
|
||||
private float basePower = 160.0f;
|
||||
private float maxPower = 350.0f;
|
||||
private float temperature = 20f;
|
||||
|
||||
public ServerRack(boolean plugged) {
|
||||
super(plugged);
|
||||
}
|
||||
|
||||
public void step(float seconds, float ventRPM) {
|
||||
if (super.getPowerState()) {
|
||||
temperature += min(basePower + (temperature - 20f) * 1.8f, maxPower)
|
||||
* seconds * 0.024f;
|
||||
}
|
||||
|
||||
temperature -= (temperature - 20f) * ventRPM*0.0003f * seconds;
|
||||
temperature -= 0.006f * (temperature - 20f) * seconds;
|
||||
}
|
||||
|
||||
public float getPowerConsumption() {
|
||||
if (super.getPowerState()) {
|
||||
return min(basePower + (temperature - 20f) * 1.8f, maxPower);
|
||||
} else {
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
public class Vent extends Appliance {
|
||||
private float maxPower = 90.0f;
|
||||
private float rpm = 0.0f;
|
||||
private final float rotorInertia = 7.0f;
|
||||
private final float maxRPM = 6500.0f;
|
||||
|
||||
public Vent(boolean plugged) {
|
||||
super(plugged);
|
||||
}
|
||||
|
||||
public void step(float seconds) {
|
||||
// electric current usage
|
||||
if (super.getPowerState()) {
|
||||
rpm += max(min(((int) (maxRPM - rpm) * rotorInertia), maxPower), 0)
|
||||
* 10 / rotorInertia * seconds;
|
||||
}
|
||||
|
||||
// air drag (always present)
|
||||
rpm -= (rpm / 200) / rotorInertia;
|
||||
}
|
||||
|
||||
public float getPowerConsumption() {
|
||||
if (super.getPowerState()) {
|
||||
return max(min(rotorInertia*(maxRPM - rpm), maxPower), 0);
|
||||
} else {
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
|
||||
public float getRPM() {
|
||||
return rpm;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue