As posted before, the only way to solve the problem of getting the diode to freewheel is by a nodal analysis. Essentially, the current through an inductor should not change instantaneously. If it does, it is bad design and I'll have to figure that out later. But in standard converter circuits, the inductor is expected to freewheel through a diode. As in the example of the buck converter.
So what I have coded is a function to perform nodal analysis take inductors as current sources. So I perform a dc analysis on a snapshot of the circuit at an instant and find out where the currents would flow if the inductor current were to remain constant.
Take the snapshot when the switch turns off with the inductor La having a non-zero current. So this current will flow through the switch and the diode almost equally since these are two high resistance paths.
With these currents calculated through nodal analysis, I try to figure out if any of the devices in the circuit has the capability to change its state. Since only nonlinear devices can do so, the remaining devices will have empty functions. The diode and switch will have the function called determine_state. Here is the code for the switch (click on "View Raw" at the bottom of the code box to see code in another window)
And here is the code for the nodal analysis (click on "View Raw" at the bottom of the code box to see code in another window)
Yes, it is a monster. Anyway, the basic concept is:
1. If a branch is not stiff and has an inductor, it becomes a current source.
2. If it has no impedance, the nodes on either end of the branch become super nodes.
3. If it only a resistance or is a stiff branch, it is expressed in KCL
4. Also if a branch has zero impedance, the current through it is treated as a current source.
5. Because of this, the only thing to be calculated are the resistive branches which is what needs to be done because that is where the non-linear devices will be.
6. The super node was a little tough to handle. When two nodes are connected by a branch with zero impedance, the current through that branch is tough to define. Therefore, the KCLs at the two nodes can be added up to form one KCL equation. And the second equation will be the voltage of the branch connecting these nodes.
7. In general, several nodes can be connected together to form a super node. Even worse, several groups of super nodes can exist.
8. Once the KCL equations are established, they need to reduced to upper triangular form and solved backwards. One node will become the reference node and will have zero voltage.
9. The branch currents will then be calculated from these node voltages. and there comes the next part of the story.
Because the nodal analysis could have changed the nature of the circuit by changing the state of nonlinear devices, it may so happen that the loop currents calculated before are not correct. So we need to recalculate the loop currents from the branch currents that the nodal analysis has given. The code for that is in compute_loop_currents (click on "View Raw" at the bottom of the code box to see code in another window)
This function looks to isolate branches to single loops and once that happens, the current of that branch becomes the loop current. If you are lucky, and in the case of the buck converter I was, the loops will be such that every non stiff loop will have a non stiff branch in that loop only and no other. Otherwise, a row operation is performed to isolate branches.
Anyway, long post but it is long overdue. The buck converter is now working. I am not sure if this will work in its present form for all power electronic converters. Need to test it thoroughly. But I'll release the next version soon.