C  MAGNORM.FOR. FORTRAN 77 CODED PROGRAM
C  Vers:1.0;  January 1995
C  *****************************************************
      INTEGER   IN,OUT,REF,RESULT,OXOUT(11),NOXOUT,LOOPCNT
      REAL      Lm,IRC,V1,V2
      PARAMETER (IN=5, OUT=6, REF=9, RESULT=10)
      CHARACTER*1 ANSWER
      CHARACTER*5 OXIDE(11), TITLEX
      CHARACTER*30 NAME
      DIMENSION WTP(11),V(9),CATPC(11),H(3)
C  ********************************************
C  WTP - WeighT Percent. CATPC - CATion PerCent
C  ********************************************
      DATA OXIDE(1)/'SiO2 '/
      DATA OXIDE(2)/'Al2O3'/
      DATA OXIDE(3)/'Fe2O3'/
      DATA OXIDE(4)/'FeO  '/
      DATA OXIDE(5)/'MnO  '/
      DATA OXIDE(6)/'MgO  '/
      DATA OXIDE(7)/'CaO  '/
      DATA OXIDE(8)/'H2O  '/
      DATA OXIDE(9)/'ZrO2 '/
      DATA OXIDE(10)/'Cr2O3'/
      DATA OXIDE(11)/'B2O3 '/
C  **************************
      LOOPCNT=0.0
      SUM=0.0
      SUM1=0.0
      WRITE(OUT,10)
10    FORMAT(' Sample No: ?')
      READ(IN,20)NAME
20    FORMAT(A)
      WRITE(OUT,30)
30    FORMAT(' How many elements have been analyzed: ?')
      READ(IN,40)NUM
40    FORMAT(I2)
      WRITE(OUT,45)
