StorageElement.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package classes;
  2. import blackstart.resistanceCalculator;
  3. import com.google.gson.annotations.Expose;
  4. import ui.model.Model;
  5. public class StorageElement extends HolonElement implements Comparable<StorageElement> {
  6. @Expose
  7. private Mode status;
  8. @Expose
  9. private float stateOfCharge;
  10. @Expose
  11. private float capacity;
  12. @Expose
  13. private float maxInRatio;
  14. @Expose
  15. private float maxOutRatio;
  16. @Expose
  17. private float chargingRatio;
  18. @Expose
  19. private resistanceCalculator resistanceCalculator;
  20. public StorageElement(String eleName, int amount, float energy, Model model, float capacity, float maxInRatio, float maxOutRatio) {
  21. super(eleName, amount, energy, model);
  22. this.stateOfCharge = 0;
  23. this.maxInRatio = maxInRatio;
  24. this.maxOutRatio = maxOutRatio;
  25. this.capacity = capacity * 60;// for example tesla power wall 10kwh, 10kw per hour thus * 60 if iteration =
  26. // minutes//TODO:!
  27. this.status = Mode.STANDBY;
  28. this.chargingRatio = 0;
  29. this.resistanceCalculator = new resistanceCalculator(model);
  30. }
  31. public float getEnergyPerElement() {
  32. if (status == Mode.STANDBY) {
  33. return 0;
  34. }
  35. return this.energyPerElement;
  36. }
  37. public float getPossibleProduction(float energyRequiredForPowerplantBlackstart) {
  38. if (stateOfCharge > 0) {
  39. if (stateOfCharge >= maxOutRatio) {
  40. return energyAfterResistance(maxOutRatio, energyRequiredForPowerplantBlackstart);
  41. } else {
  42. return energyAfterResistance(stateOfCharge, energyRequiredForPowerplantBlackstart);
  43. }
  44. } else {
  45. return 0;
  46. }
  47. }
  48. private float energyAfterResistance(float energy, float energyRequiredForPowerplantBlackstart){
  49. return resistanceCalculator.calcEnergyAfterResistance(energy, getDistance(), energyRequiredForPowerplantBlackstart);
  50. }
  51. /**
  52. *
  53. * @param status Mode of currect operation
  54. * @param energyWanted float how much energy is needed from storage/ can be collected
  55. * by the storage
  56. * @return float how much energy was collected/emited
  57. */
  58. //TODO: das beachtet nicht wie viel durch widerstand verloren geht
  59. public float setStatusAndSetEnergy(Mode status, float energyWanted, float energyRequiredForPowerplantBlackstart) {
  60. this.status = status;
  61. switch (status) {
  62. case STANDBY:
  63. this.setEnergyPerElement(0);
  64. chargingRatio = 0;
  65. return 0;
  66. case EMIT:
  67. if (energyWanted >= maxOutRatio) { // more energy wanted than can be giving
  68. if (stateOfCharge >= maxOutRatio) { // more energy wanted than can be given
  69. this.setEnergyPerElement(energyAfterResistance(maxOutRatio, energyRequiredForPowerplantBlackstart));
  70. chargingRatio = maxOutRatio;
  71. return energyAfterResistance(maxOutRatio, energyRequiredForPowerplantBlackstart);
  72. } else { // less energy stored that can be given
  73. return notEnoughChargedToEmitWantedEnergy();
  74. }
  75. } else {// less wanted than what can be given
  76. if (stateOfCharge >= energyWanted) { // more energy stored than wanted
  77. this.setEnergyPerElement(energyAfterResistance(energyWanted, energyRequiredForPowerplantBlackstart));
  78. chargingRatio = energyWanted;
  79. return energyAfterResistance(energyWanted, energyRequiredForPowerplantBlackstart);
  80. } else { // less energy stored than wanted
  81. return notEnoughChargedToEmitWantedEnergy();
  82. }
  83. }
  84. case COLLECT://TODO: more testing
  85. if (energyWanted >= maxInRatio) { // more engery given than can be collected
  86. if (stateOfCharge + maxInRatio > capacity) { // Storage nearly full only load rest to get full
  87. this.setEnergyPerElement(-(capacity - stateOfCharge));
  88. chargingRatio = -(capacity - stateOfCharge);
  89. return capacity - stateOfCharge;
  90. } else { // load with maximal of what can be collected
  91. this.setEnergyPerElement(-maxInRatio);
  92. chargingRatio = -maxInRatio;
  93. return maxInRatio;
  94. }
  95. } else { // less engery given than can be taken in
  96. if (capacity == stateOfCharge) { // storage full no energy collected
  97. this.status = Mode.STANDBY;
  98. chargingRatio = 0;
  99. return 0;
  100. } else {
  101. if (stateOfCharge + energyWanted > capacity) { // Storage nearly full only load rest to get full
  102. this.setEnergyPerElement(-(capacity - stateOfCharge));
  103. chargingRatio = -(capacity - stateOfCharge);
  104. return capacity - stateOfCharge;
  105. } else { // take all engery that is available
  106. this.setEnergyPerElement(-energyWanted);
  107. chargingRatio = -energyWanted;
  108. return energyWanted;
  109. }
  110. }
  111. }
  112. default:
  113. System.out.println("no status available");
  114. return 0;
  115. }
  116. }
  117. public Mode getStatus() {
  118. return this.status;
  119. }
  120. public void stateOfChargeCalculation(){
  121. System.out.println("stateofcharge" + getId()+ "before:" + stateOfCharge + " with incomming " + status + " after resi " + getEnergyPerElement() +" before resi "+ chargingRatio);
  122. switch (status){
  123. case COLLECT:
  124. case EMIT:
  125. stateOfCharge = stateOfCharge - chargingRatio;
  126. break;
  127. default:
  128. }
  129. //TODO: fix for gui error comment out bottom if (not sure if side effects should work since storage gets disabled every iteration)
  130. if(stateOfCharge <= 0 || stateOfCharge >= capacity){
  131. status = Mode.STANDBY;
  132. chargingRatio = 0;
  133. setEnergyPerElement(0);
  134. }
  135. System.out.println("stateofcharge after:" + stateOfCharge + " now as " + status + " " + getEnergyPerElement() );
  136. }
  137. public void setStateOfCharge(float stateOfCharge){
  138. if(stateOfCharge < capacity){
  139. this.stateOfCharge = stateOfCharge;
  140. }else{
  141. this.stateOfCharge = capacity;
  142. }
  143. }
  144. private float notEnoughChargedToEmitWantedEnergy(){
  145. if(stateOfCharge <= 0){
  146. this.status = Mode.STANDBY;
  147. this.setEnergyPerElement(0);
  148. return 0;
  149. }else{
  150. this.setEnergyPerElement(stateOfCharge);
  151. return stateOfCharge;
  152. }
  153. }
  154. public float getStateOfCharge() {
  155. return stateOfCharge;
  156. }
  157. public float getMaxOutRatio() {
  158. return maxOutRatio;
  159. }
  160. public boolean chargeDepleted(){
  161. return stateOfCharge <= 0;
  162. }
  163. public boolean fullyCharged(){
  164. return stateOfCharge >= capacity;
  165. }
  166. @Override
  167. public int compareTo(StorageElement storageElement) {
  168. if(this.stateOfCharge < storageElement.getStateOfCharge()){
  169. return -1;
  170. }else if(this.stateOfCharge > storageElement.getStateOfCharge()){
  171. return 1;
  172. }else{
  173. return 0;
  174. }
  175. }
  176. public enum Mode {
  177. COLLECT, EMIT, STANDBY
  178. }
  179. }