summaryrefslogtreecommitdiff
path: root/Donjon/src/PCREAD.f90
blob: db378d09a6626b01aeb499edddeac1bb7e7adf3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
MODULE PCREAD
!
!-----------------------------------------------------------------------
!
!Purpose:
! Fortran support module for PMAXS reading.
!
!Copyright:
! Copyright (C) 2019 Ecole Polytechnique de Montreal
!
!Author(s): A. Hebert
!
!-----------------------------------------------------------------------
!
    use PCRDATA

    IMPLICIT NONE

    TYPE(PMAXS_WISE_TYPE),target :: PMAXO
    CHARACTER(80), ALLOCATABLE ::PMAXS_F_name(:)
    INTEGER(4),   ALLOCATABLE ::Bran_struct(:)

    INTEGER(4) :: ntbase1,nhinc
    TYPE(BRANCH_WISE_TYPE), DIMENSION(:,:), POINTER :: incbase !(ntbase1,nhinc)
    INTEGER(4), DIMENSION(:), allocatable :: bset

    INTEGER(4) :: n_hist_type, hist_type(Nallvar)

CONTAINS

!---------------------------------------------------------------------
   SUBROUTINE AllocateBranch(Bran)
!---------------------------------------------------------------------
!
    IMPLICIT NONE   
   
    TYPE(BRANCH_WISE_TYPE),target :: Bran
    INTEGER(4) :: k,NBURN
    NBURN=PMAX%Bset(Bran%ibset)%NBURN
    allocate(Bran%XS(NBURN))
    do k=1,NBURN
        XS=>Bran%XS(k)
        call AllocateXSBlock
    enddo
   END SUBROUTINE AllocateBranch

!---------------------------------------------------------------------
   SUBROUTINE ClearBranch(Bran)
!---------------------------------------------------------------------
!
    IMPLICIT NONE   
   
    TYPE(BRANCH_WISE_TYPE),target :: Bran
    INTEGER(4) :: k,NBURN

    NBURN=PMAX%Bset(bran%ibset)%NBURN
    do k=1,NBURN
        XS=>bran%XS(k)
        CALL Clear_XS
    enddo
    deallocate(Bran%XS)
   END SUBROUTINE ClearBranch

!---------------------------------------------------------------------
   SUBROUTINE AllocateTIVB(TIVB)
!---------------------------------------------------------------------
!
    IMPLICIT NONE
   
    TYPE(HIST_TIV_TYPE),target :: TIVB
    INTEGER(4) :: k,NBURN

    NBURN=PMAX%Bset(TIVB%ibset)%NBURN
    allocate(TIVB%TIV(NBURN))
    do k=1,NBURN
       TIV=>TIVB%TIV(k)
       if(xinv .GT. 0)then
         allocate(TIV%sig(NGROUP,xinv))
       else
         allocate(TIV%sig(1,1))
       endif
       if(EDHL .GT. 0)then
         allocate(TIV%kinp(EDHL))
       else
         allocate(TIV%kinp(1))
      endif
      TIV%sig=0
      TIV%kinp=0
      TIV%yld=0
      TIV%power=0.0
      TIV%days=0.0
      TIV%burnup=0.0      
      TIV%ndxe=0.0
      TIV%ndsm=0.0
      TIV%ndi =0.0
    enddo
   END SUBROUTINE AllocateTIVB

!---------------------------------------------------------------------
   SUBROUTINE ClearTIVB(TIVB)
!---------------------------------------------------------------------
!
    IMPLICIT NONE
   
    TYPE(HIST_TIV_TYPE),target :: TIVB
    INTEGER(4) :: k,NBURN

    NBURN=PMAX%Bset(TIVB%ibset)%NBURN
    do k=1,NBURN
        TIV=>TIVB%TIV(k)
        !deallocate(TIVB%TIV(k)%sig) ! commented for flang
        deallocate(TIVB%TIV(k)%kinp)
    enddo
    deallocate(TIVB%TIV)
   END SUBROUTINE ClearTIVB

!---------------------------------------------------------------------
   SUBROUTINE read_TIV(PMAXS_unit)
!---------------------------------------------------------------------
      IMPLICIT NONE
      REAL, ALLOCATABLE, DIMENSION(:,:) :: TIVtemp
      INTEGER(4) :: PMAXS_unit
      INTEGER(4) i,j,k  !,m
      REAL(8) dump
99    format(8E12.5)

!1)   fission spectrum inverse velocity and detector xs
        if(iXSTI .GT. 0)then
          allocate(TIVtemp(NGROUP,4))
          read(PMAXS_unit,99)((TIVtemp(i,j),i=1,NGROUP),j=1,iXSTI)
          do j=1,iXSTI
             k=iTIV(j)
             if(k .GT. 0)then
                do i=1,NGROUP
                   TIV%sig(i,k)=TIVtemp(i,j)
                enddo
             endif
          enddo
          deallocate(TIVtemp)
       endif
!2)   yiled
    if(pyld)then
       if(lyld)then
          read(PMAXS_unit,99)TIV%YLD(:)
       else
          read(PMAXS_unit,99)
       endif
    endif

!cdf
       IF(tcdf)THEN
          READ(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,NCD)
       ENDIF
! gff
       if(tgff.and.NRODS .GT. 0)then
             read(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,NRODS)
       endif

!3)   BETA of Delayed neutron data
    if(pbet)then
        if(lbet)then
           read(PMAXS_unit,99)TIV%kinp(BBET:EBET)
        else
           read(PMAXS_unit,99)(dump,i=1,NDLAY)
        endif
     endif
!4)lambda of Delayed neutron data
    if(pamb)then
        if(lamb)then
           read(PMAXS_unit,99)TIV%kinp(BLAM:ELAM)
        else
           read(PMAXS_unit,99)(dump,i=1,NDLAY)
        endif
     endif
!5)   Decay heat data
    if(pdec)then
        if(ldec)then
              read(PMAXS_unit,99)TIV%kinp(BDHB:EDHB)
              read(PMAXS_unit,99)TIV%kinp(BDHL:EDHL)
        else
          read(PMAXS_unit,99)(dump,i=1,NDCAY)
          read(PMAXS_unit,99)(dump,i=1,NDCAY)
        endif
     endif
     return
   END SUBROUTINE read_TIV

!---------------------------------------------------------------------
   SUBROUTINE read_XS_Block(PMAXS_unit)
!---------------------------------------------------------------------
       IMPLICIT NONE
       INTEGER(4) :: PMAXS_unit
       REAL(8) LPFtemp(8)
       REAL(8) dump
       INTEGER(4) i,j  !,k
       read(PMAXS_unit,99)((XS%sig(i,j),i=1,NGROUP),j=1,4)

       if(pxes)then
          if(pdet)then
             if(lxes)then
                if(ldet)then
                   read(PMAXS_unit,99)((XS%sig(i,j),i=1,NGROUP),j=5,7),XS%det
                else
                   read(PMAXS_unit,99)((XS%sig(i,j),i=1,NGROUP),j=5,7),(dump,i=1,NGROUP)
                endif
             else
                if(ldet)then
                   read(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,3),XS%det
                else
                   read(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,4)
                endif
             endif
          else
             if(lxes)then
                   read(PMAXS_unit,99)((XS%sig(i,j),i=1,NGROUP),j=5,7)
                   XS%sig(1,5) = XS%sig(1,5) * 1E24
                   XS%sig(2,5) = XS%sig(2,5) * 1E24
                   XS%sig(1,6) = XS%sig(1,6) * 1E24
                   XS%sig(2,6) = XS%sig(2,6) * 1E24
             else
                   read(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,3)
             endif
          endif
       else
          if(pdet)then
                if(ldet)then
                   read(PMAXS_unit,99)XS%det
                else
                   read(PMAXS_unit,99)(dump,i=1,NGROUP)
                endif
          endif
       endif
! sct (scattering cross sections)
       read(PMAXS_unit,99)XS%sct
