StorageElement.java 6.6 KB

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