
/PREP7

!NSCALE, ,all,,,1e-3,1e-3,1e-3
ALLSEL
EPLOT

!ESEL,u,MAT,,1 !bone
!ESEL,u,MAT,,16 !dentine
!ESEL,u,MAT,,14 !enamal
!ESEL,u,MAT,,2 !PDL incisor & molar
!ESEL,u,MAT,,12 !pulp
!ESEL,u,MAT,,18  !molar


!!!!!!!!!!!!!!!!!! material properties

!!!!!!!!!!!!!!!  molar
MP,EX,18,30000         
MP,PRXY,18,0.3

!!!!!!!!!!!!!!!  pulp
MP,EX,12,2.03     
MP,PRXY,12,0.45

!!!!!!!!!!!!!!!  dentine
MP,EX,16,18300     
MP,PRXY,16,0.31

!!!!!!!!!!!!!!!  enamal
MP,EX,14,82500 
MP,PRXY,14,0.33

!!!!!!!!!!!!!!!  bone
MP,EX,1,20000    
MP,PRXY,1,0.3

!!!!!!!!!!!!!! PDL_incisor & molar
MP,EX,2,50
MP,PRXY,2,0.4


ALLSEL
ESEL,s,,,all
NSLE
  NUMMRG, node 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


!!!!!!!!!!!!!!!!!! defining local CS for new joint
CSYS

WPCSYS
WPROTA,-10,,
CSWPLA,100,0


ET,100,MPC184  !!! Define MPC general joint element without creating corresponding elements
KEYOPT,100,1,16
KEYOPT,100,4,0  !General joint with both displacement and rotational degrees of freedom activated


!!! Constraints

NSEL,s,node,,248585
NSEL,a,node,,248978
CM,Node_Selected,NODE

CSYS,100 !!!0 
NROTAT, Node_Selected

D,Node_Selected,UX,0 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CSYS,100  !!!0

ALLSEL
!!!! right TMJ nodes
NSEL,s,node,,1163045
NSEL,a,node,,1163056
NSEL,a,node,,1163080
CM,Node_Selected_r,NODE

!!!! left TMJ nodes
NSEL,s,node,,1163586
NSEL,a,node,,1163594
NSEL,a,node,,1163600

CM,Node_Selected_l,NODE

!!!!!!!!!!!!!!!!!!!!!!!
ALLSEL
NROTAT, Node_Selected_l
NROTAT, Node_Selected_r

D,Node_Selected_r,UX,0 
D,Node_Selected_r,UZ,0 

!D,Node_Selected_r,UY,0 

D,Node_Selected_l,UX,0 
D,Node_Selected_l,UY,0 
D,Node_Selected_l,UZ,0 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



!!!!!!!!!!!!!!!!!!!!!!!!!! Muscle forces  !!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ALLSEL

CSYS
!CSYS,100
WPCSYS

to_skip=1       ! enter number of lines to skip
/INQUIRE,numlines,LINES,RatMuscleForcesCorrected,csv !
to_read=numlines-to_skip


*DEL,mytable,,NOPR
*DIM,mytable,TABLE,to_read,9    

*TREAD,mytable,RatMuscleForcesCorrected,csv,,to_skip

*DEL,Rat_MF,,NOPR
*DIM,Rat_MF,ARRAY,to_read,10    
!
*vfun,Rat_MF(1,1),copy,mytable(0,0)     ! Shift down and right
*vfun,Rat_MF(1,2),copy,mytable(0,1)     ! Shift down and right
*vfun,Rat_MF(1,3),copy,mytable(0,2)     ! Shift down and right
*vfun,Rat_MF(1,4),copy,mytable(0,3)     ! Shift down and right
*vfun,Rat_MF(1,5),copy,mytable(0,4)     ! Shift down and right
*vfun,Rat_MF(1,6),copy,mytable(0,5)     ! Shift down and right
*vfun,Rat_MF(1,7),copy,mytable(0,6)     ! Shift down and right
*vfun,Rat_MF(1,8),copy,mytable(0,7)     ! Shift down and right
*vfun,Rat_MF(1,9),copy,mytable(0,8)     ! Shift down and right
*vfun,Rat_MF(1,10),copy,mytable(0,9)    ! Shift down and right


!!!!!!!!!!!!!!!!!! read node number and muscle force vector components !!!!!!!!!!
to_skip=1       ! enter number of lines to skip
/INQUIRE,numlines,LINES,Rat_MusF,csv !
to_read=numlines-to_skip


*DEL,mytable1,,NOPR
*DIM,mytable1,TABLE,to_read,3

*TREAD,mytable1,Rat_MusF,csv,,to_skip

