Another major comment. If the number of nodes (including the reference or ground node) is N and the number of branches is B, the number of independent loops will be B-N+1. However, while trying to generate just that many number of independent loops, the code was beginning to fail. So the code posted will show all the possible "distinct" loops out of which many of them will simply be linear combinations of the others. I hope this will get sorted out while solving the equations, since the upper triangularization of the matrices will result in a number of rows to be zero. Anyway, this comes later.
As before, click on "view raw" to see the code that goes out of the box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Remove any repitions in loop_list | |
for c1 in range(len(loop_list)-1): | |
for c2 in range(len(loop_list)-1, c1, -1): | |
if loop_list[c1]==loop_list[c2]: | |
del loop_list[c2] | |
# The actual elements from the branches | |
# to be entered into the loops | |
loop_branches=[] | |
# Go through every element in loop_list | |
for c1 in range(len(loop_list)): | |
# If the loop has two elements | |
# it means it is a group of | |
# parallel branches between nodes | |
if len(loop_list[c1])==2: | |
curr_br=loop_list[c1][0] | |
# Get every permutation of branch pairs possible | |
for c2 in range(len(branch_map[curr_br[0]][curr_br[1]])-1): | |
for c3 in range(c2+1, len(branch_map[curr_br[0]][curr_br[1]])): | |
loop_updt=[] | |
# Iterate in the forward direction | |
for c4 in range(len(branch_map[curr_br[0]][curr_br[1]][c2])): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][c2][c4]) | |
# Iterate in the reverse direction | |
for c4 in range(len(branch_map[curr_br[0]][curr_br[1]][c3])-2, -1, -1): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][c3][c4]) | |
loop_branches.append(loop_updt) | |
else: | |
loop_updt=[] | |
# Go through all elements in the loop | |
for c2 in range(0, len(loop_list[c1])-1): | |
# Mark two elements in the loop | |
# The current and the next element | |
curr_br=loop_list[c1][c2] | |
curr_br_beg=branch_map[curr_br[0]][curr_br[1]][0][0] | |
curr_br_end=branch_map[curr_br[0]][curr_br[1]][0][-1] | |
next_br=loop_list[c1][c2+1] | |
next_br_beg=branch_map[next_br[0]][next_br[1]][0][0] | |
next_br_end=branch_map[next_br[0]][next_br[1]][0][-1] | |
curr_dir="forward" | |
# Start stringing the branches together | |
# So if it is the first branch | |
# Check if the beginning element of the branch | |
# is the same as the beginning or ending element | |
# if the next branch | |
# In that case, the first/current branch | |
# is to be reversed | |
if not loop_updt: | |
if curr_br_beg==next_br_beg or curr_br_beg==next_br_end: | |
curr_dir="reverse" | |
# If the loop update is in progress | |
# check how the current element is linked to | |
# the last element on the updated loop | |
else: | |
if curr_br_end==loop_updt[-1]: | |
curr_dir="reverse" | |
# Depending on the direction in which | |
# an element is to be added to | |
# the loop. | |
if curr_dir=="forward": | |
for c3 in range(len(branch_map[curr_br[0]][curr_br[1]][0])): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][0][c3]) | |
else: | |
for c3 in range(len(branch_map[curr_br[0]][curr_br[1]][0])-1, -1, -1): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][0][c3]) | |
# Repeat for the last element | |
next_dir="forward" | |
if next_br_end==loop_updt[-1]: | |
next_dir="reverse" | |
if next_dir=="forward": | |
for c3 in range(len(branch_map[next_br[0]][next_br[1]][0])): | |
loop_updt.append(branch_map[next_br[0]][next_br[1]][0][c3]) | |
else: | |
for c3 in range(len(branch_map[next_br[0]][next_br[1]][0])-1, -1, -1): | |
loop_updt.append(branch_map[next_br[0]][next_br[1]][0][c3]) | |
# Remove any repitions in the elements | |
# in consecutive elements only | |
for c3 in range(len(loop_updt)-1, 0, -1): | |
if loop_updt[c3]==loop_updt[c3-1]: | |
del loop_updt[c3] | |
loop_branches.append(loop_updt) | |
loop_count=len(loop_branches) | |
print "Number of nodes", | |
print number_of_nodes | |
print "Number of branches", | |
print number_of_branches | |
print "Number of loops", | |
print loop_count | |
print "*"*50 | |
excel_col="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" | |
excel_dict={} | |
excel_col_list=excel_col.split(" ") | |
for c1 in range(26): | |
excel_dict[c1]=excel_col_list[c1] | |
for item in loop_branches: | |
for c1 in range(len(item)): | |
print str(item[c1][0]+1) + excel_dict[item[c1][1]], | |
No comments:
Post a Comment