Skip to content

Commit

Permalink
A large commit that cannot be described in one sentence except by say…
Browse files Browse the repository at this point in the history
…ing we were using telegram most of the time to collaborate on code.
  • Loading branch information
hamza-algohary committed Jan 9, 2023
1 parent dfcebca commit 2b9e213
Show file tree
Hide file tree
Showing 32 changed files with 774 additions and 844 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
bin/
bin/
old/
images/
Binary file added img/Wire2.1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/Wire3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/Wire4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/jchart2d.jar
Binary file not shown.
Binary file added lib/jide-oss-2.9.7.jar
Binary file not shown.
Binary file added lib/xmlgraphics-commons-1.3.1.jar
Binary file not shown.
4 changes: 1 addition & 3 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
build:
-mkdir bin
javac -cp ".:./lib/ejml.jar" src/*.java -d bin/
run:
java -cp ".:./lib/ejml.jar" bin/Main
javac -cp ".:./lib/ejml.jar:./lib/jchart2d.jar" src/*.java -d bin/
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
cd bin
java -cp ".:../lib/ejml.jar" Main
java -cp "../lib/ejml.jar:../lib/jchart2d-3.2.2.jar" Main
1 change: 1 addition & 0 deletions src/Capacitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public double getValue(){
}
@Override
public void setState(double I , double v1 , double v2){
super.setState(I, v1, v2);
System.out.println("!!!!!!!!!!!!!!!!!!!!! I = "+I+" !!!!!!!!!!!!!!!!");
if(!simulator.isDelayedComputation())
this.q += I*simulator.getTimeStepSecond();
Expand Down
33 changes: 20 additions & 13 deletions src/CircuitSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
public class CircuitSimulator{
public Component components[] = new Component[0];
public Node nodes[] = new Node[0];
int currentTime_ms = 0;
int timeStep_ms = 1;
double currentTime_ms = 0;
double timeStep_ms = 0.005;
boolean delayedComputation = false;
public boolean isDelayedComputation(){return delayedComputation;}
public boolean hasDelayedComponents(){
Expand All @@ -22,8 +22,8 @@ public boolean hasDelayedComponents(){
this.currentTime_ms += timeStep_ms;
return solve();
}
public CircuitSimulator(Component components[])throws Exception{
convertWiresToNodes(components);
public CircuitSimulator(Component components[]){
//convertWiresToNodes(components);
this.components = components;
for(Component component : this.components){
component.simulator = this;
Expand All @@ -32,9 +32,9 @@ public CircuitSimulator(Component components[])throws Exception{
}
//public CircuitSimulator(){}

private void convertWiresToNodes(Component components[])throws Exception{
/*private void convertWiresToNodes(Component components[])throws Exception{
for(Component component : components){
if(component.type == Component.Type.W && component.getValue()<0.00000001){
if(component.getType() == Component.Type.W && component.getValue()<0.00000001){
for(int i=1 ;i<component.points.size();i++){
renameNodeInComponents(components, component.points.get(0), component.points.get(i));
}
Expand All @@ -47,10 +47,10 @@ private void convertWiresToNodes(Component components[])throws Exception{
}
renameNodeInComponents(components, component.points.get(0), point);
}*/
}
/* }
}
}
private void renameNodeInComponents(Component components[] , Point toBeRenamed , Point newName){
}*/
/*private void renameNodeInComponents(Component components[] , Point toBeRenamed , Point newName){
for(Component component : components){
for(Point point : component.points){
//System.out.println(point.x);
Expand All @@ -64,10 +64,10 @@ private void renameNodeInComponents(Component components[] , Point toBeRenamed ,
}
}
}
}
}*/
public void selectRandomGround(){
for(Component c : components){
if( c.type == Component.Type.V ){
if( c.getType() == Component.Type.V ){
c.isGround = true;
break;
}
Expand All @@ -81,7 +81,8 @@ public Equation[] getEquations()throws Exception{
equationsVector.add(node.getEquation());
}
for(Component component : components){
equationsVector.addAll(new Vector<Equation>(Arrays.asList(component.getEquations())));
if(component.hasEquation())
equationsVector.addAll(new Vector<Equation>(Arrays.asList(component.getEquations())));
}
Equation groundEquation = new Equation();
groundEquation.constant = 0;
Expand All @@ -98,7 +99,13 @@ public Equation[] getEquations()throws Exception{
LinearSystem system = new LinearSystem(getEquations());
varNames = system.solve();
for(Component component : components){
component.setState(varNames.get(component.getIName()), varNames.get(component.getVstartName()), varNames.get(component.getVendName()));
/*component.setState(
varNames.get(component.getIName()),
varNames.get(component.getVstartName()),
varNames.get(component.getVendName()));*/
component.setState(Utils.getKey(varNames, component.getIName(), 0.0),
Utils.getKey(varNames, component.getVstartName(), 0.0),
Utils.getKey(varNames, component.getVendName(), 0.0));
}
delayedComputation = true;
}
Expand Down
18 changes: 18 additions & 0 deletions src/CircularArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Vector;

public class CircularArray <T>{
Vector<T> ints = new Vector<>();
void setVector(Vector<T> ints){
this.ints = ints;
}
Vector<T> getVector(int begin , int size){
Vector<T> result = new Vector<>();
for(int i=begin ; i< ints.size() ;i++){
result.add(ints.get(i));
}
for(int i=0 ; i<begin ; i++){
result.add(ints.get(i));
}
return Utils.shortenVector(result , size);
}
}
37 changes: 24 additions & 13 deletions src/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ public Component(Point[] points , Double args[]){
this.args = Arrays.asList(args);
this.points = Arrays.asList(points);
}
private double stateI = 0 , stateV = 0;
public double getI(){
return stateI;
}
public double getV(){
return stateV;
}
// returns true if they weren't initially sorted, false otherwise.
/*protected boolean sortStartAndEnd(){
if(getStart().x > getEnd().x || getStart().y > getEnd().y){
Expand All @@ -25,7 +32,7 @@ protected boolean isSorted(){
return true;
}
public String imageName = "";
enum Type{IV , V ,I , W}
enum Type{IV , V ,I , OTHER}
Type type;
boolean isGround = false;
public Component(){}
Expand All @@ -35,6 +42,9 @@ public Component(){}
//public void setEnd(Point p){points.set(1 , p);}
//public Point start = new Point();
//public Point end = new Point();
public Type getType(){
return type;
}
public List<Point> points = new LinkedList<>(); //Arrays.asList(new Point[10]);
public List<Double> args = new LinkedList<>(); //Arrays.asList(new Double[10]);
/*static Component constructComponent(String name , double args[]){
Expand All @@ -58,13 +68,13 @@ public boolean isDelayed(){
}
CircuitSimulator simulator;
boolean hasVar(){
return type.equals(Type.IV) || type.equals(Type.V);
return getType().equals(Type.IV) || getType().equals(Type.V);
}
boolean hasEquation(){
return type.equals(Type.IV) || type.equals(Type.V);
return getType().equals(Type.IV) || getType().equals(Type.V);
}
boolean hasConstant(){
return type.equals(Type.I);
return getType().equals(Type.I);
}
/*public String getBranchName(){
return getStart().toString() + ":" + getEnd().toString();
Expand All @@ -73,10 +83,11 @@ public double getValue() throws Exception{
throw new Exception("Empty Component");
}
public void setState(double I , double v1 , double v2){

stateI = I;
stateV = v1 - v2;
}
public double getConstant(Point currentNode)throws Exception{
if(!type.equals(Type.I))
if(!getType().equals(Type.I))
throw new Exception("Non-I components don't have constants");
if(currentNode.equals(getEnd())){
return getValue();
Expand All @@ -85,18 +96,18 @@ public double getConstant(Point currentNode)throws Exception{
}
}
public Variable getVar(Point currentNode)throws Exception{
if(type == Type.I)
if(getType() == Type.I)
throw new Exception("Invalid Operation on non-IV component.");
String name = "";
double value = 0;
if(type == Type.IV){
if(getType() == Type.IV){
value = -1/getValue();
if(currentNode.equals(getStart())){
name = getVendName();
}else{
name = getVstartName();
}
}else if(type == Type.V){
}else if(getType() == Type.V){
name = getIName();
if(currentNode.equals(getStart())){
value = /*getValue()*/1;
Expand All @@ -109,7 +120,7 @@ public Variable getVar(Point currentNode)throws Exception{
}
public Equation[] getEquations()throws Exception{
Equation equation = new Equation();
switch(type){
switch(getType()){
case IV:
equation.constant = 0;
equation.vars.add(new Variable(getIName(), isSorted()?-1:1));
Expand All @@ -127,17 +138,17 @@ public Equation[] getEquations()throws Exception{
Equation equations[] = new Equation[]{equation};
return equations;
}
public String getIName(){
public final String getIName(){
//return "I"+getBranchName();
if(isSorted()){
return "I"+getStart()+":"+getEnd();
}
return "I"+getEnd()+":"+getStart();
}
public String getVstartName(){
public final String getVstartName(){
return "V"+getStart();
}
public String getVendName(){
public final String getVendName(){
return "V"+getEnd();
}
}
23 changes: 23 additions & 0 deletions src/ComponentFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class ComponentFactory {
public static Component construct(String name , Point[] targetNodes , Double args[]) throws Exception {
if(Constants.R.compareTo(name) == 0){
return new Resistance(targetNodes, args);
}else if(Constants.DC_V.compareTo(name) == 0){
return new DC_V(targetNodes, args);
}else if(Constants.DC_I.compareTo(name) == 0){
return new DC_I(targetNodes, args);
}else if(Constants.AC_V.compareTo(name) == 0){
return new AC_V(targetNodes, args);
}else if(Constants.AC_I.compareTo(name) == 0){
return new AC_I(targetNodes, args);
}else if(Constants.C.compareTo(name) == 0){
return new Capacitor(targetNodes, args);
}else if(Constants.D.compareTo(name) == 0){
return new Diode(targetNodes , args);
}else if(Constants.W.compareTo(name) == 0){
return new DC_V(targetNodes, new Double[]{0.0});
}else{
throw new Exception("Unknown Component\n");
}
}
}
Loading

0 comments on commit 2b9e213

Please sign in to comment.