From f9405800775111131d817d8e9edf772489721917 Mon Sep 17 00:00:00 2001
From: mloubout <mathias.louboutin@gmail.com>
Date: Sat, 19 Oct 2024 22:46:48 -0400
Subject: [PATCH] api: enforce sympy shifts

---
 .github/workflows/pytest-core-nompi.yml |  2 +-
 devito/finite_differences/tools.py      |  6 +++---
 devito/symbolics/printer.py             | 13 +++++++++++++
 tests/test_tensors.py                   |  4 +++-
 4 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/pytest-core-nompi.yml b/.github/workflows/pytest-core-nompi.yml
index d30f1752e1..6e9df39498 100644
--- a/.github/workflows/pytest-core-nompi.yml
+++ b/.github/workflows/pytest-core-nompi.yml
@@ -149,7 +149,7 @@ jobs:
       run : |
           if [ "${{ runner.os }}" == 'macOS' ]; then
             brew install llvm libomp
-            echo "/opt/homebrew/bin:/opt/homebrew/opt/llvm/bin" >> $GITHUB_PATH
+            echo "/opt/homebrew/opt/llvm/bin" >> $GITHUB_PATH
           fi
       id: set-tests
     
diff --git a/devito/finite_differences/tools.py b/devito/finite_differences/tools.py
index 8a879923df..8cb6ac608b 100644
--- a/devito/finite_differences/tools.py
+++ b/devito/finite_differences/tools.py
@@ -2,7 +2,7 @@
 from itertools import product
 
 import numpy as np
-from sympy import S, finite_diff_weights, cacheit, sympify, Function
+from sympy import S, finite_diff_weights, cacheit, sympify, Function, Rational
 
 from devito.tools import Tag, as_tuple
 from devito.types.dimension import StencilDimension
@@ -308,8 +308,8 @@ def make_shift_x0(shift, ndim):
     """
     if shift is None:
         return lambda s, d, i, j: None
-    elif isinstance(shift, float):
-        return lambda s, d, i, j: d + s * d.spacing
+    elif sympify(shift).is_Number:
+        return lambda s, d, i, j: d + Rational(s) * d.spacing
     elif type(shift) is tuple and np.shape(shift) == ndim:
         if len(ndim) == 1:
             return lambda s, d, i, j: d + s[j] * d.spacing
diff --git a/devito/symbolics/printer.py b/devito/symbolics/printer.py
index 672971cf4f..f60feee443 100644
--- a/devito/symbolics/printer.py
+++ b/devito/symbolics/printer.py
@@ -7,6 +7,8 @@
 
 from mpmath.libmp import prec_to_dps, to_str
 from packaging.version import Version
+
+from sympy.codegen.ast import float32, float64
 from sympy.logic.boolalg import BooleanFunction
 from sympy.printing.precedence import PRECEDENCE_VALUES, precedence
 from sympy.printing.c import C99CodePrinter
@@ -18,6 +20,9 @@
 __all__ = ['ccode']
 
 
+_type_mapper = {np.float32: float32, np.float64: float64}
+
+
 class CodePrinter(C99CodePrinter):
 
     """
@@ -179,12 +184,20 @@ def _print_Add(self, expr, order=None):
 
     def _print_Float(self, expr):
         """Print a Float in C-like scientific notation."""
+        try:
+            # Make sure the float is in the correct format
+            expr = _type_mapper[self.dtype].cast_nocheck(expr)
+            rv = str(expr)
+        except KeyError:
+            pass
+
         prec = expr._prec
 
         if prec < 5:
             dps = 0
         else:
             dps = prec_to_dps(expr._prec)
+
         if self._settings["full_prec"] is True:
             strip = False
         elif self._settings["full_prec"] is False:
diff --git a/tests/test_tensors.py b/tests/test_tensors.py
index 15e18ababd..0202cae96d 100644
--- a/tests/test_tensors.py
+++ b/tests/test_tensors.py
@@ -1,5 +1,6 @@
 import numpy as np
 import sympy
+from sympy import Rational
 
 import pytest
 
@@ -372,7 +373,8 @@ def test_shifted_lap_of_vector(shift, ndim):
             assert dfvi == ref
 
 
-@pytest.mark.parametrize('shift, ndim', [(None, 2), (.5, 2), (.5, 3),
+@pytest.mark.parametrize('shift, ndim', [(None, 2), (Rational(1/2), 2),
+                                         (Rational(1/2), 3),
                                          (tuple([tuple([.5]*3)]*3), 3)])
 def test_shifted_lap_of_tensor(shift, ndim):
     grid = Grid(tuple([11]*ndim))