File size: 2,331 Bytes
cc3506e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Common interface every surrogate architecture plugs into.

The dashboard only talks to this interface. To wire in a real model,
subclass Surrogate, implement load() and the predict methods for the
dataset(s) it supports, and register it in registry.py.
"""
from __future__ import annotations

import os
from abc import ABC, abstractmethod
from dataclasses import dataclass, field

import numpy as np


@dataclass
class ModelInfo:
    name: str                     # display name, e.g. "FNO"
    supports_d1: bool             # Dataset 1 (AT2, perforated plate)
    supports_d2: bool             # Dataset 2 (PF-CZM, FGM plate)
    inference_note: str = ""      # e.g. "~0.4 s/sample" or "slow: 500 DDPM steps"
    # Benchmark numbers from the manuscript (shown in the UI tables)
    d1_min_mse: dict = field(default_factory=dict)   # {11: ..., 22: ..., 44: ...} x1e-6
    d2_mean_mse: float | None = None                 # x1e-4


class Surrogate(ABC):
    """One trained architecture, possibly with separate weights per dataset."""

    info: ModelInfo

    def __init__(self, checkpoint_dir: str):
        self.checkpoint_dir = checkpoint_dir
        self._loaded = False

    # ------------------------------------------------------------------
    @abstractmethod
    def load(self) -> None:
        """Load checkpoints/weights. Called once, lazily, then cached."""

    def ensure_loaded(self) -> None:
        if not self._loaded:
            from .weights import fetch_missing
            fetch_missing(os.path.basename(os.path.normpath(self.checkpoint_dir)))
            self.load()
            self._loaded = True

    # ------------------------------------------------------------------
    # Dataset 1: u(x,0) -> d(x,t), vector phase field over N load steps
    def predict_d1(self, u0: np.ndarray, n_steps: int) -> np.ndarray:
        """u0: (128,128,2) initial displacement field (normalized).
        Returns d: (n_steps,128,128,2)."""
        raise NotImplementedError(f"{self.info.name} has no Dataset-1 weights")

    # Dataset 2: (d(x,0), E(x)) -> d(x,tf), scalar field at final step
    def predict_d2(self, d0: np.ndarray, E: np.ndarray) -> np.ndarray:
        """d0, E: (256,128) each (normalized). Returns d_tf: (256,128)."""
        raise NotImplementedError(f"{self.info.name} has no Dataset-2 weights")