armi.utils.mathematics module

Various math utilities.

armi.utils.mathematics.average1DWithinTolerance(vals, tolerance=0.2)[source]

Compute the average of a series of 1D arrays with a tolerance.

Tuned for averaging assembly meshes or block heights.

Parameters:
  • vals (2D np.array) – could be assembly x axial mesh tops or heights

  • tolerance (float) – The accuracy to which we need to know the average.

Returns:

The average of all the input 1D NumPy arrays.

Return type:

1D np.array

armi.utils.mathematics.convertToSlice(x, increment=0)[source]

Convert a int, float, list of ints or floats, None, or slice to a slice. Also optionally increments that slice to make it easy to line up lists that don’t start with 0.

Use this with np.array (np.ndarray) types to easily get selections of it’s elements.

Parameters:
  • x (multiple types allowed.) – int: select one index. list of int: select these index numbers. None: select all indices. slice: select this slice

  • increment (integer (or boolean), optional) – Step size, when taking your slices. (False is zero.)

Returns:

  • slice (slice) – Returns a slice object that can be used in an array like a[x] to select from its members. Also, the slice has its index numbers decremented by 1. It can also return a numpy array, which can be used to slice other numpy arrays in the same way as a slice.

  • increment (int) – Step size to take, if you want to take less then every datum in the collection.

Examples

>>> a = np.array([10, 11, 12, 13])
>>> convertToSlice(2)
slice(2, 3, None)
>>> a[convertToSlice(2)]
array([12])
>>> convertToSlice(2, increment=-1)
slice(1, 2, None)
>>> a[convertToSlice(2, increment=-1)]
array([11])
>>> a[convertToSlice(None)]
array([10, 11, 12, 13])
>>> a[utils.convertToSlice([1, 3])]
array([11, 13])
>>> a[utils.convertToSlice([1, 3], increment=-1)]
array([10, 12])
>>> a[utils.convertToSlice(slice(2, 3, None), increment=-1)]
array([11])
armi.utils.mathematics.efmt(a: str) str[source]

Converts string exponential number to another string with just 2 digits in the exponent.

armi.utils.mathematics.expandRepeatedFloats(repeatedList)[source]

Return an expanded repeat list.

Notes

R char is valid for showing the number of repeats in MCNP. For examples the list: [150, 200, ‘9R’] indicates a 150 day cycle followed by 10 200 day cycles.

armi.utils.mathematics.findClosest(listToSearch, val, indx=False)[source]

Find closest item in a list.

Parameters:
  • listToSearch (list) – The list to search through

  • val (float) – The target value that is being searched for in the list

  • indx (bool, optional) – If true, returns minVal and minIndex, otherwise, just the value

Returns:

  • minVal (float) – The item in the listToSearch that is closest to val

  • minI (int) – The index of the item in listToSearch that is closest to val. Returned if indx=True.

armi.utils.mathematics.findNearestValue(searchList, searchValue)[source]

Search a given list for the value that is closest to the given search value.

armi.utils.mathematics.findNearestValueAndIndex(searchList, searchValue)[source]

Search a given list for the value that is closest to the given search value. Return a tuple containing the value and its index in the list.

armi.utils.mathematics.fixThreeDigitExp(strToFloat: str) float[source]

Convert FORTRAN numbers that cannot be converted into floats.

Notes

Converts a number line “9.03231714805651-101” (no e or E) to “9.03231714805651e-101”. Some external depletion kernels currently need this fix. From contact with developer: The notation like 1.0-101 is a FORTRAN thing, with history going back to the 60’s. They will only put E before an exponent 99 and below. Fortran will also read these guys just fine, and they are valid floating point numbers. It would not be a useful effort, in terms of time, trying to get FORTRAN to behave differently. The approach has been to write a routine in the reading code which will interpret these.

This helps when the scientific number exponent does not fit.

armi.utils.mathematics.getFloat(val)[source]

Returns float version of val, or None if it’s impossible. Useful for converting user-input into floats when ‘’ might be possible.

armi.utils.mathematics.getStepsFromValues(values, prevValue=0.0)[source]

Convert list of floats to list of steps between each float.

armi.utils.mathematics.isMonotonic(inputIter, relation)[source]

Checks if an iterable contains elements that are monotonically increasing or decreasing, whatever that might mean for the specific types of the elements.

Parameters:
  • inputIter (list) – Some list to check. Values in the list should have a defined relation to each other.

  • relation ({'<=', '<', '>=', '>'}) – The relation between the elements to check, from left to right through the iterable.

Return type:

bool

armi.utils.mathematics.linearInterpolation(x0, y0, x1, y1, targetX=None, targetY=None)[source]

Does a linear interpolation (or extrapolation) for y=f(x).