45    FORMAT(' Use only: SiO2, Al2O3, Fe2O3, FeO, MnO, MgO, CaO, H2O, Zr
     &O2, Cr2O3, B2O3'/)
      WRITE(OUT,50)
50    FORMAT(' Enter: Oxides and Wt-%')
      WRITE(OUT,55)
55    FORMAT(' For Oxides (column 1-5), for Wt-% (column 6 onwards)'/)
      DO 5 IJ=1,NUM
2     READ(IN,60)TITLEX,WTPER
60    FORMAT(A,F8.4)
      DO 3 I=1,11
      NUMCOM=I
      IF(TITLEX.EQ.OXIDE(I)) GOTO 4
3     CONTINUE
      WRITE(OUT,70)TITLEX
70    FORMAT(/' No compound called ',A,' in data')
      WRITE(OUT,80)
80    FORMAT(' Do you want to try again (Y/y)?')
      READ(IN,20) ANSWER
      IF (ANSWER.EQ.'Y'.OR.ANSWER.EQ.'y') GOTO 2
      STOP
4     CONTINUE
      IF (I.LT.14) SUM=SUM+WTPER
      WTP(NUMCOM)=WTPER
5     CONTINUE
      OPEN(UNIT=RESULT, FILE='MAG.OUT')
      WRITE(RESULT,90)NAME
90    FORMAT(15X,' S A M P L E: ',A)
      WRITE(RESULT,1000)
1000  FORMAT(1X,73('*'))
      WRITE(RESULT,100)
100   FORMAT(3X,'Oxides',4X,'wt-%',4X,'Oxides',4X,'wt-%',4X,'Oxides',
     &4X,'wt-%',4X,'Oxides',4X,'wt-%')
      NOXOUT=0.0
      DO 6 I=1,11
      CATPC(I)=WTP(I)
      SUM1=SUM1+CATPC(I)
6     CONTINUE
      DO 7 IJ=1,11
      CATPC(IJ)=100*CATPC(IJ)/SUM1
      IF (WTP(IJ).NE.0.0) THEN
      NOXOUT=NOXOUT+1
      OXOUT(NOXOUT)=IJ
      END IF
7     CONTINUE
      WRITE(RESULT,110)(OXIDE(OXOUT(I)),WTP(OXOUT(I)), I=1,NOXOUT)
110   FORMAT(3X,A,F9.3,4X,A,F9.3,4X,A,F9.3,4X,A,F9.3)
      WRITE(RESULT,120)SUM
120   FORMAT(/,2X,6H Total,F9.3)
C     *************
      V(1)=CATPC(1)
      H(1)=CATPC(1)
C     *************
      V(2)=CATPC(2)
C     *************
      V(3)=CATPC(3)
      H(2)=V(3)
C     *************
      V(4)=CATPC(5)+CATPC(6)
C     *************
      V(5)=CATPC(7)
      H(3)=CATPC(7)
C     *************
      V(6)=CATPC(8)
      V(7)=CATPC(9)
      V(8)=CATPC(10)
      V(9)=CATPC(11)
C     *****************
      WRITE(RESULT,1000)
      WRITE(RESULT,1000)
C  ********************
C  CALCIUM:SILICA RATIO (C/SR)=CaO/SiO2
C  ********************
      IF (H(1).LE.0.0.OR.H(3).LE.0.0) GOTO 130
      CSR=0.0
      CSR=H(3)/H(1)
      GOTO 140
130   CSR=0.0
C  **********
C  IRON:CALCIUM RATIO (IR/C)=FeO/CaO  
C  ******************
140   IF (H(2).LE.0.0.OR.H(3).LE.0.0) GOTO 150
      IRC=H(2)/H(3)
      GOTO 160
150   IRC=0.0
C  *******************************************************
C  ******         CALCULATION OF THE PHASES         ******
C  *******************************************************
C
C  *************************
C  1. Brucite (Bru): MgO.H2O
C  *************************
160   IF (V(6).LE.0.0) GOTO 170
      A=2.237*V(6)
      Bru=V(6)+A
      V(4)=V(4)-A
      V(6)=0.0
C  ***************************
C  2. Chromite (Cm): FeO.Cr2O3
C  ***************************
170   A=0.0
      IF (V(8).LE.0.0) GOTO 180
      A=0.473*V(8)
      Cm=V(8)+A
      V(3)=V(3)-(1.1113*A)
      V(8)=0.0
      GOTO 190
C  ***********************************
C  3. Calcium Zirconate (Cz): CaO.ZrO2
C  ***********************************
180   A=0.0
      IF (V(7).LE.0.0) GOTO 190
      A=0.455*V(7)
      Cz=V(7)+A
      V(5)=V(5)-A
      V(7)=0.0
190   V(1)=V(1)+V(2)
      V(4)=V(4)+V(3)
      V1=V(1)
      V2=V(4)
      V3=V(5)
      IF (Cz.GT.0.0) GOTO 200
      IF (Cm.GT.0.0) GOTO 270
      IF (IRC.GE.0.45.AND.CSR.GT.4.0) GOTO 310
      IF (IRC.GE.0.45.AND.CSR.GT.1.0.AND.CSR.LT.4.0) GOTO 200
      IF (IRC.GE.0.45.AND.CSR.LE.1.0) GOTO 270
      IF (IRC.GE.0.3.AND.IRC.LT.0.45.AND.CSR.LT.2.0) GOTO 270
      IF (IRC.GT.0.1.AND.IRC.LT.0.3. AND.CSR.LT.2.0) GOTO 310 
      IF (IRC.GT.0.1.AND.CSR.LE.1.0) GOTO 270
      GOTO 230
C  ************************************
C  5. Bredigite (C7MS4): 7CaO.MgO.4SiO2
C  ************************************
200   A=0.0
      B=0.0
      IF (V(5).GT.V(1)) GOTO 210
C  CaO<SiO2  *****
      A=0.103*V(5)
      B=0.612*V(5)
      ECMS=V(5)+A+B
      V(1)=V(1)-B
      V(4)=V(4)-A 
      V(5)=0.0
      GOTO 330
C  CaO>SiO2  *****
210   A=0.168*V(1)
      B=1.633*V(1)
      ECMS=V(1)+A+B
      V(4)=V(4)-A
      V(5)=V(5)-B
      V(1)=0.0
      IF (V(5).LT.0.0) THEN 
       ECMS=0.0
	V(1)=V1
	V(5)=V3
       GOTO 270
      END IF
      GOTO 330
C  **************************************
C  6. Dicalcium Silicate (C2S): 2CaO.SiO2
C  **************************************
220   LOOPCNT=LOOPCNT+1
      V(1)=VA
      V(4)=VB
      V(5)=VC
230   A=0.0
      B=0.0
      C=0.0
      IF (V(1).GT.V(5)) GOTO 250
C  SiO2<CaO  *****
      A=1.866*V(1)
      DCS=V(1)+A
      IF ((V(5)-A).LT.0.0) GOTO 240
      V(5)=V(5)-A
      V(1)=0.0
      GOTO 330 
C  (CaO-(1.866*SiO2))<0.0
240   A=V(5)/1.866
      B=V(1)-A
      C=1.866*B
      DCS=B+C
      V(5)=V(5)-C
      V(1)=V(1)-B
      GOTO 270
C  SiO2>CaO  *****
250   C=0.536*V(5)
      DCS=V(5)+C
      IF ((V(1)-C).LT.0.0) GOTO 260
      V(1)=V(1)-C
      V(5)=0.0
      IF (V(1).GT.0.0.AND.LOOPCNT.EQ.0) THEN
	DCS=0.0
	 V(1)=V1
	V(5)=V3
      END IF
      IF (LOOPCNT.GT.0) V(5)=A1
      GOTO 270
C  (SiO2-(0.536*CaO))<0.0
260   A=V(1)/0.536
      B=V(5)-A
      C=0.536*B
      DCS=B+C
      V(5)=V(5)-B
      V(1)=V(1)-C
C  ***********************************
C  7. Monticellite (CMS): CaO.MgO.SiO2
C  ***********************************
270   A=0.0
      B=0.0
      C=0.0
      D=0.0
      IF (V(1).GT.V(5)) GOTO 290
      A=0.933*V(1)
      B=0.671*V(1)
      CMS=V(1)+A+B
      IF ((V(5)-A).LT.0.0) GOTO 280
      V(4)=V(4)-B
      V(5)=V(5)-A
      V(1)=0.0
      GOTO 330
C  IF (CaO-(0.933*SiO2))<0.0
280   A=V(5)/0.933
      B=V(1)-A
      C=0.933*B
      D=0.671*B
      CMS=B+C+D
      V(1)=V(1)-A
      V(5)=V(5)-C
      V(4)=V(4)-D
      IF (LOOPCNT.GT.1) GOTO 310
      IF (V(1).GT.0.0) THEN 
       DCS=0.0
	CMS=0.0
	 A1=(V(1)/1.071)+((V(1)/1.071)*0.755)
	 VA=V1-V(1)
	VB=V2
       VC=V3-A1
      END IF
      GOTO 220
C  IF SiO2<CaO  *****
290   A=1.071*V(5)
      B=0.719*V(5)
      CMS=V(5)+A+B
      IF ((V(1)-A).LT.0.0) GOTO 300
      V(1)=V(1)-A
      V(4)=V(4)-B
      V(5)=0.0
      IF (LOOPCNT.GT.1) GOTO 330
      IF (V(1).GT.0.0) THEN 
       DCS=0.0
	CMS=0.0
	 A1=(V(1)/1.071)+((V(1)/1.071)*0.755)
	 VA=V1-V(1)
	VB=V2
       VC=V3-A1
      END IF
      GOTO 220
C  (SiO2-(1.071*CaO))<0.0
300   A=V(1)/1.071
      A1=V(5)-A
      B=1.071*A1
      C=0.719*A1
      CMS=A1+B+C
      V(1)=V(1)-A
      V(5)=V(5)-B
      V(4)=V(4)-C
      IF (LOOPCNT.GT.1) GOTO 310
      IF (V(1).GT.0.0) THEN 
       DCS=0.0
	CMS=0.0
	 A1=(V(1)/1.071)+((V(1)/1.071)*0.755)
	 VA=V1-V(1)
	VB=V2
       VC=V3-A1
      END IF
      GOTO 220
C  ************************************
C  8. Merwinite (C3MS2): 3CaO.MgO.2SiO2
C  ************************************
310   A=0.00
      B=0.00
      IF (V(1).LE.0.0.OR.V(5).LE.0.0) GOTO 330
      IF (V(1).GT.V(5)) GOTO 320
C  CaO>SiO2 *****
      A=1.4*V(1)
      B=0.335*V(1)
      TCMS=V(1)+A+B
      V(5)=V(5)-A
      V(4)=V(4)-B
      V(1)=0.0
      IF (V(5).LT.0.0)  THEN
       TCMS=0.0
	V(5)=V(5)+A
	 V(4)=V(4)+B
	V(1)=B/0.335
       GOTO 270
      END IF
      GOTO 330
C  CaO<SiO2  *****
320   A=0.714*V(5)
      B=0.239*V(5)
      TCMS=V(5)+A+B
      V(1)=V(1)-A
      V(4)=V(4)-B
      V(5)=0.0
      IF (V(1).GT.0.0) THEN
       TCMS=0.0
	V(1)=V(1)+A
	 V(4)=V(4)+B
	V(5)=B/0.239
       GOTO 270
      END IF
C  ***********************
C  9. Periclase (Per): MgO
C  ***********************
330   Per=V(4)
C  ******************
C  10. Lime (Lm): CaO
C  ******************
      Lm=V(5)
C  **********************
C  11. Excess Silica (Si)
C  **********************
      Si=V(1)
C  ********************
      WRITE(RESULT,340)
340   FORMAT(/2X,' Periclase   Lime   Brucite  Chromite   Excess Silica
     &')
      WRITE(RESULT,350)Per,Lm,Bru,Cm,Si
350   FORMAT(1X,4F9.2,F12.2)
      WRITE(RESULT,360)
360   FORMAT(///5X,' C2S     CMS      C3MS2   C7MS4     CZ   ')
      WRITE(RESULT,370)DCS,CMS,TCMS,ECMS,CZ
370   FORMAT(2X,2F8.2,2F9.2,F9.2)
      WRITE(RESULT,1000)
      WRITE(RESULT,380)
380   FORMAT(/2X,' Ca:Si Ratio    Fe:Ca Ratio  ')
      WRITE(RESULT,390) CSR,IRC
390   FORMAT(3X,F7.2,9X,F8.2)
      WRITE(RESULT,1000)
      STOP
      END

