Download src/generate_t2_graybox_closure.py from HaomingLuo/AgentFEM-Material-Loading-Memory: direct link, hf CLI and curl.
- Browser
- Download file 6.88 kB
-
https://huggingface.co/datasets/HaomingLuo/AgentFEM-Material-Loading-Memory/resolve/main/src/generate_t2_graybox_closure.py
- Command line
-
hf download hf://datasets/HaomingLuo/AgentFEM-Material-Loading-Memory/src/generate_t2_graybox_closure.py
-
curl -L -o generate_t2_graybox_closure.py https://huggingface.co/datasets/HaomingLuo/AgentFEM-Material-Loading-Memory/resolve/main/src/generate_t2_graybox_closure.py
6.88 kB
| """Generate the out-of-template cohort for incomplete-physics closure. | |
| The reference material has three kinematic time scales and a piecewise-linear | |
| isotropic hardening curve. DENRM is deliberately restricted to two memory | |
| channels and is not given either reference evolution law. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import time | |
| from pathlib import Path | |
| import h5py | |
| import numpy as np | |
| from agentfem import constitutive | |
| from src.generate_t2_graybox_cohort import design, split_for_family | |
| from src.t2_multiaxial_ood_v2 import PATH_FAMILIES, strain_history, tensor_to_voigt | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DATA_DIR = ROOT / "data" / "t2_graybox_closure_v1" | |
| DATA_PATH = DATA_DIR / "cohort.h5" | |
| MANIFEST_PATH = DATA_DIR / "manifest.json" | |
| MATERIAL = { | |
| "young_pa": 190.0e9, | |
| "poisson": 0.30, | |
| "yield_stress_pa": 280.0e6, | |
| "backstress_c1_pa": 28.0e9, | |
| "backstress_gamma1": 80.0, | |
| "backstress_c2_pa": 10.0e9, | |
| "backstress_gamma2": 12.0, | |
| "backstress_c3_pa": 3.0e9, | |
| "backstress_gamma3": 1.5, | |
| "hardening_peeq": (0.0, 0.002, 0.006, 0.015, 0.030, 0.060, 0.100), | |
| "hardening_stress_pa": ( | |
| 280.0e6, | |
| 301.0e6, | |
| 324.0e6, | |
| 345.0e6, | |
| 358.0e6, | |
| 370.0e6, | |
| 378.0e6, | |
| ), | |
| } | |
| def closure_design(count_per_family: int = 16) -> list[dict[str, object]]: | |
| rows = design(count_per_family) | |
| for row in rows: | |
| row.update(MATERIAL) | |
| row["material_model"] = "hidden_three_memory_tabulated_hardening" | |
| return rows | |
| def material() -> constitutive.ChabocheCombinedHardening: | |
| hardening = constitutive.TabulatedIsotropicHardening( | |
| equivalent_plastic_strain=tuple(MATERIAL["hardening_peeq"]), | |
| yield_stress=tuple(MATERIAL["hardening_stress_pa"]), | |
| extrapolation="constant", | |
| ) | |
| return constitutive.chaboche( | |
| young=float(MATERIAL["young_pa"]), | |
| poisson=float(MATERIAL["poisson"]), | |
| yield_stress=float(MATERIAL["yield_stress_pa"]), | |
| backstresses=( | |
| (float(MATERIAL["backstress_c1_pa"]), float(MATERIAL["backstress_gamma1"])), | |
| (float(MATERIAL["backstress_c2_pa"]), float(MATERIAL["backstress_gamma2"])), | |
| (float(MATERIAL["backstress_c3_pa"]), float(MATERIAL["backstress_gamma3"])), | |
| ), | |
| isotropic_hardening=hardening, | |
| name="hidden three-memory tabulated reference", | |
| ) | |
| def solve(parameters: dict[str, object], *, points: int | None = None) -> dict[str, np.ndarray]: | |
| law = material() | |
| coordinates, strains = strain_history(parameters, points=points) | |
| count = len(strains) | |
| stress = np.empty_like(strains) | |
| plastic = np.empty_like(strains) | |
| peeq = np.empty(count) | |
| increment = np.empty(count) | |
| truth_memories = np.empty((count, 3, 3, 3)) | |
| latent_memories = np.empty((count, 2, 3, 3)) | |
| radius = np.empty(count) | |
| state = None | |
| for index, strain in enumerate(strains): | |
| update = law.update(strain, state, linearization="none") | |
| state = update.state | |
| stress[index] = update.stress | |
| plastic[index] = state.plastic_strain | |
| peeq[index] = state.equivalent_plastic_strain | |
| increment[index] = update.plastic_multiplier_increment | |
| truth_memories[index] = state.backstresses | |
| # The two-channel closure sees one fast state and one deliberately | |
| # unresolved aggregate of the medium and slow reference mechanisms. | |
| latent_memories[index, 0] = state.backstresses[0] | |
| latent_memories[index, 1] = state.backstresses[1] + state.backstresses[2] | |
| radius[index] = law.current_yield_stress(peeq[index]) - law.yield_stress | |
| return { | |
| "strain": tensor_to_voigt(strains), | |
| "stress_pa": tensor_to_voigt(stress), | |
| "plastic_strain": tensor_to_voigt(plastic), | |
| "peeq": peeq, | |
| "plastic_increment": increment, | |
| "memories_pa": tensor_to_voigt(latent_memories), | |
| "truth_memories_pa": tensor_to_voigt(truth_memories), | |
| "isotropic_radius_pa": radius, | |
| "path_coordinates": coordinates, | |
| } | |
| def generate(count_per_family: int = 16, *, force: bool = False) -> dict[str, object]: | |
| rows = closure_design(count_per_family) | |
| DATA_DIR.mkdir(parents=True, exist_ok=True) | |
| if DATA_PATH.exists() and not force: | |
| raise FileExistsError(f"{DATA_PATH} already exists; pass --force to replace it.") | |
| temporary = DATA_PATH.with_suffix(".h5.tmp") | |
| started = time.perf_counter() | |
| with h5py.File(temporary, "w") as h5: | |
| h5.attrs["schema"] = "agentfem.physics-data.incomplete-physics-closure" | |
| h5.attrs["schema_version"] = "1.0.0" | |
| h5.attrs["material_json"] = json.dumps(MATERIAL, sort_keys=True) | |
| for index, row in enumerate(rows): | |
| group = h5.create_group(f"{index:05d}") | |
| group.attrs["path_family"] = str(row["path_family"]) | |
| group.attrs["split"] = split_for_family(str(row["path_family"])) | |
| group.attrs["parameters_json"] = json.dumps(row, sort_keys=True) | |
| for name, value in solve(row).items(): | |
| group.create_dataset(name, data=value, compression="gzip", shuffle=True) | |
| temporary.replace(DATA_PATH) | |
| manifest = { | |
| "schema": "agentfem.physics-data.incomplete-physics-closure", | |
| "schema_version": "1.0.0", | |
| "sample_count": len(rows), | |
| "count_per_family": count_per_family, | |
| "points_per_trajectory": 241, | |
| "path_families": list(PATH_FAMILIES), | |
| "splits": { | |
| "train": 5 * count_per_family, | |
| "validation": count_per_family, | |
| "test": 2 * count_per_family, | |
| }, | |
| "known_to_model": ["young_pa", "poisson", "yield_stress_pa", "J2_geometry"], | |
| "hidden_from_model": [ | |
| "three_reference_memory_channels", | |
| "all_reference_recovery_parameters", | |
| "piecewise_linear_isotropic_hardening_table", | |
| "reference_hardening_update_equations", | |
| ], | |
| "intentional_model_mismatch": ( | |
| "reference has three memories; DENRM has two and must close the lumped medium/slow state" | |
| ), | |
| "ground_truth": "AgentFEM three-memory Chaboche with tabulated isotropic hardening", | |
| "scope": "Out-of-template synthetic closure cohort; fixed material.", | |
| "elapsed_seconds": time.perf_counter() - started, | |
| "bytes": DATA_PATH.stat().st_size, | |
| } | |
| MANIFEST_PATH.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8") | |
| print(json.dumps(manifest, indent=2)) | |
| return manifest | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--count-per-family", type=int, default=16) | |
| parser.add_argument("--force", action="store_true") | |
| args = parser.parse_args() | |
| generate(args.count_per_family, force=args.force) | |
| if __name__ == "__main__": | |
| main() | |