| |
| |
|
|
| |
|
|
|
|
| import numpy as np |
| import scipy.io |
|
|
| f = "../data/piececonst_r241_N1024_smooth1.mat" |
|
|
| mat = scipy.io.loadmat(f) |
|
|
|
|
| |
|
|
|
|
| kcoeff = mat["Kcoeff"] |
| kcoeff_x = mat["Kcoeff_x"] |
| kcoeff_y = mat["Kcoeff_y"] |
| sol = mat["sol"] |
| coeff = mat["coeff"] |
|
|
| print(kcoeff.shape, "kcoeff") |
| print(kcoeff_x.shape, "kcoeff x") |
| print(kcoeff_y.shape, "kcoeff y") |
| print(sol.shape, "sol") |
| print(coeff.shape, "coeff") |
|
|
|
|
| |
|
|
|
|
| import matplotlib.pyplot as plt |
|
|
| idx = 0 |
| plt.imshow(kcoeff[idx]) |
| plt.title("kcoeff") |
| plt.colorbar() |
| plt.show() |
|
|
| plt.imshow(kcoeff_y[idx]) |
| plt.title("kcoeff_y") |
| plt.colorbar() |
| plt.show() |
|
|
| plt.imshow(kcoeff_x[idx]) |
| plt.title("kcoeff_x") |
| plt.colorbar() |
| plt.show() |
|
|
| plt.imshow(sol[idx]) |
| plt.title("sol") |
| plt.colorbar() |
| plt.show() |
|
|
| plt.imshow(coeff[idx]) |
| plt.title("coeff") |
| plt.colorbar() |
| plt.show() |
|
|
|
|
| |
|
|
|
|
| def darcy_residual(u, a): |
| |
| |
| |
| forcing_func = np.ones_like(u) |
| D = 1 |
| size = u.shape[0] |
| dx = D / size |
| dy = dx |
|
|
| ux = np.gradient(u, dx, axis=0) |
| uy = np.gradient(u, dy, axis=1) |
|
|
| aux = a * ux |
| auy = a * uy |
|
|
| auxx = np.gradient(aux, dx, axis=0) |
| auyy = np.gradient(auy, dy, axis=1) |
| lhs = -(auxx + auyy) |
| return lhs - forcing_func |
|
|
|
|
| residual = darcy_residual(sol[idx], kcoeff[idx]) |
|
|
|
|
| |
|
|
|
|
| residual.shape |
|
|
|
|
| |
|
|
|
|
| plt.imshow(residual) |
| plt.colorbar() |
|
|
|
|
| |
|
|
|
|
| residual.sum(), residual.mean(), np.linalg.norm(residual) |
|
|
|
|
| |
|
|
|
|
| |
| def batched_darcy_residual(u, a): |
| |
| |
| forcing_func = np.ones_like(u) |
| D = 1 |
| bsize = u.shape[0] |
| size = u.shape[1] |
| dx = D / size |
| dy = dx |
|
|
| ux = np.gradient(u, dx, axis=1) |
| uy = np.gradient(u, dy, axis=2) |
|
|
| aux = a * ux |
| auy = a * uy |
|
|
| auxx = np.gradient(aux, dx, axis=1) |
| auyy = np.gradient(auy, dy, axis=2) |
| lhs = -(auxx + auyy) |
| return lhs - forcing_func |
|
|
|
|
| def corrupt_a(a, p: float): |
| max_val = a.max().item() |
| min_val = a.min().item() |
| ax = np.gradient(a, axis=0) |
| ay = np.gradient(a, axis=1) |
| field = ax + ay |
|
|
| |
| non_zero_indices = np.argwhere(field != 0) |
|
|
| |
| offsets = np.array( |
| [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]] |
| ) |
|
|
| |
| neighbor_indices = non_zero_indices[:, None, :] + offsets[None, :, :] |
|
|
| |
| neighbor_indices = neighbor_indices.reshape(-1, 2) |
|
|
| |
| valid_mask = ( |
| (neighbor_indices[:, 0] >= 0) |
| & (neighbor_indices[:, 0] < field.shape[0]) |
| & (neighbor_indices[:, 1] >= 0) |
| & (neighbor_indices[:, 1] < field.shape[1]) |
| ) |
| valid_neighbors = neighbor_indices[valid_mask] |
|
|
| |
| updated_field = a.copy() |
|
|
| |
| rand_vals = np.random.rand(valid_neighbors.shape[0]) |
| valid_neighbors = valid_neighbors[rand_vals < p] |
|
|
| |
| valid_neighbors = np.random.permutation(valid_neighbors) |
| size = valid_neighbors.shape[0] // 2 |
| updated_field[valid_neighbors[size:, 0], valid_neighbors[size:, 1]] = max_val |
| updated_field[valid_neighbors[:size, 0], valid_neighbors[:size, 1]] = min_val |
|
|
| return updated_field |
|
|
|
|
| |
|
|
|
|
| from tqdm import tqdm |
|
|
| corruptions = np.linspace(0, 1, 20, endpoint=True) |
| print("Corruption vals", corruptions) |
| residual_corruption_norm = {c: [] for c in corruptions} |
| residual_corruption_mean = {c: [] for c in corruptions} |
| for i in tqdm(range(sol.shape[0])): |
| u = sol[i] |
| a = coeff[i] |
| for c in corruptions: |
| new_a = corrupt_a(a, p=c) |
| residual = darcy_residual(u, new_a) |
| residual_corruption_norm[c].append(np.linalg.norm(residual)) |
| residual_corruption_mean[c].append(np.mean(residual)) |
|
|
|
|
| |
|
|
|
|
| for k in residual_corruption_norm.keys(): |
| residual_corruption_norm[k] = np.asarray(residual_corruption_norm[k]) |
| residual_corruption_mean[k] = np.asarray(residual_corruption_mean[k]) |
|
|
|
|
| |
|
|
|
|
| plt.plot( |
| residual_corruption_norm.keys(), |
| [residual_corruption_norm[k].mean() for k in residual_corruption_norm.keys()], |
| ) |
| plt.title("Norm based comparison") |
| plt.xlabel("Percentage of corruption") |
| plt.ylabel("Mean of residual norm values") |
| plt.show() |
| plt.plot( |
| residual_corruption_mean.keys(), |
| [residual_corruption_mean[k].mean() for k in residual_corruption_mean.keys()], |
| ) |
| plt.title("Mean based comparison") |
| plt.xlabel("Percentage of corruption") |
| plt.ylabel("Mean of Mean residual values") |
| plt.show() |
|
|
|
|
| |
|
|
|
|
| plt.imshow(corrupt_a(coeff[idx], p=0.0001)) |
| plt.show() |
|
|
|
|
| |
|
|