def randomlist(self,mode=1,size=1000,low=0,high=100): "generate a list followed given distribution" "IN:distribution model code which refer to randomlist method; sequence size; sequence range" "OUT:a sequence followed distribution like" x = [] if mode == 1: x = random.randint(low,high,size=size) if mode == 2: x = map(float,random.normal(loc=(low+high)/2.0, scale=(high-low)/6.0, size=size)) if mode == 3: x = map(long,random.exponential(scale=1, size=size)+low) if mode == 4: x = map(long,random.pareto(1,size=size)+low) if mode == 5: x = map(long,random.poisson(lam=(low+high)/2.0, size=size)) # x = random.choice(x,size=100) return x
def draw_sky_positions(self, size): """ Based on Wainscoat 1992 and Faucher-Giguere 2007. Code thanks to Mortiz Pleintinger. """ # Parameters for possun [-8.5,0,0], from Wainscoat 1992 k = np.array([4.25, 4.25, 4.89, 4.89]) r0 = np.array([3.48, 3.48, 4.90, 4.90]) theta0 = np.array([0.0, 3.141, 2.525, 5.666]) # Wainscoat 1992 height = 0.3 # kpc idx = np.random.randint(4, size=size) ks = k[idx] r0s = r0[idx] theta0s = theta0[idx] tet = ks * np.log(self._distances / r0s) + theta0s winklcor = np.random.uniform(0.0, 2 * np.pi, size=size) corrtet = winklcor * np.exp( -0.35 * self._distances) # Faucher-Giguere 2007 spiraltheta = tet + corrtet # Faucher-Giguere 2007 zpos = rd.exponential(height, size=size) zpos *= np.random.choice([-1, 1], size=size) self._distances = self._distances + np.random.normal( 0, scale=0.07 * np.abs(self._distances), size=size) phi = np.arccos(zpos / np.sqrt(self._distances**2 + zpos**2)) self._theta = spiraltheta self._phi = phi
def run_delayed_ssa(system): """ SSA with delays and custom event functions """ #vars used in the simulation time = 0 #unitless end_time = system['sim-time'] species = system['participants'] parameters = system['parameters'] events = system['events'] prop_funcs = {} exec_funcs = {} props = {} delays = {} last_exec_time = {} #return values time_array = [] species_array = [] #populate results array time_array = [time] row = [0]*len(species) species_names = [''] * len(species) #create species vars so that rate code can be executed i = 0 for name in species: species_names[i] = name exec( name + '=' + str(species[name]) ) row[i] = species[name] i += 1 species_array.append(row) #create parameter vars so that rate code can be executed for name in parameters: exec( name + '=' + str(parameters[name]) ) #create (compile) functions from input strings for rates and events for name in events: if events[name].get('delay'): delays[name] = events[name]['delay'] else: delays[name] = 0.0 last_exec_time[name] = -1 props[name] = 0.0 prop_funcs[name] = compile("props['" + name + "'] = " + str(events[name]['propensity']), 'prop_funcs_'+name, 'exec') exec_funcs[name] = compile(events[name]['consequence'], 'exec_funcs_'+name, 'exec') #MAIN LOOP while time < end_time: #calculate propensities for name in props: exec(prop_funcs[name]) if delays[name] > 0 and delays[name] + last_exec_time[name] < time: print(name) props[name] = 0.0 #calculate total of all propensities total_prop = 0 for name in props: total_prop += props[name] u = random.uniform(0,total_prop) usum = 0 lucky = None for name in props: usum += props[name] if usum > u: lucky = name break #fire that reaction if lucky: last_exec_time[lucky] = time exec(exec_funcs[lucky]) row = [0]*len(species) i = 0 for name in species: row[i] = eval(name) i += 1 time_array.append(time) species_array.append(row) #update next time using exp distrib if total_prop == 0.0: #jump to next delay lowest_delay = inf for name in props: if delays[name] > 0 and delays[name] < lowest_delay: lowest_delay = delays[name] time += lowest_delay else: dt = random.exponential(1.0/total_prop) time += dt #END MAIN LOOP result = {'time':time_array, 'participants':species_array, 'headers': species_names} return result
def gen_exponential(seedx, seedy): ''' Generate exponential coordinates ''' return random.exponential(seedx), random.exponential(seedy)