def test_namespacewriter_simple(): f = Frame() f.addfield("Y", 1.) f.addgroup("A") f.A.addfield("B", 0.) 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.writer = writers.namespacewriter() f.writer.dumping = True f.run() Y = f.writer.read.sequence("Y") assert np.all(Y == [1., 0.]) x = f.writer.read.sequence("x") assert np.all(x == [0., 1.]) data = f.writer.read.all() assert np.all(data.Y == [1., 0.]) assert np.all(data.x == [0., 1.]) f.writer.reset()
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.])
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)
def test_frame_run(): f = Frame() f.verbosity = 0 f.addintegrationvariable("x", 0.) with pytest.raises(RuntimeError): f.run() f.integrator = Integrator(f.x) f.integrator._var = None with pytest.raises(RuntimeError): f.run() f.integrator = Integrator(f.x) with pytest.raises(RuntimeError): f.run() f.x = 3. f.x.snapshots = [1., 2.] with pytest.raises(RuntimeError): f.run() def dx(f): return 0.1 f.x.updater = dx f.x = 1.5 f.run() assert np.all(f.x == 2.)
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)
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)
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()
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)
def test_simple_read_files(): f = Frame() f.addgroup("A") f.A.addfield("B", [0., 0.]) 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.writer = writers.hdf5writer() f.run() x = f.writer.read.sequence("x") assert np.all(x == [0., 1.]) Y = f.writer.read.sequence("Y") assert np.all(Y == [1., 0.]) B = f.writer.read.sequence("A.B") assert np.all(B == [0., 0.]) with pytest.raises(TypeError): f.writer.read.sequence(1) data = f.writer.read.all() assert np.all(data.x == [0., 1.]) assert np.all(data.Y == [1., 0.]) assert np.all(data.A.B == [0., 0.]) data0000 = f.writer.read.output(0) assert np.all(data0000.x == 0.) assert np.all(data0000.Y == 1.) assert np.all(data0000.A.B == 0.) with pytest.raises(RuntimeError): f.writer.read.output(2) shutil.rmtree(f.writer.datadir) with pytest.raises(RuntimeError): f.writer.datadir = "temp" f.writer.read.all() with pytest.raises(RuntimeError): f.writer.read.sequence("x") f.writer.datadir = "data"
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.
def test_group_repr_str(): f = Frame() assert isinstance(repr(f), str) assert isinstance(str(f), str) f.addintegrationvariable("x", 0) f.addfield("Y", 1.) f.addfield("abcdefghijklm", 0.) f.addgroup("A") f.addgroup("BCDEFGHIJKLMN") f.C = None f.abcdef1234567 = None f.A.addfield("k", 0.) assert isinstance(repr(f), str) assert isinstance(str(f), str) assert isinstance(repr(f.A), str) assert isinstance(str(f.A), str) f.integrator = Integrator(f.x) f.writer = writers.namespacewriter() assert isinstance(repr(f), str) assert isinstance(str(f), str)
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)
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)
def test_field_jacobian(): f = Frame() f.addfield("Y", 1.) f.addintegrationvariable("x", 0.) def jac(f, x): if x == None: return 0. else: return x f.Y.jacobinator = jac with pytest.raises(RuntimeError): f.Y.jacobian() f.integrator = Integrator(f.x) f.integrator._var = None with pytest.raises(RuntimeError): f.Y.jacobian() f.integrator = Integrator(f.x) assert f.Y.jacobian() == 0. assert f.Y.jacobian(x=1.) == 1.
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)
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)
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
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()
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)