PyMPDATA_examples.Arabas_and_Farhat_2020.analysis_table_1

 1import numpy as np
 2from joblib import Parallel, delayed, parallel_backend
 3from PyMPDATA_examples.Arabas_and_Farhat_2020.analysis_figures_2_and_3 import (
 4    error_L2_norm,
 5)
 6from PyMPDATA_examples.Arabas_and_Farhat_2020.setup2_american_put import Settings
 7from PyMPDATA_examples.Arabas_and_Farhat_2020.simulation import Simulation
 8
 9
10def compute_row(simulations):
11    S0 = simulations[0].settings.S0
12    T = simulations[0].settings.T
13    for i in range(1, len(simulations)):
14        assert simulations[i].settings.T == T
15        assert simulations[i].settings.S0 == S0
16    row = [T, S0]
17    f = None
18    for simulation in simulations:
19        f = simulation.run(n_iters=2)
20        row.append(
21            error_L2_norm(
22                simulation.solvers,
23                simulation.settings,
24                simulation.S,
25                simulation.nt,
26                n_iters=2,
27            )
28        )
29        np.testing.assert_almost_equal(simulation.S[simulation.ix_match], S0)
30    row.append(f[simulations[-1].ix_match])
31    row.append(simulations[0].settings.analytical_solution(S0))
32    row.append(simulations[0].settings.analytical_solution(S0, amer=False))
33    return row
34
35
36def table_1_data():
37    with parallel_backend("threading", n_jobs=-2):
38        result = Parallel(verbose=10)(
39            delayed(compute_row)(
40                tuple(
41                    Simulation(Settings(T=T, C_opt=C_opt, S0=S0))
42                    for C_opt in (0.02, 0.01, 0.005)
43                )
44            )
45            for T in (0.25, 0.5, 3)
46            for S0 in (80, 90, 100, 110, 120)
47        )
48        return result
def compute_row(simulations):
11def compute_row(simulations):
12    S0 = simulations[0].settings.S0
13    T = simulations[0].settings.T
14    for i in range(1, len(simulations)):
15        assert simulations[i].settings.T == T
16        assert simulations[i].settings.S0 == S0
17    row = [T, S0]
18    f = None
19    for simulation in simulations:
20        f = simulation.run(n_iters=2)
21        row.append(
22            error_L2_norm(
23                simulation.solvers,
24                simulation.settings,
25                simulation.S,
26                simulation.nt,
27                n_iters=2,
28            )
29        )
30        np.testing.assert_almost_equal(simulation.S[simulation.ix_match], S0)
31    row.append(f[simulations[-1].ix_match])
32    row.append(simulations[0].settings.analytical_solution(S0))
33    row.append(simulations[0].settings.analytical_solution(S0, amer=False))
34    return row
def table_1_data():
37def table_1_data():
38    with parallel_backend("threading", n_jobs=-2):
39        result = Parallel(verbose=10)(
40            delayed(compute_row)(
41                tuple(
42                    Simulation(Settings(T=T, C_opt=C_opt, S0=S0))
43                    for C_opt in (0.02, 0.01, 0.005)
44                )
45            )
46            for T in (0.25, 0.5, 3)
47            for S0 in (80, 90, 100, 110, 120)
48        )
49        return result