-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapacitorElm.java
More file actions
137 lines (132 loc) · 4.23 KB
/
CapacitorElm.java
File metadata and controls
137 lines (132 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.awt.*;
import java.util.StringTokenizer;
class CapacitorElm extends CircuitElm {
double capacitance;
double compResistance, voltdiff;
Point plate1[], plate2[];
public static final int FLAG_BACK_EULER = 2;
public CapacitorElm(int xx, int yy) {
super(xx, yy);
capacitance = 1e-5;
}
public CapacitorElm(int xa, int ya, int xb, int yb, int f,
StringTokenizer st) {
super(xa, ya, xb, yb, f);
capacitance = new Double(st.nextToken()).doubleValue();
voltdiff = new Double(st.nextToken()).doubleValue();
}
boolean isTrapezoidal() { return (flags & FLAG_BACK_EULER) == 0; }
void setNodeVoltage(int n, double c) {
super.setNodeVoltage(n, c);
voltdiff = volts[0]-volts[1];
}
void reset() {
current = curcount = 0;
// put small charge on caps when reset to start oscillators
voltdiff = 1e-3;
}
int getDumpType() { return 'c'; }
String dump() {
return super.dump() + " " + capacitance + " " + voltdiff;
}
void setPoints() {
super.setPoints();
double f = (dn/2-4)/dn;
// calc leads
lead1 = interpPoint(point1, point2, f);
lead2 = interpPoint(point1, point2, 1-f);
// calc plates
plate1 = newPointArray(2);
plate2 = newPointArray(2);
interpPoint2(point1, point2, plate1[0], plate1[1], f, 12);
interpPoint2(point1, point2, plate2[0], plate2[1], 1-f, 12);
}
void draw(Graphics g) {
int hs = 12;
setBbox(point1, point2, hs);
// draw first lead and plate
setVoltageColor(g, volts[0]);
drawThickLine(g, point1, lead1);
setPowerColor(g, false);
drawThickLine(g, plate1[0], plate1[1]);
if (sim.powerCheckItem.getState())
g.setColor(Color.gray);
// draw second lead and plate
setVoltageColor(g, volts[1]);
drawThickLine(g, point2, lead2);
setPowerColor(g, false);
drawThickLine(g, plate2[0], plate2[1]);
updateDotCount();
if (sim.dragElm != this) {
drawDots(g, point1, lead1, curcount);
drawDots(g, point2, lead2, -curcount);
}
drawPosts(g);
if (sim.showValuesCheckItem.getState()) {
String s = getShortUnitText(capacitance, "F");
drawValues(g, s, hs);
}
}
void stamp() {
// capacitor companion model using trapezoidal approximation
// (Norton equivalent) consists of a current source in
// parallel with a resistor. Trapezoidal is more accurate
// than backward euler but can cause oscillatory behavior
// if RC is small relative to the timestep.
if (isTrapezoidal())
compResistance = sim.timeStep/(2*capacitance);
else
compResistance = sim.timeStep/capacitance;
sim.stampResistor(nodes[0], nodes[1], compResistance);
sim.stampRightSide(nodes[0]);
sim.stampRightSide(nodes[1]);
}
void startIteration() {
if (isTrapezoidal())
curSourceValue = -voltdiff/compResistance-current;
else
curSourceValue = -voltdiff/compResistance;
//System.out.println("cap " + compResistance + " " + curSourceValue + " " + current + " " + voltdiff);
}
void calculateCurrent() {
double voltdiff = volts[0] - volts[1];
// we check compResistance because this might get called
// before stamp(), which sets compResistance, causing
// infinite current
if (compResistance > 0)
current = voltdiff/compResistance + curSourceValue;
}
double curSourceValue;
void doStep() {
sim.stampCurrentSource(nodes[0], nodes[1], curSourceValue);
}
void getInfo(String arr[]) {
arr[0] = "capacitor";
getBasicInfo(arr);
arr[3] = "C = " + getUnitText(capacitance, "F");
arr[4] = "P = " + getUnitText(getPower(), "W");
//double v = getVoltageDiff();
//arr[4] = "U = " + getUnitText(.5*capacitance*v*v, "J");
}
public EditInfo getEditInfo(int n) {
if (n == 0)
return new EditInfo("Capacitance (F)", capacitance, 0, 0);
if (n == 1) {
EditInfo ei = new EditInfo("", 0, -1, -1);
ei.checkbox = new Checkbox("Trapezoidal Approximation", isTrapezoidal());
return ei;
}
return null;
}
public void setEditValue(int n, EditInfo ei) {
if (n == 0 && ei.value > 0)
capacitance = ei.value;
if (n == 1) {
if (ei.checkbox.getState())
flags &= ~FLAG_BACK_EULER;
else
flags |= FLAG_BACK_EULER;
}
}
int getShortcut() { return 'c'; }
}