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()
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()
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
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
def test_can_construct_with_positive_global_error_tolerance( dummy_args, global_error_tolerance): hyp.assume( global_error_tolerance > 0 ) # this test does not cover the case where tol = 0 and max_iterations = None IDESolver(*dummy_args, global_error_tolerance=global_error_tolerance)
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
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])
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
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)
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])
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
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
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
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
def test_y_intermediate_list_exists_if_store_intermediate_y_is_true(): 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, ) assert hasattr(solver, "y_intermediate")
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()
def test_cannot_construct_with_negative_global_error_tolerance( dummy_args, global_error_tolerance): hyp.assume(global_error_tolerance < 0) with pytest.raises(InvalidParameter): IDESolver(*dummy_args, global_error_tolerance=global_error_tolerance)
import numpy as np import pytest from idesolver import IDESolver GELMI_EXAMPLES = [ ( # 1 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, ), lambda x: np.log(1 + x), ), ( # 2 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,
def test_cannot_construct_with_nonpositive_max_iterations( dummy_args, max_iterations): with pytest.raises(InvalidParameter): IDESolver(*dummy_args, max_iterations=max_iterations)
def test_can_construct_with_good_smoothing_factor(dummy_args, smoothing_factor): hyp.assume(smoothing_factor != 0 and smoothing_factor != 1) IDESolver(*dummy_args, smoothing_factor=smoothing_factor)
def default_solver(): return IDESolver(x=np.linspace(0, 1, 100), y_0=0)
def test_cannot_construct_with_bad_smoothing_factor(dummy_args, smoothing_factor): with pytest.raises(InvalidParameter): IDESolver(*dummy_args, smoothing_factor=smoothing_factor)
def test_can_construct_without_global_error_tolerance_set_and_with_max_iterations( dummy_args, ): IDESolver(*dummy_args, global_error_tolerance=0, max_iterations=50)
def test_cannot_construct_without_global_error_tolerance_set_and_without_max_iterations( dummy_args, ): with pytest.raises(InvalidParameter): IDESolver(*dummy_args, global_error_tolerance=0, max_iterations=None)
def upper_bound(x): return 1 def f(y): return y if __name__ == "__main__": # create 20 IDESolvers ides = [ IDESolver( x=np.linspace(0, 1, 100), y_0=0, c=c, d=d, k=k, lower_bound=lower_bound, upper_bound=upper_bound, f=f, ) for y_0 in np.linspace(0, 1, 20) ] with multiprocessing.Pool(processes=2) as pool: results = pool.map(run, ides) print(results)
def test_can_construct_with_positive_max_iterations(dummy_args, max_iterations): IDESolver(*dummy_args, max_iterations=max_iterations)