PySDM_examples.Srivastava_1982.simulation

 1import numpy as np
 2from PySDM_examples.Srivastava_1982.settings import SimProducts
 3
 4from PySDM import Builder, Formulae
 5from PySDM.environments import Box
 6from PySDM.products import SuperDropletCountPerGridbox, VolumeFirstMoment, ZerothMoment
 7
 8
 9class Simulation:
10    def __init__(
11        self, n_steps, settings, collision_dynamic=None, double_precision=True
12    ):
13        self.collision_dynamic = collision_dynamic
14        self.settings = settings
15        self.n_steps = n_steps
16
17        self.double_precision = double_precision
18
19        self.simulation_res = {
20            n_sd: {prod: {} for prod in self.settings.prods}
21            for n_sd in self.settings.n_sds
22        }
23
24    def build(self, n_sd, seed, products):
25        env = Box(dt=self.settings.dt, dv=self.settings.dv)
26        builder = Builder(
27            backend=self.settings.backend_class(
28                formulae=Formulae(
29                    constants={"rho_w": self.settings.rho},
30                    fragmentation_function="ConstantMass",
31                    seed=seed,
32                ),
33                double_precision=self.double_precision,
34            ),
35            n_sd=n_sd,
36            environment=env,
37        )
38        builder.add_dynamic(self.collision_dynamic)
39        particulator = builder.build(
40            products=products,
41            attributes={
42                "multiplicity": np.full(n_sd, self.settings.total_number_0 / n_sd),
43                "volume": np.full(
44                    n_sd,
45                    self.settings.total_volume / self.settings.total_number_0,
46                ),
47            },
48        )
49        return particulator
50
51    def run_convergence_analysis(self, x, seeds):
52        for n_sd in self.settings.n_sds:
53            for seed in seeds:
54                products = (
55                    SuperDropletCountPerGridbox(
56                        name=SimProducts.PySDM.super_particle_count.name
57                    ),
58                    VolumeFirstMoment(name=SimProducts.PySDM.total_volume.name),
59                    ZerothMoment(name=SimProducts.PySDM.total_numer.name),
60                )
61
62                particulator = self.build(n_sd, seed, products)
63
64                for prod in self.settings.prods:
65                    self.simulation_res[n_sd][prod][seed] = np.full(
66                        self.n_steps + 1, -np.inf
67                    )
68
69                for step in range(len(x)):
70                    if step != 0:
71                        particulator.run(steps=1)
72                    for prod in self.settings.prods:
73                        (self.simulation_res[n_sd][prod][seed][step],) = (
74                            particulator.products[prod].get()
75                        )
76
77                np.testing.assert_allclose(
78                    actual=self.simulation_res[n_sd][
79                        SimProducts.PySDM.total_volume.name
80                    ][seed],
81                    desired=self.settings.total_volume,
82                    rtol=1e-3,
83                )
84
85        return self.simulation_res
class Simulation:
10class Simulation:
11    def __init__(
12        self, n_steps, settings, collision_dynamic=None, double_precision=True
13    ):
14        self.collision_dynamic = collision_dynamic
15        self.settings = settings
16        self.n_steps = n_steps
17
18        self.double_precision = double_precision
19
20        self.simulation_res = {
21            n_sd: {prod: {} for prod in self.settings.prods}
22            for n_sd in self.settings.n_sds
23        }
24
25    def build(self, n_sd, seed, products):
26        env = Box(dt=self.settings.dt, dv=self.settings.dv)
27        builder = Builder(
28            backend=self.settings.backend_class(
29                formulae=Formulae(
30                    constants={"rho_w": self.settings.rho},
31                    fragmentation_function="ConstantMass",
32                    seed=seed,
33                ),
34                double_precision=self.double_precision,
35            ),
36            n_sd=n_sd,
37            environment=env,
38        )
39        builder.add_dynamic(self.collision_dynamic)
40        particulator = builder.build(
41            products=products,
42            attributes={
43                "multiplicity": np.full(n_sd, self.settings.total_number_0 / n_sd),
44                "volume": np.full(
45                    n_sd,
46                    self.settings.total_volume / self.settings.total_number_0,
47                ),
48            },
49        )
50        return particulator
51
52    def run_convergence_analysis(self, x, seeds):
53        for n_sd in self.settings.n_sds:
54            for seed in seeds:
55                products = (
56                    SuperDropletCountPerGridbox(
57                        name=SimProducts.PySDM.super_particle_count.name
58                    ),
59                    VolumeFirstMoment(name=SimProducts.PySDM.total_volume.name),
60                    ZerothMoment(name=SimProducts.PySDM.total_numer.name),
61                )
62
63                particulator = self.build(n_sd, seed, products)
64
65                for prod in self.settings.prods:
66                    self.simulation_res[n_sd][prod][seed] = np.full(
67                        self.n_steps + 1, -np.inf
68                    )
69
70                for step in range(len(x)):
71                    if step != 0:
72                        particulator.run(steps=1)
73                    for prod in self.settings.prods:
74                        (self.simulation_res[n_sd][prod][seed][step],) = (
75                            particulator.products[prod].get()
76                        )
77
78                np.testing.assert_allclose(
79                    actual=self.simulation_res[n_sd][
80                        SimProducts.PySDM.total_volume.name
81                    ][seed],
82                    desired=self.settings.total_volume,
83                    rtol=1e-3,
84                )
85
86        return self.simulation_res
Simulation(n_steps, settings, collision_dynamic=None, double_precision=True)
11    def __init__(
12        self, n_steps, settings, collision_dynamic=None, double_precision=True
13    ):
14        self.collision_dynamic = collision_dynamic
15        self.settings = settings
16        self.n_steps = n_steps
17
18        self.double_precision = double_precision
19
20        self.simulation_res = {
21            n_sd: {prod: {} for prod in self.settings.prods}
22            for n_sd in self.settings.n_sds
23        }
collision_dynamic
settings
n_steps
double_precision
simulation_res
def build(self, n_sd, seed, products):
25    def build(self, n_sd, seed, products):
26        env = Box(dt=self.settings.dt, dv=self.settings.dv)
27        builder = Builder(
28            backend=self.settings.backend_class(
29                formulae=Formulae(
30                    constants={"rho_w": self.settings.rho},
31                    fragmentation_function="ConstantMass",
32                    seed=seed,
33                ),
34                double_precision=self.double_precision,
35            ),
36            n_sd=n_sd,
37            environment=env,
38        )
39        builder.add_dynamic(self.collision_dynamic)
40        particulator = builder.build(
41            products=products,
42            attributes={
43                "multiplicity": np.full(n_sd, self.settings.total_number_0 / n_sd),
44                "volume": np.full(
45                    n_sd,
46                    self.settings.total_volume / self.settings.total_number_0,
47                ),
48            },
49        )
50        return particulator
def run_convergence_analysis(self, x, seeds):
52    def run_convergence_analysis(self, x, seeds):
53        for n_sd in self.settings.n_sds:
54            for seed in seeds:
55                products = (
56                    SuperDropletCountPerGridbox(
57                        name=SimProducts.PySDM.super_particle_count.name
58                    ),
59                    VolumeFirstMoment(name=SimProducts.PySDM.total_volume.name),
60                    ZerothMoment(name=SimProducts.PySDM.total_numer.name),
61                )
62
63                particulator = self.build(n_sd, seed, products)
64
65                for prod in self.settings.prods:
66                    self.simulation_res[n_sd][prod][seed] = np.full(
67                        self.n_steps + 1, -np.inf
68                    )
69
70                for step in range(len(x)):
71                    if step != 0:
72                        particulator.run(steps=1)
73                    for prod in self.settings.prods:
74                        (self.simulation_res[n_sd][prod][seed][step],) = (
75                            particulator.products[prod].get()
76                        )
77
78                np.testing.assert_allclose(
79                    actual=self.simulation_res[n_sd][
80                        SimProducts.PySDM.total_volume.name
81                    ][seed],
82                    desired=self.settings.total_volume,
83                    rtol=1e-3,
84                )
85
86        return self.simulation_res