def run_simple(self): # Load model m = os.path.join(myotest.DIR_DATA, 'lr-1991.mmt') m, p, x = myokit.load(m) # Run a simulation s = myokit.ICSimulation(m, p) d, e = s.run(20, log_interval=5) # Create a datablock from the simulation log b = s.block(d, e) del (d, e) # Calculate eigenvalues b.eigenvalues('derivatives')
def test_basic(self): # Test basic usage. # Load model m, p, _ = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) n = m.count_states() # Run a simulation with WarningCollector() as c: s = myokit.ICSimulation(m, p) self.assertIn('`ICSimulation` is deprecated', c.text()) self.assertEqual(s.time(), 0) self.assertEqual(s.state(), m.state()) self.assertEqual(s.default_state(), m.state()) self.assertTrue(np.all(s.derivatives() == np.eye(n))) d, e = s.run(20, log_interval=5) self.assertEqual(s.time(), 20) self.assertNotEqual(s.state(), m.state()) self.assertEqual(s.default_state(), m.state()) self.assertFalse(np.all(s.derivatives() == np.eye(n))) # Create a datablock from the simulation log b = s.block(d, e) # Calculate eigenvalues b.eigenvalues('derivatives') # Log with missing time value d2 = d.clone() del(d2['engine.time']) self.assertRaisesRegex(ValueError, 'time', s.block, d2, e) # Wrong size derivatives array self.assertRaisesRegex(ValueError, 'shape', s.block, d, e[:-1]) # Time can't be negative self.assertRaises(ValueError, s.run, -1) # Test running without a protocol s.set_protocol(None) s.run(1) # Test step size is > 0 self.assertRaises(ValueError, s.set_step_size, 0) # Test negative log interval is ignored s.run(1, log_interval=-1)
def periodic(self): """ Test periodic logging. """ m, p, x = myokit.load(os.path.join(myotest.DIR_DATA, 'lr-1991.mmt')) s = myokit.ICSimulation(m, p) if DEBUG: print('= ' + s.__class__.__name__ + ' :: Periodic logging =') # Set tolerance for equality testing emax = 1e-2 # Time steps for logging are approximate # Test 1: Simple 5 ms simulation, log_interval 0.5 ms d, e = s.run(5, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if DEBUG: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 2: Very short simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 1, 0.5) if DEBUG: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 3: Stop and start a simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if DEBUG: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax)
def test_periodic(self): # Test periodic logging. m, p, x = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) with WarningCollector(): s = myokit.ICSimulation(m, p) # Set tolerance for equality testing emax = 1e-2 # Time steps for logging are approximate # Test 1: Simple 5 ms simulation, log_interval 0.5 ms d, e = s.run(5, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 2: Very short simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 1, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 3: Stop and start a simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax)
def test_progress_reporter(self): # Test running with a progress reporter. m, p, x = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) # Test using a progress reporter with WarningCollector() as c: s = myokit.ICSimulation(m, p) with myokit.tools.capture() as c: s.run(110, progress=myokit.ProgressPrinter()) c = c.text().splitlines() self.assertTrue(len(c) > 0) # Not a progress reporter self.assertRaisesRegex( ValueError, 'ProgressReporter', s.run, 5, progress=12) # Cancel from reporter self.assertRaises( myokit.SimulationCancelledError, s.run, 1, progress=CancellingReporter(0))