Пример #1
0
def test_basic_replay(mesh, casedir):
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    pp = PostProcessor(dict(casedir=casedir))
    pp.add_fields([
        MockFunctionField(Q, dict(save=True)),
        MockVectorFunctionField(V, dict(save=True))
    ])

    replay_fields = lambda save: [Norm("MockFunctionField", dict(save=save)),
                             Norm("MockVectorFunctionField", dict(save=save)),
                             TimeIntegral("Norm_MockFunctionField", dict(save=save)),]
    rf_names = [f.name for f in replay_fields(False)]

    # Add fields, but don't save (for testing)
    pp.add_fields(replay_fields(False))

    # Solutions to check against
    checks = {}
    pp.update_all({}, 0.0, 0)
    checks[0] = dict([(name, pp.get(name)) for name in rf_names])
    pp.update_all({}, 0.1, 1)
    checks[1] = dict([(name, pp.get(name)) for name in rf_names])
    pp.update_all({}, 0.2, 2)
    pp.finalize_all()
    checks[2] = dict([(name, pp.get(name)) for name in rf_names])

    # Make sure that nothing is saved yet
    for name in rf_names:
        assert not os.path.isfile(os.path.join(pp.get_savedir(name), name+".db"))
    # ----------- Replay -----------------
    pp = PostProcessor(dict(casedir=casedir))

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
    ])

    # This time, save the fields
    pp.add_fields(replay_fields(True))

    replayer = Replay(pp)
    replayer.replay()

    # Test that replayed solution is the same as computed in the original "solve"
    for name in rf_names:
        data = shelve.open(os.path.join(pp.get_savedir(name), name+".db"), 'r')

        for i in range(3):
            assert data.get(str(i), None) == checks[i][name] or abs(data.get(str(i), None) - checks[i][name]) < 1e-8
        data.close()
Пример #2
0
def test_Boundary(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    bmesh = BoundaryMesh(problem.mesh, "exterior")
    bspacepool = SpacePool(bmesh)
    Qb = bspacepool.get_space(1,0)
    Vb = bspacepool.get_space(1,1)

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
    ])

    pp.add_fields([
        Boundary("MockFunctionField"),
        Boundary("MockVectorFunctionField"),
        ])
    D = problem.D

     # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)
        if start_time < t < end_time:
            assert errornorm(
                    pp.get("Boundary_MockFunctionField"),
                    interpolate(Expression("1+x[0]*x[1]*t", degree=1, t=t), Qb)
            ) < 1e-8

            if D == 2:
                assert errornorm(
                    pp.get("Boundary_MockVectorFunctionField"),
                    interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), Vb)
                ) < 1e-8
            else:
                assert errornorm(
                    pp.get("Boundary_MockVectorFunctionField"),
                    interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), Vb)
                ) < 1e-8

    pp.finalize_all()
    assert errornorm(
             pp.get("Boundary_MockFunctionField"),
             interpolate(Expression("1+x[0]*x[1]*t", degree=1, t=t), Qb)
        ) < 1e-8

    if D == 2:
        assert errornorm(
            pp.get("Boundary_MockVectorFunctionField"),
            interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), Vb)
        ) < 1e-8
    else:
        assert errornorm(
            pp.get("Boundary_MockVectorFunctionField"),
            interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), Vb)
        ) < 1e-8
Пример #3
0
def test_default_save(mesh, casedir):
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(1, 0)
    V = spacepool.get_space(1, 1)

    mff = MockFunctionField(Q, dict(save=True))
    mvff = MockVectorFunctionField(V, dict(save=True))
    mtf = MockTupleField(dict(save=True))
    msf = MockScalarField(dict(save=True))

    pp = PostProcessor(dict(casedir=casedir))
    pp.add_fields([mff, mvff, mtf, msf])

    pp.update_all({}, 0.0, 0)
    pp.update_all({}, 0.1, 1)
    pp.update_all({}, 0.2, 2)
    pp.finalize_all()

    for mf in [mff, mvff]:
        assert os.path.isdir(pp.get_savedir(mf.name))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), "metadata.db"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + ".hdf5"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + ".h5"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + ".xdmf"))

        assert len(os.listdir(pp.get_savedir(mf.name))) == 4

        md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"),
                         'r')
        assert 'hdf5' in md["0"]
        assert 'hdf5' in md['saveformats']
        assert 'xdmf' in md["0"]
        assert 'xdmf' in md['saveformats']
        assert set(md['saveformats']) == set(['hdf5', 'xdmf'])
        md.close()

    for mf in [mtf, msf]:
        assert os.path.isdir(pp.get_savedir(mf.name))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), "metadata.db"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + ".db"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + ".txt"))

        md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"),
                         'r')
        assert 'txt' in md["0"]
        assert 'txt' in md['saveformats']
        assert 'shelve' in md["0"]
        assert 'shelve' in md['saveformats']
        assert set(md['saveformats']) == set(['txt', 'shelve'])
        md.close()
Пример #4
0
def test_dolfinplot(mesh):
    pp = PostProcessor()
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(1, 0)
    V = spacepool.get_space(1, 1)

    pp.add_field(MockFunctionField(Q, dict(plot=True)))
    pp.add_field(MockVectorFunctionField(V, dict(plot=True)))
    pp.update_all({}, 0.0, 0)
    pp.update_all({}, 0.1, 1)
    pp.update_all({}, 0.6, 2)
    pp.update_all({}, 1.6, 3)
