<commit_before>"""Module containing sample classes for approach definitions."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import scipy.sparse.linalg
from scipy import optimize

from .mytypes import doublenp
from .mytypes import complexnp

class Approach(object):
    """
    Sample class used to define different approaches.

    Attributes
    ----------
    qd : QuantumDot
        QuantumDot object.
    leads : LeadsTunneling
        LeadsTunneling object.
    si : StateIndexingDM
        StateIndexingDM object.
    kern : array
        Kernel (Liouvillian) representing the master equation.
    bvec : array
        Right hand side column vector for master equation.
        The entry funcp.norm_row is 1 representing normalization condition.
    sol0 : array
        Least squares solution for the master equation.
    phi0 : array
        Values of zeroth order density matrix elements.
    phi1 : array
        Values of first order density matrix elements
        stored in nleads by ndm1 numpy array.
    current : array
        Values of the current having nleads entries.
    energy_current : array
        Values of the energy current having nleads entries.
    heat_current : array
        Values of the heat current having nleads entries.
    funcp : FunctionProperties
        FunctionProperties object.
    paulifct : array
        Factors used for generating Pauli master equation kernel.
    phi1fct : array
        Factors used for generating 1vN, Redfield master equation kernels.
    phi1fct_energy : array
        Factors used to calculate energy and heat currents in 1vN, Redfield approaches.
    tLba : array
        Jump operator matrix in many-body basis for Lindblad approach.
    """

    kerntype = 'not defined'
    indexing_class_name = 'StateIndexingDM'

    @staticmethod
    def generate_fct(sys): pass
    @staticmethod
    def generate_kern(sys): pass
    @staticmethod
    def generate_current(sys): pass
    @staticmethod
    def generate_vec(sys): pass

    def __init__(self, sys):
        """
        Initialization of the Approach class.

        Parameters
        ----------
        sys : Approach, Builder, etc. object.
            Any object having qd, leads, si, and funcp attributes.
        """
        self.qd = sys.qd
        self.leads = sys.leads
        self.si = sys.si
        self.funcp = sys.funcp
        self.restart()

    def restart(self):
        """Restart values of some variables."""
        self.kern, self.bvec, self.norm_vec = None, None, None
        self.sol0, self.phi0, self.phi1 = None, None, None
        self.current = None
        self.energy_current = None
        self.heat_current = None
        self.phi1fct, self.paulifct = None, None

    def set_phi0_init(self):
        if self.kerntype in {'Pauli', 'pyPauli'}:
            phi0_init = np.zeros(self.si.npauli, dtype=doublenp)
        else:
            phi0_init = np.zeros(self.si.ndm0r, dtype=doublenp)
        phi0_init[0] = 1.0
        return phi0_init

    def solve_kern(self):
        """Finds the stationary state using least squares or by inverting the matrix."""
        solmethod = self.funcp.solmethod
        symq = self.funcp.symq
        norm_row = self.funcp.norm_row

        # Determine the proper solution method
        if solmethod == 'n':
            solmethod = 'solve' if symq else 'lsqr'
        if not symq and solmethod != 'lsqr' and solmethod != 'lsmr':
            print("WARNING: Using solmethod=lsqr, because the kernel is not symmetric, symq=False.")
            solmethod = 'lsqr'
        self.funcp.solmethod = solmethod

        # Replace one equation by the normalisation condition
        if symq:
            kern, bvec = self.kern, self.bvec
            replaced_eq = np.array(kern[norm_row])
            kern[norm_row] = self.norm_vec
        else:
            kern, bvec = self.kern_ext, self.bvec_ext
            kern[-1] = self.norm_vec

        # Try to solve the master equation
        try:
            if   solmethod == 'solve': self.sol0 = [np.linalg.solve(kern, bvec)]
            elif solmethod == 'lsqr':  self.sol0 = np.linalg.lstsq(kern, bvec, rcond=None)

            self.phi0 = self.sol0[0]
            self.success = True
        except Exception as exept:
            self.funcp.print_error(exept)
            self.phi0 = np.zeros(self.si.ndm0r)
            self.success = False

        # Return back the replaced equation
        if symq:
            kern[norm_row] = replaced_eq

    def solve_matrix_free(self):
        """Finds the stationary state using matrix free methods like broyden, krylov, etc."""<commit_msg>Restore the old behaviour of NumPy (prior 1.14.0) for numpy.linalg.lstsq using rcond=-1.<commit_after>"""Module containing sample classes for approach definitions."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import scipy.sparse.linalg