! adf
            IF(padf)THEN
               IF(ladf)THEN
                  READ(PMAXS_unit,99)((XS%adf(i,j),i=1,NGROUP),j=1,NAD)
                  if(NAD .LT. NADF)then
                     if(NAD .EQ. 1)then
                        do i=1,NGROUP
                            XS%adf(i,:)=XS%adf(i,1)
                        enddo
                     elseif(NAD .EQ. 2)then
                        if(NADF .EQ. 3) then
                           call XABORT('read_XS_Block: Error - Please Use Same NADF In All PMAXS Files')
                        elseif(NADF .EQ. 4)then
                            do i=1,NGROUP
                               XS%adf(i,3)=XS%adf(i,2)
                               XS%adf(i,4)=XS%adf(i,1)
                            enddo
                        else
                            do i=1,NGROUP
                               XS%adf(i,3)=XS%adf(i,1)
                               XS%adf(i,4)=XS%adf(i,2)
                               XS%adf(i,5)=XS%adf(i,1)
                               XS%adf(i,6)=XS%adf(i,2)
                            enddo
                        endif
                     else
                            do i=1,NGROUP
                               XS%adf(i,4)=XS%adf(i,1)
                               XS%adf(i,5)=XS%adf(i,2)
                               XS%adf(i,6)=XS%adf(i,3)
                            enddo
                     endif
                  endif
               ELSE
                  READ(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,NAD)
               ENDIF
            ENDIF
! lpf
  99   format(8e12.5)
       if(iLPF .GT. 0)then
          read(PMAXS_unit,99)(LPFtemp(j),j=1,iLPF)
          if(pded.and.lded)XS%LPF(1:4)=LPFtemp(1:4)
          if(pj1f.and.lj1f)XS%LPF(xlpk:xj1c)=LPFtemp(ilpk:ij1c)
       endif
! cdf
            IF(pcdf)THEN
               IF(lcdf)THEN
                  READ(PMAXS_unit,99)((XS%cdf(i,j),i=1,NGROUP),j=1,NCD)
                  if(NCD .LT. NCDF)then
                     if(NCD .EQ. 1)then
                        do i=1,NGROUP
                            XS%cdf(i,:)=XS%cdf(i,1)
                        enddo
                     elseif(NCD .EQ. 2)then
                               if(NCDF .EQ. 5)then
                                  do i=1,NGROUP
                                     XS%cdf(i,5)=XS%cdf(i,2)
                                     XS%cdf(i,4)=XS%cdf(i,2)
                                     XS%cdf(i,3)=XS%cdf(i,1)
                                     XS%cdf(i,2)=XS%cdf(i,1)
                                  enddo
                               elseif(NCDF .EQ. 6)then
                                  do i=1,NGROUP
                                     XS%cdf(i,3)=XS%cdf(i,1)
                                     XS%cdf(i,4)=XS%cdf(i,2)
                                     XS%cdf(i,5)=XS%cdf(i,1)
                                     XS%cdf(i,6)=XS%cdf(i,2)
                                  enddo
                               elseif(NCDF .EQ. 8)then
                                  do i=1,NGROUP
                                     XS%cdf(i,8)=XS%cdf(i,2)
                                     XS%cdf(i,7)=XS%cdf(i,2)
                                     XS%cdf(i,6)=XS%cdf(i,2)
                                     XS%cdf(i,5)=XS%cdf(i,2)
                                     XS%cdf(i,4)=XS%cdf(i,1)
                                     XS%cdf(i,3)=XS%cdf(i,1)
                                     XS%cdf(i,2)=XS%cdf(i,1)
                                  enddo
                               else
                                  call XABORT('read_XS_Block: Error - Please Use Same NCDF In All PMAXS Files')
                               endif
                     elseif(NCD .EQ. 3)then
                               if(NCDF .EQ. 4)then
                                  do i=1,NGROUP
                                     XS%cdf(i,4)=XS%cdf(i,2)
                                  enddo
                               elseif(NCDF .EQ. 5)then
                                  do i=1,NGROUP
                                     XS%cdf(i,4)=XS%adf(i,1)
                                     XS%cdf(i,5)=XS%adf(i,2)
                                  enddo
                               elseif(NCDF .EQ. 6)then
                                  do i=1,NGROUP
                                     XS%cdf(i,4)=XS%cdf(i,1)
                                     XS%cdf(i,5)=XS%cdf(i,2)
                                     XS%cdf(i,6)=XS%cdf(i,3)
                                  enddo
                               elseif(NCDF .EQ. 8)then
                                  do i=1,NGROUP
                                     XS%cdf(i,4)=XS%cdf(i,2)
                                     XS%cdf(i,5)=XS%adf(i,1)
                                     XS%cdf(i,6)=XS%adf(i,2)
                                     XS%cdf(i,7)=XS%adf(i,2)
                                     XS%cdf(i,8)=XS%adf(i,1)
                                  enddo
                               endif
                     elseif(NCD .EQ. 4)then
                              if(NCDF .EQ. 8)then
                                  do i=1,NGROUP
                                     XS%cdf(i,5)=XS%adf(i,1)
                                     XS%cdf(i,6)=XS%adf(i,2)
                                     XS%cdf(i,7)=XS%adf(i,3)
                                     XS%cdf(i,8)=XS%adf(i,4)
                                  enddo
                              else
                                  call XABORT('read_XS_Block: Error - Please Use Same NCDF In All PMAXS Files')
                              endif
                     elseif(NCD .EQ. 5)then
                                  do i=1,NGROUP
                                     XS%cdf(i,8)=XS%cdf(i,4)
                                     XS%cdf(i,7)=XS%cdf(i,5)
                                     XS%cdf(i,6)=XS%cdf(i,5)
                                     XS%cdf(i,5)=XS%cdf(i,4)
                                     XS%cdf(i,4)=XS%cdf(i,2)
                                  enddo
                     else
                                  call XABORT('read_XS_Block: Please use same NCDF in all PMAXS files')
                     endif
                  endif
               ELSE
                  READ(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,NCD)
               ENDIF
            ENDIF
! gff
       if(pgff.and.NRODS .GT. 0)then
          if(lgff)then
             read(PMAXS_unit,99)XS%gff
          else
             read(PMAXS_unit,99)((dump,i=1,NGROUP),j=1,NRODS)
          endif
       endif
       return
   END SUBROUTINE read_XS_Block

!---------------------------------------------------------------------
   SUBROUTINE det_var_position
!---------------------------------------------------------------------
!          determine variable position in input PMAXS file
         
         IMPLICIT NONE
         
         INTEGER(4) i  !, i_pri,i_adf,i_lpf
! XS block
! LPF
         if(pded)then
            i=4
         else
            i=0
         endif
         if(pj1f)then
            ilpk=i+1
            ij1c=i+4
            iLPF=ij1c
         else
            iLPF=i
         endif

! TIV block
         if(pchi)then
            i=1
            iTIV(i)=xchi
         else
            i=0
         endif
         if(pchd)then
            i=i+1
            iTIV(i)=xchd
         endif
         if(pvel)then
            i=i+1
            iTIV(i)=xinv
         endif
         iXSTI=i
   END SUBROUTINE det_var_position

!---------------------------------------------------------------------
   SUBROUTINE set_var_position
!---------------------------------------------------------------------
!          set variable position in memory and output PMAXS
         
         IMPLICIT NONE
         INTEGER(4) :: i
         formng='(1P008E12.5)'
         if(NGROUP .GT. 4)then
            write(formng(4:6),'(I3.3)')NGROUP
         elseif(NGROUP .EQ. 3)then
            formng='(1P006E12.5)'
         endif

         if(NADF .EQ. 0)ladf=.false.
         if(NCDF .EQ. 0)lcdf=.false.
         if(NRODS .EQ. 0)lgff=.false.

!    ded
         if(lded)then
            i=4
         else
            i=0
         endif
         if(lj1f)then
            xlpk=i+1
            xj1i=i+2
            xj1s=i+3
            xj1c=i+4
            NLPF=xj1c
         else
            NLPF=i
         endif

! TIV block
         if(lchi)then
            i=1
             TIVname(i)='Chi'
         else
            i=0
         endif
         if(lchd)then
            i=i+1
            TIVname(i)='Chd'
         endif
         xchd=i
         if(linv)then
            i=i+1
            TIVname(i)='inV'
         endif
         xinv=i

! beta and lambda
         EBET=NDLAY
         BLAM=EBET+1
         ELAM=EBET+NDLAY
! decay heat
         BDHB=ELAM+1
         EDHB=ELAM+NDCAY
         BDHL=EDHB+1
         EDHL=EDHB+NDCAY

! format
         formng='(1P008E12.5)'
         if(NGROUP .GT. 4)then
             write(formng(4:6),'(I3.3)')NGROUP
         elseif(NGROUP .EQ. 3)then
             formng='(1P006E12.5)'
         endif
   END SUBROUTINE set_var_position

