-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphemgp_0.5.f90
1632 lines (1323 loc) · 53.5 KB
/
phemgp_0.5.f90
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!------------------------------------------------------------------------
!
! Copyright 2014,2015 Andrea Zunino
!
!
! Phemgp is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! Phemgp is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with Phemgp. If not, see <http://www.gnu.org/licenses/>.
!
!------------------------------------------------------------------------
!#######################################################################
!#######################################################################
module realprec
integer,parameter :: pdigits=15 !15
integer,parameter :: rexprange=307 !307
!! RESULT = SELECTED_REAL_KIND([P, R, RADIX])
integer,parameter :: dp = selected_real_kind(pdigits, rexprange)
public :: pdigits,rexprange,dp
end module realprec
!#######################################################################
module proptab
use realprec
implicit none
integer, parameter :: maxnumtab = 20
integer, parameter :: lenstrnameprop = 24
integer, parameter :: numnameprops = 50
type tabledims
character(len=1024) :: inptable(maxnumtab)
integer :: nxpts,nypts,nzfiles,nprop,totnumphases
real(dp) :: xmin,ymin,dx,dy
real(dp), allocatable :: ztabs(:)
character(len=128) :: title
character(len=18) :: xname,yname
character(len=lenstrnameprop) :: nameprop(numnameprops)
end type tabledims
type indexes
integer :: vol_index
end type indexes
real(dp), allocatable, target :: propar(:,:,:,:)
character(12), allocatable :: phasar(:,:,:,:)
integer :: proparlinelength
integer :: phasarlinelength
integer, parameter :: rowlength = 2048
character(len=12), allocatable :: lstphas(:)
public :: tabledims,indexes,maxnumtab,propar,phasar,proparlinelength,phasarlinelength,lstphas,&
redtab,mixprop,extractminpr,extractsingleminpr
private :: rowlength,average,bilinear,xy2ij,checknodes,calcz
contains
!!=====================================================================
subroutine redtab(propar, phasar, tbd,lstphas)
! propar: 3d array of real(dp) containing the bulk prop. and
! the properties of each phase. the first 3 indices contain
! p, t, followed by the no. of the phases present at
! given conditions. the remaining elements are the bulks
! properties followed by the prop. for each phase.
! phasar: 3d array of character containing names of the phases
! present at that conditions
! title: title of the table as appear inside the file
! nxpts, nypts: no. of points along i [x] and j [y] axes
! xmin, ymin: starting point of x and y
! dx, dy: increments in x and y direction
! xname, yname: label of x and y axis
! nprop: no. of properties
!
! ------------------------------------------------------
! xpts = xpoint, ypts = ypoint
!
implicit none
character(len=12), allocatable :: lstphas(:)
character(len=100) :: tfile,filenumstr
character(1) :: row(rowlength),substring(lenstrnameprop)
character(24) :: formatversion
character(12) :: pres,temp,listofphases(rowlength)
real(dp), allocatable :: propar(:,:,:,:)
character(12), allocatable :: phasar(:,:,:,:)
character(12) :: curphase,emptystring,strfmt
integer :: nprop,nphas,iph,kount,p, &
nelem,ibulk,i,j,k,fle,maxlinelength,maxnphas, &
noindipvar,istrs,istre,startstr(rowlength),endstr(rowlength)&
,mah,k2
type(tabledims) :: tbd
maxlinelength=0
maxnphas=0
if (allocated(tbd%ztabs) ) deallocate(tbd%ztabs)
allocate(tbd%ztabs(tbd%nzfiles))
!! starts reading tables
!! ###############################################################
!! ##### Tables MUST have the same number of properties !!!! #####
!! ###############################################################
!!------------ scan for phases present in the table ----------
write(*,*)
write(*,*) "Scanning for phase names..."
write(*,*)
!! remember, in redtab: phasar=" "
emptystring=" " !!!phasar(1,1,1,phasarlinelength)
listofphases(1) = " "
p=1
maxnphas=0
row(:)=' '
substring(:)=' '
do fle=1,tbd%nzfiles
tfile = trim(adjustl(tbd%inptable(fle)))
open(unit=10, file=tfile, status='old')
read(10,*) formatversion
read(10,*) tbd%title
read(10,*) tbd%ztabs(fle) !!! <<< NEW
read(10,*) noindipvar
read(10,*) tbd%xname
read(10,*) tbd%xmin
read(10,*) tbd%dx
read(10,*) tbd%nxpts
read(10,*) tbd%yname
read(10,*) tbd%ymin
read(10,*) tbd%dy
read(10,*) tbd%nypts
read(10,*) tbd%nprop
tbd%nprop=tbd%nprop-4 ! skipping first 4 columns |this header == all fields (# columns)
read(10,*) !! skipping properties etc strings
! loop on y nodes
do j=1,tbd%nypts
! loop on x nodes
do i=1,tbd%nxpts
read(10,*) curphase, nphas
!print*,curphase,"nphas", nphas
if ( nphas > maxnphas ) then
maxnphas=nphas
endif
LOOP_IDX4: do iph=1,nphas
read(10,fmt='(a12)') curphase
!print*,'curphase: ',curphase
curphase=trim(adjustl(curphase))
if (curphase==emptystring) then
!! if curphase is an empty string jump to next element
cycle LOOP_IDX4
else if ( all(listofphases(1:p) /= curphase) ) then
!!! ( all(listofphases(1:(p-1)) /= curphase) )
print*,'curphase: ',curphase
listofphases(p)=curphase
p=p+1
endif
enddo LOOP_IDX4
! enddo LOOP_IDX4
enddo
enddo
close(10)
enddo !! ends z loop
if (allocated(lstphas) ) deallocate(lstphas)
allocate( lstphas(p-1) ) !! p-1 = total number of phases
lstphas = listofphases(1:p-1)
tbd%totnumphases = p-1
print*,'total number of phases', p-1
!print*,'max num. phases per one line',maxnphas
!!------- end scan -----------
phasarlinelength= maxnphas
proparlinelength= (phasarlinelength+1)*(tbd%nprop+1)-1
print*,"############################################################"
print*, "proparlinelength:", proparlinelength
print*, "phasarlinelength:", phasarlinelength
print*,"############################################################"
! allocate variable contained in module sizetab
if (allocated(propar) ) deallocate(propar) !! in case one asks to reload tables from main menu
if (allocated(phasar) ) deallocate(phasar) !! in case one asks to reload tables from main menu
allocate( propar(tbd%nxpts,tbd%nypts,tbd%nzfiles,proparlinelength) )
allocate( phasar(tbd%nxpts,tbd%nypts,tbd%nzfiles,phasarlinelength) )
propar=0.0 !! initialize arrays
phasar=" "
do fle=1,tbd%nzfiles
print*, 'tbd%nzfiles',tbd%nzfiles
tfile = trim(adjustl(tbd%inptable(fle)))
print*,"--------------------------------------------"
write (*,*) 'Reading file: ',tfile
open(unit=10, file=tfile, status='old')
read(10,*) formatversion
read(10,*) tbd%title
read(10,*) tbd%ztabs(fle) !!! <<< NEW
read(10,*) noindipvar
read(10,*) tbd%xname
read(10,*) tbd%xmin
read(10,*) tbd%dx
read(10,*) tbd%nxpts
read(10,*) tbd%yname
read(10,*) tbd%ymin
read(10,*) tbd%dy
read(10,*) tbd%nypts
read(10,*) tbd%nprop
tbd%nprop=tbd%nprop-4 ! this header == all fields (# columns):
!+ Name Counter T(K) P(bar) phase,vo% ......
write(strfmt,'(i12)') rowlength
strfmt="(a"//trim(adjustl(strfmt))//")"
!print*,'^^^^ strfmt:',strfmt
write(strfmt,'(i12)') rowlength
strfmt="("//trim(adjustl(strfmt))//"(a1))"
read(10,strfmt) (row(k),k=1,rowlength)
!print *, "row:",(row(k),k=1,rowlength),"|END"
istrs=1
istre=1
do k=1,(rowlength-1)
if ( (row(k)==" ") .and. (row(k+1)/=" ") ) then
startstr(istrs)=k+1
!print *,istrs,startstr(istrs),row(k+1)
istrs=istrs+1
else if ( (row(k)/=" ") .and. (row(k+1)==" ") ) then
endstr(istre)=k
!print *,istre,endstr(istre),row(k)
istre=istre+1
end if
end do
k2=1
! skip first 2+noindipvar substrings
do k=(2+noindipvar+1),istrs-1
!print *,'k:',k,startstr(k),endstr(k)
substring(1:(-startstr(k)+endstr(k)+1)) = &
row(startstr(k):endstr(k))
!print *, "substring:",substring," | ",row(startstr(k):endstr(k))
do mah=1,(-startstr(k)+endstr(k)+1)
tbd%nameprop(k2)(mah:mah)=substring(mah)
enddo
do mah=(-startstr(k)+endstr(k)+1)+1,lenstrnameprop !! len=24 !12
tbd%nameprop(k2)(mah:mah)=" "
enddo
! print *, "substring:",substring
! print *,"tbd%nameprop:",tbd%nameprop(k2)
k2 = k2 + 1
enddo
write(*,*) "Format version: ",formatversion
write(*,*) "Number of independent variables: ",noindipvar
write(*,*) 'Title inside file: ',trim(tbd%title)
write(*,*) 'z value (could be e.g. basalt fraction or water content): ',tbd%ztabs(fle)
write(*,*) "x: (npts,xmin,x_incr) ",tbd%nxpts,tbd%xmin,tbd%dx
write(*,*) "y: (npts,ymin,y_incr) ",tbd%nypts,tbd%ymin,tbd%dy
write(*,*) "num. of tables:",tbd%nzfiles
write(*,*) 'x name, y name: ',tbd%xname,tbd%yname
write(*,*) "Number of properties: ",tbd%nprop
write(*,*) 'Properties: ',(tbd%nameprop(k)//" ",k=1,tbd%nprop)
write(*,*) "---------------------------------------------"
write(*,*)
! tbd%nxpts*tbd%nypts lines correspond to the no. of points in x and y
! loop on y nodes
do j=1,tbd%nypts
! loop on x nodes
do i=1,tbd%nxpts
! propar(i,j,fle,*ibulk+1*): ibulk+1 because first element (bulk)
! contains the no. of phases
read(10,fmt='(a12)',ADVANCE="no") curphase
read(10,*) nphas, temp, pres, (propar(i,j,fle,ibulk+1),ibulk=1,tbd%nprop)
propar(i,j,fle,1)= real(nphas)
do iph=1,nphas
kount=iph*tbd%nprop
! propar(i,j,fle,*nelem+2*): *nelem+2* because starts at tbd%nprop+2
read(10,fmt='(a12)',ADVANCE="no") phasar(i,j,fle,iph) !! to read '/' and other keywords in a string
read(10,*) nphas, temp, pres, &
(propar(i,j,fle,nelem+2),nelem=kount,kount+tbd%nprop-1 )
enddo
end do ! end do for i
end do ! end do for j
close(10)
enddo !!### enddo on tbd%nzfiles ################
return
end subroutine redtab
!!=====================================================================
subroutine mixprop(propar,x,y,z,tbd,inp_req,indx,bulk_prop,phas_prop)
implicit none
integer :: i,j,k,num_preq_bulk,num_preq_phas
real(dp),intent(in) :: x,y,z
real(dp) ::zdec
logical :: left,down
logical :: onanode
integer :: i_int,j_int,z1,z2,ii,jj
real(dp) :: line_a(proparlinelength),line_b(proparlinelength) &
,line_c(proparlinelength),line_d(proparlinelength)
real(dp),allocatable :: bulk_prop_int1(:),bulk_prop_int2(:)&
,phas_prop_int1(:),phas_prop_int2(:)
real(dp),allocatable,intent(out) :: bulk_prop(:), phas_prop(:)
real(dp), target,intent(in) :: propar(:,:,:,:)
real(dp),allocatable :: av_phas_prop_a(:),av_phas_prop_b(:),&
av_phas_prop_c(:), av_phas_prop_d(:),bulk_prop_a(:) &
,bulk_prop_b(:),bulk_prop_c(:),bulk_prop_d(:)
real(dp), pointer :: propar1(:,:,:),propar2(:,:,:)
integer, allocatable :: preq_bulk(:),preq_phas(:,:)
integer,intent(in) :: inp_req(:,:)
type(tabledims),intent(in) :: tbd
type(indexes),intent(in) :: indx ! which property to average on!
num_preq_bulk = count(inp_req(1,:)>0)
num_preq_phas = count(inp_req(2,:)>0)
allocate( preq_bulk(num_preq_bulk),preq_phas(2,num_preq_phas) )
preq_bulk=0 ! initializing to zero all elements
preq_phas=0
! requested bulk properties
do j=1,num_preq_bulk
preq_bulk(j) =inp_req(1,j) ! bulk props from perplex, not used
enddo
! requested phase properties
do j=1,num_preq_phas
preq_phas(1,j) = inp_req(2,j) !! which properties
preq_phas(2,j) = inp_req(3,j) !! average scheme
enddo
if (.not. allocated(bulk_prop) ) then
allocate(bulk_prop(num_preq_bulk))
endif
allocate(bulk_prop_a(num_preq_bulk), &
bulk_prop_b(num_preq_bulk),bulk_prop_c(num_preq_bulk),&
bulk_prop_d(num_preq_bulk), bulk_prop_int1(num_preq_bulk))
if (.not. allocated(phas_prop) ) then
allocate(phas_prop(num_preq_phas))
endif
allocate(av_phas_prop_a(num_preq_phas),&
av_phas_prop_b(num_preq_phas),av_phas_prop_c(num_preq_phas),&
av_phas_prop_d(num_preq_phas),phas_prop_int1(num_preq_phas))
if (tbd%nzfiles > 1) then
!! compute the z fraction
call calcz(z,tbd,z1,z2,zdec)
else
z1=1
end if
!----------------------------------------------------------------------
!######### TABLE 1 ###################################
propar1 => propar(:,:,z1,:) !! pointer to table 1
call xy2ij(x,y,tbd,ii,jj,left,down)
call checknodes(x,y,ii,jj,tbd,left,down,onanode,i_int,j_int)
do k=1,proparlinelength
line_a(k) = propar1(i_int, j_int, k)
line_b(k) = propar1(i_int+1,j_int, k)
line_c(k) = propar1(i_int+1,j_int+1,k)
line_d(k) = propar1(i_int, j_int+1,k)
enddo
call average(line_a,tbd%nprop,preq_bulk,preq_phas,bulk_prop_a,av_phas_prop_a,indx)
call average(line_b,tbd%nprop,preq_bulk,preq_phas,bulk_prop_b,av_phas_prop_b,indx)
call average(line_c,tbd%nprop,preq_bulk,preq_phas,bulk_prop_c,av_phas_prop_c,indx)
call average(line_d,tbd%nprop,preq_bulk,preq_phas,bulk_prop_d,av_phas_prop_d,indx)
call bilinear(bulk_prop_a,bulk_prop_b,bulk_prop_c,bulk_prop_d, &
tbd,x,y,i_int,j_int,bulk_prop_int1)
call bilinear(av_phas_prop_a,av_phas_prop_b,av_phas_prop_c,av_phas_prop_d, &
tbd,x,y,i_int,j_int,phas_prop_int1)
!----------------------------------------------------------------------
!######### TABLE 2 ###################################
if (tbd%nzfiles > 1) then
allocate(bulk_prop_int2(num_preq_bulk))
allocate(phas_prop_int2(num_preq_phas))
propar2 => propar(:,:,z2,:) !! pointer to table 2
call xy2ij(x,y,tbd,ii,jj,left,down)
call checknodes(x,y,ii,jj,tbd,left,down,onanode,i_int,j_int)
do k=1,proparlinelength
line_a(k) = propar2(i_int, j_int, k)
line_b(k) = propar2(i_int+1,j_int, k)
line_c(k) = propar2(i_int+1,j_int+1,k)
line_d(k) = propar2(i_int, j_int+1,k)
enddo
call average(line_a,tbd%nprop,preq_bulk,preq_phas,bulk_prop_a,av_phas_prop_a,indx)
call average(line_b,tbd%nprop,preq_bulk,preq_phas,bulk_prop_b,av_phas_prop_b,indx)
call average(line_c,tbd%nprop,preq_bulk,preq_phas,bulk_prop_c,av_phas_prop_c,indx)
call average(line_d,tbd%nprop,preq_bulk,preq_phas,bulk_prop_d,av_phas_prop_d,indx)
call bilinear(bulk_prop_a,bulk_prop_b,bulk_prop_c,bulk_prop_d, &
tbd,x,y,i_int,j_int,bulk_prop_int2)
call bilinear(av_phas_prop_a,av_phas_prop_b,av_phas_prop_c,av_phas_prop_d, &
tbd,x,y,i_int,j_int,phas_prop_int2)
!----------------------------------------------------------------------
!##### arithmetic mean between tables 1 & 2 #####
do i=1,num_preq_bulk
!! arithmetic mean in z direction
bulk_prop(i) = (1.-zdec)*bulk_prop_int1(i)+zdec*bulk_prop_int2(i)
enddo
do i=1,num_preq_phas
!! arithmetic mean in z direction
phas_prop(i) = (1.-zdec)*phas_prop_int1(i)+zdec*phas_prop_int2(i)
enddo
deallocate(bulk_prop_int2,phas_prop_int2 )
else
!!!### only 1 table loaded
do i=1,num_preq_bulk
bulk_prop(i) = bulk_prop_int1(i)
enddo
do i=1,num_preq_phas
phas_prop(i) = phas_prop_int1(i)
enddo
endif
deallocate(preq_bulk,preq_phas)
deallocate(bulk_prop_a, &
bulk_prop_b,bulk_prop_c,&
bulk_prop_d, bulk_prop_int1)
deallocate(av_phas_prop_a,&
av_phas_prop_b,av_phas_prop_c,&
av_phas_prop_d,phas_prop_int1)
return
end subroutine mixprop
!______________________________________________________________
!______________________________________________________________
subroutine average(line,nprop,preq_bulk,preq_phas,bulk_prop,av_phas_prop,indx)
implicit none
real(dp) :: line(proparlinelength) ,cur_prop_av,cur_prop_reuss,cur_prop_voigt,sumvol
integer :: nphas,nprop,l,k,i,j
real(dp) :: bulk_prop(:),av_phas_prop(:)
real(dp), allocatable :: cur_prop(:),vol(:)
integer :: preq_bulk(:),preq_phas(:,:)
type(indexes) :: indx
nphas=int(line(1))
allocate(cur_prop(nphas),vol(nphas))
!! ######### bulk properties ############
bulk_prop=0.0
if ( (maxval(preq_bulk)) > 0 ) then
do i=1,size(preq_bulk)
!! i+1 because first is the no. of phases
bulk_prop(i) = line(preq_bulk(i)+1) !!CHECK CHECK CHECK
enddo
endif
!! ######### phase properties ############
if ( (maxval(preq_phas(1,:))) > 0 ) then
l=1
sumvol=0.0
do k = (nprop+1+indx%vol_index),(nprop*nphas+1+nprop),nprop
vol(l)=line(k) !!!!! <<<<<<<<<<<<<<<<<<<<<<<<<-----------<<<<
sumvol = sumvol + vol(l)
l = l+1
enddo
!!!!!######### normalization ########
vol=vol/sumvol !! for all elements
! loop only on requested properties
do j=1,size(preq_phas,2)
i=preq_phas(1,j) !! <<<<<<---<<<<<<
! loop on phases for a single property
l=1
do k = (nprop+1+i),(nprop*(nphas+1)+1),nprop
cur_prop(l)=line(k)
l = l+1
enddo
if (preq_phas(2,j)==1) then
!! #### arithmetic mean #####
cur_prop_av=0.0
do k = 1,nphas
cur_prop_av=cur_prop_av+vol(k)*cur_prop(k)
enddo
av_phas_prop(j)=cur_prop_av
else if (preq_phas(2,j)==2) then
!!#### VRH averaging ####
cur_prop_voigt=0.0d0
cur_prop_reuss=0.0d0
do k = 1,nphas
cur_prop_voigt = cur_prop_voigt+vol(k)*cur_prop(k)
cur_prop_reuss = cur_prop_reuss+vol(k)/cur_prop(k)
enddo
cur_prop_reuss = 1. / cur_prop_reuss
cur_prop_av = 0.5d0*(cur_prop_reuss + cur_prop_voigt)
av_phas_prop(j)=cur_prop_av
else if (preq_phas(2,j)==3) then
!!#### Voigt averaging ####
cur_prop_voigt=0.0d0
do k = 1,nphas
cur_prop_voigt = cur_prop_voigt+vol(k)*cur_prop(k)
enddo
av_phas_prop(j)=cur_prop_voigt
else if (preq_phas(2,j)==4) then
!!#### Reuss averaging ####
cur_prop_reuss=0.0d0
do k = 1,nphas
cur_prop_reuss = cur_prop_reuss+vol(k)/cur_prop(k)
enddo
cur_prop_reuss = 1. / cur_prop_reuss
av_phas_prop(j)= cur_prop_reuss
endif
enddo ! for nprop...
endif !! ends: if ( (maxval(preq_phas(1,:))) > 0 ) then
deallocate(cur_prop,vol)
return
end subroutine average
!!=====================================================================
subroutine bilinear(prop_node_a,prop_node_b,prop_node_c,prop_node_d, &
tbd,x,y,i_int,j_int,prop_int)
!----------------------------------------------------------------------
! cfr. numerical recipes in fortran 77 vol.1 pag 116
!
!----------------------------------------------------------------------
implicit none
integer :: k,i_int,j_int
real(dp) :: xlow,xhigh,ylow,yhigh,t,u,no1,no2,no3,no4,x,y
real(dp) :: prop_int(:),prop_node_a(:),&
prop_node_b(:),prop_node_c(:),prop_node_d(:)
type(tabledims) :: tbd
!
! the nodes are anticlockwise numbered
!
! no4 no3
!
! no1 no2
!
! no1 : the reference point; the origin for u and t.
! t and u go from 0.0 to 1.0
!
! bilinear interpolation for propar
! i + nx*(j-1)
xlow = tbd%xmin + dble(i_int-1) * tbd%dx
xhigh = tbd%xmin + dble(i_int+1-1)* tbd%dx
ylow = tbd%ymin + dble(j_int-1) * tbd%dy
yhigh = tbd%ymin + dble(j_int+1-1)* tbd%dy
t = (x-xlow) / (xhigh-xlow)
u = (y-ylow) / (yhigh-ylow)
do k=1,size(prop_node_a)
no1=prop_node_a(k)
no2=prop_node_b(k)
no3=prop_node_c(k)
no4=prop_node_d(k)
prop_int(k) = no1*(1.-t)*(1.-u)+t*(1.-u)*no2+t*u*no3+(1.-t)*u*no4
enddo
return
end subroutine bilinear
!!!!##############################################################
subroutine extractminpr(xvec,yvec,zvec,propar,phasar,nprop,inp_minpr,tbd,outfilemin1,outfilemin2)
implicit none
integer :: i,j,z,itab,jtab,nprop,z1,z2,iph,&
ipr,sngpr,nphases,l,num_minpr_req,pp
real(dp) :: xvec,yvec,zdec,zvec
integer, allocatable :: minpr_req(:)
character(len=12), allocatable :: phasnamelst(:)
character(len=12),allocatable :: phasar(:,:,:,:)
real(dp), allocatable :: proplst(:)
real(dp), allocatable :: propar(:,:,:,:)
type(tabledims) :: tbd
character(100) :: outfilemin1,outfilemin2
logical :: left,down,onanode
integer :: inp_minpr(:)
call xy2ij(xvec,yvec,tbd,i,j,left,down)
call checknodes(xvec,yvec,i,j,tbd,left,down,onanode,itab,jtab)
call calcz(zvec,tbd,z1,z2,zdec)
open(unit=31,file=outfilemin1,status='unknown',POSITION='APPEND')
open(unit=32,file=outfilemin2,status='unknown',POSITION='APPEND')
num_minpr_req = count(inp_minpr(:)>0)
allocate(minpr_req(num_minpr_req))
! requested bulk properties
do j=1,num_minpr_req
minpr_req(j) = inp_minpr(j)
enddo
! if (onanode.eqv..true.) then
!! ###### IF ON A NODE ???? #####
if (tbd%nzfiles == 1 ) z2=z1 !! no loop on z
do z=z1,z2
!print*, "z1,z2",z1,z2
do j=jtab,jtab+1 !! #### how to keep "checknodes" routine order ??
do i=itab,itab+1
nphases=int(propar(i,j,z,1))
allocate(proplst(num_minpr_req*nphases))
allocate(phasnamelst(nphases))
pp=1 !! array index starts from 1!!
!! loop on phases
do iph=1,nphases
phasnamelst(iph) = phasar(i,j,z,iph)
! loop only on requested properties
do l=1,num_minpr_req
ipr=minpr_req(l)
sngpr = nprop*iph+1+ipr
proplst(pp) = propar(i,j,z,sngpr)
pp=pp+1
enddo
enddo
!! ---------------------------
if ( (i==itab).and.(j==jtab)) then
if (z==z1) then !! TABLE 1
write(31,*) "--- node A ---",xvec,yvec
write(31,*) phasnamelst
write(31,*) proplst
else if((z==z2).and.(tbd%nzfiles > 1 ) ) then !! TABLE 2
write(32,*) "--- node A ---",xvec,yvec
write(32,*) phasnamelst
write(32,*) proplst
endif
else if ((i==itab+1).and.(j==jtab) ) then
if (z==z1) then !! TABLE 1
write(31,*) "--- node B ---",xvec,yvec
write(31,*) phasnamelst
write(31,*) proplst
else if((z==z2).and.(tbd%nzfiles > 1)) then !! TABLE 2
write(32,*) "--- node B ---",xvec,yvec
write(32,*) phasnamelst
write(32,*) proplst
endif
else if ((i==itab+1).and.(j==jtab+1) ) then
if (z==z1) then !! TABLE 1
write(31,*) "--- node C ---",xvec,yvec
write(31,*) phasnamelst
write(31,*) proplst
else if((z==z2).and.(tbd%nzfiles > 1 )) then !! TABLE 2
write(32,*) "--- node C ---",xvec,yvec
write(32,*) phasnamelst
write(32,*) proplst
endif
else if ((i==itab).and.(j==jtab+1) ) then
if (z==z1) then !! TABLE 1
write(31,*) "--- node D ---",xvec,yvec
write(31,*) phasnamelst
write(31,*) proplst
else if((z==z2).and.(tbd%nzfiles > 1 )) then !! TABLE 2
write(32,*) "--- node D ---",xvec,yvec
write(32,*) phasnamelst
write(32,*) proplst
endif
endif
deallocate(proplst,phasnamelst)
enddo
enddo
enddo
deallocate(minpr_req)
close(31)
close(32)
return
end subroutine extractminpr
!!----------------------------------------------------------------------
!!=====================================================================
subroutine extractsingleminpr(xvec,yvec,zvec,propar,phasar,nprop,inp_singleminpr,&
req_singlemin,tbd,propssinglemin,phaseispresent)
implicit none
integer :: i,j,z,itab,jtab,nprop,z1,z2,iph,&
ipr,sngpr,nphases,l,num_minpr_req,pp,idxreqphase
real(dp),intent(in) :: xvec,yvec,zvec
integer, allocatable :: minpr_req(:)
character(len=12),intent(in) :: phasar(:,:,:,:)
character(len=12) :: req_singlemin
real(dp),intent(out) :: propssinglemin(:)
real(dp), allocatable :: tab_1_nd_a(:),&
tab_2_nd_a(:),tab_1_nd_b(:),tab_2_nd_b(:),&
tab_1_nd_c(:),tab_2_nd_c(:),tab_1_nd_d(:),tab_2_nd_d(:),&
prop_int_tab_1(:),prop_int_tab_2(:)
real(dp),intent(in) :: propar(:,:,:,:)
real(dp) :: zdec
type(tabledims) :: tbd
logical :: left,down,onanode
integer :: inp_singleminpr(:),nphasepresent
logical,intent(out) :: phaseispresent
call xy2ij(xvec,yvec,tbd,i,j,left,down)
call checknodes(xvec,yvec,i,j,tbd,left,down,onanode,itab,jtab)
if (tbd%nzfiles == 1 ) then
z1=1
z2=z1 !! no loop on z
else
call calcz(zvec,tbd,z1,z2,zdec)
end if
num_minpr_req = count(inp_singleminpr(:)>0)
allocate(minpr_req(num_minpr_req))
!!allocate(propssinglemin(num_minpr_req))
allocate( tab_1_nd_a(num_minpr_req),tab_2_nd_a(num_minpr_req),&
tab_1_nd_b(num_minpr_req),tab_2_nd_b(num_minpr_req),&
tab_1_nd_c(num_minpr_req),tab_2_nd_c(num_minpr_req),&
tab_1_nd_d(num_minpr_req),tab_2_nd_d(num_minpr_req),&
prop_int_tab_1(num_minpr_req),prop_int_tab_2(num_minpr_req) )
! requested properties
do j=1,num_minpr_req
minpr_req(j) = inp_singleminpr(j)
enddo
nphasepresent=0 !! initializing variable
phaseispresent=.false.
do z=z1,z2
do j=jtab,jtab+1 !! #### how to keep "checknodes" routine order ??
do i=itab,itab+1
nphases=int(propar(i,j,z,1))
idxreqphase=0 !! means phase is not present
pp=1 !! array index starts from 1!!
!! loop on phases
do iph=1,nphases
!print*,i,j,z,trim(phasar(i,j,z,iph)),trim(req_singlemin)
if (trim(phasar(i,j,z,iph))==(trim(req_singlemin))) then
idxreqphase=iph
nphasepresent=nphasepresent+1
endif
enddo
if (idxreqphase==0) then
!! set to zero the weight of the node if phase is not present
propssinglemin=0
else
! loop only on requested properties
do l=1,num_minpr_req
ipr=minpr_req(l)
sngpr = nprop * idxreqphase +1+ipr
propssinglemin(pp) = propar(i,j,z,sngpr)
pp=pp+1
enddo
endif
!! -----------------------------------
if ( (i==itab).and.(j==jtab)) then
if (z==z1) then !! TABLE 1
tab_1_nd_a = propssinglemin
else if((z==z2).and.(tbd%nzfiles > 1 )) then !! TABLE 2
tab_2_nd_a = propssinglemin
endif
else if ((i==itab+1).and.(j==jtab) ) then
if (z==z1) then !! TABLE 1
tab_1_nd_b = propssinglemin
else if((z==z2).and.(tbd%nzfiles > 1 )) then !! TABLE 2
tab_2_nd_b = propssinglemin
endif
else if ((i==itab+1).and.(j==jtab+1) ) then
if (z==z1) then !! TABLE 1
tab_1_nd_c = propssinglemin
else if((z==z2).and.(tbd%nzfiles > 1 )) then !! TABLE 2
tab_2_nd_c = propssinglemin
endif
else if ((i==itab).and.(j==jtab+1) ) then
if (z==z1) then !! TABLE 1
tab_1_nd_d = propssinglemin
else if((z==z2).and.(tbd%nzfiles > 1 )) then !! TABLE 2
tab_2_nd_d = propssinglemin
endif
endif
enddo
enddo
enddo
!print*, 'nphasepresent',nphasepresent
if ( ((tbd%nzfiles==1).and.(nphasepresent==4)) .or. &
((tbd%nzfiles>1).and.(nphasepresent==8)) ) then
call bilinear(tab_1_nd_a,tab_1_nd_b,tab_1_nd_c,tab_1_nd_d, &
tbd,xvec,yvec,itab,jtab,prop_int_tab_1)
if (tbd%nzfiles > 1 ) then
call bilinear(tab_2_nd_a,tab_2_nd_b,tab_2_nd_c,tab_2_nd_d, &
tbd,xvec,yvec,itab,jtab,prop_int_tab_2)
do i=1,num_minpr_req
!! arithmetic mean in z direction
propssinglemin(i) = (1.-zdec)*prop_int_tab_1(i)+zdec*prop_int_tab_2(i)
enddo
else
propssinglemin = prop_int_tab_1
endif
phaseispresent = .true.
else
phaseispresent = .false.
!print*,"### Phase not present, skipping x,y,z =",xvec,yvec,zvec
endif
deallocate(minpr_req)
deallocate( tab_1_nd_a,tab_2_nd_a,&
tab_1_nd_b,tab_2_nd_b,&
tab_1_nd_c,tab_2_nd_c,&
tab_1_nd_d,tab_2_nd_d,&
prop_int_tab_1,prop_int_tab_2 )
return
end subroutine extractsingleminpr
!!===========================================================
subroutine xy2ij (x,y,tbd,i,j,left,down)
!----------------------------------------------------------------------
! xy2ij - identifies the grid point associated with coordinate x-y
! and the sector of the x-y coordinate relative to the nodal point
!----------------------------------------------------------------------
implicit none
integer :: i, j
logical :: left, down
real(dp) :: res, x, y
type(tabledims) :: tbd
res = (x-tbd%xmin)/tbd%dx + 1d0
i = int(res)
if( i.lt.1 ) then
i=1
elseif (i.ge.tbd%nxpts) then
i=tbd%nxpts
endif
if (res-dble(i).gt.0.5d0) then
i = i + 1
left = .true.
else
left = .false.
end if
res = (y-tbd%ymin)/tbd%dy + 1d0
j = int(res)
if( j.lt.1 ) then
j=1
elseif (j.ge.tbd%nypts) then
j=tbd%nypts
endif
if (res-dble(j).gt.0.5d0) then
j = j + 1
down = .true.
else
down = .false.
end if
return
end subroutine xy2ij
!#######################################################################
subroutine checknodes(x,y,i,j,tbd,left,down,onanode,i_int,j_int)
implicit none
logical :: onanode,left,down
real(dp) :: res_x,res_y,x,y
integer :: i_int,j_int,i,j
type(tabledims) :: tbd
! checking whether we're on a node, if so then you can just use
! properties as kept in propar.
res_x = (x-tbd%xmin)/tbd%dx
res_y = (y-tbd%ymin)/tbd%dy
if (res_x.le.1d-6.and.res_y.le.1d-6) then
onanode=.true.
! i_int=i !! for future modifications...
! j_int=j
! return
else
onanode=.false.
endif
!! determining the 4 nodes
! the origin i_int,j_int determines the lower left corner
! i axis
if (left .eqv. .true.) then
i_int = i-1
else
i_int = i
endif
! check bounds
if (i_int .lt. 1 ) then
i_int = 1
else if (i_int .ge. tbd%nxpts ) then