def _interpolator(V, dat, expr, subset): to_element = create_element(V.ufl_element(), vector_is_mixed=False) to_pts = [] if V.ufl_element().mapping() != "identity": raise NotImplementedError("Can only interpolate onto elements " "with affine mapping. Try projecting instead") for dual in to_element.dual_basis(): if not isinstance(dual, FIAT.functional.PointEvaluation): raise NotImplementedError("Can only interpolate onto point " "evaluation operators. Try projecting instead") to_pts.append(list(iterkeys(dual.pt_dict))[0]) if len(expr.ufl_shape) != len(V.ufl_element().value_shape()): raise RuntimeError('Rank mismatch: Expression rank %d, FunctionSpace rank %d' % (len(expr.ufl_shape), len(V.ufl_element().value_shape()))) if expr.ufl_shape != V.ufl_element().value_shape(): raise RuntimeError('Shape mismatch: Expression shape %r, FunctionSpace shape %r' % (expr.ufl_shape, V.ufl_element().value_shape())) mesh = V.ufl_domain() coords = mesh.coordinates if not isinstance(expr, (firedrake.Expression, SubExpression)): if expr.ufl_domain() and expr.ufl_domain() != V.mesh(): raise NotImplementedError("Interpolation onto another mesh not supported.") if expr.ufl_shape != V.shape: raise ValueError("UFL expression has incorrect shape for interpolation.") ast, oriented, coefficients = compile_ufl_kernel(expr, to_pts, coords) kernel = op2.Kernel(ast, ast.name) indexed = True elif hasattr(expr, "eval"): kernel, oriented, coefficients = compile_python_kernel(expr, to_pts, to_element, V, coords) indexed = False elif expr.code is not None: kernel, oriented, coefficients = compile_c_kernel(expr, to_pts, to_element, V, coords) indexed = True else: raise RuntimeError("Attempting to evaluate an Expression which has no value.") cell_set = coords.cell_set if subset is not None: assert subset.superset == cell_set cell_set = subset args = [kernel, cell_set] if indexed: args.append(dat(op2.WRITE, V.cell_node_map()[op2.i[0]])) else: args.append(dat(op2.WRITE, V.cell_node_map())) if oriented: co = mesh.cell_orientations() args.append(co.dat(op2.READ, co.cell_node_map())) for coefficient in coefficients: args.append(coefficient.dat(op2.READ, coefficient.cell_node_map())) return partial(op2.par_loop, *args)
def _interpolator(V, dat, expr, subset): to_element = V.fiat_element to_pts = [] if V.ufl_element().mapping() != "identity": raise NotImplementedError("Can only interpolate onto elements " "with affine mapping. Try projecting instead") for dual in to_element.dual_basis(): if not isinstance(dual, FIAT.functional.PointEvaluation): raise NotImplementedError("Can only interpolate onto point " "evaluation operators. Try projecting instead") to_pts.append(dual.pt_dict.keys()[0]) if len(expr.ufl_shape) != len(V.ufl_element().value_shape()): raise RuntimeError('Rank mismatch: Expression rank %d, FunctionSpace rank %d' % (len(expr.ufl_shape), len(V.ufl_element().value_shape()))) if expr.ufl_shape != V.ufl_element().value_shape(): raise RuntimeError('Shape mismatch: Expression shape %r, FunctionSpace shape %r' % (expr.ufl_shape, V.ufl_element().value_shape())) mesh = V.ufl_domain() coords = mesh.coordinates if not isinstance(expr, (firedrake.Expression, SubExpression)): if expr.ufl_domain() and expr.ufl_domain() != V.mesh(): raise NotImplementedError("Interpolation onto another mesh not supported.") if expr.ufl_shape != V.shape: raise ValueError("UFL expression has incorrect shape for interpolation.") ast, oriented, coefficients = compile_ufl_kernel(expr, to_pts, coords) kernel = op2.Kernel(ast, ast.name) indexed = True elif hasattr(expr, "eval"): kernel, oriented, coefficients = compile_python_kernel(expr, to_pts, to_element, V, coords) indexed = False elif expr.code is not None: kernel, oriented, coefficients = compile_c_kernel(expr, to_pts, to_element, V, coords) indexed = True else: raise RuntimeError("Attempting to evaluate an Expression which has no value.") cell_set = coords.cell_set if subset is not None: assert subset.superset == cell_set cell_set = subset args = [kernel, cell_set] if indexed: args.append(dat(op2.WRITE, V.cell_node_map()[op2.i[0]])) else: args.append(dat(op2.WRITE, V.cell_node_map())) if oriented: co = mesh.cell_orientations() args.append(co.dat(op2.READ, co.cell_node_map())) for coefficient in coefficients: args.append(coefficient.dat(op2.READ, coefficient.cell_node_map())) return partial(op2.par_loop, *args)
def _interpolator(V, dat, expr, subset, access): to_element = create_element(V.ufl_element(), vector_is_mixed=False) to_pts = [] if access is op2.READ: raise ValueError("Can't have READ access for output function") if V.ufl_element().mapping() != "identity": raise NotImplementedError("Can only interpolate onto elements " "with affine mapping. Try projecting instead") for dual in to_element.dual_basis(): if not isinstance(dual, FIAT.functional.PointEvaluation): raise NotImplementedError("Can only interpolate onto point " "evaluation operators. Try projecting instead") pts, = dual.pt_dict.keys() to_pts.append(pts) if len(expr.ufl_shape) != len(V.ufl_element().value_shape()): raise RuntimeError('Rank mismatch: Expression rank %d, FunctionSpace rank %d' % (len(expr.ufl_shape), len(V.ufl_element().value_shape()))) if expr.ufl_shape != V.ufl_element().value_shape(): raise RuntimeError('Shape mismatch: Expression shape %r, FunctionSpace shape %r' % (expr.ufl_shape, V.ufl_element().value_shape())) mesh = V.ufl_domain() coords = mesh.coordinates if not isinstance(expr, firedrake.Expression): if expr.ufl_domain() and expr.ufl_domain() != V.mesh(): raise NotImplementedError("Interpolation onto another mesh not supported.") if expr.ufl_shape != V.shape: raise ValueError("UFL expression has incorrect shape for interpolation.") ast, oriented, needs_cell_sizes, coefficients, _ = compile_ufl_kernel(expr, to_pts, coords, coffee=False) kernel = op2.Kernel(ast, ast.name) elif hasattr(expr, "eval"): kernel, oriented, needs_cell_sizes, coefficients = compile_python_kernel(expr, to_pts, to_element, V, coords) else: raise RuntimeError("Attempting to evaluate an Expression which has no value.") cell_set = coords.cell_set if subset is not None: assert subset.superset == cell_set cell_set = subset args = [kernel, cell_set] if dat in set((c.dat for c in coefficients)): output = dat dat = op2.Dat(dat.dataset) if access is not op2.WRITE: copyin = (partial(output.copy, dat), ) else: copyin = () copyout = (partial(dat.copy, output), ) else: copyin = () copyout = () args.append(dat(access, V.cell_node_map())) if oriented: co = mesh.cell_orientations() args.append(co.dat(op2.READ, co.cell_node_map())) if needs_cell_sizes: cs = mesh.cell_sizes args.append(cs.dat(op2.READ, cs.cell_node_map())) for coefficient in coefficients: m_ = coefficient.cell_node_map() args.append(coefficient.dat(op2.READ, m_)) for o in coefficients: domain = o.ufl_domain() if domain is not None and domain.topology != mesh.topology: raise NotImplementedError("Interpolation onto another mesh not supported.") return copyin + (op2.ParLoop(*args).compute, ) + copyout
def __init__(self, expr, V, overwrite_pts=None): assert isinstance(expr, ufl.classes.Expr) if isinstance(V, themis.Function): f = V V = f.function_space() else: f = themis.Function(V) # Make sure we have an expression of the right length i.e. a value for # each component in the value shape of each function space dims = [np.prod(fs.ufl_element().value_shape(), dtype=int) for fs in V] if np.prod(expr.ufl_shape, dtype=int) != sum(dims): raise RuntimeError( 'Expression of length %d required, got length %d' % (sum(dims), np.prod(expr.ufl_shape, dtype=int))) if len(V) > 1: raise NotImplementedError( "UFL expressions for mixed functions are not yet supported.") if len(expr.ufl_shape) != len(V.ufl_element().value_shape()): raise RuntimeError( 'Rank mismatch: Expression rank %d, FunctionSpace rank %d' % (len(expr.ufl_shape), len(V.ufl_element().value_shape()))) if expr.ufl_shape != V.ufl_element().value_shape(): raise RuntimeError( 'Shape mismatch: Expression shape %r, FunctionSpace shape %r' % (expr.ufl_shape, V.ufl_element().value_shape())) mesh = V.ufl_domain() to_element = V.themis_element() if overwrite_pts is None: ptsx = np.array(to_element.sub_elements[0][0].spts) ptsy = np.array(to_element.sub_elements[0][1].spts) ptsz = np.array(to_element.sub_elements[0][2].spts) else: ptsx, ptsy, ptsz = overwrite_pts to_pts = [] for lx in range(ptsx.shape[0]): for ly in range(ptsy.shape[0]): for lz in range(ptsz.shape[0]): if mesh.ndim == 1: loc = np.array([ ptsx[lx], ]) if mesh.ndim == 2: loc = np.array([ptsx[lx], ptsy[ly]]) if mesh.ndim == 3: loc = np.array([ptsx[lx], ptsy[ly], ptsz[lz]]) to_pts.append(loc) ast, oriented, needs_cell_sizes, coefficients, tabulations = compile_ufl_kernel( expr, to_pts, mesh.coordinates, interface=kernel_interface.ExpressionKernelBuilder) # process tabulations processed_tabulations = [] for tabname, shape in tabulations: splittab = tabname.split('_') tabobj = {} tabobj['name'] = tabname # this matches the string generated in runtime_tabulated.py in FInAT # ie variant_order_derivorder_shiftaxis_{d,c}_restrict tabobj['variant'] = splittab[1] tabobj['order'] = int(splittab[2]) tabobj['derivorder'] = int(splittab[3]) tabobj['shiftaxis'] = int(splittab[4]) if splittab[5] == 'd': tabobj['cont'] = 'L2' if splittab[5] == 'c': tabobj['cont'] = 'H1' tabobj['restrict'] = splittab[6] tabobj['shape'] = shape processed_tabulations.append(tabobj) # create InterpolationKernel self.kernel = InterpolationKernel(mesh, to_element, to_pts, processed_tabulations, coefficients, ast, V) self.field = f
def _interpolator(V, dat, expr, subset, access): to_element = create_element(V.ufl_element(), vector_is_mixed=False) to_pts = [] if access is op2.READ: raise ValueError("Can't have READ access for output function") if V.ufl_element().mapping() != "identity": raise NotImplementedError("Can only interpolate onto elements " "with affine mapping. Try projecting instead") for dual in to_element.dual_basis(): if not isinstance(dual, FIAT.functional.PointEvaluation): raise NotImplementedError("Can only interpolate onto point " "evaluation operators. Try projecting instead") pts, = dual.pt_dict.keys() to_pts.append(pts) if len(expr.ufl_shape) != len(V.ufl_element().value_shape()): raise RuntimeError('Rank mismatch: Expression rank %d, FunctionSpace rank %d' % (len(expr.ufl_shape), len(V.ufl_element().value_shape()))) if expr.ufl_shape != V.ufl_element().value_shape(): raise RuntimeError('Shape mismatch: Expression shape %r, FunctionSpace shape %r' % (expr.ufl_shape, V.ufl_element().value_shape())) mesh = V.ufl_domain() coords = mesh.coordinates if not isinstance(expr, firedrake.Expression): if expr.ufl_domain() and expr.ufl_domain() != V.mesh(): raise NotImplementedError("Interpolation onto another mesh not supported.") if expr.ufl_shape != V.shape: raise ValueError("UFL expression has incorrect shape for interpolation.") ast, oriented, needs_cell_sizes, coefficients, _ = compile_ufl_kernel(expr, to_pts, coords, coffee=False) kernel = op2.Kernel(ast, ast.name) elif hasattr(expr, "eval"): kernel, oriented, needs_cell_sizes, coefficients = compile_python_kernel(expr, to_pts, to_element, V, coords) else: raise RuntimeError("Attempting to evaluate an Expression which has no value.") cell_set = coords.cell_set if subset is not None: assert subset.superset == cell_set cell_set = subset args = [kernel, cell_set] if dat in set((c.dat for c in coefficients)): output = dat dat = op2.Dat(dat.dataset) if access is not op2.WRITE: copyin = (partial(output.copy, dat), ) else: copyin = () copyout = (partial(dat.copy, output), ) else: copyin = () copyout = () args.append(dat(access, V.cell_node_map())) if oriented: co = mesh.cell_orientations() args.append(co.dat(op2.READ, co.cell_node_map())) if needs_cell_sizes: cs = mesh.cell_sizes args.append(cs.dat(op2.READ, cs.cell_node_map())) for coefficient in coefficients: m_ = coefficient.cell_node_map() args.append(coefficient.dat(op2.READ, m_)) for o in coefficients: domain = o.ufl_domain() if domain is not None and domain.topology != mesh.topology: raise NotImplementedError("Interpolation onto another mesh not supported.") return copyin + (partial(op2.par_loop, *args), ) + copyout