44bfc7cdca00d5853f063eef8e4f3860053f6e92
[course.git] / asymptote / Circ-test.asy
1 /* Test suite for Circ.asy.
2  *
3  * Copyright (C) 2008-2009 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 TwoTerminal Rn = resistor((0,v), 0, normal, "$R_{normal}$", "30\ohm");
26 dot("beg", Rn.beg, NW, red);
27 dot("end", Rn.end, NE, red);
28 dot("mid", Rn.mid, S, red);
29 TwoTerminal Rvb = resistor((2u,v), 0, variable, "$R_{variable}$", "30\kohm");
30 v -= u;
31
32 write("capacitor");
33 TwoTerminal Cn = capacitor((0,v), 0, normal, "$C_{normal}$", "30 $\mu$F");
34 TwoTerminal Ce = capacitor((u,v), 0, electrolytic, "$C_{electrolytic}$", "30 $\mu$F");
35 TwoTerminal Cvb = capacitor((2u,v), 0, variable, "$C_{variable}$", "30 $\mu$F");
36 TwoTerminal Cvt = capacitor((3u,v), 0, variant, "$C_{variant}$", "30 $\mu$F");
37 v -= u;
38
39 write("inductor");
40 TwoTerminal Lup = inductor((0,v), 0, Up, "$L_{Up}$", "30 H");
41 TwoTerminal Ldown = inductor((u,v), 0, Down, "$L_{Down}$", "30 H");
42 v -= u;
43
44 write("diode");
45 TwoTerminal Dn = diode((0,v), 0, normal, "$D_{normal}$", "1.3 V");
46 TwoTerminal Dz = diode((u,v), 0, zener, "$D_{zener}$", "1.3 V");
47 TwoTerminal Dled = diode((2u,v), 0, LED, "$D_{LED}$", "1.7 V");
48 v -= u;
49
50 write("battery");
51 TwoTerminal B = battery((0,v), 0, "Battery", "1.5 V");
52 v -= u;
53
54 write("switch");
55 TwoTerminal B = switchSPST((0,v), 0, NO, "$S_{NO}$", "Open");
56 TwoTerminal B = switchSPST((u,v), 0, NC, "$S_{NC}$", "Closed");
57 v -= u;
58
59 write("current");
60 TwoTerminal Icurr = current((0,v), 0, "I", "10 A");
61 v -= u;
62
63 write("source");
64 TwoTerminal Sdc = source((0,v), 0, DC, "DC", "5 V");
65 TwoTerminal Sac = source((u,v), 0, AC, "AC", "5 V$_{pp}$");
66 TwoTerminal Si = source((2u,v), 0, I, "I", "5 A");
67 TwoTerminal Sv = source((3u,v), 0, V, "V", "5 V");
68 v -= u;
69
70 write("lamp");
71 TwoTerminal Ln = lamp((0,v), 0, normal, "indicator", "5\ohm");
72 TwoTerminal Li = lamp((u,v), 0, illuminating, "illuminator", "5\ohm");
73 v -= 1.5u;
74
75 write("positioning");
76 TwoTerminal Spos = source((u,v), 90, DC, "DC", "5 V");
77 TwoTerminal Rpos = resistor((0,0), 0, normal, "+offset", "5 \ohm", draw=false);
78 Rpos.centerto(Spos.beg, Spos.end, offset=u);
79 Rpos.draw();
80 dot("Sa", Spos.beg, S);
81 dot("Sb", Spos.end, N);
82 dot("Ra", Rpos.beg, S);
83 dot("Rb", Rpos.end, N);
84 TwoTerminal Cpos = capacitor((0,0), 0, normal, "-2offset", "4 F",draw=false);
85 Cpos.centerto(Spos.beg, Spos.end, offset=-2u);
86 Cpos.draw();
87 v -= u;
88
89 write("wires");
90 dot("nsq", (0,v), N);
91 dot("b", (u/2,v-u/2), S);
92 wire((0,v), (u/2,v-u/2), nsq);
93 dot("udsq", (u,v), N);
94 dot("b", (3u/2,v-u/2), S);
95 wire((u,v), (3u/2,v-u/2), udsq);
96 dot("rlsq", (2u,v), N);
97 dot("b", (5u/2,v-u/2), S);
98 wire((2u,v), (5u/2,v-u/2), rlsq);
99 dot("udsq w/d", (3u,v), N);
100 dot("b", (7u/2,v-u/2), S);
101 wire((3u,v), (7u/2,v-u/2), udsq, -u/4);
102 dot("rlsq w/d", (4u,v), N);
103 dot("b", (9u/2,v-u/2), S);
104 wire((4u,v), (9u/2,v-u/2), rlsq, u/4);
105 v -= u;
106
107 write("circuit");
108 v -= u/2;  // this circuit takes up more height than the previous lines.
109 TwoTerminal Ccirc = capacitor((0,v), ang=90);
110 TwoTerminal Rcirc = resistor(draw=false);
111 Rcirc.centerto(Ccirc.beg, Ccirc.end);
112 Rcirc.shift((u,0));  // gratuitous use of shift, but whatever...
113 Rcirc.draw();
114 // In the following, we assume the resistor is longer than the
115 // capacitor.  If that is not true, you can find the corners of the
116 // circuit programatically and use those corners.
117 wire(Rcirc.end, Ccirc.end, rlsq);
118 wire(Rcirc.beg, Ccirc.beg, rlsq);
119 pair Pcirc[] = {(Ccirc.beg.x,Rcirc.beg.y), (Ccirc.end.x,Rcirc.end.y),
120                 Rcirc.end, Rcirc.beg};
121 kirchhoff_loop(Pcirc);
122 v -= u;