Author Topic: Ngspice Polarity Question  (Read 1919 times)

0 Members and 1 Guest are viewing this topic.

Offline eev_carlTopic starter

  • Regular Contributor
  • *
  • Posts: 224
  • Country: us
Ngspice Polarity Question
« on: November 18, 2017, 02:40:14 pm »
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 5
r1 2 1 560
r2 0 2 470
r3 3 2 270
r4 0 3 100
r5 4 3 720
vs 0 4 dc 60

Thanks in advance,
Carl
 

Offline hermit

  • Frequent Contributor
  • **
  • Posts: 482
  • Country: us
Re: Ngspice Polarity Question
« Reply #1 on: November 18, 2017, 05:05:18 pm »
I can get 2.15A if I reverse my node order for vs (4 0 instead of 0 4).
I 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. 
 

Offline eev_carlTopic starter

  • Regular Contributor
  • *
  • Posts: 224
  • Country: us
Re: Ngspice Polarity Question
« Reply #2 on: November 18, 2017, 05:14:17 pm »
Thanks for replying.  Would the "4 0" be different if the battery's polarity were reversed?
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11891
  • Country: us
Re: Ngspice Polarity Question
« Reply #3 on: November 18, 2017, 05:23:40 pm »
Thanks for replying.  Would the "4 0" be different if the battery's polarity were reversed?

Changing 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.
 

Offline eev_carlTopic starter

  • Regular Contributor
  • *
  • Posts: 224
  • Country: us
Re: Ngspice Polarity Question
« Reply #4 on: November 18, 2017, 05:29:16 pm »
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.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11891
  • Country: us
Re: Ngspice Polarity Question
« Reply #5 on: November 18, 2017, 05:37:14 pm »
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?
 
The following users thanked this post: eev_carl

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21686
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Ngspice Polarity Question
« Reply #6 on: November 18, 2017, 08:15:41 pm »
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 r2 is 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 VS would 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
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: eev_carl

Offline eev_carlTopic starter

  • Regular Contributor
  • *
  • Posts: 224
  • Country: us
Re: Ngspice Polarity Question
« Reply #7 on: November 18, 2017, 08:35:56 pm »
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 5
r1 1 2 560
r2 2 0 470
r3 2 3 270
r4 3 0 100
r5 3 4 720
vs 4 0 dc 60

If 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
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11891
  • Country: us
Re: Ngspice Polarity Question
« Reply #8 on: November 18, 2017, 09:52:53 pm »
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 5
r1 1 2 560
r2 2 0 470
r3 2 3 270
r4 3 0 100
r5 3 4 720
vs 4 0 dc 60

If 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

I 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".
 
The following users thanked this post: eev_carl

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21686
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Ngspice Polarity Question
« Reply #9 on: November 18, 2017, 10:16:17 pm »
Since both sources are inverted now, the circuit polarity is wrong, but the magnitude is correct.  Partial credit. :P

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: eev_carl

Offline eev_carlTopic starter

  • Regular Contributor
  • *
  • Posts: 224
  • Country: us
Re: Ngspice Polarity Question
« Reply #10 on: November 18, 2017, 11:11:01 pm »
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 5
r1 1 2 560
r2 2 0 470
r3 2 3 270
r4 3 0 100
r5 3 4 720
vs 0 4 dc 60

If 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.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11891
  • Country: us
Re: Ngspice Polarity Question
« Reply #11 on: November 18, 2017, 11:32:01 pm »
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.

That'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.)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf