def setUp(self, ):

        # temporary local variables for convenience
        NEQ = self.NEQ
        ZERO = self.ZERO
        ONE = self.ONE

        self.data = FerTron([self.PT25, self.ONEPT5], [ONE, self.TWO * np.pi])

        #/* Create serial vectors of length NEQ */

        self.u = NvectorNdarrayFloat64(NEQ)  # temporary for init
        self.s = NvectorNdarrayFloat64(NEQ)
        self.c = NvectorNdarrayFloat64(NEQ)

        self.s.Constant(ONE)  # /* no scaling */

        self.c.data[0] = ZERO  #/* no constraint on x1 */
        self.c.data[1] = ZERO  #/* no constraint on x2 */
        self.c.data[2] = ONE  #/* l1 = x1 - x1_min >= 0 */
        self.c.data[3] = -ONE  #/* L1 = x1 - x1_max <= 0 */
        self.c.data[4] = ONE  #/* l2 = x2 - x2_min >= 0 */
        self.c.data[5] = -ONE  #/* L2 = x2 - x22_min <= 0 */

        # Seutp dense solver and tolerances

        self.data.initSolver(self.u)
        self.data.setupDenseLinearSolver(self.u.Size())

        self.data.funcNormTol = self.FTOL
        self.data.scaledStepTol = self.STOL
        self.data.SetConstraints(self.c)
    def __init__(self, mx, my, num_species, ax, ay, uround):
        self.mx = mx
        self.my = my
        self.ns = num_species
        self.np = num_species / 2
        self.ax = ax
        self.ay = ay
        self.dx = ax / (mx - 1)
        self.dy = ay / (my - 1)
        self.uround = uround
        self.sqruround = np.sqrt(uround)

        #/*
        # * Allocate memory for data structure of type UserData
        # */
        self.P = np.empty((mx, my, num_species, num_species), dtype=np.float64)
        self.pivot = np.zeros((mx, my, num_species), dtype=np.int32)
        self.acoef = np.empty((num_species, num_species))
        self.bcoef = np.empty(num_species)
        self.cox = np.empty(num_species)
        self.coy = np.empty(num_species)
        self.rates = NvectorNdarrayFloat64((mx, my, num_species))

        # /* Set up the coefficients a and b plus others found in the equations */

        for i in range(self.np):
            a1 = (i, self.np)
            a2 = (i + self.np, 0)

            for j in range(self.np):
                self.acoef[i, self.np + j] = -GG
                self.acoef[i + self.np, j] = EE
                self.acoef[i, j] = ZERO
                self.acoef[i + self.np, self.np + j] = ZERO

            self.acoef[i, i] = -AA
            self.acoef[i + self.np, i + self.np] = -AA

            self.bcoef[i] = BB
            self.bcoef[i + self.np] = -BB

            self.cox[i] = DPREY / self.dx**2
            self.cox[i + self.np] = DPRED / self.dx**2

            self.coy[i] = DPREY / self.dy**2
            self.coy[i + self.np] = DPRED / self.dy**2
    def SetInitialGuess2(self, data):
        """
        Initial guess 2
        
        There are two known solutions for this problem    
        this init. guess should take us to (0.5; 3.1415926)        
        """
        u = NvectorNdarrayFloat64(self.NEQ)

        lb = data.lb
        ub = data.ub

        x1 = self.PT5 * (lb[0] + ub[0])
        x2 = self.PT5 * (lb[1] + ub[1])

        u.data[0] = x1
        u.data[1] = x2
        u.data[2] = x1 - lb[0]
        u.data[3] = x1 - ub[0]
        u.data[4] = x2 - lb[1]
        u.data[5] = x2 - ub[1]

        return u
    def SetInitialGuess1(self, data):
        """
        Initial guess 1
        
        There are two known solutions for this problem    
        this init. guess should take us to (0.29945; 2.83693)
        """
        u = NvectorNdarrayFloat64(self.NEQ)

        lb = data.lb
        ub = data.ub

        x1 = lb[0]
        x2 = lb[1]

        u.data[0] = x1
        u.data[1] = x2
        u.data[2] = x1 - lb[0]
        u.data[3] = x1 - ub[0]
        u.data[4] = x2 - lb[1]
        u.data[5] = x2 - ub[1]

        return u
