Skip to content

Commit

Permalink
Merge pull request #50 from pyccel/symbolic-expr
Browse files Browse the repository at this point in the history
Use SymbolicExpr instead of print_expression and pycode.
  • Loading branch information
ratnania authored Aug 30, 2019
2 parents b857483 + f5be1de commit 85c698f
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 486 deletions.
131 changes: 40 additions & 91 deletions psydac/api/ast/evaluation.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,30 @@
# TODO: - replace call to print_expression by SymbolicExpr (may need to use LogicalExpr)

from collections import OrderedDict
from itertools import groupby
import numpy as np

from sympy import Basic
from sympy import symbols, Symbol, IndexedBase, Indexed, Function
from sympy import Mul, Add, Tuple, Min, Max, Pow
from sympy import Matrix, ImmutableDenseMatrix
from sympy import sqrt as sympy_sqrt
from sympy import S as sympy_S
from sympy import Integer, Float
from sympy.core.relational import Le, Ge
from sympy.logic.boolalg import And
from sympy import Mod, Abs
from sympy.core.function import AppliedUndef

from pyccel.ast.core import Variable, IndexedVariable
from pyccel.ast.core import For
from pyccel.ast.core import Assign
from pyccel.ast.core import AugAssign
from pyccel.ast.core import Slice
from pyccel.ast.core import Range, Product
from pyccel.ast.core import FunctionDef
from pyccel.ast.core import FunctionCall
from pyccel.ast.core import Import
from pyccel.ast import Zeros
from pyccel.ast import Import
from pyccel.ast import DottedName
from pyccel.ast import Nil
from pyccel.ast import Len
from pyccel.ast import If, Is, Return
from pyccel.ast import String, Print, Shape
from pyccel.ast import Comment, NewLine
from pyccel.ast.core import _atomic
from pyccel.ast.utilities import build_types_decorator

from sympy import symbols
from sympy import Tuple, Pow

from sympde.core import Cross_3d
from sympde.core import Constant
from sympde.calculus import grad
from sympde.topology import Mapping
from sympde.topology import ScalarField
from sympde.topology import VectorField, IndexedVectorField
from sympde.topology import Boundary, BoundaryVector, NormalVector, TangentVector
from sympde.topology import Covariant, Contravariant
from sympde.topology import ElementArea
from sympde.topology import LogicalExpr
from sympde.topology import SymbolicExpr
from sympde.topology.derivatives import _partial_derivatives
from sympde.topology import Mapping
from sympde.topology import ScalarField
from sympde.topology import VectorField
from sympde.topology import SymbolicExpr
from sympde.topology.space import ScalarTestFunction
from sympde.topology.derivatives import _logical_partial_derivatives
from sympde.topology.derivatives import get_max_partial_derivatives
from sympde.topology.space import ScalarFunctionSpace
from sympde.topology.space import ScalarTestFunction
from sympde.topology.space import VectorTestFunction
from sympde.topology.space import IndexedTestTrial
from sympde.topology.space import Trace
from sympde.topology.derivatives import print_expression
from sympde.topology.derivatives import get_atom_derivatives
from sympde.topology.derivatives import get_index_derivatives
from sympde.expr import BilinearForm, LinearForm, Functional, BasicForm

from psydac.fem.splines import SplineSpace
from psydac.fem.tensor import TensorFemSpace
from psydac.fem.vector import ProductFemSpace

from .basic import SplBasic

from pyccel.ast.core import IndexedVariable
from pyccel.ast.core import For
from pyccel.ast.core import Assign
from pyccel.ast.core import Slice
from pyccel.ast.core import Range
from pyccel.ast.core import FunctionDef
from pyccel.ast.utilities import build_types_decorator

from .basic import SplBasic
from .utilities import build_pythran_types_header, variables
from .utilities import filter_loops, filter_product
from .utilities import rationalize_eval_mapping
from .utilities import compute_atoms_expr_mapping
from .utilities import compute_atoms_expr_field


