|
| 1 | +import argparse |
| 2 | +import os |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import torch |
| 6 | +from matplotlib import pyplot as plt |
| 7 | + |
| 8 | +import nnopinf |
| 9 | +import nnopinf.models as models |
| 10 | +import nnopinf.training |
| 11 | +import nnopinf.training.trainers |
| 12 | + |
| 13 | + |
| 14 | +def laplacian_dirichlet(u, n, h): |
| 15 | + """Five-point Laplacian on an n x n interior grid with zero Dirichlet boundaries.""" |
| 16 | + u2d = u.reshape(n, n) |
| 17 | + upad = np.pad(u2d, ((1, 1), (1, 1)), mode="constant") |
| 18 | + lap = ( |
| 19 | + upad[2:, 1:-1] |
| 20 | + + upad[:-2, 1:-1] |
| 21 | + + upad[1:-1, 2:] |
| 22 | + + upad[1:-1, :-2] |
| 23 | + - 4.0 * upad[1:-1, 1:-1] |
| 24 | + ) / (h**2) |
| 25 | + return lap.reshape(-1) |
| 26 | + |
| 27 | + |
| 28 | +class HeatDiffusionFOM: |
| 29 | + def __init__(self, n, forcing_value): |
| 30 | + self.n = n |
| 31 | + self.h = 1.0 / (n + 1) |
| 32 | + self.x = np.linspace(self.h, 1.0 - self.h, n) |
| 33 | + xx, yy = np.meshgrid(self.x, self.x, indexing="ij") |
| 34 | + self.u0 = ( |
| 35 | + np.sin(np.pi * xx) * np.sin(np.pi * yy) |
| 36 | + + 0.25 * np.sin(2.0 * np.pi * xx) * np.sin(np.pi * yy) |
| 37 | + ).reshape(-1) |
| 38 | + self.forcing = forcing_value * np.ones(self.n * self.n) |
| 39 | + |
| 40 | + def velocity(self, u, kappa): |
| 41 | + return kappa * laplacian_dirichlet(u, self.n, self.h) + self.forcing |
| 42 | + |
| 43 | + def solve(self, dt, end_time, kappa): |
| 44 | + u = self.u0.copy() |
| 45 | + t = 0.0 |
| 46 | + rk4const = np.array([1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 1.0]) |
| 47 | + snapshots = [] |
| 48 | + time = [] |
| 49 | + while t <= end_time + 0.5 * dt: |
| 50 | + snapshots.append(u.copy()) |
| 51 | + time.append(t) |
| 52 | + u0 = u.copy() |
| 53 | + for i in range(4): |
| 54 | + f = self.velocity(u, kappa) |
| 55 | + u = u0 + dt * rk4const[i] * f |
| 56 | + t += dt |
| 57 | + return np.array(snapshots).T, np.array(time) |
| 58 | + |
| 59 | + |
| 60 | +class NnOpInfRom: |
| 61 | + def __init__(self, model): |
| 62 | + self.model_ = model |
| 63 | + self.inputs_ = {} |
| 64 | + |
| 65 | + def velocity(self, u): |
| 66 | + self.inputs_["x"] = torch.tensor(u[None], dtype=torch.float64) |
| 67 | + return self.model_.forward(self.inputs_)[0].detach().numpy() |
| 68 | + |
| 69 | + def solve(self, u0, dt, end_time): |
| 70 | + u = u0.copy() |
| 71 | + t = 0.0 |
| 72 | + rk4const = np.array([1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 1.0]) |
| 73 | + snapshots = [] |
| 74 | + time = [] |
| 75 | + while t <= end_time + 0.5 * dt: |
| 76 | + snapshots.append(u.copy()) |
| 77 | + time.append(t) |
| 78 | + u0_l = u.copy() |
| 79 | + for i in range(4): |
| 80 | + f = self.velocity(u) |
| 81 | + u = u0_l + dt * rk4const[i] * f |
| 82 | + t += dt |
| 83 | + return np.array(snapshots).T, np.array(time) |
| 84 | + |
| 85 | + |
| 86 | +def flatten_for_training(q): |
| 87 | + return np.reshape(q, (q.shape[0], q.shape[1] * q.shape[2])).T |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + parser = argparse.ArgumentParser( |
| 92 | + description="Transient heat diffusion on the unit square with a linear SPD operator." |
| 93 | + ) |
| 94 | + parser.add_argument("--grid-size", type=int, default=32, help="Interior points per direction.") |
| 95 | + parser.add_argument("--dt", type=float, default=2.0e-4, help="Time step for RK4.") |
| 96 | + parser.add_argument("--end-time", type=float, default=1.0, help="Final simulation time.") |
| 97 | + parser.add_argument("--rom-dim", type=int, default=20, help="Requested POD rank.") |
| 98 | + parser.add_argument("--kappa", type=float, default=0.75, help="Diffusion coefficient.") |
| 99 | + parser.add_argument("--forcing", type=float, default=1.0, help="Constant forcing amplitude.") |
| 100 | + parser.add_argument("--num-epochs", type=int, default=500, help="Training epochs.") |
| 101 | + parser.add_argument("--tr-delta0", type=float, default=1.0, help="Initial trust-region radius.") |
| 102 | + parser.add_argument("--tr-cg-max-iters", type=int, default=200, help="Maximum CG iterations per TR step.") |
| 103 | + parser.add_argument("--tr-batch-size", type=int, default=50, help="Batch size used by TR optimizer.") |
| 104 | + args = parser.parse_args() |
| 105 | + |
| 106 | + torch.manual_seed(1) |
| 107 | + np.random.seed(1) |
| 108 | + |
| 109 | + output_dir = os.path.dirname(os.path.abspath(__file__)) |
| 110 | + model_dir = os.path.join(output_dir, "ml-models") |
| 111 | + os.makedirs(model_dir, exist_ok=True) |
| 112 | + |
| 113 | + fom = HeatDiffusionFOM(args.grid_size, args.forcing) |
| 114 | + u, _ = fom.solve(args.dt, args.end_time, args.kappa) |
| 115 | + snapshots_all = u[..., None] |
| 116 | + |
| 117 | + # POD basis from all training trajectories. |
| 118 | + snapshots_matrix = np.reshape( |
| 119 | + snapshots_all, (snapshots_all.shape[0], snapshots_all.shape[1] * snapshots_all.shape[2]) |
| 120 | + ) |
| 121 | + phi, singular_values, _ = np.linalg.svd(snapshots_matrix, full_matrices=False) |
| 122 | + relative_energy = np.cumsum(singular_values**2) / np.sum(singular_values**2) |
| 123 | + pod_rank = np.searchsorted(relative_energy, 0.999999999) + 1 |
| 124 | + rom_dim = min(args.rom_dim, int(pod_rank)) |
| 125 | + phi = phi[:, :rom_dim] |
| 126 | + print(f"Using ROM dimension K={rom_dim}") |
| 127 | + |
| 128 | + # Reduced snapshots and time derivatives. |
| 129 | + uhat = np.einsum("ij,ikn->jkn", phi, snapshots_all) |
| 130 | + uhat_dot = (uhat[:, 2:, :] - uhat[:, :-2, :]) / (2.0 * args.dt) |
| 131 | + uhat = uhat[:, 1:-1, :] |
| 132 | + |
| 133 | + x_input = nnopinf.variables.Variable(size=rom_dim, name="x", normalization_strategy="MaxAbs") |
| 134 | + target = nnopinf.variables.Variable(size=rom_dim, name="y", normalization_strategy="MaxAbs") |
| 135 | + |
| 136 | + diffusion_operator = nnopinf.operators.LinearAffineSpdTensorOperator( |
| 137 | + acts_on=x_input, depends_on=(), positive=False |
| 138 | + ) |
| 139 | + forcing_operator = nnopinf.operators.VectorOffsetOperator(n_outputs=rom_dim) |
| 140 | + model = models.Model([diffusion_operator, forcing_operator]) |
| 141 | + |
| 142 | + training_settings = nnopinf.training.get_default_settings() |
| 143 | + training_settings["output-path"] = model_dir |
| 144 | + training_settings["optimizer"] = "ADAM" |
| 145 | + training_settings["batch-size"] = 5000 |
| 146 | + training_settings["num-epochs"] = args.num_epochs |
| 147 | + training_settings["weight-decay"] = 1.0e-6 |
| 148 | + training_settings["LBFGS-acceleration"] = True |
| 149 | + |
| 150 | + x_input.set_data(flatten_for_training(uhat)) |
| 151 | + target.set_data(flatten_for_training(uhat_dot)) |
| 152 | + |
| 153 | + print("Training linear SPD diffusion model") |
| 154 | + nnopinf.training.trainers.train( |
| 155 | + model, variables=[x_input], y=target, training_settings=training_settings |
| 156 | + ) |
| 157 | + |
| 158 | + # Evaluate for the same diffusion coefficient used to generate training data. |
| 159 | + test_kappa = args.kappa |
| 160 | + u_fom, t = fom.solve(args.dt, args.end_time, test_kappa) |
| 161 | + u0_r = phi.T @ fom.u0 |
| 162 | + |
| 163 | + rom = NnOpInfRom(model) |
| 164 | + u_rom_r, _ = rom.solve(u0_r, args.dt, args.end_time) |
| 165 | + u_rom = phi @ u_rom_r |
| 166 | + |
| 167 | + relative_error = np.linalg.norm(u_rom - u_fom) / np.linalg.norm(u_fom) |
| 168 | + print(f"Relative trajectory error (kappa={test_kappa:.3f}): {relative_error:.4e}") |
| 169 | + |
| 170 | + u_fom_final = u_fom[:, -1].reshape(args.grid_size, args.grid_size) |
| 171 | + u_rom_final = u_rom[:, -1].reshape(args.grid_size, args.grid_size) |
| 172 | + diff_final = np.abs(u_fom_final - u_rom_final) |
| 173 | + |
| 174 | + fig, axes = plt.subplots(1, 3, figsize=(12, 3.8), constrained_layout=True) |
| 175 | + extent = (0.0, 1.0, 0.0, 1.0) |
| 176 | + vmin = min(np.min(u_fom_final), np.min(u_rom_final)) |
| 177 | + vmax = max(np.max(u_fom_final), np.max(u_rom_final)) |
| 178 | + |
| 179 | + im0 = axes[0].imshow(u_fom_final, origin="lower", extent=extent, cmap="viridis", vmin=vmin, vmax=vmax) |
| 180 | + axes[0].set_title("FOM final state") |
| 181 | + axes[0].set_xlabel("x") |
| 182 | + axes[0].set_ylabel("y") |
| 183 | + |
| 184 | + im1 = axes[1].imshow(u_rom_final, origin="lower", extent=extent, cmap="viridis", vmin=vmin, vmax=vmax) |
| 185 | + axes[1].set_title("NN-OpInf final state") |
| 186 | + axes[1].set_xlabel("x") |
| 187 | + axes[1].set_ylabel("y") |
| 188 | + |
| 189 | + im2 = axes[2].imshow(diff_final, origin="lower", extent=extent, cmap="magma") |
| 190 | + axes[2].set_title("Absolute error") |
| 191 | + axes[2].set_xlabel("x") |
| 192 | + axes[2].set_ylabel("y") |
| 193 | + |
| 194 | + fig.colorbar(im0, ax=axes[0], fraction=0.046, pad=0.04) |
| 195 | + fig.colorbar(im1, ax=axes[1], fraction=0.046, pad=0.04) |
| 196 | + fig.colorbar(im2, ax=axes[2], fraction=0.046, pad=0.04) |
| 197 | + fig.suptitle(f"Transient heat diffusion, kappa={test_kappa:.2f}, rel. error={relative_error:.2e}") |
| 198 | + |
| 199 | + fig.savefig(os.path.join(output_dir, "heat_diffusion_solution.pdf")) |
| 200 | + fig.savefig(os.path.join(output_dir, "heat_diffusion_solution.png"), dpi=200) |
| 201 | + plt.close(fig) |
| 202 | + |
| 203 | + |
| 204 | +if __name__ == "__main__": |
| 205 | + main() |
0 commit comments