def test_tri_mass_mat_trig():
    """Check the integral of some trig functions on a square using the mass matrix"""

    from hedge.mesh.generator import make_square_mesh
    from hedge.discretization.local import TriangleDiscretization
    from math import pi, cos, sin

    mesh = make_square_mesh(a=-pi, b=pi, max_area=(2 * pi / 10)**2 / 2)
    discr = discr_class(mesh,
                        TriangleDiscretization(8),
                        debug=discr_class.noninteractive_debug_flags())

    f = discr.interpolate_volume_function(
        lambda x, el: cos(x[0])**2 * sin(x[1])**2)
    ones = discr.interpolate_volume_function(lambda x, el: 1)

    from hedge.optemplate import MassOperator
    mass_op = MassOperator()

    num_integral_1 = numpy.dot(ones, mass_op.apply(discr, f))
    num_integral_2 = numpy.dot(f, mass_op.apply(discr, ones))
    true_integral = pi**2
    err_1 = abs(num_integral_1 - true_integral)
    err_2 = abs(num_integral_2 - true_integral)
    #print err_1, err_2
    assert err_1 < 1e-10
    assert err_2 < 1e-10
def test_1d_mass_mat_trig():
    """Check the integral of some trig functions on an interval using the mass
    matrix
    """
    from hedge.mesh.generator import make_uniform_1d_mesh
    from hedge.discretization.local import IntervalDiscretization
    from math import pi, cos
    from numpy import dot

    mesh = make_uniform_1d_mesh(-4 * pi, 9 * pi, 17, periodic=True)
    discr = discr_class(mesh,
                        IntervalDiscretization(8),
                        debug=discr_class.noninteractive_debug_flags())

    f = discr.interpolate_volume_function(lambda x, el: cos(x[0])**2)
    ones = discr.interpolate_volume_function(lambda x, el: 1)

    from hedge.optemplate import MassOperator
    mass_op = MassOperator()

    num_integral_1 = dot(ones, mass_op.apply(discr, f))
    num_integral_2 = dot(f, mass_op.apply(discr, ones))
    num_integral_3 = discr.integral(f)

    true_integral = 13 * pi / 2
    err_1 = abs(num_integral_1 - true_integral)
    err_2 = abs(num_integral_2 - true_integral)
    err_3 = abs(num_integral_3 - true_integral)

    assert err_1 < 1e-10
    assert err_2 < 1e-10
    assert err_3 < 1e-10
예제 #3
0
def test_tri_mass_mat_trig():
    """Check the integral of some trig functions on a square using the mass matrix"""

    from hedge.mesh.generator import make_square_mesh
    from hedge.discretization.local import TriangleDiscretization
    from math import sqrt, pi, cos, sin

    mesh = make_square_mesh(a=-pi, b=pi, max_area=(2 * pi / 10) ** 2 / 2)
    discr = discr_class(mesh, TriangleDiscretization(8), debug=discr_class.noninteractive_debug_flags())

    f = discr.interpolate_volume_function(lambda x, el: cos(x[0]) ** 2 * sin(x[1]) ** 2)
    ones = discr.interpolate_volume_function(lambda x, el: 1)

    from hedge.optemplate import MassOperator

    mass_op = MassOperator()

    num_integral_1 = numpy.dot(ones, mass_op.apply(discr, f))
    num_integral_2 = numpy.dot(f, mass_op.apply(discr, ones))
    true_integral = pi ** 2
    err_1 = abs(num_integral_1 - true_integral)
    err_2 = abs(num_integral_2 - true_integral)
    # print err_1, err_2
    assert err_1 < 1e-10
    assert err_2 < 1e-10
예제 #4
0
def test_1d_mass_mat_trig():
    """Check the integral of some trig functions on an interval using the mass matrix"""
    from hedge.mesh.generator import make_uniform_1d_mesh
    from hedge.discretization.local import IntervalDiscretization
    from math import sqrt, pi, cos, sin
    from numpy import dot

    mesh = make_uniform_1d_mesh(-4 * pi, 9 * pi, 17, periodic=True)
    discr = discr_class(mesh, IntervalDiscretization(8), debug=discr_class.noninteractive_debug_flags())

    f = discr.interpolate_volume_function(lambda x, el: cos(x[0]) ** 2)
    ones = discr.interpolate_volume_function(lambda x, el: 1)

    from hedge.optemplate import MassOperator

    mass_op = MassOperator()

    num_integral_1 = dot(ones, mass_op.apply(discr, f))
    num_integral_2 = dot(f, mass_op.apply(discr, ones))
    num_integral_3 = discr.integral(f)

    true_integral = 13 * pi / 2
    err_1 = abs(num_integral_1 - true_integral)
    err_2 = abs(num_integral_2 - true_integral)
    err_3 = abs(num_integral_3 - true_integral)

    assert err_1 < 1e-10
    assert err_2 < 1e-10
    assert err_3 < 1e-10
def test_quadrature_tri_mass_mat_monomial():
    """Check that quadrature integration on triangles is exact as designed."""

    from hedge.mesh.generator import make_square_mesh

    mesh = make_square_mesh(a=-1, b=1, max_area=4 * 1 / 8 + 0.001)
    order = 4
    discr = discr_class(mesh,
                        order=order,
                        debug=discr_class.noninteractive_debug_flags(),
                        quad_min_degrees={"quad": 3 * order})

    m, n = 2, 1
    f = Monomial((m, n))
    f_vec = discr.interpolate_volume_function(lambda x, el: f(x))

    from hedge.discretization import ones_on_volume
    ones = ones_on_volume(discr)

    if False:
        from hedge.visualization import SiloVisualizer
        vis = SiloVisualizer(discr)
        visf = vis.make_file("test")
        vis.add_data(visf, [("f", f_vec * f_vec)])
        visf.close()

    from hedge.optemplate import (MassOperator, Field, QuadratureGridUpsampler)
    f_fld = Field("f")
    mass_op = discr.compile(MassOperator()(f_fld * f_fld))
    from hedge.optemplate.primitives import make_common_subexpression as cse
    f_upsamp = cse(QuadratureGridUpsampler("quad")(f_fld))
    quad_mass_op = discr.compile(MassOperator()(f_upsamp * f_upsamp))

    num_integral_1 = numpy.dot(ones, mass_op(f=f_vec))
    num_integral_2 = numpy.dot(ones, quad_mass_op(f=f_vec))
    true_integral = 4 / ((2 * m + 1) * (2 * n + 1))
    err_1 = abs(num_integral_1 - true_integral)
    err_2 = abs(num_integral_2 - true_integral)
    print num_integral_1, num_integral_2, true_integral
    print err_1, err_2
    assert err_1 > 1e-8
    assert err_2 < 1e-14
예제 #6
0
    def op_template(self, apply_minv, u=None, dir_bc=None, neu_bc=None):
        from hedge.optemplate import Field
        if u is None:
            u = Field("u")

        result = PoissonOperator.op_template(self, apply_minv, u, dir_bc,
                                             neu_bc)

        if apply_minv:
            return result + self.k**2 * u
        else:
            from hedge.optemplate import MassOperator
            return result + self.k**2 * MassOperator()(u)
예제 #7
0
    def prepare_rhs(self, rhs):
        """Prepare the right-hand side for the linear system op(u)=rhs(f).

        In matrix form, LDG looks like this:

        .. math::
            Mv = Cu + g
            Mf = Av + Bu + h

        where v is the auxiliary vector, u is the argument of the operator, f
        is the result of the grad operator, g and h are inhom boundary data, and
        A,B,C are some operator+lifting matrices.

        .. math::

            M f = A M^{-1}(Cu + g) + Bu + h

        so the linear system looks like

        .. math::

            M f = A M^{-1} Cu + A M^{-1} g + Bu + h
            M f - A M^{-1} g - h = (A M^{-1} C + B)u (*)

        So the right hand side we're putting together here is really

        .. math::

            M f - A M^{-1} g - h

        .. note::

            Resist the temptation to left-multiply by the inverse
            mass matrix, as this will result in a non-symmetric
            matrix, which (e.g.) a conjugate gradient Krylov
            solver will not take well.
        """
        pop = self.poisson_op

        from hedge.optemplate import MassOperator
        return (
            MassOperator().apply(self.discr, rhs) -
            self.compiled_bc_op(u=self.discr.volume_zeros(),
                                dir_bc=pop.dirichlet_bc.boundary_interpolant(
                                    self.discr, pop.dirichlet_tag),
                                neu_bc=pop.neumann_bc.boundary_interpolant(
                                    self.discr, pop.neumann_tag)))