#==============================================================================
class EvalQuadratureMapping(SplBasic):

Expand Down Expand Up @@ -100,7 +52,7 @@ def __new__(cls, space, mapping, discrete_boundary=None, name=None,
# ...

# ...
ops = _partial_derivatives[:dim]
ops = _logical_partial_derivatives[:dim]
M = mapping

components = [M[i] for i in range(0, dim)]
Expand Down Expand Up @@ -196,10 +148,8 @@ def _initialize(self):
space = self.space
dim = space.ldim

_print = lambda i: print_expression(i, mapping_name=False)
mapping_atoms = [_print(f) for f in self.components]
mapping_str = [_print(f) for f in self.elements]
# mapping_str = sorted([_print(f) for f in self.elements])
mapping_atoms = [SymbolicExpr(f).name for f in self.components]
mapping_str = [SymbolicExpr(f).name for f in self.elements ]

# ... declarations
degrees = variables( 'p1:%s'%(dim+1), 'int')
Expand Down Expand Up @@ -229,7 +179,7 @@ def _initialize(self):

# ...
nderiv = self.nderiv
ops = _partial_derivatives[:dim]
ops = _logical_partial_derivatives[:dim]

if nderiv > 0:
weights_elements += [d(weights_pts) for d in ops]
Expand All @@ -242,7 +192,7 @@ def _initialize(self):
raise NotImplementedError('TODO')
# ...

mapping_weights_str = [_print(f) for f in weights_elements]
mapping_weights_str = [SymbolicExpr(f).name for f in weights_elements]
mapping_wvalues = variables(['{}_values'.format(f) for f in mapping_weights_str],
dtype='real', rank=dim, cls=IndexedVariable)

Expand All @@ -267,11 +217,12 @@ def _initialize(self):
# ...

# ...
Nj = ScalarTestFunction(space, name='Nj')
body = []
init_basis = OrderedDict()
updates = []
Nj = ScalarTestFunction(space, name='Nj')
body = []
init_basis = OrderedDict()
updates = []
atomic_exprs = self.elements + weights_elements

inits, updates = compute_atoms_expr_mapping(atomic_exprs, indices_quad,
indices_basis, basis, Nj)

Expand Down Expand Up @@ -356,7 +307,6 @@ def __new__(cls, space, fields, discrete_boundary=None, name=None,
obj._backend = backend
obj._func = obj._initialize()


return obj

@property
Expand Down Expand Up @@ -431,8 +381,8 @@ def _initialize(self):

self._fields = Tuple(*fields)

fields_str = [print_expression(f) for f in self._fields]
fields_val = variables(['{}_values'.format(f) for f in fields_str],
fields_str = [SymbolicExpr(f).name for f in self._fields]
fields_val = variables(['{}_values'.format(f) for f in fields_str],
dtype='real', rank=dim, cls=IndexedVariable)


Expand Down Expand Up @@ -539,7 +489,7 @@ def _initialize(self):
indices_quad = variables('g1:%s'%(dim+1), 'int')
basis = variables('basis1:%s'%(dim+1),
dtype='real', rank=3, cls=IndexedVariable)
coeffs = ['coeff_{}'.format(print_expression(f)) for f in vector_field_atoms]
coeffs = ['coeff_{}'.format(SymbolicExpr(f).name) for f in vector_field_atoms]
vector_fields_coeffs = variables(coeffs, dtype='real', rank=dim, cls=IndexedVariable)
# ...

Expand Down Expand Up @@ -568,7 +518,7 @@ def _initialize(self):
init_map[str(stmt.lhs)] = stmt

self._vector_fields = Tuple(*vector_fields)
vector_fields_str = [print_expression(f) for f in self.vector_fields]
vector_fields_str = [SymbolicExpr(f).name for f in self.vector_fields]
vector_fields_val = variables(['{}_values'.format(f) for f in vector_fields_str],
dtype='real', rank=dim, cls=IndexedVariable)

Expand Down Expand Up @@ -626,6 +576,7 @@ def _create_loop(indices, ranges, body):
return body

#==============================================================================
# NOTE: this is used in module 'psydac.api.ast.glt'
class EvalArrayField(SplBasic):

def __new__(cls, space, fields, discrete_boundary=None, name=None,
Expand All @@ -645,7 +596,6 @@ def __new__(cls, space, fields, discrete_boundary=None, name=None,
obj._backend = backend
obj._func = obj._initialize()


return obj

@property
Expand Down Expand Up @@ -680,7 +630,7 @@ def _initialize(self):
mapping = self.mapping

field_atoms = self.fields.atoms(ScalarField)
fields_str = sorted([print_expression(f) for f in self.fields])
fields_str = sorted([SymbolicExpr(f).name for f in self.fields])

# ... declarations
degrees = variables( 'p1:%s'%(dim+1), 'int')
Expand Down Expand Up @@ -771,6 +721,7 @@ def _initialize(self):


#==============================================================================
# NOTE: this is used in module 'psydac.api.ast.glt'
class EvalArrayMapping(SplBasic):

def __new__(cls, space, mapping, name=None,
Expand All @@ -795,7 +746,7 @@ def __new__(cls, space, mapping, name=None,
# ...

# ...
ops = _partial_derivatives[:dim]
ops = _logical_partial_derivatives[:dim]
M = mapping

components = [M[i] for i in range(0, dim)]
Expand Down Expand Up @@ -880,10 +831,8 @@ def _initialize(self):
space = self.space
dim = space.ldim

_print = lambda i: print_expression(i, mapping_name=False)
mapping_atoms = [_print(f) for f in self.components]
mapping_str = [_print(f) for f in self.elements]
# mapping_str = sorted([_print(f) for f in self.elements])
mapping_atoms = [SymbolicExpr(f).name for f in self.components]
mapping_str = [SymbolicExpr(f).name for f in self.elements ]

# ... declarations
degrees = variables( 'p1:%s'%(dim+1), 'int')
Expand Down Expand Up @@ -917,7 +866,7 @@ def _initialize(self):

# ...
nderiv = self.nderiv
ops = _partial_derivatives[:dim]
ops = _logical_partial_derivatives[:dim]

if nderiv > 0:
weights_elements += [d(weights_pts) for d in ops]
Expand All @@ -930,7 +879,7 @@ def _initialize(self):
raise NotImplementedError('TODO')
# ...

mapping_weights_str = [_print(f) for f in weights_elements]
mapping_weights_str = [SymbolicExpr(f).name for f in weights_elements]
mapping_wvalues = variables(['{}_values'.format(f) for f in mapping_weights_str],
dtype='real', rank=dim, cls=IndexedVariable)

Expand Down Expand Up @@ -983,8 +932,8 @@ def _initialize(self):
assign_spans = []
for x, i_span, span in zip(indices_quad, i_spans, spans):
assign_spans += [Assign(i_span, span[x])]

body = assign_spans + body

# put the body in for loops of quadrature points
body = _create_loop(indices_quad, ranges_quad, body)

Expand Down
78 changes: 29 additions & 49 deletions psydac/api/ast/expr.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,41 @@

from collections import OrderedDict
from itertools import groupby
import string
import random
import numpy as np

from sympy import Basic
from sympy import symbols, Symbol, IndexedBase, Indexed, Function
from sympy import Mul, Add, Tuple
from sympy import symbols, Symbol, IndexedBase
from sympy import Mul, Tuple
from sympy import Matrix, ImmutableDenseMatrix
from sympy import sqrt as sympy_sqrt
from sympy import S as sympy_S
from sympy import simplify, expand
from sympy.core.numbers import ImaginaryUnit

from pyccel.ast.core import Variable, IndexedVariable
from pyccel.ast.core import IndexedVariable
from pyccel.ast.core import For
from pyccel.ast.core import Assign
from pyccel.ast.core import AugAssign
from pyccel.ast.core import Slice
from pyccel.ast.core import Range
from pyccel.ast.core import FunctionDef
from pyccel.ast.core import FunctionCall
from pyccel.ast import Zeros
from pyccel.ast import Import
from pyccel.ast import DottedName
from pyccel.ast import Nil
from pyccel.ast import Len
from pyccel.ast import If, Is, Return
from pyccel.ast import String, Print, Shape
from pyccel.ast import Comment, NewLine
from pyccel.ast.core import _atomic

from sympde.core import Constant
from sympde.topology import ScalarField, VectorField
from sympde.topology import IndexedVectorField
from sympde.topology import Mapping
from sympde.expr import BilinearForm
from pyccel.ast import Import
from pyccel.ast import Nil
from pyccel.ast import Len
from pyccel.ast import If, Is, Return
from pyccel.ast.core import _atomic

from sympde.core import Constant
from sympde.topology import ScalarField, VectorField
from sympde.topology import IndexedVectorField
from sympde.topology.derivatives import _partial_derivatives
from sympde.topology.derivatives import _logical_partial_derivatives
from sympde.topology.derivatives import get_max_partial_derivatives
from sympde.topology.derivatives import print_expression
from sympde.topology.derivatives import get_atom_derivatives
from sympde.topology.derivatives import get_index_derivatives
from sympde.topology import LogicalExpr
from sympde.topology import SymbolicExpr
from sympde.topology import SymbolicDeterminant

from .basic import SplBasic
from .utilities import random_string
from .utilities import build_pythran_types_header, variables
from .utilities import is_scalar_field, is_vector_field, is_mapping
from .utilities import math_atoms_as_str
#from .evaluation import EvalArrayVectorField
from .evaluation import EvalArrayMapping, EvalArrayField

from psydac.fem.splines import SplineSpace
from psydac.fem.tensor import TensorFemSpace
from psydac.fem.vector import ProductFemSpace, VectorFemSpace
from sympde.topology import LogicalExpr
from sympde.topology import SymbolicExpr
from sympde.topology import SymbolicDeterminant

from .basic import SplBasic
from .utilities import random_string
from .utilities import build_pythran_types_header, variables
from .utilities import is_scalar_field, is_vector_field
from .utilities import math_atoms_as_str

from psydac.fem.vector import ProductFemSpace

#==============================================================================
def compute_atoms_expr(atom, basis, indices, loc_indices, dim):
Expand Down Expand Up @@ -268,8 +246,8 @@ def _initialize(self):
self._fields = tuple(expr.atoms(ScalarField))
self._vector_fields = tuple(expr.atoms(VectorField))
# ...
fields_str = tuple(map(print_expression, atomic_expr_field))
vector_fields_str = tuple(map(print_expression, atomic_expr_vector_field))
fields_str = tuple(SymbolicExpr(f).name for f in atomic_expr_field)
vector_fields_str = tuple(SymbolicExpr(f).name for f in atomic_expr_vector_field)

fields = symbols(fields_str)
vector_fields = symbols(vector_fields_str)
Expand Down Expand Up @@ -362,10 +340,12 @@ def _initialize(self):
val = val[indices]

if isinstance(expr, (Matrix, ImmutableDenseMatrix)):
body += [Assign(val, expr[i_row,i_col])]
rhs = SymbolicExpr(expr[i_row,i_col])
body += [Assign(val, rhs)]

else:
body += [Assign(val, expr)]
rhs = SymbolicExpr(expr)
body += [Assign(val, rhs)]

for i in range(dim-1,-1,-1):
x = indices[i]
Expand Down
Loading

0 comments on commit 85c698f

Please sign in to comment.