Parameters:
  • x0 (float) – Coordinates of two points to interpolate between

  • y0 (float) – Coordinates of two points to interpolate between

  • x1 (float) – Coordinates of two points to interpolate between

  • y1 (float) – Coordinates of two points to interpolate between

  • targetX (float, optional) – X value to evaluate the line at

  • targetY (float, optional) – Y value we want to find the x value for (inverse interpolation)

Returns:

  • interpY (float) – The value of y(targetX), if targetX is not None

  • interpX (float) – The value of x where y(x) = targetY (if targetY is not None)

  • y = m(x-x0) + b

  • x = (y-b)/m

armi.utils.mathematics.minimizeScalarFunc(func, goal, guess, maxIterations=None, cs=None, positiveGuesses=False, method=None, tol=0.001)[source]

Use SciPy minimize with the given function, goal value, and first guess.

Parameters:
  • func (function) – The function that guess will be changed to try to make it return the goal value.

  • goal (float) – The function will be changed until it’s return equals this value.

  • guess (float) – The first guess value to do Newton’s method on the func.

  • maxIterations (int) – The maximum number of iterations that the Newton’s method will be allowed to perform.

Returns:

ans – The guess that when input to the func returns the goal.

Return type:

float

armi.utils.mathematics.newtonsMethod(func, goal, guess, maxIterations=None, cs=None, positiveGuesses=False)[source]

Solves a Newton’s method with the given function, goal value, and first guess.

Parameters:
  • func (function) – The function that guess will be changed to try to make it return the goal value.

  • goal (float) – The function will be changed until it’s return equals this value.

  • guess (float) – The first guess value to do Newton’s method on the func.

  • maxIterations (int) – The maximum number of iterations that the Newton’s method will be allowed to perform.

Returns:

ans – The guess that when input to the func returns the goal.

Return type:

float

armi.utils.mathematics.parabolaFromPoints(p1, p2, p3)[source]

Find the parabola that passes through three points.

We solve a simultaneous equation with three points.

A = x1**2 x1 1

x2**2 x2 1 x3**2 x3 1

b = y1

y2 y3

find coefficients Ax=b

Parameters:
  • p1 (tuple) – first point (x,y) coordinates

  • p2 (tuple) – second (x,y) points

  • p3 (tuple) – third (x,y) points

Returns:

3 floats: a,b,c coefficients of y=ax^2+bx+c

Return type:

tuple

armi.utils.mathematics.parabolicInterpolation(ap, bp, cp, targetY)[source]

Given parabola coefficients, this interpolates the time that would give k=targetK.

keff = at^2+bt+c We want to solve a*t^2+bt+c-targetK = 0.0 for time. if there are real roots, we should probably take the smallest one because the larger one might be at very high burnup. If there are no real roots, just take the point where the deriv ==0, or 2at+b=0, so t = -b/2a The slope of the curve is the solution to 2at+b at whatever t has been determined

Parameters:
  • ap (float) – coefficients ap of a parabola y = ap*x^2 + bp*x + cp

  • bp (float) – coefficients bp of a parabola y = ap*x^2 + bp*x + cp

  • cp (float) – coefficients cp of a parabola y = ap*x^2 + bp*x + cp

  • targetY (float) – The keff to find the cycle length of

Returns:

realRoots – (root, slope) The best guess of the cycle length that will give k=targetK If no positive root was found, this is the maximum of the curve. In that case, it will be a negative number. If there are two positive roots, there will be two entries.

slopefloat

The slope of the keff vs. time curve at t=newTime

Return type:

list of tuples

armi.utils.mathematics.relErr(v1: float, v2: float) float[source]

Find the relative error between to numbers.

armi.utils.mathematics.resampleStepwise(xin, yin, xout, avg=True)[source]

Resample a piecewise-defined step function from one set of mesh points to another. This is useful for reallocating values along a given axial mesh (or assembly of blocks).

Parameters:
  • xin (list) – interval points / mesh points

  • yin (list) – interval values / inter-mesh values

  • xout (list) – new interval points / new mesh points

  • avg (bool) – By default, this is set to True, forcing the resampling to be done by averaging. But if this is False, the resmampling will be done by summation, to try and preserve the totals after resampling.

armi.utils.mathematics.rotateXY(x, y, degreesCounterclockwise=None, radiansCounterclockwise=None)[source]

Rotates x, y coordinates.

Parameters:
  • x (float) – X coordinates, array-like

  • y (float) – Y coordinates, array-like

  • degreesCounterclockwise (float) – Degrees to rotate in the CCW direction

  • radiansCounterclockwise (float) – Radians to rotate in the CCW direction

Returns:

xr, yr: the rotated coordinates

Return type:

tuple