def test_pde_constrained(polynomial_order, in_expression): interpolate_expression = Expression(in_expression, degree=3) xmin, xmax = 0., 1. ymin, ymax = 0., 1. property_idx = 1 dt = 1. k = polynomial_order # Make mesh mesh = RectangleMesh(Point(xmin, ymin), Point(xmax, ymax), 40, 40) # Make function spaces and functions W_e = FiniteElement("DG", mesh.ufl_cell(), k) T_e = FiniteElement("DG", mesh.ufl_cell(), 0) Wbar_e = FiniteElement("DGT", mesh.ufl_cell(), k) W = FunctionSpace(mesh, W_e) T = FunctionSpace(mesh, T_e) Wbar = FunctionSpace(mesh, Wbar_e) psi_h, psi0_h = Function(W), Function(W) lambda_h = Function(T) psibar_h = Function(Wbar) uadvect = Constant((0, 0)) # Define particles x = RandomRectangle(Point(xmin, ymin), Point(xmax, ymax)).generate([500, 500]) s = assign_particle_values(x, interpolate_expression) psi0_h.assign(interpolate_expression) # Just make a complicated particle, possibly with scalars and vectors mixed p = particles(x, [s], mesh) p.interpolate(psi0_h, 1) # Initialize forms FuncSpace_adv = { 'FuncSpace_local': W, 'FuncSpace_lambda': T, 'FuncSpace_bar': Wbar } forms_pde = FormsPDEMap(mesh, FuncSpace_adv).forms_theta_linear( psi0_h, uadvect, dt, Constant(1.0)) pde_projection = PDEStaticCondensation(mesh, p, forms_pde['N_a'], forms_pde['G_a'], forms_pde['L_a'], forms_pde['H_a'], forms_pde['B_a'], forms_pde['Q_a'], forms_pde['R_a'], forms_pde['S_a'], [], property_idx) # Assemble and solve pde_projection.assemble(True, True) pde_projection.solve_problem(psibar_h, psi_h, lambda_h, 'none', 'default') error_psih = abs(assemble((psi_h - psi0_h) * (psi_h - psi0_h) * dx)) error_lamb = abs(assemble(lambda_h * lambda_h * dx)) assert error_psih < 1e-15 assert error_lamb < 1e-15
# Initialize forms FuncSpace_adv = { "FuncSpace_local": W, "FuncSpace_lambda": T, "FuncSpace_bar": Wbar } forms_pde = FormsPDEMap(mesh, FuncSpace_adv).forms_theta_linear( psi0_h, uh, dt, Constant(1.0)) pde_projection = PDEStaticCondensation( mesh, p, forms_pde["N_a"], forms_pde["G_a"], forms_pde["L_a"], forms_pde["H_a"], forms_pde["B_a"], forms_pde["Q_a"], forms_pde["R_a"], forms_pde["S_a"], [bc], property_idx, ) # Initialize the l2 projection lstsq_psi = l2projection(p, W, property_idx) # Set initial condition at mesh and particles psi0_h.interpolate(psi0_expression) p.interpolate(psi0_h.cpp_object(), property_idx) # Initialize add/delete particle
uadvect = uh - umesh # Now throw in the particles x = RandomRectangle(Point(xmin, ymin), Point(xmax, ymax)).generate([pres, pres]) s = assign_particle_values(x, CosineHill(radius=0.25, center=[0.25, 0.5], amplitude=1.0, degree=1)) p = particles(x, [s], mesh) # Define projections problem FuncSpace_adv = {'FuncSpace_local': Q_Rho, 'FuncSpace_lambda': T_1, 'FuncSpace_bar': Qbar} FormsPDE = FormsPDEMap(mesh, FuncSpace_adv, beta_map=Constant(1e-8)) forms_pde = FormsPDE.forms_theta_linear(phih0, uadvect, dt, Constant(1.0), zeta=Constant(0.), h=Constant(0.)) pde_projection = PDEStaticCondensation(mesh, p, forms_pde['N_a'], forms_pde['G_a'], forms_pde['L_a'], forms_pde['H_a'], forms_pde['B_a'], forms_pde['Q_a'], forms_pde['R_a'], forms_pde['S_a'], [], 1) ap = advect_rk3(p, V, uh, 'open') # Initialize the initial condition at mesh by an l2 projection lstsq_rho = l2projection(p, Q_Rho, 1) lstsq_rho.project(phih0) outfile << phih0 for step in range(num_steps): u_expr.t = step * float(dt) u_expre_neg.t = step * float(dt) uh.assign(u_expr)
# Set-up density projection funcspaces_rho = { 'FuncSpace_local': Q_Rho, 'FuncSpace_lambda': T_1, 'FuncSpace_bar': Qbar } forms_rho = FormsPDEMap(mesh, funcspaces_rho).forms_theta_linear( rho0, ubar0_a, dt, theta_map=Constant(1.), theta_L=Constant(0.), zeta=Constant(20.)) pde_rho = PDEStaticCondensation(mesh, p, forms_rho['N_a'], forms_rho['G_a'], forms_rho['L_a'], forms_rho['H_a'], forms_rho['B_a'], forms_rho['Q_a'], forms_rho['R_a'], forms_rho['S_a'], 1) # Set-up momentum projection FuncSpace_u = { 'FuncSpace_local': W_2, 'FuncSpace_lambda': T_2, 'FuncSpace_bar': Wbar_2 } forms_u = FormsPDEMap(mesh, FuncSpace_u).forms_theta_nlinear_multiphase( rho, rho0, rho00, rhobar, u0, ubar0_a,
# Initialize forms FuncSpace_adv = { "FuncSpace_local": W, "FuncSpace_lambda": T, "FuncSpace_bar": Wbar } forms_pde = FormsPDEMap(mesh, FuncSpace_adv).forms_theta_linear( psi0_h, uh, dt, Constant(1.0)) pde_projection = PDEStaticCondensation( mesh, p, forms_pde["N_a"], forms_pde["G_a"], forms_pde["L_a"], forms_pde["H_a"], forms_pde["B_a"], forms_pde["Q_a"], forms_pde["R_a"], forms_pde["S_a"], [], property_idx, ) # Set initial condition at mesh and particles psi0_h.interpolate(psi0_expression) p.interpolate(psi0_h.cpp_object(), property_idx) # Add/Delete particles AD = AddDelete(p, 10, 20, [psi0_h]) step = 0
# Simulation time and time step Constants t = Constant(0.0) dt = Constant(1e-2) # Initialise advection forms FuncSpace_adv = {'FuncSpace_local': Wh, 'FuncSpace_lambda': Th, 'FuncSpace_bar': Wbarh} forms_pde = FormsPDEMap(mesh, FuncSpace_adv).forms_theta_linear(gamma0, u_vec, dt, Constant(1.0), theta_L=Constant(1.0), zeta=Constant(25.0)) phi_bcs = [DirichletBC(Wbarh, Constant(0.0), "near(x[1], 0.0)", "geometric"), DirichletBC(Wbarh, Constant(1.0), "near(x[1], 1.0)", "geometric")] pde_projection = PDEStaticCondensation(mesh, ptcls, forms_pde['N_a'], forms_pde['G_a'], forms_pde['L_a'], forms_pde['H_a'], forms_pde['B_a'], forms_pde['Q_a'], forms_pde['R_a'], forms_pde['S_a'], phi_bcs, property_idx) # Function spaces for Stokes mixedL = FunctionSpace(mesh, MixedElement([W_e_2, Q_E])) mixedG = FunctionSpace(mesh, MixedElement([Wbar_e_2_H12, Qbar_E])) U0, Uh = Function(mixedL), Function(mixedL) Uhbar = Function(mixedG) # BCs bcs = [DirichletBC(mixedG.sub(0), Constant((0, 0, 0.0)), "near(x[1], 0.0) or near(x[1], 1.0)"), DirichletBC(mixedG.sub(0).sub(0), Constant(0), CompiledSubDomain("near(x[0], 0.0) or near(x[0], lmbda)", lmbda=lmbdax)),
u_vec, dt, Constant(1.0), theta_L=Constant(1.0), zeta=Constant(25.0)) phi_bcs = [ DirichletBC(Wbarh, Constant(0.0), "near(x[1], 0.0)", "geometric"), DirichletBC(Wbarh, Constant(1.0), "near(x[1], 1.0)", "geometric"), ] pde_projection = PDEStaticCondensation( mesh, ptcls, forms_pde["N_a"], forms_pde["G_a"], forms_pde["L_a"], forms_pde["H_a"], forms_pde["B_a"], forms_pde["Q_a"], forms_pde["R_a"], forms_pde["S_a"], phi_bcs, property_idx, ) # Function spaces for Stokes mixedL = FunctionSpace(mesh, MixedElement([W_e_2, Q_E])) mixedG = FunctionSpace(mesh, MixedElement([Wbar_e_2_H12, Qbar_E])) U0, Uh = Function(mixedL), Function(mixedL) Uhbar = Function(mixedG) # BCs
"FuncSpace_bar": Qbar } forms_rho = FormsPDEMap(mesh, funcspaces_rho).forms_theta_linear( rho0, ubar0_a, dt, theta_map=Constant(1.0), theta_L=Constant(0.0), zeta=Constant(20.0)) pde_rho = PDEStaticCondensation( mesh, p, forms_rho["N_a"], forms_rho["G_a"], forms_rho["L_a"], forms_rho["H_a"], forms_rho["B_a"], forms_rho["Q_a"], forms_rho["R_a"], forms_rho["S_a"], 1, ) # Set-up momentum projection FuncSpace_u = { "FuncSpace_local": W_2, "FuncSpace_lambda": T_2, "FuncSpace_bar": Wbar_2 } forms_u = FormsPDEMap(mesh, FuncSpace_u).forms_theta_nlinear_multiphase( rho,
lambda_h = Function(T) psibar_h = Function(Wbar) # Boundary conditions bc = DirichletBC(Wbar, Constant(0.), "on_boundary") # Initialize forms FuncSpace_adv = { 'FuncSpace_local': W, 'FuncSpace_lambda': T, 'FuncSpace_bar': Wbar } forms_pde = FormsPDEMap(mesh, FuncSpace_adv).forms_theta_linear( psi0_h, uh, dt, Constant(1.0)) pde_projection = PDEStaticCondensation( mesh, p, forms_pde['N_a'], forms_pde['G_a'], forms_pde['L_a'], forms_pde['H_a'], forms_pde['B_a'], forms_pde['Q_a'], forms_pde['R_a'], forms_pde['S_a'], [bc], property_idx) # Set initial condition at mesh and particles psi0_h.interpolate(psi0_expression) p.interpolate(psi0_h, property_idx) # Initialize add/delete for safety AD = AddDelete(p, 15, 25, [psi0_h]) step = 0 t = 0. area_0 = assemble(psi0_h * dx) timer = Timer() timer.start()
funcspace_dict = { 'FuncSpace_local': W_2, 'FuncSpace_lambda': T_2, 'FuncSpace_bar': Wbar_2 } forms_adv = FormsPDEMap(mesh, funcspace_dict).forms_theta_nlinear( u0_a, ubar0_a, dt, theta_map=Constant(1.0), theta_L=theta_L, duh0=duh0, duh00=duh00) pde_projection = PDEStaticCondensation(mesh, p, forms_adv['N_a'], forms_adv['G_a'], forms_adv['L_a'], forms_adv['H_a'], forms_adv['B_a'], forms_adv['Q_a'], forms_adv['R_a'], forms_adv['S_a'], property_idx) # Forms Stokes # Set pressure in corner to zero bc1 = DirichletBC(mixedG.sub(1), Constant(0), Corner(geometry), "pointwise") bcs = [bc1] forms_stokes = FormsStokes(mesh, mixedL, mixedG, alpha).forms_unsteady(ustar, dt, nu, f) ssc = StokesStaticCondensation(mesh, forms_stokes['A_S'], forms_stokes['G_S'], forms_stokes['B_S'], forms_stokes['Q_S'], forms_stokes['S_S'])
} forms_adv = FormsPDEMap(mesh, funcspace_dict).forms_theta_nlinear( u0_a, ubar0_a, dt, theta_map=Constant(1.0), theta_L=theta_L, duh0=duh0, duh00=duh00) pde_projection = PDEStaticCondensation( mesh, p, forms_adv["N_a"], forms_adv["G_a"], forms_adv["L_a"], forms_adv["H_a"], forms_adv["B_a"], forms_adv["Q_a"], forms_adv["R_a"], forms_adv["S_a"], property_idx, ) # Forms Stokes # Set pressure in corner to zero bc1 = DirichletBC(mixedG.sub(1), Constant(0), Corner(geometry), "pointwise") bcs = [bc1] forms_stokes = FormsStokes(mesh, mixedL, mixedG, alpha).forms_unsteady(ustar, dt, nu, f)
constrained_domain=PeriodicBoundary(lim_dict)) psi_h, psi0_h = Function(W), Function(W) lambda_h = Function(T) psibar_h = Function(Wbar) # Initialize forms FuncSpace_adv = { 'FuncSpace_local': W, 'FuncSpace_lambda': T, 'FuncSpace_bar': Wbar } forms_pde = FormsPDEMap(mesh, FuncSpace_adv).forms_theta_linear( psi0_h, uh, dt, Constant(1.0)) pde_projection = PDEStaticCondensation( mesh, p, forms_pde['N_a'], forms_pde['G_a'], forms_pde['L_a'], forms_pde['H_a'], forms_pde['B_a'], forms_pde['Q_a'], forms_pde['R_a'], forms_pde['S_a'], [], property_idx) # Set initial condition at mesh and particles psi0_h.interpolate(psi0_expression) p.interpolate(psi0_h.cpp_object(), property_idx) # Add/Delete particles AD = AddDelete(p, 10, 20, [psi0_h]) step = 0 area_0 = assemble(psi0_h * dx) timer = Timer() timer.start() while step < num_steps:
def test_moving_mesh(): t = 0. dt = 0.025 num_steps = 20 xmin, ymin = 0., 0. xmax, ymax = 2., 2. xc, yc = 1., 1. nx, ny = 20, 20 pres = 150 k = 1 mesh = RectangleMesh(Point(xmin, ymin), Point(xmax, ymax), nx, ny) n = FacetNormal(mesh) # Class for mesh motion dU = PeriodicVelocity(xmin, xmax, dt, t, degree=1) Qcg = VectorFunctionSpace(mesh, 'CG', 1) boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1) boundaries.set_all(0) leftbound = Left(xmin) leftbound.mark(boundaries, 99) ds = Measure('ds', domain=mesh, subdomain_data=boundaries) # Create function spaces Q_E_Rho = FiniteElement("DG", mesh.ufl_cell(), k) T_1 = FunctionSpace(mesh, 'DG', 0) Qbar_E = FiniteElement("DGT", mesh.ufl_cell(), k) Q_Rho = FunctionSpace(mesh, Q_E_Rho) Qbar = FunctionSpace(mesh, Qbar_E) phih, phih0 = Function(Q_Rho), Function(Q_Rho) phibar = Function(Qbar) # Advective velocity uh = Function(Qcg) uh.assign(Constant((0., 0.))) # Mesh velocity umesh = Function(Qcg) # Total velocity uadvect = uh-umesh # Now throw in the particles x = RandomRectangle(Point(xmin, ymin), Point(xmax, ymax)).generate([pres, pres]) s = assign_particle_values(x, GaussianPulse(center=(xc, yc), sigma=float(0.25), U=[0, 0], time=0., height=1., degree=3)) x = comm.bcast(x, root=0) s = comm.bcast(s, root=0) p = particles(x, [s], mesh) # Define projections problem FuncSpace_adv = {'FuncSpace_local': Q_Rho, 'FuncSpace_lambda': T_1, 'FuncSpace_bar': Qbar} FormsPDE = FormsPDEMap(mesh, FuncSpace_adv, ds=ds) forms_pde = FormsPDE.forms_theta_linear(phih0, uadvect, dt, Constant(1.0), zeta=Constant(0.)) pde_projection = PDEStaticCondensation(mesh, p, forms_pde['N_a'], forms_pde['G_a'], forms_pde['L_a'], forms_pde['H_a'], forms_pde['B_a'], forms_pde['Q_a'], forms_pde['R_a'], forms_pde['S_a'], [], 1) # Initialize the initial condition at mesh by an l2 projection lstsq_rho = l2projection(p, Q_Rho, 1) lstsq_rho.project(phih0.cpp_object()) for step in range(num_steps): # Compute old area at old configuration old_area = assemble(phih0*dx) # Pre-assemble rhs pde_projection.assemble_state_rhs() # Move mesh dU.compute_ubc() umesh.assign(project(dU, Qcg)) ALE.move(mesh, project(dU * dt, Qcg)) dU.update() # Relocate particles as a result of mesh motion # NOTE: if particles were advected themselve, # we had to run update_facets_info() here as well p.relocate() # Assemble left-hand side on new config, but not the right-hand side pde_projection.assemble(True, False) pde_projection.solve_problem(phibar.cpp_object(), phih.cpp_object(), 'mumps', 'none') # Needed to compute conservation, note that there # is an outgoing flux at left boundary new_area = assemble(phih*dx) gamma = conditional(ge(dot(uadvect, n), 0), 0, 1) bflux = assemble((1-gamma) * dot(uadvect, n) * phih * ds) # Update solution assign(phih0, phih) # Put assertion on (global) mass balance, local mass balance is # too time consuming but should pass also assert new_area - old_area + bflux * dt < 1e-12 # Assert that max value of phih stays close to 2 and # min value close to 0. This typically will fail if # we do not do a correct relocate of particles assert np.amin(phih.vector().get_local()) > -0.015 assert np.amax(phih.vector().get_local()) < 1.04
property_idx = 1 p = particles(x, [s, s], mesh) ap = advect_rk3(p, W_2, Udiv, 'periodic', lims.flatten()) # Particle management AD = AddDelete(p, 15, 25, [Udiv, duh0]) # Forms PDE map funcspace_dict = {'FuncSpace_local': W_2, 'FuncSpace_lambda': T_2, 'FuncSpace_bar': Wbar_2} forms_adv = FormsPDEMap(mesh, funcspace_dict).forms_theta_nlinear(u0_a, ubar0_a, dt, theta_map=Constant(1.0), theta_L=theta_L, duh0=duh0, duh00=duh00) pde_projection = PDEStaticCondensation(mesh, p, forms_adv['N_a'], forms_adv['G_a'], forms_adv['L_a'], forms_adv['H_a'], forms_adv['B_a'], forms_adv['Q_a'], forms_adv['R_a'], forms_adv['S_a'], property_idx) # Forms Stokes # Set pressure in corner to zero bc1 = DirichletBC(mixedG.sub(1), Constant(0), Corner(geometry), "pointwise") bcs = [bc1] forms_stokes = FormsStokes(mesh, mixedL, mixedG, alpha).forms_unsteady(ustar, dt, nu, f) ssc = StokesStaticCondensation(mesh, forms_stokes['A_S'], forms_stokes['G_S'], forms_stokes['B_S'], forms_stokes['Q_S'], forms_stokes['S_S'])
"FuncSpace_bar": Qbar } FormsPDE = FormsPDEMap(mesh, FuncSpace_adv, beta_map=Constant(1e-8)) forms_pde = FormsPDE.forms_theta_linear(phih0, uadvect, dt, Constant(1.0), zeta=Constant(0.0), h=Constant(0.0)) pde_projection = PDEStaticCondensation( mesh, p, forms_pde["N_a"], forms_pde["G_a"], forms_pde["L_a"], forms_pde["H_a"], forms_pde["B_a"], forms_pde["Q_a"], forms_pde["R_a"], forms_pde["S_a"], [], 1, ) ap = advect_rk3(p, V, uh, "open") # Initialize the initial condition at mesh by an l2 projection lstsq_rho = l2projection(p, Q_Rho, 1) lstsq_rho.project(phih0) outfile << phih0 for step in range(num_steps):