-
Notifications
You must be signed in to change notification settings - Fork 15
/
pso.f90
1254 lines (1172 loc) · 62.8 KB
/
pso.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
#include "alias.inc"
subroutine pso_fit (PINPT, PPRAM, PKPTS, PWGHT, PGEOM, NN_TABLE, EDFT, iseed_, pso_miter)
use mykind
use mpi_setup
use parameters, only: incar, params, kpoints, weight, poscar, hopping, energy
use cost_function, only: get_dE, get_cost_function, get_dE12
use print_io
use random_mod
implicit none
type(incar ) :: PINPT ! only INCAR from first system is used
type(params ) :: PPRAM ! asume each system shares same parameter
type(kpoints ), dimension(PINPT%nsystem) :: PKPTS
type(weight ), dimension(PINPT%nsystem) :: PWGHT
type(poscar ), dimension(PINPT%nsystem) :: PGEOM
type(hopping ), dimension(PINPT%nsystem) :: NN_TABLE
type(energy ), dimension(PINPT%nsystem) :: EDFT, ETBA
logical flag_stat
integer(kind=sp) n_particles, nparam_free
integer(kind=sp) iparam, iter, iptcl
real(kind=dp), allocatable :: fvec(:), fvec_plain(:), fvec_orb(:)
real(kind=dp), allocatable :: vel(:,:), pos(:,:)
real(kind=dp), allocatable :: pbest(:,:), gbest(:)
real(kind=dp), allocatable :: pbest_history(:,:,:)
real(kind=dp), allocatable :: cpbest(:), cpbest_plain(:), cpbest_orb(:)
real(kind=dp) :: cgbest, cgbest_plain, cost
real(kind=dp) :: cgbest_orb
real(kind=dp), allocatable :: costs(:), costs_plain(:), costs_orb(:)
real(kind=dp), allocatable :: costs_(:), costs_plain_(:), costs_orb_(:)
integer(kind=sp) i, min_id
integer(kind=sp) imode, ldjac
integer(kind=sp) min_loc(1), pso_miter
logical flag_order, flag_go
real(kind=dp) c1, c2, w, r1, r2
integer(kind=sp) iseed, iseed_
integer(kind=sp) mpierr
integer(kind=sp) iselect_mode
real(kind=dp), external :: enorm
real(kind=dp) fnorm, fnorm_plain, fnorm_orb
real(kind=dp) pmax, pmin
external get_eig
integer(kind=sp) info, maxfev
real(kind=dp) epsfcn, factor, xtol, ftol, gtol
logical flag_with_lmdif
logical flag_fit_plain, flag_fit_orbital
integer(kind=sp), allocatable :: ourgroup(:), ourjob(:)
integer(kind=sp) mygroup(0:nprocs-1), groupid
#ifdef MPI
integer(kind=sp) mpistat(MPI_STATUS_SIZE)
#endif
!PPRAM%flag_fit_plain = .TRUE.
flag_fit_orbital= PINPT%flag_fit_orbital
flag_fit_plain = PPRAM%flag_fit_plain
flag_with_lmdif = PINPT%flag_pso_with_lmdif
flag_order = .FALSE.
imode = 13
n_particles = PPRAM%pso_nparticles
nparam_free = PPRAM%nparam_free
c1 = PPRAM%pso_c1
c2 = PPRAM%pso_c2
w = PPRAM%pso_w
iseed = iseed_
r1 = 1d0
r2 = 1d0
call random_init(iseed)
if(flag_fit_orbital .and. flag_fit_plain) then
flag_fit_plain = .FALSE. ! we consider fit_orbital instead of fit_plain
endif
if(flag_fit_orbital) then
iselect_mode = 3
elseif(flag_fit_plain) then
iselect_mode = 2
else
iselect_mode = 1
endif
#ifdef MPI
call MPI_BARRIER(mpi_comm_earth, mpierr)
!Note: n_particles will be solved in n_groups of cpu family.
! Each group will have n_member ~ nprocs/ngroup
! The n_member will be further parallized to solve eigenvalue problem in get_eig routine
call get_npar(trim(PINPT%ifilenm(1)))
allocate(ourgroup(npar)); allocate(ourjob(npar))
call mpi_job_distribution_group(npar, n_particles, ourgroup, mygroup, ourjob)
call mpi_comm_anmeldung(COMM_KOREA, npar, mygroup)
groupid = COMM_KOREA%color
write(message,'(A,I0,A)') ' JOB DISTRUBUTION for PSO particles over NPAR (NPAR=',npar,'):' ; write_msg
do i = 1, npar
write(message,'(A,3(I0,A))') ' -> Group_id(',i-1,'): ',ourgroup(i), &
' cpus asigned and ',ourjob(i),' jobs are distributed' ; write_msg
enddo
call MPI_BARRIER(mpi_comm_earth, mpierr)
#else
allocate(ourjob(1))
ourjob = n_particles
groupid= 0
#endif
factor = 100.0D+00
maxfev = PINPT%miter * ( PPRAM%nparam_free + 1 )
ftol = PINPT%ftol ;xtol = PINPT%ptol ; gtol = 0.0D+00; epsfcn = 0.000D+00
if(allocated(PPRAM%pso_cost_history)) deallocate(PPRAM%pso_cost_history)
if(allocated(PPRAM%pso_cost_history_i)) deallocate(PPRAM%pso_cost_history_i)
allocate(PPRAM%pso_cost_history(pso_miter))
allocate(PPRAM%pso_cost_history_i(pso_miter,n_particles))
PPRAM%pso_cost_history = 0d0
PPRAM%pso_cost_history_i = 0d0
do i = 1, PINPT%nsystem
call allocate_ETBA(PGEOM(i), PINPT, PKPTS(i), ETBA(i), .FALSE., 0)
if(allocated(ETBA(i)%dE)) deallocate(ETBA(i)%dE)
allocate(ETBA(i)%dE( size(ETBA(i)%E(:,1)) , size(ETBA(i)%E(1,:)) ))
ETBA(i)%E = 0d0 ; ETBA(i)%V = 0d0 ; ETBA(i)%SV = 0d0 ; ETBA(i)%dE=0d0
if(flag_fit_orbital) then
if(allocated(ETBA(i)%dORB)) deallocate(ETBA(i)%dORB)
allocate(ETBA(i)%dORB( size(ETBA(i)%E(:,1)),size(ETBA(i)%E(1,:)) ))
endif
enddo
allocate(vel( n_particles, nparam_free))
allocate(pos( n_particles, nparam_free))
allocate(pbest(n_particles, nparam_free))
if(allocated(PPRAM%pso_pbest_history)) deallocate(PPRAM%pso_pbest_history)
allocate(PPRAM%pso_pbest_history(pso_miter, n_particles, nparam_free))
allocate(gbest( nparam_free))
allocate(cpbest(n_particles ))
allocate(cpbest_plain(n_particles ))
allocate(cpbest_orb(n_particles ))
allocate(costs(n_particles ))
allocate(costs_plain(n_particles ))
allocate(costs_orb(n_particles ))
allocate(costs_(n_particles ))
allocate(costs_plain_(n_particles ))
allocate(costs_orb_(n_particles ))
costs = 0.d0 ; costs_plain = 0.d0 ; costs_ = 0.d0 ; costs_plain_ = 0.d0
costs_orb = 0d0 ; costs_orb_ = 0d0
vel = 0.d0 ; pos = 0.d0
pbest = 0.d0 ; gbest = 0.d0
cpbest = 0.d0 ; cpbest_plain = 0.d0 ; cpbest_orb = 0d0
cgbest = 0.d0 ; cgbest_plain = 0.d0 ; cgbest_orb = 0d0
PPRAM%pso_pbest_history = 0d0
ldjac = 0
do i = 1, PINPT%nsystem
ldjac = ldjac + PKPTS(i)%nkpoint * PGEOM(i)%nband * PINPT%nspin
enddo
if(ldjac .ne. 1) then
allocate(fvec(ldjac))
allocate(fvec_plain(ldjac))
!if(flag_fit_orbital) allocate(fvec_orb(ldjac))
allocate(fvec_orb(ldjac))
endif
fvec = 0d0 ; fvec_plain = 0d0
if(flag_fit_orbital) fvec_orb = 0d0
! initialize parameters with random noise
do iptcl = 1, n_particles
do iparam = 1, nparam_free
flag_go = .FALSE.
do while (.not. flag_go)
#ifdef MPI
if_main r1 = (random()*2d0 - 1d0) * PPRAM%pso_max_noise_amplitude
if_main r2 = (random()*2d0 - 1d0) + 1d-10 ! sign
if_main r2 = r2 / abs(r2)
call MPI_BCAST(r1, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(r2, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
! NOTE: all processors share random number over "iptcl" loop
! so that pos and vel update is to be mpi_comm_earth wide
! => up to this point, every processor knows vel and pos for all particles
#else
r1 = (random()*2d0 - 1d0) * 5d0
r2 = (random()*2d0 - 1d0) + 1d-10 ! sign
r2 = r2 / abs(r2)
#endif
if(iptcl .eq. 1) then ! use initial parameter without noise for particle-1 only
r1 = 0d0
r2 = 1d0
endif
if( PPRAM%param( PPRAM%iparam_free(iparam) ) .ge. PPRAM%param_const(2, PPRAM%iparam_free(iparam) ) .or. &
PPRAM%param( PPRAM%iparam_free(iparam) ) .le. PPRAM%param_const(3, PPRAM%iparam_free(iparam) )) then
pmax = PPRAM%param_const(2, PPRAM%iparam_free(iparam) )
pmin = PPRAM%param_const(3, PPRAM%iparam_free(iparam) )
if(PPRAM%iparam_type( PPRAM%iparam_free(iparam) ) .le. 2) then
pos(iptcl, iparam) = (pmax+pmin)*0.5d0*r2 + r1
else ! overlap
pos(iptcl, iparam) = (pmax+pmin)*0.5d0*r2
endif
else
if(PPRAM%iparam_type( PPRAM%iparam_free(iparam) ) .le. 2) then
pos(iptcl, iparam) = PPRAM%param( PPRAM%iparam_free(iparam) )*r2 + r1
else !overlap
pos(iptcl, iparam) = PPRAM%param( PPRAM%iparam_free(iparam) )*r2 !+ r1
endif
endif
vel(iptcl, iparam) = r1
if( pos(iptcl, iparam) .ge. PPRAM%param_const(2, PPRAM%iparam_free(iparam) ) .or. &
pos(iptcl, iparam) .le. PPRAM%param_const(3, PPRAM%iparam_free(iparam) )) then
flag_go = .FALSE.
else
flag_go = .TRUE.
endif
enddo
enddo
enddo
! initial costs with initial parameters
write(message,'(A)')" "; write_msg
write(message,'(A)')" Preparing initial costs" ; write_msg
do iptcl = sum(ourjob(1:groupid)) + 1, sum(ourjob(1:groupid+1))
PPRAM%param(PPRAM%iparam_free(:)) = pos(iptcl, :)
if(.not. flag_with_lmdif) then
call get_dE(fvec, fvec_plain, fvec_orb, ldjac, imode, PINPT, PPRAM, NN_TABLE, EDFT, ETBA, PWGHT, PGEOM, PKPTS, flag_fit_orbital)
costs(iptcl) = enorm(ldjac, fvec)
costs_plain(iptcl) = enorm(ldjac, fvec_plain)
if(flag_fit_orbital) costs_orb(iptcl) = enorm(ldjac, fvec_orb)
elseif(flag_with_lmdif) then
call lmdif(get_eig, NN_TABLE, ldjac, imode, PINPT, PPRAM, PKPTS, PGEOM, EDFT, nparam_free, PWGHT, &
ftol, xtol, gtol, fnorm, fnorm_plain, fnorm_orb, maxfev, epsfcn, factor, info, .FALSE., flag_fit_orbital)
costs(iptcl) = fnorm
costs_plain(iptcl) = fnorm_plain
pos(iptcl, :) = PPRAM%param(PPRAM%iparam_free(:))
if(flag_fit_orbital) costs_orb(iptcl) = fnorm_orb
endif
if(COMM_KOREA%flag_split .and. PINPT%pso_verbose .eq. 1) then
if(iselect_mode .eq. 3) then
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl)
else
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl)
endif
elseif(.not. COMM_KOREA%flag_split .and. PINPT%pso_verbose .eq. 1) then
if(iselect_mode .eq. 3) then
if_main write(6,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl)
else
if_main write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl)
endif
endif
enddo
#ifdef MPI
call MPI_BARRIER(mpi_comm_earth, mpierr)
if(COMM_KOREA%flag_split) then
if(COMM_KOREA%myid .eq. 0 .and. myid .ne. 0) then
call MPI_SEND(costs(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 111, mpi_comm_earth, mpierr)
call MPI_SEND(costs_plain(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 222, mpi_comm_earth, mpierr)
if(flag_fit_orbital) then
call MPI_SEND(costs_orb(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 222, mpi_comm_earth, mpierr)
endif
!if(flag_with_lmdif) then
call MPI_SEND(pos(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1)), :), ourjob(groupid+1)*PPRAM%nparam_free, &
MPI_REAL8, 0, 110, mpi_comm_earth, mpierr)
!endif
call MPI_SEND(vel(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1)), :), ourjob(groupid+1)*PPRAM%nparam_free, &
MPI_REAL8, 0, 110, mpi_comm_earth, mpierr)
elseif( myid .eq. 0 ) then
do i = 1, npar - 1
call MPI_RECV(costs(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 111, mpi_comm_earth, mpistat, mpierr)
call MPI_RECV(costs_plain(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 222, mpi_comm_earth, mpistat, mpierr)
if(flag_fit_orbital) then
call MPI_RECV(costs_orb(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 222, mpi_comm_earth, mpistat, mpierr)
endif
!if(flag_with_lmdif) then
call MPI_RECV(pos(sum(ourjob(1:i))+1:sum(ourjob(1:i+1)),:), ourjob(i+1)*PPRAM%nparam_free, &
MPI_REAL8, COMM_KOREA%group_main(i+1), 110, mpi_comm_earth, mpistat, mpierr)
!endif
call MPI_RECV(vel(sum(ourjob(1:i))+1:sum(ourjob(1:i+1)),:), ourjob(i+1)*PPRAM%nparam_free, &
MPI_REAL8, COMM_KOREA%group_main(i+1), 110, mpi_comm_earth, mpistat, mpierr)
enddo
endif
endif
call MPI_BARRIER(mpi_comm_earth, mpierr)
#endif
! report initial cost
do iptcl = 1, n_particles
if(iselect_mode .eq. 3) then
write(message,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl); write_msg_file
else
write(message,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl); write_msg_file
endif
enddo
! find best particles and costs
if(myid .eq. 0) then
if(iselect_mode .eq. 1) then
min_loc = minloc(costs(:))
elseif(iselect_mode .eq. 2 ) then
min_loc = minloc(costs_plain(:))
elseif(iselect_mode .eq. 3) then
min_loc = minloc(costs_orb(:))
endif
min_id= min_loc(1)
gbest = pos(min_loc(1),:)
cgbest= costs(min_loc(1))
cgbest_plain = costs_plain(min_loc(1))
cgbest_orb = costs_orb(min_loc(1))
cpbest = costs
cpbest_plain = costs_plain
cpbest_orb = costs_orb
pbest = pos
endif
#ifdef MPI
! share cost information with other members
call MPI_BCAST(gbest , size(gbest) , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cgbest , 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cgbest_plain, 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
if(iselect_mode .eq. 3) then
call MPI_BCAST(cgbest_orb, 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
endif
call MPI_BCAST(pbest , size(pbest) , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cpbest , size(cpbest), MPI_REAL8, 0, mpi_comm_earth, mpierr)
#endif
! report best one
if(iselect_mode .eq. 1 .or. iselect_mode .eq. 2) then
write(message,'(A, i5, 2(A, F24.12), A,I0)')" PSO INITIAL RESULT: iter =",0, &
" ,GBest COST = ", cgbest, " ,COST(w/o weight) = ", cgbest_plain, &
" ,Best Particle ID = ", min_id ; write_msg
elseif(iselect_mode .eq. 3) then
write(message,'(A, i5, 3(A, F24.12), A,I0)')" PSO INITIAL RESULT: iter =",0, &
" ,GBest COST = ", cgbest, " ,COST(w/o weight) = ", cgbest_plain, &
" ,COST(orbital) = ", cgbest_orb, &
" ,Best Particle ID = ", min_id ; write_msg
endif
! main PSO loop
write(message,'(A)')" "; write_msg
if(iselect_mode .eq. 2) then
write(message,'(A)')" Main PSO Loop start: minimizing COST w/o WEIGHT" ; write_msg
elseif(iselect_mode .eq. 1) then
write(message,'(A)')" Main PSO Loop start: minimizing COST " ; write_msg
elseif(iselect_mode .eq. 3) then
write(message,'(A)')" Main PSO Loop start: minimizing COST w orbital" ; write_msg
endif
it:do iter = 1, pso_miter
write(message,'(A)')" "; write_msg
write(message,'(A, i5)')" *Iter: ",iter ; write_msg
! update particle velocity and position
do iptcl = 1, n_particles
do iparam = 1, nparam_free
#ifdef MPI
if_main r1 = random()
if_main r2 = random()
call MPI_BCAST(r1, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(r2, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
#else
r1 = random() ; r2 = random()
#endif
vel(iptcl, iparam) = w * vel(iptcl, iparam) + c1 * r1 * (pbest(iptcl, iparam) - pos(iptcl, iparam)) &
+ c2 * r2 * (gbest(iparam) - pos(iptcl, iparam))
enddo
enddo
pos = pos + vel
! get cost for each particle
costs = 0d0 ; costs_plain = 0d0
ptcl:do iptcl = sum(ourjob(1:groupid)) + 1, sum(ourjob(1:groupid+1))
PPRAM%param(PPRAM%iparam_free(:)) = pos(iptcl, :)
if(.not. flag_with_lmdif) then
call get_dE(fvec, fvec_plain, fvec_orb, ldjac, imode, PINPT, PPRAM, NN_TABLE, EDFT, ETBA, PWGHT, PGEOM, PKPTS, flag_fit_orbital)
costs(iptcl) = enorm(ldjac, fvec)
costs_plain(iptcl) = enorm(ldjac, fvec_plain)
if(flag_fit_orbital) costs_orb(iptcl) = enorm(ldjac, fvec_orb)
elseif(flag_with_lmdif) then
call lmdif(get_eig, NN_TABLE, ldjac, imode, PINPT, PPRAM, PKPTS, PGEOM, EDFT, nparam_free, PWGHT, &
ftol, xtol, gtol, fnorm, fnorm_plain, fnorm_orb, maxfev, epsfcn, factor, info, .FALSE., flag_fit_orbital)
costs(iptcl) = fnorm
costs_plain(iptcl) = fnorm_plain
if(flag_fit_orbital) costs_orb(iptcl) = fnorm_orb
pos(iptcl, :) = PPRAM%param(PPRAM%iparam_free(:))
endif
if(COMM_KOREA%flag_split .and. PINPT%pso_verbose .eq. 1) then
if(iselect_mode .eq. 3) then
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl);
else
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl)
endif
elseif(.not. COMM_KOREA%flag_split .and. PINPT%pso_verbose .eq. 1) then
if(iselect_mode .eq. 3) then
if_main write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_plain(iptcl)
else
if_main write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl)
endif
endif
enddo ptcl
#ifdef MPI
! merge costs
call MPI_BARRIER(mpi_comm_earth, mpierr)
if(COMM_KOREA%flag_split) then
if(COMM_KOREA%myid .eq. 0 .and. myid .ne. 0) then
call MPI_SEND(costs(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 333, mpi_comm_earth, mpierr)
call MPI_SEND(costs_plain(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 444, mpi_comm_earth, mpierr)
if(iselect_mode .eq. 3) then
call MPI_SEND(costs_orb(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 444, mpi_comm_earth, mpierr)
endif
call MPI_SEND(pos(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1)),:), ourjob(groupid+1)*PPRAM%nparam_free, &
MPI_REAL8, 0, 330, mpi_comm_earth, mpierr)
call MPI_SEND(vel(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1)),:), ourjob(groupid+1)*PPRAM%nparam_free, &
MPI_REAL8, 0, 330, mpi_comm_earth, mpierr)
elseif( myid .eq. 0 ) then
do i = 1, npar - 1
call MPI_RECV(costs(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 333, mpi_comm_earth, mpistat, mpierr)
call MPI_RECV(costs_plain(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 444, mpi_comm_earth, mpistat, mpierr)
if(iselect_mode .eq. 3) then
call MPI_RECV(costs_orb(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 444, mpi_comm_earth, mpistat, mpierr)
endif
call MPI_RECV(pos(sum(ourjob(1:i))+1:sum(ourjob(1:i+1)),:), ourjob(i+1)*PPRAM%nparam_free, &
MPI_REAL8, COMM_KOREA%group_main(i+1), 330, mpi_comm_earth, mpistat, mpierr)
call MPI_RECV(vel(sum(ourjob(1:i))+1:sum(ourjob(1:i+1)),:), ourjob(i+1)*PPRAM%nparam_free, &
MPI_REAL8, COMM_KOREA%group_main(i+1), 330, mpi_comm_earth, mpistat, mpierr)
enddo
endif
endif
call MPI_BARRIER(mpi_comm_earth, mpierr)
#endif
! report initial cost
do iptcl = 1, n_particles
if(iselect_mode .eq. 3) then
write(message,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl); write_msg_file
else
write(message,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl); write_msg_file
endif
enddo
! update gbest, pbest
if(myid .eq. 0) then
if(iselect_mode .eq. 1) then
min_loc = minloc(costs(:))
if( costs(min_loc(1)) .lt. cgbest) then
cgbest = costs(min_loc(1))
cgbest_plain = costs_plain(min_loc(1))
gbest = pos(min_loc(1),:)
min_id = min_loc(1)
endif
do iptcl = 1, n_particles
if( costs(iptcl) .lt. cpbest(iptcl) ) then
cpbest(iptcl) = costs(iptcl)
cpbest_plain(iptcl) = costs_plain(iptcl)
pbest(iptcl,:) = pos(iptcl,:)
endif
enddo
elseif(iselect_mode .eq. 2) then
min_loc = minloc(costs_plain(:))
if( costs_plain(min_loc(1)) .lt. cgbest_plain) then
cgbest = costs(min_loc(1))
cgbest_plain = costs_plain(min_loc(1))
gbest = pos(min_loc(1),:)
min_id = min_loc(1)
endif
do iptcl = 1, n_particles
if( costs_plain(iptcl) .lt. cpbest_plain(iptcl) ) then
cpbest(iptcl) = costs(iptcl)
cpbest_plain(iptcl) = costs_plain(iptcl)
pbest(iptcl,:) = pos(iptcl,:)
endif
enddo
elseif(iselect_mode .eq. 3) then
min_loc = minloc(costs_orb(:))
if( costs_orb(min_loc(1)) .lt. cgbest_orb) then
cgbest = costs(min_loc(1))
cgbest_plain = costs_plain(min_loc(1))
cgbest_orb = costs_orb(min_loc(1))
gbest = pos(min_loc(1),:)
min_id = min_loc(1)
endif
do iptcl = 1, n_particles
if( costs_orb(iptcl) .lt. cpbest_orb(iptcl) ) then
cpbest(iptcl) = costs(iptcl)
cpbest_plain(iptcl) = costs_plain(iptcl)
cpbest_orb(iptcl) = costs_orb(iptcl)
pbest(iptcl,:) = pos(iptcl,:)
endif
enddo
endif
endif
#ifdef MPI
! share cost information with other members
call MPI_BCAST(gbest , size(gbest) , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cgbest , 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cgbest_plain, 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
if(iselect_mode .eq. 3) then
call MPI_BCAST(cgbest_orb, 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
endif
call MPI_BCAST(pbest , size(pbest) , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cpbest , size(cpbest), MPI_REAL8, 0, mpi_comm_earth, mpierr)
#endif
if(iselect_mode .eq. 1) then
PPRAM%pso_cost_history(iter) = cgbest
PPRAM%pso_cost_history_i(iter,:) = cpbest
elseif(iselect_mode .eq. 2) then
PPRAM%pso_cost_history(iter) = cgbest_plain
PPRAM%pso_cost_history_i(iter,:) = cpbest_plain
elseif(iselect_mode .eq. 3) then
PPRAM%pso_cost_history(iter) = cgbest_orb
PPRAM%pso_cost_history_i(iter,:) = cpbest_orb
endif
PPRAM%pso_pbest_history(iter,:,:) = pbest
PPRAM%niter = iter
if(iselect_mode .eq. 1 .or. iselect_mode .eq. 2) then
write(message,'(A, i5, 2(A, F24.12), A,I0)')" PSO RESULT: iter = ",iter, &
" ,GBest COST = ", cgbest, " ,COST(w/o weight) = ", cgbest_plain, &
" ,Best Particle ID = ", min_id ; write_msg
elseif(iselect_mode .eq. 3) then
write(message,'(A, i5, 3(A, F24.12), A,I0)')" PSO RESULT: iter = ",iter, &
" ,GBest COST = ", cgbest, " ,COST(w/o weight) = ", cgbest_plain, &
" ,COST(orbital) = ", cgbest_orb, &
" ,Best Particle ID = ", min_id ; write_msg
endif
enddo it ! iter
! calculate with final parameters and save costs ETBA%dE
PPRAM%param(PPRAM%iparam_free(:)) = gbest(:)
if(.not. flag_with_lmdif) then
ldjac = 1
call get_dE(fvec, fvec_plain, fvec_orb, ldjac, imode, PINPT, PPRAM, NN_TABLE, EDFT, ETBA, PWGHT, PGEOM, PKPTS, flag_fit_orbital)
elseif(flag_with_lmdif) then
call lmdif(get_eig, NN_TABLE, ldjac, imode, PINPT, PPRAM, PKPTS, PGEOM, EDFT, nparam_free, PWGHT, &
ftol, xtol, gtol, fnorm, fnorm_plain, fnorm_orb, maxfev, epsfcn, factor, info, .FALSE., flag_fit_orbital)
call get_dE12(PINPT, PPRAM, NN_TABLE, EDFT, ETBA, PWGHT, PGEOM, PKPTS, flag_fit_orbital)
endif
#ifdef MPI
call MPI_BARRIER(mpi_comm_earth, mpierr)
COMM_KOREA%flag_split = .FALSE.
#endif
return
endsubroutine
subroutine pso_fit_best (PINPT, PPRAM, PKPTS, PWGHT, PGEOM, NN_TABLE, EDFT, iseed_, pso_miter)
use mykind
use mpi_setup
use parameters, only: incar, params, kpoints, weight, poscar, hopping, energy
use cost_function, only: get_dE, get_cost_function, get_dE12
use print_io
use random_mod
use sorting
implicit none
type(incar ) :: PINPT ! only INCAR from first system is used
type(params ) :: PPRAM ! asume each system shares same parameter
type(kpoints ), dimension(PINPT%nsystem) :: PKPTS
type(weight ), dimension(PINPT%nsystem) :: PWGHT
type(poscar ), dimension(PINPT%nsystem) :: PGEOM
type(hopping ), dimension(PINPT%nsystem) :: NN_TABLE
type(energy ), dimension(PINPT%nsystem) :: EDFT, ETBA
logical flag_stat
integer(kind=sp) n_particles, nparam_free
integer(kind=sp) iparam, iter, iptcl
real(kind=dp), allocatable :: fvec(:), fvec_plain(:), fvec_orb(:)
real(kind=dp), allocatable :: vel(:,:), pos(:,:), pos_(:,:)
real(kind=dp), allocatable :: pbest(:,:), gbest(:)
real(kind=dp), allocatable :: pbest_history(:,:,:)
real(kind=dp), allocatable :: cpbest(:), cpbest_plain(:), cpbest_orb(:)
real(kind=dp) :: cgbest, cgbest_plain, cost
real(kind=dp) :: cgbest_orb
real(kind=dp), allocatable :: costs(:), costs_plain(:), costs_orb(:)
real(kind=dp), allocatable :: costs_(:), costs_plain_(:), costs_orb_(:)
integer(kind=sp), allocatable :: icosts(:) ! ordering of the costs
integer(kind=sp) i, min_id
integer(kind=sp) imode, ldjac
integer(kind=sp) min_loc(1), pso_miter
logical flag_order, flag_go
real(kind=dp) c1, c2, w, r1, r2, r3
integer(kind=sp) iseed, iseed_
integer(kind=sp) mpierr
integer(kind=sp) iselect_mode
real(kind=dp), external :: enorm
real(kind=dp) fnorm, fnorm_plain, fnorm_orb
real(kind=dp) pmax, pmin
external get_eig
integer(kind=sp) info, maxfev, bestn
real(kind=dp) epsfcn, factor, xtol, ftol, gtol
logical flag_with_lmdif
logical flag_fit_plain, flag_fit_orbital
integer(kind=sp), allocatable :: ourgroup(:), ourjob(:)
integer(kind=sp) mygroup(0:nprocs-1), groupid
#ifdef MPI
integer(kind=sp) mpistat(MPI_STATUS_SIZE)
#endif
!PPRAM%flag_fit_plain = .TRUE.
flag_fit_orbital= PINPT%flag_fit_orbital
flag_fit_plain = PPRAM%flag_fit_plain
flag_with_lmdif = PINPT%flag_pso_with_lmdif
flag_order = .FALSE.
imode = 13
n_particles = PPRAM%pso_nparticles
nparam_free = PPRAM%nparam_free
c1 = PPRAM%pso_c1
c2 = PPRAM%pso_c2
w = PPRAM%pso_w
iseed = iseed_
r1 = 1d0
r2 = 1d0
r3 = 0d0
bestn = int(real(PPRAM%pso_nparticles) * PPRAM%pso_report_ratio)
call random_init(iseed)
if(flag_fit_orbital .and. flag_fit_plain) then
flag_fit_plain = .FALSE. ! we consider fit_orbital instead of fit_plain
endif
if(flag_fit_orbital .and. .not. flag_fit_plain) then
iselect_mode = 3
elseif(flag_fit_plain .and. .not. flag_fit_orbital) then
iselect_mode = 2
elseif(flag_fit_orbital .and. flag_fit_plain) then
iselect_mode = 4
elseif(.not. flag_fit_plain .and. .not. flag_fit_orbital) then
iselect_mode = 1
endif
#ifdef MPI
call MPI_BARRIER(mpi_comm_earth, mpierr)
!Note: n_particles will be solved in n_groups of cpu family.
! Each group will have n_member ~ nprocs/ngroup
! The n_member will be further parallized to solve eigenvalue problem in get_eig routine
call get_npar(trim(PINPT%ifilenm(1)))
allocate(ourgroup(npar)); allocate(ourjob(npar))
call mpi_job_distribution_group(npar, n_particles, ourgroup, mygroup, ourjob)
call mpi_comm_anmeldung(COMM_KOREA, npar, mygroup)
groupid = COMM_KOREA%color
write(message,'(A,I0,A)') ' JOB DISTRUBUTION for PSO particles over NPAR (NPAR=',npar,'):' ; write_msg
do i = 1, npar
write(message,'(A,3(I0,A))') ' -> Group_id(',i-1,'): ',ourgroup(i), &
' cpus asigned and ',ourjob(i),' jobs are distributed' ; write_msg
enddo
call MPI_BARRIER(mpi_comm_earth, mpierr)
#else
allocate(ourjob(1))
ourjob = n_particles
groupid= 0
#endif
factor = 100.0D+00
maxfev = PINPT%miter * ( PPRAM%nparam_free + 1 )
ftol = PINPT%ftol ;xtol = PINPT%ptol ; gtol = 0.0D+00; epsfcn = 0.000D+00
if(bestn .lt. npar) bestn = npar
if(bestn .gt. PPRAM%pso_nparticles) bestn = PPRAM%pso_nparticles
if(allocated(PPRAM%pso_cost_history)) deallocate(PPRAM%pso_cost_history)
if(allocated(PPRAM%pso_cost_history_i)) deallocate(PPRAM%pso_cost_history_i)
allocate(PPRAM%pso_cost_history(pso_miter))
allocate(PPRAM%pso_cost_history_i(pso_miter,n_particles))
PPRAM%pso_cost_history = 0d0
PPRAM%pso_cost_history_i = 0d0
do i = 1, PINPT%nsystem
call allocate_ETBA(PGEOM(i), PINPT, PKPTS(i), ETBA(i), .FALSE., 0)
if(allocated(ETBA(i)%dE)) deallocate(ETBA(i)%dE)
allocate(ETBA(i)%dE( size(ETBA(i)%E(:,1)) , size(ETBA(i)%E(1,:)) ))
ETBA(i)%E = 0d0 ; ETBA(i)%V = 0d0 ; ETBA(i)%SV = 0d0 ; ETBA(i)%dE=0d0
if(flag_fit_orbital) then
if(allocated(ETBA(i)%dORB)) deallocate(ETBA(i)%dORB)
allocate(ETBA(i)%dORB( size(ETBA(i)%E(:,1)),size(ETBA(i)%E(1,:)) ))
endif
enddo
allocate(vel( n_particles, nparam_free))
allocate(pos( n_particles, nparam_free))
allocate(pbest(n_particles, nparam_free))
if(allocated(PPRAM%pso_pbest_history)) deallocate(PPRAM%pso_pbest_history)
allocate(PPRAM%pso_pbest_history(pso_miter, n_particles, nparam_free))
allocate(gbest( nparam_free))
allocate(cpbest(n_particles ))
allocate(cpbest_plain(n_particles ))
allocate(cpbest_orb(n_particles ))
allocate(costs(n_particles ))
allocate(costs_plain(n_particles ))
allocate(costs_orb(n_particles ))
allocate(costs_(n_particles ))
allocate(costs_plain_(n_particles ))
allocate(costs_orb_(n_particles ))
allocate(icosts(n_particles ))
costs = 0.d0 ; costs_plain = 0.d0 ; costs_ = 0.d0 ; costs_plain_ = 0.d0
costs_orb = 0d0 ; costs_orb_ = 0d0
vel = 0.d0 ; pos = 0.d0
pbest = 0.d0 ; gbest = 0.d0
cpbest = 0.d0 ; cpbest_plain = 0.d0 ; cpbest_orb = 0d0
cgbest = 0.d0 ; cgbest_plain = 0.d0 ; cgbest_orb = 0d0
PPRAM%pso_pbest_history = 0d0
ldjac = 0
do i = 1, PINPT%nsystem
ldjac = ldjac + PKPTS(i)%nkpoint * PGEOM(i)%nband * PINPT%nspin
enddo
if(ldjac .ne. 1) then
allocate(fvec(ldjac))
allocate(fvec_plain(ldjac))
allocate(fvec_orb(ldjac))
endif
fvec = 0d0 ; fvec_plain = 0d0
if(flag_fit_orbital) fvec_orb = 0d0
! initialize parameters with random noise
do iptcl = 1, n_particles
do iparam = 1, nparam_free
flag_go = .FALSE.
do while (.not. flag_go)
#ifdef MPI
if_main r1 = (random()*2d0 - 1d0) * PPRAM%pso_max_noise_amplitude
if_main r2 = (random()*2d0 - 1d0) + 1d-10 ! sign
if_main r2 = r2 / abs(r2)
call MPI_BCAST(r1, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(r2, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
! NOTE: all processors share random number over "iptcl" loop
! so that pos and vel update is to be mpi_comm_earth wide
! => up to this point, every processor knows vel and pos for all particles
#else
r1 = (random()*2d0 - 1d0) * 5d0
r2 = (random()*2d0 - 1d0) + 1d-10 ! sign
r2 = r2 / abs(r2)
#endif
if(iptcl .eq. 1) then ! use initial parameter without noise for particle-1 only
r1 = 0d0
r2 = 1d0
endif
if( PPRAM%param( PPRAM%iparam_free(iparam) ) .ge. PPRAM%param_const(2, PPRAM%iparam_free(iparam) ) .or. &
PPRAM%param( PPRAM%iparam_free(iparam) ) .le. PPRAM%param_const(3, PPRAM%iparam_free(iparam) )) then
pmax = PPRAM%param_const(2, PPRAM%iparam_free(iparam) )
pmin = PPRAM%param_const(3, PPRAM%iparam_free(iparam) )
if(PPRAM%iparam_type( PPRAM%iparam_free(iparam) ) .le. 2) then
pos(iptcl, iparam) = (pmax+pmin)*0.5d0*r2 + r1
else ! overlap
pos(iptcl, iparam) = (pmax+pmin)*0.5d0*r2
endif
else
if(PPRAM%iparam_type( PPRAM%iparam_free(iparam) ) .le. 2) then
pos(iptcl, iparam) = PPRAM%param( PPRAM%iparam_free(iparam) )*r2 + r1
else ! overlap
pos(iptcl, iparam) = PPRAM%param( PPRAM%iparam_free(iparam) )*r2 !+ r1
endif
endif
vel(iptcl, iparam) = r1
if( pos(iptcl, iparam) .ge. PPRAM%param_const(2, PPRAM%iparam_free(iparam) ) .or. &
pos(iptcl, iparam) .le. PPRAM%param_const(3, PPRAM%iparam_free(iparam) )) then
flag_go = .FALSE.
else
flag_go = .TRUE.
endif
enddo
enddo
enddo
! initial costs with initial parameters
write(message,'(A)')" "; write_msg
write(message,'(A)')" Preparing initial costs" ; write_msg
do iptcl = sum(ourjob(1:groupid)) + 1, sum(ourjob(1:groupid+1))
PPRAM%param(PPRAM%iparam_free(:)) = pos(iptcl, :)
if(.not. flag_with_lmdif) then
call get_dE(fvec, fvec_plain, fvec_orb, ldjac, imode, PINPT, PPRAM, NN_TABLE, EDFT, ETBA, PWGHT, PGEOM, PKPTS, flag_fit_orbital)
costs(iptcl) = enorm(ldjac, fvec)
costs_plain(iptcl) = enorm(ldjac, fvec_plain)
if(flag_fit_orbital) costs_orb(iptcl) = enorm(ldjac, fvec_orb)
elseif(flag_with_lmdif) then
call lmdif(get_eig, NN_TABLE, ldjac, imode, PINPT, PPRAM, PKPTS, PGEOM, EDFT, nparam_free, PWGHT, &
ftol, xtol, gtol, fnorm, fnorm_plain, fnorm_orb, maxfev, epsfcn, factor, info, .FALSE., flag_fit_orbital)
costs(iptcl) = fnorm
costs_plain(iptcl) = fnorm_plain
pos(iptcl, :) = PPRAM%param(PPRAM%iparam_free(:))
if(flag_fit_orbital) costs_orb(iptcl) = fnorm_orb
endif
if(COMM_KOREA%flag_split .and. PINPT%pso_verbose .eq. 1) then
if(iselect_mode .ge. 3) then
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl)
else
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl)
endif
elseif(.not. COMM_KOREA%flag_split .and. PINPT%pso_verbose .eq. 1) then
if(iselect_mode .ge. 3) then
if_main write(6,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl)
else
if_main write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl)
endif
endif
enddo
#ifdef MPI
call MPI_BARRIER(mpi_comm_earth, mpierr)
if(COMM_KOREA%flag_split) then
if(COMM_KOREA%myid .eq. 0 .and. myid .ne. 0) then
call MPI_SEND(costs(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 111, mpi_comm_earth, mpierr)
call MPI_SEND(costs_plain(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 222, mpi_comm_earth, mpierr)
if(flag_fit_orbital) then
call MPI_SEND(costs_orb(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1))), ourjob(groupid+1), &
MPI_REAL8, 0, 222, mpi_comm_earth, mpierr)
endif
!if(flag_with_lmdif) then
call MPI_SEND(pos(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1)), :), ourjob(groupid+1)*PPRAM%nparam_free, &
MPI_REAL8, 0, 110, mpi_comm_earth, mpierr)
!endif
call MPI_SEND(vel(sum(ourjob(1:groupid))+1:sum(ourjob(1:groupid+1)), :), ourjob(groupid+1)*PPRAM%nparam_free, &
MPI_REAL8, 0, 110, mpi_comm_earth, mpierr)
elseif( myid .eq. 0 ) then
do i = 1, npar - 1
call MPI_RECV(costs(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 111, mpi_comm_earth, mpistat, mpierr)
call MPI_RECV(costs_plain(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 222, mpi_comm_earth, mpistat, mpierr)
if(flag_fit_orbital) then
call MPI_RECV(costs_orb(sum(ourjob(1:i))+1:sum(ourjob(1:i+1))), ourjob(i+1), &
MPI_REAL8, COMM_KOREA%group_main(i+1), 222, mpi_comm_earth, mpistat, mpierr)
endif
!if(flag_with_lmdif) then
call MPI_RECV(pos(sum(ourjob(1:i))+1:sum(ourjob(1:i+1)),:), ourjob(i+1)*PPRAM%nparam_free, &
MPI_REAL8, COMM_KOREA%group_main(i+1), 110, mpi_comm_earth, mpistat, mpierr)
!endif
call MPI_RECV(vel(sum(ourjob(1:i))+1:sum(ourjob(1:i+1)),:), ourjob(i+1)*PPRAM%nparam_free, &
MPI_REAL8, COMM_KOREA%group_main(i+1), 110, mpi_comm_earth, mpistat, mpierr)
enddo
endif
endif
call MPI_BARRIER(mpi_comm_earth, mpierr)
#endif
! report initial cost
do iptcl = 1, n_particles
if(iselect_mode .ge. 3) then
write(message,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital ) = ", costs_orb(iptcl); write_msg_file
else
write(message,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl); write_msg_file
endif
enddo
! sort costs and pick bestn particles only and save
call get_bestn_particles(bestn, iselect_mode, n_particles, nparam_free, pos, vel, costs, costs_plain, costs_orb, icosts)
! report sorted cost
write(message,'(A)')" "; write_msg
do iptcl = 1, bestn
if(iselect_mode .ge. 3) then
write(message,'(A, i5, 3(A, F20.8), A, I0)')" Best PTCL: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital )*= ", costs_orb(iptcl), &
", Particle ID = ", icosts(iptcl) ; write_msg
elseif(iselect_mode .eq. 2) then
write(message,'(A, i5, 2(A, F20.8), A, I0)')" Best PTCL: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
", Particle ID = ", icosts(iptcl) ; write_msg
elseif(iselect_mode .eq. 1) then
write(message,'(A, i5, 2(A, F20.8), A, I0)')" Best PTCL: ",iptcl, " COST*= ", costs(iptcl), " ,COST(w/o weight)*= ", costs_plain(iptcl), &
", Particle ID = ", icosts(iptcl) ; write_msg
endif
enddo
! find best particles and costs
if(myid .eq. 0) then
if(iselect_mode .eq. 1) then
min_loc = minloc(costs(:))
elseif(iselect_mode .eq. 2 ) then
min_loc = minloc(costs_plain(:))
elseif(iselect_mode .eq. 3) then
min_loc = minloc(costs_orb(:))
endif
min_id= min_loc(1)
gbest = pos(min_loc(1),:)
cgbest= costs(min_loc(1))
cgbest_plain = costs_plain(min_loc(1))
cgbest_orb = costs_orb(min_loc(1))
cpbest = costs
cpbest_plain = costs_plain
cpbest_orb = costs_orb
pbest = pos
endif
#ifdef MPI
! share cost information with other members
call MPI_BCAST(gbest , size(gbest) , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cgbest , 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cgbest_plain, 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
if(iselect_mode .ge. 3) then
call MPI_BCAST(cgbest_orb, 1 , MPI_REAL8, 0, mpi_comm_earth, mpierr)
endif
call MPI_BCAST(pbest , size(pbest) , MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(cpbest , size(cpbest), MPI_REAL8, 0, mpi_comm_earth, mpierr)
#endif
! report best one
if(iselect_mode .eq. 1) then
write(message,'(A, i5, 2(A, F24.12), A,I0)')" PSO INITIAL RESULT: iter = ",0, &
" ,GBest COST*= ", cgbest, " ,COST(w/o weight) = ", cgbest_plain, &
" ,Particle ID = ", icosts(1) ; write_msg
elseif(iselect_mode .eq. 2) then
write(message,'(A, i5, 2(A, F24.12), A,I0)')" PSO INITIAL RESULT: iter = ",0, &
" ,GBest COST = ", cgbest, " ,COST(w/o weight)*= ", cgbest_plain, &
" ,Particle ID = ", icosts(1) ; write_msg
elseif(iselect_mode .ge. 3) then
write(message,'(A, i5, 3(A, F24.12), A,I0)')" PSO INITIAL RESULT: iter = ",0, &
" ,GBest COST = ", cgbest, " ,COST(w/o weight) = ", cgbest_plain, &
" ,COST(orbital)*= ", cgbest_orb, &
" ,Particle ID = ", icosts(1) ; write_msg
endif
!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!! main PSO loop !!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!
write(message,'(A)')" "; write_msg
if(iselect_mode .eq. 2) then
write(message,'(A)')" Main PSO Loop start: minimizing COST w/o WEIGHT" ; write_msg
elseif(iselect_mode .eq. 1) then
write(message,'(A)')" Main PSO Loop start: minimizing COST " ; write_msg
elseif(iselect_mode .eq. 3) then
write(message,'(A)')" Main PSO Loop start: minimizing COST w orbital" ; write_msg
elseif(iselect_mode .eq. 4) then
write(message,'(A)')" Main PSO Loop start: minimizing COST w/o WEIGHT and w orbital" ; write_msg
endif
it:do iter = 1, pso_miter
write(message,'(A)')" "; write_msg
write(message,'(A, i5)')" *Iter: ",iter ; write_msg
! update particle velocity and position
do iptcl = 1, n_particles
do iparam = 1, nparam_free
#ifdef MPI
if_main r1 = random()
if_main r2 = random()
if_main r3 = (random()*2d0 - 1d0) * PPRAM%pso_max_noise_amplitude
call MPI_BCAST(r1, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(r2, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
call MPI_BCAST(r3, 1, MPI_REAL8, 0, mpi_comm_earth, mpierr)
#else
r1 = random() ; r2 = random() ; r3 = (random()*2d0 - 1d0) * PPRAM%pso_max_noise_amplitude
#endif
if(PPRAM%iparam_type( PPRAM%iparam_free(iparam) ) .le. 2) then
vel(iptcl, iparam) = w * vel(iptcl, iparam) + c1 * r1 * (pbest(iptcl, iparam) - pos(iptcl, iparam)) &
+ c2 * r2 * (gbest(iparam) - pos(iptcl, iparam)) + r3*0.3d0
else
vel(iptcl, iparam) = w * vel(iptcl, iparam) + c1 * r1 * (pbest(iptcl, iparam) - pos(iptcl, iparam)) &
+ c2 * r2 * (gbest(iparam) - pos(iptcl, iparam)) !+ r3*0.3d0
endif
enddo
enddo
pos = pos + vel
! get cost for each particle
costs = 0d0 ; costs_plain = 0d0
ptcl:do iptcl = sum(ourjob(1:groupid)) + 1, sum(ourjob(1:groupid+1))
PPRAM%param(PPRAM%iparam_free(:)) = pos(iptcl, :)
if(.not. flag_with_lmdif) then
call get_dE(fvec, fvec_plain, fvec_orb, ldjac, imode, PINPT, PPRAM, NN_TABLE, EDFT, ETBA, PWGHT, PGEOM, PKPTS, flag_fit_orbital)
costs(iptcl) = enorm(ldjac, fvec)
costs_plain(iptcl) = enorm(ldjac, fvec_plain)
if(flag_fit_orbital) costs_orb(iptcl) = enorm(ldjac, fvec_orb)
elseif(flag_with_lmdif) then
call lmdif(get_eig, NN_TABLE, ldjac, imode, PINPT, PPRAM, PKPTS, PGEOM, EDFT, nparam_free, PWGHT, &
ftol, xtol, gtol, fnorm, fnorm_plain, fnorm_orb, maxfev, epsfcn, factor, info, .FALSE., flag_fit_orbital)
costs(iptcl) = fnorm
costs_plain(iptcl) = fnorm_plain
if(flag_fit_orbital) costs_orb(iptcl) = fnorm_orb
pos(iptcl, :) = PPRAM%param(PPRAM%iparam_free(:))
endif
if(COMM_KOREA%flag_split .and. PINPT%pso_verbose .eq. 1) then
if(iselect_mode .ge. 3) then
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 3(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl), &
" ,COST(orbital )*= ", costs_orb(iptcl);
elseif(iselect_mode .eq. 2) then
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST = ", costs(iptcl), " ,COST(w/o weight)*= ", costs_plain(iptcl)
elseif(iselect_mode .eq. 1) then
if(COMM_KOREA%myid .eq. 0) write(6,'(A, i5, 2(A, F20.8))')" Particle: ",iptcl, " COST*= ", costs(iptcl), " ,COST(w/o weight) = ", costs_plain(iptcl)