Convert TwoTerminal to a more general MultiTerminal in Circ.asy.
[course.git] / asymptote / Circ-test.asy
1 /* Test suite for Circ.asy.
2  *
3  * Copyright (C) 2008-2012 W. Trevor King <wking@drexel.edu>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 import Circ;
21
22 real u = 2cm, v=0;
23
24 write("resistor");
25 MultiTerminal Rn = resistor(beg=(0,v), dir=0, type=normal,
26     label=Label("$R_{normal}$", align=N), value=Label("$30\ohm$", align=S));
27 dot("beg", Rn.terminal[0], NW, red);
28 dot("end", Rn.terminal[1], NE, red);
29 dot("center", Rn.center, S, red);
30
31 MultiTerminal Rvb = resistor(beg=(2u,v), dir=0, type=variable,
32     label=Label("$R_{variable}$", align=NW),
33     value=Label("$30\kohm$", align=SE));
34 v -= u;
35
36 write("capacitor");
37 MultiTerminal Cn = capacitor((0,v), type=normal, "$C_{normal}$",
38     "30 $\mu$F");
39 MultiTerminal Ce = capacitor((u,v), type=electrolytic, "$C_{electrolytic}$",
40     "30 $\mu$F");
41 MultiTerminal Cvb = capacitor((2u,v), type=variable, "$C_{variable}$",
42     "30 $\mu$F");
43 MultiTerminal Cvt = capacitor((3u,v), type=variant, "$C_{variant}$",
44     "30 $\mu$F");
45 v -= u;
46
47 write("inductor");
48 MultiTerminal Lup = inductor((0,v), type=Up, "$L_{Up}$", "30 H");
49 MultiTerminal Ldown = inductor((u,v), type=Down, "$L_{Down}$", "30 H");
50 v -= u;
51
52 write("diode");
53 MultiTerminal Dn = diode((0,v), type=normal, "$D_{normal}$", "1.3 V");
54 MultiTerminal Dz = diode((u,v), type=zener, "$D_{zener}$", "1.3 V");
55 MultiTerminal Dled = diode((2u,v), type=LED, "$D_{LED}$", "1.7 V");
56 v -= u;
57
58 write("battery");
59 MultiTerminal B = battery((0,v), "Battery", "1.5 V");
60 v -= u;
61
62 write("switch");
63 MultiTerminal So = switchSPST((0,v), type=open, "$S$", "Open");
64 MultiTerminal Sc = switchSPST((u,v), type=closed, "$S$", "Closed");
65 MultiTerminal SPDT_o = switchSPDT((2u,v), type=open, "$S$", "Open");
66 MultiTerminal SPDT_a = switchSPDT((3u,v), type=closed_a, "$S$", "Closed-A");
67 MultiTerminal SPDT_b = switchSPDT((4u,v), type=closed_b, "$S$", "Closed-B");
68 v -= u;
69
70 write("current");
71 MultiTerminal Icurr = current((0,v), "I", "10 A");
72 v -= u;
73
74 write("source");
75 MultiTerminal Sdc = source((0,v), type=DC, "DC", "5 V");
76 MultiTerminal Sac = source((u,v), type=AC, "AC", "5 V$_{pp}$");
77 MultiTerminal Si = source((2u,v), type=I, "I", "5 A");
78 MultiTerminal Sv = source((3u,v), type=V, "V", "5 V");
79 v -= u;
80
81 write("lamp");
82 MultiTerminal Ln = lamp((0,v), 0, normal, "indicator", "5\ohm");
83 MultiTerminal Li = lamp((u,v), 0, illuminating, "illuminator", "5\ohm");
84 v -= 1.5u;
85
86 write("positioning");
87 MultiTerminal Spos = source((u,v), dir=90, type=DC, "DC", "5 V");
88 MultiTerminal Rpos = resistor(type=normal,
89     label=rotate(90)*Label("+offset", align=W),
90     value=rotate(90)*Label("5 \ohm", align=E), draw=false);
91 Rpos.centerto(Spos.terminal[0], Spos.terminal[1], offset=u);
92 Rpos.draw();
93 dot("Sa", Spos.terminal[0], S);
94 dot("Sb", Spos.terminal[1], N);
95 dot("Ra", Rpos.terminal[0], S);
96 dot("Rb", Rpos.terminal[1], N);
97 MultiTerminal Cpos = capacitor(type=normal, "-2offset", "4 F",draw=false);
98 Cpos.centerto(Spos.terminal[0], Spos.terminal[1], offset=-2u);
99 Cpos.draw();
100 v -= u;
101
102 write("wires");
103 dot("nsq", (0,v), N);
104 dot("b", (u/2,v-u/2), S);
105 wire((0,v), (u/2,v-u/2), nsq);
106 dot("udsq", (u,v), N);
107 dot("b", (3u/2,v-u/2), S);
108 wire((u,v), (3u/2,v-u/2), udsq);
109 dot("rlsq", (2u,v), N);
110 dot("b", (5u/2,v-u/2), S);
111 wire((2u,v), (5u/2,v-u/2), rlsq);
112 dot("udsq w/d", (3u,v), N);
113 dot("b", (7u/2,v-u/2), S);
114 wire((3u,v), (7u/2,v-u/2), udsq, -u/4);
115 dot("rlsq w/d", (4u,v), N);
116 dot("b", (9u/2,v-u/2), S);
117 wire((4u,v), (9u/2,v-u/2), rlsq, u/4);
118 v -= u;
119
120 write("circuit");
121 v -= u/2;  // this circuit takes up more height than the previous lines.
122 MultiTerminal Ccirc = capacitor((0,v), dir=90);
123 MultiTerminal Rcirc = resistor(draw=false);
124 two_terminal_centerto(Ccirc, Rcirc);
125 Rcirc.shift((u,0));  // gratuitous use of shift, but whatever...
126 Rcirc.draw();
127 // In the following, we assume the resistor is longer than the
128 // capacitor.  If that is not true, you can find the corners of the
129 // circuit programatically and use those corners.
130 wire(Rcirc.terminal[1], Ccirc.terminal[1], rlsq);
131 wire(Rcirc.terminal[0], Ccirc.terminal[0], rlsq);
132 pair Pcirc[] = {
133   (Ccirc.terminal[0].x,Rcirc.terminal[0].y),
134   (Ccirc.terminal[1].x,Rcirc.terminal[1].y),
135   Rcirc.terminal[1], Rcirc.terminal[0]};
136 kirchhoff_loop(Pcirc);
137 v -= u;