def test_constant_new(self): """Test that new u[x, y] instances don't cache""" u0 = Constant(name='u') u0.data = 6. u1 = Constant(name='u') u1.data = 2. assert u0.data == 6. assert u1.data == 2.
def test_cache_constant_new(): """Test that new u[x, y] instances don't cache""" u0 = Constant(name='u') u0.data = 6. u1 = Constant(name='u') u1.data = 2. assert u0.data == 6. assert u1.data == 2.
def test_const_change(self): """ Test that Constand.data can be set as required. """ n = 5 t = Constant(name='t', dtype=np.int32) grid = Grid(shape=(2, 2)) x, y = grid.dimensions f = TimeFunction(name='f', grid=grid, save=n+1) f.data[:] = 0 eq = Eq(f.dt-1) stencil = Eq(f.forward, solve(eq, f.forward)) op = Operator([stencil]) op.apply(time_m=0, time_M=n-1, dt=1) check = Function(name='check', grid=grid) eq_test = Eq(check, f[t, x, y]) op_test = Operator([eq_test]) for j in range(0, n+1): t.data = j # Ensure constant is being updated correctly op_test.apply(t=t) assert(np.amax(check.data[:], axis=None) == j) assert(np.amin(check.data[:], axis=None) == j)
def test_lm_fb(self, dat, dtype): """ Test logistic map with forward and backward terms that should cancel. """ iterations = 10000 r = Constant(name='r', dtype=dtype) r.data = dtype(dat) s = dtype(0.1) grid = Grid(shape=(2, 2), extent=(1, 1), dtype=dtype) dt = grid.stepping_dim.spacing print("dt = ", dt) f0 = TimeFunction(name='f0', grid=grid, time_order=2, dtype=dtype) f1 = TimeFunction(name='f1', grid=grid, time_order=2, save=iterations+2, dtype=dtype) initial_condition = dtype(0.7235) lmap0 = Eq(f0.forward, r*f0*(1.0-f0+(1.0/s)*dt*f0.backward - f0.backward+(1.0/s)*dt*f0.forward-f0.forward)) lmap1 = Eq(f1.forward, r*f1*(1.0-f1+(1.0/s)*dt*f1.backward - f1.backward+(1.0/s)*dt*f1.forward-f1.forward)) f0.data[1, :, :] = initial_condition f1.data[1, :, :] = initial_condition op0 = Operator([Eq(f0.forward, dtype(0.0)), lmap0]) op1 = Operator(lmap1) op0(time_m=1, time_M=iterations, dt=s) op1(time_m=1, time_M=iterations, dt=s) assert np.allclose(f0.data[np.mod(iterations+1, 3)], f1.data[iterations+1], atol=0, rtol=0)
def test_lm_ds(self, dat, dtype): """ Test logistic map with 2nd derivative term that should cancel. """ iterations = 10000 r = Constant(name='r', dtype=dtype) r.data = dtype(0.5*dat) s = dtype(0.1) grid = Grid(shape=(2, 2), extent=(1, 1), dtype=dtype) f0 = TimeFunction(name='f0', grid=grid, time_order=2, dtype=dtype) f1 = TimeFunction(name='f1', grid=grid, time_order=2, save=iterations+2, dtype=dtype) initial_condition = dtype(0.7235) lmap0 = Eq(f0.forward, -r*f0.dt2*s**2*(1.0-f0) + r*(1.0-f0)*(f0.backward+f0.forward)) lmap1 = Eq(f1.forward, -r*f1.dt2*s**2*(1.0-f1) + r*(1.0-f1)*(f1.backward+f1.forward)) f0.data[1, :, :] = initial_condition f1.data[1, :, :] = initial_condition op0 = Operator([Eq(f0.forward, dtype(0.0)), lmap0]) op1 = Operator(lmap1) op0(time_m=1, time_M=iterations, dt=s) op1(time_m=1, time_M=iterations, dt=s) assert np.allclose(f0.data[np.mod(iterations+1, 3)], f1.data[iterations+1], atol=0, rtol=0)
def test_constant(): c = Constant(name='c') assert c.data == 0. c.data = 1. pkl_c = pickle.dumps(c) new_c = pickle.loads(pkl_c) # .data is initialized, so it should have been pickled too assert np.all(c.data == 1.) assert np.all(new_c.data == 1.)
def test_derive_constant_value(self): """Ensure that values for :class:`Constant` symbols are derived correctly.""" grid = Grid(shape=(5, 6)) f = Function(name='f', grid=grid) a = Constant(name='a', value=3.) Operator(Eq(f, a))() assert np.allclose(f.data, 3.) g = Function(name='g', grid=grid) b = Constant(name='b') op = Operator(Eq(g, b)) b.data = 4. op() assert np.allclose(g.data, 4.)