Sunday, February 5, 2017

Releasing the book

I am happy to announce the release of my book "Simulating non-linear circuits with Python Power Electronics: an open source simulator based on Python". The book has been self-published and released on Gumroad. The link is:
To read a sample chapter, I have released Chapter 4 of the book on my website:
Follow my Facebook page for continuous updates:

The outline of the book is below:

Chapter 1 introduces the concept of simulation and describes the challenges in circuit simulation. Python Power Electronics as an open source circuit simulator is proposed with its objectives and target audience.

Chapter 2 provides an overview of the Python programming language. For a detailed tutorial, a reader is recommended to either read a book on Python programming or follow an online tutorial on the internet. The purpose of the chapter is to enable the reader to understand the code segments that will be provided in the subsequent chapters that deal with user-defined control functions and the case study.

Chapter 3 describes the interface that the simulator uses to interact with the user. The chapter describes the philosophy behind choosing spreadsheets as the mode of extracting information from the user. Spreadsheets are used by the user to enter simulation parameters,circuit schematics, parameters of the components in the circuit schematics and also the structure of control functions. The chapter describes how the structure of every component class in the simulator library and how the data entered by the user is processed by each component class. The chapter also describes the concept of how classes are instantiated for every component found resulting in objects and how these objects are referenced by the simulator. The chapter describes the execution flow in the simulator and how the simulator processes the data provided by the user and makes it available to the core simulation engine. The chapter does not describe how user-defined control functions are processed as the whole of Chapter 4 is dedicated for this purpose.

Chapter 4 describes how a user can write control functions for a simulation. Chapter 3 has described which of the circuit components can be controlled externally. Besides these controllable components, a control function need not perform a control action, but can instead be used to process simulation data or perform calculations. The chapter describes how the control functions have to be written as Python 2 files and specified in the simulation parameter spreadsheet. Every control function will have an interface to the simulation in terms of inputs and outputs and this interface is described by a spreadsheet called a descriptor. Besides inputs and outputs, every control function can use certain types of variables that perform special functions. The chapter describes the importance of each type of control variable and how they are implemented in the simulator. The chapter describes how control functions are scheduled by the simulator using time events and with an example, it is described how the simulator ensures that the control functions execute at the desired time instant. A simple example has been provided to describe how control functions can be interfaced with the simulation and also with each other.

Chapter 5 describes how a user can simulate a circuit with a power electronic converter. The example chosen has been a shunt connected three-phase VAR compensator realized using a two-level voltage source converter in a three-phase system. The voltage source converter consists of controllable ideal switches that are turned on and turned off by pulse width modulation. The chapter describes how the user can build this simulation in stages such that every new subsystem added to the circuit can be verified. The chapter also describes how the user can write control functions with detailed examples of each control function in the simulation and also design the control interfaces through descriptors. Every stage of the chapter contains simulation results to show how the project develops. Through this example, every feature of the simulator has been described with details so that users can develop their own simulations.

Chapter 6 describes how the simulator processes the circuit schematics that the user enters in spreadsheets. The connectivity information is extracted from the circuit schematics in the form of nodes, branches and loops. Nodes, branches and loops are used to perform circuit analysis through loop analysis and nodal analysis which are described in the next chapters. The chapter describes through sample circuits, the algorithms used to determine the nodes, branches and loops. The chapter introduces the concept of the LoopMap which is used for performing loop analysis in Chapter 7 and the concept of KCLBranchMap which is used for performing nodal analysis in Chapter 8.

Chapter 7 describes how loop analysis is performed in the simulator. The chapter describes how the matrix equation for performing loop analysis is generated from the LoopMap described in Chapter 6. A brief description is provided about how the matrices in this equation are transformed using row operations such that they can be solved by using numerical integration techniques. The chapter describes how loop currents and branch currents in the circuit can be mapped which allows for calculation of branch currents from loop currents and vice versa. The chapter describes with an example how time constants of branches of the circuit can make the simulation unstable and introduces the concept of a stiff loop. By providing a sample circuit and its corresponding LoopMap, the chapter describes the need to isolate stiff loops so as to be able to simulate a circuit. With this example, the concept of loop manipulations is described and with advanced examples, the effectiveness of the procedure is described. The chapter describes the limitation of loop analysis with another set of examples and therefore the need for nodal analysis.

