def test_norm_sparse(self): """ Test that norm produces the correct result against numpy """ grid = Grid((101, 101), extent=(1000., 1000.)) nrec = 101 rec0 = SparseTimeFunction(name='rec0', grid=grid, nt=1001, npoint=nrec) rec0.data[:, :] = 1 + np.random.rand(*rec0.shape).astype(grid.dtype) term1 = np.linalg.norm(rec0.data) term2 = norm(rec0) assert np.isclose(term1 / term2 - 1, 0.0, rtol=0.0, atol=1e-5)
def __new__(cls, **kwargs): name = kwargs.pop('name') grid = kwargs.pop('grid') time_range = kwargs.pop('time_range') time_order = kwargs.pop('time_order', 2) p_dim = kwargs.pop('dimension', Dimension(name='p_%s' % name)) coordinates = kwargs.pop('coordinates', kwargs.pop('coordinates_data', None)) # Either `npoint` or `coordinates` must be provided npoint = kwargs.pop('npoint', None) if npoint is None: if coordinates is None: raise TypeError("Need either `npoint` or `coordinates`") npoint = coordinates.shape[0] # Create the underlying SparseTimeFunction object obj = SparseTimeFunction.__new__(cls, name=name, grid=grid, dimensions=(grid.time_dim, p_dim), npoint=npoint, nt=time_range.num, time_order=time_order, coordinates=coordinates, **kwargs) obj._time_range = time_range._rebuild() # If provided, copy initial data into the allocated buffer data = kwargs.get('data') if data is not None: obj.data[:] = data return obj
def __new__(cls, *args, **kwargs): options = kwargs.get('options', {}) if cls in _SymbolCache: obj = sympy.Function.__new__(cls, *args, **options) obj._cached_init() else: name = kwargs.pop('name') grid = kwargs.pop('grid') time_range = kwargs.pop('time_range') time_order = kwargs.pop('time_order', 2) p_dim = kwargs.pop('dimension', Dimension(name='p_%s' % name)) coordinates = kwargs.pop('coordinates', kwargs.pop('coordinates_data', None)) # Either `npoint` or `coordinates` must be provided npoint = kwargs.pop('npoint', None) if npoint is None: if coordinates is None: raise TypeError("Need either `npoint` or `coordinates`") npoint = coordinates.shape[0] # Create the underlying SparseTimeFunction object obj = SparseTimeFunction.__new__(cls, name=name, grid=grid, dimensions=(grid.time_dim, p_dim), npoint=npoint, nt=time_range.num, time_order=time_order, coordinates=coordinates, **kwargs) obj._time_range = time_range._rebuild() # If provided, copy initial data into the allocated buffer data = kwargs.get('data') if data is not None: obj.data[:] = data return obj
def test_min_max_sparse(self): """ Test that mmin/mmax work on SparseFunction """ grid = Grid((101, 101), extent=(1000., 1000.)) nrec = 101 rec0 = SparseTimeFunction(name='rec0', grid=grid, nt=1001, npoint=nrec) rec0.data[:, :] = 1 + np.random.randn(*rec0.shape).astype(grid.dtype) term1 = np.min(rec0.data) term2 = mmin(rec0) assert np.isclose(term1 / term2 - 1, 0.0, rtol=0.0, atol=1e-5) term1 = np.max(rec0.data) term2 = mmax(rec0) assert np.isclose(term1 / term2 - 1, 0.0, rtol=0.0, atol=1e-5)
def __new__(cls, *args, **kwargs): options = kwargs.get('options', {}) key = cls._cache_key(*args, **kwargs) obj = cls._cache_get(key) if obj is not None: newobj = sympy.Function.__new__(cls, *args, **options) newobj.__init_cached__(key) return newobj # Not in cache. Create a new PointSouce via devito.SparseTimeFunction name = kwargs.pop('name') grid = kwargs.pop('grid') time_range = kwargs.pop('time_range') time_order = kwargs.pop('time_order', 2) p_dim = kwargs.pop('dimension', Dimension(name='p_%s' % name)) coordinates = kwargs.pop('coordinates', kwargs.pop('coordinates_data', None)) # Either `npoint` or `coordinates` must be provided npoint = kwargs.pop('npoint', None) if npoint is None: if coordinates is None: raise TypeError("Need either `npoint` or `coordinates`") npoint = coordinates.shape[0] # Create the underlying SparseTimeFunction object obj = SparseTimeFunction.__new__(cls, name=name, grid=grid, dimensions=(grid.time_dim, p_dim), npoint=npoint, nt=time_range.num, time_order=time_order, coordinates=coordinates, **kwargs) obj._time_range = time_range._rebuild() # If provided, copy initial data into the allocated buffer data = kwargs.get('data') if data is not None: obj.data[:] = data return obj
def __new__(cls, *args, **kwargs): options = kwargs.get('options', {}) key = cls obj = cls._cache_get(key) if obj is not None: newobj = sympy.Function.__new__(cls, *args, **options) newobj.__init_cached__(key) return newobj p_dim = kwargs.get('dimension', Dimension('p_%s' % kwargs.get("name"))) npoint = kwargs.get("npoint") coords = kwargs.get("coordinates") if npoint is None: if coords is None: raise TypeError("Need either `npoint` or `coordinates`") else: npoint = coords.shape[0] grid = kwargs.get("grid") ntime = kwargs.get("ntime") if kwargs.get("data") is None: if ntime is None: error('Either data or ntime are required to' 'initialise source/receiver objects') else: ntime = kwargs.get("ntime") or kwargs.get("data").shape[0] # Create the underlying SparseTimeFunction object kwargs["nt"] = ntime kwargs['npoint'] = npoint obj = SparseTimeFunction.__new__(cls, dimensions=[grid.time_dim, p_dim], **kwargs) # If provided, copy initial data into the allocated buffer if kwargs.get("data") is not None: obj.data[:] = kwargs.get("data") return obj
def test_inner_sparse(self): """ Test that inner produces the correct result against numpy """ grid = Grid((101, 101), extent=(1000., 1000.)) nrec = 101 rec0 = SparseTimeFunction(name='rec0', grid=grid, nt=1001, npoint=nrec) rec1 = SparseTimeFunction(name='rec1', grid=grid, nt=1001, npoint=nrec) rec0.data[:, :] = 1 + np.random.randn(*rec0.shape).astype(grid.dtype) rec1.data[:, :] = 1 + np.random.randn(*rec1.shape).astype(grid.dtype) term1 = inner(rec0, rec1) term2 = np.inner(rec0.data.reshape(-1), rec1.data.reshape(-1)) assert np.isclose(term1 / term2 - 1, 0.0, rtol=0.0, atol=1e-5)