!---------------------------------------------------------------------
   SUBROUTINE read_PMAXS_file(iPMAX,kread,PMAXS_unit)
!---------------------------------------------------------------------
      use PCRDATA

      IMPLICIT NONE

      INTEGER(4) :: iPMAX,kread,PMAXS_unit
      INTEGER(4) :: itemp,i_s
      CHARACTER(8) :: tit
      CHARACTER(80) :: oneline

      read(PMAXS_unit,'(A80)',end=101)oneline
      if(oneline(1:8).NE.'GLOBAL_V') call XABORT('dep_read_pmaxs_file: GLOBAL_V expected.')
!1) global variables
      if(oneline(64:64).eq.' ')then
         read(oneline,*)tit,NHST,NGR,NDL,NDC,NAD,NCD,NRODS,NCOLA, &
            padf,pxes,pded,pj1f,pchi,pchd,pvel,pdet,pyld,pcdf,pgff,pbet,pamb,pdec
            derivatives=.true.
            pzdf = .FALSE.
      else if(oneline(66:66).eq.' ')then
         read(oneline,*)tit,NHST,NGR,NDL,NDC,NAD,NCD,NRODS,NCOLA, &
            padf,pxes,pded,pj1f,pchi,pchd,pvel,pdet,pyld,pcdf,pgff,pbet,pamb,pdec,derivatives
            pzdf = .FALSE.
      else
         read(oneline,*)tit,NHST,NGR,NDL,NDC,NAD,NCD,NRODS,NCOLA, &
            padf,pxes,pded,pj1f,pchi,pchd,pvel,pdet,pyld,pcdf,pgff,pbet,pamb,pdec,pzdf,derivatives
      endif
      if(kread.LE.0)THEN
        if(kread .EQ. -1)THEN
          NGROUP=NGR
          NDLAY =NDL
          NDCAY =NDC
          NADF  =NAD
          NCDF  =NCD
          MHST  =NHST
          MRODS =NRODS
          MCOLA =NCOLA
          if(MCOLA .LT. NROWA)MCOLA=NROWA
          MBset=1
          MBRA=1
          MBCR=0
        else
          if(NGROUP.NE.NGR) then
            call XABORT('read_PMAXS_file: Error - NGROUP must be the same in all PMAXS files')
          endif
          if( NDLAY.NE.NDL)THEN
            if(NDLAY .EQ. 0)THEN
              NDLAY=NDL
            ELSEif(NDL .GT. 0 .AND. pbet .AND. pamb)THEN
              call XABORT('read_PMAXS_file: Error - NDLAY must be the same in all PMAXS files')
            ENDIF
          endif
          if( NDCAY.NE.NDC)THEN
            if(NDCAY .EQ. 0)THEN
               NDCAY=NDC
            elseif(NDC .GT. 0 .AND. pdec)THEN
              call XABORT('read_PMAXS_file: Error - NDCAY must be same in all PMAXS files')
            endif
          endif
          if(  NADF .LT. NAD)NADF=NAD
          if(  NCDF .LT. NCD)NCDF=NCD
          if( MHST   .LT. NHST ) MHST  =NHST
          if( MRODS  .LT. NRODS) MRODS =NRODS
          if( MCOLA  .LT. NCOLA) MCOLA =NCOLA
          if( MCOLA  .LT. NROWA) MCOLA =NROWA
        endif
      endif
      call set_var_position

      read(PMAXS_unit,'(A80)') hcomment(1)
      read(PMAXS_unit,'(A80)') hcomment(2)
      read(PMAXS_unit,'(A80)') hcomment(3)
      lxes=.false.
      NXST=4
      if(INDEX(hcomment(3),"xe,sm" ) /= 0) THEN
        lxes=.true.
        NXST=7
      endif
      if(INDEX(hcomment(3),"det" ) /= 0) THEN
        lxes=.true.
        NXST=8
      endif
      tcdf=.false.
      tgff=.false.
      if(pcdf)then
         if(INDEX(hcomment(3),"CDF" ) /= 0) THEN
           tcdf=.true.
           pcdf=.false.
         ENDIF
      endif
      if(pgff)then
         if(INDEX(hcomment(3),"GFF" ) /= 0) THEN
           tgff=.true.
           pgff=.false.
         ENDIF
      endif
      read(PMAXS_unit,'(A80)') hcomment(4)
      read(PMAXS_unit,'(A80)') hcomment(5)
      read(PMAXS_unit,'(A80)') hcomment(6)

      call read_pmax_head(iPMAX, PMAXS_unit)
!4) XS Set identification
      do
         read(PMAXS_unit,*,end=101)tit
         if(tit .EQ. 'XS_SET')exit
      enddo

      backspace(PMAXS_unit)
      read(PMAXS_unit,*)tit,itemp,i_s,itemp,itemp,NCOLA,NROWA,NPART,PITCH,XBE,YBE,iHMD,Dsat,ARWatR,ARByPa,ARConR

      CALL test_pinpower

      call AllocatePMAXS
      call DEP_read_main(PMAXS_unit)
      return
      101 call XABORT('read_PMAXS_file: Error - Reached The End Of PMAXS File')
   END SUBROUTINE read_PMAXS_file

!---------------------------------------------------------------------
   SUBROUTINE test_pinpower 
!---------------------------------------------------------------------
     IMPLICIT NONE

     computed_part: SELECT CASE (NPART)
     CASE (0)
         NCOL=NCOLA
         NROW=NROWA
         NRODS=NCOL*NROW
     CASE (1)
         NCOL=NCOLA
         NROW=NROWA
         if(NCOL.ne.NROW) THEN
            call XABORT('test_pinpower: Error - Assembly Must Be Square For NPART=1')
         END IF
         NRODS=NCOL*(NCOL+1)/2
     CASE (2)
         NCOL=(NCOLA+1)/2
         NROW=(NROWA+1)/2
         NRODS=NCOL*NROW
     CASE (3)
         NCOL=(NCOLA+1)/2
         NROW=(NROWA+1)/2
         if(NCOL.ne.NROW) THEN
            call XABORT('test_pinpower: Error - Assembly Must Be Square For NPART=3')
         END IF
         NRODS=NCOL*(NCOL+1)/2
     END SELECT computed_part
   END SUBROUTINE test_pinpower

!---------------------------------------------------------------------
   SUBROUTINE read_pmax_head(iPMAX, PMAXS_unit)
!---------------------------------------------------------------------
     use PCRDATA
     IMPLICIT NONE
     INTEGER(4) :: iPMAX, PMAXS_unit
     INTEGER(4) :: i,ibra,itemp,inb,j
     CHARACTER(8) :: tit

     if(NDL .EQ. 0)then
       pbet=.false.
       pamb=.false.
     endif
     if(NDC .EQ. 0)pdec=.false.
     if(NAD .EQ. 0)padf=.false.
     if(NCD .EQ. 0)pcdf=.false.
     if(NRODS .EQ. 0)pgff=.false.

     call det_var_position

     bran_i=>Bran_info(iPMAX)
     if(bran_i%NOT_assigned)then
       bran_i%NOT_assigned=.false.
!2) States data
       do
          read(PMAXS_unit,*,end=101)tit
          if(tit .EQ. 'STA_VAR') then
             backspace(PMAXS_unit)
             read(PMAXS_unit,*)tit,Nstat_var
             bran_i%Nstat_var=Nstat_var
             allocate(bran_i%var_ind(Nstat_var),bran_i%var_nam(Nstat_var))
             var_ind=>bran_i%var_ind
             backspace(PMAXS_unit)
             read(PMAXS_unit,*)tit,Nstat_var,bran_i%var_nam(1:Nstat_var)
             ktf=0
             inb=1
             do i=1,Nstat_var
                validname=.false.
                do j=inb,Nallvar
                     if(bran_i%var_nam(i).eq.all_var_nam(j))then
                      validname=.true.
                      var_ind(i)=j
                      inb=j+1
                      exit
                      endif
                enddo
                if(validname)then
                   if(inb .EQ. 5)ktf=i
                else
                   call XABORT('read_pmax_head: Error - State Variable Name Invalid')
                endif
             enddo
             exit
          endif
          if(tit .EQ. 'BRANCHES'.or.tit .EQ. 'BURNUPS'.or.tit .EQ. 'XS_SET') then
             backspace(PMAXS_unit)
             Nstat_var=5
             bran_i%Nstat_var=Nstat_var
             allocate(bran_i%var_ind(Nstat_var),bran_i%var_nam(Nstat_var))
             var_ind=>bran_i%var_ind
             ktf=4
             do i=1,Nstat_var
                var_ind(i)=i
                bran_i%var_nam(i)=all_var_nam(i)
             enddo
             exit
          endif
       enddo

