Example #1
0
 def dvdn_exact(beta, mu0, r, R, n):
     from scitools.std import log
     term1 = (beta / (2. * mu0))**(1. / n)
     term2 = n / (n + 1.)
     term3 = (R**(1 + 1. / n) - r**(1 + 1. / n))
     sum = 0
     sum += -1./n**2 * log(beta/(2.*mu0)) * \
       term1 * term2 * term3
     sum += 1. / (n + 1)**2 * term1 * term3
     sum += (-1./n**2) * \
      (R**(1+1./n)*log(R) - r**(1+1./n)*log(r))\
       * term1 * term2
     return sum
	def dvdn_exact(beta, mu0, r, R, n):
		from scitools.std import log
		term1 = (beta / (2.*mu0))**(1./n)
		term2 = n/(n+1.)
		term3 = (R**(1+1./n) - r**(1+1./n))
		sum = 0
		sum += -1./n**2 * log(beta/(2.*mu0)) * \
			 term1 * term2 * term3
		sum += 1./(n+1)**2 * term1 * term3
		sum += (-1./n**2) * \
			(R**(1+1./n)*log(R) - r**(1+1./n)*log(r))\
			 * term1 * term2
		return sum
Example #3
0
def test_convergenceRates():
    """
    Test for the convergence rates of the solver.
    The expected result is that the variable r takes the value 2, because
    the Crank-Nicolson scheme and the geometric average have errors of order
    dt**2. The final error should then be O(dt**2) which gives r=2. 
    """
    dt_start = 1.0
    num_dt = 10
    E_values = zeros(num_dt)
    T = 10.
    g = 9.81
    m = 50.
    Cd = 1.2
    rho = 1.0
    A = 0.5
    a = Cd * rho * A / (2. * m)

    dt = zeros(num_dt)
    dt[0] = dt_start
    for i in range(1, len(dt)):
        dt[i] = dt[i - 1] / 2.

    D = -0.39
    B = 0.76
    C = -0.145

    def exact(t):
        return D * t**3 + B * t + C

    def src(t):
        return m * g + m * a * abs(
            exact(t)) * exact(t) + m * (3 * D * t**2 + B)

    # Simulate for different timesteps, and store the error in E_values
    for i in range(num_dt):
        v, t = solver(T, dt[i], exact(0), Cd, rho, A, m, src)
        ve = exact(t)
        diff = v - ve
        E_values[i] = sqrt(dt[i] * sum(diff**2))

    # Calculate r-values corresponding to the error with each different timestep
    r = zeros(len(E_values) - 1)
    for i in range(1, len(r)):
        r[i] = (log(E_values[i - 1] / E_values[i])) / (log(dt[i - 1] / dt[i]))
    import nose.tools as nt
    nt.assert_almost_equal(r[-1], 2, delta=0.1)
Example #4
0
def test_convergenceRates():
    """
    Test for the convergence rates of the solver.
    The expected result is that the variable r takes the value 2, because
    the Crank-Nicolson scheme and the geometric average have errors of order
    dt**2. The final error should then be O(dt**2) which gives r=2. 
    """
    dt_start = 1.0; num_dt = 10
    E_values = zeros(num_dt)
    T = 10.; g = 9.81; m = 50.; Cd = 1.2; rho = 1.0; A = 0.5;
    a = Cd*rho*A/(2.*m)
    
    dt = zeros(num_dt); dt[0] = dt_start
    for i in range(1,len(dt)):
        dt[i] = dt[i-1]/2.
    
    D = -0.39; B = 0.76; C = -0.145
    def exact(t):
        return D*t**3  + B*t + C
    def src(t):
        return m*g + m*a*abs(exact(t))*exact(t) + m*(3*D*t**2 + B)
    
     # Simulate for different timesteps, and store the error in E_values
    for i in range(num_dt):
        v, t = solver(T, dt[i], exact(0), Cd, rho, A, m, src)
        ve = exact(t)
        diff = v - ve
        E_values[i] = sqrt(dt[i]*sum(diff**2))
    
    # Calculate r-values corresponding to the error with each different timestep
    r=zeros(len(E_values)-1)
    for i in range(1, len(r)):
        r[i] = (log(E_values[i-1]/E_values[i]))/(log(dt[i-1]/dt[i]))
    import nose.tools as nt
    nt.assert_almost_equal(r[-1], 2, delta=0.1)
    

    
