PSyclone icon indicating copy to clipboard operation
PSyclone copied to clipboard

sympy expression errors in psyad

Open rupertford opened this issue 1 year ago • 1 comments

When trying to process poly1d_vert_adv_kernel_mod.F90 with psyad sympy expression errors occur.

rupertford avatar Jul 17 '22 14:07 rupertford

Any details? Can you perhaps provide the file?

hiker avatar Jul 18 '22 02:07 hiker

An update, the problem here is triggered by:

stencil(p+1) = k - floor(real(vertical_order,r_def)/2.0_r_def) + p

which gives:

File "/home/kbc59144/.pyenv/versions/3.10.7/envs/psyclone310/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py", line 907, in eval_expr
expr = eval(
File "<string>", line 1
k -Function ('FLOOR' )(Function ('REAL' )(Symbol ('vertical_order' ),Symbol ('r_def' ))/Float ('2.0' )Symbol ('_r_def' ))+p 
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

arporter avatar May 30 '23 08:05 arporter

You can reproduce this failure by doing:

cd PSyclone/examples/psyad/lfric
psyad -api lfric -a advective -- tangent_linear/tl_poly1d_vert_adv_kernel_mod.F90

arporter avatar May 30 '23 08:05 arporter

Issue is that floor is not supported in PSyIR (it ends up as a code block). Three changes required: 1.

--- a/src/psyclone/psyir/frontend/fparser2.py
+++ b/src/psyclone/psyir/frontend/fparser2.py
@@ -962,6 +962,7 @@ class Fparser2Reader():
         ('.not.', UnaryOperation.Operator.NOT),
         ('abs', UnaryOperation.Operator.ABS),
         ('ceiling', UnaryOperation.Operator.CEIL),
+        ('floor', UnaryOperation.Operator.FLOOR),
         ('exp', UnaryOperation.Operator.EXP),
         ('log', UnaryOperation.Operator.LOG),
         ('log10', UnaryOperation.Operator.LOG10),
 src/psyclone/psyir/nodes/operation.py
index f50a4f85b..59c5da980 100644
--- a/src/psyclone/psyir/nodes/operation.py
+++ b/src/psyclone/psyir/nodes/operation.py
@@ -320,7 +320,7 @@ class UnaryOperation(Operation):
         # Trigonometric Operators
         'COS', 'SIN', 'TAN', 'ACOS', 'ASIN', 'ATAN',
         # Other Maths Operators
-        'ABS', 'CEIL',
+        'ABS', 'CEIL', 'FLOOR',
         # Casting Operators
         'REAL', 'INT', 'NINT'
         ])
diff --git a/src/psyclone/psyir/backend/sympy_writer.py b/src/psyclone/psyir/backend/sympy_writer.py
index 13f81a708..012c36ef5 100644
--- a/src/psyclone/psyir/backend/sympy_writer.py
+++ b/src/psyclone/psyir/backend/sympy_writer.py
@@ -106,6 +106,7 @@ class SymPyWriter(FortranWriter):
                                  (BinaryOperation.Operator.MAX, "Max"),
                                  (NaryOperation.Operator.MIN, "Min"),
                                  (BinaryOperation.Operator.MIN, "Min"),
+                                 (UnaryOperation.Operator.FLOOR, "floor"),
                                  (BinaryOperation.Operator.REM, "Mod"),
                                  # exp is needed for a test case only, in
                                  # general the maths functions can just be

I'll fix this as part of #2120

hiker avatar May 30 '23 10:05 hiker

Thanks @hiker - just to make you aware we're transitioning intrinsics like this over to being subclasses of IntrinsicCall.

It may therefore be best as a separate PR.

arporter avatar May 31 '23 19:05 arporter