From f4ba544880be165ffb785a1a2c784a2356bcd95e Mon Sep 17 00:00:00 2001
From: Stephan Seitz <stephan.seitz@fau.de>
Date: Tue, 14 Jul 2020 20:13:37 +0200
Subject: [PATCH] Fix #23: Make Field.Access NotIterable

---
 pystencils/field.py                        |  6 +----
 pystencils_tests/test_field_access_poly.py | 29 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)
 create mode 100644 pystencils_tests/test_field_access_poly.py

diff --git a/pystencils/field.py b/pystencils/field.py
index 9fe59d1f4..bdff32343 100644
--- a/pystencils/field.py
+++ b/pystencils/field.py
@@ -676,6 +676,7 @@ class Field(AbstractField):
             >>> central_y_component.at_index(0)  # change component
             v_C^0
         """
+        _iterable = False  # see https://i10git.cs.fau.de/pycodegen/pystencils/-/merge_requests/166#note_10680
 
         def __new__(cls, name, *args, **kwargs):
             obj = Field.Access.__xnew_cached_(cls, name, *args, **kwargs)
@@ -756,11 +757,6 @@ class Field(AbstractField):
         def __getitem__(self, *idx):
             return self.__call__(*idx)
 
-        def __iter__(self):
-            """This is necessary to work with parts of sympy that test if an object is iterable (e.g. simplify).
-            The __getitem__ would make it iterable"""
-            raise TypeError("Field access is not iterable")
-
         @property
         def field(self) -> 'Field':
             """Field that the Access points to"""
diff --git a/pystencils_tests/test_field_access_poly.py b/pystencils_tests/test_field_access_poly.py
new file mode 100644
index 000000000..befeca9bd
--- /dev/null
+++ b/pystencils_tests/test_field_access_poly.py
@@ -0,0 +1,29 @@
+#
+# Copyright © 2020 Stephan Seitz <stephan.seitz@fau.de>
+#
+# Distributed under terms of the GPLv3 license.
+
+"""
+
+"""
+
+
+from pystencils.session import *
+
+from sympy import poly
+
+
+def test_field_access_poly():
+    dh = ps.create_data_handling((20, 20))
+    ρ = dh.add_array('rho')
+    rho = ρ.center
+    a = poly(rho+0.5, rho)
+    print(a)
+
+
+def test_field_access_piecewise():
+    dh = ps.create_data_handling((20, 20))
+    ρ = dh.add_array('rho')
+    pw = sp.Piecewise((0, 1 < sp.Max(-0.5, ρ.center+0.5)), (1, True))
+    a = sp.simplify(pw)
+    print(a)
-- 
GitLab