def test_pefrl(self): # Given. integrator = PEFRLIntegrator(fluid=PEFRLStep()) equations = [SHM(dest="fluid", sources=None)] self._setup_integrator(equations=equations, integrator=integrator) tf = np.pi dt = 0.1 * 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=4)
def test_pefrl_is_fourth_order(self): # Given. integrator = PEFRLIntegrator(fluid=PEFRLStep()) 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.1 * 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.assertTrue(err1 / err2 > 16.0)