def vorticity(u, v): """ Computes the vorticity field for a two-dimensional simulation. Parameters ---------- u, v: Field objects Velocity fields. Returns ------- vorticity: Field object The vorticity field. """ print('\tCompute the vorticity field ...') mask_x = numpy.where(numpy.logical_and(u.x > v.x[0], u.x < v.x[-1]))[0] mask_y = numpy.where(numpy.logical_and(v.y > u.y[0], v.y < u.y[-1]))[0] # vorticity nodes at cell vertices intersection xw, yw = 0.5 * (v.x[:-1] + v.x[1:]), 0.5 * (u.y[:-1] + u.y[1:]) # compute vorticity w = ((v.values[mask_y, 1:] - v.values[mask_y, :-1]) / numpy.outer(numpy.ones(yw.size), v.x[1:] - v.x[:-1]) - (u.values[1:, mask_x] - u.values[:-1, mask_x]) / numpy.outer(u.y[1:] - u.y[:-1], numpy.ones(xw.size))) return ioPetIBM.Field(x=xw, y=yw, values=w)
def restriction(fine, coarse): """Restriction of the solution from a fine grid onto a coarse grid. Parameters ---------- fine, coarse: ioPetIBM.Field Fine and coarse numerical solutions. Returns ------- fine_on_coarse: ioPetIBM.Field The solution on the fine grid restricted to the coarse grid. """ def intersection(a, b, tolerance=1.0E-06): return numpy.any(numpy.abs(a - b[:, numpy.newaxis]) <= tolerance, axis=0) mask_x = intersection(fine.x, coarse.x) mask_y = intersection(fine.y, coarse.y) fine_on_coarse = ioPetIBM.Field(x=fine.x[mask_x], y=fine.y[mask_y], values=numpy.array([ fine.values[j][mask_x] for j in xrange(fine.y.size) if mask_y[j] ])) assert numpy.allclose(coarse.x, fine_on_coarse.x, rtol=1.0E-04) assert numpy.allclose(coarse.y, fine_on_coarse.y, rtol=1.0E-04) assert coarse.values.shape == fine_on_coarse.values.shape return fine_on_coarse
def interpolate_cell_centers(velocity): """Interpolates the velocity field at the cell-centers. Parameters ---------- velocity: list(ioPetIBM.Field) Velocity field on a staggered grid. Returns ------- velocity: list(ioPetIBM.Field) Velocity field at cell-centers. """ dim3 = (True if len(velocity) == 3 else False) x_centers, y_centers = velocity[1].x[1:-1], velocity[0].y[1:-1] u, v = velocity[0].values, velocity[1].values if dim3: z_centers = velocity[0].z[1:-1] w = velocity[2].values u = 0.5 * (u[1:-1, 1:-1, :-1] + u[1:-1, 1:-1, 1:]) v = 0.5 * (v[1:-1, :-1, 1:-1] + v[1:-1:, 1:, 1:-1]) w = 0.5 * (w[:-1, 1:-1, 1:-1] + w[1:, 1:-1, 1:-1]) # tests assert (z_centers.size, y_centers.size, x_centers.size) == u.shape assert (z_centers.size, y_centers.size, x_centers.size) == v.shape assert (z_centers.size, y_centers.size, x_centers.size) == w.shape return [ ioPetIBM.Field(x=x_centers, y=y_centers, z=z_centers, values=u), ioPetIBM.Field(x=x_centers, y=y_centers, z=z_centers, values=v), ioPetIBM.Field(x=x_centers, y=y_centers, z=z_centers, values=w) ] else: u = 0.5 * (u[1:-1, :-1] + u[1:-1, 1:]) v = 0.5 * (v[:-1, 1:-1] + v[1:, 1:-1]) # tests assert (y_centers.size, x_centers.size) == u.shape assert (y_centers.size, x_centers.size) == v.shape return [ ioPetIBM.Field(x=x_centers, y=y_centers, values=u), ioPetIBM.Field(x=x_centers, y=y_centers, values=v) ]