Chapter 8 describes how nodal analysis can be used to determine the currents through stiff branches (that have a very low time constant) in the circuit. With the example of a simple buck converter, the chapter describes how loop analysis is insufficient in determining the conduction of power devices during switching events. The chapter then describes how nodal analysis can be used effectively in determining how power devices conduct and the transfer of current from one device to another. The chapter introduces the concept of events and how the matrix equations for the circuit will be constant until an event occurs. The chapter finally describes the logical flow of processes in the simulator as it performs loop analysis and nodal analysis one after the other.

Chapter 9 will conclude the book by highlighting the advantages of the simulator and the future development intended in this project.

Saturday, January 7, 2017

The book

Hope everyone had a good new year celebration.

This January, my goal is to complete the book for publication in February. So that means simulation of new circuits will come to a temporary stop. The first revision of the book is ready and for the table of contents check out:

A rigorous proof read is currently in progress and should be complete in two weeks. After that, another two weeks will be spent in editing the book, arranging the layout and ensuring a printable camera ready format.

The book will be self-published on Amazon using CreateSpace where they will print copies of the book on demand. Besides this I am also looking at other avenues where a simple PDF can be made available for those not wanting to buy a printed book but wanting simulation tutorials and code along with the pdf of the book. Still need to work out the details of that but that will probably follow only after the book has been released.

As for the book, I will be providing one chapter of the book on my site for download so that anyone can get a flavour of the book. In the meantime, check out the short papers that have been uploaded on the above link. For regular updates, follow my facebook page:

Let's hope for a good 2017!

Saturday, November 26, 2016

Nonstiff loops

There was an issue that I partly solved and didn't rigorously check before and this was related to how non-stiff loops are solved. Nonstiff loops are those loops that have only non-stiff branches and therefore these loop currents will be those that have non-negligible values. Their solution is therefore critical to the simulation stability and accuracy.

This is the problem. Let us suppose there are four branches in parallel. One of the branches is the load resistor and is therefore small in value. The others are parasitic resistances that may be significantly large. These parallel branches may be connected to a larger circuit with inductances. If loops are to be written having only one of these parallel branches but with the rest of the circuit, each branch will have a different time constant which is the sum total of the inductance of the loop divided by the sum total of the resistance. A loop can only be solved as a differential equation if the time constant of the loop is smaller than around ten times the simulation time step. Theoretically, by Nyquist's criterion this would be twice, but in practice we need a margin of ten. If the time constant if less than ten times the simulation time step, the equation should be converted to a static equation and the inductance should be ignored as solving this as a differential equation will cause instability.

Now when there are parallel branches with different resistances but none of these resistances are large enough for the branches to be considered stiff (like voltmeter branches), any of these branches can appear in loops with the external circuit. Loops are completely random and therefore it is possible that a nonstiff loop containing inductance is formulated with a branch with high resistance. Such a loop will be approximated to a static equation. If care is not taken in formulating the loops, it might so happen that none of these parallel branches will appear with the external circuit in a differential equation. This means that a dynamic of the circuit has disappeared.

To ensure this doesn't happen, we use the concept that any loop in a circuit must follow the path of least resistance. Also, we need to ensure that every branch appears in at least one loop. To do this, divide loops into two types - those with more than two branches and those with only two branches. For loops with more than two branches, these play an important part in linking a circuit together. These therefore capture the dynamics of a circuit. It is extremely important that as far as possible, they have a time constant that can be solved as a differential equation. It may be possible that this may not happen, time constant is too low and the equation is solved as a static equation. If this happens, it means it is a bad selection of parameters and simulation time step. But, to minimize this occurrence, go through every branch in these loops and check if the branch is one of parallel connected branches between nodes. If so, check if the branch has minimum resistance. If not, exchange the branch with the branch that has minimum resistance. By doing so, as far as possible, loops with more than two branches will always follow the path of least resistance and therefore will be solvable as differential equations.