!2) States data
       allocate(bran_i%NBR(Nstat_var))
       NBR=>bran_i%NBR
       NBRA=1
       do
          read(PMAXS_unit,*,end=101)tit
          if(tit .EQ. 'BRANCHES') then
             backspace(PMAXS_unit)
             read(PMAXS_unit,*)tit,itemp,NBR
             do i=1,Nstat_var
                NBRA=NBRA+NBR(i)
             enddo
             allocate(bran_i%state(Nstat_var,NBRA),bran_i%state_nam(NBRA))
             if(NBRA .GT. 1)then
                state=>bran_i%state
                 if(ktf .GT. 0)then
                   do ibra=1,NBRA
                      read(PMAXS_unit,*,end=101)bran_i%state_nam(ibra),itemp,state(:,ibra)
                      state(ktf,ibra)=dsqrt(state(ktf,ibra))
                   enddo
                else
                   do ibra=1,NBRA
                      read(PMAXS_unit,*,end=101)bran_i%state_nam(ibra),itemp,state(:,ibra)
                   enddo
                endif
             else
                bran_i%state=0
             endif
             exit
          endif
          if(tit .EQ. 'BURNUPS'.or.tit .EQ. 'XS_SET')   then
             backspace(PMAXS_unit)
             allocate(bran_i%state(Nstat_var,NBRA))
             bran_i%state=0
             NBR=0
             exit
          endif
       enddo
       bran_i%NBRA=NBRA
       bran_i%ktf=ktf
       if(MBRA .LT. NBRA)MBRA=NBRA
       if(var_ind(1) .EQ. 1)then
          if(MBCR .LT. NBR(1))MBCR=NBR(1)
       endif
     else
       Nstat_var=bran_i%Nstat_var
       NBRA=bran_i%NBRA
       ktf=bran_i%ktf
     endif

!3) Burnup  information
     do
        read(PMAXS_unit,*,end=101)tit
        if(tit .EQ. 'BURNUPS') then
            backspace(PMAXS_unit)
            read(PMAXS_unit,*)tit,PMAX%NBset
            if(MBset .LT. PMAX%NBset)MBset=PMAX%NBset
            allocate(PMAX%Bset(PMAX%NBset))
            do i=1,PMAX%NBset
                read(PMAXS_unit,*)itemp,itemp
                allocate(PMAX%Bset(i)%burns(itemp))
                backspace(PMAXS_unit)
                read(PMAXS_unit,*)itemp,PMAX%Bset(i)%NBURN,PMAX%Bset(i)%burns
            enddo
            exit
        endif
        if(tit .EQ. 'XS_SET')   then
            backspace(PMAXS_unit)
            PMAX%NBset=1
            allocate(PMAX%Bset(PMAX%NBset))
            allocate(PMAX%Bset(1)%burns(1))
            PMAX%Bset(1)%NBURN=1
            PMAX%Bset(1)%burns(1)=0
            exit
        endif
     enddo
     return
     101  call XABORT('read_pmax_head: Error - Reached The End Of PMAXS File')
     STOP
   END SUBROUTINE read_pmax_head

!---------------------------------------------------------------------
   SUBROUTINE DEP_read_main(PMAXS_unit)
!---------------------------------------------------------------------
     use PCRDATA
     IMPLICIT NONE
     INTEGER(4) :: PMAXS_unit
     INTEGER(4) :: i,ihst,ibra,itemp,iBset,NBURN
     CHARACTER(4) :: tit4
     CHARACTER(8) :: tit
!     History case wise data
     do ihst=1,NHST
