|
1 | 1 | """ |
2 | | -Module for operator vectorize implementation. Differential operator are used to |
3 | | -write any differential problem. These operator are implemented to work on |
4 | | -different accellerators: CPU, GPU, TPU or MPS. All operator take as input a |
5 | | -tensor onto which computing the operator, a tensor with respect to which |
6 | | -computing the operator, the name of the output variables to calculate the |
7 | | -operator for (in case of multidimensional functions), and the variables name |
8 | | -on which the operator is calculated. |
| 2 | +Module for vectorized differential operators implementation. |
| 3 | +
|
| 4 | +Differential operators are used to define differential problems and are |
| 5 | +implemented to run efficiently on various accelerators, including CPU, GPU, TPU, |
| 6 | +and MPS. |
| 7 | +
|
| 8 | +Each differential operator takes the following inputs: |
| 9 | +- A tensor on which the operator is applied. |
| 10 | +- A tensor with respect to which the operator is computed. |
| 11 | +- The names of the output variables for which the operator is evaluated. |
| 12 | +- The names of the variables with respect to which the operator is computed. |
9 | 13 | """ |
10 | 14 |
|
11 | 15 | import torch |
|
14 | 18 |
|
15 | 19 | def grad(output_, input_, components=None, d=None): |
16 | 20 | """ |
17 | | - Perform gradient operation. The operator works for vectorial and scalar |
18 | | - functions, with multiple input coordinates. |
19 | | -
|
20 | | - :param LabelTensor output_: the output tensor onto which computing the |
21 | | - gradient. |
22 | | - :param LabelTensor input_: the input tensor with respect to which computing |
23 | | - the gradient. |
24 | | - :param list(str) components: the name of the output variables to calculate |
25 | | - the gradient for. It should be a subset of the output labels. If None, |
26 | | - all the output variables are considered. Default is None. |
27 | | - :param list(str) d: the name of the input variables on which the gradient is |
28 | | - calculated. d should be a subset of the input labels. If None, all the |
29 | | - input variables are considered. Default is None. |
30 | | -
|
31 | | - :return: the gradient tensor. |
| 21 | + Compute the gradient of the ``output_`` with respect to the ``input``. |
| 22 | +
|
| 23 | + This operator supports both vector-valued and scalar-valued functions with |
| 24 | + one or multiple input coordinates. |
| 25 | +
|
| 26 | + :param LabelTensor output_: The output tensor on which the gradient is |
| 27 | + computed. |
| 28 | + :param LabelTensor input_: The input tensor with respect to which the |
| 29 | + gradient is computed. |
| 30 | + :param list[str] components: The names of the output variables for which to |
| 31 | + compute the gradient. It must be a subset of the output labels. |
| 32 | + If ``None``, all output variables are considered. Default is ``None``. |
| 33 | + :param list[str] d: The names of the input variables with respect to which |
| 34 | + the gradient is computed. It must be a subset of the input labels. |
| 35 | + If ``None``, all input variables are considered. Default is ``None``. |
| 36 | + :raises TypeError: If the input tensor is not a LabelTensor. |
| 37 | + :raises RuntimeError: If the output is a scalar field and the components |
| 38 | + are not equal to the output labels. |
| 39 | + :raises NotImplementedError: If the output is neither a vector field nor a |
| 40 | + scalar field. |
| 41 | + :return: The computed gradient tensor. |
32 | 42 | :rtype: LabelTensor |
33 | 43 | """ |
34 | 44 |
|
35 | 45 | def grad_scalar_output(output_, input_, d): |
36 | 46 | """ |
37 | | - Perform gradient operation for a scalar output. |
38 | | -
|
39 | | - :param LabelTensor output_: the output tensor onto which computing the |
40 | | - gradient. It has to be a column tensor. |
41 | | - :param LabelTensor input_: the input tensor with respect to which |
42 | | - computing the gradient. |
43 | | - :param list(str) d: the name of the input variables on which the |
44 | | - gradient is calculated. d should be a subset of the input labels. If |
45 | | - None, all the input variables are considered. Default is None. |
46 | | -
|
47 | | - :raises RuntimeError: a vectorial function is passed. |
48 | | - :raises RuntimeError: missing derivative labels. |
49 | | - :return: the gradient tensor. |
| 47 | + Compute the gradient of a scalar-valued ``output_``. |
| 48 | +
|
| 49 | + :param LabelTensor output_: The output tensor on which the gradient is |
| 50 | + computed. It must be a column tensor. |
| 51 | + :param LabelTensor input_: The input tensor with respect to which the |
| 52 | + gradient is computed. |
| 53 | + :param list[str] d: The names of the input variables with respect to |
| 54 | + which the gradient is computed. It must be a subset of the input |
| 55 | + labels. If ``None``, all input variables are considered. |
| 56 | + :raises RuntimeError: If a vectorial function is passed. |
| 57 | + :raises RuntimeError: If missing derivative labels. |
| 58 | + :return: The computed gradient tensor. |
50 | 59 | :rtype: LabelTensor |
51 | 60 | """ |
52 | 61 |
|
@@ -102,25 +111,26 @@ def grad_scalar_output(output_, input_, d): |
102 | 111 |
|
103 | 112 | def div(output_, input_, components=None, d=None): |
104 | 113 | """ |
105 | | - Perform divergence operation. The operator works for vectorial functions, |
106 | | - with multiple input coordinates. |
107 | | -
|
108 | | - :param LabelTensor output_: the output tensor onto which computing the |
109 | | - divergence. |
110 | | - :param LabelTensor input_: the input tensor with respect to which computing |
111 | | - the divergence. |
112 | | - :param list(str) components: the name of the output variables to calculate |
113 | | - the divergence for. It should be a subset of the output labels. If None, |
114 | | - all the output variables are considered. Default is None. |
115 | | - :param list(str) d: the name of the input variables on which the divergence |
116 | | - is calculated. d should be a subset of the input labels. If None, all |
117 | | - the input variables are considered. Default is None. |
118 | | -
|
119 | | - :raises TypeError: div operator works only for LabelTensor. |
120 | | - :raises ValueError: div operator works only for vector fields. |
121 | | - :raises ValueError: div operator must derive all components with |
122 | | - respect to all coordinates. |
123 | | - :return: the divergenge tensor. |
| 114 | + Compute the divergence of the ``output_`` with respect to ``input``. |
| 115 | +
|
| 116 | + This operator supports vector-valued functions with multiple input |
| 117 | + coordinates. |
| 118 | +
|
| 119 | + :param LabelTensor output_: The output tensor on which the divergence is |
| 120 | + computed. |
| 121 | + :param LabelTensor input_: The input tensor with respect to which the |
| 122 | + divergence is computed. |
| 123 | + :param list[str] components: The names of the output variables for which to |
| 124 | + compute the divergence. It must be a subset of the output labels. |
| 125 | + If ``None``, all output variables are considered. Default is ``None``. |
| 126 | + :param list[str] d: The names of the input variables with respect to which |
| 127 | + the divergence is computed. It must be a subset of the input labels. |
| 128 | + If ``None``, all input variables are considered. Default is ``None``. |
| 129 | + :raises TypeError: If the input tensor is not a LabelTensor. |
| 130 | + :raises ValueError: If the output is a scalar field. |
| 131 | + :raises ValueError: If the number of components is not equal to the number |
| 132 | + of input variables. |
| 133 | + :return: The computed divergence tensor. |
124 | 134 | :rtype: LabelTensor |
125 | 135 | """ |
126 | 136 | if not isinstance(input_, LabelTensor): |
@@ -151,42 +161,43 @@ def div(output_, input_, components=None, d=None): |
151 | 161 |
|
152 | 162 | def laplacian(output_, input_, components=None, d=None, method="std"): |
153 | 163 | """ |
154 | | - Compute Laplace operator. The operator works for vectorial and |
155 | | - scalar functions, with multiple input coordinates. |
156 | | -
|
157 | | - :param LabelTensor output_: the output tensor onto which computing the |
158 | | - Laplacian. |
159 | | - :param LabelTensor input_: the input tensor with respect to which computing |
160 | | - the Laplacian. |
161 | | - :param list(str) components: the name of the output variables to calculate |
162 | | - the Laplacian for. It should be a subset of the output labels. If None, |
163 | | - all the output variables are considered. Default is None. |
164 | | - :param list(str) d: the name of the input variables on which the Laplacian |
165 | | - is calculated. d should be a subset of the input labels. If None, all |
166 | | - the input variables are considered. Default is None. |
167 | | - :param str method: used method to calculate Laplacian, defaults to 'std'. |
168 | | -
|
169 | | - :raises NotImplementedError: 'divgrad' not implemented as method. |
170 | | - :return: The tensor containing the result of the Laplacian operator. |
| 164 | + Compute the laplacian of the ``output_`` with respect to ``input``. |
| 165 | +
|
| 166 | + This operator supports both vector-valued and scalar-valued functions with |
| 167 | + one or multiple input coordinates. |
| 168 | +
|
| 169 | + :param LabelTensor output_: The output tensor on which the laplacian is |
| 170 | + computed. |
| 171 | + :param LabelTensor input_: The input tensor with respect to which the |
| 172 | + laplacian is computed. |
| 173 | + :param list[str] components: The names of the output variables for which to |
| 174 | + compute the laplacian. It must be a subset of the output labels. |
| 175 | + If ``None``, all output variables are considered. Default is ``None``. |
| 176 | + :param list[str] d: The names of the input variables with respect to which |
| 177 | + the laplacian is computed. It must be a subset of the input labels. |
| 178 | + If ``None``, all input variables are considered. Default is ``None``. |
| 179 | + :param str method: The method used to compute the Laplacian. Default is |
| 180 | + ``std``. |
| 181 | + :raises NotImplementedError: If ``std=divgrad``. |
| 182 | + :return: The computed laplacian tensor. |
171 | 183 | :rtype: LabelTensor |
172 | 184 | """ |
173 | 185 |
|
174 | 186 | def scalar_laplace(output_, input_, components, d): |
175 | 187 | """ |
176 | | - Compute Laplace operator for a scalar output. |
177 | | -
|
178 | | - :param LabelTensor output_: the output tensor onto which computing the |
179 | | - Laplacian. It has to be a column tensor. |
180 | | - :param LabelTensor input_: the input tensor with respect to which |
181 | | - computing the Laplacian. |
182 | | - :param list(str) components: the name of the output variables to |
183 | | - calculate the Laplacian for. It should be a subset of the output |
184 | | - labels. If None, all the output variables are considered. |
185 | | - :param list(str) d: the name of the input variables on which the |
186 | | - Laplacian is computed. d should be a subset of the input labels. |
187 | | - If None, all the input variables are considered. Default is None. |
188 | | -
|
189 | | - :return: The tensor containing the result of the Laplacian operator. |
| 188 | + Compute the laplacian of a scalar-valued ``output_``. |
| 189 | +
|
| 190 | + :param LabelTensor output_: The output tensor on which the laplacian is |
| 191 | + computed. It must be a column tensor. |
| 192 | + :param LabelTensor input_: The input tensor with respect to which the |
| 193 | + laplacian is computed. |
| 194 | + :param list[str] components: The names of the output variables for which |
| 195 | + to compute the laplacian. It must be a subset of the output labels. |
| 196 | + If ``None``, all output variables are considered. |
| 197 | + :param list[str] d: The names of the input variables with respect to |
| 198 | + which the laplacian is computed. It must be a subset of the input |
| 199 | + labels. If ``None``, all input variables are considered. |
| 200 | + :return: The computed laplacian tensor. |
190 | 201 | :rtype: LabelTensor |
191 | 202 | """ |
192 | 203 |
|
@@ -230,22 +241,23 @@ def scalar_laplace(output_, input_, components, d): |
230 | 241 |
|
231 | 242 | def advection(output_, input_, velocity_field, components=None, d=None): |
232 | 243 | """ |
233 | | - Perform advection operation. The operator works for vectorial functions, |
234 | | - with multiple input coordinates. |
235 | | -
|
236 | | - :param LabelTensor output_: the output tensor onto which computing the |
237 | | - advection. |
238 | | - :param LabelTensor input_: the input tensor with respect to which computing |
239 | | - the advection. |
240 | | - :param str velocity_field: the name of the output variables which is used |
241 | | - as velocity field. It should be a subset of the output labels. |
242 | | - :param list(str) components: the name of the output variables to calculate |
243 | | - the Laplacian for. It should be a subset of the output labels. If None, |
244 | | - all the output variables are considered. Default is None. |
245 | | - :param list(str) d: the name of the input variables on which the advection |
246 | | - is calculated. d should be a subset of the input labels. If None, all |
247 | | - the input variables are considered. Default is None. |
248 | | - :return: the tensor containing the result of the advection operator. |
| 244 | + Perform the advection operation on the ``output_`` with respect to the |
| 245 | + ``input``. This operator support vector-valued functions with multiple input |
| 246 | + coordinates. |
| 247 | +
|
| 248 | + :param LabelTensor output_: The output tensor on which the advection is |
| 249 | + computed. |
| 250 | + :param LabelTensor input_: the input tensor with respect to which advection |
| 251 | + is computed. |
| 252 | + :param str velocity_field: The name of the output variable used as velocity |
| 253 | + field. It must be chosen among the output labels. |
| 254 | + :param list[str] components: The names of the output variables for which |
| 255 | + to compute the advection. It must be a subset of the output labels. |
| 256 | + If ``None``, all output variables are considered. Default is ``None``. |
| 257 | + :param list[str] d: The names of the input variables with respect to which |
| 258 | + the advection is computed. It must be a subset of the input labels. |
| 259 | + If ``None``, all input variables are considered. Default is ``None``. |
| 260 | + :return: The computed advection tensor. |
249 | 261 | :rtype: LabelTensor |
250 | 262 | """ |
251 | 263 | if d is None: |
|
0 commit comments