StorageElement.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 nominalOutRatio;
  15. @Expose
  16. private float currentMaxOutRatio;
  17. @Expose
  18. private float chargingRatio;
  19. @Expose
  20. private resistanceCalculator resistanceCalculator;
  21. public StorageElement(String eleName, int amount, float energy, Model model, float capacity, float maxInRatio, float nominalOutRatio) {
  22. super(eleName, amount, energy, model);
  23. this.stateOfCharge = 0;
  24. this.maxInRatio = maxInRatio;
  25. this.nominalOutRatio = nominalOutRatio;
  26. this.capacity = capacity * 60;// we save in watts per minute for ease of use
  27. this.status = Mode.STANDBY;
  28. this.chargingRatio = 0;
  29. this.resistanceCalculator = new resistanceCalculator(230, 20000,10, 25, 0.017, 0.017);//TODO
  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 >= nominalOutRatio) {
  40. return energyAfterResistance(nominalOutRatio, energyRequiredForPowerplantBlackstart);
  41. } else {
  42. return energyAfterResistance(stateOfCharge, energyRequiredForPowerplantBlackstart);
  43. }
  44. } else {
  45. return 0;
  46. }
  47. }
  48. private float getPossiblePower(){
  49. if (stateOfCharge > 0) {
  50. if (stateOfCharge >= nominalOutRatio) {
  51. return nominalOutRatio;
  52. } else {
  53. return stateOfCharge;
  54. }
  55. } else {
  56. return 0;
  57. }
  58. }
  59. private float energyAfterResistance(float energy, float energyRequiredForPowerplantBlackstart){
  60. return resistanceCalculator.calcEnergyAfterResistance(energy, getLowDistance(), getHighDistance(), energyRequiredForPowerplantBlackstart);
  61. }
  62. /**
  63. *
  64. * @param status Mode of currect operation
  65. * @param energyWanted float how much energy is needed from storage/ can be collected
  66. * by the storage
  67. * @return float how much energy was collected/emited
  68. */
  69. public float setStatusAndSetEnergy(Mode status, float energyWanted, float energyRequiredForPowerplantBlackstart) {
  70. float energyNeed = resistanceCalculator.calcEnergyNeededForCertainEnergyAfterResistance(energyWanted, getLowDistance(), getHighDistance(), energyRequiredForPowerplantBlackstart);
  71. this.status = status;
  72. switch (status) {
  73. case STANDBY:
  74. this.setEnergyPerElement(0);
  75. chargingRatio = 0;
  76. return 0;
  77. case EMIT:
  78. if (energyNeed >= currentMaxOutRatio) { // more energy wanted than can be giving
  79. if (stateOfCharge >= currentMaxOutRatio) { // more energy wanted than can be given
  80. return emitWantedEnergy(currentMaxOutRatio, energyRequiredForPowerplantBlackstart);
  81. }
  82. } else {// less wanted than what can be max be given
  83. if(stateOfCharge >= energyNeed){//TODO: das kann doch garnicht funktioniern
  84. // return emitWantedEnergy(energyNeed, energyRequiredForPowerplantBlackstart);//zurueckrechnungs problem! float nicht genau genug
  85. this.setEnergyPerElement(energyWanted);
  86. chargingRatio = energyNeed;
  87. return energyWanted;
  88. }
  89. }
  90. return notEnoughChargedToEmitWantedEnergy(energyRequiredForPowerplantBlackstart);
  91. case COLLECT://TODO: more testing IGNORES resistanceses!
  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. setCurrentMaxOutRatio();
  134. break;
  135. default:
  136. }
  137. //TODO: fix for gui error comment out bottom if (not sure if side effects should work since storage gets disabled every iteration)
  138. if(stateOfCharge <= 0 || stateOfCharge >= capacity){
  139. status = Mode.STANDBY;
  140. chargingRatio = 0;
  141. setEnergyPerElement(0);
  142. }
  143. // System.out.println("stateofcharge after:" + stateOfCharge + " now as " + status + " " + getEnergyPerElement() );
  144. }
  145. public void setStateOfCharge(float percentage){
  146. this.stateOfCharge = capacity * (percentage / 100);
  147. setCurrentMaxOutRatio();
  148. }
  149. public float getStateOfChargeInPercent(){// 1.0 = 100%
  150. return stateOfCharge / capacity;
  151. }
  152. private void setCurrentMaxOutRatio(){
  153. if(getStateOfChargeInPercent() > 0.8){
  154. currentMaxOutRatio = (float) (nominalOutRatio * (1 + (0.005 * (getStateOfChargeInPercent() * 100 - 0.8 * 100))));
  155. }else if(getStateOfChargeInPercent() < 0.2){
  156. currentMaxOutRatio = (float) (nominalOutRatio * (1 - (0.01 * (0.2 * 100 - getStateOfChargeInPercent() * 100))));
  157. }else{
  158. currentMaxOutRatio = nominalOutRatio;
  159. }
  160. }
  161. private float notEnoughChargedToEmitWantedEnergy(float energyRequiredForPowerplantBlackstart){
  162. if(stateOfCharge <= 0){
  163. this.status = Mode.STANDBY;
  164. this.setEnergyPerElement(0);
  165. return 0;
  166. }else{
  167. return emitWantedEnergy(stateOfCharge, energyRequiredForPowerplantBlackstart);
  168. }
  169. }
  170. private float emitWantedEnergy(float maxEnergy, float energyRequiredForPowerplantBlackstart){
  171. float energyAfterResistance = energyAfterResistance(maxEnergy, energyRequiredForPowerplantBlackstart);
  172. this.setEnergyPerElement(energyAfterResistance);
  173. chargingRatio = maxEnergy;
  174. return energyAfterResistance;
  175. }
  176. public float getUtilization(){
  177. return (getEnergyPerElement() * 100) / currentMaxOutRatio;
  178. }
  179. public float getStateOfCharge() {
  180. return stateOfCharge;
  181. }
  182. public float getNominalOutRatio() {
  183. return nominalOutRatio;
  184. }
  185. public float getCurrentMaxOutRatio() {
  186. return currentMaxOutRatio;
  187. }
  188. public boolean chargeDepleted(){
  189. return stateOfCharge <= 0;
  190. }
  191. public boolean fullyCharged(){
  192. return stateOfCharge >= capacity;
  193. }
  194. @Override
  195. public int compareTo(StorageElement storageElement) {//TODO:!!!
  196. if(this.getPossiblePower() < storageElement.getPossiblePower()){
  197. return 1;
  198. }else if(this.getPossiblePower() > storageElement.getPossiblePower()){
  199. return -1;
  200. }else if(this.stateOfCharge < storageElement.getStateOfCharge()){
  201. return -1;
  202. }else if(this.stateOfCharge > storageElement.getStateOfCharge()){
  203. return 1;
  204. }else{
  205. return Double.compare(storageElement.getLowDistance()+storageElement.getHighDistance(), this.getLowDistance() + this.getHighDistance());
  206. }
  207. }
  208. public enum Mode {
  209. COLLECT, EMIT, STANDBY
  210. }
  211. }