Esempio n. 1
0
def test_field_derivative():
    f = Frame()
    f.addfield("Y", 1.)
    f.addintegrationvariable("x", 0.)

    def diff(f, x, Y):
        return -Y

    f.Y.differentiator = diff
    with pytest.raises(RuntimeError):
        f.Y.derivative()
    f.integrator = Integrator(f.x)
    f.integrator._var = None
    with pytest.raises(RuntimeError):
        f.Y.derivative()
    f.integrator = Integrator(f.x)
    assert np.all(f.Y.derivative() == -f.Y)
    f.addfield("Y", [1., 0])
    assert np.all(f.Y.derivative() == 0.)

    def jac(f, x):
        return [[2., 0], [0., 2.]]

    f.Y.jacobinator = jac
    assert np.all(f.Y.derivative() == [2., 0.])
Esempio n. 2
0
def test_namespacewriter_read_empty():
    f = Frame()
    f.addfield("Y", 1.)
    f.writer = writers.namespacewriter()
    with pytest.raises(RuntimeError):
        Y = f.writer.read.sequence("Y")
    with pytest.raises(RuntimeError):
        data = f.writer.read.all()
Esempio n. 3
0
def test_field_set():
    f = Frame()
    f.addfield("Y", 0.)
    f.Y._setvalue(1.)
    assert f.Y == 1.
    f.Y.constant = True
    with pytest.raises(RuntimeError):
        f.Y._setvalue(0.)
Esempio n. 4
0
def test_field_update():
    f = Frame()
    f.addfield("Y", 1.)

    def upd(f):
        return 0.

    f.Y.updater = upd
    f.Y.update()
    assert f.Y == 0.
Esempio n. 5
0
def test_impl_1_euler_gmres_fail():
    f = Frame()
    f.addfield("Y", 1.)

    def jac(f, x):
        return np.array([-1.])

    f.Y.jacobinator = jac
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 0.1

    f.x.updater = dx
    f.x.snapshots = [10.]

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [
        Instruction(schemes.impl_1_euler_gmres,
                    f.Y,
                    controller={
                        "gmres_opt": {
                            "atol": 0.,
                            "tol": 1.e-18,
                            "maxiter": 1
                        }
                    })
    ]

    with pytest.raises(StopIteration):
        f.run()
Esempio n. 6
0
def test_expl_5_cash_karp_adptv():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return f.x.suggested

    f.x.updater = dx
    f.x.snapshots = [10.]
    f.x.suggest(0.1)

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [
        Instruction(schemes.expl_5_cash_karp_adptv,
                    f.Y,
                    controller={"eps": 1.e-3})
    ]

    f.run()
    assert np.allclose(f.Y, 4.57114092616805e-05)
Esempio n. 7
0
def test_expl_3_gottlieb_shu_adptv():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return f.x.suggested

    f.x.updater = dx
    f.x.snapshots = [10.]
    f.x.suggest(0.1)

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [
        Instruction(schemes.expl_3_gottlieb_shu_adptv,
                    f.Y,
                    controller={"eps": 1.e-4})
    ]

    f.run()
    assert np.allclose(f.Y, 4.5390485375346277e-05)
Esempio n. 8
0
def test_expl_2_heun_euler_adaptive():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return f.x.suggested

    f.x.updater = dx
    f.x.snapshots = [10.]
    f.x.suggest(0.1)

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [
        Instruction(schemes.expl_2_heun_euler_adptv,
                    f.Y,
                    controller={"eps": 1.e-3})
    ]

    f.run()
    assert np.allclose(f.Y, 4.553150014598088e-05)
Esempio n. 9
0
def test_expl_5_dormand_prince_adptv():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return f.x.suggested

    f.x.updater = dx
    f.x.snapshots = [10.]
    f.x.suggest(0.1)

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [
        Instruction(schemes.expl_5_dormand_prince_adptv,
                    f.Y,
                    controller={"eps": 1.e-2})
    ]

    f.run()
    assert np.allclose(f.Y, 1.8016162079480785e-06)
