def vector(dx, dt, tau, I0, r02, k_sc, k_abs): nx = 2 ** dx # number of points in space nt = 2 ** dt # number of points in time bx = 1 # [0, b] - interval hx = bx / (nx - 1) # step bt = 1 # [0, b] - interval ht = bt / (nt - 1) # step x = hx * tt.xfun(2, dx) # coordinates generation ex = tt.ones(2, dx) t = ht * tt.xfun(2, dt) # coordinates generation et = tt.ones(2, dt) X = tt.mkron(x, ex, ex, et) Y = tt.mkron(ex, x, ex, et) Z = tt.mkron(ex, ex, x, et) T = tt.mkron(ex, ex, ex, t) Qs = tt.multifuncrs([X, Y, Z, T], lambda arg: I0 * (arg[:, 3]/tau *np.exp(-arg[:, 3]/tau)) * (np.exp( -(arg[:,1]**2 - arg[:,2]**2) / r02*np.exp(k_sc*arg[:,0]) ) * np.exp(-(k_sc + k_abs) * arg[:, 0])) , 1e-6, verb = True) Qs = Qs.round(1e-6) return Qs
def __call__(self,observation): noise_model = lambda x,y,s : 1/(y*s*np.sqrt(2*np.pi)) * np.exp(-(np.log(y)-np.log(x+1))**2/(2*s**2)) lst = [ tt.tensor(noise_model(np.arange(self.N[i]),observation[i],self.sigmas[i])) if self.observation_vector[i] else tt.ones([self.N[i]]) for i in range(len(self.N))] tens = tt.mkron(lst) return tens
def __call__(self,observation): lst = [ tt.tensor(np.exp(-0.5*(observation[i]-np.arange(self.N[i]))**2/self.sigmas[i]**2)/(self.sigmas[i]*np.sqrt(2*np.pi))) if self.observation_vector[i] else tt.ones([self.N[i]]) for i in range(len(self.N))] lst = [ tt.tensor(np.exp(-0.5*(observation[i]-np.arange(self.N[i]))**2/self.sigmas[i]**2)) if self.observation_vector[i] else tt.ones([self.N[i]]) for i in range(len(self.N))] tens = tt.mkron(lst) return tens
def vector(dx, dt, tau, I0, r02, k_sc, k_abs, rho, Cv, T0): nx = 2 ** dx # number of points in space nt = 2 ** dt # number of points in time bx = 1 # [0, b] - interval hx = bx / (nx - 1) # step bt = 1 # [0, b] - interval ht = bt / (nt - 1) # step x = hx * tt.xfun(2, dx) # coordinates generation ex = tt.ones(2, dx) t = ht * tt.xfun(2, dt) # coordinates generation et = tt.ones(2, dt) t0 = (T0 - 1)*tt.delta(2, dx, center = 0) # initial conditions for spatial t0 = tt.mkron(ex, ex, ex, et) + tt.mkron(t0, t0, t0, et) X = tt.mkron(x, ex, ex, et) Y = tt.mkron(ex, x, ex, et) Z = tt.mkron(ex, ex, x, et) T = tt.mkron(ex, ex, ex, t) Qs = tt.multifuncrs([X, Y, Z, T], lambda arg: I0 * (arg[:, 3]/tau *np.exp(-arg[:, 3]/tau)) * (np.exp( -(arg[:,1]**2 - arg[:,2]**2) / r02*np.exp(k_sc*arg[:,0]) ) * np.exp(-(k_sc + k_abs) * arg[:, 0])) , 1e-14, verb = True) Qs = Qs + t0 Qs = Qs.round(1e-14) return 1/(rho*Cv) * Qs
def goal_function(thetuta): L1 = np.array([lagrange(thetuta[0], i, pts1) for i in range(pts1.size)]) L2 = np.array([lagrange(thetuta[1], i, pts2) for i in range(pts2.size)]) L3 = np.array([lagrange(thetuta[2], i, pts3) for i in range(pts3.size)]) L4 = np.array([lagrange(thetuta[3], i, pts4) for i in range(pts4.size)]) L5 = np.array([lagrange(thetuta[4], i, pts5) for i in range(pts5.size)]) val = tt.dot( Post, tt.mkron(tt.tensor(L1.flatten()), tt.tensor(L2.flatten()), tt.tensor(L3.flatten()), tt.tensor(L4.flatten()), tt.tensor(L5.flatten()))) return -val
def findif(d, mode=MODE_NP, tau=None, h=1., name=DEF_MATRIX_NAME): ''' Finite difference 1D matrix [i, j] = 1/h if i=j, [i, j] =-1/h if i=j+1 and = 0 otherwise. ''' res = Matrix(None, d, mode, tau, False, name) if mode == MODE_NP or mode == MODE_SP: res.x = sp_diag([np.ones(res.n), -np.ones(res.n - 1)], [0, -1], format='csr') res.x *= 1. / h if mode == MODE_NP: res.x = res.x.toarray() if mode == MODE_TT: e1 = tt.tensor(np.array([0., 1.])) e2 = tt.mkron([e1] * d) res.x = tt.Toeplitz(-e2, kind='U') + tt.eye(2, d) res.x *= (1. / h) res = res.round() return res
# Set up model mdl = CME(N, Pre, Post, rates * 0 + 1, Props) Atts = mdl.construct_generator_tt(as_list=True) Nl = 64 mult = 6 param_range = [[0, r * mult] for r in rates] # basis = [LegendreBasis(Nl,[p[0],p[1]]) for p in param_range] basis = [BSplineBasis(Nl, [p[0], p[1]], deg=2) for p in param_range] pts = [b.integration_points(4)[0] for b in basis] ws = [b.integration_points(4)[1] for b in basis] lint = pts[0].size WS = tt.mkron([tt.tensor(b.get_integral()) for b in basis]) A_tt = extend_cme(Atts, pts) A_tt = A_tt.round(1e-10, 20) mass_tt, mass_inv_tt = get_mass(basis) stiff_tt = get_stiff(A_tt, N, pts, ws, basis) M_tt = tt.kron(tt.eye(N), mass_inv_tt) @ stiff_tt #%% Get observation np.random.seed(34548) # reaction_time,reaction_jumps,reaction_indices = Gillespie(np.array(Initial),time_observation[-1],Pre,Post-Pre,rates) # observations = Observations_grid(time_observation, reaction_time, reaction_jumps) # observations_noise = observations+np.random.normal(0,sigma,observations.shape)
def tt_meshgrid(xs): lst = [] for i in range(len(xs)): lst.append(tt.mkron([tt.tensor(xs[i]) if i==k else tt.ones([xs[k].size]) for k in range(len(xs)) ])) return lst