Ngspice Polarity Question - Page 1
Forum section: electronics / beginners

author: eev_carl
author_rank: Regular Contributor
timestamp: November 18, 2017, 02:40:14 pm
content: Hi,I'm trying to solve a superposition problem with Ngspice but am having difficulty reconciling the node direction with my answer. I was wondering if someone could look over my Ngspice solution. In the attached photo of a schematic, I'm solving for the current across R2 (i2). According to the book and my manual solution, this is 2.15A. With Ngspice, I get 2.17A. The discrepancy is in whether to add or subtract the i2 from the voltage source.I can get 2.15A if I reverse my node order for vs (4 0 instead of 0 4). Using the node ids penciled in, I have the positive side of the current source at 1, negative at 0. The positive side of the voltage source is at 0 with the negative at 4.Here's my Ngspice solution (this adds the i2's together to get 2.17A)is 1 0 dc 5r1 2 1 560r2 0 2 470r3 3 2 270r4 0 3 100r5 4 3 720vs 0 4 dc 60Thanks in advance,Carl

author: hermit
author_rank: Frequent Contributor
timestamp: November 18, 2017, 05:05:18 pm
content: Quote from: eev_carl on November 18, 2017, 02:40:14 pmI don't know why but every spice example I've ever seen would use the 4 0 notation over the 0 4. I just peeked at the Ngspice manual and saw the same thing. I couldn't find a reason given but I didn't look real hard. You might download the manual and do some reading. The answer might be in there somewhere.

author: eev_carl
author_rank: Regular Contributor
timestamp: November 18, 2017, 05:14:17 pm
content: Thanks for replying. Would the "4 0" be different if the battery's polarity were reversed?

author: IanB
author_rank: Super Contributor
timestamp: November 18, 2017, 05:23:40 pm
content: Quote from: eev_carl on November 18, 2017, 05:14:17 pmChanging the statement from "vs 0 4" to "vs 4 0" by definition changes the source polarity (the orientation of the battery). You need to choose between the two alternatives following the spice documentation to get the polarity you intend.

author: eev_carl
author_rank: Regular Contributor
timestamp: November 18, 2017, 05:29:16 pm
content: If I have n+ as 1 and n- as 0 for the Is polarity, then should I have n+ as 0 and n- as 4 for the Vs polarity in the schematic? This is in accordance with the documentation, but I'm not getting the expected results of the current and voltage sources sending opposing currents across R2.

author: IanB
author_rank: Super Contributor
timestamp: November 18, 2017, 05:37:14 pm
content: Why don't you verify what your spice simulation is giving you by looking at the resulting voltages at each node? For example, assuming V0 is zero, is V4 equal to +60 V or -60 V? Similarly, is V1 positive or negative?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 18, 2017, 08:15:41 pm
content: The syntax is:<type><name> <from_node> <to_node> <value>So, IS is wrong, and VS is correct.You can also write "VS 4 0 DC -60", it doesn't matter to the circuit.If this were a conventional circuit, rather than a contrived homework problem,the sources would probably be acting in the positive direction, and in that case, the node voltages would all be positive, and the branch-to-ground currents would be expected to be positive as well. It's more typical to write "R2 2 0 470" for this reason, so that the computed r2is positive. (Note that a DC source, delivering power to the circuit, normally has negative current. If VS were a positive voltage from 4 to 0, then VSwould be negative.)So, whereas we eyeball the current paths, and remember the signs when summing up node currents for KCL, SPICE has a unique direction associated with each component, specified by the order the terminals are connected. If in doubt about a sign, check it against the node order in that component's statement.Tim

author: eev_carl
author_rank: Regular Contributor
timestamp: November 18, 2017, 08:35:56 pm
content: Does this look right for conventional current? The answer is what I expected (-2.15A) which is the Is current minus the Vs current.is 1 0 dc 5r1 1 2 560r2 2 0 470r3 2 3 270r4 3 0 100r5 3 4 720vs 4 0 dc 60If it looks good, could you explain why "vs 4 0 60" has 4 as the n+ argument and 0 as the n- argument? I keep wanting to put the + side of Vs at node 0.Thanks

author: IanB
author_rank: Super Contributor
timestamp: November 18, 2017, 09:52:53 pm
content: Quote from: eev_carl on November 18, 2017, 08:35:56 pmI think T3sl4co1l explained to you that you have "is" wrong. According to the arrow the current of 5 A goes from node 0 to node 1, so your statement should be: "is 0 1 dc 5". You have it reversed. If you write both "is 0 1 dc 5" and also "vs 0 4 dc 60" then you should get the 2.15 A that you expect.With the voltage, the potential difference of 60 V goes from node 0 to node 4, so the statement should be "vs 0 4 dc 60".

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 18, 2017, 10:16:17 pm
content: Since both sources are inverted now, the circuit polarity is wrong, but the magnitude is correct. Partial credit.Tim

author: eev_carl
author_rank: Regular Contributor
timestamp: November 18, 2017, 11:11:01 pm
content: This is working for me. I get a 2.15A result. I was assuming that n+ was the positive terminal and n- was the negative terminal. Thanks for the current flow explanation.is 0 1 dc 5r1 1 2 560r2 2 0 470r3 2 3 270r4 3 0 100r5 3 4 720vs 0 4 dc 60If anyone's interested, this is Superposition #3 in "Mastering the Art of DC Circuit Theory: A Practical Workbook for the Electronic Technician". The answer is completely worked out, but I'm interested in learning more about Ngspice.

author: IanB
author_rank: Super Contributor
timestamp: November 18, 2017, 11:32:01 pm
content: Quote from: eev_carl on November 18, 2017, 11:11:01 pmThat's cool, but you honestly made really, really hard work of this problem. You didn't need any explanations from us, you just needed to look at the resulting voltages at node 1 and node 4 in your simulation and then adjust the "vs" and "is" statements until the voltages had the expected signs. (I.e. node 1 should have a positive voltage and node 4 should have a negative voltage.)