ahkab.utilities¶

This module holds miscellaneous utility functions.

Module reference¶

Celsius2Kelvin(cel)[source]

Convert Celsius degrees to Kelvin

EPS = 2.2204460492503131e-16

The machine epsilon, the upper bound on the relative error due to rounding in floating point arithmetic.

Kelvin2Celsius(kel)[source]

Convert Kelvin degrees to Celsius

check_circuit(circ)[source]

Performs some easy sanity checks.

Checks performed:

• Has the circuit more than one node?
• Has the circuit a connection to ground?
• Has the circuit more than two elements?
• Are there no two elements with the same part_id?

Parameters:

circ : circuit instance
The circuit to be checked.

Returns:

chk : boolean
The logical and() of the answer to the above questions.
msg : string
A message describing the error, if any.
check_file(filename)[source]

Checks whether the supplied path refers to a valid file.

Parameters:

filename : string
The file name.

Returns:

chk : boolean
A value of True if filename is found and it is a file.
Raises: IOError – if no such file exists or if the supplied file is a directory.
check_ground_paths(mna, circ, reduced_mna=True, verbose=3)[source]

Checks that every node has a DC path to ground

The path to ground might be through non-linear elements.

Note

• This does not ensure that the circuit will have a DC solution.
• A node without DC path to ground would be rescued (likely) by GMIN so (for the time being at least) we do not halt the execution.
• Also, two series capacitors always fail this check (GMIN saves us)

Bottom line: if there is no DC path to ground, there is probably a mistake in the netlist. Print a warning.

Returns:

chk : boolean
A boolean set to true if there is a DC path to ground from all nodes in the circuit.
check_step_and_points(step=None, points=None, period=None, default_points=100)[source]

Sets consistently the step size and the number of points

The calculation is done according to the given period.

Parameters:

step : scalar, optional
The discretization step.
points : int, optional
The number of points to be used in the discretization.
period : scalar, optional
The length of the interval to be discretized. Not setting this parameter will result in a ValueError.
default_points : int, optional
The default number of points.

Returns:

(points, step) : tuple
The adjusted number of points and step value.
class combinations(L, k)[source]

This class is an iterator that returns all the k-combinations _without_repetition_ of the elements of the supplied list.

Each combination is made of a subset of the list, consisting of k elements.

Parameters:

L : list
The set from which the elements are taken.
k : int
The size of the subset, the number of elements to be taken
next()[source]
convergence_check(x, dx, residuum, nv_minus_one, debug=False)[source]

Perform a convergence check

Parameters:

x : array-like
The results to be checked.
dx : array-like
The last increment from a Newton-Rhapson iteration, solving F(x) = 0.
residuum : array-like
The remaining error, ie F(x) = residdum
nv_minus_one : int
Number of voltage variables in x. If nv_minus_one is equal to n, it means x[:n] are all voltage variables.
debug : boolean, optional
Whether extra information is needed for debug purposes. Defaults to False.

Returns:

chk : boolean
Whether the check was passed or not. True means ‘convergence!’.
rbn : ndarray
The convergence check results by node, if debug was set to True, else None.
current_convergence_check(x, dx, residuum, debug=False)[source]

Perform a convergence check for current variables

Parameters:

x : array-like
The results to be checked.
dx : array-like
The last increment from a Newton-Rhapson iteration, solving F(x) = 0.
residuum : array-like
The remaining error, ie F(x) = residdum
debug : boolean, optional
Whether extra information is needed for debug purposes. Defaults to False.

Returns:

chk : boolean
Whether the check was passed or not. True means ‘convergence!’.
rbn : ndarray
The convergence check results by node, if debug was set to True, else None.
custom_convergence_check(x, dx, residuum, er, ea, eresiduum, debug=False)[source]

Perform a custom convergence check

Parameters:

