HaomingLuo's picture
Release T4 modal neural state-space v0.1.0
522e396 verified
Raw History Blame Contribute Delete
17 kB
"""Train the T4 physics-conditioned modal neural state-space model.
The model keeps the stable second-order oscillator update explicit. A small
conditioner learns how structural parameters map modal responses into a shared
POD field basis. Splits are inherited from the finite-element campaign at the
configuration level, so four load histories from one structure never cross a
train/test boundary.
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import random
import sys
import h5py
import numpy as np
import torch
from torch import nn
DEFAULT_DATA = Path(
"/Users/luo/Desktop/科研/我的科研工作/AgentFEM-Physics-Data/"
"data/t4_structural_dynamics_v1/cases"
)
PARAMETER_NAMES = (
"length_m",
"height_m",
"young_pa",
"density_kg_m3",
"damping_ratio",
"traction_amplitude_pa",
)
CONDITIONER_FEATURE_NAMES = ("log_length", "log_height", "log_slenderness")
def parameters(config: dict) -> np.ndarray:
return np.asarray(
[
config["geometry"]["length_m"],
config["geometry"]["height_m"],
config["material"]["young_pa"],
config["material"]["density_kg_m3"],
config["dynamics"]["target_damping_ratio"],
config["dynamics"]["traction_amplitude_pa"],
],
dtype=np.float64,
)
def modal_force_scale(parameter: np.ndarray) -> float:
"""Euler--Bernoulli force/mass scale for the tip-normalized first mode."""
length = float(parameter[0])
density = float(parameter[3])
traction = float(parameter[5])
return traction / (density * length)
def conditioner_coordinates(parameter: np.ndarray) -> np.ndarray:
"""Coordinates left after known force, mass, damping and modal physics."""
length = float(parameter[0])
height = float(parameter[1])
return np.log(np.asarray([length, height, length / height], dtype=np.float64))
def case_paths(data: Path) -> list[Path]:
selected = sorted(data.glob("config_*.h5"))
if len(selected) != 128:
raise RuntimeError(f"Expected 128 corrected T4 cases, found {len(selected)}")
return selected
def check_case(path: Path) -> None:
with h5py.File(path, "r") as h5:
if h5.attrs.get("schema_version") != "1.1.0":
raise RuntimeError(f"{path.name} is not corrected T4 schema 1.1.0")
if h5.attrs.get("generator_fix") != "component-preserving-kinematic-bcs":
raise RuntimeError(f"{path.name} lacks the kinematic-BC correction seal")
def split(path: Path) -> tuple[str, str]:
with h5py.File(path, "r") as h5:
return str(h5.attrs["split"]), str(h5.attrs["protocol_role"])
def field_matrix(path: Path):
with h5py.File(path, "r") as h5:
for name in sorted(h5["trajectories"]):
values = np.asarray(
h5[f"trajectories/{name}/fields/displacement_m"], dtype=np.float64
)[:, :, :2]
yield values.reshape(len(values), -1)
def pod_basis(train_paths: list[Path], rank: int) -> tuple[np.ndarray, float]:
values = np.vstack(
[field.astype(np.float32, copy=False) for path in train_paths for field in field_matrix(path)]
)
total = float(np.sum(values.astype(np.float64) ** 2))
rng = np.random.default_rng(20260927)
omega = rng.normal(size=(values.shape[1], rank + 8)).astype(np.float32)
projected = values @ omega
# One power iteration is sufficient because T4 is strongly modal.
projected = values @ (values.T @ projected)
basis, _ = np.linalg.qr(projected, mode="reduced")
small = basis.T @ values
_, singular, vectors = np.linalg.svd(small, full_matrices=False)
selected = vectors[:rank].astype(np.float64)
# Make the basis orientation deterministic and tip-positive where possible.
for index in range(rank):
pivot = int(np.argmax(np.abs(selected[index])))
if selected[index, pivot] < 0.0:
selected[index] *= -1.0
explained = float(np.sum(singular[:rank].astype(np.float64) ** 2) / total)
return selected, explained
def newmark_unit_responses(
force: np.ndarray,
frequencies_hz: np.ndarray,
alpha: float,
beta_rayleigh: float,
dt: float,
) -> np.ndarray:
"""Unit modal responses using the same average-acceleration update as T4."""
omega = 2.0 * np.pi * np.asarray(frequencies_hz, dtype=np.float64)
stiffness = omega**2
damping = alpha + beta_rayleigh * stiffness
count = len(force)
modes = len(omega)
q = np.zeros((count, modes), dtype=np.float64)
v = np.zeros_like(q)
a = np.zeros_like(q)
a[0] = force[0]
effective = 1.0 + 0.5 * dt * damping + 0.25 * dt**2 * stiffness
for index in range(1, count):
q_predictor = q[index - 1] + dt * v[index - 1] + 0.25 * dt**2 * a[index - 1]
v_predictor = v[index - 1] + 0.5 * dt * a[index - 1]
a[index] = (
force[index] - damping * v_predictor - stiffness * q_predictor
) / effective
q[index] = q_predictor + 0.25 * dt**2 * a[index]
v[index] = v_predictor + 0.5 * dt * a[index]
return q
def case_system(path: Path, pod: np.ndarray) -> dict:
with h5py.File(path, "r") as h5:
config = json.loads(h5.attrs["config_json"])
frequencies = np.asarray(h5["modal/frequencies_hz"], dtype=np.float64)[:4]
responses = []
targets = []
outputs = []
names = []
for name in sorted(h5["trajectories"]):
group = h5[f"trajectories/{name}"]
force = np.asarray(group["force_scale"], dtype=np.float64)
dt = float(group.attrs["dt_s"])
alpha = float(group.attrs["rayleigh_mass_coefficient_per_s"])
beta_rayleigh = float(group.attrs["rayleigh_stiffness_coefficient_s"])
modal_response = newmark_unit_responses(
force, frequencies, alpha, beta_rayleigh, dt
)
field_time = np.asarray(group["fields/time_s"], dtype=np.float64)
indices = np.rint(field_time / dt).astype(int)
response = modal_response[indices]
field = np.asarray(group["fields/displacement_m"], dtype=np.float64)[
:, :, :2
].reshape(len(indices), -1)
responses.append(response)
targets.append(field @ pod.T)
outputs.append(field)
names.append(name)
response_matrix = np.vstack(responses)
target_matrix = np.vstack(targets)
gain, *_ = np.linalg.lstsq(response_matrix, target_matrix, rcond=1.0e-12)
return {
"path": path,
"case_id": str(h5.attrs["case_id"]),
"split": str(h5.attrs["split"]),
"role": str(h5.attrs["protocol_role"]),
"parameters": parameters(config),
"frequencies_hz": frequencies,
"gain": gain,
"responses": responses,
"targets": targets,
"outputs": outputs,
"names": names,
}
class ModalConditioner(nn.Module):
def __init__(self, inputs: int, outputs: int, hidden: int = 48):
super().__init__()
self.network = nn.Sequential(
nn.Linear(inputs, hidden),
nn.SiLU(),
nn.Linear(hidden, hidden),
nn.SiLU(),
nn.Linear(hidden, outputs),
)
def forward(self, values: torch.Tensor) -> torch.Tensor:
return self.network(values)
def relative_l2(prediction: np.ndarray, target: np.ndarray) -> float:
return float(
np.linalg.norm(prediction - target)
/ max(np.linalg.norm(target), np.finfo(float).eps)
)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--data", type=Path, default=DEFAULT_DATA)
parser.add_argument("--output", type=Path, default=Path(__file__).resolve().parent / "artifacts")
parser.add_argument("--rank", type=int, default=8)
parser.add_argument("--hidden", type=int, default=24)
parser.add_argument("--epochs", type=int, default=4000)
parser.add_argument("--seed", type=int, default=20260927)
args = parser.parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.set_num_threads(max(1, min(8, torch.get_num_threads())))
paths = case_paths(args.data)
for path in paths:
check_case(path)
train_paths = [path for path in paths if split(path)[0] == "train"]
pod, explained = pod_basis(train_paths, args.rank)
cases = [case_system(path, pod) for path in paths]
train_cases = [item for item in cases if item["split"] == "train"]
validation_cases = [item for item in cases if item["split"] == "validation"]
coordinate_matrix = np.stack(
[conditioner_coordinates(item["parameters"]) for item in train_cases]
)
parameter_mean = coordinate_matrix.mean(axis=0)
parameter_scale = coordinate_matrix.std(axis=0)
feature_train = (coordinate_matrix - parameter_mean) / parameter_scale
gain_train = np.stack(
[item["gain"] / modal_force_scale(item["parameters"]) for item in train_cases]
)
gain_scale = np.sqrt(np.mean(gain_train**2, axis=0))
gain_scale[gain_scale < 1.0e-14] = 1.0
target_train = gain_train / gain_scale
device = torch.device("cpu")
model = ModalConditioner(
len(CONDITIONER_FEATURE_NAMES),
target_train.shape[1] * target_train.shape[2],
hidden=args.hidden,
).to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=2.0e-3, weight_decay=1.0e-6)
x_train = torch.as_tensor(feature_train, dtype=torch.float32, device=device)
y_train = torch.as_tensor(target_train.reshape(len(train_cases), -1), dtype=torch.float32, device=device)
x_validation = torch.as_tensor(
(
np.stack(
[conditioner_coordinates(item["parameters"]) for item in validation_cases]
)
- parameter_mean
)
/ parameter_scale,
dtype=torch.float32,
device=device,
)
y_validation = torch.as_tensor(
(
np.stack(
[
item["gain"] / modal_force_scale(item["parameters"])
for item in validation_cases
]
)
/ gain_scale
).reshape(len(validation_cases), -1),
dtype=torch.float32,
device=device,
)
best = None
best_epoch = 0
patience = 500
for epoch in range(args.epochs):
model.train()
optimizer.zero_grad(set_to_none=True)
prediction = model(x_train)
loss = torch.mean((prediction - y_train) ** 2)
loss.backward()
optimizer.step()
model.eval()
with torch.no_grad():
validation_loss = torch.mean((model(x_validation) - y_validation) ** 2).item()
if best is None or validation_loss < best[0]:
best = (
validation_loss,
{name: value.detach().cpu().clone() for name, value in model.state_dict().items()},
)
best_epoch = epoch
elif epoch - best_epoch >= patience:
break
assert best is not None
model.load_state_dict(best[1])
def predict_gain(item: dict) -> np.ndarray:
feature = (
conditioner_coordinates(item["parameters"]) - parameter_mean
) / parameter_scale
with torch.no_grad():
value = model(torch.as_tensor(feature[None], dtype=torch.float32)).numpy()[0]
return (
value.reshape(gain_scale.shape)
* gain_scale
* modal_force_scale(item["parameters"])
)
def evaluate(selected: list[dict]) -> dict:
learned_sse = oracle_sse = pod_sse = reference = 0.0
trajectory_metrics = []
for item in selected:
learned_gain = predict_gain(item)
for name, response, target_q, output in zip(
item["names"], item["responses"], item["targets"], item["outputs"]
):
learned = (response @ learned_gain) @ pod
oracle = (response @ item["gain"]) @ pod
pod_projection = target_q @ pod
learned_error = float(np.sum((learned - output) ** 2))
oracle_error = float(np.sum((oracle - output) ** 2))
pod_error = float(np.sum((pod_projection - output) ** 2))
norm = float(np.sum(output**2))
learned_sse += learned_error
oracle_sse += oracle_error
pod_sse += pod_error
reference += norm
trajectory_metrics.append(
{
"trajectory_id": f"{item['case_id']}__{name}",
"protocol_role": item["role"],
"relative_l2": float(np.sqrt(learned_error / max(norm, np.finfo(float).eps))),
}
)
return {
"configuration_count": len(selected),
"trajectory_count": len(trajectory_metrics),
"learned_rollout_relative_l2": float(np.sqrt(learned_sse / reference)),
"oracle_modal_relative_l2": float(np.sqrt(oracle_sse / reference)),
"pod_projection_relative_l2": float(np.sqrt(pod_sse / reference)),
"median_trajectory_relative_l2": float(
np.median([item["relative_l2"] for item in trajectory_metrics])
),
"maximum_trajectory_relative_l2": float(
np.max([item["relative_l2"] for item in trajectory_metrics])
),
"trajectories": trajectory_metrics,
}
cohorts = {
"train": train_cases,
"validation": validation_cases,
"test_all": [item for item in cases if item["split"] == "test"],
"test_id": [item for item in cases if item["split"] == "test" and item["role"] == "id"],
"test_excitation_ood": [item for item in cases if item["role"] == "excitation_ood"],
"test_parameter_ood": [item for item in cases if item["role"] == "parameter_ood"],
}
metrics = {
"model": "physics-conditioned modal neural state-space",
"model_version": "0.1.0",
"seed": args.seed,
"pod_rank": args.rank,
"pod_explained_energy": explained,
"modal_oscillator_count": 4,
"hidden_width": args.hidden,
"trainable_parameters": sum(value.numel() for value in model.parameters()),
"best_epoch": best_epoch,
"best_validation_gain_mse": best[0],
"data_requirement": "corrected T4 schema 1.1.0",
"cohorts": {name: evaluate(items) for name, items in cohorts.items()},
}
args.output.mkdir(parents=True, exist_ok=True)
torch.save(model.state_dict(), args.output / "model_state_dict.pt")
np.savez_compressed(
args.output / "model_arrays.npz",
parameter_mean=parameter_mean,
parameter_scale=parameter_scale,
gain_scale=gain_scale,
pod=pod,
)
(args.output / "config.json").write_text(
json.dumps(
{
"schema_version": "0.1.0",
"parameter_names": PARAMETER_NAMES,
"conditioner_feature_names": CONDITIONER_FEATURE_NAMES,
"gain_physics_scale": "traction_amplitude_pa / (density_kg_m3 * length_m)",
"rank": args.rank,
"modal_oscillator_count": 4,
"hidden_width": args.hidden,
"trainable_parameters": sum(value.numel() for value in model.parameters()),
"data_schema_required": "1.1.0",
"data_generator_fix_required": "component-preserving-kinematic-bcs",
},
indent=2,
)
+ "\n",
encoding="utf-8",
)
torch.save(
{
"state_dict": model.state_dict(),
"parameter_names": PARAMETER_NAMES,
"conditioner_feature_names": CONDITIONER_FEATURE_NAMES,
"parameter_mean": parameter_mean,
"parameter_scale": parameter_scale,
"gain_scale": gain_scale,
"gain_physics_scale": "traction_amplitude_pa / (density_kg_m3 * length_m)",
"pod": pod,
"pod_explained_energy": explained,
"rank": args.rank,
"modal_oscillator_count": 4,
"hidden_width": args.hidden,
"schema_version": "0.1.0",
},
args.output / "t4_modal_neural_state_space.pt",
)
(args.output / "metrics.json").write_text(
json.dumps(metrics, indent=2, sort_keys=True) + "\n", encoding="utf-8"
)
print(json.dumps({key: value for key, value in metrics.items() if key != "cohorts"}, indent=2))
for name, value in metrics["cohorts"].items():
print(name, {key: item for key, item in value.items() if key != "trajectories"})
if __name__ == "__main__":
sys.exit(main())