Thursday, November 19, 2015

Three-phase standalone inverter

So lets start the simulation series with a standalone three-phase inverter - basically the ends of the filter shorted together. This is the screenshot of the .csv file.



The parameters of the inverter are in this screenshot



A few things to note:
  1. The voltage source is just 10V because with a short circuit, that's all I need to drive a large current. A dc voltage is specified by making the ac peak 0 and adding a dc offset. The reason is that the peak automatically means an ac signal of frequency specified in the next column.
  2. The switches S1 to S6 are shown to have control signals as s1gate to s6gate. However, when the parameter file is initialized, these values are arbitrary and may even be the same. The control signals can't be the same! The problem is not from the simulator point of view but for the control code. Each switch needs a unique control tag. So change as needed.

Before continuing, these are the main inputs the simulator wants from the user:

------------------------------------------------------------------------------------------------------------------------------

$ python circuit_solver.py
CSV file containing the network layout --> standalone_inv

Enter parameters in file standalone_inv_params.csv. When done, close the file and press y and enter to continue -> y 

**************************************************
Resistor is Cdc = 0.001100 located at 5C
Resistor is Ra = 0.100000 located at 26AA
Resistor is Rc = 0.100000 located at 12AA
Voltage Source is Cdc of 0.000000 V(peak), 0.000000 Hz(frequency), 0.000000 (degrees phase shift) and 10.000000 dc offset located at 9C with positive polarity towards 8C
Ammeter is Ac located at 12U with positive polarity towards 12V
Ammeter is Aa located at 26U with positive polarity towards 26V
Inductor is Lc = 0.001000 located at 12X
Inductor is La = 0.001000 located at 26X
Diode is D2 located at 18H with cathode polarity towards 17H
Diode is D4 located at 18L with cathode polarity towards 17L
Diode is D6 located at 18P with cathode polarity towards 17P
Switch is S4 located at 17J with negative polarity towards 18J
Switch is S6 located at 17N with negative polarity towards 18N
Voltmeter is Vdc located at 11A with positive polarity towards 10A
Inductor is Lb = 0.001000 located at 19X
Switch is S1 located at 6F with negative polarity towards 7F
Diode is D1 located at 6H with cathode polarity towards 5H
Switch is S2 located at 17F with negative polarity towards 18F
Switch is S3 located at 6J with negative polarity towards 7J
Diode is D3 located at 6L with cathode polarity towards 5L
Switch is S5 located at 6N with negative polarity towards 7N
Diode is D5 located at 6P with cathode polarity towards 5P
Ammeter is Ab located at 19U with positive polarity towards 19V
Resistor is Rb = 0.100000 located at 19AA
************************************************** 

Enter the control files. Omit the .py extension and just leave spaces between files --> currcont_3ph
Enter control parameters in the following files -->
currcont_3ph_desc.csv

Enter control code in the following files -->
currcont_3ph.py
When ready press y and enter to continue -> y

**************************************************
Meters are in the following sequence: ['12U', '19U', '26U', '11A']
Control variables to be plotted are in the following sequence
['control_signal1', 'control_signal3', 'control_signal2', 'control_signal4']
**************************************************
Simulation time step in seconds (recommended <= 10.0e-6) --> 1.0e-6
Duration of simulation in seconds --> 0.1
Data storage time step in seconds ( >= 0.000001) --> 1.0e-6
Output data file name (.dat extension will be added) --> ckt_out 

Data stored in ckt_out.dat. Check output log file output_log.txt for description of output
258.522240877
------------------------------------------------------------------------------------------------------------------------------