Пример #5
0
def test_TimeIntegral(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    params = dict(finalize=True, start_time=start_time, end_time=end_time)

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])

    pp.add_fields([
            TimeIntegral("t", params),
            TimeIntegral("MockFunctionField", params),
            TimeIntegral("MockVectorFunctionField", params),
            TimeIntegral("MockTupleField", params),
            ])

    # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)

    pp.finalize_all()

    assert abs( pp.get("TimeIntegral_t") - (0.5*(end_time**2-start_time**2)) ) < 1e-8
    assert errornorm(
        pp.get("TimeIntegral_MockFunctionField"),
        interpolate(Expression("t1-t0+0.5*x[0]*x[1]*(t1*t1-t0*t0)", degree=1, t1=end_time, t0=start_time), Q)
        ) < 1e-8

    D = problem.D
    if D == 2:
        assert errornorm(
           pp.get("TimeIntegral_MockVectorFunctionField"),
           interpolate(Expression(("t1-t0+0.5*x[0]*(t1*t1-t0*t0)", "3*(t1-t0)+0.5*x[1]*(t1*t1-t0*t0)"), degree=1, t1=end_time, t0=start_time), V)
        ) < 1e-8
    elif D == 3:
        assert errornorm(
           pp.get("TimeIntegral_MockVectorFunctionField"),
           interpolate(Expression(("t1-t0+0.5*x[0]*(t1*t1-t0*t0)", "3*(t1-t0)+0.5*x[1]*(t1*t1-t0*t0)", "10*(t1-t0)+0.5*x[2]*(t1*t1-t0*t0)"), degree=1, t1=end_time, t0=start_time), V)
        ) < 1e-8

    I = 0.5*(end_time**2-start_time**2)
    assert max( [ abs(x1-x0) for x1,x0 in zip(pp.get("TimeIntegral_MockTupleField"), (I, 3*I, end_time-start_time+5*I) ) ] ) < 1e-8
Пример #6
0
def test_xmlgz_save(mesh, casedir):
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(1, 0)
    V = spacepool.get_space(1, 1)

    mff = MockFunctionField(Q, dict(save=True, save_as="xml.gz"))
    mvff = MockVectorFunctionField(V, dict(save=True, save_as="xml.gz"))

    pp = PostProcessor(dict(casedir=casedir))
    pp.add_fields([mff, mvff])

    pp.update_all({}, 0.0, 0)
    pp.update_all({}, 0.1, 1)
    pp.update_all({}, 0.2, 2)
    pp.finalize_all()

    for mf, FS in [(mff, Q), (mvff, V)]:
        assert os.path.isdir(pp.get_savedir(mf.name))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), "metadata.db"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), "mesh.hdf5"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + "0.xml.gz"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + "1.xml.gz"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + "2.xml.gz"))

        md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"),
                         'r')
        assert 'xml.gz' in md["0"]
        assert 'xml.gz' in md["1"]
        assert 'xml.gz' in md["2"]
        assert 'xml.gz' in md['saveformats']

        assert md['saveformats'] == ['xml.gz']
        md.close()

        assert len(os.listdir(pp.get_savedir(mf.name))) == 1 + 1 + 3

        # Read back
        for i in ["0", "1", "2"]:
            f = Function(
                FS,
                os.path.join(pp.get_savedir(mf.name), mf.name + i + ".xml.gz"))

        assert norm(f) == norm(pp.get(mf.name))
Пример #7
0
def test_Maximum(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])


    pp.add_fields([
        Maximum("t"),
        Maximum("MockFunctionField"),
        Maximum("MockVectorFunctionField"),
        Maximum("MockTupleField"),
        ])

    xmax = MPI.max(mpi_comm_world(), max(problem.mesh.coordinates()[:,0]))
    ymax = MPI.max(mpi_comm_world(), max(problem.mesh.coordinates()[:,1]))
    if problem.D > 2:
        zmax = MPI.max(mpi_comm_world(), max(problem.mesh.coordinates()[:,2]))

     # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)
        if start_time < t < end_time:
            assert abs(pp.get("Maximum_t") - t) < 1e-8
            assert abs(pp.get("Maximum_MockFunctionField") - (1+xmax*ymax*t)) < 1e-8
            if problem.D == 2:
                assert abs(pp.get("Maximum_MockVectorFunctionField") - (3+ymax*t)) < 1e-8
            elif problem.D == 3:
                assert abs(pp.get("Maximum_MockVectorFunctionField") - (10+zmax*t)) < 1e-8
            assert abs(pp.get("Maximum_MockTupleField") - (1+5*t)) < 1e-8

    pp.finalize_all()

    assert abs(pp.get("Maximum_t") - t) < 1e-8
    assert abs(pp.get("Maximum_MockFunctionField") - (1+xmax*ymax*t)) < 1e-8
    if problem.D == 2:
        assert abs(pp.get("Maximum_MockVectorFunctionField") - (3+ymax*t)) < 1e-8
    elif problem.D == 3:
        assert abs(pp.get("Maximum_MockVectorFunctionField") - (10+zmax*t)) < 1e-8
    assert abs(pp.get("Maximum_MockTupleField") - (1+5*t)) < 1e-8
Пример #8
0
def test_hdf5_save(mesh, casedir):
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(1, 0)
    V = spacepool.get_space(1, 1)

    mff = MockFunctionField(Q, dict(save=True, save_as="hdf5"))
    mvff = MockVectorFunctionField(V, dict(save=True, save_as="hdf5"))

    pp = PostProcessor(dict(casedir=casedir))
    pp.add_fields([mff, mvff])

    pp.update_all({}, 0.0, 0)
    pp.update_all({}, 0.1, 1)
    pp.update_all({}, 0.2, 2)
    pp.finalize_all()

    for mf, FS in [(mff, Q), (mvff, V)]:
        assert os.path.isdir(pp.get_savedir(mf.name))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), "metadata.db"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + ".hdf5"))

        md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"),
                         'r')
        assert 'hdf5' in md["0"]
        assert 'hdf5' in md["1"]
        assert 'hdf5' in md["2"]
        assert 'hdf5' in md['saveformats']

        assert md['saveformats'] == ['hdf5']
        md.close()

        assert len(os.listdir(pp.get_savedir(mf.name))) == 2

        # Read back
        hdf5file = HDF5File(
            mpi_comm_world(),
            os.path.join(pp.get_savedir(mf.name), mf.name + ".hdf5"), 'r')
        f = Function(FS)
        for i in ["0", "1", "2"]:
            hdf5file.read(f, mf.name + i)

        assert norm(f) == norm(pp.get(mf.name))
