|
@@ -11,6 +11,7 @@ import holeg.simple_grid.SimpleGridNode;
|
|
import holeg.ui.PowerFlowAnalysisMenu;
|
|
import holeg.ui.PowerFlowAnalysisMenu;
|
|
import holeg.ui.SolveResultMessageBox;
|
|
import holeg.ui.SolveResultMessageBox;
|
|
import ui.controller.FlexManager;
|
|
import ui.controller.FlexManager;
|
|
|
|
+import ui.controller.SingletonControl;
|
|
import ui.model.*;
|
|
import ui.model.*;
|
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -39,6 +40,7 @@ public class HolegGateway {
|
|
for (AbstractCanvasObject object : network.getNodeAndSwitches()) {
|
|
for (AbstractCanvasObject object : network.getNodeAndSwitches()) {
|
|
SimpleGridNode node = gridBuilder.addHouse(new ComplexNumber(0, 0));
|
|
SimpleGridNode node = gridBuilder.addHouse(new ComplexNumber(0, 0));
|
|
node.tag = object;
|
|
node.tag = object;
|
|
|
|
+ node.typeByDesign = NodeType.Bus;
|
|
canvasToGrid.put(object, node);
|
|
canvasToGrid.put(object, node);
|
|
}
|
|
}
|
|
for (IntermediateCableWithState cable : network.getEdgeList()) {
|
|
for (IntermediateCableWithState cable : network.getEdgeList()) {
|
|
@@ -55,27 +57,32 @@ public class HolegGateway {
|
|
}
|
|
}
|
|
|
|
|
|
private static void decorateNetwork(MinimumNetwork network, int iteration, FlexManager flexManager, DecoratedNetwork decoratedNetwork, Grid grid) {
|
|
private static void decorateNetwork(MinimumNetwork network, int iteration, FlexManager flexManager, DecoratedNetwork decoratedNetwork, Grid grid) {
|
|
- for (GridNode node : grid.getNodes()) {
|
|
|
|
- SimpleGridNode simpleNode = (SimpleGridNode) node;
|
|
|
|
- if (node.getPowerGeneration().lenSquared() > 0)
|
|
|
|
- decoratedNetwork.getSupplierList().add(new Supplier((HolonObject) simpleNode.tag, (float) node.getPowerGeneration().real, 0, simpleNode.voltage, simpleNode.phaseDegrees, simpleNode.typeSolved == NodeType.Slack));
|
|
|
|
- else if (node.getPowerConsumption().lenSquared() > 0)
|
|
|
|
- decoratedNetwork.getConsumerList().add(new Consumer((HolonObject) simpleNode.tag, (float) node.getPowerConsumption().real, simpleNode.voltage, simpleNode.phaseDegrees, Math.cos(simpleNode.getPowerConsumption().angle()), simpleNode.typeSolved == NodeType.Slack));
|
|
|
|
- else if (simpleNode.tag instanceof HolonObject)
|
|
|
|
- decoratedNetwork.getPassivNoEnergyList().add(new Passiv((HolonObject) simpleNode.tag));
|
|
|
|
- }
|
|
|
|
- for (GridEdge edge : grid.getEdges()) {
|
|
|
|
- SimpleGridEdge simpleGridEdge = (SimpleGridEdge) edge;
|
|
|
|
- decoratedNetwork.getDecoratedCableList().add(
|
|
|
|
- new DecoratedCable(
|
|
|
|
- ((IntermediateCableWithState)simpleGridEdge.tag).getModel(),
|
|
|
|
- DecoratedCable.CableState.Working,
|
|
|
|
- (float)simpleGridEdge.power.real,
|
|
|
|
- (float)simpleGridEdge.loss.real));
|
|
|
|
|
|
+ synchronized (decoratedNetwork.getLockObject()) {
|
|
|
|
+ decoratedNetwork.clear();
|
|
|
|
+ for (GridNode node : grid.getNodes()) {
|
|
|
|
+ SimpleGridNode simpleNode = (SimpleGridNode) node;
|
|
|
|
+ if (node.getPowerGeneration().lenSquared() > 0)
|
|
|
|
+ decoratedNetwork.getSupplierList().add(new Supplier((HolonObject) simpleNode.tag, (float) node.getPowerGeneration().real, 0, simpleNode.voltage, simpleNode.phaseDegrees, simpleNode.typeSolved == NodeType.Slack));
|
|
|
|
+ else if (node.getPowerConsumption().lenSquared() > 0)
|
|
|
|
+ decoratedNetwork.getConsumerList().add(new Consumer((HolonObject) simpleNode.tag, (float) node.getPowerConsumption().real, simpleNode.voltage, simpleNode.phaseDegrees, Math.cos(simpleNode.getPowerConsumption().angle()), simpleNode.typeSolved == NodeType.Slack));
|
|
|
|
+ else if (simpleNode.tag instanceof HolonObject)
|
|
|
|
+ decoratedNetwork.getPassivNoEnergyList().add(new Passiv((HolonObject) simpleNode.tag));
|
|
|
|
+ }
|
|
|
|
+ for (GridEdge edge : grid.getEdges()) {
|
|
|
|
+ SimpleGridEdge simpleGridEdge = (SimpleGridEdge) edge;
|
|
|
|
+ decoratedNetwork.getDecoratedCableList().add(
|
|
|
|
+ new DecoratedCable(
|
|
|
|
+ ((IntermediateCableWithState) simpleGridEdge.tag).getModel(),
|
|
|
|
+ DecoratedCable.CableState.Working,
|
|
|
|
+ (float) simpleGridEdge.power.real,
|
|
|
|
+ (float) simpleGridEdge.loss.real));
|
|
|
|
+ }
|
|
|
|
+ decoratedNetwork.calculateStates();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static void solve(HolegPowerFlowContext context, MinimumNetwork minimumNetwork, int iteration, FlexManager flexManager, DecoratedNetwork network) {
|
|
public static void solve(HolegPowerFlowContext context, MinimumNetwork minimumNetwork, int iteration, FlexManager flexManager, DecoratedNetwork network) {
|
|
|
|
+ boolean solve = !PowerFlowAnalysisMenu.getInstance().areUpdatesDisabled();
|
|
Grid grid = convert(minimumNetwork, iteration, flexManager);
|
|
Grid grid = convert(minimumNetwork, iteration, flexManager);
|
|
|
|
|
|
// Check if the grid is equal to the one already solved
|
|
// Check if the grid is equal to the one already solved
|
|
@@ -87,24 +94,64 @@ public class HolegGateway {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Keep size constrained to 32 saved grids
|
|
|
|
- if (context.lastSolvedGrids.size() >= 32)
|
|
|
|
- context.lastSolvedGrids.remove(0);
|
|
|
|
- context.lastSolvedGrids.add(grid);
|
|
|
|
|
|
+ // Only remember grid when solving
|
|
|
|
+ if (solve) {
|
|
|
|
+ // Keep size constrained to 32 saved grids
|
|
|
|
+ if (context.lastSolvedGrids.size() >= 32)
|
|
|
|
+ context.lastSolvedGrids.remove(0);
|
|
|
|
+
|
|
|
|
+ // Grid is updated by solver directly, so we can save the reference to the grid
|
|
|
|
+ context.lastSolvedGrids.add(grid);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- // Start solver
|
|
|
|
- GridSolverResult result = null;
|
|
|
|
- if (!PowerFlowAnalysisMenu.getInstance().areUpdatesDisabled()) {
|
|
|
|
- HolegPowerFlow powerFlow = new HolegPowerFlow();
|
|
|
|
- result = powerFlow.solve(grid, PowerFlowSettings.getDefault());
|
|
|
|
|
|
+ // Stop old solver
|
|
|
|
+ if (context != null && context.solverJob != null) {
|
|
|
|
+ try {
|
|
|
|
+ context.solverJob.interrupt();
|
|
|
|
+ context.solverJob.join(200);
|
|
|
|
+ }
|
|
|
|
+ catch(InterruptedException ignored) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ context.solverJob = null;
|
|
}
|
|
}
|
|
|
|
|
|
- // Decorate network
|
|
|
|
- decorateNetwork(minimumNetwork, iteration, flexManager, network, grid);
|
|
|
|
|
|
+ // Create solver job
|
|
|
|
+ Thread solverJob = new Thread(() -> {
|
|
|
|
+ long start = System.nanoTime();
|
|
|
|
+ GridSolverResult result = null;
|
|
|
|
+ if (solve) {
|
|
|
|
+ HolegPowerFlow powerFlow = new HolegPowerFlow();
|
|
|
|
+ result = powerFlow.solve(grid, PowerFlowSettings.getDefault());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Decorate network
|
|
|
|
+ decorateNetwork(minimumNetwork, iteration, flexManager, network, grid);
|
|
|
|
+ if (context != null)
|
|
|
|
+ context.solverTimeMilliseconds = (System.nanoTime() - start) / 1e6f;
|
|
|
|
+ SingletonControl.getInstance().getControl().calculateVisual();
|
|
|
|
+
|
|
|
|
+ // Show result
|
|
|
|
+ if (result != null && PowerFlowAnalysisMenu.getInstance().shouldShowResult())
|
|
|
|
+ SolveResultMessageBox.show(result);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
|
|
- // Show result
|
|
|
|
- if (result != null && PowerFlowAnalysisMenu.getInstance().shouldShowResult())
|
|
|
|
- SolveResultMessageBox.show(result);
|
|
|
|
|
|
+ // Wait or save solver job
|
|
|
|
+ try {
|
|
|
|
+ if (context == null || context.settings.waitForSolverJob) {
|
|
|
|
+ solverJob.start();
|
|
|
|
+ solverJob.join();
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ decorateNetwork(minimumNetwork, iteration, flexManager, network, grid);
|
|
|
|
+ context.solverJob = solverJob;
|
|
|
|
+ solverJob.start();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch(InterruptedException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|