[Ktechlab-devel] Calculating the currents in a circuit

P Zoltan zoltan.padrah at gmail.com
Sun Oct 3 21:16:59 UTC 2010


   Hello,

  I've looked into the problem of finding the currents in a circuit, on the  
minimalistic simulator branch.

  Because I couldn't understand the existing code, I've looked at the  
previous, working version.

  There the following is the idea:
- solve the circuit with modified nodal analysis
- after having a solution, calculate the currents in the following way:

  - call a calculateCurrents() method on all the elements. This way the  
currents flowing from the elements into the CNodes are calculated, but  
they are stored in the elements. The CNodes are not a good storage for the  
currents, because one CNode is associated with more elements.

See the orignal method, for a resistance, here, at the end of the file:

http://ktechlab.svn.sourceforge.net/viewvc/ktechlab/branches/ktechlab-0.3.7/src/electronics/simulation/resistance.cpp?revision=172&view=markup

  - next, these values for currents are tranferred from the elements, to  
the pins, by the component::setNodelCurrents() method, line 152:

http://ktechlab.svn.sourceforge.net/viewvc/ktechlab/branches/ktechlab-0.3.7/src/electronics/component.cpp?revision=172&view=markup

   From there, calculating the currents throught the wires is easy.

  So, in order to have a reliable current calculation, I'd like to apply  
the ideas from the version 0.3.7. The following changes are needed:

- add a method to Element:
	double nodeCurrent(int nodeNumber) const ;
  and implement it, by using a doube m_nodeCurrent[4]; protected member.

- in all the lements, updaete the m_nodeCurrent member, in the  
updateCurrents() method, instead of updating the node currents. As  
explained earlier, the node currents will get updated by many elements, so  
setting the current there is meaningless.

- add a double sourceCurrent() const; method to the Pin class, and a  
setter method for it. The returned value should be interpreted as the  
current flowing from the element into the pin. If the pin has no attached  
element, then this value must be 0. A currentIsKnown() method already  
exists, so that can be also reused.

- i don't really understand why is the ElementMap a separate class. Maybe  
someone can explain it. That class is a helper for Element, that connects  
the Element to Pins. Wouldn't it be more simple to make the element aware  
of pins? I won't research that, but a wrapper class is needed that sets up  
an element and tranfers the value of currents into the pins.

- after having all these in place, the void Circuit::updateCurrents()  
method could be updated such that calculate all the currents flowing from  
the elements in the pins, and also all the currents flowing through the  
wires.

  Any comments, ideas and suggestions are welcome.

   Zoltan




More information about the Ktechlab-devel mailing list