StorageElement.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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, 10000,35, 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 >= currentMaxOutRatio) {
  40. return energyAfterResistance(currentMaxOutRatio, 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 double setStatusAndSetEnergy(Mode status, double energyWanted, float energyRequiredForPowerplantBlackstart) {
  70. double energyNeed = resistanceCalculator.calcEnergyNeededForCertainEnergyAfterResistance(
  71. energyWanted, getLowDistance(), getHighDistance(), energyRequiredForPowerplantBlackstart);
  72. this.status = status;
  73. switch (status) {
  74. case STANDBY:
  75. this.setEnergyPerElement(0);
  76. chargingRatio = 0;
  77. return 0;
  78. case EMIT:
  79. if (energyNeed >= currentMaxOutRatio) { // more energy wanted than can be giving
  80. if (stateOfCharge >= currentMaxOutRatio) { // more energy wanted than can be given
  81. return emitWantedEnergy(currentMaxOutRatio, energyRequiredForPowerplantBlackstart);
  82. }
  83. } else {// less wanted than what can be max be given
  84. if(stateOfCharge >= energyNeed){
  85. //das kann doch garnicht funktioniern!
  86. // doch geht need 'vor abzuegen'ist das was abgezogen wird want 'nach abzuegen' das eingespeist wird
  87. // return emitWantedEnergy(energyNeed, energyRequiredForPowerplantBlackstart);//zurueckrechnungs problem! float nicht genau genug
  88. this.setEnergyPerElement((float) energyWanted);
  89. chargingRatio = (float) energyNeed;
  90. return energyWanted;
  91. }
  92. }
  93. return notEnoughChargedToEmitWantedEnergy(energyRequiredForPowerplantBlackstart);
  94. case COLLECT://TODO: more testing IGNORES resistanceses!
  95. if (energyWanted >= maxInRatio) { // more engery given than can be collected
  96. if (stateOfCharge + maxInRatio > capacity) { // Storage nearly full only load rest to get full
  97. this.setEnergyPerElement(-(capacity - stateOfCharge));
  98. chargingRatio = -(capacity - stateOfCharge);
  99. return capacity - stateOfCharge;
  100. } else { // load with maximal of what can be collected
  101. this.setEnergyPerElement(-maxInRatio);
  102. chargingRatio = -maxInRatio;
  103. return maxInRatio;
  104. }
  105. } else { // less engery given than can be taken in
  106. if (capacity == stateOfCharge) { // storage full no energy collected
  107. this.status = Mode.STANDBY;
  108. chargingRatio = 0;
  109. return 0;
  110. } else {
  111. if (stateOfCharge + energyWanted > capacity) { // Storage nearly full only load rest to get full
  112. this.setEnergyPerElement(-(capacity - stateOfCharge));
  113. chargingRatio = -(capacity - stateOfCharge);
  114. return capacity - stateOfCharge;
  115. } else { // take all engery that is available
  116. this.setEnergyPerElement((float) -energyWanted);
  117. chargingRatio = (float) -energyWanted;
  118. return energyWanted;
  119. }
  120. }
  121. }
  122. default:
  123. System.out.println("no status available");
  124. return 0;
  125. }
  126. }
  127. public Mode getStatus() {
  128. return this.status;
  129. }
  130. public void stateOfChargeCalculation(){
  131. // System.out.println("stateofcharge" + getId()+ "before:" + stateOfCharge + " with incomming " + status + " after resi " + getEnergyPerElement() +" before resi "+ chargingRatio);
  132. switch (status){
  133. case COLLECT:
  134. case EMIT:
  135. stateOfCharge = stateOfCharge - chargingRatio;
  136. setCurrentMaxOutRatio();
  137. break;
  138. default:
  139. }
  140. //TODO: fix for gui error comment out bottom if (not sure if side effects should work since storage gets disabled every iteration)
  141. if(stateOfCharge <= 0 || stateOfCharge >= capacity){
  142. status = Mode.STANDBY;
  143. chargingRatio = 0;
  144. setEnergyPerElement(0);
  145. }
  146. // System.out.println("stateofcharge after:" + stateOfCharge + " now as " + status + " " + getEnergyPerElement() );
  147. }
  148. public void setStateOfCharge(float percentage){
  149. this.stateOfCharge = capacity * (percentage / 100);
  150. setCurrentMaxOutRatio();
  151. }
  152. public float getStateOfChargeInPercent(){// 1.0 = 100%
  153. return stateOfCharge / capacity;
  154. }
  155. private void setCurrentMaxOutRatio(){
  156. if(getStateOfChargeInPercent() > 0.8){
  157. currentMaxOutRatio = (float) (nominalOutRatio * (1 + (0.005 * (getStateOfChargeInPercent() * 100 - 0.8 * 100))));
  158. }else if(getStateOfChargeInPercent() < 0.2){
  159. currentMaxOutRatio = (float) (nominalOutRatio * (1 - (0.01 * (0.2 * 100 - getStateOfChargeInPercent() * 100))));
  160. }else{
  161. currentMaxOutRatio = nominalOutRatio;
  162. }
  163. }
  164. private float notEnoughChargedToEmitWantedEnergy(float energyRequiredForPowerplantBlackstart){
  165. if(stateOfCharge <= 0){
  166. this.status = Mode.STANDBY;
  167. this.setEnergyPerElement(0);
  168. return 0;
  169. }else{
  170. return emitWantedEnergy(stateOfCharge, energyRequiredForPowerplantBlackstart);
  171. }
  172. }
  173. private float emitWantedEnergy(float maxEnergy, float energyRequiredForPowerplantBlackstart){
  174. float energyAfterResistance = energyAfterResistance(maxEnergy, energyRequiredForPowerplantBlackstart);
  175. this.setEnergyPerElement(energyAfterResistance);
  176. chargingRatio = maxEnergy;
  177. return energyAfterResistance;
  178. }
  179. public double getUtilization(float energyRequiredForPowerplantBlackstart){
  180. if(currentMaxOutRatio == 0){
  181. return 0;
  182. }else {
  183. return (resistanceCalculator.calcEnergyNeededForCertainEnergyAfterResistance(getEnergyPerElement(), getLowDistance(), getHighDistance(), energyRequiredForPowerplantBlackstart)
  184. * 100) / currentMaxOutRatio;
  185. }
  186. }
  187. public float getStateOfCharge() {
  188. return stateOfCharge;
  189. }
  190. public float getNominalOutRatio() {
  191. return nominalOutRatio;
  192. }
  193. public float getCurrentMaxOutRatio() {
  194. return currentMaxOutRatio;
  195. }
  196. public boolean chargeDepleted(){
  197. return stateOfCharge <= 0;
  198. }
  199. public boolean fullyCharged(){
  200. return stateOfCharge >= capacity;
  201. }
  202. public float getChargingRatio() {
  203. return chargingRatio;
  204. }
  205. // @Override
  206. // public int compareTo(StorageElement storageElement) {//TODO:!!!
  207. //// if(this.getPossiblePower() < storageElement.getPossiblePower()){
  208. //// return -1;
  209. //// }else if(this.getPossiblePower() > storageElement.getPossiblePower()){
  210. //// return 1;
  211. //// }else if(this.stateOfCharge < storageElement.getStateOfCharge()){
  212. //// return -1;
  213. //// }else if(this.stateOfCharge > storageElement.getStateOfCharge()){
  214. //// return 1;
  215. //// }else{
  216. //// return Double.compare(storageElement.getLowDistance()+storageElement.getHighDistance(), this.getLowDistance() + this.getHighDistance());
  217. //// }
  218. // }
  219. public enum Mode {
  220. COLLECT, EMIT, STANDBY
  221. }
  222. }