! ============================================================
! M05 - Restricciones y cargas auditables
! Termina con la base de datos preparada, antes del calculo.
! Unidades coherentes: m, kg, s, N, Pa.
! ============================================================

/CLEAR,START
/FILNAME,m05_bc_audit,1
/TITLE,M05 - Condiciones de contorno auditables
/UNITS,SI

case_id=0
beam_l=1.0
beam_h=0.10
beam_b=0.05
mesh_h=0.05
young=210E9
nu=0.30
tip_force=-1000
select_tol=MIN(beam_h,beam_b)*1E-5
load_tol=0.001
zero_tol=1E-12

! --- 1. MODELO Y MALLA SELECCIONADA EN M04 ---
/PREP7
ET,1,SOLID185
KEYOPT,1,2,0
MP,EX,1,young
MP,PRXY,1,nu
TYPE,1
MAT,1
BLOCK,0,beam_l,0,beam_h,0,beam_b

div_x=beam_l/mesh_h
div_y=beam_h/mesh_h
div_z=beam_b/mesh_h
MSHAPE,0,3D
MSHKEY,1
LSEL,S,LENGTH,,beam_l
LESIZE,ALL,,,div_x,,1
LSEL,S,LENGTH,,beam_h
LESIZE,ALL,,,div_y,,1
LSEL,S,LENGTH,,beam_b
LESIZE,ALL,,,div_z,,1
ALLSEL,ALL
VMESH,ALL

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

! --- 2. REGIONES FISICAS ---
SELTOL,select_tol
CSYS,0
NSEL,S,LOC,X,0
CM,fixed_nodes,NODE
*GET,n_fixed,NODE,0,COUNT
ALLSEL,ALL

NSEL,S,LOC,X,beam_l
CM,tip_nodes,NODE
*GET,n_tip,NODE,0,COUNT
ALLSEL,ALL

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

! --- 3. APLICACION EN EL PROCESADOR DE SOLUCION ---
! Todavia no se declara el tipo de analisis ni se resuelve.
FINISH
/SOLU

CMSEL,S,fixed_nodes
D,ALL,ALL,0
ALLSEL,ALL

CMSEL,S,tip_nodes
force_per_node=tip_force/n_tip
F,ALL,FY,force_per_node
ALLSEL,ALL

! Los listados son evidencia legible, pero no la unica auditoria.
DLIST,ALL,ALL
FLIST,ALL,ALL

! --- 4. AUDITORIA DE LA BASE DE DATOS ---
! Las restricciones se comprueban sobre el componente al que se aplicaron.
! El alcance global queda documentado por DLIST.
! *GET,F devuelve cero cuando no existe fuerza aplicada.
n_constrained_dof=0
n_bad_constraints=0
n_force_nodes=0
force_sum_x=0
force_sum_y=0
force_sum_z=0

CMSEL,S,fixed_nodes
node_id=0
*DO,j,1,n_fixed
  node_id=NDNEXT(node_id)
  *GET,dof_ux,NODE,node_id,D,UX
  *GET,dof_uy,NODE,node_id,D,UY
  *GET,dof_uz,NODE,node_id,D,UZ

  n_constrained_dof=n_constrained_dof+3
  *IF,ABS(dof_ux),GT,zero_tol,THEN
    n_bad_constraints=n_bad_constraints+1
  *ENDIF
  *IF,ABS(dof_uy),GT,zero_tol,THEN
    n_bad_constraints=n_bad_constraints+1
  *ENDIF
  *IF,ABS(dof_uz),GT,zero_tol,THEN
    n_bad_constraints=n_bad_constraints+1
  *ENDIF
*ENDDO

! Las fuerzas se suman sobre toda la base de datos para detectar
! cargas fuera de tip_nodes o componentes inesperadas.
ALLSEL,ALL
node_id=0
*DO,j,1,n_nodes
  node_id=NDNEXT(node_id)
  *GET,node_fx,NODE,node_id,F,FX
  *GET,node_fy,NODE,node_id,F,FY
  *GET,node_fz,NODE,node_id,F,FZ
  force_sum_x=force_sum_x+node_fx
  force_sum_y=force_sum_y+node_fy
  force_sum_z=force_sum_z+node_fz

  node_force_norm=ABS(node_fx)+ABS(node_fy)+ABS(node_fz)
  *IF,node_force_norm,GT,zero_tol,THEN
    n_force_nodes=n_force_nodes+1
  *ENDIF
*ENDDO

load_error=ABS(force_sum_y-tip_force)/ABS(tip_force)

passes=1
*IF,n_nodes,NE,126,THEN
  passes=0
*ENDIF
*IF,n_elements,NE,40,THEN
  passes=0
*ENDIF
*IF,n_fixed,NE,6,THEN
  passes=0
*ENDIF
*IF,n_tip,NE,6,THEN
  passes=0
*ENDIF
*IF,n_overlap,NE,0,THEN
  passes=0
*ENDIF
*IF,n_constrained_dof,NE,3*n_fixed,THEN
  passes=0
*ENDIF
*IF,n_bad_constraints,NE,0,THEN
  passes=0
*ENDIF
*IF,n_force_nodes,NE,n_tip,THEN
  passes=0
*ENDIF
*IF,ABS(force_sum_x),GT,zero_tol,THEN
  passes=0
*ENDIF
*IF,ABS(force_sum_z),GT,zero_tol,THEN
  passes=0
*ENDIF
*IF,load_error,GE,load_tol,THEN
  passes=0
*ENDIF

*CFOPEN,m05_bc_audit,csv
*VWRITE
('case,mesh_h,n_nodes,n_elements,n_fixed,n_tip,n_constrained_dof,n_force_nodes,force_per_node,force_sum_x,force_sum_y,force_sum_z,target_force,load_error,passes')
*VWRITE,case_id,mesh_h,n_nodes,n_elements,n_fixed,n_tip,n_constrained_dof,n_force_nodes,force_per_node,force_sum_x,force_sum_y,force_sum_z,tip_force,load_error,passes
(F3.0,',',E16.8,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',F10.0,',',E16.8,',',E16.8,',',E16.8,',',E16.8,',',E16.8,',',E16.8,',',F2.0)
*CFCLOS

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

FINISH