*DEL,NodeNumFV,,NOPR
*DIM,NodeNumFV,ARRAY,to_read,4
!
*vfun,NodeNumFV(1,1),copy,mytable1(0,0)    ! Shift down and right
*vfun,NodeNumFV(1,2),copy,mytable1(0,1)    ! Shift down and right
*vfun,NodeNumFV(1,3),copy,mytable1(0,2)    ! Shift down and right
*vfun,NodeNumFV(1,4),copy,mytable1(0,3)    ! Shift down and right

num_row=to_read
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! applyng muscle forces


*DO,kk,1,10

jj=1
NumOFNode=8   !the number of node in each muscle group (medial or lateral side)
!kk=1          !the column number corresponding muscle loading scenario 




*DO,ii,1,num_row


VFX=NodeNumFV(ii,2)/sqrt(NodeNumFV(ii,2)**2+NodeNumFV(ii,3)**2+NodeNumFV(ii,4)**2)
VFY=NodeNumFV(ii,3)/sqrt(NodeNumFV(ii,2)**2+NodeNumFV(ii,3)**2+NodeNumFV(ii,4)**2)
VFZ=NodeNumFV(ii,4)/sqrt(NodeNumFV(ii,2)**2+NodeNumFV(ii,3)**2+NodeNumFV(ii,4)**2)

F,NodeNumFV(ii,1),FX,VFX*Rat_MF(jj,kk)/NumOFNode
F,NodeNumFV(ii,1),FY,VFY*Rat_MF(jj,kk)/NumOFNode
F,NodeNumFV(ii,1),FZ,VFZ*Rat_MF(jj,kk)/NumOFNode

*IF,NodeNumFV(ii,1),EQ,1123193,THEN
test_nodenum1=NodeNumFV(ii,1)
test_force1=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=30


*ELSEIF,NodeNumFV(ii,1),EQ,1128367
test_nodenum2=NodeNumFV(ii,1)
test_force2=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=23

*ELSEIF,NodeNumFV(ii,1),EQ,1155237
test_nodenum3=NodeNumFV(ii,1)
test_force3=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

NumOFNode=24

*ELSEIF,NodeNumFV(ii,1),EQ,1154667
test_nodenum4=NodeNumFV(ii,1)
test_force4=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=19

*ELSEIF,NodeNumFV(ii,1),EQ,1152215
test_nodenum5=NodeNumFV(ii,1)
test_force5=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

NumOFNode=24

*ELSEIF,NodeNumFV(ii,1),EQ,1152839
test_nodenum6=NodeNumFV(ii,1)
test_force6=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=14

*ELSEIF,NodeNumFV(ii,1),EQ,1209642
test_nodenum7=NodeNumFV(ii,1)
test_force7=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

NumOFNode=15

*ELSEIF,NodeNumFV(ii,1),EQ,1210573
test_nodenum8=NodeNumFV(ii,1)
test_force8=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=14

*ELSEIF,NodeNumFV(ii,1),EQ,1156116
test_nodenum9=NodeNumFV(ii,1)
test_force9=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=12

*ELSEIF,NodeNumFV(ii,1),EQ,1155484
test_nodenum10=NodeNumFV(ii,1)
test_force10=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=32

*ELSEIF,NodeNumFV(ii,1),EQ,1189210
test_nodenum11=NodeNumFV(ii,1)
test_force11=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

NumOFNode=34

*ELSEIF,NodeNumFV(ii,1),EQ,1188675
test_nodenum12=NodeNumFV(ii,1)
test_force12=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

jj=jj+2
NumOFNode=20

*ELSEIF,NodeNumFV(ii,1),EQ,1117913
test_nodenum13=NodeNumFV(ii,1)
test_force13=Rat_MF(jj,kk)*sqrt(VFX**2+VFY**2+VFZ**2)

NumOFNode=23


*ELSE


*ENDIF


*ENDDO

!/NERR,,,-1 !switch off error checking

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Solve !!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ALLSEL
CSYS
WPCSYS

/SOLU
ANTYPE,STATIC

SOLVE

ALLSEL

/POST1
SET,1,LAST
PLNSOL, S, EQV
/CONTOUR,,9,0,7/9 !30/9

/plopts,date,0
/plopts,leg1,0
/plopts,leg2,0
/TRIAD,off

ALLSEL
CSYS
WPCSYS

*GET, NodeMax_Z, NODE, 0, MXLOC, Z
*GET, NodeMin_Z, NODE, 0, MNLOC, Z

SecNum=10                                !number of cross sections in A-P direction
SecSpa=(NodeMax_Z-NodeMin_Z)/(SecNum-1)  !spacing between sections
AP_axis=0                                !axial coordinate in AP direction

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

*DO,jj,1,SecNum  !! calculating stresses in each section

!jj=5

