From 18e434f97d85009f5630427fb6376e588698057c Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Keck <Jean-Baptiste.Keck@imag.fr>
Date: Sun, 2 Dec 2018 00:13:55 +0100
Subject: [PATCH] fixed some bugs in fft operator tests

---
 hysop/backend/device/codegen/base/variables.py              | 5 +++--
 hysop/backend/device/codegen/symbolic/expr.py               | 5 +++++
 .../codegen/symbolic/functions/custom_symbolic_function.py  | 4 +++-
 hysop/backend/device/opencl/opencl_types.py                 | 5 +++++
 hysop/backend/host/python/operator/derivative.py            | 2 +-
 hysop/operator/base/custom_symbolic_operator.py             | 6 +++---
 6 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/hysop/backend/device/codegen/base/variables.py b/hysop/backend/device/codegen/base/variables.py
index 73d02c471..e10575633 100644
--- a/hysop/backend/device/codegen/base/variables.py
+++ b/hysop/backend/device/codegen/base/variables.py
@@ -851,14 +851,15 @@ class CodegenVectorClBuiltin(CodegenVector):
                 value = [self.svalue[i] for i in key]
                 return '({})({})'.format(ctype, ','.join(value))
             return access
-        elif isinstance(key, int) :
+        elif isinstance(key, (int,long)) :
             if key<0:
                 key += dim
             if key<0 or key>=dim:
                 raise IndexError, "The index {} is out of range.".format(key)
             return self.sval(key)
         else:
-            raise TypeError, 'Invalid key type!'
+            msg='Invalid key type {}!'.format(type(key))
+            raise TypeError(msg)
     
     def declare(self, codegen=None, init=None, **kargs):
         init = init or self.init
diff --git a/hysop/backend/device/codegen/symbolic/expr.py b/hysop/backend/device/codegen/symbolic/expr.py
index 4b20d5e00..f02f7daee 100644
--- a/hysop/backend/device/codegen/symbolic/expr.py
+++ b/hysop/backend/device/codegen/symbolic/expr.py
@@ -163,6 +163,11 @@ class IntegerConstant(NumericalConstant):
     pass
 class FloatingPointConstant(NumericalConstant):
     pass
+class ComplexFloatingPointConstant(NumericalConstant):
+    def _ccode(self, printer):
+        return '(({})({}, {}))'.format(self.ctype, 
+                printer.typegen.dump(self.value.real),
+                printer.typegen.dump(self.value.imag))
 
 class OpenClVariable(TypedExpr):
     def __new__(cls, ctype, var, *args):
diff --git a/hysop/backend/device/codegen/symbolic/functions/custom_symbolic_function.py b/hysop/backend/device/codegen/symbolic/functions/custom_symbolic_function.py
index d3ebe5d34..7f2ba3318 100644
--- a/hysop/backend/device/codegen/symbolic/functions/custom_symbolic_function.py
+++ b/hysop/backend/device/codegen/symbolic/functions/custom_symbolic_function.py
@@ -21,7 +21,7 @@ from hysop.backend.device.codegen.symbolic.expr import VLoad, VStore, \
         VStoreIf, VLoadIf, OpenClVariable, OpenClPrinter,  TypedI, \
         OpenClAssignment, OpenClCast, OpenClIndexedVariable, \
         NumericalConstant, FloatingPointConstant, IntegerConstant, \
-        FunctionCall, Return
+        FunctionCall, Return, ComplexFloatingPointConstant
 
 from hysop.backend.device.codegen.symbolic.map import map_expression, OpenClCastUtils
 
@@ -180,6 +180,8 @@ class CustomSymbolicFunction(OpenClFunctionCodeGenerator):
             pexpr = IntegerConstant('int', expr)
         elif isinstance(expr, (float, sm.Rational, sm.Float)):
             pexpr = FloatingPointConstant(csc.typegen.fbtype, expr)
+        elif isinstance(expr, complex):
+            pexpr = ComplexFloatingPointConstant(csc.typegen.fbtype+'2', expr)
         elif isinstance(expr, npw.number):
             ctype = dtype_to_ctype(expr)
             pexpr = NumericalConstant(ctype, expr)
diff --git a/hysop/backend/device/opencl/opencl_types.py b/hysop/backend/device/opencl/opencl_types.py
index cda11cef1..10fa35301 100644
--- a/hysop/backend/device/opencl/opencl_types.py
+++ b/hysop/backend/device/opencl/opencl_types.py
@@ -86,6 +86,9 @@ def vtype_access(i,N,mode='hex'):
     else: return ('s' if mode.lower()=='hex' else '') + vtype_component_adressing(i,mode)
 
 def float_to_hex_str(f,fbtype):
+    if (f!=f):
+        return 'NAN'
+
     sf = float(f).hex().split('0x') + ['']
     buf = sf[1].split('p')
 
@@ -104,6 +107,8 @@ def float_to_hex_str(f,fbtype):
     return ''.join(sf)
 
 def float_to_dec_str(f,fbtype):
+    if (f!=f):
+        return 'NAN'
     
     sf = float(f).__repr__().split('.')
     sf += (3-len(sf))*[None]
diff --git a/hysop/backend/host/python/operator/derivative.py b/hysop/backend/host/python/operator/derivative.py
index e6a4cb18c..7317aa43d 100644
--- a/hysop/backend/host/python/operator/derivative.py
+++ b/hysop/backend/host/python/operator/derivative.py
@@ -35,7 +35,7 @@ class PythonSpectralSpaceDerivative(SpectralSpaceDerivativeBase, HostOperator):
     def compute_derivative(self):
         from hysop.constants import BoxBoundaryCondition
         for nd_dkd in self.nd_dkds:
-            self.Ft.output_buffer[...] *= nd_dkd
+            self.Ft.full_output_buffer[...] *= nd_dkd
 
     def scale_derivative(self):
         out   = self.Bt.output_buffer
diff --git a/hysop/operator/base/custom_symbolic_operator.py b/hysop/operator/base/custom_symbolic_operator.py
index 506364e08..8763743d7 100644
--- a/hysop/operator/base/custom_symbolic_operator.py
+++ b/hysop/operator/base/custom_symbolic_operator.py
@@ -607,7 +607,7 @@ class SymbolicExpressionParser(object):
         if isinstance(expr, npw.ndarray):
             assert expr.ndim == 0
             expr = expr.tolist()
-        if isinstance(expr, (str, int,long,float,npw.number)):
+        if isinstance(expr, (str, int,long,float,complex,npw.number)):
             return
         elif isinstance(expr, (AppliedSymbolicField, SymbolicScalarParameter, SymbolicArray)):
             cls.read(variables, info, expr)
@@ -861,7 +861,7 @@ class SymbolicExpressionParser(object):
             assert expr.ndim == 0
             expr = expr.tolist()
 
-        if isinstance(expr, (int, long, sm.Integer, float, sm.Rational, sm.Float, npw.number)):
+        if isinstance(expr, (int, long, sm.Integer, float, complex, sm.Rational, sm.Float, npw.number)):
             return {}
         elif isinstance(expr, SymbolicArray):
             return {expr: expr.new_requirements()}
@@ -1026,7 +1026,7 @@ class SymbolicExpressionParser(object):
             assert expr.ndim == 0
             expr = expr.tolist()
         di = ExprDiscretizationInfo()
-        if isinstance(expr, (int,long,float,npw.number)):
+        if isinstance(expr, (int,long,float,complex,npw.number)):
             return expr, di
         elif cls.should_transpose_expr(info, expr):
             expr = cls.transpose_expr(info, expr)
-- 
GitLab