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_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)