Ejemplo n.º 1
0
def testDenseAddIdentity():
    """
    Simple test of denseAddIdentity to check it works like the scipy
    equivalent.
    """

    N = 3
    I = np.identity(N)

    A_sp = np.random.random(size=(N, N))
    A = np.asfortranarray(A_sp)

    denseAddIdentity(A)

    assert_allclose(A, A_sp + I)
Ejemplo n.º 2
0
def testDenseAddIdentity():
    """
    Simple test of denseAddIdentity to check it works like the scipy
    equivalent.
    """
    
    N = 3
    I = np.identity(N)
    
    A_sp = np.random.random(size=(N,N))
    A = np.asfortranarray(A_sp)
    
    denseAddIdentity(A)
    
    assert_allclose(A, A_sp+I)
Ejemplo n.º 3
0
    def SpilsPrecSetup(self, t, c, fc, jok, jcurPtr, gamma, vtemp1, vtemp2,
                       vtemp3):
        """
        This routine generates the block-diagonal part of the Jacobian
        corresponding to the interaction rates, multiplies by -gamma, adds
        the identity matrix, and calls denseGETRF to do the LU decomposition of
        each diagonal block. The computation of the diagonal blocks uses
        the preset block and grouping information. One block per group is
        computed. The Jacobian elements are generated by difference
        quotients using calls to the routine fblock.

        This routine can be regarded as a prototype for the general case
        of a block-diagonal preconditioner. The blocks are of size mp, and
        there are ngrp=ngx*ngy blocks computed in the block-grouping scheme.
        """

        cdata = c.data
        rewt = self.rewt
        self.errWeights(rewt)

        rewtdata = rewt.data

        uround = UNIT_ROUNDOFF

        P = self.P
        pivot = self.pivot
        jxr = self.jxr
        jyr = self.jyr
        mp = self.mp
        srur = self.srur
        #ngrp = self.ngrp
        ngx = self.ngx
        ngy = self.ngy
        #mxmp = self.mxmp
        fsave = self.fsave
        """
        Make mp calls to fblock to approximate each diagonal block of Jacobian.
        Here, fsave contains the base value of the rate vector and 
        r0 is a minimum increment factor for the difference quotient. 
        """

        f1 = vtemp1.data

        fac = fc.WrmsNorm(rewt)
        r0 = 1000.0 * np.abs(gamma) * uround * NEQ * fac
        if (r0 == ZERO): r0 = ONE

        for igy in range(ngy):
            jy = jyr[igy]
            #if00 = jy*mxmp
            for igx in range(ngx):
                jx = jxr[igx]
                #if0 = if00 + jx*mp
                #ig = igx + igy*ngx
                # Generate ig-th diagonal block
                for j in range(mp):
                    # Generate the jth column as a difference quotient
                    #jj = if0 + j
                    save = cdata[:, igx, igy]
                    r = np.max(srur * np.abs(save), r0 / rewtdata[:, igx, igy])
                    cdata[:, igx, igy] += r
                    fac = -gamma / r
                    self.fblock(t, cdata, jx, jy, f1)
                    for i in range(mp):
                        P[igx, igy, j, i] = (f1[i] - fsave[i, jx, jy]) * fac

                    cdata[j, jx, jy] = save

        # Add identity matrix and do LU decompositions on blocks.

        for igy in range(ngy):
            for igx in range(ngx):
                denseAddIdentity(P[igx, igy, ...])
                ier = denseGETRF(P[igx, igy, ...], pivot[igx, igy, ...])
                if (ier != 0): return 1

            jcurPtr = True

        return 0