For the loops with only two branches. These are loops between branches connected in parallel between two nodes. If a number of branches are connected in parallel, the branch with the minimum resistance should be found. All loops between the parallel branches should be so chosen that they are between this branch with minimum resistance and all other branches. The reason, this branch with minimum resistance will be present in loops which link the circuit together and described above and therefore will introduce their dynamics into the parallel connected branches.

Now, by arranging the loops according to their time constants, it can be ensured that the circuit is always stable and dynamics are always captured when possible.

Thursday, November 17, 2016

Python 3 compatibility

Last weekend I gave a ten minute talk in PyCon Canada 2016. As usual met some wonderful people and attended some interesting talks. The video will not be online for a few weeks.

As usual a lot of items on the agenda. The simplest is continue building for larger systems. The next is something I have been putting off for quite a while but think I should get down to pretty soon - compatibility with Python 3. So far the simulator used Python 2 but this is now legacy software. All Linux OS ship with Python 3 and even my IDE complains of syntax errors when there is nothing wrong with the syntax except that it looks for Python 3. Will need to read into the differences between Python 2 and 3 to make sure this transition is smooth.

The next thing is a little more complicated. I have been trying to speed the simulator up. One simple way to do so is to try to store as many of the loops and computations in a dictionary so that they can be looked up rather than calculated every time. The only question is what is the limit of this storage and will I ever run out of memory? I am assuming that a dictionary will be stored in the heap which is limited by the RAM which nowadays is good enough. If however, this dictionary gets stored in the stack, it would be a serious problem as the stack is limited and running out of memory is possible. I will need to look into some analytic tools that can tell me how my simulator is performing with respect to memory - which functions use the most?

As always, follow me on LinkedIn or my Facebook page for regular updates:

Saturday, October 15, 2016


Been a while since I posted. To begin with, I am almost done writing a book on the circuit simulator. I put up the table of contents of the first draft on my site:

It will take me another 2-4 weeks to complete the first draft with all chapters. I hope the book will be published by February next year.

I released several versions of the software:

As of now the simulator works with three inverters. I will continue expanding the system with an increasing number of inverters to push the simulator to its limits.

I am scheduled to give a 10 minute talk on the simulator in PyCon Canada next month. My talk is on November 12 at 2:05pm. So if you are attending PyCon Canada, it would be great to meet up anyone interested in the circuit simulator. The schedule for PyCon Canada is:

The talk will be recorded and a video will be put up online.

Follow my Facebook page for regular updates on the simulator:

Wednesday, August 10, 2016

Loop analysis

The last three releases of the circuit simulator were primarily dealing with errors in the loop manipulation technique used by the circuit solver. A brief background is that loop analysis is the backbone of the circuit simulator. Nodal analysis is also used but that is used to determine conducting paths in a non-linear circuit and to determine the currents through stiff branches. But the bulk of the simulator is the loop analysis.

The loops can be divided into stiff loops and non-stiff loops. The non-stiff loops are solved as ODEs while the stiff loops are mere static equations. After any event in the circuit, it is necessary to ensure that the non-stiff equations fully represent the circuit. This means that the if the circuit is divided into two parts - non-stiff branches and stiff branches - the number of loops formed by the non-stiff branches with their associated nodes should be included in the loop analysis. A failure to do so will cause the simulator to break as the simulator in that case is not solving all the equations that represent the circuit.

The first error was it was assumed that two loops could be manipulated and the result need only be checked to ensure all nodes occur twice. This meant there would be no stray branches or multiple loops. This is not entirely true. It is possible that manipulating two loops could result in multiple completely disjointed loops that satisfy the condition that all nodes occur only twice. So it became necessary to trace out the loop from an origin node back to this node. If more than one loop was found, the loop manipulation was not valid.

The second error was in assuming that after two loops are manipulated there is no need to check the directions. It is always possible to have conflicts in the directions of branches with two loops that are being manipulated. For example, say loops L1 and L2 are being manipulated. There loops have a branch (say B1) common between them and the branch is in the same direction of loops L1 and L2. It is also possible that the loops have another branch (say B2) common but the direction of this branch may be the opposite in loops L1 and L2. So, with such loops, computing sum or difference of the loops is not totally straightforward and after performing the loop manipulations, it becomes necessary to check the directions of the branches in the resultant loop to make sure no branch direction is wrong.

