-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynsampdist.f90old
1145 lines (851 loc) · 34.3 KB
/
dynsampdist.f90old
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
subroutine dynsampdist(stat,nDIM,DIMPC,ipar,par,makesamples,ntermsold,nterms,nptsold,npts,nptstoaddpercyc,fct,fpcb,gpcb,hpcb,xcof,RN)
use dimpce
implicit none
include 'collsub.h'
include 'mpif.h'
real*8::fv,gv(ndim),hv(ndim,ndim)
integer ::stat, fct,ierr
integer ::ndim,dimpc,makesamples
integer,intent(in)::nterms
integer,intent(in)::npts,nptsold,nptstoaddpercyc
integer :: ipar(MAXVAR)
integer :: seed
real*8 :: dinvnorm,par(nDIM,MAXPAR),x(NDIM)
real*8,intent(inout)::RN(ndim,maxout)
real*8::RNTEMP(NDIM,nptstoaddpercyc)
character*60::filename
integer :: i,ii,j,jj,jjj,k,kk,kp,l,NTOEX,NTOEXtmp,triangle_node(ndim+1,100000),triangle_num,triangle_coor_num,NCP,node,knnptr(20000),nseed
integer :: mode
double precision :: Dtoextmp(ndim),Dtoex(ndim,100000),dist(100000),minftoex(100000),maxftoex(100000),distmean,ftoextry(2,100000)
double precision :: diff2,RMSE(100000),RMSEmean,EI,Ddibtmp(ndim,0:1000),Dgdibtmp(ndim,0:1000),fdibtmp(0:1000),gdibtmp(ndim,0:1000),hdibtmp(ndim,ndim,0:1000),diffloctmp,difflocmin,difflocavg, SIGMAmean,distcomp
double precision, dimension(nptstoaddpercyc) :: f
double precision, dimension(ndim,nptstoaddpercyc) :: df,Dad,v
double precision, dimension(ndim,ndim,nptstoaddpercyc) :: d2f
double precision, DIMENSION(200) :: SIGV
double precision, DIMENSION(200) :: SIGG
integer :: Taylororder, NCPG,idec,is,ie,id,point,kpc,nptstoaddpercyctmp, nptstoaddpercycorig
double precision :: BETA, GAMM, SIGMA(100000),distmeanloc,RMSEmeanloc,minftoexloc(10000),maxftoexloc(10000),phi,invphi
character*60::export
integer::ntermsold
real*8,intent(inout):: fpcb(MAXPTS),gpcb(NDIM,MAXPTS),hpcb(NDIM,NDIM,maxpts)
real*8,intent(in):: xcof(MAXTRM)
real*8::diffloc
real*8::derivdummy(ndim),dblederivdummy(ndim,ndim)
integer::npass
if (lhsdyn) then
!==========================================
! LHS - DYNAMIC TRAINING POINT SELECTION
!==========================================
if (id_proc.eq.0) then
write(filenum,*) '>> [Picking points dynamically for LHS]'
call get_seed(nseed)
call latin_random(ndim,nptstoaddpercyc,nseed,Dtoex)
do j=1,ndim
do i =1,nptstoaddpercyc
dtoex(j,i)=par(j,1)+(par(j,2)-par(j,1))*DTOEX(j,i) !Scale the test candidates to the domain
end do
end do
do ii=1,nptstoaddpercyc
RN(1:ndim,nptsold+ii)=Dtoex(:,ii)
x(1:ndim)=Dtoex(1:ndim,ii)
call evalcostf(stat,ndim,fct,x,fv,gv,hv)
fpcb(nptsold+ii)=fv !Store the cost function value at appropriate location
if (stat.gt.0) then
gpcb(1:ndim,nptsold+ii)=gv(1:ndim) !Store them appropriately
end if
if (stat.gt.1) then
hpcb(1:ndim,1:ndim,nptsold+ii)=hv(1:ndim,1:ndim)
end if
end do !! ii loop
end if
call MPI_Barrier(MPI_COMM_WORLD,ierr)
call MPI_BCAST(RN(:,:),ndim*maxout,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(fpcb,maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(gpcb,ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(hpcb,ndim*ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
goto 888
return
end if
!==========================================
! DYNAMIC TRAINING POINT SELECTION WITH MIR
!==========================================
! if (abs(nptsold-npts).lt.nptstoaddpercyc) npts=npts+nptstoaddpercyc
! if (abs(nptsold-npts).lt.ndim) npts=npts+nptstoaddpercyc
call mirtunableparams(fct,ndim,nptsold,ncp,taylororder)
! print *,nptsold, ncp
! NTOEX=5000*NDIM
NTOEX=10100
if (NTOEX.gt.25000) NTOEX=25000
! if (stat.eq.2) NTOEX=int(NTOEX*nDIM*nDIM)
if(id_proc.eq.0) write(filenum,*) '>> Number of test candidates',NTOEX
if (id_proc.eq.0) then
call get_seed(nseed)
call latin_random(ndim,NTOEX,nseed,Dtoex)
do j=1,ndim
do i =1,NTOEX
dtoex(j,i)=par(j,1)+(par(j,2)-par(j,1))*DTOEX(j,i) !Scale the test candidates to the domain
end do
end do
! write(export, '(a,i3.3,a)')'testcand.dat'
! open(10,file='./KrigSamples/'//export,form='formatted',status='unknown')
! read(10,*)(Dtoex(:,i),i=1,NToex)
! close(10)
end if !master
! Information sharing by master with slaves
call MPI_BCAST(Dtoex(:,:),100000,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
! do kk=1,ndim
call MPI_BCAST(RN(:,:),ndim*maxout,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
!end do
!if (id_proc.eq.1) print*, RN(1:ndim,1:nptsold),id_proc,ierr,nptsold,maxout
call MPI_BCAST(fpcb,maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(gpcb,ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_Barrier(MPI_COMM_WORLD,ierr)
! print *, DTOEX(1:nDIm,1:ntoex),id_proc,ntoex
idec = dble(NTOEX)/dble(num_proc)
is = idec*id_proc + 1
ie = idec*(id_proc+1)
if(id_proc.eq.num_proc-1)ie =NTOEX
write(filenum,'(11x,a,2i10,a,i3)') '>> [',is,ie,' ] for Processor',id_proc
do k=is,ie !Main loop for test candidates
! Still need to make sure points are not collinear in higher dimensions!
call knn(Dtoex(:,k),RN,knnptr,ndim,nptsold,NCP)!? !Find NCP closest points
NCPG=0
do j=0,NCP-1
node=knnptr(j+1)
Ddibtmp(1:ndim,j)=RN(1:ndim,node)
fdibtmp(j)=fpcb(node)
! print *, fdibtmp(j)
if (stat.gt.0) then
Dgdibtmp(1:ndim,NCPG)=RN(1:ndim,node)
gdibtmp(1:ndim,NCPG)=gpcb(1:ndim,node)
NCPG=NCPG+1
! print *, gdibtmp(1:ndim,NCPG-1)
end if
end do
! Calculate the best parameters beta and gamma
sigv=0.d0
sigg=0.d0
CALL MIR_BETA_GAMMA(1, ndim, NCP, Ddibtmp(:,0:NCP-1), fdibtmp(0:NCP-1), SIGV, NCPG , Dgdibtmp(:,0:NCPG-1), gdibtmp(:,0:NCPG-1), SIGG, Taylororder, 1, dble(1.0), BETA, GAMM, IERR)
if (IERR.ne.0) stop'Error in MIR Local Surrogate Beta Gamma'
CALL MIR_EVALUATE(1, ndim, 1, Dtoex(:,k), NCP, Ddibtmp(:,0:NCP-1), fdibtmp(0:NCP-1), SIGV, NCPG , Dgdibtmp(:,0:NCPG-1), gdibtmp(:,0:NCPG-1), SIGG, BETA, GAMM, Taylororder, 1, ftoextry(1,k), SIGMA(k), IERR)
if (IERR.ne.0)stop'Error in MIR Local Surrogate Beta Gamma'
call evalPC(ndim,dimPC-1,ipar,xcof,Dtoex(:,k),ftoextry(2,k),derivdummy,dblederivdummy) ! Evaluates PC at x and returns yhat
! print *,k,ftoextry(2,k),ftoextry(1,k)!(ftoextry(2,k)-ftoextry(1,k))!,ftoextry(2,k),ftoextry(1,k),
end do !NTOEX
! Information Sharing --Exchange the function values
do id=0,num_proc-1
is = idec*id + 1
ie = idec*(id+1)
if(id.eq.num_proc-1)ie = NTOEX
call MPI_BCAST(ftoextry(1,is:ie),ie-is+1,MPI_DOUBLE_PRECISION,id,MPI_COMM_WORLD,ierr)
call MPI_BCAST(ftoextry(2,is:ie),ie-is+1,MPI_DOUBLE_PRECISION,id,MPI_COMM_WORLD,ierr)
end do
!!$
!!$ if (id_proc.eq.0) then
!!$ do i=1,npts
!!$ print *, RN(1:NDIM,i)
!!$ end do
!!$
!!$ print*,'after'
!!$end if
if (id_proc.eq.0) then
do k =1,NTOEX
minftoex(k)=ftoextry(1,k)
maxftoex(k)=ftoextry(1,k)
if (ftoextry(2,k).gt.maxftoex(k)) then
maxftoex(k)=ftoextry(2,k)
else
minftoex(k)=ftoextry(2,k)
end if
end do
! Figure out distance to closest real sample point and the mean of all these distances
distmean=0.0
do k=1,NTOEX
dist(k)=1000000000000.0
do kk=1,nptsold!nhs
diff2=0.0
do jj=1,ndim
diff2=diff2+(Dtoex(jj,k)-RN(jj,kk))**2
end do
if ( diff2.lt.dist(k) ) then !current sample closer than previous samples update the smallest distance
dist(k)=diff2
end if
end do
dist(k)=SQRT(dist(k))
distmean= distmean+dist(k)
end do
distmean=distmean/real(NTOEX)
! Pick test candidate with largest difference in values, but above distcomp distance to nearest neighbours
! if (NTOEX.lt.10) then
distcomp=distmean
! else
! distcomp=1.1*distmean !0.618
! end if
diffloctmp=0.0d0
diffloc2=0.0d0
difflocmax=0.0d0
do k=1,NTOEX
diffloctmp=(maxftoex(k)-minftoex(k))**2
diffloc2=diffloc2+diffloctmp
difflocmax=max(difflocmax,sqrt(diffloctmp))
end do
diffloc2=diffloc2/dble(ntoex)
diffloc2=sqrt(diffloc2)
diffloc=0.0
do ii=1,nptstoaddpercyc
npass=0
do while (npass.ne.1)
kp=0
diffloctmp=0.0d0
do k=1,NTOEX
!! Successful Training point passes the follwing tests:
!! 1. The local difference between the local and global surrogate should >
!! 2. The next training point should be atleast distcomp away from the closest existing sample
!! 3. RMSE should be greater than RMSE mean of Kriging
!! 4. SIGMA should be greater than SIGMA mean of MIR
if ((maxftoex(k)-minftoex(k)).gt.diffloctmp .and. dist(k).ge.distcomp ) then !.and. SIGMA(k).ge.SIGMAmean .and. RMSE(k).ge.RMSEmean
diffloctmp=maxftoex(k)-minftoex(k)
kp=k
end if
end do
diffloc=max(diffloc,diffloctmp)
if (kp.eq.0) then ! if no successful candidate
write (filenum,*) ' >>No passing candidate found . . .'
write (filenum,*) ' >>Relaxing geometric constraint by 2 percent . . .'
distcomp=0.98*distcomp
else
npass=npass+1 ! one successful candidate
write(filenum,*)
write(filenum,'(a,E10.2,a,i6,a,i4)') '>>Loc diff is',diffloctmp,' for test candidate',kp,' at iteration',dyncyccnt
write(filenum,'(a,20F10.2)')' x = ',DTOEX(1:NDIM,kp)
end if
end do! while loop execute until a passing candidate is found
!!$
!!$ write (filenum,*) 'Could not find suitable test candidate just take the one with largest difference'
!!$ diffloctmp=0.0
!!$ do k=1,NTOEX
!!$ if ((maxftoex(k)-minftoex(k)).gt.diffloctmp) then
!!$ diffloctmp=maxftoex(k)-minftoex(k)
!!$ kp=k
!!$ end if
!!$ end do
!!$
! Trick to not consider this point again
maxftoex(kp)=minftoex(kp)
! Update other minimum distances
do k=1,NTOEX
if (k.ne.kp) then
diff2=0.0
do jj=1,ndim
diff2=diff2+(Dtoex(jj,k)-Dtoex(jj,kp))**2
end do
diff2=SQRT(diff2)
if ( diff2.lt.dist(k) ) then
dist(k)=diff2
end if
end if
end do
RN(1:ndim,nptsold+ii)=Dtoex(:,kp)
x(1:ndim)=Dtoex(1:ndim,kp)
call evalcostf(stat,ndim,fct,x,fv,gv,hv)
fpcb(nptsold+ii)=fv !Store the cost function value at appropriate location
if (stat.gt.0) then
gpcb(1:ndim,nptsold+ii)=gv(1:ndim) !Store them appropriately
end if
if (stat.gt.1) then
hpcb(1:ndim,1:ndim,nptsold+ii)=hv(1:ndim,1:ndim)
end if
! if (fct.eq.20) then
! x(1)=x(1)*180.0/4.0/atan(1.0) !RADINS TO degree
! end if
!!$
!!$ ! Evaluate desired quantities
!!$ if(nstyle.eq.0) then
!!$ Dad(:,ii)=Dtoex(:,kp)
!!$ call evalfunc(Dtoex(:,kp),ndim,fct,0,hstatad(ii),f(ii),df(:,ii),d2f(:,:,ii),v(:,ii))
!!$ end if
!INSTEAD STORE INTO RN
end do !! ii loop
! difflocmax=diffloc !stores maxdiff to write as output
END IF! master thread
call MPI_BCAST(RN(:,:),ndim*maxout,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(fpcb,maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(gpcb,ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(hpcb,ndim*ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
goto 888
!1 ) Assume that the model is built with a particular order say 3, [x,f(x)] are already in a file named sampleandvalues.dat (for now lets forget about center and borders)
!2 ) Now for order 4 we want to choose nptstoaddpercyc points to samplepoints.dat in this subroutine and pass it as RNF
!3 ) Make setting up RHS part of the program, read the previous [x,f(x)] from the file samplepoints.dat and then evaluate the remaining points from evalfunc.f90 and immediately append the values to samplepoints.dat
!!$ if (id_proc.eq.0) then
!!$ ! dont forget to scale and output the RN
!!$
!!$ do i=1,npts
!!$ print *,i,RN(1:NDIM,i)
!!$ end do
!!$ end if
!!$ call MPI_Barrier(MPI_COMM_WORLD,ierr)!slaves wait until master arrives
!!$ call stop_all
888 continue
if (id_proc.eq.0 ) then
if (fct.eq.20) then
if (fctindx.eq.0) then
open(unit=88,file='output/tecsamp00.dat')
else
open(unit=88,file='output/tecsamp04.dat')
end if
if (ndim.eq.2) then
write(88,*) 'variables= "x","y","f"'
write(88,*) 'zone i=',npts,' datapacking=point'
else if (nDim.eq.1) then
write(88,*) 'variables= "x","f"'
write(88,*) 'zone i=',npts,' datapacking=point'
end if
do j=1,npts
x(1:nDIM)=RN(1:nDIM,j)
x(1)=x(1)*180.0/4.0/atan(1.0) !RADINS TO degree
write (88,*) (x(ii),ii=1,ndim),fpcb(j)
end do ! loop over all points
close(88)
end if
if(ndim.le.2) then
!====================================================
! Polynomial chaos sample points to file
!=====================================================
if (ndim.eq.2) then
open(unit=43,file='output/outputsamplePCtmp')
write(43,*) 'variables= "x","y","fpcb"'
write(43,*) 'zone i=',npts,' datapacking=point'
! write(43,*) 'variables= "x","y"'
! write(43,*) 'zone i=',npts,' j=',npts,' datapacking=point'
else if (nDim.eq.1) then
write(43,*) 'variables= "x"'
write(43,*) 'zone i=',npts,' datapacking=point'
end if
do j=1,npts
write (43,*) (RN(i,j),i=1,nDIM),0.0d0
enddo
close(43)
end if
end if
call MPI_Barrier(MPI_COMM_WORLD,ierr)!slaves wait until master arrives
return
end subroutine dynsampdist
!++++++++++++++++++++++++++++++++++++++++
real*8::fv,gv(ndim),hv(ndim,ndim)
integer ::stat, fct,ierr
integer ::ndim,dimpc,makesamples
integer,intent(in)::nterms
integer,intent(in)::npts,nptsold,nptstoaddpercyc
integer :: ipar(MAXVAR)
integer :: seed
real*8 :: dinvnorm,par(nDIM,MAXPAR),x(NDIM)
real*8,intent(inout)::RN(ndim,maxout)
real*8::RNTEMP(NDIM,nptstoaddpercyc)
character*60::filename
integer :: i,ii,j,jj,jjj,k,kk,kp,l,NTOEX,NTOEXtmp,triangle_node(ndim+1,100000),triangle_num,triangle_coor_num,NCP,node,knnptr(20000),nseed
integer :: mode
double precision :: Dtoextmp(ndim),Dtoex(ndim,100000),dist(100000),minftoex(100000),maxftoex(100000),distmean,ftoextry(2,100000)
double precision :: diff2,RMSE(100000),RMSEmean,EI,Ddibtmp(ndim,0:1000),Dgdibtmp(ndim,0:1000),fdibtmp(0:1000),gdibtmp(ndim,0:1000),hdibtmp(ndim,ndim,0:1000),diffloctmp,difflocmin,difflocavg, SIGMAmean,distcomp
double precision, dimension(nptstoaddpercyc) :: f
double precision, dimension(ndim,nptstoaddpercyc) :: df,Dad,v
double precision, dimension(ndim,ndim,nptstoaddpercyc) :: d2f
double precision, DIMENSION(200) :: SIGV
double precision, DIMENSION(200) :: SIGG
integer :: Taylororder, NCPG,idec,is,ie,id,point,kpc,nptstoaddpercyctmp, nptstoaddpercycorig
double precision :: BETA, GAMM, SIGMA(100000),distmeanloc,RMSEmeanloc,minftoexloc(10000),maxftoexloc(10000),phi,invphi
character*60::export
integer::ntermsold
real*8,intent(inout):: fpcb(MAXPTS),gpcb(NDIM,MAXPTS),hpcb(NDIM,NDIM,maxpts)
real*8,intent(in):: xcof(MAXTRM)
real*8::diffloc
real*8::derivdummy(ndim),dblederivdummy(ndim,ndim)
integer::npass
if (lhsdyn) then
!==========================================
! LHS - DYNAMIC TRAINING POINT SELECTION
!==========================================
if (id_proc.eq.0) then
write(filenum,*) '>> [Picking points dynamically for LHS]'
call get_seed(nseed)
call latin_random(ndim,nptstoaddpercyc,nseed,Dtoex)
do j=1,ndim
do i =1,nptstoaddpercyc
dtoex(j,i)=par(j,1)+(par(j,2)-par(j,1))*DTOEX(j,i) !Scale the test candidates to the domain
end do
end do
do ii=1,nptstoaddpercyc
RN(1:ndim,nptsold+ii)=Dtoex(:,ii)
x(1:ndim)=Dtoex(1:ndim,ii)
call evalcostf(stat,ndim,fct,x,fv,gv,hv)
fpcb(nptsold+ii)=fv !Store the cost function value at appropriate location
if (stat.gt.0) then
gpcb(1:ndim,nptsold+ii)=gv(1:ndim) !Store them appropriately
end if
if (stat.gt.1) then
hpcb(1:ndim,1:ndim,nptsold+ii)=hv(1:ndim,1:ndim)
end if
end do !! ii loop
end if
call MPI_Barrier(MPI_COMM_WORLD,ierr)
call MPI_BCAST(RN(:,:),ndim*maxout,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(fpcb,maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(gpcb,ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(hpcb,ndim*ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
goto 888
return
end if
!==========================================
! DYNAMIC TRAINING POINT SELECTION WITH MIR
!==========================================
! if (abs(nptsold-npts).lt.nptstoaddpercyc) npts=npts+nptstoaddpercyc
! if (abs(nptsold-npts).lt.ndim) npts=npts+nptstoaddpercyc
call mirtunableparams(fct,ndim,nptsold,ncp,taylororder)
! print *,nptsold, ncp
! NTOEX=5000*NDIM
NTOEX=10100
if (NTOEX.gt.25000) NTOEX=25000
! if (stat.eq.2) NTOEX=int(NTOEX*nDIM*nDIM)
if(id_proc.eq.0) write(filenum,*) '>> Number of test candidates',NTOEX
if (id_proc.eq.0) then
call get_seed(nseed)
call latin_random(ndim,NTOEX,nseed,Dtoex)
do j=1,ndim
do i =1,NTOEX
dtoex(j,i)=par(j,1)+(par(j,2)-par(j,1))*DTOEX(j,i) !Scale the test candidates to the domain
end do
end do
! write(export, '(a,i3.3,a)')'testcand.dat'
! open(10,file='./KrigSamples/'//export,form='formatted',status='unknown')
! read(10,*)(Dtoex(:,i),i=1,NToex)
! close(10)
end if !master
! Information sharing by master with slaves
call MPI_BCAST(Dtoex(:,:),100000,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
! do kk=1,ndim
call MPI_BCAST(RN(:,:),ndim*maxout,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
!end do
!if (id_proc.eq.1) print*, RN(1:ndim,1:nptsold),id_proc,ierr,nptsold,maxout
call MPI_BCAST(fpcb,maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(gpcb,ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_Barrier(MPI_COMM_WORLD,ierr)
! print *, DTOEX(1:nDIm,1:ntoex),id_proc,ntoex
idec = dble(NTOEX)/dble(num_proc)
is = idec*id_proc + 1
ie = idec*(id_proc+1)
if(id_proc.eq.num_proc-1)ie =NTOEX
write(filenum,'(11x,a,2i10,a,i3)') '>> [',is,ie,' ] for Processor',id_proc
do k=is,ie !Main loop for test candidates
! Still need to make sure points are not collinear in higher dimensions!
call knn(Dtoex(:,k),RN,knnptr,ndim,nptsold,NCP)!? !Find NCP closest points
NCPG=0
do j=0,NCP-1
node=knnptr(j+1)
Ddibtmp(1:ndim,j)=RN(1:ndim,node)
fdibtmp(j)=fpcb(node)
! print *, fdibtmp(j)
if (stat.gt.0) then
Dgdibtmp(1:ndim,NCPG)=RN(1:ndim,node)
gdibtmp(1:ndim,NCPG)=gpcb(1:ndim,node)
NCPG=NCPG+1
! print *, gdibtmp(1:ndim,NCPG-1)
end if
end do
! Calculate the best parameters beta and gamma
sigv=0.d0
sigg=0.d0
CALL MIR_BETA_GAMMA(1, ndim, NCP, Ddibtmp(:,0:NCP-1), fdibtmp(0:NCP-1), SIGV, NCPG , Dgdibtmp(:,0:NCPG-1), gdibtmp(:,0:NCPG-1), SIGG, Taylororder, 1, dble(1.0), BETA, GAMM, IERR)
if (IERR.ne.0) stop'Error in MIR Local Surrogate Beta Gamma'
CALL MIR_EVALUATE(1, ndim, 1, Dtoex(:,k), NCP, Ddibtmp(:,0:NCP-1), fdibtmp(0:NCP-1), SIGV, NCPG , Dgdibtmp(:,0:NCPG-1), gdibtmp(:,0:NCPG-1), SIGG, BETA, GAMM, Taylororder, 1, ftoextry(1,k), SIGMA(k), IERR)
if (IERR.ne.0)stop'Error in MIR Local Surrogate Beta Gamma'
call evalPC(ndim,dimPC-1,ipar,xcof,Dtoex(:,k),ftoextry(2,k),derivdummy,dblederivdummy) ! Evaluates PC at x and returns yhat
! print *,k,ftoextry(2,k),ftoextry(1,k)!(ftoextry(2,k)-ftoextry(1,k))!,ftoextry(2,k),ftoextry(1,k),
end do !NTOEX
! Information Sharing --Exchange the function values
do id=0,num_proc-1
is = idec*id + 1
ie = idec*(id+1)
if(id.eq.num_proc-1)ie = NTOEX
call MPI_BCAST(ftoextry(1,is:ie),ie-is+1,MPI_DOUBLE_PRECISION,id,MPI_COMM_WORLD,ierr)
call MPI_BCAST(ftoextry(2,is:ie),ie-is+1,MPI_DOUBLE_PRECISION,id,MPI_COMM_WORLD,ierr)
end do
!!$
!!$ if (id_proc.eq.0) then
!!$ do i=1,npts
!!$ print *, RN(1:NDIM,i)
!!$ end do
!!$
!!$ print*,'after'
!!$end if
if (id_proc.eq.0) then
do k =1,NTOEX
minftoex(k)=ftoextry(1,k)
maxftoex(k)=ftoextry(1,k)
if (ftoextry(2,k).gt.maxftoex(k)) then
maxftoex(k)=ftoextry(2,k)
else
minftoex(k)=ftoextry(2,k)
end if
end do
! Figure out distance to closest real sample point and the mean of all these distances
distmean=0.0
do k=1,NTOEX
dist(k)=1000000000000.0
do kk=1,nptsold!nhs
diff2=0.0
do jj=1,ndim
diff2=diff2+(Dtoex(jj,k)-RN(jj,kk))**2
end do
if ( diff2.lt.dist(k) ) then !current sample closer than previous samples update the smallest distance
dist(k)=diff2
end if
end do
dist(k)=SQRT(dist(k))
distmean= distmean+dist(k)
end do
distmean=distmean/real(NTOEX)
! Pick test candidate with largest difference in values, but above distcomp distance to nearest neighbours
! if (NTOEX.lt.10) then
distcomp=distmean
! else
! distcomp=1.1*distmean !0.618
! end if
diffloctmp=0.0d0
diffloc2=0.0d0
difflocmax=0.0d0
do k=1,NTOEX
diffloctmp=(maxftoex(k)-minftoex(k))**2
diffloc2=diffloc2+diffloctmp
difflocmax=max(difflocmax,sqrt(diffloctmp))
end do
diffloc2=diffloc2/dble(ntoex)
diffloc2=sqrt(diffloc2)
diffloc=0.0
do ii=1,nptstoaddpercyc
npass=0
do while (npass.ne.1)
kp=0
diffloctmp=0.0d0
do k=1,NTOEX
!! Successful Training point passes the follwing tests:
!! 1. The local difference between the local and global surrogate should >
!! 2. The next training point should be atleast distcomp away from the closest existing sample
!! 3. RMSE should be greater than RMSE mean of Kriging
!! 4. SIGMA should be greater than SIGMA mean of MIR
if ((maxftoex(k)-minftoex(k)).gt.diffloctmp .and. dist(k).ge.distcomp ) then !.and. SIGMA(k).ge.SIGMAmean .and. RMSE(k).ge.RMSEmean
diffloctmp=maxftoex(k)-minftoex(k)
kp=k
end if
end do
diffloc=max(diffloc,diffloctmp)
if (kp.eq.0) then ! if no successful candidate
write (filenum,*) ' >>No passing candidate found . . .'
write (filenum,*) ' >>Relaxing geometric constraint by 2 percent . . .'
distcomp=0.98*distcomp
else
npass=npass+1 ! one successful candidate
write(filenum,*)
write(filenum,'(a,E10.2,a,i6,a,i4)') '>>Loc diff is',diffloctmp,' for test candidate',kp,' at iteration',dyncyccnt
write(filenum,'(a,20F10.2)')' x = ',DTOEX(1:NDIM,kp)
end if
end do! while loop execute until a passing candidate is found
!!$
!!$ write (filenum,*) 'Could not find suitable test candidate just take the one with largest difference'
!!$ diffloctmp=0.0
!!$ do k=1,NTOEX
!!$ if ((maxftoex(k)-minftoex(k)).gt.diffloctmp) then
!!$ diffloctmp=maxftoex(k)-minftoex(k)
!!$ kp=k
!!$ end if
!!$ end do
!!$
! Trick to not consider this point again
maxftoex(kp)=minftoex(kp)
! Update other minimum distances
do k=1,NTOEX
if (k.ne.kp) then
diff2=0.0
do jj=1,ndim
diff2=diff2+(Dtoex(jj,k)-Dtoex(jj,kp))**2
end do
diff2=SQRT(diff2)
if ( diff2.lt.dist(k) ) then
dist(k)=diff2
end if
end if
end do
RN(1:ndim,nptsold+ii)=Dtoex(:,kp)
x(1:ndim)=Dtoex(1:ndim,kp)
call evalcostf(stat,ndim,fct,x,fv,gv,hv)
fpcb(nptsold+ii)=fv !Store the cost function value at appropriate location
if (stat.gt.0) then
gpcb(1:ndim,nptsold+ii)=gv(1:ndim) !Store them appropriately
end if
if (stat.gt.1) then
hpcb(1:ndim,1:ndim,nptsold+ii)=hv(1:ndim,1:ndim)
end if
! if (fct.eq.20) then
! x(1)=x(1)*180.0/4.0/atan(1.0) !RADINS TO degree
! end if
!!$
!!$ ! Evaluate desired quantities
!!$ if(nstyle.eq.0) then
!!$ Dad(:,ii)=Dtoex(:,kp)
!!$ call evalfunc(Dtoex(:,kp),ndim,fct,0,hstatad(ii),f(ii),df(:,ii),d2f(:,:,ii),v(:,ii))
!!$ end if
!INSTEAD STORE INTO RN
end do !! ii loop
! difflocmax=diffloc !stores maxdiff to write as output
END IF! master thread
call MPI_BCAST(RN(:,:),ndim*maxout,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(fpcb,maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(gpcb,ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(hpcb,ndim*ndim*maxpts,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
goto 888
!1 ) Assume that the model is built with a particular order say 3, [x,f(x)] are already in a file named sampleandvalues.dat (for now lets forget about center and borders)
!2 ) Now for order 4 we want to choose nptstoaddpercyc points to samplepoints.dat in this subroutine and pass it as RNF
!3 ) Make setting up RHS part of the program, read the previous [x,f(x)] from the file samplepoints.dat and then evaluate the remaining points from evalfunc.f90 and immediately append the values to samplepoints.dat
!!$ if (id_proc.eq.0) then
!!$ ! dont forget to scale and output the RN
!!$
!!$ do i=1,npts
!!$ print *,i,RN(1:NDIM,i)
!!$ end do
!!$ end if
!!$ call MPI_Barrier(MPI_COMM_WORLD,ierr)!slaves wait until master arrives
!!$ call stop_all
888 continue
if (id_proc.eq.0 ) then
if (fct.eq.20) then
if (fctindx.eq.0) then
open(unit=88,file='output/tecsamp00.dat')
else
open(unit=88,file='output/tecsamp04.dat')
end if
if (ndim.eq.2) then
write(88,*) 'variables= "x","y","f"'
write(88,*) 'zone i=',npts,' datapacking=point'
else if (nDim.eq.1) then
write(88,*) 'variables= "x","f"'
write(88,*) 'zone i=',npts,' datapacking=point'
end if
do j=1,npts
x(1:nDIM)=RN(1:nDIM,j)
x(1)=x(1)*180.0/4.0/atan(1.0) !RADINS TO degree
write (88,*) (x(ii),ii=1,ndim),fpcb(j)
end do ! loop over all points
close(88)
end if
if(ndim.le.2) then
!====================================================
! Polynomial chaos sample points to file
!=====================================================
if (ndim.eq.2) then
open(unit=43,file='output/outputsamplePCtmp')
write(43,*) 'variables= "x","y","fpcb"'
write(43,*) 'zone i=',npts,' datapacking=point'
! write(43,*) 'variables= "x","y"'
! write(43,*) 'zone i=',npts,' j=',npts,' datapacking=point'
else if (nDim.eq.1) then
write(43,*) 'variables= "x"'
write(43,*) 'zone i=',npts,' datapacking=point'
end if
do j=1,npts
write (43,*) (RN(i,j),i=1,nDIM),0.0d0
enddo
close(43)
end if
end if
call MPI_Barrier(MPI_COMM_WORLD,ierr)!slaves wait until master arrives
return
end subroutine dynsampdist
!++++++++++++++++++++++++++++++++++++++++
subroutine mirtunableparams(fct,ndim,nhs,ncp,taylororder)
implicit none
integer::fct,ndim,taylororder,ncp,nhs
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
! EXP
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
if (Fct.eq.0) then
if (nhs.le.11) then
NCP=nhs
Taylororder=3!nhs!2!2!INT(NHS/4)
else if (nhs.gt.11 .and. nhs.le.15) then
NCP=10
Taylororder=5!ncp
else if (nhs.gt.15 .and. nhs.le.25) then
NCP=15
Taylororder=5!ncp
else if (nhs.gt.25 .and. nhs.le.35) then
NCP=20
Taylororder=5!ncp
else if (nhs.gt.35 .and. nhs.le.100) then
NCP=20!20+3*ndim
Taylororder=5
! else if (nhs.gt.70 .and. nhs.le.100) then
! NCP=30!50+0.1*nhs
! Taylororder=17
else
NCP=20!+5*ndim
tAYLORORDER=5
end if
end if
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
! COS
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
if (Fct.eq.1) then
if (nhs.le.11) then
NCP=5
Taylororder=ncp!2!2!INT(NHS/4)
else if (nhs.gt.11 .and. nhs.le.15) then
NCP=10
Taylororder=5!ncp
else if (nhs.gt.15 .and. nhs.le.25) then
NCP=15
Taylororder=5!ncp
else if (nhs.gt.25 .and. nhs.le.35) then
NCP=20
Taylororder=5!ncp
else if (nhs.gt.35 .and. nhs.le.70) then
NCP=20!50+0.1*nhs
Taylororder=5
else if (nhs.gt.70 .and. nhs.le.100) then
NCP=20!50+0.1*nhs
Taylororder=5
else
NCP=20
tAYLORORDER=5
end if
end if
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
! RUNGE
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
if (fct.eq.2) then
if (nhs.le.20) then
NCP=nhs
Taylororder=5!nhs!2!2!INT(NHS/4)
! else if (nhs.gt.11 .and. nhs.le.15) then
! NCP=nhs
! Taylororder=7!ncp
!
! else if (nhs.gt.15 .and. nhs.le.25) then
! NCP=nhs
! Taylororder=13!ncp
!
! else if (nhs.gt.25 .and. nhs.le.35) then
! NCP=nhs