Skip to content
Snippets Groups Projects
Commit 8bb0ce46 authored by Stephan Seitz's avatar Stephan Seitz
Browse files

Throw error when trying to sympify `pystencils.Field` (e.g. using it in an...

Throw error when trying to sympify `pystencils.Field` (e.g. using it in an Assignment without indexing)
parent e512cf88
No related branches found
No related tags found
No related merge requests found
Pipeline #20782 failed
......@@ -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
......
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment