diff --git a/sumpy/kernel.py b/sumpy/kernel.py index 26f27adc800ac23fe1e64f27b740ac0f8ecd2466..039368916020ccfc4cb6f5a4f20bb92be62a5fa1 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -1030,12 +1030,15 @@ class AxisTargetDerivative(DerivativeBase): target_vec = make_sympy_vector(self.target_array_name, self.dim) + # bvec = tgt - ctr expr = self.inner_kernel.postprocess_at_target(expr, bvec) if isinstance(expr, DifferentiatedExprDerivativeTaker): transformation = diff_transformation(expr.derivative_transformation, self.axis, target_vec) return DifferentiatedExprDerivativeTaker(expr.taker, transformation) else: + # Since `bvec` and `tgt` are two different symbolic variables + # need to differentiate by both to get the correct answer return expr.diff(bvec[self.axis]) + expr.diff(target_vec[self.axis]) def replace_base_kernel(self, new_base_kernel): @@ -1129,6 +1132,8 @@ class DirectionalTargetDerivative(DirectionalDerivative): if not isinstance(expr, DifferentiatedExprDerivativeTaker): result = 0 for axis in range(self.dim): + # Since `bvec` and `tgt` are two different symbolic variables + # need to differentiate by both to get the correct answer result += (expr.diff(bvec[axis]) + expr.diff(target_vec[axis])) \ * dir_vec[axis] return result diff --git a/sumpy/tools.py b/sumpy/tools.py index df7080537c253509d998486ac8b8cb3e6e09f9eb..d37ef2aa8f98337febebf20320cd91aaec78f6fa 100644 --- a/sumpy/tools.py +++ b/sumpy/tools.py @@ -433,11 +433,19 @@ class DifferentiatedExprDerivativeTaker: def diff_transformation(derivative_transformation, variable_idx, variables): + """Differentiate a derivative transformation dictionary using the + variable given by **variable_idx** and return a new derivative transformation + dictionary + """ new_transformation = defaultdict(lambda: 0) for mi, coeff in derivative_transformation.items(): + # In the case where we have x * u.diff(x), the result should + # be x.diff(x) + x * u.diff(x, x) + # Calculate the first term by differentiating the coefficients new_coeff = sym.sympify(coeff).diff(variables[variable_idx]) if new_coeff != 0: new_transformation[mi] += new_coeff + # Next calculate the second term by differentitating the derivatives new_mi = list(mi) new_mi[variable_idx] += 1 new_transformation[tuple(new_mi)] += coeff