PySDM.products.impl.moment_product

common code for products computing statistical moments (e.g., effective radius, acidity)

 1"""common code for products computing statistical moments (e.g., effective radius, acidity)"""
 2
 3from abc import ABC
 4
 5import numpy as np
 6
 7from PySDM.products.impl.product import Product
 8
 9
10class MomentProduct(Product, ABC):
11    def __init__(self, name, unit):
12        super().__init__(name=name, unit=unit)
13        self.moment_0 = None
14        self.moments = None
15
16    def register(self, builder):
17        super().register(builder)
18        self.moment_0 = self.particulator.Storage.empty(
19            self.particulator.mesh.n_cell, dtype=float
20        )
21        self.moments = self.particulator.Storage.empty(
22            (1, self.particulator.mesh.n_cell), dtype=float
23        )
24
25    def _download_moment_to_buffer(
26        self,
27        *,
28        attr,
29        rank,
30        filter_attr="signed water mass",
31        filter_range=(-np.inf, np.inf),
32        weighting_attribute="water mass",
33        weighting_rank=0,
34        skip_division_by_m0=False,
35    ):
36        self.particulator.moments(
37            moment_0=self.moment_0,
38            moments=self.moments,
39            specs={attr: (rank,)},
40            attr_name=filter_attr,
41            attr_range=filter_range,
42            weighting_attribute=weighting_attribute,
43            weighting_rank=weighting_rank,
44            skip_division_by_m0=skip_division_by_m0,
45        )
46        if rank == 0:  # TODO #217
47            self._download_to_buffer(self.moment_0)
48        else:
49            self._download_to_buffer(self.moments[0, :])
class MomentProduct(PySDM.products.impl.product.Product, abc.ABC):
11class MomentProduct(Product, ABC):
12    def __init__(self, name, unit):
13        super().__init__(name=name, unit=unit)
14        self.moment_0 = None
15        self.moments = None
16
17    def register(self, builder):
18        super().register(builder)
19        self.moment_0 = self.particulator.Storage.empty(
20            self.particulator.mesh.n_cell, dtype=float
21        )
22        self.moments = self.particulator.Storage.empty(
23            (1, self.particulator.mesh.n_cell), dtype=float
24        )
25
26    def _download_moment_to_buffer(
27        self,
28        *,
29        attr,
30        rank,
31        filter_attr="signed water mass",
32        filter_range=(-np.inf, np.inf),
33        weighting_attribute="water mass",
34        weighting_rank=0,
35        skip_division_by_m0=False,
36    ):
37        self.particulator.moments(
38            moment_0=self.moment_0,
39            moments=self.moments,
40            specs={attr: (rank,)},
41            attr_name=filter_attr,
42            attr_range=filter_range,
43            weighting_attribute=weighting_attribute,
44            weighting_rank=weighting_rank,
45            skip_division_by_m0=skip_division_by_m0,
46        )
47        if rank == 0:  # TODO #217
48            self._download_to_buffer(self.moment_0)
49        else:
50            self._download_to_buffer(self.moments[0, :])

Helper class that provides a standard way to create an ABC using inheritance.

MomentProduct(name, unit)
12    def __init__(self, name, unit):
13        super().__init__(name=name, unit=unit)
14        self.moment_0 = None
15        self.moments = None
moment_0
moments
def register(self, builder):
17    def register(self, builder):
18        super().register(builder)
19        self.moment_0 = self.particulator.Storage.empty(
20            self.particulator.mesh.n_cell, dtype=float
21        )
22        self.moments = self.particulator.Storage.empty(
23            (1, self.particulator.mesh.n_cell), dtype=float
24        )

Register a virtual subclass of an ABC.

Returns the subclass, to allow usage as a class decorator.