From bd6a285f8f847b8bfb97d3f816563948d9e9a751 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Sun, 18 Jun 2023 20:08:19 +0300 Subject: [PATCH] lab6: initial commit with some working code --- labs/6/Appliance.java | 33 +++++++++++ labs/6/Dishwasher.java | 50 +++++++++++++++++ labs/6/Main.java | 121 +++++++++++++++++++++++++++++++++++++++++ labs/6/RPI.java | 28 ++++++++++ labs/6/ServerRack.java | 28 ++++++++++ labs/6/Vent.java | 33 +++++++++++ 6 files changed, 293 insertions(+) create mode 100644 labs/6/Appliance.java create mode 100644 labs/6/Dishwasher.java create mode 100644 labs/6/Main.java create mode 100644 labs/6/RPI.java create mode 100644 labs/6/ServerRack.java create mode 100644 labs/6/Vent.java diff --git a/labs/6/Appliance.java b/labs/6/Appliance.java new file mode 100644 index 0000000..2a65e61 --- /dev/null +++ b/labs/6/Appliance.java @@ -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) {} +} diff --git a/labs/6/Dishwasher.java b/labs/6/Dishwasher.java new file mode 100644 index 0000000..838005f --- /dev/null +++ b/labs/6/Dishwasher.java @@ -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; + } + } +} diff --git a/labs/6/Main.java b/labs/6/Main.java new file mode 100644 index 0000000..d347ef1 --- /dev/null +++ b/labs/6/Main.java @@ -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; + } +} diff --git a/labs/6/RPI.java b/labs/6/RPI.java new file mode 100644 index 0000000..7187d4c --- /dev/null +++ b/labs/6/RPI.java @@ -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; + } + } +} diff --git a/labs/6/ServerRack.java b/labs/6/ServerRack.java new file mode 100644 index 0000000..d1cc71e --- /dev/null +++ b/labs/6/ServerRack.java @@ -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; + } + } +} diff --git a/labs/6/Vent.java b/labs/6/Vent.java new file mode 100644 index 0000000..df62f3b --- /dev/null +++ b/labs/6/Vent.java @@ -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; + } +}