def smagorinsky(particle, fieldset, time): dx = 0.01 dudx = (fieldset.U[time, particle.depth, particle.lat, particle.lon + dx] - fieldset.U[time, particle.depth, particle.lat, particle.lon - dx]) / (2 * dx) dudy = (fieldset.U[time, particle.depth, particle.lat + dx, particle.lon] - fieldset.U[time, particle.depth, particle.lat - dx, particle.lon]) / (2 * dx) dvdx = (fieldset.V[time, particle.depth, particle.lat, particle.lon + dx] - fieldset.V[time, particle.depth, particle.lat, particle.lon - dx]) / (2 * dx) dvdy = (fieldset.V[time, particle.depth, particle.lat + dx, particle.lon] - fieldset.V[time, particle.depth, particle.lat - dx, particle.lon]) / (2 * dx) A = fieldset.cell_areas[time, 0, particle.lat, particle.lon] deg_to_m = (1852 * 60)**2 * math.cos(particle.lat * math.pi / 180) A = A / deg_to_m Vh = fieldset.Cs * A * math.sqrt(dudx**2 + 0.5 * (dudy + dvdx)**2 + dvdy**2) xres = 1 # [degrees] yres = 1 hitBoundary = True tries = 0 while hitBoundary and tries < 5: dlat = yres * random.normalvariate(0., 1.) * math.sqrt( 2 * math.fabs(particle.dt) * Vh) #/ dy_cell dlon = xres * random.normalvariate(0., 1.) * math.sqrt( 2 * math.fabs(particle.dt) * Vh) #/ dx_cell if (fieldset.U[time, particle.depth, particle.lat + dlat, particle.lon + dlon] > 0): if (fieldset.V[time, particle.depth, particle.lat + dlat, particle.lon + dlon] > 0): hitBoundary = False elif (fieldset.V[time, particle.depth, particle.lat + dlat, particle.lon + dlon] < 0): hitBoundary = False elif (fieldset.U[time, particle.depth, particle.lat + dlat, particle.lon + dlon] < 0): if (fieldset.V[time, particle.depth, particle.lat + dlat, particle.lon + dlon] > 0): hitBoundary = False elif (fieldset.V[time, particle.depth, particle.lat + dlat, particle.lon + dlon] < 0): hitBoundary = False tries += 1 if tries == 5: particle.beached = 1 dlat = 0 dlon = 0 particle.lat += dlat particle.lon += dlon
def sample(self): s = [None] * self.p s[0] = random.normalvariate(self.mu[0], self.sigma[0]) for i in range(1, self.p): pid = self.parents[i] assert s[pid] != None # XXX assumes that the features are in topological order shift_mu = self.mu[i] - (self.w[i] * self.mu[pid]) s[i] = random.normalvariate(shift_mu + (self.w[i] * s[pid]), self.sigma[i]) return s
def sample(self): s = [None] * self.p s[0] = random.normalvariate(self.mu[0], self.sigma[0]) for i in range(1, self.p): pid = self.parents[i] assert s[ pid] != None # XXX assumes that the features are in topological order shift_mu = self.mu[i] - (self.w[i] * self.mu[pid]) s[i] = random.normalvariate(shift_mu + (self.w[i] * s[pid]), self.sigma[i]) return s
def randProjectPts(pts, cluDim): d = len(pts[0]) r = 1.0 / float(len(pts[0])**.5) M = ([[random.normalvariate(0, 1) * r for a in range(cluDim)] for b in range(d)]) return mult(pts, M)
def default(particle, fieldset, time): """Smagorinski parametrization kernel. """ dx = 0.01; dudx = (fieldset.U[time, particle.depth, particle.lat, particle.lon+dx]-fieldset.U[time, particle.depth, particle.lat, particle.lon-dx]) / (2*dx) dudy = (fieldset.U[time, particle.depth, particle.lat+dx, particle.lon]-fieldset.U[time, particle.depth, particle.lat-dx, particle.lon]) / (2*dx) dvdx = (fieldset.V[time, particle.depth, particle.lat, particle.lon+dx]-fieldset.V[time, particle.depth, particle.lat, particle.lon-dx]) / (2*dx) dvdy = (fieldset.V[time, particle.depth, particle.lat+dx, particle.lon]-fieldset.V[time, particle.depth, particle.lat-dx, particle.lon]) / (2*dx) A = fieldset.cell_areas[time, 0, particle.lat, particle.lon] Vh = fieldset.Cs * A * math.sqrt(dudx**2 + 0.5*(dudy + dvdx)**2 + dvdy**2) xres = fieldset.resolutionx # [degrees] yres = fieldset.resolutiony dx_cell = fieldset.cell_edge_sizes_x[0, 0, particle.lat, particle.lon] # [meters] dy_cell = fieldset.cell_edge_sizes_y[0, 0, particle.lat, particle.lon] hitBoundary = True tries = 0 while hitBoundary and tries <15: dlat = yres * random.normalvariate(0,1) * math.sqrt(2*math.fabs(particle.dt)* Vh) / dy_cell dlon = xres * random.normalvariate(0,1) * math.sqrt(2*math.fabs(particle.dt)* Vh) / dx_cell if(fieldset.U[time, particle.depth, particle.lat+dlat, particle.lon+dlon] > 0): if(fieldset.V[time, particle.depth, particle.lat+dlat, particle.lon+dlon] > 0): hitBoundary = False elif(fieldset.V[time, particle.depth, particle.lat+dlat, particle.lon+dlon] < 0): hitBoundary = False elif(fieldset.U[time, particle.depth, particle.lat+dlat, particle.lon+dlon] < 0): if(fieldset.V[time, particle.depth, particle.lat+dlat, particle.lon+dlon] > 0): hitBoundary = False elif(fieldset.V[time, particle.depth, particle.lat+dlat, particle.lon+dlon] < 0): hitBoundary = False tries += 1 if tries == 15: dlat = 0 dlon = 0 particle.lat += dlat particle.lon += dlon
def test_upsample(): before_num = 500 after_num = 1000 xs = [random.normalvariate(0,1) for i in range(before_num)] ps = [1/float(before_num) for i in range(before_num)] states = zip(xs,ps) mu0 = sum([x*p for x,p in states]) sigma0 = sum([(x**2)*p for x,p in states]) new_states = upsample(states,after_num) mu1 = sum([x*p for x,p in new_states]) sigma1 = sum([(x**2)*p for x,p in new_states]) print mu0,sigma0 print mu1,sigma1
def randProjectPts(pts,cluDim): d = len(pts[0]) r = 1.0/float(len(pts[0])**.5) M= ([[random.normalvariate(0,1)*r for a in range(cluDim)] for b in range(d)]) return mult(pts,M)