Fix x position of last solution segment in Serway and Jewett v8's 25.36.
[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 -= 1.5u;
69
70 write("positioning");
71 TwoTerminal Spos = source((u,v), 90, DC, "DC", "5 V");
72 TwoTerminal Rpos = resistor((0,0), 0, normal, "+offset", "5 \ohm", draw=false);
73 Rpos.centerto(Spos.beg, Spos.end, offset=u);
74 Rpos.draw();
75 dot("Sa", Spos.beg, S);
76 dot("Sb", Spos.end, N);
77 dot("Ra", Rpos.beg, S);
78 dot("Rb", Rpos.end, N);
79 TwoTerminal Cpos = capacitor((0,0), 0, normal, "-2offset", "4 F",draw=false);
80 Cpos.centerto(Spos.beg, Spos.end, offset=-2u);
81 Cpos.draw();
82 v -= u;
83
84 write("wires");
85 dot("nsq", (0,v), N);
86 dot("b", (u/2,v-u/2), S);
87 wire((0,v), (u/2,v-u/2), nsq);
88 dot("udsq", (u,v), N);
89 dot("b", (3u/2,v-u/2), S);
90 wire((u,v), (3u/2,v-u/2), udsq);
91 dot("rlsq", (2u,v), N);
92 dot("b", (5u/2,v-u/2), S);
93 wire((2u,v), (5u/2,v-u/2), rlsq);
94 dot("udsq w/d", (3u,v), N);
95 dot("b", (7u/2,v-u/2), S);
96 wire((3u,v), (7u/2,v-u/2), udsq, -u/4);
97 dot("rlsq w/d", (4u,v), N);
98 dot("b", (9u/2,v-u/2), S);
99 wire((4u,v), (9u/2,v-u/2), rlsq, u/4);
100 v -= u;
101
102 write("circuit");
103 v -= u/2;  // this circuit takes up more height than the previous lines.
104 TwoTerminal Ccirc = capacitor((0,v), ang=90);
105 TwoTerminal Rcirc = resistor(draw=false);
106 Rcirc.centerto(Ccirc.beg, Ccirc.end);
107 Rcirc.shift((u,0));  // gratuitous use of shift, but whatever...
108 Rcirc.draw();
109 // In the following, we assume the resistor is longer than the
110 // capacitor.  If that is not true, you can find the corners of the
111 // circuit programatically and use those corners.
112 wire(Rcirc.end, Ccirc.end, rlsq);
113 wire(Rcirc.beg, Ccirc.beg, rlsq);
114 pair Pcirc[] = {(Ccirc.beg.x,Rcirc.beg.y), (Ccirc.end.x,Rcirc.end.y),
115                 Rcirc.end, Rcirc.beg};
116 kirchhoff_loop(Pcirc);
117 v -= u;