def test_failsafe_sweep(): interpolate_expression = Expression('x[0]', degree=1) mesh = UnitSquareMesh(5, 5) V = FunctionSpace(mesh, "DG", 1) v = Function(V) v.assign(interpolate_expression) np_min, np_max = 1, 2 np_failsafe = 4 # Initialize particles x = RandomRectangle(Point(0.0, 0.0), Point(1., 1.)).generate([100, 100]) s = assign_particle_values(x, interpolate_expression) # Broadcast to other procs x = comm.bcast(x, root=0) s = comm.bcast(s, root=0) property_idx = 1 p = particles(x, [s], mesh) AD = AddDelete(p, np_min, np_max, [v]) AD.do_sweep_failsafe(np_failsafe) # Must recover linear lstsq_rho = l2projection(p, V, property_idx) lstsq_rho.project(v.cpp_object()) error = sqrt( assemble( (v - interpolate_expression) * (v - interpolate_expression) * dx)) assert len(p.positions() == mesh.num_cells() * np_failsafe) assert error < 1e-12
def test_l2_projection_3D(polynomial_order, in_expression): xmin, ymin, zmin = 0.0, 0.0, 0.0 xmax, ymax, zmax = 1.0, 1.0, 1.0 nx = 25 property_idx = 1 mesh = BoxMesh(Point(xmin, ymin, zmin), Point(xmax, ymax, zmax), nx, nx, nx) interpolate_expression = Expression(in_expression, degree=3) if len(interpolate_expression.ufl_shape) == 0: V = FunctionSpace(mesh, "DG", polynomial_order) elif len(interpolate_expression.ufl_shape) == 1: V = VectorFunctionSpace(mesh, "DG", polynomial_order) v_exact = Function(V) v_exact.assign(interpolate_expression) x = RandomBox(Point(0.0, 0.0, 0.0), Point(1.0, 1.0, 1.0)).generate([4, 4, 4]) s = assign_particle_values(x, interpolate_expression) # Just make a complicated particle, possibly with scalars and vectors mixed p = particles(x, [s], mesh) # Do AddDelete sweep AD = AddDelete(p, 13, 15, [v_exact]) AD.do_sweep() vh = Function(V) lstsq_vh = l2projection(p, V, property_idx) lstsq_vh.project(vh.cpp_object()) error_sq = abs(assemble(dot(v_exact - vh, v_exact - vh) * dx)) if comm.Get_rank() == 0: assert error_sq < 1e-13
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 AD = AddDelete(p, 15, 25, [psi0_h]) step = 0 t = 0.0 area_0 = assemble(psi0_h * dx) timer = Timer() timer.start() while step < num_steps: step += 1 t += float(dt) if comm.rank == 0: print("Step " + str(step)) # Advect particle, assemble and solve pde projection
Vcg = FunctionSpace(model.mesh.mesh, 'DG', 1) (xp, pstrain, ptemp, pepsII) = (p.return_property(mesh, 0), p.return_property(mesh, 1), p.return_property(mesh, 2), p.return_property(mesh, 3)) del p p = particles(xp, [pstrain, ptemp, pepsII], model.mesh.mesh) else: p.relocate() # Advect particles -- Turn this on to advect particles now that it is removed from Stokes script Vdg = FunctionSpace(model.mesh.mesh, 'DG', 1) ap = advect_rk3(p, model.vector2, model.u_k, "open") ap.do_step(time_step) AD = AddDelete(p, p_min, p_max, [ interpolate(model.strain, Vdg), interpolate(model.temp, Vdg), interpolate(model.epsII, Vdg) ]) # Sweep over mesh to delete/insert particles AD.do_sweep() # Plotting (xp, pstrain, ptemp, pepsII) = (p.return_property(mesh, 0), p.return_property(mesh, 1), p.return_property(mesh, 2), p.return_property(mesh, 3)) pstrain[xp[:, 0] < xyield_min] = 0.0 p.change_property(pstrain, 1) xx = np.linspace(0, length * 1.5, 101) xs = np.linspace(0, length, 101) plt.figure(1)
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: step += 1 # Add/delete particles, must be done before advection! AD.do_sweep() # Advect particle, assemble and solve pde projection ap.do_step(float(dt)) pde_projection.assemble(True, True)
uh = Function(V) uh.assign(Expression(('-Uh*x[1]', 'Uh*x[0]'), Uh=Uh, degree=3)) # Generate particles x = RandomCircle(Point(x0, y0), r).generate([pres, pres]) s = assign_particle_values(x, psi0_expr) p = particles(x, [s], mesh) # Initialize advection class, use RK3 scheme ap = advect_rk3(p, V, uh, 'closed') # Init projection lstsq_psi = l2projection(p, W, 1) # Do projection to get initial field lstsq_psi.project(psi_h.cpp_object(), lb, ub) AD = AddDelete(p, 10, 20, [psi_h], [1], [lb, ub]) step = 0 t = 0. area_0 = assemble(psi_h * dx) timer = Timer() timer.start() outfile.write(psi_h, t) while step < num_steps: step += 1 t += float(dt) if comm.Get_rank() == 0: print("Step " + str(step))
# Define the variational (projection problem) k = 1 W_e = FiniteElement("DG", mesh.ufl_cell(), k) T_e = FiniteElement("DG", mesh.ufl_cell(), 0) Wbar_e = FiniteElement("DGT", mesh.ufl_cell(), k) # Composition field space Wh = FunctionSpace(mesh, W_e) Th = FunctionSpace(mesh, T_e) Wbarh = FunctionSpace(mesh, Wbar_e) phi = interpolate(StepFunction(), Wh) gamma0 = interpolate(StepFunction(), Wh) ad = AddDelete(ptcls, 50, 55, [phi], [1], [0.0, 1.0]) ptcls.interpolate(phi, property_idx) ad.do_sweep() lambda_h = Function(Th) psibar_h = Function(Wbarh) # Elements for Stokes W_e_2 = VectorElement("DG", mesh.ufl_cell(), k) T_e_2 = VectorElement("DG", mesh.ufl_cell(), 0) Wbar_e_2 = VectorElement("DGT", mesh.ufl_cell(), k) Wbar_e_2_H12 = VectorElement("CG", mesh.ufl_cell(), k)["facet"] Q_E = FiniteElement("DG", mesh.ufl_cell(), k-1) Qbar_E = FiniteElement("DGT", mesh.ufl_cell(), k)
# Define the variational (projection problem) k = 1 W_e = FiniteElement("DG", mesh.ufl_cell(), k) T_e = FiniteElement("DG", mesh.ufl_cell(), 0) Wbar_e = FiniteElement("DGT", mesh.ufl_cell(), k) # Composition field space Wh = FunctionSpace(mesh, W_e) Th = FunctionSpace(mesh, T_e) Wbarh = FunctionSpace(mesh, Wbar_e) phi = interpolate(StepFunction(), Wh) gamma0 = interpolate(StepFunction(), Wh) ad = AddDelete(ptcls, 25, 30, [phi], [1], [0.0, 1.0]) ptcls.interpolate(phi, property_idx) ad.do_sweep() lambda_h = Function(Th) psibar_h = Function(Wbarh) # Elements for Stokes W_e_2 = VectorElement("DG", mesh.ufl_cell(), k) T_e_2 = VectorElement("DG", mesh.ufl_cell(), 0) Wbar_e_2 = VectorElement("DGT", mesh.ufl_cell(), k) Wbar_e_2_H12 = VectorElement("CG", mesh.ufl_cell(), k)["facet"] Q_E = FiniteElement("DG", mesh.ufl_cell(), k - 1) Qbar_E = FiniteElement("DGT", mesh.ufl_cell(), k)
lims = np.array([ [xmin, xmin, ymin, ymax], [xmax, xmax, ymin, ymax], [xmin, xmax, ymin, ymin], [xmin, xmax, ymax, ymax], ]) # Particle specific momentum is stored at slot 1 # the second slot will be to store old velocities at particle level property_idx = 1 p = particles(x, [s, s], mesh) ap = advect_rk3(p, W_2, Udiv, "periodic", lims.flatten()) # Particle management AD = AddDelete(p, 10, 20, [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)
[xmin, xmin, ymin, ymax, zmin, zmax], [xmax, xmax, ymin, ymax, zmin, zmax], [xmin, xmax, ymin, ymin, zmin, zmax], [xmin, xmax, ymax, ymax, zmin, zmax], [xmin, xmax, ymin, ymax, zmin, zmin], [xmin, xmax, ymin, ymax, zmax, zmax], ]) # Particle specific momentum is stored at slot 1 # the second slot will be to store old velocities at particle level 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)