ResistanceCalculator.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. public class ResistanceCalculator {
  4. @Expose
  5. private int lowVoltage;
  6. @Expose
  7. private int highVoltage;
  8. @Expose
  9. private int lowDiameter;
  10. @Expose
  11. private int highDiameter;
  12. @Expose
  13. private double lowSpecificMaterialResistance;
  14. @Expose
  15. private double highSpecificMaterialResistance;
  16. public ResistanceCalculator(int lowVoltage, int highVoltage, int lowDiameter, int highDiameter, double lowSpecificMaterialResistance, double highSpecificMaterialResistance){
  17. this.lowVoltage = lowVoltage;
  18. this.highVoltage = highVoltage;
  19. this.lowDiameter = lowDiameter;
  20. this.highDiameter = highDiameter;
  21. this.lowSpecificMaterialResistance = lowSpecificMaterialResistance;
  22. this.highSpecificMaterialResistance = highSpecificMaterialResistance;
  23. }
  24. public void setDistancesToCalcResistance(HolonObject powerPlant) {
  25. if (powerPlant != null) {
  26. // travers
  27. if (powerPlant.getConnectedTo().size() > 0) {
  28. for (CpsEdge edge : powerPlant.getConnectedTo()) {
  29. if (powerPlant.getId() == edge.getA().getId()) {
  30. traversEdges(edge.getB(), powerPlant, calcEdgeLength(edge), 0);
  31. } else {
  32. traversEdges(edge.getA(), powerPlant, calcEdgeLength(edge), 0);
  33. }
  34. }
  35. } else {
  36. System.out.println("Nothing connected to powerplant");
  37. }
  38. } else {
  39. System.out.println("No powerplant connected");
  40. }
  41. }
  42. private void traversEdges(AbstractCpsObject currentObject, AbstractCpsObject last, double distance, double lastDistance) {
  43. if (currentObject.getConnectedTo().size() > 1) { // recursive call for all edges
  44. for (CpsEdge edge : currentObject.getConnectedTo()) {
  45. double newDistance = calcEdgeLength(edge);
  46. if (currentObject.getId() == edge.getA().getId()) { // look at which way the edge points
  47. if (last.getId() != edge.getB().getId()) {
  48. traversEdges(edge.getB(), currentObject, distance + newDistance, newDistance);
  49. }
  50. } else {
  51. if (last.getId() != edge.getA().getId()) {
  52. traversEdges(edge.getA(), currentObject, distance + newDistance, newDistance);
  53. }
  54. }
  55. }
  56. } else { // at leaf
  57. for (HolonElement ele : ((HolonObject) currentObject).getElements()) {
  58. ele.setHighDistance(distance - lastDistance);
  59. ele.setLowDistance(lastDistance);
  60. }
  61. }
  62. }
  63. private double calcEdgeLength(CpsEdge edge) {
  64. Position aPos = edge.getA().getPosition();
  65. Position bPos = edge.getB().getPosition();
  66. double xDiff = Math.abs(aPos.x - bPos.x);
  67. double yDiff = Math.abs(aPos.y - bPos.y);
  68. return Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
  69. }
  70. public float calcEnergyAfterResistance(float currentEnergy, double lowDistance, double highDistance, float energyRequiredForPowerplantBlackstart){
  71. double energyAfterLowVoltage = calcEnergyAfterPartOfGrid(currentEnergy, lowVoltage, lowDistance, lowSpecificMaterialResistance, lowDiameter, energyRequiredForPowerplantBlackstart);
  72. energyAfterLowVoltage = (0.98 * energyAfterLowVoltage); //transfomer loses
  73. return (float) calcEnergyAfterPartOfGrid(energyAfterLowVoltage, highVoltage, highDistance, highSpecificMaterialResistance, highDiameter, energyRequiredForPowerplantBlackstart);
  74. }
  75. private double calcEnergyAfterPartOfGrid(double currentEnergy, int voltage, double distance,
  76. double specificMaterialResistance, int diameter, float energyRequiredForPowerplantBlackstart){
  77. double blackstartResistance = blackstartResistance(voltage, energyRequiredForPowerplantBlackstart);
  78. double cableResistance = cableResistance(specificMaterialResistance, distance, diameter);
  79. return (1 - (cableResistance / (cableResistance + blackstartResistance))) * currentEnergy;
  80. }
  81. private double blackstartResistance(int voltage, float energyRequiredForPowerplantBlackstart){
  82. return Math.pow(voltage, 2) / energyRequiredForPowerplantBlackstart;
  83. }
  84. private double cableResistance(double specificMaterialResistance, double distance, int diameter){
  85. return specificMaterialResistance * (distance / (0.25 * Math.PI * Math.pow(diameter, 2)));
  86. }
  87. double calcEnergyNeededForCertainEnergyAfterResistance(double energyWanted, double lowDistance, double highDistance, float energyRequiredForPowerplantBlackstart){
  88. double energyBeforeHighVoltage = calcEnergyNeededBeforePartOfGrid(energyWanted, highVoltage, highDistance, highSpecificMaterialResistance, highDiameter, energyRequiredForPowerplantBlackstart);
  89. energyBeforeHighVoltage = 1.02 * energyBeforeHighVoltage;
  90. return calcEnergyNeededBeforePartOfGrid(energyBeforeHighVoltage, lowVoltage, lowDistance, lowSpecificMaterialResistance, lowDiameter, energyRequiredForPowerplantBlackstart);
  91. }
  92. private double calcEnergyNeededBeforePartOfGrid(double energyWanted, int voltage, double distance,
  93. double specificMaterialResistance, int diameter, float energyRequiredForPowerplantBlackstart){
  94. double blackstartResistance = blackstartResistance(voltage, energyRequiredForPowerplantBlackstart);
  95. double cableResistance = cableResistance(specificMaterialResistance, distance, diameter);
  96. return (energyWanted / (1 - (cableResistance / (cableResistance + blackstartResistance))));
  97. }
  98. }