A quick description:

  1. Execute circuit_solver.py. Make sure all the simulator files and .csv files are in the same folder.
  2. It asks you for the circuit layout which is a csv file. In this case it is standalone_inv.csv but the .csv is dropped in the input as that is added automatically.
  3. If the network layout has just been created, the simulator will read all the components in the layout and will insert them into the parameter file (shown above) with default parameters. These default parameters are almost definitely not the ones you would like to use, so open the standalone_inv_params.csv file, make changes, save it and close it.
  4. It prints out the parameters for one last confirmation.
  5. Next it asks for control files. Now, these need not be control files but can also be signal processing or any processing file like heat/loss calculation etc. So you do not need to have controlled components to have a control file. Add these control files.
  6. Again for each control file the simulator creates a default "descriptor" file as a .csv file. More on this later. The default will have one input (usually a meter). If you would like to use another input or have more inputs, change or copy that row and make changes. There will be one output which will be a controlled entity if there is one. There is one time event. And there is a static variable. And there is one variable storage. More on these later.
  7. It asks you to make sure control code is ready in the control files.
  8. Then outputs the manner in which the output is saved in the data file. Here the first column is always the time so the other columns start from 2 onwards.
  9. Then the next columns will be the stored variables. More on these later.
  10. Next inputs are the time step (1 microsec), duration of simulation run (0.1 sec), storage time step (1 microsec but can be higher) and the name of the output data file.
  11. On completion tells you how long the simulation took to run.

Now about the descriptor file currcont_3ph_desc.csv


  1. In this case, the inputs are the three ammeter outputs. The next column are the names of the variables by which these ammeter outputs will be available in the control code. The simulator makes these meter outputs available with these variable names so you can directly use in the control code. Will be shown.
  2. The outputs are shown next. The first three columns are from the parameter file. The next two are specific to the control file. The desired variable name will be assigned to the control outputs at the end so the user can assign any value to the control outputs and they will be passed on. And finally an initial output.
  3. The next set are the static variables, these are the variables whose values need to be stored from one iteration to the next. Any variable not declared here but used in the control function is a local variable to that control function. It will exist only in that particular iteration and will cease to exist when the control is executed. So for any values that need to be stored between iterations like itegrals in PI controllers etc, use static variables. In general unless sure that you don't need to store a variable, declare it as a static variable.
  4. Two time events are generated - t1, and tcarr. t1 is the time step of the control algorithm. tcarr is the time step of generation of the carrier wave and comparison with the modulation to generate the control signals.
  5. The last are the control_signals 1 to 4. These are control signals that can be plotted for debugging like modulation signals, integrals, error signals etc. A note - these control signals are common to all control functions in a simulation. So declaring the same control signal for two control functions is a violation. Also, this is a way to share control signals between control functions. Similar to routing signals between blocks.

Finally the control code:



The control code shows the closed loop control using a PI controller with d-q transformation. Take a moment to find out which variables have not been declared static and the reason why. For that matter, all the variables used can be declared static. Python does not need variables to be declared and variables come into existence when they are used for the first time. So for example dt_sample=100.0e-6 is when dt_sample is declared.


A few results. First one is the current waveforms. The next one is the tracking performance. The reference for d is 10 and q is 0. From waveforms, the actual currents are begin to track the references.





Anyway, that's all for now folks. A long post after a long time.





Continuing with the simulator

Been ages since I posted. Lots of changes - new job, moved to another city and a host of others. Programming has been happening slow and steady. As for this blog and the project in general, I am thinking of taking a slightly different course.

So far have been posting code. From now on I will post simulation results. The purpose is that I would like to create a running document with applications rather than just code. This will be of more use to power electronics engineers than mere code based on the simulator.

So keep visiting this page :)

Friday, October 10, 2014

Releasing version 0.2.5

Releasing the version 0.2.5:
http://sourceforge.net/projects/pythonpowerelec/


Thursday, October 9, 2014

Next update

A loooong time since I posted. Made a huge number of changes to the simulator. It is now working for a circuit like a three-phase diode rectified feeding a dc capacitor that is in turn the dc bus for a three-phase 6 switch inverter that has an LC filter. So I feel most of the major bugs have been ironed out.

Anyway, I will be releasing the next version of the simulator tomorrow. But before I end the day, I would like to ask a question to some of the readers of the blog.

Since, documenting this project has been a major pain - this blog is the only thing I have and I am quite glad I did at least that. But this blog too is a mess and I need something a little more systematic. So I was thinking of writing a book. Does anyone know of any book on circuit simulation that actually describes how a simulator can be built? Or if anyone would be interested in reading such a book, what would your expectations be from such a book and why? Out of curiosity, because you are trying something similar, or because you are not happy with existing software?

