Thursday, July 26, 2018

Importing and exporting parameters in the Django web app

The main reason for creating the Django based web app was to have a single interface for most tasks related to simulating circuits. For example, in the command line interface, you would need to do the following:

1. Create circuit schematics in a spreadsheet.
2. Launch the circuit simulator in command line with "python circuit_solver.py"
3. Enter the simulation parameters in circuit_inputs.csv.
4. Enter the component parameters in the parameter spreadsheets.
5. Enter the control variables in the control descriptors.
6. When the simulation is running, run separate plot commands in either matplotlib or gnuplot to view results.

There are a few steps that are fundamental and unlikely to change. Those are circuits will be designed in circuit schematics with a spreadsheet software and saved as .csv files and control functions will have to be written in text files as .py files. But the command line needs a lot of back and forth between spreadsheets and also plotting the output needs a whole new set of commands and the need to learn gnuplot or matplotlib.

The Django web app simplified that. Only circuit schematics and control functions are still independent. All other parameters and commands can be given in the web interface by clicking on buttons and filling forms. Much more convenient without needing to go back and forth. However, the problem came while transferring simulations. Because now the simulation is stored in a SQLITE database and copy pasting the database will transfer the entire simulation collection rather than just what you want. What is needed is a way to extract the simulation as a set of simple files that can be zipped and copied or emailed.

What I have now added to the Django app has been an export button and an import button. After creating your simulation and checking that it runs the way you expect it to, you can click on export buttons on every circuit component parameters edit page and export all the parameters of that circuit schematic as a .csv file. Additionally, when you want to recreate a simulation in another computer or transfer it to someone else, you need to send the .csv files and use the import button to upload the parameter spreadsheet and copy the parameters to the database. The same process can be performed for control functions as well.

These features are available in version 2.0.7 for Python 2 and in version 4.0.3 for Python 3.
http://pythonpowerelectronics.com/contents/softwaredownloads.html

I am already finding this tool useful in maintaining simulations in all my computers and creating and arranging libraries of simulations. Try it out and let me know if you have any questions or comments. To know more about installing the software, check out my You Tube video:
https://www.youtube.com/watch?v=JRKUenYBIA4&t=9s
https://www.youtube.com/watch?v=jM28A2MD8u4&t=41s

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 settings.py 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 views.py 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.