
/PREP7

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

ESEL,u,MAT,,1  !molar
ESEL,u,MAT,,39 !pulp
ESEL,u,MAT,,41 !dentine
ESEL,u,MAT,,43 !enamal
ESEL,u,MAT,,45 !bone

ESEL,u,MAT,,12 !PDL incisor
ESEL,u,MAT,,31 !PDL incisor

CM,PDL_molar,ELEM

*DEL,MatNum,,NOPR
*DIM,MatNum,ARRAY,26,1
ii=1
x=1
restElem=0

*DOWHILE,x

ESEL,s,,,PDL_molar
ESEL,u,,,restElem

*GET,ElemNumMin,ELEM,0,NUM,MIN

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

*EXIT

*ELSE

!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*GET,MatNum(ii),ELEM,ElemNumMin,ATTR,MAT

ESEL,s,MAT,,MatNum(ii) 
 
ESEL,a,,,restElem
CM,restElem,ELEM

ii=ii+1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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

*ENDDO




ESEL,s,MAT,,12 !PDL incisor
ESEL,a,MAT,,31 !PDL incisor

CM,PDL_incisor,ELEM

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

!!!!!!!!!!!!!!!  molar
MP,EX,1,3E4 !*1E6         
MP,PRXY,1,0.3

!!!!!!!!!!!!!!!  pulp
MP,EX,39,2.03 !*1E6       
MP,PRXY,39,0.45

!!!!!!!!!!!!!!!  dentine
MP,EX,41,18300 !*1E6       
MP,PRXY,41,0.31

!!!!!!!!!!!!!!!  enamal
MP,EX,43,82500 !*1E6   
MP,PRXY,43,0.33

!!!!!!!!!!!!!!!  bone
MP,EX,45,20000 !*1E6    
MP,PRXY,45,0.3

!!!!!!!!!!!!!! PDL_incisor
MP,EX,12,50 !*1E6
MP,PRXY,12,0.4

MP,EX,31,50 !*1E6
MP,PRXY,31,0.4

!!!!!!!!!!!!!! PDL_molar
*DO,ii,1,26

MP,EX,MatNum(ii),50 !*1E6
MP,PRXY,MatNum(ii),0.4

*ENDDO


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


!!!!!!!!!!!!!!!!!! defining local CS for new joint
CSYS
!N,1e6,0,0,0

WPCSYS
CSWPLA,100,0
CSWPLA,101,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,,499275
NSEL,a,node,,498317
CM,Node_Selected,NODE

CSYS,0 

D,Node_Selected,UX,0 

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

ALLSEL
!!!! right TMJ nodes
NSEL,s,node,,618916
NSEL,a,node,,618896
NSEL,a,node,,618885
CM,Node_Selected_r,NODE

!!!! left TMJ nodes
NSEL,s,node,,617330
NSEL,a,node,,617315
NSEL,a,node,,616614

CM,Node_Selected_l,NODE

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

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

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

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


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

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

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


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

*TREAD,mytable,SquirrelMuscleForcesCorrected,csv,,to_skip

*DEL,Squ_MF,,NOPR
*DIM,Squ_MF,ARRAY,to_read,10    
!
*vfun,Squ_MF(1,1),copy,mytable(0,0)     ! Shift down and right
*vfun,Squ_MF(1,2),copy,mytable(0,1)     ! Shift down and right
*vfun,Squ_MF(1,3),copy,mytable(0,2)     ! Shift down and right
*vfun,Squ_MF(1,4),copy,mytable(0,3)     ! Shift down and right
*vfun,Squ_MF(1,5),copy,mytable(0,4)     ! Shift down and right
*vfun,Squ_MF(1,6),copy,mytable(0,5)     ! Shift down and right
*vfun,Squ_MF(1,7),copy,mytable(0,6)     ! Shift down and right
*vfun,Squ_MF(1,8),copy,mytable(0,7)     ! Shift down and right
*vfun,Squ_MF(1,9),copy,mytable(0,8)     ! Shift down and right
*vfun,Squ_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,Squirrel_MusF,csv !squat_anklejoint_vertical_load_modif
to_read=numlines-to_skip


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

*TREAD,mytable1,Squirrel_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*Squ_MF(jj,kk)/NumOFNode
F,NodeNumFV(ii,1),FY,VFY*Squ_MF(jj,kk)/NumOFNode
F,NodeNumFV(ii,1),FZ,VFZ*Squ_MF(jj,kk)/NumOFNode

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

jj=jj+2
NumOFNode=47


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

NumOFNode=37

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

jj=jj+2
NumOFNode=25

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

NumOFNode=20

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

jj=jj+2
NumOFNode=40

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

NumOFNode=40

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

jj=jj+2
NumOFNode=9

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

jj=jj+2
NumOFNode=11

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

jj=jj+2
NumOFNode=10

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

jj=jj+2
NumOFNode=37

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

NumOFNode=33

*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,LEG1,0
/plopts,LEG2,0
/plopts,Date,0
/plopts,TITLE,0

/plopts,FRAMES,off

/triad,off

!/plopts,LOGO,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=2

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

!!!!!!!!!!!!!!!
CSYS
WPCSYS


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



*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_skull_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
