Skip to content

Commit

Permalink
Merge pull request #1549 from devitocodes/sympy17
Browse files Browse the repository at this point in the history
sympy: Support v1.8
  • Loading branch information
FabioLuporini authored Apr 23, 2021
2 parents 26ed700 + 5c72e32 commit a8a33dc
Show file tree
Hide file tree
Showing 47 changed files with 1,277 additions and 756 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/pytest-core-nompi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
pytest-ubuntu-py36-gcc49-omp,
pytest-ubuntu-py37-gcc5-omp,
pytest-ubuntu-py38-gcc6-omp,
pytest-ubuntu-py36-gcc7-omp,
pytest-ubuntu-py36-gcc7-omp-sympy17,
pytest-ubuntu-py37-gcc7-noomp,
pytest-ubuntu-py37-gcc8-omp,
pytest-ubuntu-py38-gcc9-omp,
Expand All @@ -57,7 +57,7 @@ jobs:
arch: "gcc-6"
language: "openmp"

- name: pytest-ubuntu-py36-gcc7-omp
- name: pytest-ubuntu-py36-gcc7-omp-sympy17
python-version: 3.6
os: ubuntu-18.04
arch: "gcc-7"
Expand Down Expand Up @@ -147,6 +147,10 @@ jobs:
pip install --upgrade pip
pip install -e .
- name: Alternative sympy version
if: matrix.name == 'pytest-ubuntu-py36-gcc7-omp-sympy17'
run: pip install sympy==1.7

- name: Test with pytest
run: |
${{ steps.set-run.outputs.RUN_CMD }} pytest -k "${{ matrix.test-set }}" -m "not parallel" --cov --cov-config=.coveragerc --cov-report=xml ${{ steps.set-tests.outputs.TESTS }}
Expand Down
1 change: 1 addition & 0 deletions devito/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from devito.types.sparse import * # noqa
from devito.types.tensor import * # noqa
from devito.finite_differences import * # noqa
from devito.operations.solve import *
from devito.operator import Operator # noqa

# Other stuff exposed to the user
Expand Down
6 changes: 3 additions & 3 deletions devito/builtins/arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
from sympy import Abs, Pow

import devito as dv
from devito.builtins.utils import MPIReduction
Expand All @@ -20,6 +19,7 @@ def norm(f, order=2):
order : int, optional
The order of the norm. Defaults to 2.
"""
Pow = dv.finite_differences.differentiable.Pow
kwargs = {}
if f.is_TimeFunction and f._time_buffering:
kwargs[f.time_dim.max_name] = f._time_size - 1
Expand All @@ -33,11 +33,11 @@ def norm(f, order=2):
with MPIReduction(f) as mr:
op = dv.Operator([dv.Eq(s, 0.0)] +
eqns +
[dv.Inc(s, Abs(Pow(p, order))), dv.Eq(mr.n[0], s)],
[dv.Inc(s, dv.Abs(Pow(p, order))), dv.Eq(mr.n[0], s)],
name='norm%d' % order)
op.apply(**kwargs)

v = Pow(mr.v, 1/order)
v = np.power(mr.v, 1/order)

return f.dtype(v)

Expand Down
1 change: 1 addition & 0 deletions devito/finite_differences/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .differentiable import * # noqa
from .elementary import * # noqa
from .finite_difference import * # noqa
from .derivative import * # noqa
from .tools import generate_fd_shortcuts # noqa
Expand Down
9 changes: 5 additions & 4 deletions devito/finite_differences/derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from devito.finite_differences.finite_difference import (generic_derivative,
first_derivative,
cross_derivative)
from devito.finite_differences.differentiable import Differentiable, EvalDiffDerivative
from devito.finite_differences.differentiable import Differentiable, EvalDerivative
from devito.finite_differences.tools import direct, transpose
from devito.tools import as_mapper, as_tuple, filter_ordered, frozendict
from devito.types.utils import DimensionTuple
Expand Down Expand Up @@ -195,10 +195,10 @@ def __call__(self, x0=None, fd_order=None, side=None):
return self._new_from_self(fd_order=_fd_order, x0=_x0)

def _new_from_self(self, **kwargs):
expr = kwargs.pop('expr', self.expr)
_kwargs = {'deriv_order': self.deriv_order, 'fd_order': self.fd_order,
'side': self.side, 'transpose': self.transpose, 'subs': self._subs,
'x0': self.x0, 'preprocessed': True}
expr = kwargs.pop('expr', self.expr)
_kwargs.update(**kwargs)
return Derivative(expr, *self.dims, **_kwargs)

Expand All @@ -214,6 +214,7 @@ def subs(self, *args, **kwargs):
rules = dict(*args)
except TypeError:
rules = dict((args,))
kwargs.pop('simultaneous', None)
return self.xreplace(rules, **kwargs)

def _xreplace(self, subs):
Expand Down Expand Up @@ -329,7 +330,7 @@ def _eval_fd(self, expr):
- 3: Evaluate remaining terms (as `g` may need to be evaluated
at a different point).
- 4: Apply substitutions.
- 5: Cast to an object of type `EvalDiffDerivative` so that we know
- 5: Cast to an object of type `EvalDerivative` so that we know
the argument stems from a `Derivative. This may be useful for
later compilation passes.
"""
Expand Down Expand Up @@ -357,6 +358,6 @@ def _eval_fd(self, expr):

