from typing import Annotated
from qewton.config.axes import EllipsisAxes, FeatureAxes, AxesDim
from qewton.backends import TensorType
from qewton.config.data_configurations import DataConfiguration as DC
from qewton.graphs.nodes import Node
[docs]
class GradientTracking(Node[TensorType]):
ell_axes = EllipsisAxes()
[docs]
def forward(
self,
inp: Annotated[TensorType, DC(ell_axes)],
) -> Annotated[TensorType, DC(ell_axes)]:
return self.backend.grad.gradient_tracking(inp)
[docs]
class Gradient(Node[TensorType]):
ell_axes = EllipsisAxes()
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(1,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))]:
return self.backend.grad.gradient(u, x)
[docs]
class Laplacian(Node[TensorType]):
"""Computes the laplacian (sum of second derivatives) of a scalar output
with respect to the input variable."""
ell_axes = EllipsisAxes()
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(1,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes())],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(1,)))]:
return self.backend.grad.laplacian(u, x)
[docs]
class NormalDerivative(Node[TensorType]):
"""Computes the normal derivative (gradient · normals) of a scalar output."""
ell_axes = EllipsisAxes()
x_dim = AxesDim(None)
u_dim = AxesDim(1)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(u_dim,)))],
normals: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(u_dim,)))]:
return self.backend.grad.normal_derivative(u, normals, x)
[docs]
class Divergence(Node[TensorType]):
"""Computes the divergence of a vector field (model output) with
respect to spatial variables."""
ell_axes = EllipsisAxes()
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(1,)))]:
return self.backend.grad.divergence(u, x)
[docs]
class Jacobian(Node[TensorType]):
"""Computes the Jacobian matrix of a vector output with respect to input variables."""
ell_axes = EllipsisAxes()
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim, x_dim)))]:
return self.backend.grad.jacobian(u, x)
[docs]
class Partial(Node[TensorType]):
"""Computes n-th order partial derivatives recursively with
respect to multiple variables."""
ell_axes = EllipsisAxes()
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(1,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))]:
return self.backend.grad.partial(u, x)
[docs]
class Hessian(Node[TensorType]):
"""Computes the Hessian matrix (second partial derivatives) of a scalar
output."""
ell_axes = EllipsisAxes()
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(1,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim, x_dim)))]:
return self.backend.grad.hessian(u, x)
[docs]
class MatrixDivergence(Node[TensorType]):
"""Computes the row-wise divergence of a matrix-valued field.
The output is a vector containing the divergence of each row of the
matrix field with respect to the spatial input.
"""
ell_axes = EllipsisAxes()
u_dim = AxesDim(None)
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(u_dim, x_dim)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(u_dim,)))]:
return self.backend.grad.matrix_divergence(u, x)
[docs]
class Rotation(Node[TensorType]):
"""Computes the curl/rotation of a 3D vector field."""
ell_axes = EllipsisAxes()
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(3,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(3,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(3,)))]:
return self.backend.grad.rotation(u, x)
[docs]
class SymmetricGradient(Node[TensorType]):
"""Computes the symmetric gradient 0.5 * (∇u + (∇u)^T)."""
ell_axes = EllipsisAxes()
u_dim = AxesDim(None)
x_dim = AxesDim(None)
[docs]
def forward(
self,
u: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(u_dim,)))],
x: Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(x_dim,)))],
) -> Annotated[TensorType, DC(ell_axes, FeatureAxes(shape=(u_dim, x_dim)))]:
return self.backend.grad.symmetric_gradient(u, x)