def testMultivariableODE(self): h : np.float = 0.01; y = odesolvers.ThetaMethod(multivariableode, self.iv, self.t0, self.tn, h, self.theta, multivariableodeJ); self.assertEqual(y[0,0],self.iv[0]); self.assertEqual(y[0,1],self.iv[1]); self.assertLess(y[1,1], y[0,1]);
def testErrorHandling(self): iv = np.array([0.0]) # negative time step with self.assertRaises(ValueError): odesolvers.ThetaMethod(stableode, iv, self.t0, self.tn, -0.1, 1) # zero time step with self.assertRaises(ValueError): odesolvers.ThetaMethod(stableode, iv, self.t0, self.tn, 0.0, 1) # time flowing negatively with self.assertRaises(ValueError): odesolvers.ThetaMethod(stableode, iv, self.tn, self.t0, 0.1, 1) # theta out of bounds with self.assertRaises(ValueError): odesolvers.ThetaMethod(stableode, iv, self.t0, self.tn, 0.1, 2) # Negative numerical tolerance with self.assertRaises(ValueError): odesolvers.ThetaMethod(stableode, iv, self.t0, self.tn, 0.1, 0.5, TOL=-0.1) # Negative number of Newton iteration steps with self.assertRaises(ValueError): odesolvers.ThetaMethod(stableode, iv, self.t0, self.tn, 0.1, 0.5, NEWTITER=-2) # Automatic differentiation with self.assertRaises(NotImplementedError): odesolvers.ThetaMethod(stableode, iv, self.t0, self.tn, 0.1, 0)
def testStiffODENotConvergent(self): h : np.float = 0.05; N : np.uint = np.uint(np.ceil((self.tn - self.t0)/h)); # final step y = odesolvers.ThetaMethod(stiffode, self.iv, self.t0, self.tn, h, 0.8, stiffodeJ); self.assertEqual(y[0,0],self.iv[0]); self.assertEqual(y[0,1],self.iv[1]); self.assertGreater(np.absolute(y[N,1]-y[0,1]),3);
return df if __name__ == '__main__': # Problem 1 iv = np.array([1.0, 2.0]) t0: np.float = 0.0 tn: np.float = 1.0 theta = np.array([0, 0.5, 1]) h = np.array([0.01, 0.05]) for thetai in np.nditer(theta): for hi in np.nditer(h): y = odesolvers.ThetaMethod(hw4ex1ode1, iv, t0, tn, hi, thetai, hw4ex1Jacobian1) # plotting the results odesolvers.plotODEsol(y[:, 0], t0, hi, 'y1(t)') tikzplotlib.save(f'problem1-y1-step-{hi}-theta-{thetai}.tex') odesolvers.plotODEsol(y[:, 1], t0, hi, 'y2(t)') tikzplotlib.save(f'problem1-y2-step-{hi}-theta-{thetai}.tex') # Problem 2 iv = np.array([10, 10]) tn = 100 h = np.array([0.1, 0.001]) for thetai in np.nditer(theta): for hi in np.nditer(h):