def test_py_stage_is_called_on_gpu(self): # Given. stepper = S12Step() integrator = LeapFrogIntegrator(fluid=stepper) equations = [SHM(dest="fluid", sources=None)] self._setup_integrator(equations=equations, integrator=integrator) dt = 1.0 tf = dt # When def callback(t): pass self._integrate(integrator, dt, tf, callback) self.pa.gpu.pull('x', 'u') # Then self.assertTrue(hasattr(stepper, 'called_with1')) self.assertEqual(stepper.called_with1, (0.0, dt)) self.assertTrue(hasattr(stepper, 'called_with2')) self.assertEqual(stepper.called_with2, (0.5 * dt, dt)) # These are not physically significant as the main purpose is to see # if the py_stage* methods are called. np.testing.assert_array_almost_equal(self.pa.x, [2.0]) np.testing.assert_array_almost_equal(self.pa.u, [1.0])
def test_integrator_calls_py_stage1(self): # Given. stepper = S1Step() integrator = LeapFrogIntegrator(fluid=stepper) equations = [SHM(dest="fluid", sources=None)] self._setup_integrator(equations=equations, integrator=integrator) tf = 1.0 dt = tf # When call_data = [] def callback(t): call_data.append(t) self._integrate(integrator, dt, tf, callback) # Then self.assertEqual(len(call_data), 1) self.assertTrue(hasattr(stepper, 'called_with1')) self.assertEqual(stepper.called_with1, (0.0, dt)) # These are not physically significant as the main purpose is to see # if the py_stage* methods are called. np.testing.assert_array_almost_equal(self.pa.x, [1.5]) np.testing.assert_array_almost_equal(self.pa.u, [0.5])
def test_detection_of_missing_arrays_for_integrator(self): # Given. x = np.asarray([1.0]) u = np.asarray([0.0]) h = np.ones_like(x) pa = get_particle_array(name='fluid', x=x, u=u, h=h, m=h) arrays = [pa] # When integrator = LeapFrogIntegrator(fluid=LeapFrogStep()) equations = [SHM(dest="fluid", sources=None)] kernel = CubicSpline(dim=1) a_eval = AccelerationEval(particle_arrays=arrays, equations=equations, kernel=kernel) comp = SPHCompiler(a_eval, integrator=integrator) # Then self.assertRaises(RuntimeError, comp.compile)
def test_leapfrog(self): # Given. integrator = LeapFrogIntegrator(fluid=LeapFrogStep()) equations = [SHM(dest="fluid", sources=None)] self._setup_integrator(equations=equations, integrator=integrator) tf = np.pi dt = 0.02 * tf # When energy = [] def callback(t): x, u = self.pa.x[0], self.pa.u[0] energy.append(0.5 * (x * x + u * u)) callback(0.0) self._integrate(integrator, dt, tf, callback) # Then energy = np.asarray(energy) self.assertAlmostEqual(np.max(np.abs(energy - 0.5)), 0.0, places=3)
def test_integrator_calls_only_py_when_no_stage(self): # Given. stepper = OnlyPyStep() integrator = LeapFrogIntegrator(fluid=stepper) equations = [SHM(dest="fluid", sources=None)] self._setup_integrator(equations=equations, integrator=integrator) tf = 1.0 dt = tf # When def callback(t): pass self._integrate(integrator, dt, tf, callback) # Then self.assertTrue(hasattr(stepper, 'called_with1')) self.assertEqual(stepper.called_with1, (0.0, dt)) self.assertTrue(hasattr(stepper, 'called_with2')) self.assertEqual(stepper.called_with2, (0.5 * dt, dt)) np.testing.assert_array_almost_equal(self.pa.x, [0.5]) np.testing.assert_array_almost_equal(self.pa.u, [1.5])
def test_leapfrog_is_second_order(self): # Given. integrator = LeapFrogIntegrator(fluid=LeapFrogStep()) equations = [SHM(dest="fluid", sources=None)] self._setup_integrator(equations=equations, integrator=integrator) # Take a dt, find the error, halve dt, and see that error is drops as # desired. # When tf = np.pi dt = 0.02 * tf energy = [] def callback(t): x, u = self.pa.x[0], self.pa.u[0] energy.append(0.5 * (x * x + u * u)) callback(0.0) self._integrate(integrator, dt, tf, callback) energy = np.asarray(energy) err1 = np.max(np.abs(energy - 0.5)) # When self.pa.x[0] = 1.0 self.pa.u[0] = 0.0 energy = [] dt *= 0.5 callback(0.0) self._integrate(integrator, dt, tf, callback) energy = np.asarray(energy) err2 = np.max(np.abs(energy - 0.5)) # Then self.assertTrue(err2 < err1) self.assertAlmostEqual(err1 / err2, 4.0, places=2)