[Ktechlab-devel] no more errors SVN - revision 491
David Saxton
david at bluehaze.org
Wed Sep 28 13:26:07 UTC 2005
On Wednesday 28 September 2005 00:07, Eric Ensmann wrote:
> I'm thinking about building an inductor component but currently I don't
> know where to start. Perhaps I get an idea of it tomorrow..
Excellent :) You'll need to start by adding code for simulating inductance
(the rest after that is easy).
As an example, I'll explain how capacitance is simulated. Have a look at
src/electronics/simulation/capacitance.cpp
Here, the capacitance formula "I = C (dV/dt)" is simulated using numerical
integration. At the moment, it uses the backwards euler method (it should
really be using trapezoidal method, but I haven't written that yet....).
By using a small time step (m_delta = h), the above formula is approximated by
I_(n+1) = C * ( V_(n+1) - V_n ) / h
(where the "V_n" is the current voltage, "V_(n+1)" is the voltage at (current
time)+(time step), and "I_(n+1)" is the current flowing into the capacitor at
(current time)+(time step). Google for "numerical integration" if the above
formula doesn't make sense.
Capacitance can be simulated as a current source in parallel with resistance -
and so the above formula is written as (turn on fixed width fonts...):
I_(n+1) = (C / h) * V_(n+1) - (C * V_n / h)
I_(n+1) = g_eq * V_(n+1) - I_eq
where g_eq is the conductance of the resistor, and I_eq is the current of the
current source.
This boils down to a few lines of code in Capacitance::time_step():
double v = p_cnode[0]->v-p_cnode[1]->v;
g_eq_new = m_cap/m_delta;
i_eq_new = -v*g_eq_new;
The rest of the code in that function is just plugging the values into the
simulation matrix for simulating as a resistance and a current source in
parallel. The simulator uses modified nodal analysis to simulate the circuit
(google for more information).
For the inductor, you'll need to do similar for the inductance formula
"V = L (dI/dt)" formula. Then, you'll need to make the inductance property
available for use by adding the right functions to the Component class
(src/electronics/component.[h/cpp]), and then writing an inductor component
that draws itself and uses the inductance property.
Any questions - I'm more than happy to help :)
Regards,
David Saxton
More information about the Ktechlab-devel
mailing list