Exemplo n.º 1
0
def create_cg1_function_space(mesh, sh):
    r = len(sh)
    if r == 0:
        V = function.FunctionSpace(mesh, ("CG", 1))
    elif r == 1:
        V = function.VectorFunctionSpace(mesh, ("CG", 1), dim=sh[0])
    else:
        V = function.TensorFunctionSpace(mesh, ("CG", 1), shape=sh)
    return V
Exemplo n.º 2
0
def _extract_function_space(expression, mesh):
    """Try to extract a suitable function space for projection of given
    expression.

    """

    # Get mesh from expression
    if mesh is None:
        domain = expression.ufl_domain()
        if domain is not None:
            mesh = domain.ufl_cargo()

    # Extract mesh from functions
    if mesh is None:
        # (Not sure if this code is relevant anymore, the above code
        # should cover this)
        # Extract functions
        functions = ufl.algorithms.extract_coefficients(expression)
        for f in functions:
            if isinstance(f, function.Function):
                mesh = f.function_space().mesh
                if mesh is not None:
                    break

    if mesh is None:
        raise RuntimeError(
            "Unable to project expression, cannot find a suitable mesh.")

    # Create function space
    shape = expression.ufl_shape
    if shape == ():
        V = function.FunctionSpace(mesh, ("Lagrange", 1))
    elif len(shape) == 1:
        V = function.VectorFunctionSpace(mesh, ("Lagrange", 1), dim=shape[0])
    elif len(shape) == 2:
        V = function.TensorFunctionSpace(mesh, ("Lagrange", 1), shape=shape)
    else:
        raise RuntimeError("Unhandled rank, shape is {}.".format((shape, )))

    return V