PyMPDATA.scalar_field

scalar field abstractions for the staggered grid

 1"""
 2scalar field abstractions for the staggered grid
 3"""
 4
 5import inspect
 6
 7import numpy as np
 8
 9from PyMPDATA.boundary_conditions import Constant
10from PyMPDATA.impl.enumerations import INVALID_INIT_VALUE
11from PyMPDATA.impl.field import Field
12
13
14class ScalarField(Field):
15    """n-dimensional scalar field including halo data, used to represent advectee, g_factor, etc."""
16
17    def __init__(self, data: np.ndarray, halo: int, boundary_conditions: tuple):
18        super().__init__(
19            grid=data.shape,
20            boundary_conditions=boundary_conditions,
21            halo=halo,
22            dtype=data.dtype,
23            fill_halos_name="fill_halos_scalar",
24        )
25
26        for dim_length in data.shape:
27            assert halo <= dim_length
28        for boundary_condition in boundary_conditions:
29            assert not inspect.isclass(boundary_condition)
30
31        shape_with_halo = tuple(data.shape[i] + 2 * halo for i in range(self.n_dims))
32        self.data = np.full(shape_with_halo, INVALID_INIT_VALUE, dtype=data.dtype)
33        self._impl_data = (self.data,)
34        self.domain = tuple(
35            slice(halo, self.data.shape[i] - halo) for i in range(self.n_dims)
36        )
37        self.get()[:] = data[:]
38
39    @staticmethod
40    def clone(field, dtype=None):
41        """returns an instance with the same dimensionality and same halo size as the argument
42        optionally with a different data type"""
43        dtype = dtype or field.dtype
44        # note: copy=False is OK as the ctor anyhow copies the data to an array with halo
45        return ScalarField(
46            field.get().astype(dtype, copy=False), field.halo, field.boundary_conditions
47        )
48
49    def get(self) -> np.ndarray:  # note: actually a view is returned
50        """returns a view (not a copy) of the field data excluding halo"""
51        results = self.data[self.domain]
52        return results
53
54    @staticmethod
55    def make_null(n_dims, traversals):
56        """returns a scalar field instance with no allocated data storage,
57        see `Field._make_null` other properties of the returned field"""
58        return Field._make_null(
59            ScalarField(
60                np.empty(shape=[0] * n_dims),
61                halo=0,
62                boundary_conditions=tuple([Constant(np.nan)] * n_dims),
63            ),
64            traversals,
65        )
class ScalarField(PyMPDATA.impl.field.Field):
15class ScalarField(Field):
16    """n-dimensional scalar field including halo data, used to represent advectee, g_factor, etc."""
17
18    def __init__(self, data: np.ndarray, halo: int, boundary_conditions: tuple):
19        super().__init__(
20            grid=data.shape,
21            boundary_conditions=boundary_conditions,
22            halo=halo,
23            dtype=data.dtype,
24            fill_halos_name="fill_halos_scalar",
25        )
26
27        for dim_length in data.shape:
28            assert halo <= dim_length
29        for boundary_condition in boundary_conditions:
30            assert not inspect.isclass(boundary_condition)
31
32        shape_with_halo = tuple(data.shape[i] + 2 * halo for i in range(self.n_dims))
33        self.data = np.full(shape_with_halo, INVALID_INIT_VALUE, dtype=data.dtype)
34        self._impl_data = (self.data,)
35        self.domain = tuple(
36            slice(halo, self.data.shape[i] - halo) for i in range(self.n_dims)
37        )
38        self.get()[:] = data[:]
39
40    @staticmethod
41    def clone(field, dtype=None):
42        """returns an instance with the same dimensionality and same halo size as the argument
43        optionally with a different data type"""
44        dtype = dtype or field.dtype
45        # note: copy=False is OK as the ctor anyhow copies the data to an array with halo
46        return ScalarField(
47            field.get().astype(dtype, copy=False), field.halo, field.boundary_conditions
48        )
49
50    def get(self) -> np.ndarray:  # note: actually a view is returned
51        """returns a view (not a copy) of the field data excluding halo"""
52        results = self.data[self.domain]
53        return results
54
55    @staticmethod
56    def make_null(n_dims, traversals):
57        """returns a scalar field instance with no allocated data storage,
58        see `Field._make_null` other properties of the returned field"""
59        return Field._make_null(
60            ScalarField(
61                np.empty(shape=[0] * n_dims),
62                halo=0,
63                boundary_conditions=tuple([Constant(np.nan)] * n_dims),
64            ),
65            traversals,
66        )

n-dimensional scalar field including halo data, used to represent advectee, g_factor, etc.

ScalarField(data: numpy.ndarray, halo: int, boundary_conditions: tuple)
18    def __init__(self, data: np.ndarray, halo: int, boundary_conditions: tuple):
19        super().__init__(
20            grid=data.shape,
21            boundary_conditions=boundary_conditions,
22            halo=halo,
23            dtype=data.dtype,
24            fill_halos_name="fill_halos_scalar",
25        )
26
27        for dim_length in data.shape:
28            assert halo <= dim_length
29        for boundary_condition in boundary_conditions:
30            assert not inspect.isclass(boundary_condition)
31
32        shape_with_halo = tuple(data.shape[i] + 2 * halo for i in range(self.n_dims))
33        self.data = np.full(shape_with_halo, INVALID_INIT_VALUE, dtype=data.dtype)
34        self._impl_data = (self.data,)
35        self.domain = tuple(
36            slice(halo, self.data.shape[i] - halo) for i in range(self.n_dims)
37        )
38        self.get()[:] = data[:]
data
domain
@staticmethod
def clone(field, dtype=None):
40    @staticmethod
41    def clone(field, dtype=None):
42        """returns an instance with the same dimensionality and same halo size as the argument
43        optionally with a different data type"""
44        dtype = dtype or field.dtype
45        # note: copy=False is OK as the ctor anyhow copies the data to an array with halo
46        return ScalarField(
47            field.get().astype(dtype, copy=False), field.halo, field.boundary_conditions
48        )

returns an instance with the same dimensionality and same halo size as the argument optionally with a different data type

def get(self) -> numpy.ndarray:
50    def get(self) -> np.ndarray:  # note: actually a view is returned
51        """returns a view (not a copy) of the field data excluding halo"""
52        results = self.data[self.domain]
53        return results

returns a view (not a copy) of the field data excluding halo

@staticmethod
def make_null(n_dims, traversals):
55    @staticmethod
56    def make_null(n_dims, traversals):
57        """returns a scalar field instance with no allocated data storage,
58        see `Field._make_null` other properties of the returned field"""
59        return Field._make_null(
60            ScalarField(
61                np.empty(shape=[0] * n_dims),
62                halo=0,
63                boundary_conditions=tuple([Constant(np.nan)] * n_dims),
64            ),
65            traversals,
66        )

returns a scalar field instance with no allocated data storage, see Field._make_null other properties of the returned field