From 8a9aef457fd26607fa2b39102e2f7aa22ffb9c33 Mon Sep 17 00:00:00 2001
From: Dominic Kempf <dominic.kempf@iwr.uni-heidelberg.de>
Date: Mon, 5 Feb 2018 08:50:40 +0100
Subject: [PATCH] Correctly preprocess gradients of coefficients

Needs monkey patching of the preprocessing to not
transfer to reference element...
---
 python/dune/perftool/ufl/preprocess.py | 16 ++++++++++++++++
 python/dune/perftool/ufl/visitor.py    |  5 ++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/python/dune/perftool/ufl/preprocess.py b/python/dune/perftool/ufl/preprocess.py
index 24d436c9..19ca1035 100644
--- a/python/dune/perftool/ufl/preprocess.py
+++ b/python/dune/perftool/ufl/preprocess.py
@@ -1,10 +1,26 @@
 """ Preprocessing algorithms for UFL forms """
 
 import ufl.classes as uc
+import ufl.algorithms.apply_function_pullbacks as afp
 
 from pytools import memoize
 
 
+class FunctionPullbackApplier(afp.FunctionPullbackApplier):
+    def argument(self, o):
+        return afp.apply_single_function_pullbacks(o)
+
+    def coefficient(self, o):
+        if o.count() in (0, 1):
+            return afp.apply_single_function_pullbacks(o)
+        else:
+            return o
+
+
+# Monkey patch the pullback applier from UFL
+afp.FunctionPullbackApplier = FunctionPullbackApplier
+
+
 @memoize
 def preprocess_form(form):
     from ufl.algorithms import compute_form_data
diff --git a/python/dune/perftool/ufl/visitor.py b/python/dune/perftool/ufl/visitor.py
index 687afd5c..071102ae 100644
--- a/python/dune/perftool/ufl/visitor.py
+++ b/python/dune/perftool/ufl/visitor.py
@@ -164,7 +164,10 @@ class UFL2LoopyVisitor(ModifiedTerminalTracker):
             # and exports it through a getter method 'getTime'
             return prim.Call(prim.Variable("getTime"), ())
         else:
-            return self.interface.pymbolic_gridfunction(o, restriction, self.reference_grad)
+            if self.reference_grad:
+                raise PerftoolUFLError("Coefficient gradients should not be transformed to reference element")
+
+            return self.interface.pymbolic_gridfunction(o, restriction, self.grad)
 
     #
     # Handlers for all indexing related stuff
-- 
GitLab