def _create_J_with_numba(Ybus, V, pvpq, pq, createJ, pvpq_lookup, npv, npq): Ibus = zeros(len(V), dtype=complex128) # create Jacobian from fast calc of dS_dV dVm_x, dVa_x = dSbus_dV_numba_sparse(Ybus.data, Ybus.indptr, Ybus.indices, V, V / abs(V), Ibus) # data in J, space preallocated is bigger than acutal Jx -> will be reduced later on Jx = empty(len(dVm_x) * 4, dtype=float64) # row pointer, dimension = pvpq.shape[0] + pq.shape[0] + 1 Jp = zeros(pvpq.shape[0] + pq.shape[0] + 1, dtype=int32) # indices, same with the preallocated space (see Jx) Jj = empty(len(dVm_x) * 4, dtype=int32) # fill Jx, Jj and Jp createJ(dVm_x, dVa_x, Ybus.indptr, Ybus.indices, pvpq_lookup, pvpq, pq, Jx, Jj, Jp) # resize before generating the scipy sparse matrix Jx.resize(Jp[-1], refcheck=False) Jj.resize(Jp[-1], refcheck=False) # generate scipy sparse matrix dimJ = npv + npq + npq J = sparse((Jx, Jj, Jp), shape=(dimJ, dimJ)) return J
def ones(shape, typecode='l', savespace=0, dtype=None): """ones(shape, dtype=int) returns an array of the given dimensions which is initialized to all ones. """ dtype = convtypecode(typecode, dtype) a = mu.empty(shape, dtype) a.fill(1) return a
def AC_jacobian(Ybus, V, pvpq, pq, pvpq_lookup, npv, npq): """ Create the AC Jacobian function with no embedded controls :param Ybus: Ybus matrix in CSC format :param V: Voltages vector :param pvpq: array of pv|pq bus indices :param pq: array of pq indices :param pvpq_lookup: array of pv|pq lookup indices :param npv: number of pv buses :param npq: number of pq buses :return: Jacobian Matrix in CSR format """ Ibus = zeros(len(V), dtype=complex128) # create Jacobian from fast calc of dS_dV dS_dVm, dS_dVa = deriv.dSbus_dV_numba_sparse_csr(Ybus.data, Ybus.indptr, Ybus.indices, V, V / abs(V), Ibus) # data in J, space pre-allocated is bigger than actual Jx -> will be reduced later on Jx = empty(len(dS_dVm) * 4, dtype=float64) # row pointer, dimension = pvpq.shape[0] + pq.shape[0] + 1 Jp = zeros(pvpq.shape[0] + pq.shape[0] + 1, dtype=int32) # indices, same with the pre-allocated space (see Jx) Jj = empty(len(dS_dVm) * 4, dtype=int32) # fill Jx, Jj and Jp in CSR order if len(pvpq) == len(pq): create_J_no_pv(dS_dVm, dS_dVa, Ybus.indptr, Ybus.indices, pvpq_lookup, pvpq, Jx, Jj, Jp) else: create_J(dS_dVm, dS_dVa, Ybus.indptr, Ybus.indices, pvpq_lookup, pvpq, pq, Jx, Jj, Jp) # resize before generating the scipy sparse matrix Jx.resize(Jp[-1], refcheck=False) Jj.resize(Jp[-1], refcheck=False) # generate scipy sparse matrix nj = npv + npq + npq return csr_matrix((Jx, Jj, Jp), shape=(nj, nj))
_types.StringType, _types.UnicodeType, _types.BufferType] ScalarType.extend(_sctype2char_dict.keys()) ScalarType = tuple(ScalarType) for key in _sctype2char_dict.keys(): cast[key] = lambda x, k=key : array(x, copy=False).astype(k) _unicodesize = array('u','U1').itemsize # Create the typestring lookup dictionary _typestr = _typedict() for key in _sctype2char_dict.keys(): if issubclass(key, allTypes['flexible']): _typestr[key] = _sctype2char_dict[key] else: _typestr[key] = empty((1,),key).dtype.str[1:] # Make sure all typestrings are in sctypeDict for key, val in _typestr.items(): if val not in sctypeDict: sctypeDict[val] = key # Add additional strings to the sctypeDict _toadd = ['int', 'float', 'complex', 'bool', 'object', 'string', ('str', allTypes['string_']), 'unicode', 'object', ('a', allTypes['string_'])] for name in _toadd: if isinstance(name, tuple): sctypeDict[name[0]] = name[1] else:
except AttributeError: # Py3K ScalarType = [int, float, complex, int, bool, bytes, str, memoryview] ScalarType.extend(_sctype2char_dict.keys()) ScalarType = tuple(ScalarType) for key in _sctype2char_dict.keys(): cast[key] = lambda x, k=key : array(x, copy=False).astype(k) # Create the typestring lookup dictionary _typestr = _typedict() for key in _sctype2char_dict.keys(): if issubclass(key, allTypes['flexible']): _typestr[key] = _sctype2char_dict[key] else: _typestr[key] = empty((1,), key).dtype.str[1:] # Make sure all typestrings are in sctypeDict for key, val in _typestr.items(): if val not in sctypeDict: sctypeDict[val] = key # Add additional strings to the sctypeDict if sys.version_info[0] >= 3: _toadd = ['int', 'float', 'complex', 'bool', 'object', 'str', 'bytes', 'object', ('a', allTypes['bytes_'])] else: _toadd = ['int', 'float', 'complex', 'bool', 'object', 'string', ('str', allTypes['string_']), 'unicode', 'object', ('a', allTypes['string_'])]
def empty(shape, typecode='l', dtype=None): dtype = convtypecode(typecode, dtype) return mu.empty(shape, dtype)