Example #1
0
    def __init__(self, v, v_out):

        if isinstance(v, expression.Expression) or not isinstance(
                v, (ufl.core.expr.Expr, function.Function)):
            raise ValueError(
                "Can only recover UFL expression or Functions not '%s'" %
                type(v))

        # Check shape values
        if v.ufl_shape != v_out.ufl_shape:
            raise RuntimeError(
                'Shape mismatch between source %s and target function spaces %s in project'
                % (v.ufl_shape, v_out.ufl_shape))

        self._same_fspace = (isinstance(v, function.Function)
                             and v.function_space() == v_out.function_space())
        self.v = v
        self.v_out = v_out
        self.V = v_out.function_space()

        # Check the number of local dofs
        if self.v_out.function_space().finat_element.space_dimension(
        ) != self.v.function_space().finat_element.space_dimension():
            raise RuntimeError(
                "Number of local dofs for each field must be equal.")

        self.average_kernel = kernels.Average(self.V)
def test_average(geometry, mesh):

    cell = mesh.ufl_cell().cellname()
    DG1_elt = FiniteElement("DG", cell, 1, variant="equispaced")
    vec_DG1 = VectorFunctionSpace(mesh, DG1_elt)
    vec_DG0 = VectorFunctionSpace(mesh, "DG", 0)
    vec_CG1 = VectorFunctionSpace(mesh, "CG", 1)

    # We will fill DG1_field with values, and average them to CG_field
    # First need to put the values into DG0 and then interpolate
    DG0_field = Function(vec_DG0)
    DG1_field = Function(vec_DG1)
    CG_field = Function(vec_CG1)
    weights = Function(vec_CG1)

    DG0_field, weights, true_values, CG_index = setup_values(
        geometry, DG0_field, weights)

    DG1_field.interpolate(DG0_field)
    kernel = kernels.Average(vec_CG1)
    kernel.apply(CG_field, weights, DG1_field)

    tolerance = 1e-12
    if geometry == "1D":
        assert abs(CG_field.dat.data[CG_index] - true_values) < tolerance
    elif geometry == "2D":
        assert abs(CG_field.dat.data[CG_index][0] - true_values[0]) < tolerance
        assert abs(CG_field.dat.data[CG_index][1] - true_values[1]) < tolerance
Example #3
0
def test_average(geometry, mesh):

    cell = mesh.ufl_cell().cellname()
    DG1_elt = FiniteElement("DG", cell, 1, variant="equispaced")
    vec_DG1 = VectorFunctionSpace(mesh, DG1_elt)
    vec_CG1 = VectorFunctionSpace(mesh, "CG", 1)

    # We will fill DG_field with values, and average them to CG_field
    DG_field = Function(vec_DG1)
    CG_field = Function(vec_CG1)
    weights = Function(vec_CG1)

    DG_field, weights, true_values = setup_values(geometry, DG_field, weights)

    kernel = kernels.Average(vec_CG1)
    kernel.apply(CG_field, weights, DG_field)

    tolerance = 1e-12
    if geometry == "1D":
        assert abs(CG_field.dat.data[1] - true_values) < tolerance
    elif geometry == "2D":
        assert abs(CG_field.dat.data[2][0] - true_values[0]) < tolerance
        assert abs(CG_field.dat.data[2][1] - true_values[1]) < tolerance