You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Note: this documentation has not yet been updated for v3 of Dedalus.**
5
-
6
-
The ``GeneralFunction`` class enables users to simply define new explicit operators for the right-hand side and analysis tasks of their simulations.
4
+
The ``GeneralFunction`` and ``UnaryGridFunction`` classes enables users to simply define new explicit operators for the right-hand side and analysis tasks of their simulations.
7
5
Such operators can be used to apply arbitrary user-defined functions to the grid values or coefficients of some set of input fields, or even do things like introduce random data or read data from an external source.
8
6
9
-
A ``GeneralFunction`` object is instantiated with a Dedalus domain, a layout object or descriptor (e.g. ``'g'`` or ``'c'`` for grid or coefficient space), a function, a list of arguments, and a dictionary of keywords.
7
+
A ``GeneralFunction`` object is instantiated with a Dedalus distributor, domain, tensor signature, dtype, layout object or descriptor (e.g. ``'g'`` or ``'c'`` for grid or coefficient space), function, list of arguments, and dictionary of keywords.
10
8
The resulting object is a Dedalus operator that can be evaluated and composed like other Dedalus operators.
11
9
It operates by first ensuring that any arguments that are Dedalus field objects are in the specified layout, then calling the function with the specified arguments and keywords, and finally setting the result as the output data in the specified layout.
12
10
13
-
Here's an example how you can use this class to apply a nonlinear function to the grid data of a single Dedalus field.
14
-
First, we define the underlying function we want to apply to the field data -- say the error function from scipy:
15
-
16
-
.. code-block:: python
17
-
18
-
from scipy import special
11
+
A simpler option that should work for many use cases is the ``UnaryGridFunction`` class, which specifically applies a function to the grid data of a single field.
12
+
The output field's distributor, domain/bases, tensor signature, and dtype are all taken to be idential to those of the input field.
13
+
Only the function and input field need to be specified.
14
+
The function must be vectorized, take a single Numpy array as input, and include an ``out`` argument that specifies the output array.
15
+
Applying most Numpy or Scipy universal functions to a Dedalus field will automatically produce the corresponding ``UnaryGridFunction`` operator.
19
16
20
-
deferf_func(field):
21
-
# Call scipy erf function on the field's data
22
-
return special.erf(field.data)
23
-
24
-
Second, we make a wrapper that returns a ``GeneralFunction`` instance that applies ``erf_func`` to a provided field in grid space.
25
-
This function produces a Dedalus operator, so it's what we want to use on the RHS or in analysis tasks:
17
+
Here's an example of using the ``UnaryGridFunction`` class to apply a custom function to the grid data of a single Dedalus field.
18
+
First, we define the underlying function we want to apply to the field data:
26
19
27
20
.. code-block:: python
28
21
29
-
import dedalus.public as de
30
-
31
-
deferf_operator(field):
32
-
# Return GeneralFunction instance that applies erf_func in grid space
33
-
return de.operators.GeneralFunction(
34
-
field.domain,
35
-
layout='g',
36
-
func= erf_func,
37
-
args= (field,)
38
-
)
22
+
# Custom function acting on grid data
23
+
defcustom_grid_function(x, out):
24
+
out[:] = (x + np.abs(x)) /2
25
+
return out
39
26
40
-
Finally, we add this wrapper to the parsing namespace to make it available in string-specified equations and analysis tasks:
27
+
Second, we make a wrapper that returns a ``UnaryGridFunction`` instance that applies ``custom_grid_function`` to a specified field.
28
+
This wrapper produces a Dedalus operator, so it's what we want to use on the RHS or in analysis tasks:
0 commit comments