def QoI(params): from scipy.integrate import odeint as solver import numpy as np x0 = [500, 0, 0, 50, 100, 0, 0, 0, 0, 0, 0] func = lambda x, t: RHS(x, t, [1, 1, 1, 1, 1, 1]) t = np.linspace(0, 1, 100) sol = solver(func, x0, t) x0 = sol[-1, :] func = lambda x, t: RHS(x, t, params) t = np.linspace(1, 20, 1000) sol = solver(func, x0, t) return sol[-1, -1]
def solve_kinetics(self, time=10, min_resolution=np.inf): if self.prepared == False: self.prepare() self.tp = 0 def da(t, c): self.tp = t rates = [0 for i in self.reacts] for i in range(len(self.reacts)): rates[i] = rates[i] + sum( [eq(c, self.reacts_ord) for eq in self.diff_eq[i]]) return rates self.kinetic_solve = solver(da, (0, time), self.c0s, max_step=min_resolution) return self.kinetic_solve
def generate_model_data_vector(self, times, parameters=None): ''' Generate ideal data vector from cosmology parameters, nuisance parameters, and experimental parameters ''' w0 = parameters[0] # frequency of oscillator at midpoint wa = parameters[ 1] # change in frequency of oscillator with angle theta c = parameters[2] # drag coefficient A = parameters[3] # amplitude of driver wd = parameters[4] # frequency of driver phid = parameters[5] # phase of driver theta_x0 = parameters[6] # initial position of oscillator theta_v0 = parameters[7] # initial velocity of oscillator #theta = self.constant_theta_0 * # np.cos(np.sqrt(self.constant_g] / self.constant_l) * self.times) def forcing_function(t): ''' output amplitude as a function of time ''' return A * np.cos(wd * t + phid) def oscillator_eqns(t, y): ''' general equations of motion for an oscillator ''' x = y[0] u = y[1] xp = u weff = w0 - x * wa # pendulum gets lighter as we go up up = (forcing_function(t) - c * u - weff**2 * x) return np.array([xp, up]) # minimum and maximum times over which the solution should be calculated interval = (np.min(times), np.max(times)) # solving the oscillator equations of motion for the total interval solution = solver(oscillator_eqns, interval, np.array([parameters[-2], parameters[-1]]), t_eval=times) self.ideal_data_vector = solution.y[0] return solution.y[0]
def generate_oscillator_timeseries(pars, t0, y0, interval=None, t_obs=None): # Cosmological parameters k = pars[0] m = pars[1] # Systematics parameters c = pars[2] A = pars[3] wd = pars[4] phid = pars[5] def forcing_function(t): return A * np.cos(wd * t + phid) def oscillator_eqns(t, y): x = y[0] u = y[1] xp = u up = (forcing_function(t) - c * u - k * x) / m return np.array([xp, up]) solution = solver(oscillator_eqns, interval, y0, t_eval=t_obs) return solution