Example #5
0
def visualize(t):
    plot(range(len(t)), log(abs(t)), ’ro’)
    # or just plot(log(abs(t)), ’ro’)
    show()
Example #6
0
A1 = 15  # amplitude of the daily temperature variations (in C)
P1 = 24 * 60 * 60.  # oscillation period of 24 h (in seconds)
# angular frequency of daily temperature variations (in rad/s)
omega1 = 2 * pi / P1
a1 = sqrt(omega1 / (2 * k))

A2 = 7  # amplitude of yearly temperature variations (in C)
P2 = 24 * 60 * 60 * 365.  # oscillation period of 1 yr (in seconds)
# angular frequency of yearly temperature variations (in rad/s)
omega2 = 2 * pi / P2
a2 = sqrt(omega2 / (2 * k))

dt = P2 / 30  # time lag: 0.1 yr
tmax = 3 * P2  # 3 year simulation
T0 = 10  # mean surface temperature in Celsius
D = -(1 / a1) * log(0.001)  # max depth
n = 501  # no of points in the z direction

z = linspace(0, D, n)


def z_scaled(x, s):
    a = x[0]
    b = x[-1]
    return a + (b - a) * ((x - a) / (b - a))**s


zs = z_scaled(z, 3)

animate(tmax, dt, zs, T, T0 - A2 - A1, T0 + A2 + A1, 0, 'z', 'T')
Example #7
0
k = 1E-6            # thermal diffusivity (in m**2/s)

A1 = 15             # amplitude of the daily temperature variations (in C)
P1 = 24 * 60 * 60.      # oscillation period of 24 h (in seconds)
omega1 = 2 * pi / P1   # angular freq of daily temp variations (in rad/s)
a1 = sqrt(omega1 / (2 * k))

A2 = 7                    # amplitude of yearly temperature variations (in C)
P2 = 24 * 60 * 60 * 365.  # oscillation period of 1 yr (in seconds)
omega2 = 2 * pi / P2      # angular freq of yearly temp variations (in rad/s)
a2 = sqrt(omega2 / (2 * k))

dt = P2 / 20            # time lag: 0.1 yr
tmax = 3 * P2               # 3 year simulation
T0 = 10                 # mean surface temperature in Celsius
D = -(1 / a1) * log(0.001)  # max depth
n = 501                 # no of points in the z direction

# set T0, A, k, omega, D, n, tmax, dt
z = linspace(0, D, n)
animate(tmax, dt, z, T, T0 - A2 - A1, T0 + A2 + A1, 0, 'z', 'T')

movie('tmp_*.png', encoder='convert', fps=6, outputfile='tmp_heatwave.gif')

import glob
import os
# Remove frames
for filename in glob.glob('tmp_*.png'):
    os.remove(filename)
Example #8
0
	def f(x):
		return log(x)
