Пример #1
0
def test_no_exception_if_expected_complex():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0j,
        c=lambda x, y: (5 * y) + 1,
        d=lambda x: -3j,
        k=lambda x, s: 1,
        lower_bound=lambda x: 0,
        upper_bound=lambda x: x,
        f=lambda y: y,
    )

    solver.solve()
Пример #2
0
def test_raise_exception_if_unexpectedly_complex():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0,  # this not being 0j is what makes the test fail
        c=lambda x, y: (5 * y) + 1,
        d=lambda x: -3j,
        k=lambda x, s: 1,
        lower_bound=lambda x: 0,
        upper_bound=lambda x: x,
        f=lambda y: y,
    )

    with pytest.raises(UnexpectedlyComplexValuedIDE):
        solver.solve()
Пример #3
0
def example_3():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=1,
        c=lambda x, y: 1 - (29 / 60) * x,
        d=lambda x: 1,
        k=lambda x, s: x * s,
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y**2,
    )
    solver.solve()
    exact = 1 + solver.x + solver.x**2

    return solver, exact
Пример #4
0
def example_1():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0,
        c=lambda x, y: y - (0.5 * x) + (1 / (1 + x)) - np.log(1 + x),
        d=lambda x: 1 / (np.log(2))**2,
        k=lambda x, s: x / (1 + s),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
    )
    solver.solve()
    exact = np.log(1 + solver.x)

    return solver, exact
Пример #5
0
def test_intermediate_solutions_of_scalar_problem_is_list_of_scalar_arrays():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0,
        c=lambda x, y: y - (0.5 * x) + (1 / (1 + x)) - np.log(1 + x),
        d=lambda x: 1 / (np.log(2))**2,
        k=lambda x, s: x / (1 + s),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
        global_error_tolerance=1e-6,
        store_intermediate_y=True,
    )
    solver.solve()

    assert np.all([y.ndim == 1 for y in solver.y_intermediate])
Пример #6
0
def example_1():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=1,
        c=lambda x, y: y - np.cos(2 * np.pi * x) -
        (2 * np.pi * np.sin(2 * np.pi * x)) - (0.5 * np.sin(4 * np.pi * x)),
        d=lambda x: 1,
        k=lambda x, s: np.sin(2 * np.pi * ((2 * x) + s)),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
    )
    solver.solve()
    exact = np.cos(2 * np.pi * solver.x)

    return solver, exact
Пример #7
0
def example_4():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=1,
        c=lambda x, y: (x * (1 + np.sqrt(x)) * np.exp(-np.sqrt(x))) -
        (((x**2) + x + 1) * np.exp(-x)),
        d=lambda x: 1,
        k=lambda x, s: x * s,
        lower_bound=lambda x: x,
        upper_bound=lambda x: np.sqrt(x),
        f=lambda y: y,
    )
    solver.solve()
    exact = np.exp(-solver.x)

    return solver, exact
Пример #8
0
def quickstart_example():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0,
        c=lambda x, y: y - (0.5 * x) + (1 / (1 + x)) - np.log(1 + x),
        d=lambda x: 1 / (np.log(2))**2,
        k=lambda x, s: x / (1 + s),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
    )
    solver.solve()
    exact = np.log(1 + solver.x)

    make_comparison_plot("quickstart_comparison", solver, exact)
    make_error_plot("quickstart_error", solver, exact)
Пример #9
0
def test_intermediate_solutions_of_vector_problem_is_list_of_vector_arrays():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=[0, 1, 0],
        c=lambda x, y:
        [y[0] - (0.5 * x) + (1 / (1 + x)) - np.log(1 + x), y[0], 1],
        d=lambda x: [1 / (np.log(2))**2, 0, 0],
        k=lambda x, s: x / (1 + s),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
        global_error_tolerance=1e-6,
        store_intermediate_y=True,
    )
    solver.solve()

    assert np.all([y.shape == (3, 100) for y in solver.y_intermediate])
Пример #10
0
def test_number_of_intermediate_solutions_is_same_as_iteration_count_plus_one(
):
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0,
        c=lambda x, y: y - (0.5 * x) + (1 / (1 + x)) - np.log(1 + x),
        d=lambda x: 1 / (np.log(2))**2,
        k=lambda x, s: x / (1 + s),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
        global_error_tolerance=1e-6,
        store_intermediate_y=True,
    )
    solver.solve()

    # the +1 is for the initial value, which isn't counted as an iteration, but is counted as a y_intermediate
    assert len(solver.y_intermediate) == solver.iteration + 1
Пример #11
0
def test_callback_is_called_correct_number_of_times(mocker):
    callback = mocker.Mock()

    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0,
        c=lambda x, y: y - (0.5 * x) + (1 / (1 + x)) - np.log(1 + x),
        d=lambda x: 1 / (np.log(2))**2,
        k=lambda x, s: x / (1 + s),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
        global_error_tolerance=1e-6,
        store_intermediate_y=True,
    )
    solver.solve(callback=callback)

    # first iteration is number 0, so add one to left to get total number of callback calls
    assert callback.call_count == solver.iteration + 1
Пример #12
0
def example_1():
    solver = IDESolver(
        x=np.linspace(0, 1, 100),
        y_0=0j,
        c=lambda x, y: (5 * y) + 1,
        d=lambda x: -3j,
        k=lambda x, s: 1,
        lower_bound=lambda x: 0,
        upper_bound=lambda x: x,
        f=lambda y: y,
    )
    solver.solve()
    exact = (2 * np.exp(5 * solver.x / 2) *
             np.sinh(0.5 * np.sqrt(25 - 12j) * solver.x) / np.sqrt(25 - 12j))

    # for s, e in zip(solver.y, exact):
    #     print(s, e)

    return solver, exact
Пример #13
0
def test_warning_when_not_enough_iterations():
    args = dict(
        x=np.linspace(0, 1, 100),
        y_0=0,
        c=lambda x, y: y - (0.5 * x) + (1 / (1 + x)) - np.log(1 + x),
        d=lambda x: 1 / (np.log(2))**2,
        k=lambda x, s: x / (1 + s),
        lower_bound=lambda x: 0,
        upper_bound=lambda x: 1,
        f=lambda y: y,
        global_error_tolerance=1e-6,
    )

    good_solver = IDESolver(**args)
    good_solver.solve()

    bad_solver = IDESolver(**args,
                           max_iterations=int(good_solver.iteration / 2))

    with pytest.warns(IDEConvergenceWarning):
        bad_solver.solve()
Пример #14
0
def iterative():
    pw = 200 * asec
    omega = ion.HydrogenBoundState(1, 0).energy / hbar
    efield = ion.potentials.GaussianPulse.from_number_of_cycles(
        pulse_width=pw, fluence=1 * Jcm2, phase=0).get_electric_field_amplitude
    kern = ide.hydrogen_kernel_LEN

    t = np.linspace(-pw * 3, pw * 3, 200)

    solver = IDESolver(
        y_initial=1,
        x=t,
        c=lambda x, y: -1j * omega * y,
        d=lambda x: -((electron_charge / hbar)**2) * efield(x),
        k=lambda x, s: efield(s) * kern(x - s),
        lower_bound=lambda x: t[0],
        upper_bound=lambda x: x,
        f=lambda y: y,
        global_error_tolerance=1e-6,
    )
    solver.solve()

    return solver