x : array-like
The results to be checked.
dx : array-like
The last increment from a Newton-Rhapson iteration, solving F(x) = 0.
residuum : array-like
The remaining error, ie F(x) = residdum
ea : float
The value to be employed for the absolute error.
er : float
The value for the relative error to be employed.
eresiduum : float
The maximum allowed error for the residuum (left over error).
debug : boolean, optional
Whether extra information is needed for debug purposes. Defaults to False.

Returns:

chk : boolean
Whether the check was passed or not. True means ‘convergence!’.
rbn : ndarray
The convergence check results by node, if debug was set to True, else None.
expand_matrix(matrix, add_a_row=False, add_a_col=False)[source]

Append a row and/or a column to the given matrix

Parameters:

matrix : ndarray
The matrix to be manipulated.
If set to True a row is appended to the supplied matrix.
If set to True a column is appended.

Returns:

matrix : ndarray
A reference to the same matrix supplied.
class lin_axis_iterator(min, max, points)[source]

This iterator provides the values for a linear sweep.

Parameters:

min : float
The minimum value, also the start point of the axis.
max : float
The maximum value, also the end point of the axis.
num : int
The number of samples to generate. In general, this should be greater than 1. A value of 1 is accepted only if min == max, in which case, only one value is returned by the iterator: min.

Start and end points are always included.

Notice that, differently from numpy’s linspace(), the values are only computed at access time, and hence the memory footprint of the iterator is low.

Raises: ValueError – if the number points is either negative or does not

respect the conditions above.

next()[source]
class log_axis_iterator(min, max, points)[source]

This iterator provides the values for a base-10 logarithmic sweep.

Parameters:

min : float
The minimum value, also the start point of the axis.
max : float
The maximum value, also the end point of the axis.
points : int
The number of points which will be used to discretize the max - min interval.

Notice that, differently from numpy’s logspace(), the values are only computed at access time, and hence the memory footprint of the iterator is low.

Start and end values are always included.

next()[source]
memoize(f)[source]

Memoization decorator

Parameters:

f : function
The function to apply memoization to.

Returns:

fm : function

Implementation:

Originally from this post, it has been modified to provide a cache of size options.cache_len.

Note

The size of the cache is per model instance and per function. If you have one model, shared by several elements, you probably prefer to have a big cache.

remove_row(matrix, rrow=0)[source]

Removes a row from a matrix

Parameters:

matrix : ndarray
The matrix to be modified.
rrow : int or None, optional
The index of the row to be removed. If set to None, no row will be removed. By default the first row is removed.

Note

No size checking is done.

Returns:

matrix : ndarray
A reference to the modified matrix.
remove_row_and_col(matrix, rrow=0, rcol=0)[source]

Removes a row and/or a column from a matrix

Parameters:

matrix : ndarray
The matrix to be modified.
rrow : int or None, optional
The index of the row to be removed. If set to None, no row will be removed. By default the first row is removed.
rcol : int or None, optional
The index of the row to be removed. If set to None, no row will be removed. By default the first column is removed.

Note

No size checking is done.

Returns:

matrix : ndarray
A reference to the modified matrix.
set_submatrix(row, col, dest_matrix, source_matrix)[source]

Copies a source matrix into another matrix

row, col : ints
The coordinates of the upper left corner in the destination matrix where the source matrix will be copied.
dest_matrix : ndarray
The matrix to be copied to.
source_matrix : ndarray
The matrix to be copied from.

Returns:

dest_matrix : ndarray
A reference to the modified destination matrix.
tuplinator(alist)[source]

Convert a list of lists (of lists...) to tuples

voltage_convergence_check(x, dx, residuum, debug=False)[source]

Perform a convergence check for voltage variables

Parameters:

x : array-like
The results to be checked.
dx : array-like
The last increment from a Newton-Rhapson iteration, solving F(x) = 0.
residuum : array-like
The remaining error, ie F(x) = residdum
debug : boolean, optional
Whether extra information is needed for debug purposes. Defaults to False.

Returns:

chk : boolean
Whether the check was passed or not. True means ‘convergence!’.
rbn : ndarray
The convergence check results by node, if debug was set to True, else None.