Wednesday, January 2, 2013

Network representation

I need a way to describe a circuit that is both convenient to the user but won't require horrendous effort in programming or be a drain on the system. So would like to avoid GUI based circuit drawing. Also, to ask the user to describe the circuit as a net list will drive most people away.

One way I can think of is a spreadsheet that is saved as a csv file. This can be done with just about any spread sheet software. A sample case:


The above is a circuit description.

1. I haven't bothered about the objects yet so they are just named as "a", "b", "c" etc.
2. Connections that are zero impedance are named as "wire".
3. Empty cells imply no connection.
4. Connections have to be along a row or a column. Can't make diagonal connections.

The .csv file as a text file looks like this:
------------------------------------------------------------
,"wire","wire","b","wire","wire","d","wire"
,"wire",,,"wire",,,"wire"
"wire","wire",,,"wire",,,"wire"
"a",,,,"c",,,"wire"
"wire",,,,"wire",,,"wire"
"wire","wire","f","wire","wire",,"wire","wire"
"wire",,,,"k",,,"h"
"wire",,,,,,,"wire"
"wire","i","wire","wire","wire","wire","e","wire"
,,,,"g",,,"wire"
,,,,"wire","wire","wire","wire"

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

The code to interpret this is:


#! /usr/bin/env python
import sys, math, matrix
new_file = open("testckt.csv","r")
# This matrix will read the .csv file
# The .csv file will contain the string "wire" where a zero impedance direct connection exists.
# Where no connection nor device (resitor, inductor etc) exists, a '' will be found.
# The devices will be strings. Maybe later will be actual object constructor calls.
conn_matrix=[]
for line in new_file:
print line
conn_matrix.append(line.split(","))
conn_rows=len(conn_matrix)
conn_columns=len(conn_matrix[0])
# A node is defined as a junction of 3 or more branches.
node_list=[]
for c1 in range(0, conn_rows):
for c2 in range(0, conn_columns):
if not (conn_matrix[c1][c2]==''):
if ((c1==0 and c2==0) or (c1==conn_rows-1 and c2==conn_columns-1) or (c1==0 and c2==conn_columns-1) or (c1==conn_rows-1 and c2==0)):
# If its a corner point it can't be a node. This prevents array index going out of range.
pass
# The next cases, can't be outer edges or corner points.
else:
if (c1==0):
# If it is the first row,
# check if the element in the next and previous columns and same row are connected.
if not (conn_matrix[c1][c2+1]=='' or conn_matrix[c1][c2-1]==''):
# Then check if the element in next row and same column is connected. Look for a T junction.
if not (conn_matrix[c1+1][c2]==''):
node_list.append([c1, c2])
if (c1==conn_rows-1):
# If it is the last row,
# check if the elements in the next and previous columns and same row are connected.
if not (conn_matrix[c1][c2+1]=='' or conn_matrix[c1][c2-1]==''):
if not (conn_matrix[c1-1][c2]==''):
# Then check if element in the previous row and same column is connected. Look for a T junction.
node_list.append([c1, c2])
if (c2==0):
# If it is the first column,
# check if the element in the next column and same row is connected.
if not (conn_matrix[c1][c2+1]==''):
# Then check if the elements in next and previous row and same column are connected. Look for a T junction.
if not (conn_matrix[c1+1][c2]=='' or conn_matrix[c1-1][c2]==''):
node_list.append([c1, c2])
if (c2==conn_columns-1):
# If it is the last column,
# check if the element in the previous column and same row is connected.
if not (conn_matrix[c1][c2-1]==''):
# Then check if the elements in next and previous row and same column are connected. Look for a T junction.
if not (conn_matrix[c1+1][c2]=='' or conn_matrix[c1-1][c2]==''):
node_list.append([c1, c2])
if (c1>0 and c1<conn_rows-1 and c2>0 and c2<conn_columns-1):
# If the element is not on the outer boundary
if (conn_matrix[c1][c2+1]!='' and conn_matrix[c1][c2-1]!=''):
# Check if the elements in next and previous columns and same row are connected
if (conn_matrix[c1+1][c2]!='' or conn_matrix[c1-1][c2]!=''):
# Then check if elements in either the next and previous row and same column are connected
node_list.append([c1, c2])
elif (conn_matrix[c1+1][c2]!='' and conn_matrix[c1-1][c2]!=''):
# This one is probably a redundant check but I'll keep it anyway.
if (conn_matrix[c1][c2+1]!='' or conn_matrix[c1][c2-1]!=''):
node_list.append([c1, c2])
else:
pass
print "*"*60
# This list contains all the nodes that are T or + junctions.
print node_list
view raw net1.py hosted with ❤ by GitHub



The output is:
-------------------------------------------------------------------------------------
,"wire","wire","b","wire","wire","d","wire"

,"wire",,,"wire",,,"wire"

"wire","wire",,,"wire",,,"wire"

"a",,,,"c",,,"wire"

"wire",,,,"wire",,,"wire"

"wire","wire","f","wire","wire",,"wire","wire"

"wire",,,,"k",,,"h"

"wire",,,,,,,"wire"

"wire","i","wire","wire","wire","wire","e","wire"

,,,,"g",,,"wire"

,,,,"wire","wire","wire","wire"

************************************************************
[[0, 4], [5, 0], [5, 4], [5, 7], [8, 4], [8, 7]]

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


A couple of comments:

1. A break in the network won't be detected. For example, E8 and F6 are empty. Which means E6 or [5,4] is not a node. This will have to be dealt later maybe by filling up breaks with high resistances or simply throwing error exceptions. The user has to ensure all connections. If its open, connect a large resistance.

2. The circuit starts from the top of the spreadsheet. But even if there are a couple of empty lines, the program still works. There will be an offset in the row indices, that can be removed by detecting the number of empty rows.


The concept behind this node list is that while finding the loops to apply KVL, you can start at a node and snake your way until you other nodes and make your way back to the starting node. I will also need the number of branches so that I can determine the number of loops. That will be the next task.

Ahead, the actual objects will have to be embedded. For example, AC_Voltage(mag=100, freq=60, res=0.1) could be one of the elements above. This may need something like regular expressions.

No comments:

Post a Comment