stressum=0                               !sum of von Mises stresses
strmean=0                                !mean of von Mises stresses of elements in one selected section
sqrsum=0                                 !square sum of differences between each element's von Mises stress and average von Mises stress in one selected section
strsd=0                                  !standard deviation of von Mises stresses of elements in one selected section                        
elemvolusum=0                            !sum of volumns of selected elements
strainsum1=0                             !sum of elastic strains
strainsum2=0                             !sum of total mechanical strains
stainmean1=0                             !mean of strains (elastic) of elements in one selected section
stainmean2=0                             !mean of strains (total mechanical) of elements in one selected section
sqrstrainsum1=0                          !square sum of differences between each element's strain and average strain in one selected section
sqrstrainsum2=0                          !square sum of differences between each element's strain and average strain in one selected section
strainsd1=0                              !standard deviation of strains of elements in one selected section
strainsd2=0                              !standard deviation of strains of elements in one selected section
volumean=0

ALLSEL
NSEL,s,loc,z,NodeMin_Z+SecSpa*(jj-1),NodeMin_Z+SecSpa*(jj-1)
ESLN,s !,1
*GET, SelNumElm, ELEM, 0, COUNT
CM,ElemsinSec,ELEM
!!!!!!!!!!!!!!!!!!

*DEL,ElemNum,,NOPR
*DIM,ElemNum,ARRAY,SelNumElm,5

CMDELE,resElem

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

*DO,ii,1,SelNumElm

ESEL,s,,,ElemsinSec
ESEL,u,,,resElem

*GET,ElemNumMin,ELEM,0,NUM,MIN
ElemNum(ii,1)=ElemNumMin

ETABLE, VMS, S, EQV                                !von Mises stress

*GET,ElemNum(ii,2), ELEM, ElemNumMin, ETAB, VMS
*GET,ElemNum(ii,3), ELEM, ElemNumMin, VOLU, 

ETABLE, ESTAN, EPEL, EQV                           !Elastic equivalent strain
*GET,ElemNum(ii,4), ELEM, ElemNumMin, ETAB, ESTAN

ETABLE, MSTAN, EPTO, EQV                           !Total equivalent mechanical strain
*GET,ElemNum(ii,5), ELEM, ElemNumMin, ETAB, MSTAN

stressum=stressum+ElemNum(ii,2)*ElemNum(ii,3)      !Total von Mises stresses of nodes on one section 
elemvolusum=elemvolusum+ElemNum(ii,3)              !Total volumns of nodes on one section 

strainsum1=strainsum1+ElemNum(ii,4)*ElemNum(ii,3)  !Total elastic strains of nodes on one section
strainsum2=strainsum2+ElemNum(ii,5)*ElemNum(ii,3)  !Total mechanical strains of nodes on one section

!!!!!!!!!!!!!!!!!!!!!!!!!!!
*IF,ElemNumMin,EQ,0,THEN

*EXIT

*ELSE

!!!!!!!!!!!!

ESEL,s,,,ElemNum(ii) 
 
ESEL,a,,,resElem
CM,resElem,ELEM

!!!!!!!!!!!!!

*ENDIF
!!!!!!!!!!!!!!!!!!!!!!!!!!!!

*ENDDO

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!! calculating mean and SD of element stress in one section

strmean=stressum/SelNumElm/(elemvolusum/SelNumElm)
stainmean1=strainsum1/SelNumElm/(elemvolusum/SelNumElm)
stainmean2=strainsum2/SelNumElm/(elemvolusum/SelNumElm)
volumean=elemvolusum/SelNumElm

*DO,ii,1,SelNumElm
sqrsum=sqrsum+(ElemNum(ii,2)*ElemNum(ii,3)-strmean*volumean)**2
sqrstrainsum1=sqrstrainsum1+(ElemNum(ii,4)*ElemNum(ii,3)-stainmean1*volumean)**2
sqrstrainsum2=sqrstrainsum2+(ElemNum(ii,5)*ElemNum(ii,3)-stainmean2*volumean)**2
*ENDDO

strsd=sqrt(sqrsum/SelNumElm)/(elemvolusum/SelNumElm)
strainsd1=sqrt(sqrstrainsum1/SelNumElm)/(elemvolusum/SelNumElm)
strainsd2=sqrt(sqrstrainsum2/SelNumElm)/(elemvolusum/SelNumElm)

!!!!!!!!!!!!!!!!!! read results into txt file

CSYS
ALLSEL
AP_axis=SecSpa*(jj-1)

*CFOPEN,output_ratskull_AP_results_corrected,txt,,append

*VWRITE,AP_axis,strmean,strsd,stainmean1,strainsd1,stainmean2,strainsd2,elemvolusum,volumean,
(F14.9,',',F14.9,',',F14.9,','F14.9,',',F14.9,',',F14.9,',',F14.9,','F14.9,',',F14.9,',')

*CFCLOSE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!'*ENDDO' of  '*DO,jj,1,SecNum'
*ENDDO

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! delete all forces on nodes
/PREP7
FDELE,all,all 

!! '*ENDDO' of 'kk' loop
*ENDDO
