-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInductor.java
More file actions
59 lines (56 loc) · 1.7 KB
/
Inductor.java
File metadata and controls
59 lines (56 loc) · 1.7 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
class Inductor {
public static final int FLAG_BACK_EULER = 2;
int nodes[];
int flags;
CirSim sim;
double inductance;
double compResistance, current;
double curSourceValue;
Inductor(CirSim s) {
sim = s;
nodes = new int[2];
}
void setup(double ic, double cr, int f) {
inductance = ic;
current = cr;
flags = f;
}
boolean isTrapezoidal() { return (flags & FLAG_BACK_EULER) == 0; }
void reset() {
current = 0;
}
void stamp(int n0, int n1) {
// inductor companion model using trapezoidal or backward euler
// approximations (Norton equivalent) consists of a current
// source in parallel with a resistor. Trapezoidal is more
// accurate than backward euler but can cause oscillatory behavior.
// The oscillation is a real problem in circuits with switches.
nodes[0] = n0;
nodes[1] = n1;
if (isTrapezoidal())
compResistance = 2*inductance/sim.timeStep;
else // backward euler
compResistance = inductance/sim.timeStep;
sim.stampResistor(nodes[0], nodes[1], compResistance);
sim.stampRightSide(nodes[0]);
sim.stampRightSide(nodes[1]);
}
boolean nonLinear() { return false; }
void startIteration(double voltdiff) {
if (isTrapezoidal())
curSourceValue = voltdiff/compResistance+current;
else // backward euler
curSourceValue = current;
}
double calculateCurrent(double voltdiff) {
// we check compResistance because this might get called
// before stamp(), which sets compResistance, causing
// infinite current
if (compResistance > 0)
current = voltdiff/compResistance + curSourceValue;
return current;
}
void doStep(double voltdiff) {
sim.stampCurrentSource(nodes[0], nodes[1], curSourceValue);
}
}