diff --git a/pystencils/field.py b/pystencils/field.py index 507be0c2cad932eed64419b70e1b55783cff4273..a8c82b47e2ddeea803c260b14f15a1bb5df30f9e 100644 --- a/pystencils/field.py +++ b/pystencils/field.py @@ -15,7 +15,7 @@ import pystencils from pystencils.alignedarray import aligned_empty from pystencils.data_types import StructType, TypedSymbol, create_type from pystencils.kernelparameters import FieldShapeSymbol, FieldStrideSymbol -from pystencils.stencil import direction_string_to_offset, offset_to_direction_string, inverse_direction +from pystencils.stencil import direction_string_to_offset, inverse_direction, offset_to_direction_string from pystencils.sympyextensions import is_integer_sequence __all__ = ['Field', 'fields', 'FieldType', 'AbstractField'] @@ -413,6 +413,12 @@ class Field(AbstractField): def __str__(self): return self.name + def __getattribute__(self, name): + if name == '__sympy__': + raise RuntimeError("pystencils.Field cannot by sympified!\n" + "Please use a proper indexing to access elements of the Field!") + return super().__getattribute__(name) + def neighbor(self, coord_id, offset): offset_list = [0] * self.spatial_dimensions offset_list[coord_id] = offset diff --git a/pystencils_tests/test_assignment_collection.py b/pystencils_tests/test_assignment_collection.py index 42e3791ab331cc111343bfe410bc59e3f52ba80b..cbe08763aac61cf9c66ed16766c9de716979e7ab 100644 --- a/pystencils_tests/test_assignment_collection.py +++ b/pystencils_tests/test_assignment_collection.py @@ -1,4 +1,6 @@ +import pytest import sympy as sp +import pystencils from pystencils import Assignment, AssignmentCollection from pystencils.astnodes import Conditional @@ -40,3 +42,15 @@ def test_free_and_defined_symbols(): print(ac) print(ac.__repr__) + + +def test_sympifying_fields(): + x, y = pystencils.fields('x, y: double[1d]') + # Apparently, we trigger our helpful error message only for Sympy>=1.5 + with pytest.raises((RuntimeError, sp.SympifyError)): + AssignmentCollection({ + x: y + }) + + with pytest.raises((RuntimeError, sp.SympifyError)): + sp.S(x)