Пример #9
0
def test_TimeDerivative(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    params = dict(finalize=True, start_time=start_time, end_time=end_time)

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])

    pp.add_fields([
            TimeDerivative("t", params),
            TimeDerivative("timestep", params),
            TimeDerivative("MockFunctionField", params),
            TimeDerivative("MockVectorFunctionField", params),
            TimeDerivative("MockTupleField", params),
            ])

    # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)
    pp.finalize_all()

    # Get and check values from the final timestep
    assert abs( (pp.get("TimeDerivative_t", compute=False)) - (1.0) ) < 1e-8
    assert abs( (pp.get("TimeDerivative_timestep", compute=False)) - (1.0/dt) ) < 1e-8
    assert errornorm(pp.get("TimeDerivative_MockFunctionField"),
                     interpolate(Expression("x[0]*x[1]", degree=1), Q)) < 1e-8
    D = problem.D
    if D == 2:
        assert errornorm(pp.get("TimeDerivative_MockVectorFunctionField"),
                         interpolate(Expression(("x[0]", "x[1]"), degree=1), V)) < 1e-8
    elif D == 3:
        assert errornorm(pp.get("TimeDerivative_MockVectorFunctionField"),
                         interpolate(Expression(("x[0]", "x[1]", "x[2]"), degree=1), V)) < 1e-8

    assert max( [ abs(x1-x0) for x1,x0 in zip(pp.get("TimeDerivative_MockTupleField"), (1,3,5)) ] ) < 1e-8
Пример #10
0
def test_Magnitude(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    mesh = problem.mesh
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    D = mesh.geometry().dim()

    f = Function(Q)
    fv = Function(Q)

    mff = MockFunctionField(Q)
    mvff = MockVectorFunctionField(V)

    pp.add_fields([mff, mvff])

    pp.add_fields([
        Magnitude("MockFunctionField"),
        Magnitude("MockVectorFunctionField"),
    ])

    if D == 2:
        vec_expr_mag = Expression("sqrt(pow(1+x[0]*t,2)+pow(3+x[1]*t,2))", degree=2, t=0.0)
    elif D == 3:
        vec_expr_mag = Expression("sqrt(pow(1+x[0]*t,2)+pow(3+x[1]*t,2)+pow(10+x[2]*t,2))", degree=2, t=0.0)

    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)

        expr_mag = mff.expr
        f.interpolate(expr_mag)
        f.vector().abs()
        vec_expr_mag.t = t
        fv.interpolate(vec_expr_mag)

        assert norm(f.vector()-pp.get("Magnitude_MockFunctionField").vector())<1e-12
        assert norm(fv.vector()-pp.get("Magnitude_MockVectorFunctionField").vector())<1e-12
Пример #11
0
def test_Restrict(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    mesh = problem.mesh
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(2,0)
    V = spacepool.get_space(2,1)
    D = V.num_sub_spaces()

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
    ])

    measures = []
    cell_domains = CellFunction("size_t", problem.mesh)
    cell_domains.set_all(0)
    subdomains = AutoSubDomain(lambda x: x[0]<0.5)
    subdomains.mark(cell_domains, 1)
    MPI.barrier(mpi_comm_world())
    submesh = create_submesh(mesh, cell_domains, 1)

    pp.add_fields([
        Restrict("MockFunctionField", submesh),
        Restrict("MockVectorFunctionField", submesh),
    ])


    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)

        assert abs(assemble(pp.get("MockFunctionField")*dx(1, subdomain_data=cell_domains)) - \
                   assemble(pp.get("Restrict_MockFunctionField")*dx)) < 1e-8

        uv = pp.get("MockVectorFunctionField")
        uvr = pp.get("Restrict_MockVectorFunctionField")
        assert abs(assemble(inner(uv,uv)*dx(1, subdomain_data=cell_domains)) - assemble(inner(uvr, uvr)*dx)) < 1e-8
Пример #12
0
def test_SubFunction(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    mesh = problem.mesh
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(2,0)
    V = spacepool.get_space(2,1)

    mff = MockFunctionField(Q)
    mvff = MockVectorFunctionField(V)

    pp.add_fields([mff, mvff])

    D = mesh.geometry().dim()

    if D == 3:
        submesh = UnitCubeMesh(6,6,6)
    elif D == 2:
        submesh = UnitSquareMesh(8,8)
    submesh.coordinates()[:] /= 2.0
    submesh.coordinates()[:] += 0.2

    Q_sub = FunctionSpace(submesh, "CG", 2)
    V_sub = VectorFunctionSpace(submesh, "CG", 2)

    pp.add_fields([
        SubFunction("MockFunctionField", submesh),
        SubFunction("MockVectorFunctionField", submesh),
    ])


    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)
        assert abs(norm(interpolate(mff.expr, Q_sub)) - norm(pp.get("SubFunction_MockFunctionField"))) < 1e-8
        assert abs(norm(interpolate(mvff.expr, V_sub)) - norm(pp.get("SubFunction_MockVectorFunctionField"))) < 1e-8
