| 1 | import os |
| 2 | from pathlib import Path |
| 3 | |
| 4 | import matplotlib.pyplot as plt |
| 5 | import torch |
| 6 | from mat_to_numpy import load_mat, snapshot |
| 7 | |
| 8 | image_dir = Path(os.environ["IMAGE_DIR"]) |
| 9 | image_dir.mkdir(parents=True, exist_ok=True) |
| 10 | |
| 11 | fields = load_mat() |
| 12 | times = fields["T"][0] |
| 13 | |
| 14 | print("1) snapshot(fields, index): 한 시각의 모든 점을 텐서 두 개로") |
| 15 | inputs, targets = snapshot(fields, 0) |
| 16 | print(f" inputs : {tuple(inputs.shape)} {inputs.dtype} ← 열 순서 (t, x, y)") |
| 17 | print(f" targets : {tuple(targets.shape)} {targets.dtype} ← 열 순서 (c, u, v, p)") |
| 18 | print(f" inputs[:3] =\n{inputs[:3]}") |
| 19 | print(f" targets[:3] =\n{targets[:3]}") |
| 20 | print( |
| 21 | f" t 열에 든 값의 종류: {[round(t, 2) for t in inputs[:, 0].unique().tolist()]} ← 한 시각이므로 한 가지뿐" |
| 22 | ) |
| 23 | print() |
| 24 | |
| 25 | print("2) index = -1 은 마지막 시각") |
| 26 | inputs, targets = snapshot(fields, -1) |
| 27 | print(f" t = {inputs[0, 0].item():.2f} (시각 목록의 마지막 값 {times[-1]:.2f})") |
| 28 | print() |
| 29 | |
| 30 | print("3) 마지막 시각에서 물리량마다 평균과 표준편차") |
| 31 | for i, name in enumerate("cuvp"): |
| 32 | col = targets[:, i] |
| 33 | print( |
| 34 | f" {name}: 평균 {col.mean().item():7.3f} 표준편차 {col.std().item():.3f} " |
| 35 | f"범위 [{col.min().item():7.3f}, {col.max().item():6.3f}]" |
| 36 | ) |
| 37 | print(" → u 는 평균이 0.7 근처다. 유체가 대체로 +x 방향(왼쪽에서 오른쪽)으로 흐른다는 뜻이다") |
| 38 | print() |
| 39 | |
| 40 | print("4) 마지막 시각의 네 물리량을 그림으로 (4개 중 1개 점만)") |
| 41 | keep = torch.arange(0, len(inputs), 4) |
| 42 | x, y = inputs[keep, 1].numpy(), inputs[keep, 2].numpy() |
| 43 | titles = ["c (observed)", "u (hidden)", "v (hidden)", "p (hidden)"] |
| 44 | fig, axes = plt.subplots(2, 2, figsize=(8, 6.4)) |
| 45 | for ax, i, title in zip(axes.flat, range(4), titles): |
| 46 | sc = ax.scatter(x, y, c=targets[keep, i].numpy(), cmap="jet", s=1) |
| 47 | ax.set_title(title) |
| 48 | ax.set_aspect("equal") |
| 49 | fig.colorbar(sc, ax=ax, shrink=0.8) |
| 50 | fig.suptitle(f"t = {times[-1]:.2f}") |
| 51 | fig.tight_layout() |
| 52 | fig.savefig(image_dir / "fields.png", dpi=100, bbox_inches="tight") |
| 53 | plt.close(fig) |
| 54 | print("[[image:fields]]") |
| 55 | print(" → 학습에 쓰는 것은 왼쪽 위(c) 하나다. 나머지 셋은 채점할 때만 꺼낸다") |
| 56 | print() |
| 57 | |
| 58 | print("5) 농도 c 가 시간에 따라 변하는 모습 (시각 11개 중 4개)") |
| 59 | picks = [0, 3, 7, 10] |
| 60 | fig, axes = plt.subplots(1, 4, figsize=(8, 2.4)) |
| 61 | for ax, j in zip(axes, picks): |
| 62 | inp, tgt = snapshot(fields, j) |
| 63 | ax.scatter( |
| 64 | inp[keep, 1].numpy(), |
| 65 | inp[keep, 2].numpy(), |
| 66 | c=tgt[keep, 0].numpy(), |
| 67 | cmap="jet", |
| 68 | s=1, |
| 69 | vmin=0.5, |
| 70 | vmax=1.0, |
| 71 | ) |
| 72 | ax.set_title(f"c at t = {times[j]:.2f}", fontsize=9) |
| 73 | ax.set_aspect("equal") |
| 74 | ax.set_xticks([]) |
| 75 | ax.set_yticks([]) |
| 76 | fig.tight_layout() |
| 77 | fig.savefig(image_dir / "c_over_time.png", dpi=100, bbox_inches="tight") |
| 78 | plt.close(fig) |
| 79 | print("[[image:c_over_time]]") |
| 80 | print(" → 농도 무늬가 흐름을 타고 움직인다. 이 움직임 속에 속도에 대한 단서가 들어 있다") |