controlAlgorithm.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package blackstart;
  2. import java.awt.*;
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import javax.swing.*;
  7. import api.AddOn;
  8. import classes.*;
  9. import ui.controller.Control;
  10. import ui.controller.StorageProductionController;
  11. public class controlAlgorithm implements AddOn {
  12. // Gui Part:
  13. private Control control;
  14. private JTextArea textArea;
  15. private JPanel content = new JPanel();
  16. // ProgressBar
  17. private long startTime;
  18. private Thread runThread;
  19. private HolonObject powerplant;
  20. private TextField blackstartEnergyrequierementTextfield;
  21. private TextField blackstartSuccessTimeTextfield;
  22. private TextField powerplantMaxOutputTextfield;
  23. private TextField blackstartStartTimeTextfield;
  24. private TextField simulationDurationTextfield;
  25. private TextField storageStartCharge;
  26. private TextField waitBetweenIterations;
  27. private TextField lowVolatageTextfield;
  28. private TextField highVolatageTextfield;
  29. private int blackstartSuccessTime;
  30. private int blackstartStartTime;
  31. private int blackstartRunningCounter;
  32. private float powerplantMaxOutput;
  33. private List<HolonObject> renewableProducers;
  34. private List<HolonObject> consumers;
  35. private StorageProductionController SPC;
  36. public static void main(String[] args) {
  37. JFrame newFrame = new JFrame("exampleWindow");
  38. controlAlgorithm instance = new controlAlgorithm();
  39. newFrame.setContentPane(instance.getPanel());
  40. newFrame.pack();
  41. newFrame.setVisible(true);
  42. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43. }
  44. public controlAlgorithm() {
  45. content.setLayout(new BorderLayout());
  46. textArea = new JTextArea();
  47. textArea.setEditable(false);
  48. JScrollPane scrollPane = new JScrollPane(textArea);
  49. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createOptionPanel(), scrollPane);
  50. splitPane.setResizeWeight(0.0);
  51. content.add(splitPane, BorderLayout.CENTER);
  52. content.setPreferredSize(new Dimension(800, 800));
  53. }
  54. public JPanel createOptionPanel() {
  55. JPanel optionPanel = new JPanel(new BorderLayout());
  56. JScrollPane scrollPane = new JScrollPane(createParameterPanel());
  57. scrollPane.setBorder(BorderFactory.createTitledBorder("Parameter"));
  58. optionPanel.add(scrollPane, BorderLayout.CENTER);
  59. optionPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
  60. return optionPanel;
  61. }
  62. private Component createParameterPanel() {
  63. JPanel parameterPanel = new JPanel(null);
  64. parameterPanel.setPreferredSize(new Dimension(510, 300));
  65. blackstartEnergyrequierementTextfield = new TextField("5000000");
  66. blackstartEnergyrequierementTextfield.setBounds(10, 10, 95, 20);
  67. parameterPanel.add(blackstartEnergyrequierementTextfield);
  68. JLabel blackstartEnergyrequierementLabel = new JLabel("Blackstart Energyrequirement in Watt");
  69. blackstartEnergyrequierementLabel.setBounds(110, 10, 300, 20);
  70. parameterPanel.add(blackstartEnergyrequierementLabel);
  71. blackstartSuccessTimeTextfield = new TextField("30");
  72. blackstartSuccessTimeTextfield.setBounds(10, 35, 95, 20);
  73. parameterPanel.add(blackstartSuccessTimeTextfield);
  74. JLabel blackstartSuccessTimeLabel = new JLabel("Time for successfull blackstart in minutes");
  75. blackstartSuccessTimeLabel.setBounds(110, 35, 300, 20);
  76. parameterPanel.add(blackstartSuccessTimeLabel);
  77. blackstartStartTimeTextfield = new TextField("15");
  78. blackstartStartTimeTextfield.setBounds(10, 60, 95, 20);
  79. parameterPanel.add(blackstartStartTimeTextfield);
  80. JLabel blackstartStartTimeLabel = new JLabel("Starttime for the blackstart");
  81. blackstartStartTimeLabel.setBounds(110, 60, 300, 20);
  82. parameterPanel.add(blackstartStartTimeLabel);
  83. simulationDurationTextfield = new TextField("500");
  84. simulationDurationTextfield.setBounds(10, 85, 95, 20);
  85. parameterPanel.add(simulationDurationTextfield);
  86. JLabel simulationDurationLabel = new JLabel("How long should the simulation run?");
  87. simulationDurationLabel.setBounds(110, 85, 300, 20);
  88. parameterPanel.add(simulationDurationLabel);
  89. powerplantMaxOutputTextfield = new TextField("40000000");
  90. powerplantMaxOutputTextfield.setBounds(10, 110, 95, 20);
  91. parameterPanel.add(powerplantMaxOutputTextfield);
  92. JLabel powerplantMaxOutputLabel = new JLabel("Powerplant Output afer Blackstart");
  93. powerplantMaxOutputLabel.setBounds(110, 110, 300, 20);
  94. parameterPanel.add(powerplantMaxOutputLabel);
  95. storageStartCharge = new TextField("0.16666666");
  96. storageStartCharge.setBounds(10, 135, 95, 20);
  97. parameterPanel.add(storageStartCharge);
  98. JLabel storageStartChargeLabel = new JLabel("Storage charge at start in kWh");
  99. storageStartChargeLabel.setBounds(110, 135, 300, 20);
  100. parameterPanel.add(storageStartChargeLabel);
  101. waitBetweenIterations = new TextField("0");
  102. waitBetweenIterations.setBounds(10, 205, 95, 20);
  103. parameterPanel.add(waitBetweenIterations);
  104. JLabel waitBetweenIterationsLabel = new JLabel("Wait time between iterations");
  105. waitBetweenIterationsLabel.setBounds(110, 205, 300, 20);
  106. parameterPanel.add(waitBetweenIterationsLabel);
  107. // ////////collums
  108. // lowVolatageTextfield = new TextField("230");
  109. // lowVolatageTextfield.setBounds(425, 10, 95, 20);
  110. // parameterPanel.add(lowVolatageTextfield);
  111. //
  112. // JLabel lowVolatageLabel = new JLabel("low voltage in volt");
  113. // lowVolatageLabel.setBounds(530, 10, 300, 20);
  114. // parameterPanel.add(lowVolatageLabel);
  115. //
  116. // highVolatageTextfield = new TextField("20000");
  117. // highVolatageTextfield.setBounds(425, 35, 95, 20);
  118. // parameterPanel.add(highVolatageTextfield);
  119. //
  120. // JLabel highVolatageLabel = new JLabel("high voltage in volt");
  121. // highVolatageLabel.setBounds(530, 35, 300, 20);
  122. // parameterPanel.add(highVolatageLabel);
  123. return parameterPanel;
  124. }
  125. public JPanel createButtonPanel() {
  126. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  127. JButton runButton = new JButton("Run");
  128. runButton.addActionListener(actionEvent -> {
  129. Runnable task = this::run;
  130. runThread = new Thread(task);
  131. runThread.start();
  132. });
  133. buttonPanel.add(runButton);
  134. return buttonPanel;
  135. }
  136. private void run() {
  137. clear();
  138. disableGuiInput(true);
  139. startTimer();
  140. initAlgo();
  141. printElapsedTime();
  142. disableGuiInput(false);
  143. }
  144. private void disableGuiInput(boolean bool) {
  145. control.guiDisable(bool);
  146. }
  147. @Override
  148. public JPanel getPanel() {
  149. return content;
  150. }
  151. @Override
  152. public void setController(Control control) {
  153. this.control = control;
  154. }
  155. private void clear() {
  156. textArea.setText("");
  157. }
  158. private void println(String message) {
  159. textArea.append(message + "\n");
  160. }
  161. private void startTimer() {
  162. startTime = System.currentTimeMillis();
  163. }
  164. private void printElapsedTime() {
  165. long elapsedMilliSeconds = System.currentTimeMillis() - startTime;
  166. println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
  167. }
  168. ///////////////////////////////////////////////////////////////////////////////////
  169. /**
  170. *
  171. */
  172. private void initAlgo() {
  173. try {
  174. // init
  175. renewableProducers = getRenewableProducers();
  176. consumers = getConsumers();
  177. powerplant = control.getSimManager().getPowerplant();// DANGER DONT GIVE NULL
  178. setPowerPlantBlackstartResistance(-Float.parseFloat(blackstartEnergyrequierementTextfield.getText()));
  179. SPC = new StorageProductionController(getStorageElements(), getEnergyRequiredForPowerplantBlackstart());// DANGER DONT GIVE NULL
  180. if(powerplant == null){
  181. println("No Power Plant in Model");
  182. return;
  183. }
  184. resistanceCalculator resistanceCalculator = new resistanceCalculator(230, 20000, 30, 30,0.017, 0.017);//TODO
  185. resistanceCalculator.setDistancesToCalcResistance(powerplant);
  186. blackstartRunningCounter = 0;
  187. deactivateBlackstart();
  188. control.getModel().setCurIteration(0);
  189. // prepare model
  190. /////////
  191. disablePowerplantProduction();
  192. enableAllConsumers();
  193. SPC.setAllStorageToStandy();
  194. // TODO: prios?
  195. /////////
  196. // Get parameters from textfields
  197. setPowerPlantBlackstartResistance(-Float.parseFloat(blackstartEnergyrequierementTextfield.getText()));
  198. blackstartSuccessTime = Integer.parseInt(blackstartSuccessTimeTextfield.getText());
  199. powerplantMaxOutput = Float.parseFloat(powerplantMaxOutputTextfield.getText());
  200. blackstartStartTime = Integer.parseInt(blackstartStartTimeTextfield.getText());
  201. control.getModel().setIterations(Integer.parseInt(simulationDurationTextfield.getText()));
  202. for (StorageElement se :
  203. getStorageElements()) {
  204. se.setStateOfCharge(Float.parseFloat(storageStartCharge.getText())*1000*60);
  205. }
  206. updateVisual();
  207. if (blackstartStartTime + blackstartSuccessTime > control.getModel().getIterations() - 1) {
  208. println("No Time for the blackstart, use working numbers");
  209. } else {
  210. blackstartMain(control.getModel().getCurIteration());
  211. }
  212. } catch (NumberFormatException e) {
  213. println("Worng Input, only numbers please");
  214. }
  215. }
  216. private void blackstartMain(int curIteration) {
  217. try {
  218. Thread.sleep(Integer.parseInt(waitBetweenIterations.getText()));
  219. } catch (InterruptedException e) {
  220. e.printStackTrace();
  221. }
  222. control.getModel().setCurIteration(curIteration);
  223. if (control.getSimManager().blackstartRunning()) {
  224. if (!blackstartAlgo()) {
  225. // blackstart for this iteration was not successfull
  226. deactivateBlackstart();
  227. updateVisual();
  228. println("Simulation of blackstart failed in Iteration: " + curIteration);
  229. } else {
  230. // blackstart for this iteration was successfull
  231. blackstartRunningCounter++;
  232. if (blackstartRunningCounter == blackstartSuccessTime) {
  233. // blackstart was successfull for the needed iterations
  234. SPC.setAllStorageToStandy();
  235. deactivateBlackstart();
  236. enableAllConsumers();
  237. updateVisual();
  238. println("Simulation of blackstart Successfull in Iteration: " + curIteration);
  239. } else {
  240. // blackstart not finished yet
  241. updateVisual();
  242. blackstartMain(curIteration + 1);
  243. }
  244. }
  245. } else {
  246. // blackstart has not started yet
  247. if (curIteration == blackstartStartTime) {
  248. activateBlackstart();
  249. disableConsumers();
  250. blackstartMain(curIteration);
  251. } else {
  252. updateVisual();
  253. blackstartMain(curIteration + 1);
  254. }
  255. }
  256. }
  257. /**
  258. * TODO: HOLEG UNTERVERSORGUNG CHECKEN
  259. * TODO: renewables mit verlust
  260. * TODO: cable diameter/specificresistance
  261. *
  262. * maybes:
  263. * * TODO: prios fuer elemente anschalten
  264. * * TODO: batterie status wechesel fuehrt zu unterversorgung in GUI FIX?: in storage if bei charge auskommentieren
  265. * * TODO: batterie laden prios? entfernung? doppelt sortieren
  266. *
  267. * @return true or false depending on whether the blackstart was successful for
  268. * this iteration
  269. */
  270. private boolean blackstartAlgo() {
  271. printCurrentEnergyState();
  272. if (currentRenewableProduction() < getEnergyRequiredForPowerplantBlackstart()) {
  273. // renewable energy production is not sufficient for the blackstart
  274. if (SPC.currentPossibleStorageProduction() >= getEnergyRequiredForPowerplantBlackstart()
  275. - currentRenewableProduction()) {// is there currently enough power available from storage?
  276. SPC.setAllStorageToStandy();//TODO: placement
  277. SPC.enableStorageDischarging(getEnergyRequiredForPowerplantBlackstart() - currentRenewableProduction());
  278. rampUpPowerplant();
  279. enableConsumers(getPowerplantProduction());
  280. return true;
  281. } else {
  282. // blackstart has failed
  283. SPC.setAllStorageToStandy();
  284. println("Not enough storage energy available");
  285. return false;
  286. }
  287. } else {
  288. //TODO: disable storage?
  289. // renewable energy production is sufficient for the blackstart
  290. rampUpPowerplant();
  291. enableConsumers(
  292. currentRenewableProduction() - getEnergyRequiredForPowerplantBlackstart() + getPowerplantProduction());
  293. return true;
  294. }
  295. }
  296. private ArrayList<StorageElement> getStorageElements() {
  297. ArrayList<StorageElement> storageElements = new ArrayList<>();
  298. for (HolonObject holonObject : control.getModel().getAllHolonObjectsOnCanvas()) {
  299. for (HolonElement ele : holonObject.getElements()) {
  300. if(ele instanceof StorageElement){
  301. storageElements.add((StorageElement) ele);
  302. }
  303. }
  304. }
  305. return storageElements;
  306. }
  307. private void disableConsumers() {
  308. // TODO: disableBatteryLoading? will ich das wirklich?
  309. // SPC.disableStorageProduction();
  310. for (HolonObject consumer : consumers) {
  311. for (HolonElement ele : consumer.getElements()) {
  312. if (ele.isActive() && ele.isConsumer()) {
  313. ele.setActive(false);
  314. }
  315. }
  316. }
  317. }
  318. private void enableConsumers(float energyAvailable) {
  319. // println("current pp production: " + getPowerplantProduction());
  320. // println("energy available for consumers" + energyAvailable);
  321. // Damit wir immer die gleiche ausgangslage haben //TODO: wirklich?
  322. disableConsumers();
  323. // TODO: das ganze lieber mit prirotaeten
  324. for (HolonObject consumer : consumers) {
  325. for (HolonElement ele : consumer.getElements()) {
  326. if (energyAvailable > 0
  327. && energyAvailable - Math.abs(ele.getEnergyPerElement() * ele.getAmount()) >= 0) {//TODO: getenergy ist hier falsch
  328. if (!ele.isActive()) {
  329. ele.setActive(true);
  330. energyAvailable = energyAvailable - Math.abs(ele.getEnergyPerElement() * ele.getAmount());// +
  331. // since
  332. // its
  333. // negative
  334. }
  335. } else {
  336. return;
  337. }
  338. }
  339. }
  340. // if (!SPC.batteriesFull()) {
  341. // // TODO: load unused batteries
  342. // } else {
  343. //
  344. // }
  345. }
  346. private void enableAllConsumers() {
  347. for (HolonObject consumer : consumers) {
  348. for (HolonElement ele : consumer.getElements()) {
  349. if (!ele.isActive() && ele.isConsumer()) {
  350. ele.setActive(true);
  351. }
  352. }//TODO: storage?
  353. }
  354. }
  355. private float currentRenewableProduction() {
  356. float production = 0;
  357. for (HolonObject house : renewableProducers) {
  358. for (HolonElement ele : house.getElements()) {
  359. if (ele.getEleName().equals("Solar Panels")) {// TODO: hier muss noch mehr dazu
  360. production = production + ele.getEnergyAtTimeStep(control.getModel().getCurIteration());
  361. }
  362. }
  363. }
  364. return production;
  365. }
  366. private void rampUpPowerplant() {
  367. for (HolonElement ele : powerplant.getElements()) {
  368. if (ele.getEleName().equals("Power")) {
  369. ele.setEnergyPerElement(ele.getEnergyPerElement() + powerplantMaxOutput / blackstartSuccessTime);
  370. }
  371. }
  372. }
  373. private float getPowerplantProduction() {
  374. float totalProduction = 0;
  375. for (HolonElement ele : powerplant.getElements()) {
  376. if (ele.getEleName().equals("Power")) {
  377. totalProduction = ele.getEnergyPerElement();
  378. }
  379. }
  380. return totalProduction;
  381. }
  382. private void disablePowerplantProduction() {
  383. for (HolonElement ele : powerplant.getElements()) {
  384. if (ele.getEleName().equals("Power")) {
  385. ele.setEnergyPerElement(0);
  386. }
  387. }
  388. }
  389. private float getEnergyRequiredForPowerplantBlackstart() {
  390. for (HolonElement ele : powerplant.getElements()) {
  391. if (ele.getEleName().equals("Blackstart")) {
  392. return -ele.getEnergyPerElement();
  393. }
  394. }
  395. return 0;
  396. }
  397. private void setPowerPlantBlackstartResistance(float resistance) {
  398. for (HolonElement ele : powerplant.getElements()) {
  399. if (ele.getEleName().equals("Blackstart")) {
  400. ele.setEnergyPerElement(resistance);
  401. }
  402. }
  403. }
  404. private void activateBlackstart() {
  405. for (HolonElement ele : powerplant.getElements()) {
  406. if (ele.getEleName().equals("Blackstart")) {
  407. ele.setActive(true);
  408. }
  409. }
  410. }
  411. private void deactivateBlackstart() {
  412. for (HolonElement ele : powerplant.getElements()) {
  413. if (ele.getEleName().equals("Blackstart")) {
  414. ele.setActive(false);
  415. }
  416. }
  417. }
  418. private LinkedList<HolonObject> getRenewableProducers() {
  419. LinkedList<HolonObject> list = new LinkedList<>();
  420. for (HolonObject holonObject : control.getModel().getAllHolonObjectsOnCanvas()) {
  421. if (holonObject.getObjName().contentEquals("House")) {// TODO: das reicht so nicht da muss noch gecheckt
  422. // werden ob es renewables gibt
  423. list.add(holonObject);
  424. }
  425. }
  426. return list;
  427. }
  428. private LinkedList<HolonObject> getConsumers() {
  429. LinkedList<HolonObject> list = new LinkedList<>();
  430. for (HolonObject holonObject : control.getModel().getAllHolonObjectsOnCanvas()) {
  431. if (holonObject.getObjName().contentEquals("House")) {// TODO: das reicht so nicht da muss noch gecheckt
  432. // werden ob es consumer gibt
  433. list.add(holonObject);
  434. }
  435. }
  436. return list;
  437. }
  438. private void printCurrentEnergyState(){
  439. println("blackstart resi: " + getEnergyRequiredForPowerplantBlackstart());
  440. println("currenctrenewable: " + currentRenewableProduction());
  441. println("currenctpossiblestorage: " + SPC.currentPossibleStorageProduction());
  442. for (StorageElement ele :
  443. getStorageElements()) {
  444. println("Storage " + ele.getId() + ", soc: " + (ele.getStateOfCharge()/60)/1000 + "kWh," + " maxpower: " + ele.getMaxOutRatio() + " W, distance: " + (ele.getLowDistance()+ele.getHighDistance()));
  445. }
  446. }
  447. /**
  448. * To let the User See the current state without touching the Canvas.
  449. */
  450. private void updateVisual() {
  451. System.out.println("Start updateVisual in Iteration: " + control.getModel().getCurIteration());
  452. control.calculateStateAndVisualForCurrentTimeStep();
  453. control.updateCanvas();
  454. System.out.println("Finish updateVisual in Iteration: " + control.getModel().getCurIteration());
  455. }
  456. }