Example #1
0
def XXX(dim, tend, dt, s, h, b, c, d, c_dir="x", d_dir="x", a=1., CN=True):
    """
     dim - sparial dimension 
     s - width of initial profile
     h - mesh size
    """
    v_c = c / a * getDirection(dim, c_dir)
    v_d = d / a * getDirection(dim, d_dir)
    v = (v_c + v_d)
    E = b / a
    if VERBOSITY:
        print("=" * 100)
        print(
            "XX Start test dim  = %d , h=%e, b=%e, c=%e, d=%e, c_dir=%s, d_dir=%s, a=%e, s=%e"
            % (dim, h, b, c, d, c_dir, d_dir, a, s))
        print("=" * 100)
        print("initial width s = ", s)
        print("diffusion = ", E)
        print("total velocity = ", v)
        print("tend = ", tend)
        print("tolerance = ", TOL)
        print("number of elements over s =", s / h)
    b0 = sqrt(-log(TAU) * 4 * (s**2 + E * tend))
    b1 = sqrt(-log(TAU)) * 2 * s
    X0_0 = max(b1, -v[0] * tend + b0)
    X0_1 = max(b1, -v[1] * tend + b0)
    l_0 = X0_0 + max(v[0] * tend + b0, b1)
    l_1 = X0_1 + max(v[1] * tend + b0, b1)
    NE_0 = max(int(l_0 / h + 0.5), 1)
    NE_1 = max(int(l_1 / h + 0.5), 1)
    if dim == 2:
        if VERBOSITY:
            print("%d x %d grid over %e  x %e with element size %e." %
                  (NE_0, NE_1, l_0, l_1, h))
        if NE_0 * NE_1 > NE_MAX:
            raise ValueError("too many elements %s." % (NE_0 * NE_1, ))
        dom = Rectangle(n0=NE_0, n1=NE_1, l0=l_0, l1=l_1)
        x0 = [X0_0, X0_1]
    else:
        X0_2 = max(b1, -v[2] * tend + b0)
        l_2 = X0_2 + max(v[2] * tend + b0, b1)
        NE_2 = max(int(l_2 / h + 0.5), 1)
        if VERBOSITY:
            print(
                "%d x %d x %d grid over %e  x %e x %e with element size %e." %
                (NE_0, NE_1, NE_2, l_0, l_1, l_2, h))
        if NE_0 * NE_1 * NE_2 > NE_MAX:
            raise ValueError("too many elements %s." % (NE_0 * NE_1 * NE_2, ))
        dom = Brick(n0=NE_0, n1=NE_1, ne2=NE_2, l0=l_0, l1=l_1, l2=l_2)
        x0 = [X0_0, X0_1, X0_2]
    if VERBOSITY:
        print("initial location = ", x0)
    print("XX", interpolate(uRef(dom, 0., E, s, v, x0),
                            FunctionOnBoundary(dom)))

    fc_BE = TransportPDE(dom, numEquations=1, useBackwardEuler=True)
    fc_BE.setValue(M=a, A=-b * kronecker(dom), B=-v_d * a, C=-v_c * a)
    fc_BE.getSolverOptions().setVerbosity(VERBOSITY)
    fc_BE.getSolverOptions().setTolerance(TOL)
    #
    fc_BE.getSolverOptions().setPreconditioner(
        fc_BE.getSolverOptions().GAUSS_SEIDEL)
    fc_BE.getSolverOptions().setNumSweeps(5)
    if VERBOSITY: print("Backward Euler Transport created")

    fc_CN = TransportPDE(dom, numEquations=1, useBackwardEuler=False)
    fc_CN.setValue(M=a, A=-b * kronecker(dom), B=-v_d * a, C=-v_c * a)
    fc_CN.getSolverOptions().setVerbosity(VERBOSITY)
    fc_CN.getSolverOptions().setTolerance(TOL)

    #fc_CN.getSolverOptions().setPreconditioner(fc_CN.getSolverOptions().GAUSS_SEIDEL)
    fc_CN.getSolverOptions().setNumSweeps(2)
    if VERBOSITY: print("Crank Nicolson Transport created")
    dt_CN = fc_CN.getSafeTimeStepSize()
    if VERBOSITY: print("time step size by Crank Nicolson=", dt_CN)

    U0 = uRef(dom, 0, E, s, v, x0)
    U0_e = uRef(dom, 0, E, s, v, x0, True)
    fc_CN.setInitialSolution(U0)
    fc_BE.setInitialSolution(U0)
    initial_error_L2 = sqrt(integrate((U0 - U0_e)**2))
    if VERBOSITY:
        print("initial Lsup = ", Lsup(U0), Lsup(U0_e))
        print("initial integral = ", integrate(U0_e))
        print("initial error = ", initial_error_L2)
        print("used time step size =", dt)

    if not CN:
        n = int(ceil(tend / dt))
        if VERBOSITY:
            print("Solve Backward Euler:")
            print("substeps : ", n)
        t0 = clock()
        for i in range(n):
            u = fc_BE.getSolution(dt)
        t0 = clock() - t0
    else:
        if VERBOSITY: print("Solve Crank Nicolson:")
        dt = dt_CN
        t0 = clock()
        u = fc_CN.getSolution(tend)
        t0 = clock() - t0
    out = QUALITY(u, uRef(dom, tend, E, s, v, x0, True))
    print("XX",
          interpolate(uRef(dom, tend, E, s, v, x0), FunctionOnBoundary(dom)))
    out['time'] = t0
    out['tend'] = tend
    out['dt'] = dt
    out['dx'] = h
    if abs(b) > 0:
        out["peclet"] = length(v) * s / b
    else:
        out["peclet"] = 9999999.
    # saveVTK("bb.vtu",u0=U0,u_CN=u_CN, uRef=uRef(dom,dt2,E,s,v,X0) )
    return out