Would be great to hear any feedback. Please send an email to pythonpowerelectronics@gmail.com.

Friday, August 22, 2014

Voltage measurement

Have been working furiously on modifying the simulator for larger circuits. But found there is a fatal error in voltage measurement. Take a look at the test circuit for a three-phase diode rectifier.


The voltmeter is modelled as a large resistance which does not disturb the current in the remaining circuit. The problem is that the branch containing the voltmeter is a stiff branch that appears in the loop equations. These equations appear randomly. So in the above circuit, when the simulation begins, suppose diodes D1 and D6 conduct. This means voltage vac appears across the capacitor Cdc and the voltmeter Vo. What would need to happen is that the voltmeter loop should be written with Cdc and therefore as Cdc charges, Vo output voltage should increase. But what is Vo appears in the loop with vac, D1 and D6? This is perfectly possible the way the program works now. This means Vo will jump to the voltage vac even though Cdc is charging gradually.

So Vo does not reflect the voltage that appears across the nodes but rather on the voltage impressed on it through the loop it appears on. And this inevitably happens whenever the circuit becomes large and loop equations become more and more random. Also, if the voltmeter were connected across a largely inductive circuit or for that matter over two fairly distant nodes in the circuit. What then?

The only error free way to solve this seems to be to do a nodal analysis with only the voltmeters for the nodes where they exist. This calls for a fairly large additional piece of code that will run at simulation time step.

Monday, July 21, 2014

Revised nodal analysis

When must nodal analysis take place?
  1. When there are inductors with sufficient energy and that energy might be abruptly interrupted.
  2. When does an inductor have sufficient energy? When it has been in at least one non stiff loop in the previous iteration. This way, the circuit is power independent.
The code is below (click on "view raw" below the code box to see the code in a new window):




The process is as follows:

  1. In the beginning, a new branch event is assumed by default whenever a branch event occurs.
  2. Look if there are any inductors that have previously been in non stiff loops.
  3. If so, nodal analysis is required.
  4. Following nodal analysis, check if there is a continuity event with branch currents from nodal analysis being different from the branch currents from previous iteration of loop analysis.
  5. If so, execute the determine_state function to check if any of the elements will change their state. If any of the elements do change state, the respective branches will have branch events.
  6. Check if there are any branch events that had not occurred in the previous iteration. If so, go back to step 2.
  7. This will repeat until no new branch events occur.

In nodal analysis, there has been one significant change. In order to perform nodal analysis, it is essential to calculate the node voltages. So the entire circuit has to be solved. However, the branch currents will be calculated only if:
  1. At a node, check if there has been an branch which has a "hard" event.
  2. A hard event is when a switch turns on or when it turns off while carrying a non negligible current. When a switch or a diode turns off when the current goes negative, that is a soft event. When a diode turns on when it is forward biased, it is a soft event.
  3. If a single branch incident at a node has a "hard" event, the currents of all the branches incident at the node will have to calculated. If not, the branch currents remain at their initial values - either zero or a current it is an inductor.
The code is below (click on "view raw" below the code box to see the code in a new window):



There has been another tag created called function_purpose. When determining state, function purpose will only calculate branch currents at nodes where there has been a "hard" event. But while calculating currents before the next loop analysis, all branch currents will be recalculated.

Determining switch and diode state

The code for determining switch state is below (click on "view raw" below the code box to view the code in a new window):



The code for the determining diode state is below (click on "view raw" below the code box to view the code in a new window):


This function is called after performing nodal analysis to check if the inductor current has caused an non linear device to change state. The difference between the code for switch and diode is that a switch can only turn off during nodal analysis while a diode can both turn on and turn off. My guess is that the switch should turn on only in the update_value function. If for some reason, the switch were to turn off during the nodal analysis because the current through it was negative due to an inductor, the switch remains off and only a diode can turn on to freewheel the current.

The reason for doing this will be the next detailed blog post.