! ----------------------------------------------------------------------
! M02 - English course download: 02_geometry_selections.mac
! Follow the matching English lesson for explanations and task details.
! Commands, parameter names, file names, CSV keys, and result tokens are preserved.
! ----------------------------------------------------------------------

/CLEAR,START
/FILNAME,m02_selections,1
/TITLE,M02 - Robust regions
/UNITS,SI

case_id=0
beam_l=1.0
beam_h=0.10
beam_b=0.05
young=210E9
nu=0.30
mesh_h=0.025
select_tol=MIN(beam_h,beam_b)*1E-5

*IF,beam_l,LE,0,THEN
  /COM,ERROR: beam_l must be positive
  /EOF
*ENDIF
*IF,beam_h,LE,0,THEN
  /COM,ERROR: beam_h must be positive
  /EOF
*ENDIF
*IF,beam_b,LE,0,THEN
  /COM,ERROR: beam_b must be positive
  /EOF
*ENDIF
*IF,mesh_h,LE,0,THEN
  /COM,ERROR: mesh_h must be positive
  /EOF
*ENDIF

/PREP7
ET,1,SOLID185
MP,EX,1,young
MP,PRXY,1,nu
TYPE,1
MAT,1

BLOCK,0,beam_l,0,beam_h,0,beam_b
ESIZE,mesh_h
VMESH,ALL

ALLSEL,ALL
*GET,n_nodes,NODE,0,COUNT
*GET,n_elements,ELEM,0,COUNT

SELTOL,select_tol

CSYS,0
NSEL,S,LOC,X,0
*GET,n_fixed,NODE,0,COUNT
CM,fixed_nodes,NODE
ALLSEL,ALL

LOCAL,11,CART,beam_l,0,0
CSYS,11
NSEL,S,LOC,X,0
*GET,n_tip,NODE,0,COUNT
CM,tip_nodes,NODE

CSYS,0
ALLSEL,ALL

NSEL,S,LOC,Y,beam_h
*GET,n_top,NODE,0,COUNT
CM,top_nodes,NODE
ALLSEL,ALL

ESEL,S,CENT,X,0.4*beam_l,0.6*beam_l
*GET,n_mid_elems,ELEM,0,COUNT
CM,mid_elems,ELEM
ALLSEL,ALL

CMSEL,S,tip_nodes
CMSEL,R,top_nodes
*GET,n_tip_top,NODE,0,COUNT
CM,tip_top_nodes,NODE
ALLSEL,ALL

CMSEL,S,fixed_nodes
CMSEL,R,tip_nodes
*GET,n_overlap,NODE,0,COUNT
ALLSEL,ALL

*GET,n_restored,NODE,0,COUNT

SELTOL,

passes=1
*IF,n_fixed,LE,0,THEN
  passes=0
*ENDIF
*IF,n_tip,LE,0,THEN
  passes=0
*ENDIF
*IF,n_top,LE,0,THEN
  passes=0
*ENDIF
*IF,n_mid_elems,LE,0,THEN
  passes=0
*ENDIF
*IF,n_tip_top,LE,0,THEN
  passes=0
*ENDIF
*IF,n_overlap,NE,0,THEN
  passes=0
*ENDIF
*IF,n_tip_top,GE,n_tip,THEN
  passes=0
*ENDIF
*IF,n_tip_top,GE,n_top,THEN
  passes=0
*ENDIF
*IF,n_mid_elems,GE,n_elements,THEN
  passes=0
*ENDIF
*IF,n_restored,NE,n_nodes,THEN
  passes=0
*ENDIF

*CFOPEN,m02_selection_audit,csv
*VWRITE
('case,beam_l,beam_h,mesh_h,n_nodes,n_elements,n_fixed,n_tip,n_top,n_mid_elems,n_tip_top,n_overlap,n_restored,passes')
*VWRITE,case_id,beam_l,beam_h,mesh_h,n_nodes,n_elements,n_fixed,n_tip,n_top,n_mid_elems,n_tip_top,n_overlap,n_restored,passes
(F2.0,',',E16.8,',',E16.8,',',E16.8,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F2.0)
*CFCLOS

*IF,passes,EQ,1,THEN
  /COM,M02 AUDIT PASSED
*ELSE
  /COM,M02 AUDIT FAILED - inspect m02_selection_audit.csv
*ENDIF

CMLIST,fixed_nodes
CMLIST,tip_nodes
CMLIST,top_nodes
CMLIST,tip_top_nodes
CMLIST,mid_elems

FINISH
