StorageElement.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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, 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, getDistance(), 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. float energyNeed = resistanceCalculator.calcEnergyNeededForCertainEnergyAfterResistance(energyWanted, getDistance(), 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(energyRequiredForPowerplantBlackstart);
  74. }
  75. } else {// less wanted than what can be max be given
  76. if(stateOfCharge >= energyNeed){
  77. this.setEnergyPerElement(energyAfterResistance(energyNeed, energyRequiredForPowerplantBlackstart));
  78. chargingRatio = energyNeed;
  79. return energyAfterResistance(energyNeed, energyRequiredForPowerplantBlackstart);
  80. }else{
  81. return notEnoughChargedToEmitWantedEnergy(energyRequiredForPowerplantBlackstart);
  82. }
  83. // if (stateOfCharge >= energyWanted) { // more energy stored than wanted
  84. // this.setEnergyPerElement(energyAfterResistance(energyWanted, energyRequiredForPowerplantBlackstart));
  85. // chargingRatio = energyWanted;
  86. // return energyAfterResistance(energyWanted, energyRequiredForPowerplantBlackstart);
  87. // } else { // less energy stored than wanted
  88. // return notEnoughChargedToEmitWantedEnergy();
  89. // }
  90. }
  91. case COLLECT://TODO: more testing
  92. if (energyWanted >= maxInRatio) { // more engery given than can be collected
  93. if (stateOfCharge + maxInRatio > capacity) { // Storage nearly full only load rest to get full
  94. this.setEnergyPerElement(-(capacity - stateOfCharge));
  95. chargingRatio = -(capacity - stateOfCharge);
  96. return capacity - stateOfCharge;
  97. } else { // load with maximal of what can be collected
  98. this.setEnergyPerElement(-maxInRatio);
  99. chargingRatio = -maxInRatio;
  100. return maxInRatio;
  101. }
  102. } else { // less engery given than can be taken in
  103. if (capacity == stateOfCharge) { // storage full no energy collected
  104. this.status = Mode.STANDBY;
  105. chargingRatio = 0;
  106. return 0;
  107. } else {
  108. if (stateOfCharge + energyWanted > capacity) { // Storage nearly full only load rest to get full
  109. this.setEnergyPerElement(-(capacity - stateOfCharge));
  110. chargingRatio = -(capacity - stateOfCharge);
  111. return capacity - stateOfCharge;
  112. } else { // take all engery that is available
  113. this.setEnergyPerElement(-energyWanted);
  114. chargingRatio = -energyWanted;
  115. return energyWanted;
  116. }
  117. }
  118. }
  119. default:
  120. System.out.println("no status available");
  121. return 0;
  122. }
  123. }
  124. public Mode getStatus() {
  125. return this.status;
  126. }
  127. public void stateOfChargeCalculation(){
  128. System.out.println("stateofcharge" + getId()+ "before:" + stateOfCharge + " with incomming " + status + " after resi " + getEnergyPerElement() +" before resi "+ chargingRatio);
  129. switch (status){
  130. case COLLECT:
  131. case EMIT:
  132. stateOfCharge = stateOfCharge - chargingRatio;
  133. break;
  134. default:
  135. }
  136. //TODO: fix for gui error comment out bottom if (not sure if side effects should work since storage gets disabled every iteration)
  137. if(stateOfCharge <= 0 || stateOfCharge >= capacity){
  138. status = Mode.STANDBY;
  139. chargingRatio = 0;
  140. setEnergyPerElement(0);
  141. }
  142. System.out.println("stateofcharge after:" + stateOfCharge + " now as " + status + " " + getEnergyPerElement() );
  143. }
  144. public void setStateOfCharge(float stateOfCharge){
  145. if(stateOfCharge < capacity){
  146. this.stateOfCharge = stateOfCharge;
  147. }else{
  148. this.stateOfCharge = capacity;
  149. }
  150. }
  151. private float notEnoughChargedToEmitWantedEnergy(float energyRequiredForPowerplantBlackstart){
  152. if(stateOfCharge <= 0){
  153. this.status = Mode.STANDBY;
  154. this.setEnergyPerElement(0);
  155. return 0;
  156. }else{
  157. this.setEnergyPerElement(energyAfterResistance(stateOfCharge, energyRequiredForPowerplantBlackstart));
  158. chargingRatio = stateOfCharge;
  159. return energyAfterResistance(stateOfCharge, energyRequiredForPowerplantBlackstart);
  160. }
  161. }
  162. public float getStateOfCharge() {
  163. return stateOfCharge;
  164. }
  165. public float getMaxOutRatio() {
  166. return maxOutRatio;
  167. }
  168. public boolean chargeDepleted(){
  169. return stateOfCharge <= 0;
  170. }
  171. public boolean fullyCharged(){
  172. return stateOfCharge >= capacity;
  173. }
  174. @Override
  175. public int compareTo(StorageElement storageElement) {
  176. if(this.stateOfCharge < storageElement.getStateOfCharge()){
  177. return -1;
  178. }else if(this.stateOfCharge > storageElement.getStateOfCharge()){
  179. return 1;
  180. }else{
  181. return 0;
  182. }
  183. }
  184. public enum Mode {
  185. COLLECT, EMIT, STANDBY
  186. }
  187. }