Example #1
0
    def __init__(self, shape, extent=None, origin=None, dimensions=None,
                 time_dimension=None, dtype=np.float32, comm=None):
        self._shape = as_tuple(shape)
        self.extent = as_tuple(extent or tuple(1. for _ in self.shape))
        self.dtype = dtype
        origin = as_tuple(origin or tuple(0. for _ in self.shape))

        if dimensions is None:
            # Create the spatial dimensions and constant spacing symbols
            assert(self.dim <= 3)
            dim_names = self._default_dimensions[:self.dim]
            dim_spacing = tuple(self._const(name='h_%s' % n, value=v, dtype=self.dtype)
                                for n, v in zip(dim_names, self.spacing))
            self.dimensions = tuple(SpaceDimension(name=n, spacing=s)
                                    for n, s in zip(dim_names, dim_spacing))
        else:
            self.dimensions = dimensions

        self.origin = tuple(self._const(name='o_%s' % d.name, value=v, dtype=self.dtype)
                            for d, v in zip(self.dimensions, origin))
        # TODO: Raise proper exceptions and logging
        assert (self.dim == len(self.origin) == len(self.extent) == len(self.spacing))
        # Store or create default symbols for time and stepping dimensions
        if time_dimension is None:
            spacing = self._const(name='dt', dtype=self.dtype)
            self.time_dim = TimeDimension(name='time', spacing=spacing)
            self.stepping_dim = self._make_stepping_dim(self.time_dim, name='t')
        elif isinstance(time_dimension, TimeDimension):
            self.time_dim = time_dimension
            self.stepping_dim = self._make_stepping_dim(self.time_dim)
        else:
            raise ValueError("`time_dimension` must be None or of type TimeDimension")

        self._distributor = Distributor(self.shape, self.dimensions, comm)
Example #2
0
    def __init__(self,
                 shape,
                 extent=None,
                 origin=None,
                 dimensions=None,
                 time_dimension=None,
                 dtype=np.float32,
                 subdomains=None,
                 comm=None):
        self._shape = as_tuple(shape)
        self._extent = as_tuple(extent or tuple(1. for _ in self.shape))
        self._dtype = dtype

        if dimensions is None:
            # Create the spatial dimensions and constant spacing symbols
            assert (self.dim <= 3)
            dim_names = self._default_dimensions[:self.dim]
            dim_spacing = tuple(
                self._const(name='h_%s' % n, value=v, dtype=self.dtype)
                for n, v in zip(dim_names, self.spacing))
            self._dimensions = tuple(
                SpaceDimension(name=n, spacing=s)
                for n, s in zip(dim_names, dim_spacing))
        else:
            self._dimensions = dimensions

        self._distributor = Distributor(self.shape, self.dimensions, comm)

        # Initialize SubDomains
        subdomains = tuple(i for i in (Domain(), Interior(),
                                       *as_tuple(subdomains)))
        for counter, i in enumerate(subdomains):
            i.__subdomain_finalize__(self.dimensions,
                                     self.shape,
                                     distributor=self._distributor,
                                     counter=counter)
        self._subdomains = subdomains

        origin = as_tuple(origin or tuple(0. for _ in self.shape))
        self._origin = tuple(
            self._const(name='o_%s' % d.name, value=v, dtype=self.dtype)
            for d, v in zip(self.dimensions, origin))

        # Sanity check
        assert (self.dim == len(self.origin) == len(self.extent) == len(
            self.spacing))

        # Store or create default symbols for time and stepping dimensions
        if time_dimension is None:
            spacing = self._const(name='dt', dtype=self.dtype)
            self._time_dim = self._make_time_dim(spacing)
            self._stepping_dim = self._make_stepping_dim(self.time_dim,
                                                         name='t')
        elif isinstance(time_dimension, TimeDimension):
            self._time_dim = time_dimension
            self._stepping_dim = self._make_stepping_dim(self.time_dim)
        else:
            raise ValueError(
                "`time_dimension` must be None or of type TimeDimension")
Example #3
0
    def __init__(self, shape, extent=None, origin=None, dimensions=None,
                 time_dimension=None, dtype=np.float32, subdomains=None,
                 comm=None, topology=None):
        shape = as_tuple(shape)

        # Create or pull the SpaceDimensions
        if dimensions is None:
            ndim = len(shape)
            assert ndim <= 3
            dim_names = self._default_dimensions[:ndim]
            dim_spacing = tuple(Scalar(name='h_%s' % n, dtype=dtype, is_const=True)
                                for n in dim_names)
            dimensions = tuple(SpaceDimension(name=n, spacing=s)
                               for n, s in zip(dim_names, dim_spacing))
        else:
            for d in dimensions:
                if not d.is_Space:
                    raise ValueError("Cannot create Grid with Dimension `%s` "
                                     "since it's not a SpaceDimension" % d)
                if d.is_Derived and not d.is_Conditional:
                    raise ValueError("Cannot create Grid with derived Dimension `%s` "
                                     "of type `%s`" % (d, type(d)))
            dimensions = dimensions

        super().__init__(shape, dimensions, dtype)

        # Create a Distributor, used internally to implement domain decomposition
        # by all Functions defined on this Grid
        self._distributor = Distributor(shape, dimensions, comm, topology)

        # The physical extent
        self._extent = as_tuple(extent or tuple(1. for _ in self.shape))

        # Initialize SubDomains
        subdomains = tuple(i for i in (Domain(), Interior(), *as_tuple(subdomains)))
        for counter, i in enumerate(subdomains):
            i.__subdomain_finalize__(self, counter=counter)
        self._subdomains = subdomains

        self._origin = as_tuple(origin or tuple(0. for _ in self.shape))
        self._origin_symbols = tuple(Scalar(name='o_%s' % d.name, dtype=dtype,
                                            is_const=True)
                                     for d in self.dimensions)

        # Sanity check
        assert (self.dim == len(self.origin) == len(self.extent) == len(self.spacing))

        # Store or create default symbols for time and stepping dimensions
        if time_dimension is None:
            spacing = Scalar(name='dt', dtype=dtype, is_const=True)
            self._time_dim = TimeDimension(name='time', spacing=spacing)
            self._stepping_dim = SteppingDimension(name='t', parent=self.time_dim)
        elif isinstance(time_dimension, TimeDimension):
            self._time_dim = time_dimension
            self._stepping_dim = SteppingDimension(name='%s_s' % self.time_dim.name,
                                                   parent=self.time_dim)
        else:
            raise ValueError("`time_dimension` must be None or of type TimeDimension")
Example #4
0
 def __setstate__(self, state):
     for k, v in state.items():
         setattr(self, k, v)
     self._distributor = Distributor(self.shape, self.dimensions)