! ============================================================
! M02 - Geometria, selecciones y componentes robustos
! Proyecto conductor: viga maciza en voladizo
! Unidades coherentes: m, kg, s, N, Pa
! Este modulo termina en /PREP7: no aplica cargas ni resuelve.
! ============================================================

/CLEAR,START
/FILNAME,m02_selections,1
/TITLE,M02 - Regiones robustas
/UNITS,SI

! --- 1. CONTRATO DE ENTRADA ---
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

! --- 2. GEOMETRIA Y MALLA HEREDADAS ---
! ET, MP, ESIZE y VMESH se estudian formalmente en M03-M04.
/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

! La tolerancia queda activa para las selecciones geometricas.
SELTOL,select_tol

! --- 3. EXTREMO FIJO EN EL SISTEMA GLOBAL ---
CSYS,0
NSEL,S,LOC,X,0
*GET,n_fixed,NODE,0,COUNT
CM,fixed_nodes,NODE
ALLSEL,ALL

! --- 4. PUNTA CON UNA REGLA LOCAL ---
! El sistema 11 tiene su origen en la punta de la viga.
LOCAL,11,CART,beam_l,0,0
CSYS,11
NSEL,S,LOC,X,0
*GET,n_tip,NODE,0,COUNT
CM,tip_nodes,NODE

! Regla de seguridad: restaurar siempre el sistema global.
CSYS,0
ALLSEL,ALL

! --- 5. CARA SUPERIOR ---
NSEL,S,LOC,Y,beam_h
*GET,n_top,NODE,0,COUNT
CM,top_nodes,NODE
ALLSEL,ALL

! --- 6. BANDA CENTRAL DE ELEMENTOS ---
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

! --- 7. INTERSECCION: PUNTA Y CARA SUPERIOR ---
CMSEL,S,tip_nodes
CMSEL,R,top_nodes
*GET,n_tip_top,NODE,0,COUNT
CM,tip_top_nodes,NODE
ALLSEL,ALL

! --- 8. PRUEBA DE NO SOLAPAMIENTO ---
CMSEL,S,fixed_nodes
CMSEL,R,tip_nodes
*GET,n_overlap,NODE,0,COUNT
ALLSEL,ALL

! ALLSEL debe recuperar el modelo completo.
*GET,n_restored,NODE,0,COUNT

! La tolerancia automatica se restaura antes de salir.
SELTOL,

! --- 9. AUDITORIA EJECUTABLE ---
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

