示例#1
0
def test_l2projection_bounded_3D(polynomial_order, lb, ub):
    xmin, ymin, zmin = 0., 0., 0.
    xmax, ymax, zmax = 1., 1., 1.
    nx = 10

    interpolate_expression = Ball(0.15, [0.5, 0.5, 0.5],
                                  degree=3,
                                  lb=lb,
                                  ub=ub)

    property_idx = 1
    mesh = BoxMesh(Point(xmin, ymin, zmin), Point(xmax, ymax, zmax), nx, nx,
                   nx)

    V = FunctionSpace(mesh, "DG", polynomial_order)

    x = RegularBox(Point(0., 0., 0.), Point(1., 1.,
                                            1.)).generate([100, 100, 100])
    s = assign_particle_values(x, interpolate_expression)

    # Just make a complicated particle, possibly with scalars and vectors mixed
    p = particles(x, [s], mesh)

    vh = Function(V)
    lstsq_rho = l2projection(p, V, property_idx)
    lstsq_rho.project(vh.cpp_object(), lb, ub)

    # Assert if it stays within bounds
    assert np.any(vh.vector().get_local() < ub + 1e-12)
    assert np.any(vh.vector().get_local() > lb - 1e-12)
def test_l2projection_bounded(polynomial_order, lb, ub):
    # Test l2 projection if it stays within bounds given by lb and ub
    interpolate_expression = SlottedDisk(radius=0.15,
                                         center=[0.5, 0.5],
                                         width=0.05,
                                         depth=0.,
                                         degree=3,
                                         lb=lb,
                                         ub=ub)

    xmin, xmax = 0., 1.
    ymin, ymax = 0., 1.

    property_idx = 5

    mesh = RectangleMesh(Point(xmin, ymin), Point(xmax, ymax), 40, 40)

    V = FunctionSpace(mesh, "DG", polynomial_order)

    x = RandomRectangle(Point(xmin, ymin), Point(xmax,
                                                 ymax)).generate([500, 500])
    s = assign_particle_values(x, interpolate_expression)

    # Just make a complicated particle, possibly with scalars and vectors mixed
    p = particles(x, [x, s, x, x, s], mesh)

    vh = Function(V)
    lstsq_rho = l2projection(p, V, property_idx)
    lstsq_rho.project(vh, lb, ub)

    # Assert if it stays within bounds
    assert np.all(vh.vector().get_local() < ub + 1e-12)
    assert np.all(vh.vector().get_local() > lb - 1e-12)
def test_l2projection(polynomial_order, in_expression):
    # Test l2 projection for scalar and vector valued expression
    interpolate_expression = Expression(in_expression, degree=3)

    xmin, xmax = 0., 1.
    ymin, ymax = 0., 1.

    property_idx = 5

    mesh = RectangleMesh(Point(xmin, ymin), Point(xmax, ymax), 40, 40)

    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.interpolate(interpolate_expression)

    x = RandomRectangle(Point(xmin, ymin), Point(xmax,
                                                 ymax)).generate([500, 500])
    s = assign_particle_values(x, interpolate_expression)

    # Just make a complicated particle, possibly with scalars and vectors mixed
    p = particles(x, [x, s, x, x, s], mesh)

    vh = Function(V)
    lstsq_rho = l2projection(p, V, property_idx)
    lstsq_rho.project(vh)

    error_sq = abs(assemble(dot(v_exact - vh, v_exact - vh) * dx))
    assert error_sq < 1e-15
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
示例#5
0
def test_mesh_generator_2d():
    """Basic functionality test."""
    mesh = RectangleMesh(Point(0.0, 0.0), Point(1.0, 1.0), 5, 5)
    for x in mesh.coordinates():
        x[0] += 0.5 * x[1]

    w = RandomCell(mesh)
    pts = w.generate(3)
    assert len(pts) == mesh.num_cells() * 3

    interpolate_expression = Expression("x[0] + x[1]", degree=1)
    s = assign_particle_values(pts, interpolate_expression, on_root=False)
    p = particles(pts, [s], mesh)

    assert np.linalg.norm(np.sum(pts, axis=1) - s) <= 1e-15
    assert np.linalg.norm(pts - p.positions()) <= 1e-15