!6) History case identification
       do
         read(PMAXS_unit,*,end=101)tit
         if(tit .EQ. 'HST_CASE')then
            backspace(PMAXS_unit)
            read(PMAXS_unit,*)tit,PMAX%history(:,ihst)
            PMAX%TIVB(ihst)%ibset=1
            exit
         endif
         if(tit .EQ. 'HISTORYC')then
            backspace(PMAXS_unit)
            read(PMAXS_unit,*)tit,PMAX%TIVB(ihst)%ibset,PMAX%history(:,ihst)
            exit
         endif
       enddo
       if(ktf .GT. 0)PMAX%history(ktf,ihst)=sqrt(PMAX%history(ktf,ihst))
       call AllocateTIVB(PMAX%TIVB(ihst))
       NBURN=PMAX%Bset(PMAX%TIVB(ihst)%ibset)%NBURN
       do i=1,NBURN
         TIV=>PMAX%TIVB(ihst)%TIV(i)
         call read_TIV(PMAXS_unit)
       enddo
!branch wise data
!7) State identification      Always
       do ibra=1,NBRA
         read(PMAXS_unit,'(A4,2I4)')tit4,itemp,iBset
         PMAX%branch(ibra,ihst)%iBset=iBset
         NBURN=PMAX%bset(iBset)%NBURN
         call read_branches(NBURN,PMAXS_unit,PMAX%branch(ibra,ihst))
       enddo !ibra
     enddo !ihst
     return
     101  call XABORT('DEP_read_main: Error - Reached The End Of PMAXS File')
   END SUBROUTINE DEP_read_main

!------------------------------------------------------------------
   SUBROUTINE read_branches(NBURN,PMAXS_unit,bran)
!------------------------------------------------------------------
       TYPE(BRANCH_WISE_TYPE) :: bran
       INTEGER(4) :: NBURN,PMAXS_unit,iburn
       call AllocateBranch(bran)
       do iburn=1,NBURN
          XS=>bran%XS(iburn)
          call read_XS_Block(PMAXS_unit)
       enddo
   END SUBROUTINE read_branches

!---------------------------------------------------------------------
   SUBROUTINE AllocatePMAXS
!---------------------------------------------------------------------
!
      PMAX%NCOL=NCOL
      PMAX%NRODS=NRODS
      PMAX%NHST=NHST
      PMAX%NROW=NROW
      PMAX%NPART=NROW
      PMAX%NROWA=NROWA
      PMAX%NCOLA=NCOLA
      PMAX%iHMD=iHMD
      PMAX%Dsat=Dsat
      PMAX%ARWatR=ARWatR
      PMAX%ARByPa=ARByPa
      PMAX%ARConR=ARConR
      PMAX%PITCH=PITCH
      PMAX%XBE=XBE
      PMAX%YBE=YBE
      PMAX%derivatives=derivatives
      allocate(PMAX%TIVB(NHST))
      allocate(PMAX%branch(NBRA,NHST))
      allocate(PMAX%history(Nstat_var,NHST))
      allocate(PMAX%base(NHST))
      allocate(PMAX%invdiff(NHST))
   END SUBROUTINE AllocatePMAXS

!---------------------------------------------------------------------
   SUBROUTINE Clear_PMAXS_file(iPMAX)
!---------------------------------------------------------------------
      use PCRDATA
      IMPLICIT NONE
      INTEGER(4) :: iPMAX, i, ihst, ibra
      bran_i=>Bran_info(iPMAX)
      if(Nstat_var > 0) then
         deallocate(bran_i%var_ind,bran_i%var_nam,bran_i%NBR)
         if(NBRA.GT.0) deallocate(bran_i%state,bran_i%state_nam)
      endif
      do ihst=1,NHST
            print *,'Clear_PMAX_file: call ClearTIVB ihst=',ihst
        call ClearTIVB(PMAX%TIVB(ihst))
        do ibra=1,NBRA
          call ClearBranch(PMAX%branch(ibra,ihst))
        enddo !ibra
      enddo
      if(PMAX%NBset > 0) then
         do i=1,PMAX%NBset
            deallocate(PMAX%Bset(i)%burns)
         enddo
         deallocate(PMAX%Bset)
      endif
      if(NHST > 0) then
        deallocate(PMAX%TIVB)
        if(NBRA.GT.0) deallocate(PMAX%branch)
        if(Nstat_var > 0) deallocate(PMAX%history)
        deallocate(PMAX%base)
        deallocate(PMAX%invdiff)
      endif
      return
   END SUBROUTINE Clear_PMAXS_file
END MODULE PCREAD