Selaa lähdekoodia

move resistance calculation to its own class

David Heck 5 vuotta sitten
vanhempi
commit
7fe2b81681
2 muutettua tiedostoa jossa 88 lisäystä ja 74 poistoa
  1. 8 74
      src/blackstart/controlAlgorithm.java
  2. 80 0
      src/blackstart/resistanceCalculator.java

+ 8 - 74
src/blackstart/controlAlgorithm.java

@@ -9,6 +9,8 @@ import javax.swing.*;
 
 import api.AddOn;
 import classes.*;
+import org.nd4j.linalg.api.ndarray.INDArray;
+import org.nd4j.linalg.factory.Nd4j;
 import ui.controller.Control;
 import ui.controller.StorageProductionController;
 
@@ -250,7 +252,8 @@ public class controlAlgorithm implements AddOn {
 				println("No Power Plant in Model");
 				return;
 			}
-			setDistanceToCalcResistance();
+			resistanceCalculator resistanceCalculator = new resistanceCalculator();
+			resistanceCalculator.setDistanceToCalcResistance(powerPlant);
 			blackstartRunningCounter = 0;
 			deactivateBlackstart();
 			control.getModel().setCurIteration(0);
@@ -298,7 +301,7 @@ public class controlAlgorithm implements AddOn {
 	}
 
 	/**
-	 * 
+	 *
 	 * @param curIteration
 	 */
 	private void blackstartMain(int curIteration) {
@@ -309,7 +312,7 @@ public class controlAlgorithm implements AddOn {
 		}
 		control.getModel().setCurIteration(curIteration);
 		if (control.getSimManager().blackstartRunning()) {
-			if (!blackstartAlgo(curIteration)) {
+			if (!blackstartAlgo()) {
 				// blackstart for this iteration was not successfull
 				deactivateBlackstart();
 				updateVisual();
@@ -352,11 +355,10 @@ public class controlAlgorithm implements AddOn {
 	 * TODO: batterie modes wo nicht passiert? obwohl laden / entladen moeglich waere?
 	 * TODO: batterie laden prios? entfernung?
 	 *
-	 * @param curIteration
 	 * @return true or false depending on whether the blackstart was successful for
 	 *         this iteration
 	 */
-	private boolean blackstartAlgo(int curIteration) {
+	private boolean blackstartAlgo() {
 		if (currentRenewableProduction() < getPowerPlantBlackstartResistance()) {
 			// renewable energy production is not sufficient for the blackstart
 			if (SPC.currentPossibleStorageProduction() >= getPowerPlantBlackstartResistance()
@@ -395,75 +397,7 @@ public class controlAlgorithm implements AddOn {
 		return storageElements;
 	}
 
-	private void setDistanceToCalcResistance() {
-
-		if (powerPlant != null) {
-//			println("Powerplant Energy @"+powerPlant.getEnergyAtTimeStep(1));
-//			println("" + getPowerplantProduction());
-
-			// travers
-			if (powerPlant.getConnectedTo().size() > 0) {
-				for (CpsEdge edge : powerPlant.getConnectedTo()) {
-//					println(edge.toString());
-					if (powerPlant.getId() == edge.getA().getId()) {
-						traversEdges(edge.getB(), powerPlant, calcEdgeLength(edge));
-					} else {
-						traversEdges(edge.getA(), powerPlant, calcEdgeLength(edge));
-					}
-				}
-			} else {
-				println("Nothing connected to powerplant");
-			}
-		} else {
-			println("No powerplant connected");
-		}
-	}
-
-	private void traversEdges(AbstractCpsObject currentObject, AbstractCpsObject last, double distance) {
-		if (currentObject.getConnectedTo().size() > 1) { // recursive call for all edges
-			for (CpsEdge edge : currentObject.getConnectedTo()) {
-				if (currentObject.getId() == edge.getA().getId()) { // look at which way the edge points
-					if (last.getId() != edge.getB().getId()) {
-						traversEdges(edge.getB(), currentObject, distance + calcEdgeLength(edge));
-					}
-				} else {
-					if (last.getId() != edge.getA().getId()) {
-						traversEdges(edge.getA(), currentObject, distance + calcEdgeLength(edge));
-					}
-				}
-			}
-		} else { // at leaf
-			for (HolonElement ele : ((HolonObject) currentObject).getElements()) {
-				ele.setDistance(distance);
-//				if (ele.getEleName().equals("Solar Panels")) {// TODO: das wollen wir ja so nicht
-//					ele.setEnergyPerElement(5000);
-//					ele.setEnergyPerElement(calcEnergyAfterResistance(ele.getEnergyPerElement(), distance));
-//				}
-			}
-
-		}
-	}
-
-	private double calcEdgeLength(CpsEdge edge) {
-		Position aPos = edge.getA().getPosition();
-		Position bPos = edge.getB().getPosition();
-		double xDiff = Math.abs(aPos.x - bPos.x);
-		double yDiff = Math.abs(aPos.y - bPos.y);
-		return Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
-	}
-
-	private float calcEnergyAfterResistance(float currentEnergy, double distance) {
-		// 230v kupfer 30mm durchmesser
-		int volatage = 230;
-		int diameter = 30;
-		// kupfer
-		double specificMaterialResistance = 0.017;
-		double blackstartResistance = Math.pow(volatage, 2) / getPowerPlantBlackstartResistance();
-		double cableResistance = specificMaterialResistance * (distance / (0.25 * Math.PI * Math.pow(diameter, 2)));
-		return (float) (1 - (cableResistance / (cableResistance + blackstartResistance))) * currentEnergy;
-	}
-
-	private void disableConsumers() {
+		private void disableConsumers() {
 		// TODO: disableBatteryLoading? will ich das wirklich?
 		// SPC.disableStorageProduction();
 

+ 80 - 0
src/blackstart/resistanceCalculator.java

@@ -0,0 +1,80 @@
+package blackstart;
+
+import classes.*;
+import org.nd4j.linalg.api.ndarray.INDArray;
+import org.nd4j.linalg.factory.Nd4j;
+
+public class resistanceCalculator {
+
+    void setDistanceToCalcResistance(HolonObject powerPlant) {
+        if (powerPlant != null) {
+//			println("Powerplant Energy @"+powerPlant.getEnergyAtTimeStep(1));
+//			println("" + getPowerplantProduction());
+
+            // travers
+            if (powerPlant.getConnectedTo().size() > 0) {
+                for (CpsEdge edge : powerPlant.getConnectedTo()) {
+//					println(edge.toString());
+                    if (powerPlant.getId() == edge.getA().getId()) {
+                        traversEdges(edge.getB(), powerPlant, calcEdgeLength(edge));
+                    } else {
+                        traversEdges(edge.getA(), powerPlant, calcEdgeLength(edge));
+                    }
+                }
+            } else {
+//                println("Nothing connected to powerplant");
+            }
+        } else {
+//            println("No powerplant connected");
+        }
+    }
+
+    private void traversEdges(AbstractCpsObject currentObject, AbstractCpsObject last, double distance) {
+        if (currentObject.getConnectedTo().size() > 1) { // recursive call for all edges
+            for (CpsEdge edge : currentObject.getConnectedTo()) {
+                if (currentObject.getId() == edge.getA().getId()) { // look at which way the edge points
+                    if (last.getId() != edge.getB().getId()) {
+                        traversEdges(edge.getB(), currentObject, distance + calcEdgeLength(edge));
+                    }
+                } else {
+                    if (last.getId() != edge.getA().getId()) {
+                        traversEdges(edge.getA(), currentObject, distance + calcEdgeLength(edge));
+                    }
+                }
+            }
+        } else { // at leaf
+            for (HolonElement ele : ((HolonObject) currentObject).getElements()) {
+                ele.setDistance(distance);
+//				if (ele.getEleName().equals("Solar Panels")) {// TODO: das wollen wir ja so nicht
+//					ele.setEnergyPerElement(5000);
+//					ele.setEnergyPerElement(calcEnergyAfterResistance(ele.getEnergyPerElement(), distance));
+//				}
+            }
+
+        }
+    }
+
+    private void matrixtest(){
+        INDArray nd = Nd4j.create(new float[]{1, 2, 3, 4}, new int[]{2, 2});
+
+    }
+
+    private double calcEdgeLength(CpsEdge edge) {
+        Position aPos = edge.getA().getPosition();
+        Position bPos = edge.getB().getPosition();
+        double xDiff = Math.abs(aPos.x - bPos.x);
+        double yDiff = Math.abs(aPos.y - bPos.y);
+        return Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
+    }
+
+    private float calcEnergyAfterResistance(float currentEnergy, double distance, float powerPlantBlackstartResistance) {
+        // 230v kupfer 30mm durchmesser
+        int volatage = 230;
+        int diameter = 30;
+        // kupfer
+        double specificMaterialResistance = 0.017;
+        double blackstartResistance = Math.pow(volatage, 2) / powerPlantBlackstartResistance; //getPowerPlantBlackstartResistance() carefull look if positive or negative
+        double cableResistance = specificMaterialResistance * (distance / (0.25 * Math.PI * Math.pow(diameter, 2)));
+        return (float) (1 - (cableResistance / (cableResistance + blackstartResistance))) * currentEnergy;
+    }
+}