コード例 #1
0
def test_mu_m_lin():

    J,grad_J = make_coef_J(1,100)

    y0 = 1
    yT = 1
    T  = 1
    a  = 1


    import matplotlib.pyplot as plt 
    
    problem = Problem1(y0,yT,T,a,J,grad_J)

    for N in [100]:
        
        Mu = [0.2*N]
        t = np.linspace(0,T,N+1)
        res1 = problem.scipy_solver(N)
        plt.plot(t,res1.x,'r--')
        L2_diff = []
        for m in [2,4,8,16]:
            res2 = problem.scipy_penalty_solve(N,m,Mu)

            error = np.sqrt(trapz((res1.x-res2.x[:N+1])**2,t))
            L2_diff.append(error)
            plt.plot(t,res2.x[:N+1])
            print "||u1-um||_2 = %f for m=%d, error/m=%f" % (error,m,error/m)
        plt.legend(['m=1','m=2','m=4','m=8','m=16'],loc=4)
        plt.xlabel('t')
        plt.ylabel('control')
        plt.title('Controls for N='+str(N)+' and mu='+str(Mu[0]))
        plt.show()
コード例 #2
0
ファイル: test_lagrange.py プロジェクト: andrthu/mek4250
def test_LagAndPen():

    y0 = 1
    yT = 1
    T  = 1
    a  = 1
    N  = 700   
    m  = 2

    C = [1]
    D = [0]

    my = [0.1*N,0.7*N,2*N]

    for i in range(len(C)):
        for j in range(len(D)):

            J,grad_J = make_coef_J(C[i],D[j])
            
            problem = Problem1(y0,yT,T,a,J,grad_J)

            opt = {"mem_lim" :40}
            
            
            res1 = problem.penalty_solve(N,m,my,Lbfgs_options=opt)
            
            try:
                res2 = problem.lagrange_penalty_solve(N,m,my,Lbfgs_options=opt)
            except:
                res2 = [{'iteration':-1},{'iteration':-1},{'iteration':-1}]
            print 
            print res1[0]['iteration'],res1[1]['iteration'],res1[2]['iteration']
            print res2[0]['iteration'],res2[1]['iteration'],res2[2]['iteration']
コード例 #3
0
ファイル: scipy_test.py プロジェクト: andrthu/mek4250
def end_term_test(): 
    y0 = 1
    yT = 1
    T  = 1
    a  = 1
    N = 1000
    power = 4
    coef = [1,0.5]
    
    J, grad_J = make_coef_J(coef[0],coef[1],power=power)
        
    def Jfunc(u,y,yT,T,power):
        return J(u,y,yT,T)
            
    problem = GeneralPowerY(y0,yT,T,a,power,Jfunc,grad_J)

    problem.scipy_simple_test(N,make_plot=False)
コード例 #4
0
def end_term_test():
    y0 = 1
    yT = 1
    T = 1
    a = 1
    N = 1000
    power = 4
    coef = [1, 0.5]

    J, grad_J = make_coef_J(coef[0], coef[1], power=power)

    def Jfunc(u, y, yT, T, power):
        return J(u, y, yT, T)

    problem = GeneralPowerY(y0, yT, T, a, power, Jfunc, grad_J)

    problem.scipy_simple_test(N, make_plot=False)
コード例 #5
0
def test_constant_C():

    J,grad_J = make_coef_J(1,100)

    y0 = 1
    yT = 1
    T  = 1
    a  = 1


    
    
    problem = Problem1(y0,yT,T,a,J,grad_J)

    problem.mu_to_N_relation(50,0.2)


    problem2 = Explicit_quadratic(y0,yT,T,a,J,grad_J)
    

    problem2.mu_to_N_relation(50,0.2)
def test_quadratic_manufactured_solution():
    
    y0 = 1
    yT = 1
    T  = 1
    a  = 1
    
    solution = 3.1

    J,grad_J = make_coef_J(1,solution)

    
    problem = Explicit_quadratic(y0,yT,T,a,J,grad_J)
    N = 200000
    u = np.zeros(N+1) + solution
    yT = problem.ODE_solver(u,N)[-1]
    print "yT=%f"%yT
    
    h_val = []
    error = []
    error2 = [[],[],[]]
    M = [2,4,8]
    
    import matplotlib.pyplot as plt

    fig,ax = plt.subplots(2, 2)
    teller = -1
    for N in [50,100,150,200,500,1000]:
        
        h_val.append(1./N) 
        
        problem = Explicit_quadratic(y0,yT,T,a,J,grad_J)
        res = problem.scipy_solver(N,disp=False)

        #u = np.zeros(N+1)
        #problem.finite_diff(u,N)
        t = np.linspace(0,T,N+1)
        
        if N!= 100 and N!=500:
            teller += 1
            ax[teller/2,teller%2].plot(t,res.x)
            
        
        err = max(abs(res.x-solution))
        error.append(err)
        print "max(|u-%.1f|)=%f for N=%d and iter=%d"%(solution,err,N,res.nit)
        
        for i in range(len(M)):
            res2 = problem.scipy_penalty_solve(N,M[i],[100])

            err2 = max(abs(res2.x[:N+1]-solution))
            print "m=%d: err=%f for N=%d and iter=%d"%(M[i],err2,N,res2.nit)
            error2[i].append(err2)

            
            if N!= 100 and N!=500:
                ax[teller/2,teller%2].plot(t,res2.x[:N+1])
        if N!= 100 and N!=500:
            ax[teller/2,teller%2].legend(['m=1','m=2','m=4','m=8'],loc=4)
            ax[teller/2,teller%2].set_title('N='+str(N))
        
    plt.show()
            

    Q = np.vstack([np.log(np.array(h_val)),np.ones(len(h_val))]).T
    LS=linalg.lstsq(Q, np.log(np.array(error)))[0]
    print LS[0],np.exp(LS[1])

    

    plt.plot(np.log(np.array(h_val)),np.log(np.array(error)))
    plt.xlabel('log(1/N)')
    plt.ylabel('log(error)')
    plt.show()

    for i in range(len(M)):
        Q = np.vstack([np.log(np.array(h_val)),np.ones(len(h_val))]).T
        LS=linalg.lstsq(Q, np.log(np.array(error2[i])))[0]
        print LS[0],np.exp(LS[1])
        plt.plot(np.log(np.array(h_val)),np.log(np.array(error2[i])))
        plt.show()