Пример #13
0
def test_ErrorNorm(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    class MockFunctionField2(MockFunctionField):
        def before_first_compute(self, get):
            t = get('t')
            self.expr = Expression("1+x[0]*x[1]*t+0.4", degree=1, t=t)

    class MockVectorFunctionField2(MockVectorFunctionField):
        def before_first_compute(self, get):
            t = get('t')

            D = self.f.function_space().mesh().geometry().dim()
            if D == 2:
                self.expr = Expression(("1+x[0]*t+0.2", "3+x[1]*t+0.3"), degree=1, t=t)
            elif D == 3:
                self.expr = Expression(("1+x[0]*t+0.2", "3+x[1]*t+0.3", "10+x[2]*t+0.4"), degree=1, t=t)

    class MockTupleField2(MockTupleField):
        def compute(self, get):
            return (t+0.2, 3*t+0.3, 1+5*t+0.4)

    class MockScalarField2(MockScalarField):
        def compute(self, get):
            t = get('t')
            return 3*t**0.5+0.3

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
        MockScalarField(),
    ])

    pp.add_fields([
        MockFunctionField2(Q),
        MockVectorFunctionField2(V),
        MockTupleField2(),
        MockScalarField2(),
    ])

    pp.add_fields([
        ErrorNorm("MockFunctionField", "MockFunctionField2"),
        ErrorNorm("MockVectorFunctionField", "MockVectorFunctionField2"),
        ErrorNorm("MockTupleField", "MockTupleField2"),
        ErrorNorm("MockScalarField", "MockScalarField2"),
    ])

    D = problem.D
    # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)
        if start_time < t < end_time:
            assert abs(pp.get("ErrorNorm_MockFunctionField_MockFunctionField2") - \
                       norm(interpolate(Expression("0.4", degree=1), Q))) < 1e-8

            assert abs(pp.get("ErrorNorm_MockVectorFunctionField_MockVectorFunctionField2") - \
                       norm(interpolate(Expression(["0.2", "0.3", "0.4"][:D], degree=1), V))) < 1e-8

            assert abs(pp.get("ErrorNorm_MockTupleField_MockTupleField2") - \
                       sum([0.2**2, 0.3**2, 0.4**2])**0.5) < 1e-8

            assert abs(pp.get("ErrorNorm_MockScalarField_MockScalarField2")-0.3) < 1e-8

    pp.finalize_all()

    assert abs(pp.get("ErrorNorm_MockFunctionField_MockFunctionField2") - \
                       norm(interpolate(Expression("0.4", degree=1), Q))) < 1e-8

    assert abs(pp.get("ErrorNorm_MockVectorFunctionField_MockVectorFunctionField2") - \
               norm(interpolate(Expression(["0.2", "0.3", "0.4"][:D], degree=1), V))) < 1e-8

    assert abs(pp.get("ErrorNorm_MockTupleField_MockTupleField2") - \
               sum([0.2**2, 0.3**2, 0.4**2])**0.5) < 1e-8

    assert abs(pp.get("ErrorNorm_MockScalarField_MockScalarField2")-0.3) < 1e-8
Пример #14
0
def test_Operators(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    D = problem.mesh.geometry().dim()


    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])

    fields = [
        Norm("MockFunctionField"),
        Norm("MockFunctionField", dict(norm_type='L2')),
        Norm("MockFunctionField", dict(norm_type='H10')),
        Norm("MockVectorFunctionField"),
        Norm("MockVectorFunctionField", dict(norm_type='L2')),
        Norm("MockVectorFunctionField", dict(norm_type='H10')),
        ]
    pp.add_fields(fields)

    exact = dict()
    exact["Norm_MockFunctionField"] = lambda t: norm(interpolate(Expression("1+x[0]*x[1]*t", degree=1, t=t), Q))
    exact["Norm_L2_MockFunctionField"] = lambda t: norm(interpolate(Expression("1+x[0]*x[1]*t", degree=1, t=t), Q), 'L2')
    exact["Norm_H10_MockFunctionField"] = lambda t: norm(interpolate(Expression("1+x[0]*x[1]*t", degree=1, t=t), Q), 'H10')
    if D == 2:
        exact["Norm_MockVectorFunctionField"] = lambda t: norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V))
        exact["Norm_L2_MockVectorFunctionField"] = lambda t: norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V), 'L2')
        exact["Norm_H10_MockVectorFunctionField"] = lambda t: norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V), 'H10')
    elif D == 3:
        exact["Norm_MockVectorFunctionField"] = lambda t: norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V))
        exact["Norm_L2_MockVectorFunctionField"] = lambda t: norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V), 'L2')
        exact["Norm_H10_MockVectorFunctionField"] = lambda t: norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V), 'H10')

    for f1 in fields:
        pp.add_field(f1+2.0)
        pp.add_field(2.0+f1)
        pp.add_field(f1-2.0)
        pp.add_field(2.0-f1)
        pp.add_field(f1*2.0)
        pp.add_field(2.0*f1)
        pp.add_field(f1/2.0)
        pp.add_field(2.0/f1)
        for f2 in fields:
            pp.add_field(f1+f2)
            pp.add_field(f1*f2)
            pp.add_field(f1-f2)
            pp.add_field(f1/f2)
    return
    # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)
        """
        # Skip these time consuming tests
        if start_time < t < end_time:
            for f1 in fields:
                E1 = exact[f1.name](t)
                assert abs(pp.get("Add_%s_2.0" %f1.name) - (2.0+E1)) < 1e-14
                assert abs(pp.get("Add_2.0_%s" %f1.name) - (2.0+E1)) < 1e-14
                assert abs(pp.get("Subtract_%s_2.0" %f1.name) - (E1-2.0)) < 1e-14
                assert abs(pp.get("Subtract_2.0_%s" %f1.name) - (2.0-E1)) < 1e-14
                assert abs(pp.get("Multiply_%s_2.0" %f1.name) - (2.0*E1)) < 1e-14
                assert abs(pp.get("Multiply_2.0_%s" %f1.name) - (2.0*E1)) < 1e-14
                assert abs(pp.get("Divide_%s_2.0" %f1.name) - (E1/2.0)) < 1e-14
                if abs(E1) > 1e-14:
                    assert abs(pp.get("Divide_2.0_%s" %f1.name) - (2.0/E1)) < 1e-14

                for f2 in fields:
                    E2 = exact[f2.name](t)
                    assert abs(pp.get("Add_%s_%s" %(f1.name, f2.name))-(E1+E2)) < 1e-14
                    assert abs(pp.get("Multiply_%s_%s" %(f1.name, f2.name))-(E1*E2)) < 1e-14
                    assert abs(pp.get("Subtract_%s_%s" %(f1.name, f2.name))-(E1-E2)) < 1e-14
                    if abs(E2) > 1e-14:
                        assert abs(pp.get("Divide_%s_%s" %(f1.name, f2.name))-(E1/E2)) < 1e-14
        """
    pp.finalize_all()
    for f1 in fields:
        E1 = exact[f1.name](t)
        assert abs(pp.get("Add_%s_2.0" %f1.name) - (2.0+E1)) < 1e-14
        assert abs(pp.get("Add_2.0_%s" %f1.name) - (2.0+E1)) < 1e-14
        assert abs(pp.get("Subtract_%s_2.0" %f1.name) - (E1-2.0)) < 1e-14
        assert abs(pp.get("Subtract_2.0_%s" %f1.name) - (2.0-E1)) < 1e-14
        assert abs(pp.get("Multiply_%s_2.0" %f1.name) - (2.0*E1)) < 1e-14
        assert abs(pp.get("Multiply_2.0_%s" %f1.name) - (2.0*E1)) < 1e-14
        assert abs(pp.get("Divide_%s_2.0" %f1.name) - (E1/2.0)) < 1e-14
        if abs(E1) > 1e-14:
            assert abs(pp.get("Divide_2.0_%s" %f1.name) - (2.0/E1)) < 1e-14

        for f2 in fields:
            E2 = exact[f2.name](t)
            assert abs(pp.get("Add_%s_%s" %(f1.name, f2.name))-(E1+E2)) < 1e-14
            assert abs(pp.get("Multiply_%s_%s" %(f1.name, f2.name))-(E1*E2)) < 1e-14
            assert abs(pp.get("Subtract_%s_%s" %(f1.name, f2.name))-(E1-E2)) < 1e-14
            if abs(E2) > 1e-14:
                assert abs(pp.get("Divide_%s_%s" %(f1.name, f2.name))-(E1/E2)) < 1e-14
