Complex numbers in kernels when using JITParticle #1780
Replies: 2 comments
-
To add to this, as the kernel I'm developing doesn't just define some complex number and then compute the real/imaginary parts of it (the example above), I'd also like to make sure any computation with complex numbers is correct also! For example: import math
def SomeComplexNumberFunction(particle, fieldset, time):
rdelta = 1./1.15 # Some relation between densities of two things
psi = (1.j * math.sqrt(1. - (rdelta - 1.)**2) + rdelta - 1.)**(1./3.)
Phi = (1.j * math.sqrt(3.) / 2.) * (1. / psi - psi) - 1. / (2. * psi) - psi / 2. + 1.
Phi = Phi.real # This not only needs to not throw an error, but be computed correctly too!
# some other computations
... |
Beta Was this translation helpful? Give feedback.
-
Thanks for the question, @michaeldenes. Parcels currently doesn't support Complex numbers in its python-to-C-codeconverter. It would be a major undertaking to implement support, requiring a lot of extra code. Is there no way around using complex numbers? One alternative, if you really need to use complex number arithmetic, is to use "custom headers". This is a little-used feature in Parcels, that allows you to directly inject C-code into your python-Kernels. See e.g. below for how this is used in the unit-tests Parcels/tests/test_kernel_language.py Lines 370 to 397 in e3c4615 If you can write a function that uses complex arithmetic in C and put it in such a header file, you could(?) then put it into a kernel and call it that way. But be warned; getting this method to work may be very finicky... |
Beta Was this translation helpful? Give feedback.
-
Question
Question
I'm wondering how to use complex numbers in kernels when using
JIT
mode. This may be quite a silly question, I simply need to extract the real part of a complex number, butx.real
doesn't seem to work with cgen. See below a minimal working example, using the NEMOparcels
tutorial example. There is an error when run inJIT
mode, but no error (and the correct output) when run inScipy
mode!Scipy
mode is just too slow for my application.Parcels version:
Parcels version: 3.0.5.dev202
Supporting code/error messages
When run in
JIT
mode:When run in
Scipy
mode, the output ofpset
is:Beta Was this translation helpful? Give feedback.
All reactions