Monday, July 2, 2018

Transformer circuits, Python 3/Django 2

I have been traveling since June 1 and have not been able to make any YouTube videos. I will get back to Canada on July 9 which is next week. The past month I have been trying out different circuits and also testing the migration to Python 3. The plan is to completely migrate both the command line and web app to Python 3 within six months as Python 2 is already legacy software.

While simulating a flyback converter, I found a bug in how I calculate the number of independent loops in a circuit. From fundamental network analysis, the number of loops is equal to B-N+1 where B is the number of branches, N the number of nodes. I never bothered to question where the "1" came from. The "1" stands for the number if connected sub-circuits. So, in a normal circuit without any transformer or any other machine that has electrically isolated parts, there is only one connected circuit. In such a case, the number of loops will be B-N+1.

But if the circuit has a transformer or a machine like an induction motor, there will be parts of the circuit that are electrically isolated from each other. So for a transformer with two windings, there will be two sub-circuits. In such as case, the number of independent loops will be B-N+2. In general, if there are M isolated sub-circuits, the number of loops will be B-N+M.

I was already calculating the number of connected sub-circuits by an iterative algorithm. Start with one branch and look for branches that are incident at either of it's nodes and keep growing the connected circuit. Count the number of connected circuits. This count is used to determine the number of independent loops that are expected so that additional loops will be considered linear combinations of the previous loops and can be deleted.

The next bug I found was with using the Django based web app with Python 3. Previously I was using Python 2.7 and Django 1.8. Both of these are grossly outdated. Latest version of Python is 3.6 and Django is 2.0.5. Turns out in versions of Django after 1.9, apps are not loaded when the server is started. An app is a component of the project which contains the view functions that define behaviour when a URL is accessed and also defines the database structure. In the circuit simulator, there is one app called simulations. This is listed in INSTALLED_APPS list in the file. Usually, Django will load all apps that are listed once the runserver command is executed. Turns out in Django 1.9 and later, this doesn't happen automatically. I guess this is to allow lazy loading. Which means, if an app is not used, no need to load it and burden the server.

So, if I run the server, everything works fine until I reach the stage of the running the simulation. At this point, it throws an error that apps are not loaded yet. To solve this, at the top of the file I need to add "import django" and "django.setup()". This causes the app to be loaded and now the simulator works as before.

It will be another week before I get back to Canada. Quite a few updates to the website are pending. Will be happy to get back with my Linux machine soon. Developing on Windows sucks.