示例#6
0
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
示例#7
0
def test_mesh_generator_3d():
    """Basic functionality test."""
    mesh = BoxMesh(Point(0.0, 0.0, 0.0), Point(1.0, 1.0, 1.0), 5, 5, 5)
    for x in mesh.coordinates():
        x[0] += 0.5 * x[1] + 0.2 * x[2]

    w = RandomCell(mesh)
    pts = w.generate(3)
    assert len(pts) == mesh.num_cells() * 3

    interpolate_expression = Expression("x[0] + x[1] + x[2]", degree=1)
    s = assign_particle_values(pts, interpolate_expression, on_root=False)

    assert np.linalg.norm(np.sum(pts, axis=1) - s) <= 1e-15

    p = particles(pts, [s], mesh)
    assert pts.shape == p.positions().shape

    for i in range(10000):
        s, t, u, v = w._random_bary(4)
        assert s >= 0 and s <= 1
        assert t >= 0 and t <= 1
        assert u >= 0 and u <= 1
        assert v >= 0 and v <= 1
示例#8
0
print('Initializing function spaces')
strain_mesh, temp_mesh, epsII_mesh = Function(Vdg), Function(Vdg), Function(
    Vdg)

# Initial conditions for strain and temp
strain_fun = Expression("0.0", degree=1)
temp_fun = temp_init(Ts, Tb, surf_fun, bed_fun, degree=1)

# Create functions defined on mesh with initial values
strain_mesh.assign(strain_init)
temp_mesh.assign(interpolate(temp_fun, Vdg))
epsII_mesh.assign(strain_init)

# Particle values at nodes
pstrain = assign_particle_values(xp, strain_fun)
pepsII = assign_particle_values(xp, strain_fun)
ptemp = assign_particle_values(xp, temp_fun)
print('Done initializing function spaces')

# Now we initialize the particle class
print('Creating particles')
p = particles(xp, [pstrain, ptemp, pepsII], mesh.mesh)
print('Done particles')

# Make sure we have enough particles per cell
#AD = AddDelete(p, p_min, p_max, [strain_mesh, temp_mesh,epsII_mesh]) # Sweep over mesh to delete/insert particles
#AD.do_sweep()

(xp, pstrain, ptemp,
 pepsII) = (p.return_property(mesh, 0), p.return_property(mesh, 1),
u_expr = Expression((ux+'*'+gt_min, vy+'*'+gt_min), degree=2, t=0.)
u_expre_neg = Expression((ux+'*'+gt_plus, vy+'*'+gt_plus), degree=2, t=0.)

# Mesh velocity
umesh = Function(Vcg)

# Advective velocity
uh = Function(V)
uh.assign(u_expr)

# 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, 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')
ubar0_a = Function(Wbar_2_H12)
Udiv = Function(W_2)
Uh = Function(mixedL)
Uhbar = Function(mixedG)
U0 = Function(mixedL)
Uhbar0 = Function(mixedG)

# Set initial density field
initial_density = BinaryBlock(geometry, float(rho1), float(rho2), degree=1)
zero_expression = Expression(("0.", "0."), degree=1)

# Initialize particles
x = RandomRectangle(Point(xmin, ymin), Point(xmax, ymax)).generate(
    [pres, int(pres * (ymax - ymin) / (xmax - xmin))])
up = assign_particle_values(x, zero_expression)
rhop = assign_particle_values(x, initial_density)

# Increment requires dup to be stored, init zero
dup = up

p = particles(x, [rhop, up, dup], mesh)

# Init rho0 field
lstsq_rho = l2projection(p, Q_Rho, 1)
lstsq_rho.project(rho0, float(rho2), float(rho1))

# Initialize l2 projection for specific momentum
lstsq_u = l2projection(p, W_2, 2)

# Initialize advection class
                        depth=0.,
                        degree=3,
                        lb=lb,
                        ub=ub)

# Function space and velocity field
W = FunctionSpace(mesh, 'DG', k)
psi_h = Function(W)

V = VectorFunctionSpace(mesh, 'DG', 3)
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()
示例#12
0
ubar0_a = Function(Wbar_2_H12)
ubar_a = Function(Wbar_2)
Udiv = Function(W_2)

U0, Uh = Function(mixedL), Function(mixedL)
Uhbar = Function(mixedG)

u0_a.assign(u_exact)
ubar0_a.assign(u_exact)
Udiv.assign(u_exact)

# Initialize particles
x = RandomRectangle(Point(xmin, ymin), Point(xmax,
                                             ymax)).generate([pres, pres])
s = assign_particle_values(x, u_exact)

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