Example #9
0
def test_undampedWaves():
    
    #define constants given in exercise
    A = 1
    mx=7.
    my=2.
    
    #define function that give
    q=lambda x,y: 1
    
    #define some variables
    Lx = 3
    Ly = 1.3
    T = 1
    C = 0.5
    dt= 0.1
    
    #define omega so equation holds
    w=pi*sqrt((mx/Lx)**2 +(my/Ly)**2)
    
    #help varabeles
    kx = pi*mx/Lx
    ky = pi*my/Ly
    
    #Exact solution
    ue = lambda x,y,t: A*cos(x*kx)*cos(y*ky)*cos(t*w)
    
    #initial condition so we get result we want.
    I = lambda x,y: A*cos(x*kx)*cos(y*ky)
    
   
    #factor dt decreeses per step
    step=0.5
    #number of steps I want to do
    val=5
    #array to store errors
    E=zeros(val)
    
    
    
    for i in range(val):
        v='vector'
        #solve eqation
        u,x,y,t,e=solver(I,None,None,q,0,Lx,Ly,dt*step**(i),T,C,1,mode=v,ue=ue)
        
        
        E[i]=e
        
    #find convergence rate between diffrent dt values
    r =zeros(val-1)
    r = log(E[1:]/E[:-1])/log(step)

    
    print "Test convergence for undamped waves:"
    for i in range(val):
        if i==0:
            print "dt: ",dt," Error: ",E[i]
        else:
            print "dt: ",dt*step**(i)," Error: ",E[i], "rate of con.: ", r[i-1]
        
    
    #requiere close to 2 in convergence rate for last r.
    assert abs(r[-1]-2)<0.01 
Example #10
0
def test_manufactured():

    A=1
    B=0
    mx=1
    my=1
    
    
    b=1
    c=1.1

    #define some variables
    Lx = 2.
    Ly = 2.
    T = 1
    C = 0.3
    dt= 0.1

    #help varabeles
    kx = pi*mx/Lx
    ky = pi*my/Ly
    w=1

    #Exact solution
    ue = lambda x,y,t: A*cos(x*kx)*cos(y*ky)*cos(t*w)*exp(-c*t)
    I = lambda x,y: A*cos(x*kx)*cos(y*ky)
    V = lambda x,y: -c*A*cos(x*kx)*cos(y*ky)
    q = lambda x,y: x**2+y**2

    f = lambda x,y,t:A*(-b*(c*cos(t*w) + w*sin(t*w))*cos(kx*x)*cos(ky*y) + c**2*cos(kx*x)*cos(ky*y)*cos(t*w) + 2*c*w*sin(t*w)*cos(kx*x)*cos(ky*y) + kx**2*(x**2 + y**2)*cos(kx*x)*cos(ky*y)*cos(t*w) + 2*kx*x*sin(kx*x)*cos(ky*y)*cos(t*w) + ky**2*(x**2 + y**2)*cos(kx*x)*cos(ky*y)*cos(t*w) + 2*ky*y*sin(ky*y)*cos(kx*x)*cos(t*w) - w**2*cos(kx*x)*cos(ky*y)*cos(t*w))*exp(-c*t)
    

    
    
    

    #factor dt decreeses per step
    step=0.5
    #number of steps I want to do
    val=5
    #array to store errors
    E=zeros(val)
    
    
    
    for i in range(val):
        v='vector'
        #solve eqation
        u,x,y,t,e=solver(I,V,f,q,b,Lx,Ly,dt*step**(i),T,C,8,mode=v,ue=ue)
        
        
        E[i]=e
        
    #find convergence rate between diffrent dt values
    r =zeros(val-1)
    r = log(E[1:]/E[:-1])/log(step)

    print
    print "Convergence rates for manufactured solution:" 
    for i in range(val):
        if i==0:
            print "dt: ",dt," Error: ",E[i]
        else:
            print "dt: ",dt*step**(i)," Error: ",E[i], "rate of con.: ", r[i-1]
        
    
    #requiere "close" to 2 in convergence rate for last r.
    assert abs(r[-1]-2)<0.5
