def periodic(self): """ Test periodic logging. """ m, p, x = myokit.load(os.path.join(myotest.DIR_DATA, 'lr-1991.mmt')) s = myokit.FiberTissueSimulation( m, m, p, ncells_fiber=(1, 1), ncells_tissue=(1, 1)) 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, logf=['engine.time'], logt=myokit.LOG_NONE, 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, logf=['engine.time'], logt=myokit.LOG_NONE, 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, logf=['engine.time'], logt=myokit.LOG_NONE, log_interval=0.5) d, e = s.run(2, logf=d, logt=myokit.LOG_NONE, log_interval=0.5) d, e = s.run(2, logf=d, logt=myokit.LOG_NONE, 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 s1(self): if self._s1 is None: self._s1 = myokit.FiberTissueSimulation( self.mf, self.mt, self.p1, ncells_fiber=(self.nfx, self.nfy), ncells_tissue=(self.ntx, self.nty), nx_paced=10, g_fiber=(235, 100), g_tissue=(9, 5), g_fiber_tissue=9, dt=0.0012, ) return self._s1
def s0(self): # Shared 2x1 simulation if self._s0 is None: self._s0 = myokit.FiberTissueSimulation( self.mt, self.mt, self.p, ncells_fiber=(1, 1), ncells_tissue=(1, 1), nx_paced=1, g_fiber=(0, 0), g_tissue=(0, 0), g_fiber_tissue=0, precision=myokit.DOUBLE_PRECISION, dt=0.01, ) return self._s0
def run_simple(self): # Load models mf = os.path.join(myotest.DIR_DATA, 'dn-1985-normalised.mmt') mt = os.path.join(myotest.DIR_DATA, 'lr-1991.mmt') mf = myokit.load_model(mf) mt = myokit.load_model(mt) # Run times run = .1 # Create pacing protocol p = myokit.pacing.blocktrain(1000, 2.0, offset=.01) # Fiber/Tissue sizes nfx = 8 nfy = 4 ntx = 8 nty = 8 # Create simulation s = myokit.FiberTissueSimulation(mf, mt, p, ncells_fiber=(nfx, nfy), ncells_tissue=(ntx, nty), nx_paced=10, g_fiber=(235, 100), g_tissue=(9, 5), g_fiber_tissue=9) s.set_step_size(0.0012) # Set up logging logf = [ 'engine.time', 'membrane.V', 'isi.isiCa', ] logt = [ 'membrane.V', 'ica.Ca_i', 'ica.ICa', ] # Run simulation with myokit.PyCapture(): logf, logt = s.run(run, logf=logf, logt=logt, log_interval=0.01)
def test_against_cvode(self): # Compare the fiber-tissue simulation output with CVODE output # Load model m = myokit.load_model(os.path.join(DIR_DATA, 'lr-1991.mmt')) # Create pacing protocol p = myokit.pacing.blocktrain(1000, 2.0, offset=0) # Create simulation s1 = myokit.FiberTissueSimulation( m, m, p, ncells_fiber=(1, 1), ncells_tissue=(1, 1), nx_paced=1, g_fiber=(0, 0), g_tissue=(0, 0), g_fiber_tissue=0, precision=myokit.DOUBLE_PRECISION, ) s1.set_step_size(0.01) # Set up logging logvars = ['engine.time', 'engine.pace', 'membrane.V', 'ica.ICa'] logt = myokit.LOG_NONE # Run simulation tmax = 100 dlog = 0.1 with myokit.PyCapture(): d1, logt = s1.run(tmax, logf=logvars, logt=logt, log_interval=dlog) del(logt) d1 = d1.npview() # Run CVODE simulation s2 = myokit.Simulation(m, p) s2.set_tolerance(1e-8, 1e-8) d2 = s2.run(tmax, logvars, log_interval=dlog).npview() # Check implementation of logging point selection e0 = np.max(np.abs(d1.time() - d2.time())) # Check implementation of pacing r1 = d1['engine.pace'] - d2['engine.pace'] e1 = np.sum(r1**2) # Check membrane potential (will have some error!) # Using MRMS from Marsh, Ziaratgahi, Spiteri 2012 r2 = d1['membrane.V', 0, 0] - d2['membrane.V'] r2 /= (1 + np.abs(d2['membrane.V'])) e2 = np.sqrt(np.sum(r2**2) / len(r2)) # Check logging of intermediary variables r3 = d1['ica.ICa', 0, 0] - d2['ica.ICa'] r3 /= (1 + np.abs(d2['ica.ICa'])) e3 = np.sqrt(np.sum(r3**2) / len(r3)) if debug: import matplotlib.pyplot as plt print('Event at t=0') print(d1.time()[:7]) print(d2.time()[:7]) print(d1.time()[-7:]) print(d2.time()[-7:]) print(e0) plt.figure() plt.suptitle('Pacing signals') plt.subplot(2, 1, 1) plt.plot(d1.time(), d1['engine.pace'], label='FiberTissue') plt.plot(d2.time(), d2['engine.pace'], label='CVODE') plt.legend() plt.subplot(2, 1, 2) plt.plot(d1.time(), r1) print(e1) plt.figure() plt.suptitle('Membrane potential') plt.subplot(2, 1, 1) plt.plot(d1.time(), d1['membrane.V', 0, 0], label='FiberTissue') plt.plot(d2.time(), d2['membrane.V'], label='CVODE') plt.legend() plt.subplot(2, 1, 2) plt.plot(d1.time(), r2) print(e2) plt.figure() plt.suptitle('Calcium current') plt.subplot(2, 1, 1) plt.plot(d1.time(), d1['ica.ICa', 0, 0], label='FiberTissue') plt.plot(d2.time(), d2['ica.ICa'], label='CVODE') plt.legend() plt.subplot(2, 1, 2) plt.plot(d1.time(), r2) print(e3) plt.show() self.assertLess(e0, 1e-10) self.assertLess(e1, 1e-14) self.assertLess(e2, 0.05) self.assertLess(e3, 0.01)