Exemple #1
0
def test_p1_trace(has_dolfin):
    """Test the trace of a P1 Dolfin function."""
    if has_dolfin:
        import dolfin
    else:
        try:
            import dolfin
        except ImportError:
            pytest.skip("DOLFIN must be installed to run this test")
    import bempp.api
    from bempp.api.external.fenics import fenics_to_bempp_trace_data

    fenics_mesh = dolfin.UnitCubeMesh(2, 2, 2)
    fenics_space = dolfin.FunctionSpace(fenics_mesh, "CG", 1)

    bempp_space, trace_matrix = fenics_to_bempp_trace_data(fenics_space)

    fenics_coeffs = np.random.rand(fenics_space.dim())
    bempp_coeffs = trace_matrix @ fenics_coeffs

    fenics_fun = dolfin.Function(fenics_space)
    fenics_fun.vector()[:] = fenics_coeffs
    bempp_fun = bempp.api.GridFunction(bempp_space, coefficients=bempp_coeffs)

    for cell in bempp_space.grid.entity_iterator(0):
        mid = cell.geometry.centroid
        bempp_val = bempp_fun.evaluate(cell.index, np.array([[1 / 3], [1 / 3]]))

        fenics_val = np.zeros(1)
        fenics_fun.eval(fenics_val, mid)

        assert np.allclose(bempp_val.T[0], fenics_val)
Exemple #2
0
def test_nc1_trace():
    """Test the trace of a (N1curl, 1) Dolfin function."""
    import dolfin
    import bempp.api
    from bempp.api.external.fenics import fenics_to_bempp_trace_data

    fenics_mesh = dolfin.UnitCubeMesh(2, 2, 2)
    fenics_space = dolfin.FunctionSpace(fenics_mesh, "N1curl", 1)

    bempp_space, trace_matrix = fenics_to_bempp_trace_data(fenics_space)

    fenics_coeffs = np.random.rand(fenics_space.dim())
    bempp_coeffs = trace_matrix @ fenics_coeffs

    fenics_fun = dolfin.Function(fenics_space)
    fenics_fun.vector()[:] = fenics_coeffs
    bempp_fun = bempp.api.GridFunction(bempp_space, coefficients=bempp_coeffs)

    for cell in bempp_space.grid.entity_iterator(0):
        mid = cell.geometry.centroid
        normal = cell.geometry.normal
        bempp_val = bempp_fun.evaluate(cell.index, np.array([[1 / 3], [1 / 3]]))

        fenics_val = np.zeros(3)
        fenics_fun.eval(fenics_val, mid)
        crossed = np.cross(fenics_val, normal)

        assert np.allclose(bempp_val.T[0], crossed)

mesh = dolfin.UnitCubeMesh(10, 10, 10)


# Next, we make the Dolfin and Bempp function spaces.
# 
# The function ``fenics_to_bempp_trace_data`` will extract the trace space from the Dolfin space and create the matrix ``trace_matrix``, which maps between the dofs (degrees of freedom) in Dolfin and Bempp.

# In[23]:


from bempp.api.external import fenics

fenics_space = dolfin.FunctionSpace(mesh, "CG", 1)
trace_space, trace_matrix =     fenics.fenics_to_bempp_trace_data(fenics_space)
bempp_space = bempp.api.function_space(trace_space.grid, "DP", 0)

print("FEM dofs: {0}".format(mesh.num_vertices()))
print("BEM dofs: {0}".format(bempp_space.global_dof_count))


# We create the boundary operators that we need.

# In[24]:


id_op = bempp.api.operators.boundary.sparse.identity(
    trace_space, bempp_space, bempp_space)
mass = bempp.api.operators.boundary.sparse.identity(
    bempp_space, bempp_space, trace_space)