NE = 50
dom = Rectangle(NE, 1, l1=1. / NE)
dom = Rectangle(NE, NE)
fc = TransportPDE(dom, numEquations=1)
fc.getSolverOptions().setVerbosityOn()
fc.getSolverOptions().setODESolver(SolverOptions.LINEAR_CRANK_NICOLSON)
fc.getSolverOptions().setODESolver(SolverOptions.BACKWARD_EULER)
fc.getSolverOptions().setODESolver(SolverOptions.CRANK_NICOLSON)
fc.setValue(M=1, C=[-1, 0])
x = dom.getX()
u0 = whereNegative(x[0] - 1. / NE)

c = 0
t = 0

saveVTK("u.%s.vtu" % c, u=u0)
fc.setInitialSolution(u0)
dt = fc.getSafeTimeStepSize()

print("u0 =", u0)
T_END = dt
print("dt = ", dt)
while t < T_END:
    print("time step t=", t + dt)
    u = fc.getSolution(dt)
    saveVTK("u.%s.vtu" % (c + 1, ), u=u)
    print("u =", u)
    c += 1
    t += dt
Example #3
0
NE=50
dom=Rectangle(NE,1,l1=1./NE)
dom=Rectangle(NE,NE)
fc=TransportPDE(dom,numEquations=1)
fc.getSolverOptions().setVerbosityOn()
fc.getSolverOptions().setODESolver(SolverOptions.LINEAR_CRANK_NICOLSON)
fc.getSolverOptions().setODESolver(SolverOptions.BACKWARD_EULER)
fc.getSolverOptions().setODESolver(SolverOptions.CRANK_NICOLSON)
fc.setValue(M=1,C=[-1,0])
x=dom.getX()
u0=whereNegative(x[0]-1./NE)

c=0
t=0

saveVTK("u.%s.vtu"%c,u=u0)
fc.setInitialSolution(u0)
dt=fc.getSafeTimeStepSize() 

print("u0 =",u0)
T_END=dt
print("dt = ",dt)
while t<T_END:
    print("time step t=",t+dt)
    u=fc.getSolution(dt)
    saveVTK("u.%s.vtu"%(c+1,),u=u)
    print("u =",u)
    c+=1
    t+=dt
Example #4
0
def XXX(dim,tend,dt, s, h,b,c,d,c_dir="x", d_dir="x", a=1., CN=True):
    """
     dim - sparial dimension 
     s - width of initial profile
     h - mesh size
    """
    v_c=c/a*getDirection(dim,c_dir)
    v_d=d/a*getDirection(dim,d_dir)
    v = (v_c+v_d)
    E=b/a 
    if VERBOSITY: 
           print("="*100)
           print("XX Start test dim  = %d , h=%e, b=%e, c=%e, d=%e, c_dir=%s, d_dir=%s, a=%e, s=%e"%(dim, h,b,c,d,c_dir, d_dir, a, s))
           print("="*100)
           print("initial width s = ",s)
           print("diffusion = ",E)
           print("total velocity = ",v)
           print("tend = ", tend)
           print("tolerance = ",TOL)
           print("number of elements over s =",s/h)
    b0=sqrt(- log(TAU) * 4*(s**2+E*tend))
    b1=sqrt(- log(TAU)) * 2*s 
    X0_0=max(b1,-v[0]*tend + b0)
    X0_1=max(b1,-v[1]*tend + b0)
    l_0=X0_0+max(v[0]*tend + b0 , b1)
    l_1=X0_1+max(v[1]*tend + b0 , b1)
    NE_0=max(int(l_0/h+0.5),1)
    NE_1=max(int(l_1/h+0.5),1)
    if dim == 2:
        if VERBOSITY: print("%d x %d grid over %e  x %e with element size %e."%(NE_0,NE_1,l_0,l_1,h))
        if NE_0*NE_1 > NE_MAX:
           raise ValueError("too many elements %s."%(NE_0*NE_1,))
        dom=Rectangle(n0=NE_0,n1=NE_1,l0=l_0,l1=l_1)
        x0=[X0_0, X0_1]
    else:
       X0_2=max(b1,-v[2]*tend + b0)
       l_2=X0_2+max(v[2]*tend + b0 , b1)
       NE_2=max(int(l_2/h+0.5),1)
       if VERBOSITY: print("%d x %d x %d grid over %e  x %e x %e with element size %e."%(NE_0,NE_1,NE_2,l_0,l_1,l_2,h))
       if NE_0*NE_1*NE_2 > NE_MAX:
          raise ValueError("too many elements %s."%(NE_0*NE_1*NE_2,))
       dom=Brick(n0=NE_0,n1=NE_1, ne2=NE_2, l0=l_0,l1=l_1, l2=l_2)
       x0=[X0_0, X0_1, X0_2]
    if VERBOSITY: 
        print("initial location = ",x0)
    print("XX", interpolate(uRef(dom,0.,E,s,v,x0), FunctionOnBoundary(dom)))
     
    fc_BE=TransportPDE(dom,numEquations=1)
    fc_BE.setValue(M=a, A=-b*kronecker(dom), B=-v_d*a, C=-v_c*a)
    fc_BE.getSolverOptions().setVerbosity(VERBOSITY)
    fc_BE.getSolverOptions().setTolerance(TOL)
    #
    fc_BE.getSolverOptions().setPreconditioner(fc_BE.getSolverOptions().GAUSS_SEIDEL)
    fc_BE.getSolverOptions().setNumSweeps(5)  
    if VERBOSITY: print("Backward Euler Transport created")

    fc_CN=TransportPDE(dom,numEquations=1)
    fc_CN.setValue(M=a, A=-b*kronecker(dom), B=-v_d*a, C=-v_c*a)
    fc_CN.getSolverOptions().setVerbosity(VERBOSITY)
    fc_CN.getSolverOptions().setTolerance(TOL)
   
    #fc_CN.getSolverOptions().setPreconditioner(fc_CN.getSolverOptions().GAUSS_SEIDEL) 
    fc_CN.getSolverOptions().setNumSweeps(2)  
    if VERBOSITY: print("Crank Nicolson Transport created")
    dt_CN=fc_CN.getSafeTimeStepSize()
    if VERBOSITY: print("time step size by Crank Nicolson=",dt_CN)

    U0=uRef(dom,0,E,s,v,x0)
    U0_e=uRef(dom,0,E,s,v,x0,True)
    fc_CN.setInitialSolution(U0)
    fc_BE.setInitialSolution(U0)
    initial_error_L2=sqrt(integrate((U0-U0_e)**2))
    if VERBOSITY:
      print("initial Lsup = ",Lsup(U0), Lsup(U0_e))
      print("initial integral = ",integrate(U0_e))
      print("initial error = ",initial_error_L2)
      print("used time step size =",dt) 
      
    if not CN:
       n=int(ceil(tend/dt))
       if VERBOSITY: 
          print("Solve Backward Euler:")
          print("substeps : ",n)
       t0=clock()
       for i in range(n): u=fc_BE.getSolution(dt)
       t0=clock()-t0
    else:
       if VERBOSITY: print("Solve Crank Nicolson:")
       dt=dt_CN
       t0=clock()
       u=fc_CN.getSolution(tend)
       t0=clock()-t0
    out=QUALITY(u,uRef(dom,tend,E,s,v,x0,True))
    print("XX", interpolate(uRef(dom,tend,E,s,v,x0), FunctionOnBoundary(dom)))
    out['time']=t0
    out['tend']=tend
    out['dt']=dt
    out['dx']=h
    if abs(b)>0:
       out["peclet"]=length(v)*s/b
    else:
        out["peclet"]=9999999.
    # saveVTK("bb.vtu",u0=U0,u_CN=u_CN, uRef=uRef(dom,dt2,E,s,v,X0) )
    return out