The third error was in how the number of non-stiff loops are ensured. The number of non-stiff loops can be determined by counting the non-stiff loops and the number of nodes that they map and the loops will be equal to B-N+1. However, what was done so far was to manipulate the stiff loops and try to triangularize the stiff branches in the loop map. This technique would work in the first attempt for smaller circuits but as the circuits became bigger, this didn't work. One reason, it is not necessary that two stiff loops are compatible. Therefore, two stiff loops may have the same stiff branch but there is no way to eliminate this stiff branch from one of the loops because the result of their manipulation is not a genuine loop. In such circumstances, it is necessary to continue the loop manipulations even with non-stiff loops if necessary until the stiff branches have been eliminated from a sufficient number of stiff loops. This is now being implemented as a boot strap technique, the loop manipulations are continuously repeated until the number of non-stiff loops in the loop map meet the expectations.

At this stage, the bugs have been fixed for a three-phase inverter with LCL filter connected to the grid and behaving as a VAR compensator. However, it is still not completely certain that all bugs have been fixed as it still appears from some loop maps that some stiff loops have stiff branches common between them. This may or may not be a problem. For now, the non-stiff loops are equal to the number expected. However, I expect another round of bug fixing coming up soon.

Sunday, July 3, 2016

Electrical machines

Been a while since I blogged, so I thought I should post. Most of my updates on the software is on my facebook page:

The latest case I posted was of a single-phase three winding transformer. I spent almost two weeks on this and most of that time was trying to build a transformer model taking into account the magnetic circuit of the transformer. A magnetic circuit would allow a detailed model to be built if necessary - magnetizing current, core losses etc. The problem was that modeling the magnetic circuit involved calculating the flux and from this flux the induced emfs.

flux1 = L1*i1 + Lm*i2
emf1 = d/dt (flux1)

Where i1 is the primary current, i2 is the secondary current, L1 is the inductance of the primary and Lm is the mutual inductance between the windings. The problem is with the differentiation. Once you perform a differentiation, you magnify any noise and errors in the simulation. Which also means, when a non-linear component such a diode rectifier is connected, the simulation becomes unstable. This is the reason why the test case considers a diode rectifier load as it was needed to check whether the simulation was stable with this.

The second point was the method of simulating the transformer. It would have been convenient for the user to simply add a single transformer component with connections rather than to have to connect all the components together and furthermore to take into consideration the polarity of the ammeters and voltmeters. As of now I can't think of a way of doing this.

An advantage of this form of transformer modeling could be stated that a user has more control over the connections. But only a small fraction of users would really need this level of control on the modeling of a single component and that too one as basic as a transformer. For most people a transformer is basically for providing isolation or transformation between two circuits at different voltages. Without a transformer, all components would need to be transformed to one of the terminals and that means conversion of the impedances etc which could be cumbersome.

This also brings up another issue. As the circuit grows bigger, it would always be advisable to test parts out separately before integrating them together. For example, consider the case of two three-phase voltage source converters operating in current control mode connected to another three-phase voltage source converter operating in voltage control mode. It would be advisable to test each converter out separately. The problem comes when you begin to put them together. Let's say inverter 1 and inverter 2 have been tested. When putting them together, inverter 2 will have to be copied into the same spreadsheet as inverter 1. This might mean that inverter will need to be moved to another part of the spreadsheet. And this implies having to change all the parameters of Inverter 2 such as polarity of switches and diodes which could only make this integration error-prone.

A way to minimize this is that there is no need to have the entire circuit in one spreadsheet. Say all three inverters were in separate spreadsheets while testing. After testing, they remain in separate spreadsheets but are only connected together by jump labels. This would mean the parameters of these three inverters in their own spreadsheet do not change. This could make the entire process of building up a circuit so much simpler.

And this in turn might to some extent solve the problem of modeling transformers and machines. If the transformer circuit and its parameter spreadsheet always remain separate, a user can just use ready made spreadsheets over and over and not bother about changing any parameters. Only jump labels need to be added to connect them to other spreadsheets.

So this will be the next major release. The simulator should be able to deal with a circuit in multiple spreadsheets. This might take me a while.