Ejemplo n.º 1
0
def norm(p, arg, dd=None):
    """
    :arg arg: is assumed to be a vector, i.e. have shape ``(n,)``.
    """
    sym = _sym()

    if dd is None:
        dd = sym.DD_VOLUME

    dd = sym.as_dofdesc(dd)

    if p == 2:
        norm_squared = sym.NodalSum(dd_in=dd)(
                sym.FunctionSymbol("fabs")(
                    arg * sym.MassOperator()(arg)))

        if isinstance(norm_squared, np.ndarray):
            norm_squared = norm_squared.sum()

        return sym.FunctionSymbol("sqrt")(norm_squared)

    elif p == np.Inf:
        result = sym.NodalMax(dd_in=dd)(sym.FunctionSymbol("fabs")(arg))
        from pymbolic.primitives import Max

        if isinstance(result, np.ndarray):
            from functools import reduce
            result = reduce(Max, result)

        return result

    else:
        raise ValueError("unsupported value of p")
Ejemplo n.º 2
0
def test_external_call(ctx_factory):
    cl_ctx = ctx_factory()
    queue = cl.CommandQueue(cl_ctx)
    actx = PyOpenCLArrayContext(queue)

    def double(queue, x):
        return 2 * x

    from meshmode.mesh.generation import generate_regular_rect_mesh

    dims = 2

    mesh = generate_regular_rect_mesh(a=(0, ) * dims,
                                      b=(1, ) * dims,
                                      n=(4, ) * dims)
    discr = DGDiscretizationWithBoundaries(actx, mesh, order=1)

    ones = sym.Ones(sym.DD_VOLUME)
    op = (ones * 3 + sym.FunctionSymbol("double")(ones))

    from grudge.function_registry import (base_function_registry,
                                          register_external_function)

    freg = register_external_function(base_function_registry,
                                      "double",
                                      implementation=double,
                                      dd=sym.DD_VOLUME)

    bound_op = bind(discr, op, function_registry=freg)

    result = bound_op(actx, double=double)
    assert actx.to_numpy(flatten(result) == 5).all()
Ejemplo n.º 3
0
def test_external_call(actx_factory):
    actx = actx_factory()

    def double(queue, x):
        return 2 * x

    dims = 2

    mesh = mgen.generate_regular_rect_mesh(a=(0, ) * dims,
                                           b=(1, ) * dims,
                                           nelements_per_axis=(4, ) * dims)
    discr = DiscretizationCollection(actx, mesh, order=1)

    ones = sym.Ones(dof_desc.DD_VOLUME)
    op = (ones * 3 + sym.FunctionSymbol("double")(ones))

    from grudge.function_registry import (base_function_registry,
                                          register_external_function)

    freg = register_external_function(base_function_registry,
                                      "double",
                                      implementation=double,
                                      dd=dof_desc.DD_VOLUME)

    bound_op = bind(discr, op, function_registry=freg)

    result = bound_op(actx, double=double)
    assert actx.to_numpy(flatten(result) == 5).all()
Ejemplo n.º 4
0
 def max_eigenvalue_expr(self):
     """Return the largest eigenvalue of Maxwell's equations as a hyperbolic
     system.
     """
     from math import sqrt
     if self.fixed_material:
         return 1/sqrt(self.epsilon*self.mu)  # a number
     else:
         import grudge.symbolic as sym
         return sym.NodalMax()(1/sym.FunctionSymbol("sqrt")(self.epsilon*self.mu))
Ejemplo n.º 5
0
 def max_eigenvalue(self, t, fields=None, discr=None):
     return sym.NodalMax()(sym.FunctionSymbol("fabs")(self.c))