-
Notifications
You must be signed in to change notification settings - Fork 3
/
CapacitorElm.java
179 lines (166 loc) · 5.45 KB
/
CapacitorElm.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright (C) Paul Falstad and Iain Sharp
Modified by Vinyasi on 23/Nov/2017 15:28
// Mod.Begin
// Mod.End
This file is part of CircuitJS1.
CircuitJS1 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
CircuitJS1 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CircuitJS1. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lushprojects.circuitjs1.client;
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);
// Mod.Begin
// capacitance = 1e-5;
// 680 micro Farads is more harmonious, ie resonant, than 10 micro Farads by default.
capacitance = 68*1e-5;
// Mod.End
}
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() {
super.reset();
current = curcount = curSourceValue = 0;
// put small charge on caps when reset to start oscillators
// Mod.Begin
// voltdiff = 1e-3; // this is very inconvenient for me ...
voltdiff = 0;
// Mod.End
}
int getDumpType() { return 'c'; }
String dump() {
return super.dump() + " " + capacitance + " " + voltdiff;
}
Point platePoints[];
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);
if (platePoints == null)
drawThickLine(g, plate2[0], plate2[1]);
else {
int i;
for (i = 0; i != 7; i++)
drawThickLine(g, platePoints[i], platePoints[i+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;
}
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");
}
@Override
String getScopeText(int v) {
return sim.LS("capacitor") + ", " + getUnitText(capacitance, "F");
}
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'; }
}