Monday, February 4, 2013

Circuit Parameters - II

The next step is to write the code such that all elements are identified and parameters are obtained. In C++ the concept that I followed was that every element was an object - a resistor, an inverter etc. There was three levels of data transfer to these objects. The first was when they were initialized. The second was when the parameters and initial conditions they contained was transferred to the system matrices for solving the ODEs and third was when the results of the ODE was transferred back to the objects to update the values of voltage/current etc.

In C++, the objects were created manually. In Python, I would like the objects to be created automatically from the network layout in the CSV file.

So, testing how to do this first step. A little piece of code to see how automatically the objects can be created and stored. This code is too ridiculously simple to put on Git, so this is it:

-----------------------------------------------------------------------------------------------------------------
#! /usr/bin/env python
import sys, math

class Resistor:
    def __init__(self, res_index):
        self.res_number=res_index
    def display(self):
        print "Resistor is ",
        print "R"+str(self.res_number)

res_list=[]
for c1 in range(1, 5):
    res_name="R"+str(c1)
    res_name=Resistor(c1)
    res_list.append(res_name)

print res_list
for res in res_list:
    res.display()
-----------------------------------------------------------------------------------------------------------------

Here, I am creating a class "Resistor" which has only one data element - its index. So the resistors will be called R1, R2, etc.

I create a list of resistors res_list and create resistors R1 to R4. Also, objects are created with that tag and added to res_list.

Here is the output:
-----------------------------------------------------------------------------------------------------------------
>>>
[<__main__.Resistor instance at 0x02A1F4E0>, <__main__.Resistor instance at 0x02A1F508>, <__main__.Resistor instance at 0x02A1F530>, <__main__.Resistor instance at 0x02A1F558>]
Resistor is  R1
Resistor is  R2
Resistor is  R3
Resistor is  R4
-----------------------------------------------------------------------------------------------------------------

res_list is a list with instances of the class Resistor. But importantly, when I access the method display of the class Resistor by res.display(), it does give me the correct resistor labels.

So now let me put everything in the spreadsheet into objects.

No comments:

Post a Comment