File size: 4,342 Bytes
ebc46a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import json
from pathlib import Path

import h5py
import numpy as np

from src.t2_material_loading_memory import (
    DATA_PATH,
    MATERIAL_MODELS,
    PATH_FAMILIES,
    _j2_monotonic_reference,
    case_identity,
    design_parameters,
    prescribed_history,
    solve_trajectory,
    split_assignments,
)


def test_t2_design_is_unique_balanced_and_split_by_trajectory():
    rows = design_parameters()
    split = split_assignments(rows)
    assert len(rows) == 96
    assert len({case_identity(row) for row in rows}) == 96
    assert sum(value == "train" for value in split.values()) == 72
    assert sum(value == "validation" for value in split.values()) == 12
    assert sum(value == "test" for value in split.values()) == 12
    for model in MATERIAL_MODELS:
        assert sum(row["material_model"] == model for row in rows) == 48
        for family in PATH_FAMILIES:
            indices = [
                index
                for index, row in enumerate(rows)
                if row["material_model"] == model
                and row["path_family"] == family
            ]
            assert len(indices) == 8
            assert sum(split[index] == "train" for index in indices) == 6
            assert sum(split[index] == "validation" for index in indices) == 1
            assert sum(split[index] == "test" for index in indices) == 1


def test_t2_prescribed_histories_have_fixed_size_and_required_reversals():
    for row in design_parameters():
        time, strain, anchors = prescribed_history(row)
        assert len(time) == len(strain) == 121
        assert np.all(np.diff(time) > 0.0)
        assert strain[0] == 0.0
        assert np.max(np.abs(strain)) <= 1.15 * row["maximum_equivalent_strain"]
        for anchor in anchors:
            assert np.any(np.isclose(strain, anchor, rtol=0.0, atol=1.0e-15))
        if row["path_family"] not in ("monotonic_tension", "unload_reload"):
            assert np.min(anchors) < 0.0 < np.max(anchors)


def test_t2_refined_paths_are_nested_at_every_coarse_state():
    for row in design_parameters():
        _, coarse, _ = prescribed_history(row, points=121)
        _, fine, _ = prescribed_history(row, points=241)
        np.testing.assert_allclose(coarse, fine[::2], rtol=0.0, atol=1.0e-15)


def test_t2_agentfem_material_point_smoke_paths_are_physical():
    rows = design_parameters()
    for model in MATERIAL_MODELS:
        for family in PATH_FAMILIES:
            row = next(
                item
                for item in rows
                if item["material_model"] == model
                and item["path_family"] == family
            )
            arrays, metrics = solve_trajectory(row, points=41)
            assert metrics["all_finite"]
            assert metrics["plastic_step_count"] > 0
            assert metrics["maximum_plastic_strain_trace"] < 1.0e-10
            assert metrics["minimum_peeq_increment"] >= -1.0e-12
            assert metrics["maximum_yield_surface_relative_residual"] < 1.0e-8
            assert arrays["signed_equivalent_stress_pa"][0] == 0.0
            if family == "symmetric_cyclic":
                assert metrics["zero_strain_stress_range_pa"] > 1.0e6


def test_t2_j2_monotonic_path_matches_closed_form():
    row = next(
        item
        for item in design_parameters()
        if item["material_model"] == "j2_linear_isotropic"
        and item["path_family"] == "monotonic_tension"
    )
    arrays, _ = solve_trajectory(row, points=41)
    stress, peeq = _j2_monotonic_reference(row)
    np.testing.assert_allclose(
        arrays["signed_equivalent_stress_pa"][-1], stress, rtol=1.0e-11
    )
    np.testing.assert_allclose(
        arrays["equivalent_plastic_strain"][-1], peeq, rtol=1.0e-11
    )


def test_t2_packaged_pilot_is_readable_when_present():
    if not DATA_PATH.exists():
        return
    quality_path = DATA_PATH.parents[2] / "artifacts" / DATA_PATH.parent.name / "quality.json"
    quality = json.loads(quality_path.read_text(encoding="utf-8"))
    assert quality["status"] == "accepted"
    with h5py.File(DATA_PATH, "r") as h5:
        assert len(h5) == 96
        for group in h5.values():
            assert group["stress_pa"].shape == (121, 3, 3)
            assert np.isfinite(group["stress_pa"][:]).all()
            assert np.isfinite(group["equivalent_plastic_strain"][:]).all()