Пример #15
0
def test_DomainAvg_DomainSD(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    mesh = problem.mesh
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(2,0)
    V = spacepool.get_space(2,1)
    D = V.num_sub_spaces()

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])


    measures = []
    facet_domains = FacetFunction("size_t", problem.mesh)
    facet_domains.set_all(0)
    cell_domains = CellFunction("size_t", problem.mesh)
    cell_domains.set_all(0)

    subdomains = AutoSubDomain(lambda x: x[0]<0.5)
    subdomains.mark(facet_domains, 1)
    subdomains.mark(cell_domains, 1)

    measures = [dict(),
                dict(measure=ds),
                dict(cell_domains=cell_domains, indicator=0),
                dict(cell_domains=cell_domains, indicator=1),
                dict(facet_domains=facet_domains, indicator=0),
                dict(facet_domains=facet_domains, indicator=1),]

    for m in measures:
        pp.add_field(DomainAvg("MockFunctionField", **m))
        pp.add_field(DomainAvg("MockVectorFunctionField", **m))
        pp.add_field(DomainSD("MockFunctionField", **m))
        pp.add_field(DomainSD("MockVectorFunctionField", **m))


    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)

        v = assemble(Constant(1)*dx(domain=mesh))
        v_dx0 = assemble(Constant(1)*dx(0, domain=mesh, subdomain_data=cell_domains))
        v_dx1 = assemble(Constant(1)*dx(1, domain=mesh, subdomain_data=cell_domains))
        v_ds = assemble(Constant(1)*ds(domain=mesh))
        v_ds0 = assemble(Constant(1)*ds(0, domain=mesh, subdomain_data=facet_domains))
        v_ds1 = assemble(Constant(1)*ds(1, domain=mesh, subdomain_data=facet_domains))

        u = pp.get("MockFunctionField")
        uv = pp.get("MockVectorFunctionField")

        avg = assemble(u*dx)/v
        avg_dx0 = assemble(u*dx(0, subdomain_data=cell_domains))/v_dx0
        avg_dx1 = assemble(u*dx(1, subdomain_data=cell_domains))/v_dx1

        avg_ds = assemble(u*ds)/v_ds
        avg_ds0 = assemble(u*ds(0, subdomain_data=facet_domains))/v_ds0
        avg_ds1 = assemble(u*ds(1, subdomain_data=facet_domains))/v_ds1

        assert abs(pp.get("DomainAvg_MockFunctionField") - avg) < 1e-8
        assert abs(pp.get("DomainAvg_MockFunctionField-dx0") - avg_dx0) < 1e-8
        assert abs(pp.get("DomainAvg_MockFunctionField-dx1") - avg_dx1) < 1e-8
        assert abs(pp.get("DomainAvg_MockFunctionField-ds") - avg_ds) < 1e-8
        assert abs(pp.get("DomainAvg_MockFunctionField-ds0") - avg_ds0) < 1e-8
        assert abs(pp.get("DomainAvg_MockFunctionField-ds1") - avg_ds1) < 1e-8

        std = sqrt(assemble((u-Constant(avg))**2*dx)/v)
        std_dx0 = sqrt(assemble((u-Constant(avg_dx0))**2*dx(0, subdomain_data=cell_domains))/v_dx0)
        std_dx1 = sqrt(assemble((u-Constant(avg_dx1))**2*dx(1, subdomain_data=cell_domains))/v_dx1)

        std_ds = sqrt(assemble((u-Constant(avg_ds))**2*ds)/v_ds)
        std_ds0 = sqrt(assemble((u-Constant(avg_ds0))**2*ds(0, subdomain_data=facet_domains))/v_ds0)
        std_ds1 = sqrt(assemble((u-Constant(avg_ds1))**2*ds(1, subdomain_data=facet_domains))/v_ds1)

        assert abs(pp.get("DomainSD_MockFunctionField") - std) < 1e-8
        assert abs(pp.get("DomainSD_MockFunctionField-dx0") - std_dx0) < 1e-8
        assert abs(pp.get("DomainSD_MockFunctionField-dx1") - std_dx1) < 1e-8
        assert abs(pp.get("DomainSD_MockFunctionField-ds") - std_ds) < 1e-8
        assert abs(pp.get("DomainSD_MockFunctionField-ds0") - std_ds0) < 1e-8
        assert abs(pp.get("DomainSD_MockFunctionField-ds1") - std_ds1) < 1e-8

        avg = [assemble(uv[i]*dx)/v for i in xrange(D)]
        avg_dx0 = [assemble(uv[i]*dx(0, subdomain_data=cell_domains))/v_dx0 for i in xrange(D)]
        avg_dx1 = [assemble(uv[i]*dx(1, subdomain_data=cell_domains))/v_dx1 for i in xrange(D)]

        avg_ds = [assemble(uv[i]*ds)/v_ds for i in xrange(D)]
        avg_ds0 = [assemble(uv[i]*ds(0, subdomain_data=facet_domains))/v_ds0 for i in xrange(D)]
        avg_ds1 = [assemble(uv[i]*ds(1, subdomain_data=facet_domains))/v_ds1 for i in xrange(D)]

        assert max(abs(x-y) for x,y in zip(avg, pp.get("DomainAvg_MockVectorFunctionField"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(avg_dx0, pp.get("DomainAvg_MockVectorFunctionField-dx0"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(avg_dx1, pp.get("DomainAvg_MockVectorFunctionField-dx1"))) < 1e-8

        assert max(abs(x-y) for x,y in zip(avg_ds, pp.get("DomainAvg_MockVectorFunctionField-ds"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(avg_ds0, pp.get("DomainAvg_MockVectorFunctionField-ds0"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(avg_ds1, pp.get("DomainAvg_MockVectorFunctionField-ds1"))) < 1e-8

        std = [sqrt(assemble((uv[i]-Constant(avg[i]))**2*dx)/v) for i in xrange(D)]
        std_dx0 = [sqrt(assemble((uv[i]-Constant(avg_dx0[i]))**2*dx(0, subdomain_data=cell_domains))/v_dx0) for i in xrange(D)]
        std_dx1 = [sqrt(assemble((uv[i]-Constant(avg_dx1[i]))**2*dx(1, subdomain_data=cell_domains))/v_dx1) for i in xrange(D)]

        std_ds = [sqrt(assemble((uv[i]-Constant(avg_ds[i]))**2*ds)/v_ds) for i in xrange(D)]
        std_ds0 = [sqrt(assemble((uv[i]-Constant(avg_ds0[i]))**2*ds(0, subdomain_data=facet_domains))/v_ds0) for i in xrange(D)]
        std_ds1 = [sqrt(assemble((uv[i]-Constant(avg_ds1[i]))**2*ds(1, subdomain_data=facet_domains))/v_ds1) for i in xrange(D)]

        assert max(abs(x-y) for x,y in zip(std, pp.get("DomainSD_MockVectorFunctionField"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(std_dx0, pp.get("DomainSD_MockVectorFunctionField-dx0"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(std_dx1, pp.get("DomainSD_MockVectorFunctionField-dx1"))) < 1e-8

        assert max(abs(x-y) for x,y in zip(std_ds, pp.get("DomainSD_MockVectorFunctionField-ds"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(std_ds0, pp.get("DomainSD_MockVectorFunctionField-ds0"))) < 1e-8
        assert max(abs(x-y) for x,y in zip(std_ds1, pp.get("DomainSD_MockVectorFunctionField-ds1"))) < 1e-8
Пример #16
0
def test_pvd_save(mesh, casedir):
    spacepool = SpacePool(mesh)
    Q = spacepool.get_space(1, 0)
    V = spacepool.get_space(1, 1)

    mff = MockFunctionField(Q, dict(save=True, save_as="pvd"))
    mvff = MockVectorFunctionField(V, dict(save=True, save_as="pvd"))

    pp = PostProcessor(dict(casedir=casedir))
    pp.add_fields([mff, mvff])

    pp.update_all({}, 0.0, 0)
    pp.update_all({}, 0.1, 1)
    pp.update_all({}, 0.2, 2)
    pp.finalize_all()

    for mf, FS in [(mff, Q), (mvff, V)]:
        assert os.path.isdir(pp.get_savedir(mf.name))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), "metadata.db"))
        assert os.path.isfile(
            os.path.join(pp.get_savedir(mf.name), mf.name + ".pvd"))
        if MPI.size(mpi_comm_world()) == 1:
            assert os.path.isfile(
                os.path.join(pp.get_savedir(mf.name),
                             mf.name + "%0.6d.vtu" % 0))
            assert os.path.isfile(
                os.path.join(pp.get_savedir(mf.name),
                             mf.name + "%0.6d.vtu" % 1))
            assert os.path.isfile(
                os.path.join(pp.get_savedir(mf.name),
                             mf.name + "%0.6d.vtu" % 2))
        else:
            assert os.path.isfile(
                os.path.join(pp.get_savedir(mf.name),
                             mf.name + "%0.6d.pvtu" % 0))
            assert os.path.isfile(
                os.path.join(pp.get_savedir(mf.name),
                             mf.name + "%0.6d.pvtu" % 1))
            assert os.path.isfile(
                os.path.join(pp.get_savedir(mf.name),
                             mf.name + "%0.6d.pvtu" % 2))

            for i in range(MPI.size(mpi_comm_world())):
                assert os.path.isfile(
                    os.path.join(pp.get_savedir(mf.name),
                                 mf.name + "_p%d_%0.6d.vtu" % (i, 0)))
                assert os.path.isfile(
                    os.path.join(pp.get_savedir(mf.name),
                                 mf.name + "_p%d_%0.6d.vtu" % (i, 1)))
                assert os.path.isfile(
                    os.path.join(pp.get_savedir(mf.name),
                                 mf.name + "_p%d_%0.6d.vtu" % (i, 2)))

        md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"),
                         'r')
        assert 'pvd' in md["0"]
        assert 'pvd' in md["1"]
        assert 'pvd' in md["2"]
        assert md['saveformats'] == ['pvd']
        md.close()

        assert len(os.listdir(pp.get_savedir(mf.name))) == 1 + 1 + 3 + int(
            MPI.size(mpi_comm_world()) != 1) * MPI.size(mpi_comm_world()) * 3
Пример #17
0
def test_PointEval(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(2,0)
    V = spacepool.get_space(2,1)

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])

    D = problem.mesh.geometry().dim()

    xmin = MPI.min(mpi_comm_world(), min(problem.mesh.coordinates()[:,0]))
    ymin = MPI.min(mpi_comm_world(), min(problem.mesh.coordinates()[:,1]))
    if problem.D > 2:
        zmin = MPI.min(mpi_comm_world(), min(problem.mesh.coordinates()[:,2]))

    xmax = MPI.max(mpi_comm_world(), max(problem.mesh.coordinates()[:,0]))
    ymax = MPI.max(mpi_comm_world(), max(problem.mesh.coordinates()[:,1]))
    if problem.D > 2:
        zmax = MPI.max(mpi_comm_world(), max(problem.mesh.coordinates()[:,2]))

    points = []
    #for i in range(5):
    bbtree = problem.mesh.bounding_box_tree()
    while len(points) < 5:
        p = [xmin+(xmax-xmin)*random(), ymin+(ymax-ymin)*random()]
        if D == 3:
            p += [zmin+(zmax-xmin)*random()]

        points.append(p)

    pp.add_fields([
        PointEval("MockFunctionField", points),
        PointEval("MockVectorFunctionField", points),
        ])

    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)

        if start_time < t < end_time:
            pevalfunction = pp.get("PointEval_MockFunctionField")
            pevalvectorfunction = pp.get("PointEval_MockVectorFunctionField")
            for i, p in enumerate(points):
                if D == 2:
                    x,y = p
                    assert abs( pevalfunction[i] - (1+x*y*t) ) < 1e-10
                    assert sum( abs( pevalvectorfunction[i][k] - (1+x*t, 3+y*t)[k]) for k in range(D)) < 1e-10
                elif D == 3:
                    x,y,z = p
                    assert abs( pevalfunction[i] - (1+x*y*t) ) < 1e-10
                    assert sum( abs( pevalvectorfunction[i][k] - (1+x*t, 3+y*t, 10+z*t)[k]) for k in range(D)) < 1e-10


    pp.finalize_all()

    pevalfunction = pp.get("PointEval_MockFunctionField")
    pevalvectorfunction = pp.get("PointEval_MockVectorFunctionField")
    for i, p in enumerate(points):
        if D == 2:
            x,y = p
            assert abs( pevalfunction[i] - (1+x*y*t) ) < 1e-10
            assert sum( abs( pevalvectorfunction[i][k] - (1+x*t, 3+y*t)[k]) for k in range(D)) < 1e-10
        elif D == 3:
            x,y,z = p
            assert abs( pevalfunction[i] - (1+x*y*t) ) < 1e-10
            assert sum( abs( pevalvectorfunction[i][k] - (1+x*t, 3+y*t, 10+z*t)[k]) for k in range(D)) < 1e-10
Пример #18
0
def test_Norm(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])

    pp.add_fields([
        Norm("t"),
        Norm("t", dict(norm_type='l2')),
        Norm("t", dict(norm_type='linf')),
        Norm("MockFunctionField"),
        Norm("MockFunctionField", dict(norm_type='L2')),
        Norm("MockFunctionField", dict(norm_type='H10')),
        Norm("MockVectorFunctionField"),
        Norm("MockVectorFunctionField", dict(norm_type='L2')),
        Norm("MockVectorFunctionField", dict(norm_type='H10')),
        Norm("MockTupleField"),
        Norm("MockTupleField", dict(norm_type='l4')),
        Norm("MockTupleField", dict(norm_type='linf')),
        ])

    D = problem.mesh.geometry().dim()

    # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)

        if start_time < t < end_time:
            assert abs(pp.get("Norm_t") - t) < 1e-14
            assert abs(pp.get("Norm_l2_t") - t) < 1e-14
            assert abs(pp.get("Norm_linf_t") - t) < 1e-14
            assert abs(pp.get("Norm_MockFunctionField") - norm(interpolate(Expression("1+x[0]*x[1]*t", degree=1, t=t), Q))) < 1e-14
            if D == 2:
                assert abs(pp.get("Norm_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V))) < 1e-14
                assert abs(pp.get("Norm_L2_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V), 'L2')) < 1e-14
                assert abs(pp.get("Norm_H10_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V), 'H10')) < 1e-14
            elif D == 3:
                assert abs(pp.get("Norm_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V))) < 1e-14
                assert abs(pp.get("Norm_L2_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V), 'L2')) < 1e-14
                assert abs(pp.get("Norm_H10_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V), 'H10')) < 1e-14
            assert abs(pp.get("Norm_MockTupleField") - sqrt(t**2+(3*t)**2+(1+5*t)**2)) < 1e-14
            assert abs(pp.get("Norm_l4_MockTupleField") - (t**4+(3*t)**4+(1+5*t)**4)**(0.25)) < 1e-14
            assert abs(pp.get("Norm_linf_MockTupleField") - (1+5*t)) < 1e-14

    pp.finalize_all()

    assert abs(pp.get("Norm_t") - t) < 1e-14
    assert abs(pp.get("Norm_l2_t") - t) < 1e-14
    assert abs(pp.get("Norm_linf_t") - t) < 1e-14
    assert abs(pp.get("Norm_MockFunctionField") - norm(interpolate(Expression("1+x[0]*x[1]*t", degree=1, t=t), Q))) < 1e-14
    if D == 2:
        assert abs(pp.get("Norm_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V))) < 1e-14
        assert abs(pp.get("Norm_L2_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V), 'L2')) < 1e-14
        assert abs(pp.get("Norm_H10_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t"), degree=1, t=t), V), 'H10')) < 1e-14
    elif D == 3:
        assert abs(pp.get("Norm_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V))) < 1e-14
        assert abs(pp.get("Norm_L2_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V), 'L2')) < 1e-14
        assert abs(pp.get("Norm_H10_MockVectorFunctionField") - norm(interpolate(Expression(("1+x[0]*t", "3+x[1]*t", "10+x[2]*t"), degree=1, t=t), V), 'H10')) < 1e-14
    assert abs(pp.get("Norm_MockTupleField") - sqrt(t**2+(3*t)**2+(1+5*t)**2)) < 1e-14
    assert abs(pp.get("Norm_l4_MockTupleField") - (t**4+(3*t)**4+(1+5*t)**4)**(0.25)) < 1e-14
    assert abs(pp.get("Norm_linf_MockTupleField") - (1+5*t)) < 1e-14
Пример #19
0
def test_TimeIntegral_of_TimeDerivative(problem, pp, start_time, end_time, dt):
    # Setup some mock scheme state
    dt, timesteps, start_timestep = compute_regular_timesteps(problem)
    spacepool = SpacePool(problem.mesh)
    Q = spacepool.get_space(1,0)
    V = spacepool.get_space(1,1)

    pp.add_fields([
        MockFunctionField(Q),
        MockVectorFunctionField(V),
        MockTupleField(),
    ])

    pp.add_fields([
        TimeDerivative("t"),
        TimeDerivative("MockFunctionField"),
        TimeDerivative("MockVectorFunctionField"),
        TimeDerivative("MockTupleField"),
        ])

    params = dict(start_time=start_time, end_time=end_time, finalize=False)
    pp.add_fields([
        TimeIntegral("TimeDerivative_t", params),
        TimeIntegral("TimeDerivative_MockFunctionField", params),
        TimeIntegral("TimeDerivative_MockVectorFunctionField", params),
        TimeIntegral("TimeDerivative_MockTupleField", params),
    ])

    # Because of backward finite differencing in TimeDerivative, a numerical error will be introduced if start_time<dt
    err_factor = max(0.0, dt-start_time)

    err_t = err_factor*0.5*(1-start_time/dt)
    err_MockFunctionField = err_factor*norm(interpolate(Expression("0.5*x[0]*x[1]*(1-t0/dt)", degree=1, dt=dt, t0=start_time), Q))

    D = problem.mesh.geometry().dim()
    if D == 2:
        err_MockVectorFunctionField = err_factor*norm(interpolate(Expression(("0.5*x[0]*(1-t0/dt)", "0.5*x[1]*(1-t0/dt)"), degree=1, dt=dt, t0=start_time), V))
    elif D == 3:
        err_MockVectorFunctionField = err_factor*norm(interpolate(Expression(("0.5*x[0]*(1-t0/dt)", "0.5*x[1]*(1-t0/dt)", "0.5*x[2]*(1-t0/dt)"), degree=1, dt=dt, t0=start_time), V))
    else:
        raise Exception("D must be 2 or 3")

    err_MockTupleField = tuple([err_factor*(1-start_time/dt)*x/2.0 for x in [1.0, 3.0, 5.0]])

    if start_time > dt:
        assert err_factor == 0.0

     # Update postprocessor for a number of timesteps, this is where the main code under test is
    for timestep, t in enumerate(timesteps, start_timestep):
        # Run postprocessing step
        pp.update_all({}, t, timestep)
        if start_time < t < end_time:
            assert abs( pp.get("TimeIntegral_TimeDerivative_t") - ((t-start_time)-err_t) ) < 1e-8

    pp.finalize_all()

    assert err_t-1e-8 < abs( pp.get("TimeIntegral_TimeDerivative_t") - (end_time-start_time)) < err_t+1e-8
    assert err_MockFunctionField-1e-8 < \
            errornorm(pp.get("TimeIntegral_TimeDerivative_MockFunctionField"),
                    interpolate(Expression("x[0]*x[1]*(t1-t0)", degree=1, t0=start_time, t1=end_time), Q)
                   ) < \
          err_MockFunctionField+1e-8

    if D == 2:
        assert err_MockVectorFunctionField-1e-8 < \
            errornorm(pp.get("TimeIntegral_TimeDerivative_MockVectorFunctionField"),
                    interpolate(Expression(("x[0]*(t1-t0)", "x[1]*(t1-t0)"), degree=1, t0=start_time, t1=end_time), V)
                   ) < \
          err_MockVectorFunctionField+1e-8
    elif D == 3:
        assert err_MockVectorFunctionField-1e-8 < \
            errornorm(pp.get("TimeIntegral_TimeDerivative_MockVectorFunctionField"),
                    interpolate(Expression(("x[0]*(t1-t0)", "x[1]*(t1-t0)", "x[2]*(t1-t0)"), degree=1, t0=start_time, t1=end_time), V)
                   ) < \
          err_MockVectorFunctionField+1e-8
    else:
        raise Exception("D must be 2 or 3")

    I = end_time-start_time
    assert abs( abs(pp.get("TimeIntegral_TimeDerivative_MockTupleField")[0] - I) - err_MockTupleField[0])  < 1e-8
    assert abs( abs(pp.get("TimeIntegral_TimeDerivative_MockTupleField")[1] - 3*I) - err_MockTupleField[1])  < 1e-8
    assert abs( abs(pp.get("TimeIntegral_TimeDerivative_MockTupleField")[2] - 5*I) - err_MockTupleField[2])  < 1e-8