# -*- coding: utf-8 -*-
# TorsoCAE Validation Journal — linear_elastic case 2: plate with circular hole (Kirsch)
# See README.md in this directory for theory and results.
# One-eighth-symmetry model: sym_x0=tag1, hole=tag2, z_top=tag3(free), far_y=tag4(free),
# sym_z0=tag5, sym_y0=tag6, far_x=tag7(traction). Tags verified empirically for this
# CSG (box minus cylinder) — see run_validation.py's _find_face_tags() for the general,
# non-hardcoded lookup used by the sweep.

import sys
sys.path.insert(0, "./server/")
from pytorsocae import TorsoCAESession

session = TorsoCAESession()
session.csg_make_shape("box", {"dx": 0.15, "dy": 0.15, "dz": 0.01, "cx": 0, "cy": 0, "cz": 0},
                        name="EighthPlate", body_id="body_0")
session.csg_make_shape("cylinder", {"r": 0.01, "h": 0.02, "cx": 0, "cy": 0, "cz": -0.005},
                        name="HoleCyl", body_id="body_1")
session.csg_boolean("cut", "body_0", "body_1", name="EighthPlateWithHole", body_id="body_2")
session.csg_delete("body_0")
session.csg_delete("body_1")

session.csg_select("body_2")
session.mesh(mesh_id="mesh_0", algo_id="delaunay", size_factor=0.15, size_min=0, size_max=0, order=2, dim=3)
session.solid("EighthPlateWithHole").material(E=210e9, nu=0.3)
session.surface(1, scope_id="body_2").bc("fixed", frame="global", components=["x"])   # sym_x0
session.surface(6, scope_id="body_2").bc("fixed", frame="global", components=["y"])   # sym_y0
session.surface(5, scope_id="body_2").bc("fixed", frame="global", components=["z"])   # sym_z0
session.surface(7, scope_id="body_2").bc("traction", values=[1000000, 0, 0])           # far_x, sigma_inf=1MPa
session.set_physics("structural", submodel="linear_elastic", backend="dolfinx")
session.set_solver_options(algo="cg", precond="gamg", tol=1e-10, max_iter=2000, n_cores=2, device="cpu")
session.compute(mesh_ids=["mesh_0"])