def test_manufactured_solution():
    
    y0 = 1
    yT = 2*np.exp(1) - 1
    T  = 1
    a  = 1
    import matplotlib.pyplot as plt

    J,grad_J = make_coef_J(1,1)

    M = [2,4,8]
    h_val = []
    error = []
    error2 = [[],[],[]]

    fig,ax = plt.subplots(2, 2)
    teller = -1
    for N in [500,1000,1500,2000,5000,8000]:
        
        h_val.append(1./N) 
        
        problem = Problem1(y0,yT,T,a,J,grad_J)
        

        res = problem.scipy_solver(N,disp=False)
        
        err = max(abs(res.x-1))
        error.append(err)
        print "max(|u-1|)=%f for N=%d and iter=%d"%(err,N,res.nit)
        t = np.linspace(0,T,N+1)
        if N!= 1000 and N!=5000:
            teller += 1
            ax[teller/2,teller%2].plot(t,res.x)
        for i in range(len(M)):
            res2 = problem.scipy_penalty_solve(N,M[i],[10])

            err2 = max(abs(res2.x[:N+1]-1))
            print "m=%d: err=%f for N=%d and iter=%d"%(M[i],err2,N,res2.nit)
            error2[i].append(err2)
            if N!= 1000 and N!=5000:
                ax[teller/2,teller%2].plot(t,res2.x[:N+1])
        if N!= 1000 and N!=5000:
            ax[teller/2,teller%2].legend(['m=1','m=2','m=4','m=8'],loc=4)
            ax[teller/2,teller%2].set_title('N='+str(N))
            
            
    plt.show()

            
    Q = np.vstack([np.log(np.array(h_val)),np.ones(len(h_val))]).T
    LS=linalg.lstsq(Q, np.log(np.array(error)))[0]
    print LS[0],np.exp(LS[1])

    

    for i in range(len(M)):
        Q = np.vstack([np.log(np.array(h_val)),np.ones(len(h_val))]).T
        LS=linalg.lstsq(Q, np.log(np.array(error2[i])))[0]
        print LS[0],np.exp(LS[1])
        plt.plot(np.log(np.array(h_val)),np.log(np.array(error2[i])))
        plt.show()

    plt.plot(np.log(np.array(h_val)),np.log(np.array(error)))
    plt.xlabel('log(1/N)')
    plt.ylabel('log(error)')
    plt.show()
def test_manufactured_resultChange_solution():
    
    solution = 20
    T = 1
    y0 = 1
    yT = (solution+1)*np.exp(T) - solution    
    a  = 1
    power = 4

    J,grad_J = make_coef_J(1,solution,power=power)
    def Jfunc(u,y,yT,T,power):
        return J(u,y,yT,T)

    import matplotlib.pyplot as plt
    h_val = []
    error = []
    error2=[[],[],[]]
    M=[2,4,8]
    

    opt={'gtol': 1e-6, 'disp': False,'maxcor':10}

    fig,ax = plt.subplots(2, 2)
    teller = -1
    for N in [50,100,150,200,500,1000]:
        
        h_val.append(1./N) 
        t = np.linspace(0,T,N+1)
        problem = GeneralPowerY(y0,yT,T,a,power,Jfunc,grad_J)
        #u = np.zeros(N+1)
        #problem.finite_diff(u,N)

        res = problem.scipy_solver(N,disp=False,options=opt)
        
        err = max(abs(res.x-solution))
        error.append(err)
        print "max(|u-%.1f|)=%f for N=%d and iter=%d"%(solution,err,N,res.nit)
        
        
        #plt.plot(t,2*t+19)
        if N!= 100 and N!=500:
            teller += 1
            ax[teller/2,teller%2].plot(t,res.x)

        
        for i in range(len(M)):
            res2 = problem.scipy_penalty_solve(N,M[i],[1],options=opt)

            err2 = max(abs(res2.x[:N+1]-solution))
            print "m=%d: err=%f for N=%d and iter=%d"%(M[i],err2,N,res2.nit)
            error2[i].append(err2)

            if N!= 100 and N!=500:
                ax[teller/2,teller%2].plot(t,res2.x[:N+1])
        if N!= 100 and N!=500:
            ax[teller/2,teller%2].legend(['m=1','m=2','m=4','m=8'],loc=4)
            ax[teller/2,teller%2].set_title('N='+str(N))
        
    plt.show()
    Q = np.vstack([np.log(np.array(h_val)),np.ones(len(h_val))]).T
    LS=linalg.lstsq(Q, np.log(np.array(error)))[0]
    print LS[0],np.exp(LS[1])

    

    plt.plot(np.log(np.array(h_val)),np.log(np.array(error)))
    plt.xlabel('log(1/N)')
    plt.ylabel('log(error)')
    plt.show()

    for i in range(len(M)):
        Q = np.vstack([np.log(np.array(h_val)),np.ones(len(h_val))]).T
        LS=linalg.lstsq(Q, np.log(np.array(error2[i])))[0]
        print LS[0],np.exp(LS[1])
        plt.plot(np.log(np.array(h_val)),np.log(np.array(error2[i])))
        plt.show()