! ============================================================
! 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

case_id=0                           ! --- 1. CONTRATO DE ENTRADA ---
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 ---
/PREP7                              ! --- 2. GEOMETRIA Y MALLA HEREDADAS ---
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                          ! ALLSEL,ALL restaura la seleccion completa antes de auditar conteos globales.
*GET,n_nodes,NODE,0,COUNT           ! *GET almacena en n_nodes el numero total de nodos del modelo activo.
*GET,n_elements,ELEM,0,COUNT

SELTOL,select_tol                   ! SELTOL fija la tolerancia de coincidencia para selecciones por LOC.

! --- 3. EXTREMO FIJO EN EL SISTEMA GLOBAL ---
CSYS,0                              ! CSYS,0 activa el sistema de coordenadas global antes de seleccionar por X.
NSEL,S,LOC,X,0                      ! NSEL,S,LOC,X selecciona nodos en el plano de empotramiento sin usar IDs.
*GET,n_fixed,NODE,0,COUNT
CM,fixed_nodes,NODE                 ! CM,fixed_nodes guarda la seleccion actual como componente reutilizable.
ALLSEL,ALL

! --- 4. PUNTA CON UNA REGLA LOCAL ---
LOCAL,11,CART,beam_l,0,0            ! LOCAL,11 define un sistema cartesiano con origen en la punta de la viga.
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

NSEL,S,LOC,Y,beam_h                 ! --- 5. CARA SUPERIOR ---
*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     ! ESEL,S,CENT,X selecciona elementos cuyo centroide cae en la banda central.
*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 intersecta tip_nodes con top_nodes para obtener la esquina superior.
CMSEL,R,top_nodes
*GET,n_tip_top,NODE,0,COUNT
CM,tip_top_nodes,NODE
ALLSEL,ALL

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

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

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

passes=1                            ! --- 9. AUDITORIA EJECUTABLE ---
*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




