Skip to content

Commit

Permalink
Adding back the Andreas's iterator. Not sure if anyone is using it.
Browse files Browse the repository at this point in the history
  • Loading branch information
nunobrum committed Oct 16, 2023
1 parent f861a56 commit 198293c
Showing 1 changed file with 93 additions and 1 deletion.
94 changes: 93 additions & 1 deletion PyLTSpice/utils/sweep_iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,96 @@
#
# -------------------------------------------------------------------------------

from spicelib.utils.sweep_iterators import *
from spicelib.utils.sweep_iterators import *

# ======================== Andreas Kaeberlein Iterator =========================

class sweep_iterators:

# *****************************
def __init__(self):
"""
Initialization
"""
self.numTotalIterations = 0 # total of iteartion if all loops are executed
self.numCurrentIteration = 0 # current iteration
self.iteratorEntrys = [] # list of dicts for iterator entrys
self.idxForNextIter = [] # currently used entry value for loop

# *****************************

# *****************************
def add(self, name="", vals=[]):
"""
@note adds entry to list of iterators
@param name component name in ltspice schematic
@param vals component values
@rtype boolean
@return successful
"""
# check for valid arguments
if (0 == len(name) or 0 == len(vals)):
raise ValueError("Empty arguments provided")
# add to iterator list
self.iteratorEntrys.append({'name': name, 'values': vals}) # add entry
self.idxForNextIter.append(0) # start on first element
# update total number of iteration
self.numTotalIterations = 1; # prepare for mutiplication
for i in self.iteratorEntrys:
self.numTotalIterations = self.numTotalIterations * len(i['values'])
# reset current iterator to ensure restart
self.numCurrentIteration = 0
# succesfull end
return True

# *****************************

# *****************************
def done(self):
"""
@note check if iteration is done
@rtype boolean
@retval True Iteration done
@retval False Iteration needs to continue
@return successful
"""
# check for proper init
if (0 == len(self.iteratorEntrys)):
return True
# iteration done?
if (self.numCurrentIteration < self.numTotalIterations):
return False
return True

# *****************************

# *****************************
def next(self):
"""
@note creates next parameter set for sweep
@rtype dict
@return parameter set
"""
# check for iterators
if (0 == len(self.iteratorEntrys)):
raise ValueError("No iterator entrys defined. Use 'add' procedure")
# assemble dict with new iterator values
nextIter = {}
for i in range(len(self.iteratorEntrys)):
nextIter[self.iteratorEntrys[i]['name']] = self.iteratorEntrys[i]['values'][self.idxForNextIter[i]]
# prepare for next cycle
for i in range(len(self.idxForNextIter) - 1, -1, -1):
# increment inner loop
if (i == len(self.idxForNextIter) - 1):
self.idxForNextIter[i] = self.idxForNextIter[i] + 1
# inner loop overflow, inc outer loop
if (self.idxForNextIter[i] >= len(self.iteratorEntrys[i]['values'])):
self.idxForNextIter[i] = 0 # restart inner loop at first element
self.idxForNextIter[max(i - 1, 0)] = self.idxForNextIter[i - 1] + 1 # go to next element in outer loop
# increment iterator
self.numCurrentIteration = self.numCurrentIteration + 1
# next iteration element
return nextIter
# *****************************

0 comments on commit 198293c

Please sign in to comment.