File size: 3,202 Bytes
1203015
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
"""Plot published double-notch FEM fields without a solver. Apache-2.0."""
import argparse
import json
from pathlib import Path

import h5py
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
from huggingface_hub import hf_hub_download

REPO = "HaomingLuo/AgentFEM-DoubleNotch-Elasticity-2D"
REVISION = "2d315d7dc6b9d41badceb02ac8fbb475e3413ec2"


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--data", type=Path, help="Existing test.h5; otherwise download pinned test split (~49 MB)")
    parser.add_argument("--sample", default="00016", help="HDF5 sample ID")
    parser.add_argument("--output", type=Path, default=Path("notch-fields.png"))
    args = parser.parse_args()
    path = args.data or hf_hub_download(REPO, "test.h5", repo_type="dataset", revision=REVISION)
    with h5py.File(path) as f:
        g = f[args.sample]
        p = json.loads(g.attrs["parameters_json"])
        xy = g["coordinates"][:, :2]
        triangles = g["connectivity"][:, :3]  # XDMF Triangle_6 corner nodes
        mises = g["cell/MISES"][:]
        displacement = g["point/UMAG"][:]
    assert np.isfinite(xy).all() and np.isfinite(mises).all() and np.isfinite(displacement).all()
    assert triangles.min() >= 0 and triangles.max() < len(xy)
    centers = xy[triangles].mean(axis=1)
    central = (centers[:, 0] > .3*p["length_m"]) & (centers[:, 0] < .7*p["length_m"])
    central_max = float(mises[central].max())
    mesh = Triangulation(*(xy * 1000).T, triangles=triangles)
    plt.rcParams.update({"font.family": "DejaVu Sans", "font.size": 10})
    fig, axes = plt.subplots(2, 1, figsize=(10, 6.5), constrained_layout=True)
    values = (displacement * 1e6, mises / 1e6)
    for ax, value, title, unit, cmap in zip(axes, values,
            ("Displacement magnitude", "Von Mises stress · original cell values"),
            ("Displacement / µm", "Stress / MPa"), ("viridis", "magma")):
        if ax is axes[0]:
            # Linear display on corner nodes, not a new FEM reconstruction.
            artist = ax.tripcolor(mesh, value, shading="gouraud", cmap=cmap)
        else:
            artist = ax.tripcolor(mesh, facecolors=value, shading="flat", cmap=cmap)
            for fraction in (.3, .7):
                ax.axvline(fraction*p["length_m"]*1000, color="#158b83", ls="--", lw=1)
        ax.set(title=title, xlabel="x / mm", ylabel="y / mm", aspect="equal")
        ax.spines[["top", "right"]].set_visible(False)
        fig.colorbar(artist, ax=ax, label=unit, shrink=.85, pad=.02)
    fig.suptitle(f"AgentFEM · Double-notch plate | sample {args.sample}\n"
                 f"Applied traction {p['traction_pa']/1e6:.2f} MPa · Central-region cell maximum {central_max/1e6:.2f} MPa",
                 fontsize=13)
    args.output.parent.mkdir(parents=True, exist_ok=True)
    fig.savefig(args.output, dpi=180)
    plt.close(fig)
    print(json.dumps({"sample": args.sample, "central_cell_mises_max_pa": central_max,
                      "maximum_displacement_um": float(displacement.max()*1e6), "output": str(args.output)}))


if __name__ == "__main__":
    main()