Esempio n. 10
0
def test_namespacewriter_read_out_of_bounds():
    f = Frame()
    f.addfield("Y", 1.)
    f.writer = writers.namespacewriter()
    f.writer.verbosity = 0
    f.writer.dumping = False
    f.writer.write(f)
    with pytest.raises(RuntimeError):
        f.writer.read.output(1)
    data0000 = f.writer.read.output(0)
    assert data0000.Y == 1.
    f.writer.reset()
Esempio n. 11
0
def test_write():
    f = Frame()
    f.writer = writers.hdf5writer
    f.writer.verbosity = 0
    filename = os.path.join(f.writer.datadir, "test.out")
    f.writer.write(f, 0, True, filename=filename)
    assert os.path.isfile(filename)
    with pytest.raises(RuntimeError):
        f.writer.write(f, 0, False, filename=filename)
    os.remove(filename)
    f.writer.overwrite = True
    f.writer.dumping = False
    f.writer.verbosity = 1
    f.writer.write(f, 0, False, filename=filename)
    assert os.path.isfile(filename)
    shutil.rmtree(f.writer.datadir)
Esempio n. 12
0
def test_field_repr_str():
    f = Frame()
    fi = Field(f, [0., 0.])
    assert isinstance(repr(fi), str)
    assert isinstance(str(fi), str)
    fi = Field(f, 0., constant=True)
    assert isinstance(repr(fi), str)
    assert isinstance(str(fi), str)
Esempio n. 13
0
def test_field_attributes():
    f = Frame()
    fi = Field(f, 1.)
    with pytest.raises(TypeError):
        fi.constant = "_"
    with pytest.raises(TypeError):
        fi.save = "_"
    with pytest.raises(RuntimeError):
        fi.buffer = None
Esempio n. 14
0
def test_instruction_wrong_step_factor():
    f = Frame()
    fi = Field(f, 0.)
    with pytest.raises(TypeError):
        i = Instruction(schemes.expl_1_euler, fi, fstep=None)
    with pytest.raises(ValueError):
        i = Instruction(schemes.expl_1_euler, fi, fstep=0.)
    with pytest.raises(ValueError):
        i = Instruction(schemes.expl_1_euler, fi, fstep=1.1)
Esempio n. 15
0
def test_hdf5writer_skip():
    f = Frame()
    f.writer = writers.hdf5writer()
    f.addfield("x", 0., save=False)
    f.writeoutput(0)
    with pytest.raises(KeyError):
        x = f.writer.read.sequence("x")
    shutil.rmtree(f.writer.datadir)
Esempio n. 16
0
def test_intvar_update():
    f = Frame()
    intv = IntVar(f, 1.)

    def dx(f):
        return 1.

    intv.updater = dx
    assert intv.update() == None
    assert intv == 1.
Esempio n. 17
0
def test_hdf5writer_dict():
    f = Frame()
    f.n = {1: 1}
    f.writer = writers.hdf5writer()
    with pytest.raises(NotImplementedError):
        f.writeoutput(0)
    shutil.rmtree(f.writer.datadir)
Esempio n. 18
0
def test_simple():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y
    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 1.
    f.x.updater = dx
    f.x.snapshots = [1.]
    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.expl_1_euler, f.Y)]
    f.run()
    assert f.Y == 0.
    assert f.x.prevstepsize == 1.
Esempio n. 19
0
def test_intvar_suggest():
    f = Frame()
    intv = IntVar(f, 0.)
    with pytest.raises(RuntimeError):
        intv.suggested
    with pytest.raises(ValueError):
        intv.suggest(-1.)
    intv.suggest(1.)
    assert intv.suggested == 1.
    intv.suggest(2.)
    assert intv.suggested == 1.
    intv.suggest(2., reset=True)
    assert intv.suggested == 2.
Esempio n. 20
0
def test_frame_attributes():
    f = Frame()
    with pytest.raises(TypeError):
        f.integrator = 1
    with pytest.raises(TypeError):
        f.progressbar = 1
    f.progressbar = Progressbar()
    with pytest.raises(TypeError):
        f.verbosity = "_"
    with pytest.raises(TypeError):
        f.writer = "_"
