forked from alistairboyce11/RJ_MCMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RJ_MCMC_test_nlay.f90
2557 lines (2163 loc) · 99.4 KB
/
RJ_MCMC_test_nlay.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
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! This program does a Transdimensional inversion of Surface wave dispersion curves
! with the reversible jump algorithm
! Thomas Bodin, ANU, December 2011
!*********************************************************************
!/////////////////////////////////////////////////////////////////////
!********************************************************************
program RJ_MCMC
use mpi
implicit none
! include 'mpif.h'
include 'params.h'
include 'params_p.h'
! ----------------------------------
! BEGINING OF THE USER-DEFINED PARAMETERS
! ----------------------------------
!-----------------------------------------
! Parameters of the Markov chain
!-----------------------------------------
! 1 Constrain using PsPp data, 0 leave it out.
integer, parameter :: PsPp_flag = 0
! 0 let Vp Free, 1 Scale Vp, 2 Fix Vp to ref.
integer, parameter :: vp_flag = 0
! scaling parameter for vph in voro when vp_flag=1
! dvp=dvs*vp_scale, when vp_scale<=1.0
! For S40RTS scaling dvp = dvs * 1.0/(depth/2891.0+2.0), when vp_scale>1.0
real, parameter :: vp_scale = 2.0
character (len=*), parameter :: mods_dirname = 'MODS_OUT_TEST' ! This is where output info files are saved and input data files are taken.
real, parameter :: noise_dspd = 0.01 ! Gaussian noise added to disp data (>0). twice for Love.
real, parameter :: obs_err_R = 0.04 ! Observational errors - obs_err_R*Ad_R=noise_dspd_R (sigma_R)
real, parameter :: obs_err_L = 0.04 ! Observational errors - obs_err_L*Ad_L=noise_dspd_L (sigma_L)
character(len=64), parameter :: rp_file = 'rayp2.in'
real, parameter :: noise_PsPp = 0.05 ! Gaussian noise added to travel time data (>0)
real, parameter :: obs_err_PsPp = 0.2 ! Observational errors - obs_err_PsPp*Ad_PsPp=noise_PsPp (sigma_PsPp)
character (len=*), parameter :: dirname = 'OUT_TEST' ! This is where output info files are saved and input data files are taken.
character*8, parameter :: storename = 'STORFFC1' ! This is where output models are saved
integer, parameter :: burn_in = 200000 ! 55000 !Burn-in period
integer, parameter :: nsample = 200000 ! 50000!Post burn-in
integer, parameter :: thin = 50 !Thinning of the chain
integer, parameter :: Scratch = 1 ! 0: Start from Stored model 1: Start from scratch
integer, parameter :: store = 999999999 !Store models every "store" iteration.
! Each chain is run for 'burn_in + nsample' steps in total. The first burn-in samples are discarded as burn-in steps, only after which the sampling algorithm is assumed to have converged. To eliminate dependent samples in the ensemble solution, every thin model visited in the second part is selected for the ensemble. The convergence of the algorithm is monitored with a number of indicators such as acceptance rates, and sampling efficiency is optimized by adjusting the variance of the Gaussian proposal functions
!------------------------------------------------
! Prior distribution (Bounds of the model space)
!------------------------------------------------
!depth
real, parameter :: d_min = 0 ! depth bounds
real, parameter :: d_max = 700
real, parameter :: width = 0.4 ! width of the prior in vsv
real, parameter :: vp_min = -0.4
real, parameter :: vp_max = 0.4 ! bounds of the prior in vp
real, parameter :: xi_min = 0.6 ! bounds of the prior in xi
real, parameter :: xi_max = 1.4
double precision, parameter :: Ad_R_max = 25 ! bounds of the prior in Ad_R - the error parameter for rayleigh wave velocity
double precision, parameter :: Ad_R_min = 0.0000002
double precision, parameter :: Ad_L_max = 25 ! bounds of the prior in Ad_L
double precision, parameter :: Ad_L_min = 0.0000002
!!!!!!!!!!!!!!! AL mods for P-wave stuff !!!!!!!!!!!!!!!!!
double precision, parameter :: Ad_PsPp_max = 25 ! bounds of the prior in Ad_PsPp
double precision, parameter :: Ad_PsPp_min = 0.0000002
!!!!!!!!!!!!!!! Add a prior for starting models close to PREM !!!!!!!!!!!!!!!!!
real, parameter :: sp_width = 0.4 ! width of the prior in vsv
real, parameter :: sp_vp_min = -0.4
real, parameter :: sp_vp_max = 0.4 ! bounds of the prior in vp
real, parameter :: sp_xi_min = 0.6 ! bounds of the prior in xi
real, parameter :: sp_xi_max = 1.4
real, parameter :: sp_malay = 80
double precision, parameter :: sp_Ad_R_max = 25 ! bounds of the prior in Ad_R - the error parameter for rayleigh wave velocity
double precision, parameter :: sp_Ad_L_max = 25 ! bounds of the prior in Ad_L
double precision, parameter :: sp_Ad_PsPp_max = 25 ! bounds of the prior in Ad_PsPp
!-----------------------------------------
! Sdt for Proposal distributions
!-----------------------------------------
! The convergence of the algorithm is monitored with a number of indicators such as
! acceptance rates, and sampling efficiency is optimized by adjusting the variance of
! the Gaussian proposal functions
! These values have to be "tuned" so the acceptance rates written in OUT/mpi.out
! are as close as possible to 44%. This determines the efficiency of posterior sampling. ! If AR larger than 44%, increase the Sdt for less Acceptance.
! If AR_* smaller than 44%, decrease the Sdt for more
real, parameter :: perturb = 0.35 ! standard deviation (I guess)
integer, parameter :: every = 1001 ! we do something every 'every' ?? This is a 'KICK' to get it moving.
integer, parameter :: switch_sigma = 10 ! swap between sigma_vsv, sigma_vp optimisation every X iterations
!--------------------------------------------
! Parameters for Displaying results
!--------------------------------------------
integer, parameter :: display = 1000 ! display results in OUT/mpi.out
!every display samples
!discretezation for the posterior distribution.
!specifies the number of velocity pixels in (Vs, depth)
!This is because the solution is visualized as an histogram,
!and hence we need to define the number of bins
integer, parameter :: disd = 200 !depth
integer, parameter :: disv = 100 !velocity/anisotropy
integer, parameter :: disA = 200 !for noise parameter
!depth of model for display
real, parameter :: prof = d_max
!parameters for minos
real, parameter :: eps=1e-3 !precision of runge-kutta, as high as possible for speed
real, parameter :: wgrav=1 !minimal frequency for gravitational effects, very low for speed
integer, parameter :: lmin=1 !min and max mode numbers (constrained by periods anyway)
integer, parameter :: lmax=6000
integer, parameter :: nmodes_max=10000 !max number of modes
integer, parameter :: nharmo_max=6 !max number of harmonics
!***************************************************************
! DECLARATIONS OF VARIABLES
!****************************************************************
real , EXTERNAL :: gasdev,ran3,interp, vp_scale_S40 ! Functions for random variables and interpolation.
real log, sqrt
integer i,ii,sample,ind,th,ount,k ! various indices
integer nlims_cur,nlims_cur_diff
logical ier, error_flag ! error flags for dispersion curves and minos_bran
integer npt,npt_prop,npt_iso,npt_ani ! numbers of points, isotropic, anisotropic
logical accept,tes,birth,birtha,death,deatha,move,value,noisd_R,noisd_L,ani,change_vp !which parameter did we change?
logical testing
integer ind2,ind_vp,ind_vsv,ind_xi,j !more indices, mostly for posterior. j sometimes is the number of processors, careful at the end!
real d !for calculating posterior, mostly depth
real histo(maxlay),histos(maxlay),histoch(disd),histochs(disd) ! histo of layers, changepoints
real avvs(disd),avvp(disd),avxi(disd),avxis(disd),avvss(disd),avvps(disd) ! average of posterior
real peri_R(ndatadmax),peri_L(ndatadmax) !periods of data
real peri_R_tmp(ndatadmax),peri_L_tmp(ndatadmax) !periods of data
real postvp(disd,disv),postvs(disd,disv),postxi(disd,disv),postvps(disd,disv),postvss(disd,disv),postxis(disd,disv) ! posteriors
real probani(disd),probanis(disd) !anisotropy probabilities
integer n_R(ndatadmax),n_L(ndatadmax),nmax_R,nmax_L,nmin_R,nmin_L !harmonic number of modes
integer n_R_tmp(ndatadmax),n_L_tmp(ndatadmax)
integer nlims_R(2,nharmo_max),nlims_L(2,nharmo_max),numharm_count,numharm_R(nharmo_max),numharm_L(nharmo_max)
real PrB,AcB,PrD,AcD,Prnd_R,Acnd_R,Prnd_L,Acnd_L,&
Acba,Prba,Acda,Prda,out,Prxi,Acxi,Pr_vp,&
Ac_vp !,PrP(2),PrV(2),AcP(2),AcV(2) ! to adjust acceptance rates for all different parameters
real PrP,PrV,AcP,AcV
real lsd_L,lsd_L_prop,lsd_L_min,lsd_R,lsd_R_prop,lsd_R_min !logprob without uncertainties
real logprob_vsv,logprob_vp !for reversible jump
real convAd_R(nsample+burn_in),convAd_Rs(nsample+burn_in),convAd_L(nsample+burn_in),convAd_Ls(nsample+burn_in) !variations of uncertainty parameters
real voro(malay,4),vsref_min,vsref_max,vpref_min,vpref_max !the model with its bounds
real voro_prop(malay,4),ncell(nsample+burn_in),convd_R(nsample+burn_in),convd_L(nsample+burn_in),&
convd_Rs(nsample+burn_in),convd_Ls(nsample+burn_in) ! the proposed model, history of lsd
real ncells(nsample+burn_in),t1,t2 !timers, history of ls
real like,like_prop,u,& !log-likelihoods
liked_R_prop,liked_R,liked_L_prop,liked_L
real ML_Ad_R(disA),ML_Ad_Rs(disA),TRA(malay,malay+1),& !histogram of uncertainty parameters
TRAs(malay,malay+1),ML_Ad_L(disA),ML_Ad_Ls(disA)
double precision logrsig,Ad_R,Ad_R_prop,Ad_L,Ad_L_prop !uncertainty parameters
real sigmav,sigmav_old,sigmav_new,AR_birth_old !proposal on velocity when Birth move
real sigmavp,sigmavp_old,sigmavp_new !proposal on vp when Birth move
integer sigma_count ! Count Sigmav & sigmavp perturbations
real d_obsdcR(ndatadmax),d_obsdcL(ndatadmax),d_obsdcRe(ndatadmax),d_obsdcLe(ndatadmax) !observed data
real inorout(21000),inorouts(21000)
integer members(21000),io ! mpi related variables
integer ndatad_R,ndatad_L,ndatad_R_tmp,ndatad_L_tmp !number of observed data points
real pxi,p_vp,pd1,pv1,pd2,pv2,pAd_R,pAd_L ! related to observed data for azimuthal anisotropy
! Geometry parameters
! Traces
integer nptref,malayv ! number of points in reference model, number of layers in voro
real convBs(nsample+burn_in),convB(nsample+burn_in),convPs(nsample+burn_in),convP(nsample+burn_in) ! birth rates, vp change rates
real convvs1(nsample+burn_in),convvs1s(nsample+burn_in),convvs2(nsample+burn_in),convvs2s(nsample+burn_in)
real convdp1(nsample+burn_in),convdp1s(nsample+burn_in),convdp2(nsample+burn_in),convdp2s(nsample+burn_in)
real convvp(nsample+burn_in),convvps(nsample+burn_in),convxis(nsample+burn_in),convxi(nsample+burn_in)
real convBas(nsample+burn_in),convBa(nsample+burn_in) ! anisotropic birth rates
real convDs(nsample+burn_in),convD(nsample+burn_in),convDas(nsample+burn_in),convDa(nsample+burn_in)
logical isoflag(malay),isoflag_prop(malay) ! is this layer isotropic?
real d_cR(ndatadmax),d_cL(ndatadmax) ! phase velocity as simulated by forward modelling
real d_cR_tmp(ndatadmax),d_cL_tmp(ndatadmax)
real rq_R(ndatadmax),rq_L(ndatadmax) ! errors from modelling
real d_cR_prop(ndatadmax),d_cL_prop(ndatadmax) ! phase velocity as simulated by forward modelling
real d_cR_prop_tmp(ndatadmax),d_cL_prop_tmp(ndatadmax) ! phase velocity as simulated by forward modelling
real rq_R_prop(ndatadmax),rq_L_prop(ndatadmax)
!for MPI
integer ra,ran,rank, nbproc, ierror ,status(MPI_STATUS_SIZE),group_world,good_group,MPI_COMM_small,flag
! ierror: MPI-related error status, nbproc: MPI-related, number of processes, rank, group_world: MPI-related
character filename*13, number*4
real likemax ! log-likelihood of best model
integer nptmax ! number of layers of the best model
real voromax(malay,4) ! best model
character filenamemax*30 !filename for writing the best model
real d_cRmoy(ndatadmax), d_cRmoys(ndatadmax), d_cLmoy(ndatadmax), d_cLmoys(ndatadmax) ! average dispersion curve
real d_cRdelta(ndatadmax), d_cRdeltas(ndatadmax), d_cLdelta(ndatadmax), d_cLdeltas(ndatadmax) ! standart deviation of dispersion curve
real model_ref(mk,9) ! reference model, all models are deviations from it
real,dimension(mk) :: r,rho,vpv,vph,vsv,vsh,qkappa,qshear,eta,xi,vp_data,vs_data ! input for forward modelling
integer :: nptfinal,nic,noc,nic_ref,noc_ref,jcom ! more inputs
integer :: nmodes,n_mode(nmodes_max),l_mode(nmodes_max) ! outputs of forward modelling
real :: c_ph(nmodes_max),period(nmodes_max),raylquo(nmodes_max),tref ! more outputs, rayquo: error measurement, should be of the order of eps
real :: wmin_R(nharmo_max),wmax_R(nharmo_max),wmin_L(nharmo_max),wmax_L(nharmo_max) ! more inputs for forward modelling
integer :: recalculated !counts the number of times we need to improve eps
logical :: stuck
integer :: nharm_R,nharm_L,iharm
real :: dummy_d_obsdcR(ndatadmax), dummy_d_obsdcL(ndatadmax)
!!!!!!!!!!!!!!! AL mods for P-wave stuff !!!!!!!!!!!!!!!!!
real, dimension (nrays_max) :: rayp, d_obsPsPp, d_obsPsPpe, d_PsPp, d_obsPsPp_prop, d_PsPp_prop
integer :: nrays
logical :: PsPp_error, noisd_PsPp
real :: lsd_PsPp, liked_PsPp, lsd_PsPp_min, liked_PsPp_prop, lsd_PsPp_prop
double precision Ad_PsPp, Ad_PsPp_prop
real :: pAd_PsPp, Prnd_PsPp,Acnd_PsPp
real convAd_PsPp(nsample+burn_in),convAd_PsPps(nsample+burn_in) !variations of uncertainty parameters
real convd_PsPp(nsample+burn_in), convd_PsPps(nsample+burn_in)
real ML_Ad_PsPp(disA),ML_Ad_PsPps(disA)!histogram of uncertainty parameters
real d_PsPpmoy(nrays_max), d_PsPpdelta(nrays_max), d_PsPpmoys(nrays_max), d_PsPpdeltas(nrays_max)
real,dimension(4) :: t
! For saving all models
integer th_all
integer, parameter :: everyall = 5000
character filebycore*15
! todo: implement a test with raylquo
1000 format(I4)
!***********************************************************************
CALL cpu_time(t1) !Tic. start counting time
!Start Parralelization of the code. From now on, the code is run on each
!processor independently, with ra = the number of the proc.
!-!
call MPI_INIT(ierror)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nbproc, ierror) ! MPI_COMM_WORLD: communicator (https://www.open-mpi.org/doc/v4.0/man3/MPI_Comm_size.3.php)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror) ! https://www.open-mpi.org/doc/v4.0/man3/MPI_Comm_rank.3.php
call MPI_Comm_group(MPI_COMM_WORLD,group_world,ierror) ! https://www.open-mpi.org/doc/v3.0/man3/MPI_Comm_group.3.php
write(*,*)dirname
pxi = 0.4 ! proposal for change in xi
p_vp = 0.1 ! proposal for change in vp
pd1 = 10! 0.2 ! proposal on change in position
pv1 = 0.1! 0.04 ! proposal on velocity
pAd_R = 0.5 ! proposal for change in R noise
pAd_L = 0.5 ! proposal for change in L noise
sigmav=0.15 ! proposal for vsv when creating a new layer
sigmavp=0.15 ! proposal for vp when creating a new layer
if (PsPp_flag==1) then
pAd_PsPp = 0.5 ! proposal for change in PsPp tdiff noise
end if
testing=.false.
if (testing) write(*,*)'testing with synthetic model'
! write(*,*)'testing',maxrq
ra=rank !seed for RNG
ran=rank
inorout=0
inorouts=0
postxi=0
postxis=0
postvp=0
postvps=0
postvs=0
postvss=0
avvs=0
avvp=0
avxi=0
avvss=0
avvps=0
avxis=0
lsd_R=0
lsd_L=0
histoch=0
histochs=0
ML_Ad_R=0
ML_Ad_Rs=0
ML_Ad_L=0
ML_Ad_Ls=0
probani=0
probanis=0
ncell=0
ncells=0
convd_R=0
convd_L=0
convd_Rs=0
convd_Ls=0
convB=0
convBs=0
convBa=0
convBas=0
convD=0
convDs=0
convvs1=0
convvs1s=0
convvs2=0
convvs2s=0
convdp1=0
convdp1s=0
convdp2=0
convdp2s=0
convDa=0
convDas=0
convP=0
convPs=0
histo=0
histos=0
convAd_R=0
convAd_Rs=0
convAd_L=0
convAd_Ls=0
TRA=0
TRAs=0
d_cRmoy=0
d_cLmoy=0
d_cRmoys=0
d_cLmoys=0
d_cRdelta=0
d_cLdelta=0
d_cRdeltas=0
d_cLdeltas=0
if (PsPp_flag==1) then
ML_Ad_PsPp=0
ML_Ad_PsPps=0
convAd_PsPp=0
convAd_PsPps=0
d_PsPpmoy=0
d_PsPpdelta=0
end if
ier=.false.
!************************************************************
! READ PREM
!************************************************************
! open(7,file="Model_PREM_SIMPLE.in",status='old',form='formatted')
! ! 110 format(20a4)
! read(7,*) nptref,nic_ref,noc_ref
! read(7,'(f8.0, 3f9.2, 2f9.1, 2f9.2, f9.5)') (model_ref(i,1),&
! model_ref(i,2),model_ref(i,3),model_ref(i,4),model_ref(i,5),&
! model_ref(i,6),model_ref(i,7),model_ref(i,8),model_ref(i,9),&
! i=1,nptref)
! close(7)
open(7,file="Model_PREM_DISC_20.in",status='old',form='formatted')
read(7,*) nptref,nic_ref,noc_ref
do i = 1,nptref
read(7,*) model_ref(i,1),&
model_ref(i,2),model_ref(i,3),model_ref(i,4),model_ref(i,5),&
model_ref(i,6),model_ref(i,7),model_ref(i,8),model_ref(i,9)
end do
close(7)
j=1
do while (model_ref(j,1)<rearth-d_max*1000.)
j=j+1
end do
j=j-1
vsref_min=minval(model_ref(j:nptref,4)*(1-width)) ! setting min/max vsv velocities for writing later
vsref_max=maxval(model_ref(j:nptref,4)*(1+width))
vpref_min=minval(model_ref(j:nptref,7)*(1-vp_max)) ! setting min/max vph velocities for writing later
vpref_max=maxval(model_ref(j:nptref,7)*(1+vp_max))
! vpref_min=minval(model_ref(j:nptref,4)*vpvs*(1+vp_min/100.)*(1-width))
! vpref_max=maxval(model_ref(j:nptref,4)*vpvs*(1+vp_max/100.)*(1+width))
! Edit lines below to match periods of synethetic data to real data
! Add / remove overtones as necessary.
if (testing) then !!!!!!!testing: create synthetic model
write(*,*)'testing'
!########################################################################################################
! GET SYNTH SWD DATA ----------------------------------------------------------------
nlims_cur=1
open(65,file='./Durand_data_raw.in',status='old')! 65: name of the opened file in memory (unit identifier)
read(65,*,IOSTAT=io)ndatad_R ! number of Rayleigh modes
read(65,*,IOSTAT=io)nharm_R ! number of harmonics
do k=1,nharm_R
read(65,*,IOSTAT=io)numharm_R(k) ! number of the harmonic (fundamental=0, first=1 etc.)
read(65,*,IOSTAT=io)nlims_cur_diff ! number of modes for this harmonic
nlims_R(1,k)=nlims_cur
nlims_R(2,k)=nlims_R(1,k)+nlims_cur_diff
do i=nlims_cur,nlims_cur+nlims_cur_diff
read(65,*,IOSTAT=io)n_R(i),peri_R(i),dummy_d_obsdcR(i),d_obsdcRe(i)
d_obsdcRe(i)=obs_err_R ! Manually override errors
enddo
nlims_cur=nlims_R(2,k)+1
enddo
nlims_cur=1
read(65,*,IOSTAT=io)ndatad_L ! number of Love modes
read(65,*,IOSTAT=io)nharm_L ! number of harmonics
do k=1,nharm_L
read(65,*,IOSTAT=io)numharm_L(k) ! number of the harmonic (fundamental=0, first=1 etc.)
read(65,*,IOSTAT=io)nlims_cur_diff ! number of modes for this harmonic
nlims_L(1,k)=nlims_cur
nlims_L(2,k)=nlims_L(1,k)+nlims_cur_diff
do i=nlims_cur,nlims_cur+nlims_cur_diff
read(65,*,IOSTAT=io)n_L(i),peri_L(i),dummy_d_obsdcL(i),d_obsdcLe(i)
d_obsdcLe(i)=obs_err_L ! Manually override errors
enddo
nlims_cur=nlims_L(2,k)+1
enddo
close(65)
do k=1,nharm_R
wmin_R(k)=1000./(maxval(peri_R(nlims_R(1,k):nlims_R(2,k)))+20)
wmax_R(k)=1000./(minval(peri_R(nlims_R(1,k):nlims_R(2,k)))-2)
enddo
do k=1,nharm_L
wmin_L(k)=1000./(maxval(peri_L(nlims_L(1,k):nlims_L(2,k)))+20)
wmax_L(k)=1000./(minval(peri_L(nlims_L(1,k):nlims_L(2,k)))-2)
enddo
nmin_R=minval(n_R(:ndatad_R))
nmax_R=maxval(n_R(:ndatad_R))
nmin_L=minval(n_L(:ndatad_L))
nmax_L=maxval(n_L(:ndatad_L))
if (ndatad_R>=ndatad_L) tref=sum(peri_R(:ndatad_R))/ndatad_R
if (ndatad_L>ndatad_R) tref=sum(peri_L(:ndatad_L))/ndatad_L
!!!!!!!!!!!!!!! AL mods for P-wave stuff !!!!!!!!!!!!!!!!!
if (PsPp_flag==1) then
call get_rayp(rp_file, nrays, rayp)
do i = 1, nrays
d_obsPsPpe(i) = obs_err_PsPp
end do
end if
!!!!!!!!!!!!!!!!!!!!!!!! create synthetic model !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
npt=0
voro(npt+1,1)=0 !depth of interface
voro(npt+1,2)=0.0 !vsv=vsv_prem*(1+voro(i,2))
voro(npt+1,3)=-1 !0.7+0.5/33.*i !xi=voro(i,3), -1 for isotropic layer
voro(npt+1,4)=0.0 !vph=vph_prem*(1+voro(i,4))
npt=npt+1
voro(npt+1,1)=100.0 !depth of interface
voro(npt+1,2)=0.0 !vsv=vsv_prem*(1+voro(i,2))
voro(npt+1,3)=-1 !0.7+0.5/33.*i !xi=voro(i,3), -1 for isotropic layer
voro(npt+1,4)=0.05 !vph=vph_prem*(1+voro(i,4))
npt=npt+1
voro(npt+1,1)=200.0 !depth of interface
voro(npt+1,2)=0.0 !vsv=vsv_prem*(1+voro(i,2))
voro(npt+1,3)=-1 !0.7+0.5/33.*i !xi=voro(i,3), -1 for isotropic layer
voro(npt+1,4)=0.0 !vph=vph_prem*(1+voro(i,4))
npt=npt+1
! take voro, combine_linear_vp it with prem into a format suitable for minos
call combine_linear_vp(model_ref,nptref,nic_ref,noc_ref,voro,npt,d_max,&
r,rho,vpv,vph,vsv,vsh,qkappa,qshear,eta,nptfinal,nic,noc,vs_data,xi,vp_data)
!calculate synthetic dispersion curves
if (ndatad_R>0) then
jcom=3 !rayleigh waves
do iharm=1,nharm_R
nmodes=0
call minos_bran(1,tref,nptfinal,nic,noc,r,rho,vpv,vph,vsv,vsh,&
qkappa,qshear,eta,eps,wgrav,jcom,lmin,lmax,&
wmin_R(iharm),wmax_R(iharm),numharm_R(iharm),numharm_R(iharm),&
nmodes_max,nmodes,n_mode,l_mode,c_ph,period,raylquo,error_flag) ! calculate normal modes
if (error_flag) stop "INVALID INITIAL MODEL - RAYLEIGH - minos_bran.f FAIL 001"
peri_R_tmp=peri_R(nlims_R(1,iharm):nlims_R(2,iharm))
ndatad_R_tmp=nlims_R(2,iharm)-nlims_R(1,iharm)+1 ! fortran slices take the first and the last element
n_R_tmp(:ndatad_R_tmp)=n_R(nlims_R(1,iharm):nlims_R(2,iharm))
call dispersion_minos(nmodes_max,nmodes,n_mode,c_ph,period,raylquo,&
peri_R_tmp,n_R_tmp,d_cR_tmp,rq_R,ndatad_R_tmp,ier) ! extract phase velocities from minos output (pretty ugly)
d_cR(nlims_R(1,iharm):nlims_R(2,iharm))=d_cR_tmp
if (ier) stop "INVALID INITIAL MODEL - RAYLEIGH - CHANGE PERIODS or MODEL"
if (maxval(abs(rq_R(:ndatad_R_tmp)))>maxrq*eps) stop "INVALID INITIAL MODEL - RAYLEIGH - CHANGE PERIODS or MODEL"
enddo
endif
if (ndatad_L>0) then
jcom=2 !love waves
do iharm=1,nharm_L
nmodes=0
call minos_bran(1,tref,nptfinal,nic,noc,r,rho,vpv,vph,vsv,vsh,&
qkappa,qshear,eta,eps,wgrav,jcom,lmin,lmax,&
wmin_L(iharm),wmax_L(iharm),numharm_L(iharm),numharm_L(iharm),&
nmodes_max,nmodes,n_mode,l_mode,c_ph,period,raylquo,error_flag) ! calculate normal modes
if (error_flag) stop "INVALID INITIAL MODEL - LOVE - minos_bran.f FAIL 002"
peri_L_tmp=peri_L(nlims_L(1,iharm):nlims_L(2,iharm))
ndatad_L_tmp=nlims_L(2,iharm)-nlims_L(1,iharm)+1 ! fortran slices take the first and the last element
n_L_tmp(:ndatad_L_tmp)=n_L(nlims_L(1,iharm):nlims_L(2,iharm))
call dispersion_minos(nmodes_max,nmodes,n_mode,c_ph,period,raylquo,&
peri_L_tmp,n_L_tmp,d_cL_tmp,rq_L,ndatad_L_tmp,ier) ! extract phase velocities from minos output (pretty ugly)
d_cL(nlims_L(1,iharm):nlims_L(2,iharm))=d_cL_tmp
if (ier) stop "INVALID INITIAL MODEL - LOVE - CHANGE PERIODS or MODEL"
if (maxval(abs(rq_L(:ndatad_L_tmp)))>maxrq*eps) stop "INVALID INITIAL MODEL - LOVE - CHANGE PERIODS or MODEL"
enddo
endif
d_obsdcR(:ndatad_R)=d_cR(:ndatad_R)
d_obsdcL(:ndatad_L)=d_cL(:ndatad_L)
!!!!!!!!!!!!!!! AL mods for P-wave stuff !!!!!!!!!!!!!!!!!
if (PsPp_flag==1) then
if (nrays>0) then
call sum_Ps_Pp_diff(r, vpv, vph, vsv, vsh, nptfinal, nrays, rayp, d_obsPsPp, PsPp_error)
if (PsPp_error) stop "INVALID INITIAL MODEL - PsPp - Gradient or Nan likely"
! do i = 1, nrays
! write(*,*) d_obsPsPp(i)
! end do
end if
end if
! Add Gaussian errors to synthetic data
IF (nbproc.gt.1) THEN
IF (ran==0) THEN
do i=1,ndatad_R
d_obsdcR(i)=d_obsdcR(i)+gasdev(ra)*noise_dspd
end do
do i=1,ndatad_L
d_obsdcL(i)=d_obsdcL(i)+gasdev(ra)*noise_dspd*2 ! Make the Love noise twice the Rayleigh
end do
if (PsPp_flag==1) then
do i=1,nrays
d_obsPsPp(i)=d_obsPsPp(i)+gasdev(ra)*noise_PsPp
end do
end if
do i=2,nbproc
call MPI_SEND(d_obsdcR, ndatadmax, MPI_Real, i-1, 1, MPI_COMM_WORLD, ierror)
call MPI_SEND(d_obsdcL, ndatadmax, MPI_Real, i-1, 1, MPI_COMM_WORLD, ierror)
if (PsPp_flag==1) then
call MPI_SEND(d_obsPsPp, nrays_max, MPI_Real, i-1, 1, MPI_COMM_WORLD, ierror)
end if
enddo
else
call MPI_RECV(d_obsdcR, ndatadmax, MPI_Real, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierror)
call MPI_RECV(d_obsdcL, ndatadmax, MPI_Real, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierror)
if (PsPp_flag==1) then
call MPI_RECV(d_obsPsPp, nrays_max, MPI_Real, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierror)
end if
endif
else
do i=1,ndatad_R
d_obsdcR(i)=d_obsdcR(i)+gasdev(ra)*noise_dspd
end do
do i=1,ndatad_L
d_obsdcL(i)=d_obsdcL(i)+gasdev(ra)*noise_dspd*2 ! Make the Love noise twice the Rayleigh
end do
if (PsPp_flag==1) then
do i=1,nrays
d_obsPsPp(i)=d_obsPsPp(i)+gasdev(ra)*noise_PsPp
end do
end if
endif
! write synthetic model into a file
open(64,file=dirname//'/true_voro_model.out',status='replace')
do i=1,npt
write(64,*)voro(i,1),voro(i,2),voro(i,3),voro(i,4)
enddo
close(64)
open(65,file=dirname//'/true_model.out',status='replace')
do i=1,nptfinal
write(65,*)(rearth-r(i))/1000,vsv(i),xi(i),vph(i)
enddo
close(65)
write(*,*)'DONE INITIALIZING SYNTHETIC MODEL'
else ! real data , untested , unedited, will probably need a little work to get working
! GET SWD DATA ----------------------------------------------------------------
write(*,*) 'Starting reading data....'
nlims_cur=1
open(65,file=dirname//'/mod_dispersion.in',status='old')! 65: name of the opened file in memory (unit identifier)
read(65,*,IOSTAT=io)ndatad_R ! number of Rayleigh modes
read(65,*,IOSTAT=io)nharm_R ! number of harmonics
do k=1,nharm_R
read(65,*,IOSTAT=io)numharm_R(k) ! number of the harmonic (fundamental=0, first=1 etc.)
read(65,*,IOSTAT=io)nlims_cur_diff ! number of modes for this harmonic
nlims_R(1,k)=nlims_cur
nlims_R(2,k)=nlims_R(1,k)+nlims_cur_diff
!read(65,*,IOSTAT=io)wmin_R(k),wmax_R(k)
do i=nlims_cur,nlims_cur+nlims_cur_diff
read(65,*,IOSTAT=io)n_R(i),peri_R(i),d_obsdcR(i),d_obsdcRe(i)
enddo
nlims_cur=nlims_R(2,k)+1
enddo
nlims_cur=1
read(65,*,IOSTAT=io)ndatad_L ! number of Love modes
read(65,*,IOSTAT=io)nharm_L ! number of harmonics
do k=1,nharm_L
read(65,*,IOSTAT=io)numharm_L(k) ! number of the harmonic (fundamental=0, first=1 etc.)
read(65,*,IOSTAT=io)nlims_cur_diff ! number of modes for this harmonic
nlims_L(1,k)=nlims_cur
nlims_L(2,k)=nlims_L(1,k)+nlims_cur_diff
!read(65,*,IOSTAT=io)wmin_L(k),wmax_L(k)
do i=nlims_cur,nlims_cur+nlims_cur_diff
read(65,*,IOSTAT=io)n_L(i),peri_L(i),d_obsdcL(i),d_obsdcLe(i)
enddo
nlims_cur=nlims_L(2,k)+1
enddo
close(65)
do k=1,nharm_R
wmin_R(k)=1000./(maxval(peri_R(nlims_R(1,k):nlims_R(2,k)))+20)
wmax_R(k)=1000./(minval(peri_R(nlims_R(1,k):nlims_R(2,k)))-2)
enddo
do k=1,nharm_L
wmin_L(k)=1000./(maxval(peri_L(nlims_L(1,k):nlims_L(2,k)))+20)
wmax_L(k)=1000./(minval(peri_L(nlims_L(1,k):nlims_L(2,k)))-2)
enddo
nmin_R=minval(n_R(:ndatad_R))
nmax_R=maxval(n_R(:ndatad_R))
nmin_L=minval(n_L(:ndatad_L))
nmax_L=maxval(n_L(:ndatad_L))
if (ndatad_R>=ndatad_L) tref=sum(peri_R(:ndatad_R))/ndatad_R
if (ndatad_L>ndatad_R) tref=sum(peri_L(:ndatad_L))/ndatad_L
! ############# READ PsPp data here #######################
if (PsPp_flag==1) then
open(71,file=dirname//'/PsPp_obs.dat',status='old')
read(71,*,IOSTAT=io)nrays
do i=1,nrays
read(71,*)rayp(i),d_obsPsPp(i),d_obsPsPpe(i)
if ((rayp(i).gt.8.840).or.(rayp(i).lt.4.642)) then
stop "INVALID rayp data in PsPp_obs.dat. Exiting......."
end if
enddo
close(71)
end if
write(*,*) 'Finished reading data....'
endif
!ra=rank
!***************************************************
!***************************************************
!**************************************************************
! Draw the first model randomly from the prior distribution
!**************************************************************
! Initilalise sigma (noise parameter)
Ad_R = sp_Ad_R_max-pAd_R! Ad_min+ran3(ra)*(Ad_R_max-Ad_min)
Ad_L = sp_Ad_L_max-pAd_L
if (PsPp_flag==1) then
Ad_PsPp = sp_Ad_PsPp_max-pAd_PsPp
end if
! Initial number of cells
!------------------------------------
j=0
tes=.false.
do while(.not.tes)
50 tes=.true.
! create a starting model randomly
npt = milay+ran3(ra)*(sp_malay-milay)
if ((npt>malay).or.(npt<milay)) goto 50
write(*,*)npt
j=j+1
! first layer always at 0
voro(1,1)= 0.
voro(1,2)= (-sp_width+2*sp_width*ran3(ra))
if (ran3(ra)<0.5) then
voro(1,3)= sp_xi_min+(sp_xi_max-sp_xi_min)*ran3(ra)
else
voro(1,3)= -1
endif
if (vp_flag==0) then
! Vp Free
voro(1,4) = sp_vp_min+(sp_vp_max-sp_vp_min)*ran3(ra)
elseif (vp_flag==1) then
! Vp scaled
if (vp_scale.gt.1.0) then
voro(1,4) = voro(1,2) * vp_scale_S40(voro(1,1))
else
voro(1,4) = voro(1,2) * vp_scale
endif
elseif (vp_flag==2) then
! Vp Fixed to ref
voro(1,4) = 0.0
end if
do i=2,npt
voro(i,1)= d_min+ran3(ra)*(d_max-d_min) ! Scatters depth points between min and max value
voro(i,2)= (-sp_width+2*sp_width*ran3(ra))
if (ran3(ra)<0.5) then
voro(i,3)= sp_xi_min+(sp_xi_max-sp_xi_min)*ran3(ra)
else
voro(i,3)= -1
endif
if (vp_flag==0) then
! Vp Free
voro(i,4) = sp_vp_min+(sp_vp_max-sp_vp_min)*ran3(ra)
elseif (vp_flag==1) then
! Vp scaled
if (vp_scale.gt.1.0) then
voro(i,4) = voro(i,2) * vp_scale_S40(voro(i,1))
else
voro(i,4) = voro(i,2) * vp_scale
endif
elseif (vp_flag==2) then
! Vp Fixed to ref
voro(i,4) = 0.0
end if
enddo
! ! Should not be necessary here.....
! do i=1,npt
! do k=1,npt-1
! if (voro(k,1).gt.voro(k+1,1)) then
! t = voro(k,:); voro(k,:) = voro(k+1,:); voro(k+1,:) = t
! ENDIF
! ENDDO
! ENDDO
! open(65,file=dirname//'/stuck_039.out',status='old')
! read(65,*,IOSTAT=io)npt
! do i=1,npt
! read(65,*,IOSTAT=io)voro(i,1),voro(i,2),voro(i,3),voro(i,4)
! enddo
! close(65)
call combine_linear_vp(model_ref,nptref,nic_ref,noc_ref,voro,npt,d_max,&
r,rho,vpv,vph,vsv,vsh,qkappa,qshear,eta,nptfinal,nic,noc,vs_data,xi,vp_data)
write(*,*)"Combined"
if (ndatad_R>0) then
jcom=3 !rayleigh waves
do iharm=1,nharm_R
nmodes=0
call minos_bran(1,tref,nptfinal,nic,noc,r,rho,vpv,vph,vsv,vsh,&
qkappa,qshear,eta,eps,wgrav,jcom,lmin,lmax,&
wmin_R(iharm),wmax_R(iharm),numharm_R(iharm),numharm_R(iharm),&
nmodes_max,nmodes,n_mode,l_mode,c_ph,period,raylquo,error_flag) ! calculate normal modes
if (error_flag) then
tes=.false.
write(*,*)"Minos_bran FAILED for RAYLEIGH 004"
else
write(*,*)"Minos_bran SUCCESS for RAYLEIGH"
end if
peri_R_tmp=peri_R(nlims_R(1,iharm):nlims_R(2,iharm))
n_R_tmp=n_R(nlims_R(1,iharm):nlims_R(2,iharm))
ndatad_R_tmp=nlims_R(2,iharm)-nlims_R(1,iharm)+1 ! fortran slices take the first and the last element
call dispersion_minos(nmodes_max,nmodes,n_mode,c_ph,period,raylquo,&
peri_R_tmp,n_R_tmp,d_cR_tmp,rq_R,ndatad_R_tmp,ier) ! extract phase velocities from minos output (pretty ugly)
write(*,*)"dispersion_minos for RAYLEIGH"
d_cR(nlims_R(1,iharm):nlims_R(2,iharm))=d_cR_tmp
if (ier) tes=.false.
if (maxval(abs(rq_R(:ndatad_R_tmp)))>maxrq*eps) tes=.false.
enddo
endif
if (ndatad_L>0) then
jcom=2 !love waves
do iharm=1,nharm_L
nmodes=0
call minos_bran(1,tref,nptfinal,nic,noc,r,rho,vpv,vph,vsv,vsh,&
qkappa,qshear,eta,eps,wgrav,jcom,lmin,lmax,&
wmin_L(iharm),wmax_L(iharm),numharm_L(iharm),numharm_L(iharm),&
nmodes_max,nmodes,n_mode,l_mode,c_ph,period,raylquo,error_flag) ! calculate normal modes
if (error_flag) then
tes=.false.
write(*,*)"Minos_bran FAILED for LOVE 004"
else
write(*,*)"Minos_bran SUCCESS for LOVE"
end if
peri_L_tmp=peri_L(nlims_L(1,iharm):nlims_L(2,iharm))
n_L_tmp=n_L(nlims_L(1,iharm):nlims_L(2,iharm))
ndatad_L_tmp=nlims_L(2,iharm)-nlims_L(1,iharm)+1 ! fortran slices take the first and the last element
call dispersion_minos(nmodes_max,nmodes,n_mode,c_ph,period,raylquo,&
peri_L_tmp,n_L_tmp,d_cL_tmp,rq_L,ndatad_L_tmp,ier) ! extract phase velocities from minos output (pretty ugly)
write(*,*)"dispersion_minos for LOVE"
d_cL(nlims_L(1,iharm):nlims_L(2,iharm))=d_cL_tmp
if (ier) tes=.false.
if (maxval(abs(rq_L(:ndatad_L_tmp)))>maxrq*eps) tes=.false.
enddo
endif
if (PsPp_flag==1) then
!!!!!!!!!!!!!!! AL mods for P-wave stuff !!!!!!!!!!!!!!!!!
if (nrays>0) then
call sum_Ps_Pp_diff(r, vpv, vph, vsv, vsh, nptfinal, nrays, rayp, d_PsPp, PsPp_error)
! if (PsPp_error) tes=.false.
if (PsPp_error) then
tes=.false.
write(*,*)"INVALID PROPOSED MODEL - PsPp - Gradient or Nan likely"
end if
end if
end if
if (j>250) stop "CAN'T INITIALIZE MODEL" ! if it doesn't work after 250 tries, give up
enddo
write(*,*)'DONE INITIALIZING STARTING MODEL'
! isoflag says if a layer is isotropic
do i=1,npt
if (voro(i,3)==-1) then
isoflag(i)=.true.
else
isoflag(i)=.false.
endif
enddo
write(*,*)'getting initial likelihood'
!***********************************************************
! Get initial likelihood
!***********************************************************
lsd_R=0
lsd_L=0
liked_R=0
liked_L=0
if (PsPp_flag==1) then
lsd_PsPp = 0
liked_PsPp = 0
end if
do i=1,ndatad_R ! calculate misfit -> log-likelihood of initial model
lsd_R=lsd_R+(d_obsdcR(i)-d_cR(i))**2 ! Sum of squared misfits
liked_R=liked_R+(d_obsdcR(i)-d_cR(i))**2/(2*(Ad_R*d_obsdcRe(i))**2) ! gaussian errors
enddo
do i=1,ndatad_L
lsd_L=lsd_L+(d_obsdcL(i)-d_cL(i))**2
liked_L=liked_L+(d_obsdcL(i)-d_cL(i))**2/(2*(Ad_L*d_obsdcLe(i))**2)
enddo
if (PsPp_flag==1) then
do i=1,nrays
lsd_PsPp=lsd_PsPp+(d_obsPsPp(i)-d_PsPp(i))**2
liked_PsPp=liked_PsPp+(d_obsPsPp(i)-d_PsPp(i))**2/(2*(Ad_PsPp*d_obsPsPpe(i))**2)
enddo
end if
lsd_R_min=lsd_R
lsd_L_min=lsd_L
if (PsPp_flag==1) then
lsd_PsPp_min=lsd_PsPp
likemax=lsd_R+lsd_L+lsd_PsPp
like= liked_R + liked_L + liked_PsPp ! Combined log likelihood for R, L PsPp
else
likemax=lsd_R+lsd_L
like= liked_R + liked_L ! Combined log likelihood for R and L
end if
write(*,*)like
stuck=.false.
sample=0 ! Sampling counter (nsamples)
th=0 ! Chain thinning counter
ount=0 ! Total: burn-in+nsamples
PrP=0
PrV=0
PrB=0
PrD=0
PrBa=0
PrDa=0
AcP=0
AcV=0
AcB=0
AcD=0
AcBa=0
AcDa=0
Acnd_R=0
Prnd_R=0
Prxi=0
Acxi=0
Pr_vp=0
Ac_vp=0
Prnd_L=0
Acnd_L=0
if (PsPp_flag==1) then
Prnd_PsPp=0
Acnd_PsPp=0
end if
sigmav_old=0
sigmavp_old=0
sigma_count=0
Ar_birth_old=0
recalculated=0
write(*,*)'all initialized'
write(filenamemax,"('/All_models_invert_',I3.3,'_',I3.3,'.out')") rank, sample/everyall
open(100,file=mods_dirname//filenamemax,status='replace')
write(100,*)rank,sample/everyall,everyall
write(100,*)burn_in,nsample,thin
write(100,*)d_min,d_max
write(100,*)-width,width,vsref_min,vsref_max
write(100,*)xi_min,xi_max
write(100,*)vp_min,vp_max,vpref_min,vpref_max
write(100,*)Ad_R_min,Ad_R_max
write(100,*)Ad_L_min,Ad_L_max
! write(100,*)Ad_PsPp_min,Ad_PsPp_max ! Cant do this without changing post processing.
do while (sample<nsample) ! main loop, sample: number of sample post burn-in
ount=ount+1
malayv=malay
if (mod(ount,every)==0) then ! check regularly if acceptance rates are in an acceptable range, else change proposal width.
if (vp_flag==0) then
! Vp Free
if ((Ac_vp/(Pr_vp+1))>0.54) p_vp=p_vp*(1+perturb)! if not increase width of proposition density
if ((Ac_vp/(Pr_vp+1))<0.34) p_vp=p_vp*(1-perturb)! or decrease it
elseif (vp_flag==1) then
! Vp scaled
p_vp=p_vp
elseif (vp_flag==2) then
! Vp Fixed to ref
p_vp=p_vp
end if
if ((Acxi/(Prxi+1))>0.54) pxi=pxi*(1+perturb)
if ((Acxi/(Prxi+1))<0.34) pxi=pxi*(1-perturb)
if ((Acnd_R/(Prnd_R+1))>0.54) pAd_R=pAd_R*(1+perturb) ! for rayleigh waves
if ((Acnd_R/(Prnd_R+1))<0.34) pAd_R=pAd_R*(1-perturb)
if ((Acnd_L/(Prnd_L+1))>0.54) pAd_L=pAd_L*(1+perturb) ! for love waves
if ((Acnd_L/(Prnd_L+1))<0.34) pAd_L=pAd_L*(1-perturb)
if (PsPp_flag==1) then
if ((Acnd_PsPp/(Prnd_PsPp+1))>0.54) pAd_PsPp=pAd_PsPp*(1+perturb) ! for PsPp
if ((Acnd_PsPp/(Prnd_PsPp+1))<0.34) pAd_PsPp=pAd_PsPp*(1-perturb)
end if
if ((Acv/(Prv+1))>0.54) pv1=pv1*(1+perturb) ! 2 layers for vsv
if ((Acv/(Prv+1))<0.34) pv1=pv1*(1-perturb)
! if ((Acv(2)/(Prv(2)+1))>0.54) pv2=pv2*(1+perturb)
! if ((Acv(2)/(Prv(2)+1))<0.34) pv2=pv2*(1-perturb)
if ((Acp/(Prp+1))>0.54) pd1=pd1*(1+perturb) ! 2 layers for changing depth
if ((Acp/(Prp+1))<0.34) pd1=pd1*(1-perturb)
! if ((Acp(2)/(Prp(2)+1))>0.54) pd2=pd2*(1+perturb)
! if ((Acp(2)/(Prp(2)+1))<0.34) pd2=pd2*(1-perturb)
!------------------------------------------------
! UPDATE SIGMAV & SIGMAVP