Skip to content

Commit

Permalink
avoid most of analyzeCircuit logic when simulation is stopped (sharpi…
Browse files Browse the repository at this point in the history
  • Loading branch information
pfalstad committed Nov 2, 2024
1 parent 30e6184 commit 74ec288
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/com/lushprojects/circuitjs1/client/CirSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@ public void updateCircuit() {
stampCircuit();
} catch (Exception e) {
stop("Exception in stampCircuit()", null);
GWT.log("Exception in stampCircuit", e);
}
perfmon.stopContext();
}
Expand Down Expand Up @@ -1973,7 +1974,6 @@ class NodeMapEntry {
}
// map points to node numbers
HashMap<Point,NodeMapEntry> nodeMap;
HashMap<Point,Integer> postCountMap;

class WireInfo {
CircuitElm wire;
Expand Down Expand Up @@ -2183,8 +2183,6 @@ void makeNodeList() {
// allocate a node for each post and match posts to nodes
for (j = 0; j != posts; j++) {
Point pt = ce.getPost(j);
Integer g = postCountMap.get(pt);
postCountMap.put(pt, g == null ? 1 : g+1);
NodeMapEntry cln = nodeMap.get(pt);

// is this node not in map yet? or is the node number unallocated?
Expand Down Expand Up @@ -2384,7 +2382,8 @@ boolean validateCircuit() {
return true;
}

// analyze the circuit when something changes, so it can be simulated
// analyze the circuit when something changes, so it can be simulated.
// Most of this has been moved to preStampCircuit() so it can be avoided if the simulation is stopped.
void analyzeCircuit() {
stopMessage = null;
stopElm = null;
Expand All @@ -2393,19 +2392,23 @@ void analyzeCircuit() {
badConnectionList = new Vector<Point>();
return;
}
makePostDrawList();

needsStamp = true;
}

boolean preStampCircuit() {
int i, j;
nodeList = new Vector<CircuitNode>();
postCountMap = new HashMap<Point,Integer>();

calculateWireClosure();
setGroundNode();

// allocate nodes and voltage sources
makeNodeList();

makePostDrawList();
if (!calcWireInfo())
return;
return false;
nodeMap = null; // done with this

int vscount = 0;
Expand Down Expand Up @@ -2440,7 +2443,7 @@ void analyzeCircuit() {

findUnconnectedNodes();
if (!validateCircuit())
return;
return false;

nodesWithGroundConnectionCount = nodesWithGroundConnection.size();
// only need this for validation
Expand All @@ -2450,10 +2453,14 @@ void analyzeCircuit() {
needsStamp = true;

callAnalyzeHook();
return true;
}

// stamp the matrix, meaning populate the matrix as required to simulate the circuit (for all linear elements, at least)
void stampCircuit() {
if (!preStampCircuit())
return;

int i;
int matrixSize = nodeList.size()-1 + voltageSourceCount;
circuitMatrix = new double[matrixSize][matrixSize];
Expand Down Expand Up @@ -2635,6 +2642,18 @@ boolean simplifyMatrix(int matrixSize) {
// others should be drawn. We can't use the node list for this purpose anymore because wires
// have the same node number at both ends.
void makePostDrawList() {
HashMap<Point,Integer> postCountMap = new HashMap<Point,Integer>();
int i, j;
for (i = 0; i != elmList.size(); i++) {
CircuitElm ce = getElm(i);
int posts = ce.getPostCount();
for (j = 0; j != posts; j++) {
Point pt = ce.getPost(j);
Integer g = postCountMap.get(pt);
postCountMap.put(pt, g == null ? 1 : g+1);
}
}

postDrawList = new Vector<Point>();
badConnectionList = new Vector<Point>();
for (Map.Entry<Point, Integer> entry : postCountMap.entrySet()) {
Expand All @@ -2644,7 +2663,6 @@ void makePostDrawList() {
// look for bad connections, posts not connected to other elements which intersect
// other elements' bounding boxes
if (entry.getValue() == 1) {
int j;
boolean bad = false;
Point cn = entry.getKey();
for (j = 0; j != elmList.size() && !bad; j++) {
Expand All @@ -2667,7 +2685,6 @@ void makePostDrawList() {
badConnectionList.add(cn);
}
}
postCountMap = null;
}

class FindPathInfo {
Expand Down
40 changes: 40 additions & 0 deletions src/com/lushprojects/circuitjs1/client/IntPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.lushprojects.circuitjs1.client;

import java.util.Objects;

public class IntPair {
private final int first;
private final int second;

public IntPair(int first, int second) {
this.first = first;
this.second = second;
}

public int getFirst() {
return first;
}

public int getSecond() {
return second;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IntPair intPair = (IntPair) o;
return first == intPair.first && second == intPair.second;
}

@Override
public int hashCode() {
return Objects.hash(first, second);
}

@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
}

0 comments on commit 74ec288

Please sign in to comment.