Skip to content

Commit

Permalink
Fix generic types, MASSIVE code clean up, update librikka
Browse files Browse the repository at this point in the history
  • Loading branch information
rikka0w0 committed Jun 8, 2020
1 parent 33f1599 commit eccdac6
Show file tree
Hide file tree
Showing 117 changed files with 358 additions and 395 deletions.
2 changes: 1 addition & 1 deletion librikka
Submodule librikka updated 25 files
+2 −2 src/main/java/rikka/librikka/ByteSerializer.java
+2 −2 src/main/java/rikka/librikka/DirHorizontal8.java
+2 −2 src/main/java/rikka/librikka/RayTraceHelper.java
+8 −7 src/main/java/rikka/librikka/block/BlockBase.java
+2 −2 src/main/java/rikka/librikka/block/BlockUtils.java
+5 −4 src/main/java/rikka/librikka/container/ContainerBase.java
+6 −5 src/main/java/rikka/librikka/container/ContainerHelper.java
+2 −2 src/main/java/rikka/librikka/container/ContainerInventory.java
+4 −4 src/main/java/rikka/librikka/container/ContainerNoInventory.java
+3 −3 src/main/java/rikka/librikka/container/ContainerSynchronizer.java
+9 −10 src/main/java/rikka/librikka/gui/AutoGuiHandler.java
+1 −0 src/main/java/rikka/librikka/model/CodeBasedModel.java
+8 −6 src/main/java/rikka/librikka/model/loader/EasyTextureLoader.java
+6 −0 src/main/java/rikka/librikka/model/quadbuilder/BakedQuadHelper.java
+1 −1 src/main/java/rikka/librikka/model/quadbuilder/IRawElement.java
+4 −4 src/main/java/rikka/librikka/model/quadbuilder/IRawGroup.java
+2 −1 src/main/java/rikka/librikka/model/quadbuilder/IRawGroupWrapper.java
+1 −1 src/main/java/rikka/librikka/model/quadbuilder/IRawModel.java
+5 −3 src/main/java/rikka/librikka/model/quadbuilder/ITransformable.java
+0 −2 src/main/java/rikka/librikka/model/quadbuilder/MutableVertex.java
+17 −16 src/main/java/rikka/librikka/model/quadbuilder/RawQuadBase.java
+5 −5 src/main/java/rikka/librikka/model/quadbuilder/RawQuadGroup.java
+3 −4 src/main/java/rikka/librikka/multiblock/MultiBlockStructure.java
+2 −2 src/main/java/rikka/librikka/tileentity/TileEntityBase.java
+5 −4 src/main/java/rikka/librikka/tileentity/TileEntityHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface ISEEnergyNetAgent {
* @return A reference to ISESubComponent, which also implements the corresponding ISEComponentParameter.
* TileEntity should store this reference and use it to access simulation results, e.g. voltage.
*/
ISESubComponent newComponent(ISEComponentParameter dataProvider, TileEntity parent);
ISESubComponent<?> newComponent(ISEComponentParameter dataProvider, TileEntity parent);

/**
* Create a new cable component, WITHOUT register it.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/simelectricity/api/node/ISEPairedComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package simelectricity.api.node;

import javax.annotation.Nonnull;

public interface ISEPairedComponent<T extends ISEPairedComponent<?>> extends ISESubComponent<T> {
@Nonnull
T getComplement();
}
4 changes: 2 additions & 2 deletions src/main/java/simelectricity/api/node/ISESubComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
* API users should not implement this interface anywhere!
*/

public interface ISESubComponent<T extends ISESubComponent> extends ISESimulatable, ISEComponentParameter {
T getComplement();
public interface ISESubComponent<T extends ISESubComponent<?>> extends ISESimulatable, ISEComponentParameter {

}
2 changes: 1 addition & 1 deletion src/main/java/simelectricity/api/tile/ISETile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface ISETile {
/**
* @return the component on the given side, null if not applicable
*/
ISESubComponent getComponent(Direction side);
ISESubComponent<?> getComponent(Direction side);
}
25 changes: 9 additions & 16 deletions src/main/java/simelectricity/common/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import simelectricity.SimElectricity;
import simelectricity.energynet.EnergyNetSimulator;
import simelectricity.energynet.EnergyNet;
import simelectricity.energynet.EnergyNetAgent;
import simelectricity.essential.Essential;

@Mod.EventBusSubscriber(modid = SimElectricity.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
Expand All @@ -19,39 +20,31 @@ public class ConfigManager {

public static BooleanValue showDebugOutput_Spec;
public static BooleanValue showEnergyNetInfo_Spec;
public static ForgeConfigSpec.ConfigValue<String> matrixSolver_Spec;
public static ForgeConfigSpec.IntValue precision_Spec;
public static ForgeConfigSpec.IntValue maxIteration_Spec;
public static ForgeConfigSpec.IntValue shuntPN_Spec;
public static ForgeConfigSpec.ConfigValue<String> matrixSolver;
public static ForgeConfigSpec.IntValue precision;
public static ForgeConfigSpec.IntValue shuntPN;
public static ForgeConfigSpec.DoubleValue joule2rf;

public static boolean showDebugOutput;
public static boolean showEnergyNetInfo;
public static String matrixSolver;
public static int precision;
public static int maxIteration;
public static int shuntPN;

private static ConfigManager instace;
private static ForgeConfigSpec configSpec;

public static void register() {
Pair<ConfigManager, ForgeConfigSpec> specPair = new ForgeConfigSpec.Builder().configure(ConfigManager::new);
configSpec = specPair.getRight();
instace = specPair.getLeft();

ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ConfigManager.configSpec);
}

public static void syncConfig() {
showDebugOutput = showDebugOutput_Spec.get();
showEnergyNetInfo = showEnergyNetInfo_Spec.get();
matrixSolver = matrixSolver_Spec.get();
precision = precision_Spec.get();
maxIteration = maxIteration_Spec.get();
shuntPN = shuntPN_Spec.get();

EnergyNetSimulator.config();
EnergyNetAgent.mapping.values().forEach(EnergyNet::notifyConfigChanged);
}

@SubscribeEvent
Expand All @@ -67,10 +60,10 @@ private ConfigManager(ForgeConfigSpec.Builder builder) {

showDebugOutput_Spec = ForgeConfigHelper.boolVal(builder, SimElectricity.MODID, "Enable Debug Output", false, "Display debug information in the console, e.g. S->C sync notifications");
showEnergyNetInfo_Spec = ForgeConfigHelper.boolVal(builder, SimElectricity.MODID,"Show EnergyNet Info", false, "Display EnergyNet information in the console, e.g. tile attached/deteched/changed event");
matrixSolver_Spec = ForgeConfigHelper.stringVal(builder, SimElectricity.MODID,"Matrix Solver", "QR", "The preferred matrix solving algorithm (QR is much more effective than Gaussian.). Options: QR, Gaussian. Warning: CASE SENSITIVE!");
precision_Spec = ForgeConfigHelper.intVal(builder, SimElectricity.MODID, "Precision", 3, "3 means that the result is accurate up to 3 decimal places");
matrixSolver = ForgeConfigHelper.stringVal(builder, SimElectricity.MODID,"Matrix Solver", "QR", "The preferred matrix solving algorithm (QR is much more effective than Gaussian.). Options: QR, Gaussian. Warning: CASE SENSITIVE!");
precision = ForgeConfigHelper.intVal(builder, SimElectricity.MODID, "Precision", 3, "3 means that the result is accurate up to 3 decimal places");
maxIteration_Spec = ForgeConfigHelper.intVal(builder, SimElectricity.MODID, "Max iteration", 50, "To aviod infinite loop, the simualtor aborts the simulation when this threshold is reached");
shuntPN_Spec = ForgeConfigHelper.intVal(builder, SimElectricity.MODID, "RPN", 1000000000, "The resistance put in parallel with every PN junction, alleviate convergence issue");//
shuntPN = ForgeConfigHelper.intVal(builder, SimElectricity.MODID, "RPN", 1000000000, "The resistance put in parallel with every PN junction, alleviate convergence issue");//
joule2rf = ForgeConfigHelper.doubleVal(builder, Essential.MODID, "Joule to RF conversion ratio", 1, 0, Double.MAX_VALUE, "This number determines how many RF equal to 1 Joule");

builder.pop();
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/simelectricity/energynet/EnergyNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import net.minecraft.world.server.ServerWorld;
import simelectricity.api.node.ISESimulatable;
import simelectricity.common.ConfigManager;
import simelectricity.common.SELogger;
import simelectricity.energynet.components.SEComponent;
import simelectricity.energynet.components.SwitchA;
Expand Down Expand Up @@ -148,7 +147,7 @@ public String[] info() {
if (tileEntityGraph.size() == 0 && this.dataProvider.getGridObjectCount() == 0) {
return new String[]{
"EnergyNet is empty and idle",
"Matrix solving algorithsm: " + ConfigManager.matrixSolver
"Matrix solving algorithsm: " + this.simulator.getMatrixSolverName()
};
}

Expand All @@ -157,7 +156,7 @@ public String[] info() {
"EnergyNet is idle",
"Tiles: " + String.valueOf(tileEntityGraph.size()),
"Grid Objects: " + String.valueOf(this.dataProvider.getGridObjectCount()),
"Matrix solving algorithsm: " + ConfigManager.matrixSolver,
"Matrix solving algorithsm: " + this.simulator.getMatrixSolverName()
};
} else {
return new String[]{
Expand All @@ -167,7 +166,7 @@ public String[] info() {
"Matrix size: " + this.simulator.getMatrixSize(),
//"Non-zero elements: " + String.valueOf(this.matrix.getTotalNonZeros()),
"Density: " + this.simulator.getDensity() + "%",
"Matrix solving algorithsm: " + ConfigManager.matrixSolver,
"Matrix solving algorithsm: " + this.simulator.getMatrixSolverName(),
"Iterations:" + String.valueOf(iterations)
};
}
Expand All @@ -177,6 +176,10 @@ public void reFresh() {
this.scheduledRefresh = true;
}

public void notifyConfigChanged() {
this.simulator.setConfigChanged();
}

public void notifyServerShuttingdown() {
this.simulator.suicide();
}
Expand Down Expand Up @@ -216,4 +219,9 @@ public static final double getCurrentMagnitude(ISESimulatable Tile) {

return Double.NaN;
}

@Override
public String toString() {
return "EnergyNet for " + world.getProviderName();
}
}
5 changes: 2 additions & 3 deletions src/main/java/simelectricity/energynet/EnergyNetAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
import java.util.WeakHashMap;

public class EnergyNetAgent implements ISEEnergyNetAgent {
@SuppressWarnings("unchecked")
public static Map<World, EnergyNet> mapping = new WeakHashMap();
public static Map<World, EnergyNet> mapping = new WeakHashMap<>();

/**
* Return the instance of energyNet for a specific world,
Expand Down Expand Up @@ -138,7 +137,7 @@ else if (neighborTileEntity instanceof ISEWireTile)
}

@Override
public ISESubComponent newComponent(ISEComponentParameter dataProvider, TileEntity parent) {
public ISESubComponent<?> newComponent(ISEComponentParameter dataProvider, TileEntity parent) {
if (dataProvider instanceof ISEDiode)
//Create a pair of DiodeInput and DiodeOutput at the same time
return new DiodeInput((ISEDiode) dataProvider, parent);
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/simelectricity/energynet/EnergyNetDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class EnergyNetDataProvider extends WorldSavedData {
///////////////////////////////////////
/// GridTile update notification
///////////////////////////////////////
private final Set<ISEGridTile> updatedGridTile = new HashSet();
private final Set<ISEGridTile> updatedGridTile = new HashSet<>();
//Map between coord and GridNode
private final HashMap<BlockPos, GridNode> gridNodeMap = new HashMap<>();
private final List<TileEntity> loadedTiles = new LinkedList<>();
Expand Down Expand Up @@ -118,7 +118,7 @@ public void addTile(TileEntity te) {
ISETile tile = (ISETile) te;

for (Direction direction : Direction.values()) {
ISESubComponent subComponent = tile.getComponent(direction);
ISESubComponent<?> subComponent = tile.getComponent(direction);
if (subComponent != null) {
this.tileEntityGraph.addVertex((SEComponent) subComponent);
}
Expand All @@ -144,7 +144,7 @@ public void updateTileConnection(TileEntity te) {

if (cable.isGridInterConnectionPoint) {
GridNode gridNode = getGridObjectAtCoord(te.getPos());
this.tileEntityGraph.interconnection(cable, gridNode);
SEGraph.interconnection(cable, gridNode);
}

//Build connection with neighbors
Expand Down Expand Up @@ -181,7 +181,7 @@ public void updateTileConnection(TileEntity te) {
}
} else if (neighborTileEntity instanceof ISETile) {
ISETile tile = (ISETile) neighborTileEntity;
ISESubComponent component = tile.getComponent(direction.getOpposite());
ISESubComponent<?> component = tile.getComponent(direction.getOpposite());

if (component != null) {
this.tileEntityGraph.addEdge((SEComponent) component, cable);
Expand All @@ -192,7 +192,7 @@ public void updateTileConnection(TileEntity te) {
ISETile tile = (ISETile) te;

for (Direction direction : Direction.values()) {
ISESubComponent subComponent = tile.getComponent(direction);
ISESubComponent<?> subComponent = tile.getComponent(direction);
if (subComponent != null) {
this.tileEntityGraph.isolateVertex((SEComponent) subComponent);
}
Expand Down Expand Up @@ -250,7 +250,7 @@ public void updateTileConnection(TileEntity te) {
} else {
// ISETile
for (Direction direction : Direction.values()) {
ISESubComponent subComponent = tile.getComponent(direction);
ISESubComponent<?> subComponent = tile.getComponent(direction);
if (subComponent != null) {
TileEntity neighborTileEntity = getTileEntityOnDirection(te, direction);

Expand Down Expand Up @@ -282,10 +282,10 @@ public void updateTileParam(TileEntity te) {
} else if (te instanceof ISETile) {
ISETile tile = (ISETile) te;
for (Direction direction : Direction.values()) {
ISESubComponent subComponent = tile.getComponent(direction);
ISESubComponent<?> subComponent = tile.getComponent(direction);

if (subComponent instanceof Tile)
((Tile) subComponent).updateComponentParameters();
((Tile<?>) subComponent).updateComponentParameters();
}
} else {
throw new RuntimeException("Unexpected TileEntity:" + te);
Expand All @@ -303,7 +303,7 @@ public void removeTile(TileEntity te) {
} else if (te instanceof ISETile) {
ISETile tile = (ISETile) te;
for (Direction direction : Direction.values()) {
ISESubComponent subComponent = tile.getComponent(direction);
ISESubComponent<?> subComponent = tile.getComponent(direction);
if (subComponent != null) {
this.tileEntityGraph.removeVertex((SEComponent) subComponent);
}
Expand Down Expand Up @@ -425,7 +425,6 @@ public void onGridTileInvalidate(TileEntity te) {

this.loadedGridTiles.remove(te);

ISEGridTile gridTile = (ISEGridTile) te;
GridNode gridObject = this.gridNodeMap.get(te.getPos());

//gridObject can be null if the GridObject is just removed
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/simelectricity/energynet/EnergyNetSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import simelectricity.common.SELogger;
import simelectricity.energynet.components.*;
import simelectricity.energynet.matrix.IMatrixSolver;
import simelectricity.energynet.matrix.IMatrixSolver.MatrixHelper;

import java.util.Iterator;
import java.util.LinkedList;
Expand All @@ -16,20 +15,33 @@ public class EnergyNetSimulator extends Thread {
/**
* The absolute tolerance
*/
private static double epsilon;
private double epsilon;
/**
* The conductance placed between each PN junction(to alleviate convergence problem)
*/
private static double Gpn;
private double Gpn;
/**
* Matrix solving algorithm used to solve the problem
*/
private static IMatrixSolver matrix;
private IMatrixSolver matrix;

public static final void config() {
epsilon = Math.pow(10, -ConfigManager.precision);
Gpn = 1.0D / ConfigManager.shuntPN;
matrix = MatrixHelper.newSolver(ConfigManager.matrixSolver);
public String getMatrixSolverName() {
return matrix.getClass().getSimpleName();
}

private volatile boolean configChanged;
public void setConfigChanged() {
configChanged = true;
}

private void checkAndUpdateConfig() {
if (configChanged) {
configChanged = false;

epsilon = Math.pow(10, -ConfigManager.precision.get());
Gpn = 1.0D / ConfigManager.shuntPN.get();
matrix = IMatrixSolver.newSolver(ConfigManager.matrixSolver.get());
}
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -121,6 +133,11 @@ public void run() {
try {
SELogger.logInfo(SELogger.simulator, this.getName() + " wake up");

if (this.matrix == null)
this.setConfigChanged();

checkAndUpdateConfig();

processing = true;
SELogger.logInfo(SELogger.simulator, this.getName() + " Started");
startAt = System.currentTimeMillis();
Expand Down Expand Up @@ -169,7 +186,7 @@ private final void calcCurrents(double[] voltages, double[] currents, Iterator<S


if (columnNode instanceof CableBase) {
CableBase cableBase = (CableBase) columnNode;
CableBase<?> cableBase = (CableBase<?>) columnNode;

if (cableBase.hasShuntResistance())
currents[columnNode.index] -= voltages[cableBase.index] / cableBase.getShuntResistance();
Expand Down Expand Up @@ -320,7 +337,7 @@ private final void formJacobian(double[] voltages, Iterator<SEComponent> iterato

//Cable - GridNode
if (columnNode instanceof CableBase) {
CableBase cableBase = (CableBase) columnNode;
CableBase<?> cableBase = (CableBase<?>) columnNode;

if (cableBase.hasShuntResistance())
diagonalElement += 1.0D / cableBase.getShuntResistance();
Expand Down Expand Up @@ -366,7 +383,7 @@ private final void formJacobian(double[] voltages, Iterator<SEComponent> iterato
if (gridNode.type == GridNode.ISEGridNode_TransformerSecondary) {
GridNode sec = gridNode;
GridNode pri = sec.complement;
double ratio = pri.ratio;
// double ratio = pri.ratio;
double res = pri.resistance;

diagonalElement += 1.0D / res;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/simelectricity/energynet/SEGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static boolean shouldCalcVoltage(SEComponent node) {
if (SEGraph.isInterconnectionTerminal(node)) //A interconnection terminal
return true;

if (node instanceof CableBase && ((CableBase) node).hasShuntResistance())
if (node instanceof CableBase && ((CableBase<?>) node).hasShuntResistance())
return true;

return node instanceof GridNode && ((GridNode) node).type != GridNode.ISEGridNode_Wire;
Expand All @@ -59,9 +59,9 @@ private static boolean isInterconnectionTerminal(SEComponent node) {

public static double calcR(SEComponent cur, SEComponent neighbor) {
if (cur instanceof CableBase) {
CableBase curConductor = (CableBase) cur;
CableBase<?> curConductor = (CableBase<?>) cur;
if (neighbor instanceof CableBase) {
return curConductor.getResistance() + ((CableBase) neighbor).getResistance();
return curConductor.getResistance() + ((CableBase<?>) neighbor).getResistance();
} else {
return curConductor.getResistance();
}
Expand All @@ -74,7 +74,7 @@ public static double calcR(SEComponent cur, SEComponent neighbor) {
}
} else {
if (neighbor instanceof CableBase) {
return ((CableBase) neighbor).getResistance();
return ((CableBase<?>) neighbor).getResistance();
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public void removeVertex(SEComponent node) {
public void isolateVertex(SEComponent node) {
//Cut possible interconnection
if (node instanceof Cable)
this.breakInterconnection((Cable) node);
SEGraph.breakInterconnection((Cable) node);

//Remove this node from its neighbor list
for (SEComponent neighbor : node.neighbors) {
Expand Down Expand Up @@ -195,7 +195,7 @@ public LinkedList<GridNode> removeGridVertex(GridNode gridNode) {
return null;

//Cut possible interconnection
this.breakInterconnection(gridNode);
SEGraph.breakInterconnection(gridNode);

//Break transformer
this.breakTransformer(gridNode);
Expand Down
Loading

0 comments on commit eccdac6

Please sign in to comment.