"""Independent implementation of Khademi & Dufour (2025), Eqs. 17--22, 50--51. Coordinates are physical nondimensional (x, y, t); output is (psi, p). Both reverse-mode and exact Taylor-mode coordinate AD are implemented. The Taylor path propagates derivatives, not finite differences, and remains fully differentiable with respect to every network parameter through PyTorch AD. """ from __future__ import annotations import torch from torch import nn from torch.nn import functional as F class TSAPINN(nn.Module): def __init__(self, hidden_layers=(50, 50, 50, 50), sigma=1.0, zeta_sin=0.5, zeta_cos=0.5, reynolds=100.0, bias_initialization="glorot_normal"): super().__init__() widths = [3, *hidden_layers, 2] self.layers = nn.ModuleList(nn.Linear(a, b) for a, b in zip(widths, widths[1:])) self.frequencies = nn.ParameterList( nn.Parameter(torch.full((width,), float(sigma))) for width in hidden_layers ) self.zeta_sin, self.zeta_cos = float(zeta_sin), float(zeta_cos) self.reynolds = float(reynolds) for layer in self.layers: nn.init.xavier_normal_(layer.weight) if bias_initialization == "glorot_normal": nn.init.normal_(layer.bias, std=(2 / (layer.in_features + layer.out_features)) ** 0.5) elif bias_initialization == "zeros": nn.init.zeros_(layer.bias) else: raise ValueError(f"Unknown bias initialization: {bias_initialization}") def forward(self, coordinates): if coordinates.ndim != 2 or coordinates.shape[1] != 3: raise ValueError("Expected coordinates [N,3] in x,y,t order") h = coordinates for layer, frequency in zip(self.layers[:-1], self.frequencies): # Paper Eq. 17: frequency multiplies the ENTIRE affine preactivation. phase = layer(h) * frequency h = self.zeta_sin * torch.sin(phase) + self.zeta_cos * torch.cos(phase) return self.layers[-1](h) def slope_recovery(self): # Exact Eq. 22, including the average over hidden layers. means = torch.stack([f.mean() for f in self.frequencies]) return torch.exp(-torch.logsumexp(means, 0) + means.new_tensor(len(means)).log()) @staticmethod def _grad(output, coordinates, create_graph=True): return torch.autograd.grad(output, coordinates, torch.ones_like(output), create_graph=create_graph, retain_graph=True)[0] def flow(self, coordinates, create_graph=True): """Velocity via u=psi_y, v=-psi_x; pressure is uncalibrated up to gauge.""" with torch.enable_grad(): if not coordinates.requires_grad: coordinates = coordinates.detach().requires_grad_(True) output = self(coordinates) gradient = self._grad(output[:, :1], coordinates, create_graph) return torch.cat((gradient[:, 1:2], -gradient[:, :1], output[:, 1:2]), 1) def residual_autograd(self, coordinates): """Reference reverse-mode AD implementation of paper Eqs. 50--51.""" with torch.enable_grad(): q = coordinates.detach().requires_grad_(True) values = self.flow(q, create_graph=True) u, v, p = values.split(1, 1) du, dv, dp = (self._grad(f, q) for f in (u, v, p)) uxx = self._grad(du[:, :1], q)[:, :1] uyy = self._grad(du[:, 1:2], q)[:, 1:2] vxx = self._grad(dv[:, :1], q)[:, :1] vyy = self._grad(dv[:, 1:2], q)[:, 1:2] ru = du[:, 2:3] + u * du[:, :1] + v * du[:, 1:2] + dp[:, :1] - (uxx + uyy) / self.reynolds rv = dv[:, 2:3] + u * dv[:, :1] + v * dv[:, 1:2] + dp[:, 1:2] - (vxx + vyy) / self.reynolds return values, torch.cat((ru, rv), 1), du[:, :1] + dv[:, 1:2] def coordinate_jet(self, coordinates, order=3): """Forward Taylor-mode AD with unscaled partials (no factorial factors). Jet order: value,x,y,t,xx,xy,yy,xt,yt,xxx,xxy,xyy,yyy. Only derivatives required by the 2D streamfunction NS equations propagate. The third-order multivariate chain rule is exact. Linear-layer derivatives are batched in one GEMM; no coordinate discretization is introduced. """ count = 4 if order == 1 else 13 initial = coordinates.new_zeros((count, *coordinates.shape)) initial[0] = coordinates for axis in range(3): initial[axis + 1, :, axis] = 1 jet = initial for layer, frequency in zip(self.layers[:-1], self.frequencies): linear = F.linear(jet, layer.weight) * frequency phase = linear[0] + layer.bias * frequency s, c = torch.sin(phase), torch.cos(phase) a = self.zeta_sin * s + self.zeta_cos * c d1 = self.zeta_sin * c - self.zeta_cos * s d2, d3 = -a, -d1 x, y, t = linear[1:4].unbind(0) parts = [a, d1 * x, d1 * y, d1 * t] if order == 3: xx, xy, yy, xt, yt = linear[4:9].unbind(0) xxx, xxy, xyy, yyy = linear[9:13].unbind(0) parts.extend(( d1 * xx + d2 * x * x, d1 * xy + d2 * x * y, d1 * yy + d2 * y * y, d1 * xt + d2 * x * t, d1 * yt + d2 * y * t, d1 * xxx + 3 * d2 * xx * x + d3 * x**3, d1 * xxy + d2 * (xx * y + 2 * xy * x) + d3 * x * x * y, d1 * xyy + d2 * (yy * x + 2 * xy * y) + d3 * x * y * y, d1 * yyy + 3 * d2 * yy * y + d3 * y**3, )) jet = torch.stack(parts) result = F.linear(jet, self.layers[-1].weight) # Bias only contributes to the zeroth derivative. return torch.cat((result[:1] + self.layers[-1].bias, result[1:]), dim=0) def flow_taylor(self, coordinates): jet = self.coordinate_jet(coordinates, order=1) return torch.stack((jet[2, :, 0], -jet[1, :, 0], jet[0, :, 1]), 1) def residual_taylor(self, coordinates): j = self.coordinate_jet(coordinates) u, v = j[2, :, 0], -j[1, :, 0] ru = j[8, :, 0] + u * j[5, :, 0] + v * j[6, :, 0] + j[1, :, 1] - (j[10, :, 0] + j[12, :, 0]) / self.reynolds rv = -j[7, :, 0] - u * j[4, :, 0] - v * j[5, :, 0] + j[2, :, 1] + (j[9, :, 0] + j[11, :, 0]) / self.reynolds return torch.stack((u, v, j[0, :, 1]), 1), torch.stack((ru, rv), 1), torch.zeros_like(u[:, None])