Esempio n. 21
0
def test_heartbeat_order():
    f = Frame()
    f.addfield("x", 1.)
    f.addfield("y", 2.)
    f.addfield("z", 3.)

    def sys(f):
        f.x = f.y * f.z

    def upd(f):
        f.y = f.z * f.x

    def dia(f):
        f.z = f.x * f.y

    f.updater = Heartbeat(updater=upd, systole=sys, diastole=dia)
    f.update()
    assert f.x == 6.
    assert f.y == 18.
    assert f.z == 108.
Esempio n. 22
0
def test_namespacewriter_read_sequence():
    f = Frame()
    f.addfield("Y", [1., 0])
    f.addfield("x", 0, save=False)
    f.writer = writers.namespacewriter()
    f.writer.write(f)
    with pytest.raises(TypeError):
        f.writer.read.sequence(1)
    with pytest.raises(RuntimeError):
        f.writer.read.sequence("x")
    Y = f.writer.read.sequence("Y")
    assert np.all(Y == [1., 0.])
    f.writer.reset()
Esempio n. 23
0
def test_expl_1_euler():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 0.1

    f.x.updater = dx
    f.x.snapshots = [10.]

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.expl_1_euler, f.Y)]

    f.run()
    assert np.allclose(f.Y, 2.656139888758694e-05)
Esempio n. 24
0
def test_expl_2_heun():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 0.1

    f.x.updater = dx
    f.x.snapshots = [10.]

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.expl_2_heun, f.Y)]

    f.run()
    assert np.allclose(f.Y, 4.6222977814657625e-05)
Esempio n. 25
0
def test_adaptive_update():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y
    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return f.x.suggested
    f.x.updater = dx
    f.x.snapshots = [10.]
    f.x.suggest(100.)

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.expl_5_cash_karp_adptv, f.Y),
                                 Instruction(schemes.update, f.Y)]

    f.run()
    assert f.Y == 5.34990702474703e-3
Esempio n. 26
0
def test_impl_1_euler_gmres():
    f = Frame()
    f.addfield("Y", 1.)

    def jac(f, x):
        return np.array([-1.])

    f.Y.jacobinator = jac
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 0.1

    f.x.updater = dx
    f.x.snapshots = [10.]

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.impl_1_euler_gmres, f.Y)]

    f.run()
    assert np.allclose(f.Y, 7.256571590148018e-05)
Esempio n. 27
0
def test_expl_4_runge_kutta():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 0.1

    f.x.updater = dx
    f.x.snapshots = [10.]

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.expl_4_runge_kutta, f.Y)]

    f.run()
    assert np.allclose(f.Y, 4.540034101629485e-05)
Esempio n. 28
0
def test_adaptive_fail():
    f = Frame()
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y
    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return f.x.suggested
    f.x.updater = dx
    f.x.snapshots = [10.]
    f.x.suggest(100.)

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(
        schemes.expl_5_cash_karp_adptv, f.Y)]
    f.integrator.maxit = 1

    with pytest.raises(StopIteration):
        f.run()
Esempio n. 29
0
def test_impl_2_midpoint_direct():
    f = Frame()
    f.addfield("Y", 1.)

    def jac(f, x):
        return np.array([-1.])

    f.Y.jacobinator = jac
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 0.1

    f.x.updater = dx
    f.x.snapshots = [10.]

    f.integrator = Integrator(f.x)
    f.integrator.instructions = [
        Instruction(schemes.impl_2_midpoint_direct, f.Y)
    ]

    f.run()
    assert np.allclose(f.Y, 4.5022605238147066e-05)
Esempio n. 30
0
def test_hdf5writer_list():
    f = Frame()
    f.n = [1, 1]
    f.writer = writers.hdf5writer()
    f.writeoutput(0)
    f.writeoutput(1)
    data0000 = f.writer.read.output(0)
    assert np.all(data0000.n == [1, 1])
    n = f.writer.read.sequence("n")
    assert np.all(n == [[1, 1], [1, 1]])
    data = f.writer.read.all()
    assert np.all(data.n == [[1, 1], [1, 1]])
    shutil.rmtree(f.writer.datadir)