Example #11
0
def test_manufactured():

    A = 1
    B = 0
    mx = 1
    my = 1

    b = 1
    c = 1.1

    #define some variables
    Lx = 2.
    Ly = 2.
    T = 1
    C = 0.3
    dt = 0.1

    #help varabeles
    kx = pi * mx / Lx
    ky = pi * my / Ly
    w = 1

    #Exact solution
    ue = lambda x, y, t: A * cos(x * kx) * cos(y * ky) * cos(t * w) * exp(-c *
                                                                          t)
    I = lambda x, y: A * cos(x * kx) * cos(y * ky)
    V = lambda x, y: -c * A * cos(x * kx) * cos(y * ky)
    q = lambda x, y: x**2 + y**2

    f = lambda x, y, t: A * (
        -b * (c * cos(t * w) + w * sin(t * w)) * cos(kx * x) * cos(ky * y) + c
        **2 * cos(kx * x) * cos(ky * y) * cos(t * w) + 2 * c * w * sin(
            t * w) * cos(kx * x) * cos(ky * y) + kx**2 *
        (x**2 + y**2) * cos(kx * x) * cos(ky * y) * cos(t * w) + 2 * kx * x *
        sin(kx * x) * cos(ky * y) * cos(t * w) + ky**2 *
        (x**2 + y**2) * cos(kx * x) * cos(ky * y) * cos(t * w) + 2 * ky * y *
        sin(ky * y) * cos(kx * x) * cos(t * w) - w**2 * cos(kx * x) * cos(
            ky * y) * cos(t * w)) * exp(-c * t)

    #factor dt decreeses per step
    step = 0.5
    #number of steps I want to do
    val = 5
    #array to store errors
    E = zeros(val)

    for i in range(val):
        v = 'vector'
        #solve eqation
        u, x, y, t, e = solver(I,
                               V,
                               f,
                               q,
                               b,
                               Lx,
                               Ly,
                               dt * step**(i),
                               T,
                               C,
                               8,
                               mode=v,
                               ue=ue)

        E[i] = e

    #find convergence rate between diffrent dt values
    r = zeros(val - 1)
    r = log(E[1:] / E[:-1]) / log(step)

    print
    print "Convergence rates for manufactured solution:"
    for i in range(val):
        if i == 0:
            print "dt: ", dt, " Error: ", E[i]
        else:
            print "dt: ", dt * step**(
                i), " Error: ", E[i], "rate of con.: ", r[i - 1]

    #requiere "close" to 2 in convergence rate for last r.
    assert abs(r[-1] - 2) < 0.5
Example #12
0
def test_undampedWaves():

    #define constants given in exercise
    A = 1
    mx = 7.
    my = 2.

    #define function that give
    q = lambda x, y: 1

    #define some variables
    Lx = 3
    Ly = 1.3
    T = 1
    C = 0.5
    dt = 0.1

    #define omega so equation holds
    w = pi * sqrt((mx / Lx)**2 + (my / Ly)**2)

    #help varabeles
    kx = pi * mx / Lx
    ky = pi * my / Ly

    #Exact solution
    ue = lambda x, y, t: A * cos(x * kx) * cos(y * ky) * cos(t * w)

    #initial condition so we get result we want.
    I = lambda x, y: A * cos(x * kx) * cos(y * ky)

    #factor dt decreeses per step
    step = 0.5
    #number of steps I want to do
    val = 5
    #array to store errors
    E = zeros(val)

    for i in range(val):
        v = 'vector'
        #solve eqation
        u, x, y, t, e = solver(I,
                               None,
                               None,
                               q,
                               0,
                               Lx,
                               Ly,
                               dt * step**(i),
                               T,
                               C,
                               1,
                               mode=v,
                               ue=ue)

        E[i] = e

    #find convergence rate between diffrent dt values
    r = zeros(val - 1)
    r = log(E[1:] / E[:-1]) / log(step)

    print "Test convergence for undamped waves:"
    for i in range(val):
        if i == 0:
            print "dt: ", dt, " Error: ", E[i]
        else:
            print "dt: ", dt * step**(
                i), " Error: ", E[i], "rate of con.: ", r[i - 1]

    #requiere close to 2 in convergence rate for last r.
    assert abs(r[-1] - 2) < 0.01