-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutines.F90
1640 lines (1270 loc) · 48.8 KB
/
routines.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
!! Binary
!! Copyright (C) 2013 Marco Tazzari, Giuseppe Lodato
!! Version: 13.0
!! Last Modified: 22 May 2013
!!
!! Aim: This program computes the evolution of an accretion disc interacting with a binary system.
!! It also computes the evolution of an accretion disc orbiting a single central object.
!!
!! This program is free software: you can redistribute it and/or modify it under the terms of the
!! GNU General Public License as published by the Free Software Foundation, either version 3
!! of the License, or (at your option) any later version.
!!
!! This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
!! without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!! See the GNU General Public License for more details.
!! You should have received a copy of the GNU General Public License along with this program.
!! If not, see <http://www.gnu.org/licenses/>.
!!
!! If you make use of this code, please quote:
!! Tazzari, Lodato, Estimating the fossil disc mass during supermassive black hole mergers:
!! the importance of torque implementation, MNRAS, Volume 449, Issue 1, p.1118-1128
!! DOI: 10.1093/mnras/stv352
!------------------------------------------------
! this subroutine sets the defaults values
! of variables reading from a file if exists,
! otherwise sets default values.
!------------------------------------------------
subroutine read_variables()
use units, only : u_mainoutput
use variables, only : inputfile, nu0,INITIAL_SIGMA_FILE, &
& sim_mode, inr, gridtype, t0, tot_time, dtausnap, dtausnap_param,&
& mp, q, rout, rout_unit,a0, f, initial_sigma_type, sigmadot_type,&
& mdotin, m_disc0, isplanet, tinjectplanet, tmigration,alpha
implicit none
integer :: ierror
integer :: findsigmacell
! defines the namelist of the input variables
namelist / inputvariables / &
& SIM_MODE , &
& INR , &
& GRIDTYPE , &
& T0 , &
& TOT_TIME , &
& DTAUSNAP , &
& DTAUSNAP_PARAM , &
& MP , &
& Q , &
& ROUT , &
& ROUT_UNIT , &
& A0 , &
& F , &
& INITIAL_SIGMA_TYPE , &
& INITIAL_SIGMA_FILE , &
& SIGMADOT_TYPE , &
& MDOTIN , &
& M_DISC0 , &
& ISPLANET , &
& TINJECTPLANET , &
& TMIGRATION , &
& ALPHA
! set the default values
SIM_MODE = 'defaults'
INR = 300
GRIDTYPE = 1
T0 = 0.0d0
TOT_TIME = 3.0d2
DTAUSNAP = 1.0d3
DTAUSNAP_PARAM = 1.0d3
MP = 1.0d-3
Q = 0.1d0
ROUT = 20.0d0
ROUT_UNIT = 1
A0 = 1.0d0
F = 1.0d0
INITIAL_SIGMA_TYPE = 5
INITIAL_SIGMA_FILE = 'restart.dat'
SIGMADOT_TYPE = 0
MDOTIN = 1.0d0
M_DISC0 = 1.0d1
ISPLANET = 1
TINJECTPLANET = 0.0d0
TMIGRATION = 1.0d-2
ALPHA = 0.1d0
! set switches...to do
! note for input.nml:
! 1. put the name of the namelist at the beginning, e.g.: &inputvariables
! 2. write the variables, one per line, with a comma at the end, e.g.: rin=0.1,
! 3. end the values with / on the same line of the last variable, e.g.: mdotin=150./
! 4. press enter to end the file
! see for an example: sample_input.nml
! reads from input.nml, if it exists
open(unit=u_mainoutput,file=trim(inputfile),delim='apostrophe',status='old',action='read',iostat=ierror)
if (ierror==0) then
write(*,'(a,$)') ' - reading from '//trim(inputfile)//' ...'
read(u_mainoutput,nml=inputvariables)
write(*,'(a)') 'done'
else
write(*,'(a)') ' - input.nml doesn''t exist, using defaults'
endif
close(u_mainoutput)
if (sim_mode=='') sim_mode='manual'
write(*,nml=inputvariables)
! writes the used input variables
!open(unit=u_mainoutput,file=trim(homedir)//'/used_input.nml',delim='apostrophe')
!write(u_mainoutput,nml=inputvariables)
!close(u_mainoutput)
if (INITIAL_SIGMA_TYPE == 6) then
! if I want to restart the simulation
write (*,*) 'Restart from : ', INITIAL_SIGMA_FILE
endif
! checks dtausnap and dtausnap_param
if ((DTAUSNAP<DTAUSNAP_PARAM).or.((DTAUSNAP/DTAUSNAP_PARAM)/=int(DTAUSNAP/DTAUSNAP_PARAM))) then
write (*,*) 'ERROR: DTAUSNAP MUST BE A MULTIPLE OF DTAUSNAP_PARAM'
stop
endif
end subroutine read_variables
!************************************************
!------------------------------------------------
! this subroutine sets the defaults values
! of variables reading from a file if exists,
! otherwise sets default values.
!------------------------------------------------
subroutine dimensional_variables()
use variables, only : mp,GMp,Ms,q,rg,rISCO,rin,rout,rout_unit,mdotin,a0,m_disc0,gwprefactor,tot_time,dtausnap,tmigration,&
& t0,T_c_prefactor,alpha,Rovermu,T_eff_prefactor,acrit,isncount_latest,dtausnap_param,rg10,&
& initial_dtausnap_param,L_Edd
use constants, only : c_light,c_light2,M_sun,parsec,year,Grav,R_gas,mu,sig_sb,twopi,fourpi,eta,sigma_T,m_p
implicit none
mp = mp * M_sun
GMp = Grav * mp
Ms = q * mp
! note : rg with 2* is the Schwarschild radius. I made this choice for a more direct comparison with Chang & al. 2010.
rg = 2.0d0 * GMp / c_light**2.
rg10 = 10.0d0 * rg
rISCO = 3.0d0 * rg
acrit = 500 * rg
isncount_latest = 500 ! (decreasing) index for the snapshots prior to merger
rin = rISCO
if (rout_unit==1) then
! a0 and rout are given in parsec
a0 = a0 * parsec
rout = rout * parsec
elseif (rout_unit==2) then
! a0 and rout are given in rg
! for Chang et al. 2009 simulations
a0 = a0 * rg
rout = rout * rg
elseif (rout_unit==3) then
! a0 is given in parsec
! rout is given in a0
! for Lodato et al. 2009 simulations
a0 = a0 * parsec
rout = rout * a0
endif
mdotin = mdotin * M_sun / year
m_disc0 = m_disc0 * M_sun
gwprefactor = 64./5. * ((rg/2)**3.) * c_light * (1+q) * q
t0 = t0 * year
tot_time = tot_time * year
dtausnap = dtausnap * year
dtausnap_param = dtausnap_param * year
initial_dtausnap_param = dtausnap_param
tmigration = tmigration * year
if (tmigration == 0.0d0 ) tmigration = 300.0d0*twopi/sqrt(GMp/(a0**3.)) ! migration starts after 300 orbits
Rovermu= R_gas / mu
T_c_prefactor = 9./16. * alpha * Rovermu / sig_sb
T_eff_prefactor = 9./8. / sig_sb
!Eddington Luminosity
L_Edd = fourpi * Grav * c_light * m_p * Mp / sigma_T
print *, 'check dimensional variables'
!write(*,'(g13.6,g13.6,g13.6,g13.6,g13.6,g13.6)') mp,GMp,Ms,rg,rISCO,rin
!write(*,'(g13.6,g13.6,g13.6,g13.6)') a0,rout,mdotin,m_disc0,gwprefactor
!write(*,'(g13.6,g13.6)') gwprefactor, tmigration/year
write (*,'(a,ES14.6)') 'Orbit period [year]: ',tmigration/300.0d0/year
end subroutine dimensional_variables
!************************************************
!------------------------------------------------
! prepares files
!------------------------------------------------
subroutine initialize_files()
use variables, only: massfilename,planetfilename,splashfilenames,homedir,today,sim_mode,sigma_negfile,inputfile
use units, only: u_snapshots,u_massfile,&
& u_splashfilenames,u_sigma_neg
implicit none
! sets the current day/month/year in the vector today(3)
call idate(today)
write(homedir,'(a,i4,i2.2,i2.2,a,a)') 'sim_',today(3),today(2),today(1),'_',trim(sim_mode)
call system('mkdir '//trim(homedir))
call system('mkdir '//trim(homedir)//'/snap')
call system('mkdir '//trim(homedir)//'/snap_latest')
call system('mkdir '//trim(homedir)//'/code')
!if already exists, deletes mass.dat
write(massfilename,'(a,a)') trim(homedir),'/mass.dat'
open(unit=u_massfile, file=massfilename,status='unknown')
close(u_massfile,status='delete')
! if already exists, deletes splash.filenames
write(splashfilenames,'(a,a)') trim(homedir),'/splash.filenames'
open(unit=u_splashfilenames, file=splashfilenames,status='unknown')
close(u_splashfilenames,status='delete')
! save the code and the parameters used for the current simulation
call system('cp '//trim(inputfile)//' '//trim(homedir)//'/code/usedinput.nml')
call system('cp binary.F90 '//trim(homedir)//'/code/binary.F90')
call system('cp routines.F90 '//trim(homedir)//'/code/routines.F90')
call system('cp variables.F90 '//trim(homedir)//'/code/variables.F90')
call system('cp constants.F90 '//trim(homedir)//'/code/constants.F90')
!call system('cp analytic.F90 '//trim(homedir)//'/code/analytic.F90')
call system('cp makefile '//trim(homedir)//'/code/makefile')
call system('cp splash.limits '//trim(homedir)//'/splash.limits')
call system('cp splash.defaults '//trim(homedir)//'/splash.defaults')
call system('cp splash.columns '//trim(homedir)//'/splash.columns')
! if already exists, deletes file sigma_neg.dat
write(sigma_negfile,'(a,a)') trim(homedir),'/sigma_neg.dat'
open(unit=u_sigma_neg, file=sigma_negfile,status='unknown')
close(u_sigma_neg,status='delete')
! reopens sigma_neg.dat to prepare it for writing
open(unit=u_sigma_neg, file=sigma_negfile,status='unknown')
end subroutine initialize_files
!************************************************
!------------------------------------------------
! allocates all the arrays
!------------------------------------------------
subroutine allocate_arrays()
use variables, only: inr,inr2,x,xpc,xrg,x12,x2,dx,dx2,xdx,D,sigma,oldsigma,mdot,&
& lambda,advvel,nu,H,exactsigma,gg,area,omega,omega2,inside_rhill,T_c,T_eff
implicit none
!write(*,'(a,$)') ' - allocating arrays...'
inr2=2*inr
allocate(x(inr2),xpc(inr),xrg(inr),x12(inr2),x2(inr2))
allocate(dx(inr2),dx2(inr2),xdx(inr),area(inr),D(inr))
allocate(sigma(inr),oldsigma(inr),exactsigma(inr),mdot(inr))
allocate(lambda(inr),advvel(inr),nu(inr),H(inr),gg(inr),omega(inr),omega2(inr))
allocate(T_c(inr),T_eff(inr))
allocate(inside_rhill(inr))
!write(*,*) 'done'
end subroutine allocate_arrays
!************************************************
!------------------------------------------------
! allocates all the arrays
!------------------------------------------------
subroutine deallocate_arrays()
use variables, only: x,xpc,xrg,x12,x2,dx,dx2,xdx,D,sigma,oldsigma,mdot,nu,omega,omega2,inside_rhill,&
& lambda,advvel,nu,H,exactsigma,gg,area,T_c,T_eff
implicit none
deallocate(x,xpc,xrg,x12,x2,dx,dx2,xdx,area,D,sigma,oldsigma,exactsigma,mdot,lambda,advvel,H,gg)
deallocate(nu,omega,omega2,T_c,T_eff)
deallocate(inside_rhill)
end subroutine deallocate_arrays
!************************************************
!------------------------------------------------
! sets the spatial grid: x, dx
!------------------------------------------------
subroutine set_grid()
use variables, only: i,inr,inr2,x,xpc,xrg,x12,x2,dx,dx2,xdx,rin,rout,dx_lin,gridtype,mass,area,omega,omega2,GMp,rg
use constants, only: twopi,parsec
implicit none
integer :: findxcell
write(*,'(a,$)') ' - setting radial grid...'
!Sets radial limits. Warning: rin=0. gives error because D(i) is proportional to x(i)**-1
select case (gridtype)
case (0)
! LINEAR GRID
!Warning: x(1) is not defined
x(2)=rin
dx_lin=(rout-rin)/real(inr2-2)
do i=3,inr2
x(i)=x(2)+(i-2)*dx_lin
enddo
!print *, x(i),dx_lin ! check OK
! x(2) and x(inr2) are the position of ghost cells
case (1)
! LOG GRID
! Warning: x(1) is not defined
! in a log grid: - delta_x/x is constant
! - x(i+1)/x(i) is constant
! - in the log space, the points are equally spaced
! 10-based logarithm
x(2)=log10(rin)
dx_lin=(log10(rout)-log10(rin))/(inr2-2)
do i=3,inr2
x(i)=10.**(x(2)+dx_lin*(i-2))
enddo
x(2)=10.**x(2)
end select
!Sets dx of the spacing grid.
!Warning: dx(1) is not defined
do i=3,inr2-1
dx(i)=x(i+1)-x(i-1)
dx2(i)=dx(i)**2.
x12(i)=sqrt(x(i))
x2(i)=x(i)**2.
enddo
x(1)=0.0d0
x12(1)=0.0d0
x12(2)=sqrt(x(2))
x12(inr2)=sqrt(x(inr2))
x2(1)=0.0d0
x2(2)=0.0d0
x2(inr2)=x(inr2)**2.
dx(2)=dx(3)
dx(inr2)=dx(inr2-1)
dx2(2)=dx(3)**2.
dx2(inr2)=dx(inr2-1)**2.
! area(i) contains annuli area
do i=1,inr
xdx(i)=x(2*i)*dx(2*i)
area(i)=twopi*xdx(i)
omega2(i)=GMp/(x(2*i)**3.)
omega(i)=sqrt(omega2(i))
xpc(i)=x(2*i)/parsec
xrg(i)=x(2*i)/rg
!print *, x(2*i),dx(2*i),area(i)
!print *, i,xdx(i),area(i),omega2(i),omega(i)
enddo
write(*,*) 'done'
! major check of the grid initialization
!do i=1,inr2
!print *, i,x(i),(x(i+1)-x(i))/x(i),dx(i)
!mass=mass+area(i/2)
!print *, i,area(i),mass
!enddo
end subroutine set_grid
!************************************************
!------------------------------------------------
! sets the time related variables
!------------------------------------------------
subroutine set_time()
use variables, only: t,t0,dtau,tot_time,dtausnap,isnapnum,timenextsnap,initial_sigma_type,timenextsnap_param,dtausnap_param
implicit none
!write(*,'(a,$)') ' - setting time-related variables and snapshots...'
t=t0
! sets the first dtau in order to correctly initialize the sigma
if (initial_sigma_type/=6) dtau=1.d-3
! sets the time interval between two snapshots and the total number of snapshots
!!dtausnap=0.1
isnapnum=nint((tot_time-t0)/dtausnap)+1
! sets the time of the next (the first) snapshot after the initial one
timenextsnap=t0+dtausnap
timenextsnap_param = t0+dtausnap_param
!write(*,*) 'done'
!write(*,'(a,i7,a)') ' - this simulation will compute:',isnapnum-1,' snapshots'
end subroutine set_time
!************************************************
!------------------------------------------------
! sets the initial distribution of sigma
!------------------------------------------------
subroutine set_initialsigma()
use variables, only: i,inr,inr2,oldsigma,sigma,sigmadot_rin,sigmadot_rout,&
& rin,rout,x,nu,tau0,t0,xin,initial_sigma_type,mdotin,nu,nu0,a,ia,&
& a0,sigma0,m_disc0,INITIAL_SIGMA_FILE,rout,nu,T_eff,T_c,H,mass,sigmadot_type,prec_out,area
use formats, only: f_snapshots
! use constants, only: pi_d !it's commented because pi_d is already defined in nrtype; pi_d is the double precision version.
use constants, only: threepi,pi,parsec,M_Sun
! Numerical Recipes modules
!use nrtype
!use nrutil
!use nr, only : bessik
!----------------------------------
implicit none
! functions declaration
!double precision :: spreading_ring
double precision :: trash
integer :: i0
integer :: findxcell
double precision :: mass_normalization
double precision :: r0
nu0=1.5d20
!Sets the sigma equal to zero everywhere.
write(*,'(a,$)') ' - setting the initial sigma distribution...'
do i=1,inr
oldsigma(i)=0.0d0
sigma(i)=0.0d0
!nu(i)=nu0*(x(2*i)/(0.1*parsec))**(1.5d0)
enddo
!print *, 'nu(inr)',inr, nu(inr) ! check OK
sigmadot_rout=0.0d0
select case(initial_sigma_type)
case(0)
! no initial sigma
if (sigmadot_type == 2) then
! correction to avoid errors in self-consistent calculation of nu, T_c, H
sigmadot_rout=1.0d-4
sigma(inr-1)=1.0d-8
sigma(inr)=1.0d-8
oldsigma(inr-1)=sigma(inr-1)
oldsigma(inr)=sigma(inr)
nu(inr-1)=1.0d0
nu(inr)=1.0d0
endif
case(1)
! Dirac-delta function centered in r0
r0=10.*a0
i0=findxcell(x,inr2,r0)/2
sigma(i0)=100.
case(2)
! linear+exponential decay
do i=1,inr
sigma(i)=(rout-x(2*i))*exp(-x(2*i)/(rout/10.))
enddo
case(3)
! exponential
mass_normalization=pi*rout**2*(exp(-rin/rout)*(rin/rout+1)-2.*exp(-1.))
do i=1,inr
sigma(i)=10.*exp(-(x(2*i)/(rout/10.)))/mass_normalization
enddo
case(4)
! spreading ring
! needs analytic.o to be included
do i=1,inr
!sigma(i)=spreading_ring(xin,x(2*i),t0)
! print *,sigma(i),xin,x(2*i),t0
enddo
case(5)
! stationary disc
!ia=findxcell(x,inr2,a)/2
!mass_normalization=mdotin/(threepi*nu(ia))*(1.-sqrt(rin/x(2*ia)))*(a**2.)
mass = 0
!do i=1,findxcell(x,inr2,rout*0.99)/2
do i = 1, inr-1
!sigma(i)=a**(-2.)*(nu(ia)/nu(i))*((1.-sqrt(rin/x(2*i)))/(1.-sqrt(rin/x(2*ia))))
!sigma(i)=mdotin/(threepi*nu(i))*(1.-sqrt((rin/x(2*i))))
sigma(i)=x(2*i)**(-3/5.)*(1.-sqrt((rin/x(2*i))))
mass = mass + area(i)*sigma(i)
oldsigma(i)=sigma(i)
!print *,x(2*i),sigma(i),nu(i)
enddo
! re-normalize the sigma distribution in order to obtain a disc of mass M_DISC0
do i = 1, inr
sigma(i)=sigma(i)/mass*m_disc0
enddo
if (sigmadot_type == 2) then
! correction to avoid errors in self-consistent calculation of nu, T_c, H
sigmadot_rout=1.0d-4
sigma(inr)=1.0d-8
oldsigma(inr)=sigma(inr)
nu(inr-1)=1.0d0
nu(inr)=1.0d0
endif
case(6)
! initialize sigma from a file
! i.e. : it is used to restart simulation
open(unit=30,file=INITIAL_SIGMA_FILE,status='old')
do i=1,inr
read(30,f_snapshots) trash,sigma(i),nu(i),T_eff(i),T_c(i),trash,trash
!write(*,'(5(ES14.6,1x))'), x(2*i),sigma(i),nu(i),T_eff(i),T_c(i)
enddo
close(30)
trash=0.0d0
case(7)
! initial condition Lodato et al. 2009
sigma0=m_disc0 / (pi*((2.0*a0)**2.-(0.8*a0)**2.))
!print*, m_disc0,2.0*a0,*a0
do i = findxcell(x,inr2,0.8*a0)/2, findxcell(x,inr2,2.0*a0)/2
sigma(i)=sigma0
!print *, i , sigma(i)
enddo
if (sigmadot_type == 2) then
! correction to avoid errors in self-consistent calculation of nu, T_c, H
sigmadot_rout=1.0d-4
sigma(inr-1)=1.0d-8
sigma(inr)=1.0d-8
oldsigma(inr-1)=sigma(inr-1)
oldsigma(inr)=sigma(inr)
nu(inr-1)=1.0d0
nu(inr)=1.0d0
endif
case(8)
! initial steady state of a b) region (gas pressure dominated)
! Chang initial condition
! initialize sigma from a file
mass = 0.0d0
open(unit=30,file=INITIAL_SIGMA_FILE,status='old')
do i=1,inr
read(30,f_snapshots) trash,sigma(i),nu(i),T_eff(i),T_c(i),trash,trash
!write(*,'(5(ES14.6,1x))'), x(2*i),sigma(i),nu(i),T_eff(i),T_c(i)
mass = mass + area(i)*sigma(i)
enddo
close(30)
trash=0.0d0
! re-normalize the sigma distribution in order to obtain a disc of mass M_DISC0
do i = 1, inr
sigma(i)=sigma(i)/mass*m_disc0
enddo
if (sigmadot_type == 2) then
! correction to avoid errors in self-consistent calculation of nu, T_c, H
sigmadot_rout=1.0d-4
sigma(inr)=1.0d-8
oldsigma(inr)=sigma(inr)
nu(inr-1)=1.0d0
nu(inr)=1.0d0
endif
case(9)
! same initial steady state of case(8), i.e. of Chang et al.
! here I clear a region around the secondary's initial radius 0.5 < r/a0 < 2.0
mass = 0.0d0
open(unit=30,file=INITIAL_SIGMA_FILE,status='old')
do i=1,inr
read(30,f_snapshots) trash,sigma(i),nu(i),T_eff(i),T_c(i),trash,trash
!write(*,'(5(ES14.6,1x))'), x(2*i),sigma(i),nu(i),T_eff(i),T_c(i)
mass = mass + area(i)*sigma(i)
enddo
close(30)
trash=0.0d0
! re-normalize the sigma distribution in order to obtain a disc of mass M_DISC0
do i = 1, inr
sigma(i)=sigma(i)/mass*m_disc0
enddo
if (sigmadot_type == 2) then
! correction to avoid errors in self-consistent calculation of nu, T_c, H
sigmadot_rout=1.0d-4
sigma(inr)=1.0d-8
oldsigma(inr)=sigma(inr)
nu(inr-1)=1.0d0
nu(inr)=1.0d0
endif
! now I clear the gap
do i = findxcell(x,inr2,0.5*a0)/2 , findxcell(x,inr2,2.0*a0)/2
sigma(i) = 0.0d0
enddo
end select
write(*,*) 'done'
call calculate_mass()
write (*,'(a,ES14.6)') 'Initial disc mass [M_Sun]: ',mass/M_sun
!Sets the initial boundary condition
!Zero-torque boundary conditions at r=rin and r=rout
sigmadot_rin=0.
sigma(1)=sigmadot_rin
sigma(inr)=sigmadot_rout
! check
! do i=1,inr
! write(*,'(5(ES14.6,1x))'), x(2*i),sigma(i),nu(i),T_eff(i),T_c(i)
! enddo
! stop
end subroutine set_initialsigma
!************************************************
!------------------------------------------------
! sets dtau according to
! courant stability (necessary condition)
!------------------------------------------------
subroutine set_dtau()
use variables, only: i,inr2,dtau,nu,dx,dx2,xdx,area,advvel,oldsigma,dtaumin,sigmacell,CFL,a,t,x,adot,iax,acc_factor
use constants, only: year
implicit none
dtaumin=1.d10
do i=2,inr2-1
sigmacell=i/2
dtaumin=min(dtaumin,dx2(i)/nu(sigmacell),dx2(i+1)/nu(sigmacell),&
& xdx(sigmacell)/abs(advvel(sigmacell)),xdx(sigmacell)/abs(advvel(sigmacell)))
!print *,dtaumin,dx2(i)/nu(sigmacell),dx2(i+1)/nu(sigmacell),&
! & area(i)/abs(advvel(sigmacell)),area(i)/abs(advvel(sigmacell))
enddo
! Lodato uses accuracy acc=0.1 (line 632 main.f). I call it acc_factor
dtaumin=min(dtaumin,acc_factor*dx(iax)/abs(adot))
! if (dtaumin >= 0.1*dx(iax)/abs(adot)) then
! !print *,'**** ECCO ****', dtaumin/year,a,iax,dx(iax)
! dtaumin=min(dtaumin,0.1*dx(iax)/abs(adot))
! endif
dtau=dtaumin*CFL
end subroutine set_dtau
!************************************************
!------------------------------------------------
! sigma dot
!------------------------------------------------
subroutine set_sigmadot()
use variables, only: sigma,oldsigma,idrip,inr,inr2,mdotin,dtau,dx,nu,x,sigmadotin,sigmadot_type
use constants, only: sixpi
implicit none
select case(sigmadot_type)
case(0)
oldsigma(inr)=0.
return
! case(1)
! ! matter drips in at a certain radius at every timestep
! oldsigma(idrip)=oldsigma(idrip)+sigmadotin*dtau
case(2)
! performs constant inflow of matter at inr
oldsigma(inr)=(sqrt(x(inr2-2))*nu(inr-1)*oldsigma(inr-1)+(mdotin*(dx(inr2-2)+dx(inr2-1)))/&
&(sixpi*sqrt(x(inr2-1))))/(sqrt(x(inr2))*nu(inr))
sigma(inr)=oldsigma(inr)
!print *, 'added',nu(inr),oldsigma(inr) ! check
return
end select
! Major check of sigma initialization
!do i=1,inr
! print *,i,x(2*i),sigma(i)
!enddo
end subroutine set_sigmadot
!************************************************
!------------------------------------------------
! computes the torque and the advective velocity
! uses torque clipping
!------------------------------------------------
subroutine set_torque_advvel_clip
use variables, only: i,inr,inr2,lambda,advvel,H,x,x12,x2,dx,f,q,&
& r_hill,a,ia,iax,ILR,OLR,iILR,iOLR,delta,deltamin,iIlRx,iOLRx,iax,&
& Rey,factor,omega,omega2,sigma,dtau,inside_rhill,r_hill,clipped_torque,rg
use constants, only : twothirds,two_at_twothirds,onehalf_at_twothirds,parsec
implicit none
integer :: findxcell
! SMOOTHING PRESCRIPTION
! From: Lodato et al. 2009, Armitage & Natarajan 2002, Goldreich & Tremaine 1980, Armitage "Astrophysics of planet formation", page 236
! Statement: - smoothing where |r-a| < max(H,r_hill), where H is the scale height
! Besides, we impose an exponential decay of lambda at radius outside the first outer Lindblad resonance.
! finds the Lindblad resonances
OLR=two_at_twothirds*a
ILR=onehalf_at_twothirds*a
!iILRx=findxcell(x,inr2,ILR)
!iOLRx=findxcell(x,inr2,OLR)
!iILR=iILRx/2
!iOLR=iOLRx/2
! gap size
deltamin=max(H(ia),r_hill)
! maybe H(ia) is the semi-thickness of the disc : chiedere a Lodato
!write(*,'(3(ES14.6))') ilr/rg,a/rg,olr/rg
do i=2,inr-1
factor=1.0d0
delta=max(deltamin,abs(x(2*i)-a),2.*dx(2*i))
if (x(2*i) <= a) then
if (x(2*i) <= ILR) factor = exp(-((x(2*i)-ILR)/(20.*H(ia)))**2.)
factor = -factor * (x(2*i)/delta)**4.
else
if (x(2*i) >= OLR) factor = exp(-((x(2*i)-OLR)/(20.*H(ia)))**2.)
factor = factor * (a/delta)**4.
endif
! if (abs(x(2*i)-a)<= r_hill) then
! inside_rhill(i)=1.
! else
! inside_rhill(i)=0.
! endif
!write(*,'(2(i3.3),7(ES14.6))') i,ia,H(ia-30),r_hill,deltamin,x(2*i)-a,2.*dx(2*i),delta,factor
lambda(i)=factor*0.5*f*(q**2.)*omega2(i)*x2(2*i)
! torque clipping
! if the local value of the torque is greater than a certain amount, then clip the torque
! I use the prescription in Alexander & Armitage 2009, page 3
clipped_torque=0.1*omega2(i)*x(2*i)*H(ia)
if (abs(lambda(i))>=clipped_torque) then
!write(*,'(2(ES14.6))') lambda(i), clipped_torque
lambda(i)=sign(clipped_torque,lambda(i))
endif
advvel(i)=2.*lambda(i)/omega(i)
enddo
! mass conservation condition: the torque has to be null in the first near cells
lambda(ia)=0.
lambda(ia+1)=0.
advvel(ia)=0.
advvel(ia+1)=0.
! checks
!print *, lambda(iILR+1),lambda(iOLR-2),OLR,x(2*iOLR-1),x(2*iOLR),x(2*iOLR+1)
!do i = 1,inr
! print *,x(2*i),lambda(i),advvel(i) !check OK
!enddo
end subroutine set_torque_advvel_clip
!************************************************
!------------------------------------------------
! computes the torque
! and the advective velocity
!------------------------------------------------
subroutine set_torque_advvel_hill()
use variables, only: i,inr,inr2,lambda,advvel,H,x,x12,x2,dx,f,q,&
& r_hill,a,ia,iax,ILR,OLR,iILR,iOLR,delta,deltamin,iIlRx,iOLRx,iax,&
& Rey,factor,omega,omega2,sigma,dtau,inside_rhill,r_hill,rg,d_smoothing_in,d_smoothing_out
use constants, only : twothirds,two_at_twothirds,onehalf_at_twothirds,parsec
implicit none
integer :: findxcell
! SMOOTHING PRESCRIPTION
! From: Lodato et al. 2009, Armitage & Natarajan 2002, Goldreich & Tremaine 1980, Armitage "Astrophysics of planet formation", page 236
! Statement: - smoothing where |r-a| < max(H,r_hill), where H is the scale height
! Besides, we impose an exponential decay of lambda at radius outside the first outer Lindblad resonance.
! finds the Lindblad resonances
OLR=two_at_twothirds*a
ILR=onehalf_at_twothirds*a
!iILRx=findxcell(x,inr2,ILR)
!iOLRx=findxcell(x,inr2,OLR)
!iILR=iILRx/2
!iOLR=iOLRx/2
! gap size
deltamin=max(2.*H(ia),r_hill)
! maybe H(ia) is the semi-thickness of the disc : chiedere a Lodato
do i=2,inr-1
! added on 21 May 2013
d_smoothing_in=370.0d0*H(i)
d_smoothing_out=75.0d0*H(i)
if (H(i)==0.) then
d_smoothing_in = dx(2*i)
d_smoothing_out = dx(2*i)
endif
factor=1.0d0
delta=max(deltamin,abs(x(2*i)-a),2.*dx(2*i))
if (x(2*i) <= a) then
!if (x(2*i) <= ILR) factor = exp(-((x(2*i)-ILR)/(H(i)))**2.)
if (x(2*i) <= ILR) factor = exp(-((x(2*i)-ILR)/d_smoothing_in)**2.)
factor = -factor * (x(2*i)/delta)**4.
else
!if (x(2*i) >= OLR) factor = exp(-((x(2*i)-OLR)/(H(i)))**2.)
if (x(2*i) >= OLR) factor = exp(-((x(2*i)-OLR)/d_smoothing_out)**2.)
factor = factor * (a/delta)**4.
endif
! if (abs(x(2*i)-a)<= r_hill) then
! inside_rhill(i)=1.
! else
! inside_rhill(i)=0.
! endif
!write(*,'(2(i3.3),7(ES14.6))') i,ia,H(ia-30),r_hill,deltamin,x(2*i)-a,2.*dx(2*i),delta,factor
lambda(i)=factor*0.5*f*(q**2.)*omega2(i)*x2(2*i)
advvel(i)=2.*lambda(i)/omega(i)
enddo
! mass conservation condition: the torque has to be null in the first near cells
lambda(ia)=0.
lambda(ia+1)=0.
advvel(ia)=0.
advvel(ia+1)=0.
! checks
!print *, lambda(iILR+1),lambda(iOLR-2),OLR,x(2*iOLR-1),x(2*iOLR),x(2*iOLR+1)
!do i = 1,inr
! print *,x(2*i),lambda(i),advvel(i) !check OK
!enddo
end subroutine set_torque_advvel_hill
!************************************************
!------------------------------------------------
! computes the torque
! and the advective velocity
!------------------------------------------------
subroutine set_torque_advvel()
use variables, only: i,inr,inr2,lambda,advvel,H,x,x12,x2,dx,f,q,&
& r_hill,a,ia,iax,ILR,OLR,iILR,iOLR,delta,deltamin,iIlRx,iOLRx,iax,&
& Rey,factor,omega,omega2,sigma,dtau,inside_rhill
use constants, only : twothirds,two_at_twothirds,onehalf_at_twothirds
implicit none
integer :: findxcell
! SMOOTHING PRESCRIPTION
! From: Lodato et al. 2009, Armitage & Natarajan 2002, Goldreich & Tremaine 1980, Armitage "Astrophysics of planet formation", page 236
! Statement: - smoothing where |r-a| < max(H,r_hill), where H is the scale height
! Besides, we impose an exponential decay of lambda at radius outside the first outer Lindblad resonance.
! finds the Lindblad resonances
!OLR=two_at_twothirds*a
!ILR=onehalf_at_twothirds*a
!iILRx=findxcell(x,inr2,ILR)
!iOLRx=findxcell(x,inr2,OLR)
!iILR=iILRx/2
!iOLR=iOLRx/2
! set initial oldsigma
do i=1,inr
! smoothing of the torque outside the Lindblad resonances.
! the resonances are not dirac delta functions; they're instead gaussian-like of width H(i)
! result from : Artymowicz, P., ApJ, v.419 419, 155 (1993).
! Marco Tazzari Torque
factor=1.0d0
delta=max(abs(x(2*i)-a),H(i))
if (abs(x(2*i)-a) < H(i)) then
factor = (x(2*i)-a)/H(i)
else
factor = sign(1.0d0,x(2*i)-a)
endif
if (x(2*i) <= a) then
!if (x(2*i) <= ILR) factor = exp(-((x(2*i)-ILR)/H(i))**2.)
lambda(i)= (x(2*i)/delta)**4. * factor
else
!if (x(2*i) > OLR) factor = exp(-((x(2*i)-OLR)/H(i))**2.)
lambda(i)= (a/delta)**4. * factor
endif
! if (abs(x(2*i)-a)<= r_hill) then
! inside_rhill(i)=1.
! else
! inside_rhill(i)=0.
! endif
lambda(i)=lambda(i)*0.5*f*(q**2.)*omega2(i)*x2(2*i)
advvel(i)=2.*lambda(i)/omega(i)
enddo
! mass conservation condition: the torque has to be null in the first near cells
lambda(ia)=0.
lambda(ia+1)=0.
advvel(ia)=0.
advvel(ia+1)=0.
! checks
!print *, lambda(iILR+1),lambda(iOLR-2),OLR,x(2*iOLR-1),x(2*iOLR),x(2*iOLR+1)
!do i = 1,inr
! print *,x(2*i),lambda(i),advvel(i) !check OK
!enddo
end subroutine set_torque_advvel
!************************************************
!------------------------------------------------
! Disc thickness with Chang et al. 2010 prescription
! computes the disc thickness taking into account the gas pressure only and fixed opacity.
!------------------------------------------------
subroutine set_thickness_chang()
use variables, only : H,i,inr,x,T_c,alpha,T_c_prefactor,omega,omega2,oldsigma,Rovermu,nu,aconst,bconst,cconst,thin_corr,tau,&
& a,i_t,accT,t,T_guess,k_ff,errT,T_eff,T_eff_prefactor,T_new
use constants, only : onethird,twothirds,a_rad,k_T,k_ff_0,oneseventh,twothirteenths,parsec
implicit none
do i=2,inr-1
! central temperature