PySDM_examples.Berry_1967.example

 1import os
 2
 3import numpy as np
 4from PySDM_examples.Berry_1967.settings import Settings
 5from PySDM_examples.Berry_1967.spectrum_plotter import SpectrumPlotter
 6from PySDM_examples.Shima_et_al_2009.example import run
 7
 8from PySDM.dynamics.collisions.collision_kernels import (
 9    Electric,
10    Geometric,
11    Hydrodynamic,
12)
13
14
15def main(plot: bool, save):
16    with np.errstate(all="ignore"):
17        u_term_approxs = ("GunnKinzer1949",)
18        dts = (1, 10, "adaptive")
19        setup_prop = {
20            Geometric: (0, 100, 200, 300, 400, 500, 600, 700, 750, 800, 850),
21            Electric: (0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000),
22            Hydrodynamic: (0, 1600, 1800, 2000, 2200),
23        }
24        setups = {}
25
26        for u_term_approx in u_term_approxs:
27            setups[u_term_approx] = {}
28            for dt in dts:
29                setups[u_term_approx][dt] = {}
30
31                for kernel_type, steps in setup_prop.items():
32                    s = Settings(terminal_velocity_variant=u_term_approx)
33                    s.dt = 10 if dt == "adaptive" else dt
34                    s.adaptive = dt == "adaptive"
35                    s.kernel = kernel_type()
36                    s._steps = steps
37                    setups[u_term_approx][dt][kernel_type] = s
38
39        states = {}
40        for u_term_approx, setup in setups.items():
41            states[u_term_approx] = {}
42            for dt in setup:
43                states[u_term_approx][dt] = {}
44                for kernel in setup[dt]:
45                    states[u_term_approx][dt][kernel], _ = run(setup[dt][kernel])
46
47    if plot or save is not None:
48        for u_term_approx, setup in setups.items():
49            for dt in setup:
50                for kernel in setup[dt]:
51                    plotter = SpectrumPlotter(setup[dt][kernel], legend=True)
52                    for step, vals in states[u_term_approx][dt][kernel].items():
53                        plotter.plot(vals, step * setup[dt][kernel].dt)
54                    if save is not None:
55                        n_sd = setup[dt][kernel].n_sd
56                        plotter.save(
57                            save
58                            + "/"
59                            + f"{n_sd}_{u_term_approx}_{dt}_{kernel.__name__}"
60                            + "."
61                            + plotter.format
62                        )
63                    if plot:
64                        plotter.show()
65
66
67if __name__ == "__main__":
68    main(plot="CI" not in os.environ, save=".")
def main(plot: bool, save):
16def main(plot: bool, save):
17    with np.errstate(all="ignore"):
18        u_term_approxs = ("GunnKinzer1949",)
19        dts = (1, 10, "adaptive")
20        setup_prop = {
21            Geometric: (0, 100, 200, 300, 400, 500, 600, 700, 750, 800, 850),
22            Electric: (0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000),
23            Hydrodynamic: (0, 1600, 1800, 2000, 2200),
24        }
25        setups = {}
26
27        for u_term_approx in u_term_approxs:
28            setups[u_term_approx] = {}
29            for dt in dts:
30                setups[u_term_approx][dt] = {}
31
32                for kernel_type, steps in setup_prop.items():
33                    s = Settings(terminal_velocity_variant=u_term_approx)
34                    s.dt = 10 if dt == "adaptive" else dt
35                    s.adaptive = dt == "adaptive"
36                    s.kernel = kernel_type()
37                    s._steps = steps
38                    setups[u_term_approx][dt][kernel_type] = s
39
40        states = {}
41        for u_term_approx, setup in setups.items():
42            states[u_term_approx] = {}
43            for dt in setup:
44                states[u_term_approx][dt] = {}
45                for kernel in setup[dt]:
46                    states[u_term_approx][dt][kernel], _ = run(setup[dt][kernel])
47
48    if plot or save is not None:
49        for u_term_approx, setup in setups.items():
50            for dt in setup:
51                for kernel in setup[dt]:
52                    plotter = SpectrumPlotter(setup[dt][kernel], legend=True)
53                    for step, vals in states[u_term_approx][dt][kernel].items():
54                        plotter.plot(vals, step * setup[dt][kernel].dt)
55                    if save is not None:
56                        n_sd = setup[dt][kernel].n_sd
57                        plotter.save(
58                            save
59                            + "/"
60                            + f"{n_sd}_{u_term_approx}_{dt}_{kernel.__name__}"
61                            + "."
62                            + plotter.format
63                        )
64                    if plot:
65                        plotter.show()