andes.core.service.NumReduce#

class andes.core.service.NumReduce(u, ref: BackRef, fun: Callable, name=None, tex_name=None, info=None, cache=True)[source]#

A helper Service type which reduces a linearly stored 2-D ExtParam into 1-D Service.

NumReduce works with ExtParam whose v field is a list of lists. A reduce function which takes an array-like and returns a scalar need to be supplied. NumReduce calls the reduce function on each of the lists and return all the scalars in an array.

Parameters:
uExtParam

Input ExtParam whose v contains linearly stored 2-dimensional values

refBackRef

The BackRef whose 2-dimensional shapes are used for indexing

funCallable

The callable for converting a 1-D array-like to a scalar

Examples

Suppose one wants to calculate the mean value of the Vn in one Area. In the Area class, one defines

class AreaModel(...):
    def __init__(...):
        ...
        # backward reference from `Bus`
        self.Bus = BackRef()

        # collect the Vn in an 1-D array
        self.Vn = ExtParam(model='Bus',
                           src='Vn',
                           indexer=self.Bus)

        self.Vn_mean = NumReduce(u=self.Vn,
                                 fun=np.mean,
                                 ref=self.Bus)

Suppose we define two areas, 1 and 2, the Bus data looks like

idx

area

Vn

1

1

110

2

2

220

3

1

345

4

1

500

Then, self.Bus.v is a list of two lists [ [1, 3, 4], [2] ]. self.Vn.v will be retrieved and linearly stored as [110, 345, 500, 220]. Based on the shape from self.Bus, numpy.mean() will be called on [110, 345, 500] and [220] respectively. Thus, self.Vn_mean.v will become [318.33, 220].

__init__(u, ref: BackRef, fun: Callable, name=None, tex_name=None, info=None, cache=True)[source]#

Methods

assign_memory(n)

Assign memory for self.v and set the array to zero.

get_names()

Return name in a list

Attributes

class_name

Return the class name

n

Return the count of values in self.v.

v

Return the reduced values from the reduction function in an array