Esempio n. 5
0
 * Get and print some final statistics
    """
    print "\nFinal Statistics:"
    print "nst = %-6ld nfe  = %-6ld nsetups = %-6ld nfeLS = %-6ld nje = %ld" % (
        cv.numSteps, cv.numRhsEvals, cv.numLinSolvSetups, cv.dlsNumRhsEvals,
        cv.dlsNumJacEvals)
    print "nni = %-6ld ncfn = %-6ld netf = %-6ld nge = %ld\n" % (
        cv.numNonlinSolvIters, cv.numNonlinSolvConvFails, cv.numErrTestFails,
        cv.numGEvals)


if __name__ == '__main__':
    # Main Program
    # Create serial vector of length NEQ for I.C. and abstol

    y = NvectorNdarrayFloat64(NEQ)
    abstol = NvectorNdarrayFloat64(NEQ)

    # Initialize y
    y.data[:] = [Y1, Y2, Y3]

    # Set the scalar relative tolerance
    reltol = RTOL
    # Set the vector absolute tolerance
    abstol.data[:] = [ATOL1, ATOL2, ATOL3]

    # Call CVodeCreate to create the solver memory and specify the
    # Backward Differentiation Formula and the use of a Newton iteration
    # Call CVodeRootInit to specify the root function g with 2 components
    cvode_mem = Roberts(multistep='bdf', iteration='newton', nrtfn=2)
Esempio n. 6
0
def main():
    abstol = ATOL
    reltol = RTOL

    # Initializations
    c = NvectorNdarrayFloat64(SHP)

    wdata = WebData()

    ns = wdata.ns
    mxns = wdata.mxns

    # Print problem description
    wdata.PrintIntro()

    # Loop over jpre and gstype (four cases)
    for jpre in (
            'left',
            'right',
    ):
        for gstype in ('modified', 'classical'):
            # Initialize c and print heading
            wdata.CInit(c)
            wdata.PrintHeader(jpre, gstype)

            # Call CVodeInit or CVodeReInit, then CVSpgmr to set up problem

            firstrun = jpre == 'left' and gstype == 'modified'
            if firstrun:
                # Already done above in WebData cinit
                #cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON);

                wdata.initSolver(T0, c)
                wdata.setTolerances(reltol, abstol)

                wdata.setupIndirectLinearSolver('spgmr',
                                                prectype=jpre,
                                                maxl=MAXL,
                                                gstype=gstype,
                                                epslin=DELT)

            else:

                wdata.reInitSolver(T0, c)
                wdata.spilsPrecType = jpre
                wdata.spilsGSType = gstype

            # Print initial values
            if firstrun:
                wdata.PrintAllSpecies(c, T0)

            # Loop over output points, call CVode, print sample solution values.
            tout = T1

            for iout in range(1, NOUT):
                flag, t = wdata.Solve(tout, c)

                wdata.PrintOutput(t)
                if firstrun and iout % 3 == 0:
                    wdata.PrintAllSpecies(c, t)

                if (tout > 0.9):
                    tout += DTOUT
                else:
                    tout *= TOUT_MULT

            # Print final statistics, and loop for next case
            wdata.PrintFinalStats()
Esempio n. 7
0
    def __init__(self, *args, **kwds):
        """
        Initialise User Data
        """
        Cvode.__init__(self, **kwds)

        ns = self.ns = NS

        self.P = np.empty((NGX, NGY, NS, NS))
        self.pivot = np.empty(NS, dtype=np.int64)

        self.rewt = NvectorNdarrayFloat64(SHP)

        self.jgx = np.empty(NGX + 1, dtype=np.int64)
        self.jgy = np.empty(NGY + 1, dtype=np.int64)
        self.jigx = np.empty(MX, dtype=np.int64)
        self.jigy = np.empty(MY, dtype=np.int64)

        self.jxr = np.empty(NGX, dtype=np.int64)
        self.jyr = np.empty(NGY, dtype=np.int64)

        self.acoef = np.empty((NS, NS))
        self.bcoef = np.empty(NS)
        self.diff = np.empty(NS)
        self.cox = np.empty(NS)
        self.coy = np.empty(NS)
        self.fsave = np.empty(SHP)

        for j in range(NS):
            for i in range(NS):
                self.acoef[i, j] = 0.

        for j in range(NP):
            for i in range(NP):
                self.acoef[NP + i][j] = EE
                self.acoef[i][NP + j] = -GG
            self.acoef[j, j] = -AA
            self.acoef[NP + j, NP + j] = -AA
            self.bcoef[j] = BB
            self.bcoef[NP + j] = -BB
            self.diff[j] = DPREY
            self.diff[NP + j] = DPRED

        # Set remaining problem parameters

        self.mxns = MXNS
        dx = self.dx = DX
        dy = self.dy = DY
        for i in range(ns):
            self.cox[i] = self.diff[i] / np.sqrt(dx)
            self.coy[i] = self.diff[i] / np.sqrt(dy)

        # Set remaining method parameters

        self.mp = MP
        self.mq = MQ
        self.mx = MX
        self.my = MY
        self.srur = np.sqrt(UNIT_ROUNDOFF)
        self.mxmp = MXMP
        self.ngrp = NGRP
        self.ngx = NGX
        self.ngy = NGY
        self.SetGroups(MX, NGX, self.jgx, self.jigx, self.jxr)
        self.SetGroups(MY, NGY, self.jgy, self.jigy, self.jyr)
                #print jx, jy, P[jx,jy,...], pivot[jx,jy,...], zdata[jx,jy,:]

        return 0


# Main Program

if __name__ == '__main__':

    # Call CVodeCreate to create the solver memory and specify the
    # Backward Differentiation Formula and the use of a Newton iteration

    cvode_mem = Diurnal(multistep='bdf', iteration='newton')

    # Allocate memory, and set problem data, initial values, tolerances
    u = NvectorNdarrayFloat64((MX, MY, NUM_SPECIES))

    cvode_mem.SetInitialProfiles(u, )
    abstol = ATOL
    reltol = RTOL

    # Call CVodeInit to initialize the integrator memory and specify the
    # user's right hand side function in u'=f(t,u), the inital time T0, and
    # the initial dependent variable vector u.
    cvode_mem.initSolver(T0, u)
    # Call CVodeSStolerances to specify the scalar relative tolerance
    # and scalar absolute tolerances
    cvode_mem.setTolerances(reltol, abstol)

    # Call CVSpgmr to specify the linear solver CVSPGMR
    # with left preconditioning and the maximum Krylov dimension maxl
def testKinFoodWeb():

    #int globalstrategy;
    #realtype fnormtol, scsteptol;
    #N_Vector cc, sc, constraints;
    #UserData data;
    #int flag, maxl, maxlrst;
    #void *kmem;

    data = FoodWeb(MX, MY, NUM_SPECIES, AX, AY, 1E-10)

    #/* Create serial vectors of length NEQ */
    cc = NvectorNdarrayFloat64((MX, MY, NUM_SPECIES))
    sc = NvectorNdarrayFloat64((MX, MY, NUM_SPECIES))

    constraints = NvectorNdarrayFloat64((MX, MY, NUM_SPECIES))
    constraints.Constant(TWO)

    data.SetInitialProfiles(cc, sc)

    fnormtol = FTOL
    scsteptol = STOL

    #/* Call KINCreate/KINInit to initialize KINSOL.
    #A pointer to KINSOL problem memory is returned and stored in kmem. */

    #/* Vector cc passed as template vector. */
    data.SetConstraints(constraints)

    #/* Call KINSpgmr to specify the linear solver KINSPGMR with preconditioner
    #routines PrecSetupBD and PrecSolveBD. */
    maxl = 15
    maxlrst = 2
    strategy = 'none'

    data.initSolver(cc)
    data.setupIndirectLinearSolver(solver='spgmr', maxl=maxl, user_pre=True)

    data.funcNormTol = FTOL
    data.scaledStepTol = STOL

    data.spilsMaxRestarts = maxlrst

    #/* Print out the problem size, solution parameters, initial guess. */
    PrintHeader(strategy, maxl, maxlrst, fnormtol, scsteptol)

    #/* Call KINSol and print output concentration profile */
    print 'Solving...'
    flag = data.Solve(cc, sc, sc, strategy=strategy)
    #  flag = KINSol(kmem,           /* KINSol memory block */
    #                cc,             /* initial guess on input; solution vector */
    #                globalstrategy, /* global stragegy choice */
    #                sc,             /* scaling vector, for the variable cc */
    #                sc);            /* scaling vector for function values fval */

    print "\n\nComputed equilibrium species concentrations:\n"
    PrintAndAssertOutput(cc)

    #/* Print final statistics and free memory */
    data.PrintFinalStats()

    return 0