# Step 5: Cast to EvaluatedDerivative
assert res.is_Add
res = EvalDiffDerivative(*res.args, evaluate=False)
res = EvalDerivative(*res.args, evaluate=False)

return res
26 changes: 20 additions & 6 deletions devito/finite_differences/differentiable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from functools import singledispatch

import sympy
from sympy.functions.elementary.integers import floor
from sympy.core.decorators import call_highest_priority
from sympy.core.evalf import evalf_table

from cached_property import cached_property
from devito.finite_differences.tools import make_shift_x0
from devito.logger import warning
from devito.tools import filter_ordered, flatten
from devito.types.lazy import Evaluable, EvalDerivative
from devito.types.lazy import Evaluable
from devito.types.utils import DimensionTuple

__all__ = ['Differentiable']
Expand Down Expand Up @@ -202,9 +201,11 @@ def __rdiv__(self, other):
__rtruediv__ = __rdiv__

def __floordiv__(self, other):
from .elementary import floor
return floor(self / other)

def __rfloordiv__(self, other):
from .elementary import floor
return floor(other / self)

def __mod__(self, other):
Expand Down Expand Up @@ -347,6 +348,19 @@ def _eval_is_zero(self):
return None


class DifferentiableFunction(DifferentiableOp):

def __new__(cls, *args, **kwargs):
return cls.__sympy_class__.__new__(cls, *args, **kwargs)

@property
def evaluate(self):
return self.func(*[getattr(a, 'evaluate', a) for a in self.args])

def _eval_at(self, func):
return self


class Add(DifferentiableOp, sympy.Add):
__sympy_class__ = sympy.Add
__new__ = DifferentiableOp.__new__
Expand Down Expand Up @@ -379,7 +393,7 @@ def _gather_for_diff(self):
for f in self.args:
if f not in self._args_diff:
new_args.append(f)
elif f is func_args:
elif f is func_args or isinstance(f, DifferentiableFunction):
new_args.append(f)
else:
ind_f = f.indices_ref._getters
Expand All @@ -405,8 +419,8 @@ class Mod(DifferentiableOp, sympy.Mod):
__new__ = DifferentiableOp.__new__


class EvalDiffDerivative(DifferentiableOp, EvalDerivative):
__sympy_class__ = EvalDerivative
class EvalDerivative(DifferentiableOp, sympy.Add):
__sympy_class__ = sympy.Add
__new__ = DifferentiableOp.__new__


Expand Down Expand Up @@ -466,7 +480,7 @@ def _(obj):
@_cls.register(Mul)
@_cls.register(Pow)
@_cls.register(Mod)
@_cls.register(EvalDiffDerivative)
@_cls.register(EvalDerivative)
def _(obj):
return obj.__class__

Expand Down
Loading

0 comments on commit a8a33dc

Please sign in to comment.