Guest User

dbc

a guest
Feb 6th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1.    
  2.  
  3.     from dolfin import *
  4.      
  5.     parameters["linear_algebra_backend"] = "uBLAS"
  6.      
  7.     Lx = Ly = 1.0
  8.     R = Constant(2500)
  9.     P = Constant(2.026)
  10.     t = 0.0
  11.     T = 20.0
  12.     dt = 0.01
  13.      
  14.     mesh = UnitSquareMesh(64, 64)
  15.      
  16.      
  17.     class onBase(SubDomain):
  18.      
  19.         def inside(self, x, on_boundary):
  20.             return bool(x[1] < DOLFIN_EPS and on_boundary)
  21.      
  22.     class onLid(SubDomain):
  23.      
  24.         def inside(self, x, on_boundary):
  25.             return bool(x[1] > (Ly - DOLFIN_EPS) and on_boundary)
  26.      
  27.     class onSides(SubDomain):
  28.        
  29.         def inside(self, x, on_boundary):
  30.             return bool(not(near(x[1], 0) or near(x[1], Ly)) and on_boundary)
  31.            
  32.     class onFaces(SubDomain):
  33.        
  34.         def inside(self, x, on_boundary):
  35.             return bool(on_boundary)
  36.      
  37.     base = onBase()
  38.     lid = onLid()
  39.     sides = onSides()
  40.     faces = onFaces()
  41.      
  42.     # Create the function spaces for u, p, c
  43.     V = VectorFunctionSpace(mesh, "CG", 1, dim=2)
  44.     W = FunctionSpace(mesh, "CG", 1)
  45.     X = FunctionSpace(mesh, "CG", 1)
  46.     VWX = MixedFunctionSpace([V, W, X])  # mixed functions space
  47.      
  48.     # Define trail and test functions
  49.     du_ = TrialFunction(VWX)
  50.     v_u, v_p, v_c = TestFunctions(VWX)
  51.      
  52.     # define functions
  53.     u_ = Function(VWX)  # current solution
  54.     u0_ = Function(VWX)  # solution from previous step
  55.      
  56.     # Split mixed functions
  57.     du, dp, dc = split(du_)
  58.     u, p, c = split(u_)
  59.     u0, p0, c0 = split(u0_)
  60.     I = Identity(2)
  61.      
  62.     #define the boundary conditions
  63.     no_slip = [DirichletBC(VWX.sub(0), Constant((0.0, 0.0)), lid),
  64.                DirichletBC(VWX.sub(0), Constant((0.0, 0.0)), base),
  65.                DirichletBC(VWX.sub(0), Constant((0.0, 0.0)), sides)]
  66.      
  67.     temp = [DirichletBC(VWX.sub(2), Constant(0), lid),
  68.             DirichletBC(VWX.sub(2), Constant(0), base)]
  69.      
  70.     bcs = no_slip + temp
  71.      
  72.     # Class representing the initial conditions
  73.     class InitialConditions(Expression):
  74.         def __init__(self):
  75.             pass
  76.      
  77.         def eval(self, values, x):
  78.             values[0] = 0
  79.             values[1] = 0
  80.             values[2] = 0
  81.             values[3] = 0.01
  82.            
  83.         def value_shape(self):
  84.             return (4,)
  85.      
  86.     # Create initial conditions and interpolate
  87.     u_init_ = InitialConditions()
  88.     u_.interpolate(u_init_)
  89.     u0_.interpolate(u_init_)
  90.      
  91.     # Crank-Nicholson time stepping
  92.     U = 0.5*(u + u0)
  93.     C = 0.5*(c + c0)
  94.      
  95.     L = (1. /dt)*inner(u - u0, v_u)*dx - \
  96.                      inner(p, div(v_u))*dx - \
  97.                      R*P*inner(c*I[:, 1], v_u)*dx +  \
  98.                      P*inner(grad(U), grad(v_u))*dx + \
  99.                      inner(div(U), v_p)*dx + (1./dt)*inner(c - c0, v_c)*dx - \
  100.                      inner(dot(U,I[:, 1]), v_c)*dx + \
  101.                      inner(grad(C), grad(v_c))*dx + \
  102.                      inner(0.5*(dot(u0, grad(u0)) + dot(u, grad(u))), v_u)*dx + \
  103.                      inner(0.5*(dot(u0, grad(c0)) + dot(u, grad(c))), v_c)*dx
  104.      
  105.     J = derivative(L, u_, du_)
  106.     problem = NonlinearVariationalProblem(L, u_, bcs, J)
  107.     solver = NonlinearVariationalSolver(problem)
  108.        
  109.     while (t < T):
  110.         t += dt
  111.         u0_.vector()[:] = u_.vector()
  112.         solver.solve()
Advertisement
Add Comment
Please, Sign In to add comment