from scipy import optimize

from .mytypes import doublenp
from .mytypes import complexnp

class Approach(object):
    """
    Sample class used to define different approaches.

    Attributes
    ----------
    qd : QuantumDot
        QuantumDot object.
    leads : LeadsTunneling
        LeadsTunneling object.
    si : StateIndexingDM
        StateIndexingDM object.
    kern : array
        Kernel (Liouvillian) representing the master equation.
    bvec : array
        Right hand side column vector for master equation.
        The entry funcp.norm_row is 1 representing normalization condition.
    sol0 : array
        Least squares solution for the master equation.
    phi0 : array
        Values of zeroth order density matrix elements.
    phi1 : array
        Values of first order density matrix elements
        stored in nleads by ndm1 numpy array.
    current : array
        Values of the current having nleads entries.
    energy_current : array
        Values of the energy current having nleads entries.
    heat_current : array
        Values of the heat current having nleads entries.
    funcp : FunctionProperties
        FunctionProperties object.
    paulifct : array
        Factors used for generating Pauli master equation kernel.
    phi1fct : array
        Factors used for generating 1vN, Redfield master equation kernels.
    phi1fct_energy : array
        Factors used to calculate energy and heat currents in 1vN, Redfield approaches.
    tLba : array
        Jump operator matrix in many-body basis for Lindblad approach.
    """

    kerntype = 'not defined'
    indexing_class_name = 'StateIndexingDM'

    @staticmethod
    def generate_fct(sys): pass
    @staticmethod
    def generate_kern(sys): pass
    @staticmethod
    def generate_current(sys): pass
    @staticmethod
    def generate_vec(sys): pass

    def __init__(self, sys):
        """
        Initialization of the Approach class.

        Parameters
        ----------
        sys : Approach, Builder, etc. object.
            Any object having qd, leads, si, and funcp attributes.
        """
        self.qd = sys.qd
        self.leads = sys.leads
        self.si = sys.si
        self.funcp = sys.funcp
        self.restart()

    def restart(self):
        """Restart values of some variables."""
        self.kern, self.bvec, self.norm_vec = None, None, None
        self.sol0, self.phi0, self.phi1 = None, None, None
        self.current = None
        self.energy_current = None
        self.heat_current = None
        self.phi1fct, self.paulifct = None, None

    def set_phi0_init(self):
        if self.kerntype in {'Pauli', 'pyPauli'}:
            phi0_init = np.zeros(self.si.npauli, dtype=doublenp)
        else:
            phi0_init = np.zeros(self.si.ndm0r, dtype=doublenp)
        phi0_init[0] = 1.0
        return phi0_init

    def solve_kern(self):
        """Finds the stationary state using least squares or by inverting the matrix."""
        solmethod = self.funcp.solmethod
        symq = self.funcp.symq
        norm_row = self.funcp.norm_row

        # Determine the proper solution method
        if solmethod == 'n':
            solmethod = 'solve' if symq else 'lsqr'
        if not symq and solmethod != 'lsqr' and solmethod != 'lsmr':
            print("WARNING: Using solmethod=lsqr, because the kernel is not symmetric, symq=False.")
            solmethod = 'lsqr'
        self.funcp.solmethod = solmethod

        # Replace one equation by the normalisation condition
        if symq:
            kern, bvec = self.kern, self.bvec
            replaced_eq = np.array(kern[norm_row])
            kern[norm_row] = self.norm_vec
        else:
            kern, bvec = self.kern_ext, self.bvec_ext
            kern[-1] = self.norm_vec

        # Try to solve the master equation
        try:
            if   solmethod == 'solve': self.sol0 = [np.linalg.solve(kern, bvec)]
            elif solmethod == 'lsqr':  self.sol0 = np.linalg.lstsq(kern, bvec, rcond=-1)

            self.phi0 = self.sol0[0]
            self.success = True
        except Exception as exept:
            self.funcp.print_error(exept)
            self.phi0 = np.zeros(self.si.ndm0r)
            self.success = False

        # Return back the replaced equation
        if symq:
            kern[norm_row] = replaced_eq

    def solve_matrix_free(self):
        """Finds the stationary state using matrix free methods like broyden, krylov, etc."""
{'fraction_non_alphanumeric': 0.07717448603057459, 'fraction_numerical': 0.006958355297838692, 'mean_word_length': 2.2254335260115607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7510185', 'n_tokens_mistral': 2841, 'n_tokens_neox': 2698, 'n_words': 880}