def project(vx, vy, occlusion): """Project the velocity field to be approximately mass-conserving, using a few iterations of Gauss-Seidel.""" p = np.zeros(vx.shape) div = -0.5 * (np.roll(vx, -1, axis=1) - np.roll(vx, 1, axis=1) + np.roll(vy, -1, axis=0) - np.roll(vy, 1, axis=0)) div = make_continuous(div, occlusion) for k in range(50): p = (div + np.roll(p, 1, axis=1) + np.roll(p, -1, axis=1) + np.roll(p, 1, axis=0) + np.roll(p, -1, axis=0))/4.0 p = make_continuous(p, occlusion) vx = vx - 0.5*(np.roll(p, -1, axis=1) - np.roll(p, 1, axis=1)) vy = vy - 0.5*(np.roll(p, -1, axis=0) - np.roll(p, 1, axis=0)) vx = occlude(vx, occlusion) vy = occlude(vy, occlusion) return vx, vy
def make_continuous(f, occlusion): non_occluded = 1 - occlusion num = np.roll(f, 1, axis=0) * np.roll(non_occluded, 1, axis=0)\ + np.roll(f, -1, axis=0) * np.roll(non_occluded, -1, axis=0)\ + np.roll(f, 1, axis=1) * np.roll(non_occluded, 1, axis=1)\ + np.roll(f, -1, axis=1) * np.roll(non_occluded, -1, axis=1) den = np.roll(non_occluded, 1, axis=0)\ + np.roll(non_occluded, -1, axis=0)\ + np.roll(non_occluded, 1, axis=1)\ + np.roll(non_occluded, -1, axis=1) return f * non_occluded + (1 - non_occluded) * num / ( den + 0.001)
def project(vx, vy): """Project the velocity field to be approximately mass-conserving, using a few iterations of Gauss-Seidel.""" p = np.zeros(vx.shape) h = 1.0/vx.shape[0] div = -0.5 * h * (np.roll(vx, -1, axis=0) - np.roll(vx, 1, axis=0) + np.roll(vy, -1, axis=1) - np.roll(vy, 1, axis=1)) for k in range(10): p = (div + np.roll(p, 1, axis=0) + np.roll(p, -1, axis=0) + np.roll(p, 1, axis=1) + np.roll(p, -1, axis=1))/4.0 vx -= 0.5*(np.roll(p, -1, axis=0) - np.roll(p, 1, axis=0))/h vy -= 0.5*(np.roll(p, -1, axis=1) - np.roll(p, 1, axis=1))/h return vx, vy