-
Notifications
You must be signed in to change notification settings - Fork 0
/
riemann_rmhd.f90
2233 lines (1876 loc) · 73.9 KB
/
riemann_rmhd.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
!$Id: riemann_rmhd.f90,v 1.8 2007-08-14 12:05:58 bgiacoma Exp $
program riemann_rmhd
use type
use global !This module contains Bx, gamma, degen, niter and initial_data
use accmod !This module contains accuracy
use interfaces,only:initialdata,contact,output,fullcontact,xi,eos_enthalpy
use output_grid !This module contains x1, x2, t, nx
use alfven_wave !contains an initial guess for the alfven waves
use eos_param !Set the EOS
implicit none
integer(I4B)::verbo,init
real(DP),dimension(7)::left,right
real(DP)::pb,VsLeft,VsRight,dump
real(DP),dimension(7,2)::solution
real(DP),dimension(7,6)::fullsolution
real(DP),dimension(3)::VsLv3,VsRv3
!This program find the exact solution of the Riemann Problem
!in Relativistic Magneto-HydroDynamics with an ideal Equation of State.
!For a detailed description of the method see:
!Giacomazzo & Rezzolla 2006, J. Fluid Mech. 562, 223-259
!(arxiv.org: gr-qc/0507102)
!Copyright (C) 2005 B. Giacomazzo, L. Rezzolla
integer(I4B)::degen_case,eos_param_value
real(DP)::leftalfven_left,rightalfven_left,leftalfven_right,rightalfven_right,eta,b2,wtot,enthalpy
real(DP),dimension(4)::all_left,all_right
real(DP),dimension(4)::unk
real(DP),PARAMETER::small_vel=1.0d-15
degen_case=0
print *,'Select initial condition:'
print *,' 0) User defined'
print *,' 1) Marti-Muller figure 7 (hydro-test)'
print *,' 2) Marti-Muller figure 6 (hydro-test)'
print *,' 3) Marti-Muller figure 5 (hydro-test)'
print *,' 4) Generic Shock-Tube Test (Bx=0)'
print *,' 5) Komissarov: Shock-Tube Test 2 (Bx=0)'
print *,' 6) Komissarov: Shock-Tube Test 1'
print *,' 7) Komissarov: Collision'
print *,' 8) Balsara Test 1'
print *,' 9) Balsara Test 2'
print *,'10) Balsara Test 3'
print *,'11) Balsara Test 4'
print *,'12) Balsara Test 5'
print *,'13) Generic Alfven Test'
read(*,*) initial_data
print *,'EOS:'
print *,'1) Ideal Fluid'
print *,'2) Meliani et al.'
read(*,*) eos_param_value
if (eos_param_value==1) then
eos_ideal = .true. !we use an ideal EOS
eos_meliani = .false. !we do not use the Meliani et al. EOS
elseif (eos_param_value==2) then
eos_ideal = .false. !we do not use an ideal EOS
eos_meliani = .true. !we use the Meliani et al. EOS
else
write(*,*)'riemann_rmhd: wrong choice for the EOS'
STOP
end if
print *,'left boundary (used only for output)='
read(*,*) x1
print *,'right boundary (used only for output)='
read(*,*) x2
print *,'time (used only for output)='
read(*,*) t
print *,'number of grid points to be used to plot the solution ='
read(*,*) nx
print *,'accuracy (usually 1.0e-10)='
read(*,*) accuracy
print *,'verbose (2=a lot of output, 1=normal, 0=only essential things)='
read(*,*) verbo
if (verbo==1) then
veryverbose=.false.
verbose=.true.
else if (verbo==2) then
veryverbose=.true.
verbose=.true.
else
veryverbose=.false.
verbose=.false.
end if
!initial conditions
init=initial_data
call initialdata(init,left,right)
!Initial guess
!NOTE:
!unk(1)= p in regions R2-R3 (total pressure)
!unk(2)= By in regions R4-R5
!unk(3)= Bz in regions R4-R5
!unk(4)= p in regions R6-R7 (total pressure)
!unk is used only if Bx is different from zero and the hybrid method is used,
! otherwise there is no need for an initial guess from an approximate Riemann solver.
select case(initial_data)
case (0) !User defined
unk=0.0e0_dp
if (abs(Bx)>epsilon(Bx)) then
print *
print *,'!!!!!!!!!!!!!!!!!!WARNING!!!!!!!!!!!!!!!!!!!!!!!'
print *,'If Bx is different from zero'
print *,'you should provide a good initial guess'
print *,'taken from an approximate Riemann solver.'
print *,'If slow shocks are present then you probably need also'
print *,'to have a look at subroutines velocity (lines 230-239)'
print *,'and contact_velocity (lines 754 and 766)'
print *,'contained in postshock.f90'
print *
print *,'press enter to continue...'
read(*,*)
print *,'approximate value of the total pressure in regions R2-R3 ='
read(*,*) unk(1)
print *,'approximate value of By in regions R4-R5 ='
read(*,*) unk(2)
print *,'approximate value of Bz in regions R4-R5 ='
read(*,*) unk(3)
print *,'approximate value of the total pressure in regions R6-R7 ='
read(*,*) unk(4)
print *
print *,'Are alfven discontinuities present in the approximate solution? (y/n)'
read(*,*) alfvenwave
if (alfvenwave=='y') then
print *,'give me an approximate value for the following'
print *,'quantities behind the LEFT GOING Alfven discontinuity:'
print *,'vx='
read(*,*) leftalfven(1)
print *,'vy='
read(*,*) leftalfven(2)
print *,'vz='
read(*,*) leftalfven(3)
print *,'By='
read(*,*) leftalfven(4)
print *,'Bz='
read(*,*) leftalfven(5)
print *
print *,'give me an approximate value for the following'
print *,'quantities behind the RIGHT GOING Alfven discontinuity:'
print *,'vx='
read(*,*) rightalfven(1)
print *,'vy='
read(*,*) rightalfven(2)
print *,'vz='
read(*,*) rightalfven(3)
print *,'By='
read(*,*) rightalfven(4)
print *,'Bz='
read(*,*) rightalfven(5)
end if
end if
case (6)
unk(1)=0.292650423E+02_dp !For Komissarov: ShockTube1
unk(2)=0.0e0_dp !For Komissarov: ShockTube1
unk(3)=0.0e0_dp !For Komissarov: ShockTube1
unk(4)=0.292650423E+02_dp !For Komissarov: ShockTube1
case (7)
unk(1)=257.0e0_dp !For Komissarov: Collision
unk(2)=0.0e0_dp !For Komissarov: Collision
unk(3)=0.0e0_dp !For Komissarov: Collision
unk(4)=257.0e0_dp !For Komissarov: Collision
case (8)
unk(1)=0.698933452e0_dp !For BALSARA 1
unk(2)=-0.428492941e0_dp !For BALSARA 1
unk(3)=0.0e0_dp !For BALSARA 1
unk(4)=0.697622885e0_dp !For BALSARA 1
case (9)
unk(1)=0.232143586E+02_dp !For BALSARA 2
unk(2)=0.320517414E+01_dp !For BALSARA 2
unk(3)=0.320517414E+01_dp !For BALSARA 2
unk(4)=0.207206998E+02_dp !For BALSARA 2
case (10)
unk(1)=0.860438975E+02_dp !For BALSARA 3
unk(2)=0.466965496E+01_dp !For BALSARA 3
unk(3)=0.466965496E+01_dp !For BALSARA 3
unk(4)=0.636328552E+02_dp !For BALSARA 3
case (11)
unk(1)=1183.5e0_dp !For BALSARA 4
unk(2)=0.0e0_dp !For BALSARA 4
unk(3)=0.0e0_dp !For BALSARA 4
unk(4)=1183.5e0_dp !For BALSARA 4
case (12)
unk(1)=5.908e0_dp !For BALSARA 5
unk(2)=-1.175e0_dp !For BALSARA 5
unk(3)=0.585e0_dp !For BALSARA 5
unk(4)=5.488e0_dp !For BALSARA 5
case (13)
unk(1)=20.832e0_dp !For Generic Alfven Test
unk(2)=5.1302e0_dp !For Generic Alfven Test
unk(3)=0.7675e0_dp !For Generic Alfven Test
unk(4)=20.853e0_dp !For Generic Alfven Test
case default
!cases that don't need an intial guess from HLLE (i.e. cases in which Bx=0)
unk(1)=0.0e0_dp
unk(2)=0.0e0_dp
unk(3)=0.0e0_dp
unk(4)=0.0e0_dp
end select
open(UNIT=1223,FILE='solution.sol',STATUS='REPLACE',ACTION='WRITE')
open(UNIT=100,FILE='solution.dat',STATUS='REPLACE',ACTION='WRITE')
open(UNIT=200,FILE='funcv.dat',STATUS='REPLACE',ACTION='WRITE')
!looking for the presence of degeneracies
degen=.false.
if(.NOT.(Bx==0.0e0_dp)) then
!Check that the alfven velocities are different from contact discontinuity's velocity
!If they are equal the case is similar to Bx=0 case:
!only 3 waves: two fast waves and the tangential discontinuity
!Compute Alfven Velocities from the left state
eta=Bx*left(3)+left(6)*left(4)+left(7)*left(5)
b2=(Bx**2+left(6)**2+left(7)**2)*(1.0e0_dp-left(3)**2-left(4)**2-left(5)**2)+eta**2
call eos_enthalpy(left(2)-0.5e0_dp*b2,left(1),gamma,enthalpy)
wtot=left(1)*enthalpy + b2
!right-going
rightalfven_left=left(3)+Bx*(1.0e0_dp-left(3)**2-left(4)**2-left(5)**2)/(eta+sqrt(wtot))
!left-going
leftalfven_left=left(3)+Bx*(1.0e0_dp-left(3)**2-left(4)**2-left(5)**2)/(eta-sqrt(wtot))
!check if the alfven velocities are equal to vx
if ((abs(rightalfven_left-left(3))<epsilon(rightalfven_left)).OR.(abs(leftalfven_left-left(3))<epsilon(leftalfven_left))) &
degen=.true.
!Compute Alfven Velocities from the right state
eta=Bx*right(3)+right(6)*right(4)+right(7)*right(5)
b2=(Bx**2+right(6)**2+right(7)**2)*(1.0e0_dp-right(3)**2-right(4)**2-right(5)**2)+eta**2
call eos_enthalpy(right(2)-0.5e0_dp*b2,right(1),gamma,enthalpy)
wtot=right(1)*enthalpy + b2
!right-going
rightalfven_right=right(3)+Bx*(1.0e0_dp-right(3)**2-right(4)**2-right(5)**2)/(eta+sqrt(wtot))
!left-going
leftalfven_right=right(3)+Bx*(1.0e0_dp-right(3)**2-right(4)**2-right(5)**2)/(eta-sqrt(wtot))
!check if the alfven velocities are equal to vx
if ((abs(rightalfven_right-right(3))<epsilon(rightalfven_right)).OR.(abs(leftalfven_right-right(3))<epsilon(leftalfven_right))) &
degen=.true.
call xi(left ,'LF',dump,all_left) !compute slow and fast eigenvalues from the left state
call xi(right,'LF',dump,all_right)!compute slow and fast eigenvalues from the right state
!EXPERIMENTAL: this is true in cases such as ShockTube1 of Komissarov,
! but still need to be tested in other cases
if ((abs(leftalfven_left-all_left(1))<=small_vel).AND.&
(abs(rightalfven_left-all_left(4))<=small_vel)) then
print *,'degeneracy ok kind 3, only SLOW WAVES'
degen_case=3
if (verbose) then
print *,'left and right-going alfven velocity and vx at the left state'
print *,leftalfven_left,rightalfven_left,left(3)
print *,'left and right-going alfven velocity and vx at the right state'
print *,leftalfven_right,rightalfven_right,right(3)
print *,'fast and slow characteristic velocities from the left state'
print *,all_left
print *,'fast and slow characteristic velocities from the right state'
print *,all_right
end if
goto 3
else if ((abs(leftalfven_right-all_right(1))<=small_vel).AND.&
(abs(rightalfven_right-all_right(4))<=small_vel)) then
print *,'degeneracy ok kind 3, only SLOW WAVES'
degen_case=3
if (verbose) then
print *,'left and right-going alfven velocity and vx at the left state'
print *,leftalfven_left,rightalfven_left,left(3)
print *,'left and right-going alfven velocity and vx at the right state'
print *,leftalfven_right,rightalfven_right,right(3)
print *,'fast and slow characteristic velocities from the left state'
print *,all_left
print *,'fast and slow characteristic velocities from the right state'
print *,all_right
end if
goto 3
else if ((abs(leftalfven_left-all_left(2))<=small_vel).AND.&
(abs(rightalfven_left-all_left(3))<=small_vel)) then
print *,'degeneracy ok kind 2, only FAST WAVES'
degen_case=2
degen=.true.
goto 3
else if ((abs(leftalfven_right-all_right(2))<=small_vel).AND.&
(abs(rightalfven_right-all_right(3))<=small_vel)) then
print *,'degeneracy ok kind 2, only FAST WAVES'
degen_case=2
degen=.true.
goto 3
end if
end if
!Find the solution at the contact discontinuity
3 if ((Bx==0.0e0_dp).OR.(degen)) then
if (degen) then
print *,'-----------------------'
print *,'-----------------------'
print *,'NO DIFFERENCE WITH Bx=0'
if (verbose) then
print *,'left and right-going alfven velocity and vx at the left state'
print *,leftalfven_left,rightalfven_left,left(3)
print *,'left and right-going alfven velocity and vx at the right state'
print *,leftalfven_right,rightalfven_right,right(3)
print *,'fast and slow characteristic velocities from the left state'
print *,all_left
print *,'fast and slow characteristic velocities from the right state'
print *,all_right
end if
print *,'-----------------------'
print *,'-----------------------'
end if
!Only two fast waves and a tangential discontinuity
write(200,*) '#iteration, [[vx]] at the TD'
write(200,*) !blank line
call contact(left,right,pb,VsLeft,VsRight,solution,0) !p-method
fullsolution=0.0e0_dp
fullsolution(:,3)=solution(:,1)
fullsolution(:,4)=solution(:,2)
VsLv3=VsLeft
VsRv3=VsRight
unk=pb
else if (degen_case==0) then
!All the waves are present:
!two fast and two slow waves,
!two Alfven discontinuities and a contact discontinuity
write(200,*) '#iteration, [[vx]],[[vy]],[[vz]],[[By]],[[Bz]],[[p]] at the CD'
write(200,*) !blank line
call fullcontact(left,right,unk,VsLv3,VsRv3,fullsolution) !hybrid method
else if (degen_case==2) then !Only fast waves
write(200,*) '#iteration, [[vx]] at the TD'
write(200,*) !blank line
call contact(left,right,pb,VsLeft,VsRight,solution,degen_case) !p-method
fullsolution=0.0e0_dp
fullsolution(:,3)=solution(:,1)
fullsolution(:,4)=solution(:,2)
VsLv3=VsLeft
VsRv3=VsRight
else if (degen_case==3) then !Only slow waves
write(200,*) '#iteration, [[vx]] at the TD'
write(200,*) !blank line
call contact(left,right,pb,VsLeft,VsRight,solution,degen_case) !p-method
fullsolution=0.0e0_dp
fullsolution(:,3)=solution(:,1)
fullsolution(:,4)=solution(:,2)
VsLv3=VsLeft
VsRv3=VsRight
else
stop 'riemann_rmhd.f90: riemann: error in degen_case'
end if
write(1223,*)
write(1223,*)
write(1223,*)
write(1223,*) 'Exact solution found with accuracy',accuracy
write(1223,*) 'Initial Condition = ',init
write(1223,*) 'left boundary (x1) = ',x1
write(1223,*) 'right boundary (x2) = ',x2
write(1223,*) 'time = ',t
write(1223,*) 'number of points (nx) = ',nx
write(1223,*)
write(1223,*) 'Bx = ',Bx
write(1223,*) 'EOS Gamma = ',gamma
write(1223,*)
write(1223,*)
write(1223,*) 'SOLUTION (at the LEFT side of the CD)'
write(1223,*) 'rho = ',fullsolution(1,3)
write(1223,*) 'p = ',fullsolution(2,3)
write(1223,*) 'vx = ',fullsolution(3,3)
write(1223,*) 'vy = ',fullsolution(4,3)
write(1223,*) 'vz = ',fullsolution(5,3)
write(1223,*) 'By = ',fullsolution(6,3)
write(1223,*) 'Bz = ',fullsolution(7,3)
write(1223,*)
write(1223,*) 'SOLUTION (at the RIGHT side of the CD)'
write(1223,*) 'rho = ',fullsolution(1,4)
write(1223,*) 'p = ',fullsolution(2,4)
write(1223,*) 'vx = ',fullsolution(3,4)
write(1223,*) 'vy = ',fullsolution(4,4)
write(1223,*) 'vz = ',fullsolution(5,4)
write(1223,*) 'By = ',fullsolution(6,4)
write(1223,*) 'Bz = ',fullsolution(7,4)
write(1223,*)
!write the exact values obtained in the different regions
open(UNIT=4000,FILE='exact.sol',STATUS='REPLACE',ACTION='WRITE')
write(4000,*) '#rho, total pressure p, vx, vy, vz, By, Bz'
write(4000,*)
if ((Bx==0.0e0_dp).OR.(degen)) then
write(4000,'(7E20.9)') left
write(4000,'(7E20.9)') fullsolution(:,3)
write(4000,'(7E20.9)') fullsolution(:,4)
write(4000,'(7E20.9)') right
else
write(4000,'(7E20.9)') left
write(4000,'(7E20.9)') fullsolution(:,1)
write(4000,'(7E20.9)') fullsolution(:,2)
write(4000,'(7E20.9)') fullsolution(:,3)
write(4000,'(7E20.9)') fullsolution(:,4)
write(4000,'(7E20.9)') fullsolution(:,5)
write(4000,'(7E20.9)') fullsolution(:,6)
write(4000,'(7E20.9)') right
end if
close(4000)
!Produce the output on a non-uniform grid.
! This is due to the fact that we want to draw the exact positions of shocks.
!The number of points used can be slightly larger than the number of points nx chosen by the user
call output(x1,x2,t,nx,left,right,VsLv3,VsRv3,unk,fullsolution)
close(100)
close(200)
close(1223)
print *
print *,'- The eight states of the exact solution are written in file ''exact.sol'''
print *
print *,'- The solution is written in file ''solution.dat'' AT EACH ITERATION. This means that the exact solution computed with the desired accuracy is written at the end of the file.'
print *
print *,'- The jumps at the CD of vx,vy,vz,By,Bz and p at each iteration are saved in ''funcv.dat'''
print *
print *,'- The values at the contact discontinuity and the waves'' velocities are saved in ''solution.sol'' at each iteration'
print *
end program riemann_rmhd
!---------------------------------------------------------------!
!-------------------------CONTACT-------------------------------!
!---------------------------------------------------------------!
subroutine contact(left,right,pb,VsL,VsR,solution,degen_case)
use type
use global
use accmod !This module contains accuracy
use interfaces,only:funcd,funcv
implicit none
real(DP),dimension(7),intent(IN)::left,right
real(DP),intent(OUT)::pb
real(DP),intent(OUT)::VsL,VsR
real(DP),dimension(7,2),intent(OUT)::solution
integer(I4B),intent(IN)::degen_case
!This subroutine solves the riemann problem at the contact discontinuity using the p-method
integer(I4B)::j,ILOOP
real(DP)::temp,dpb,tolx,d,relax,pmin,pmax,fmin,fmax,check
real(DP)::p,f,df,xl,xh,dpbold
character(len=2)::left_wave,right_wave
tolx=epsilon(pb)
relax=1.0e0_dp !relaxation factor
solution=0.0e0_dp
!Are we considering a case with Bx different from zero but only one kind of waves,
! i.e. only fast or slow waves?
if ((degen_case==0).OR.(degen_case==1)) then
left_wave='LF'
right_wave='RF'
else if (degen_case==2) then
left_wave='LF'
right_wave='RF'
else if (degen_case==3) then
left_wave='LS'
right_wave='RS'
end if
!Braketing the function
pmin=0.5e0_dp*(left(2) + right(2))
pmax=pmin
print *
print *,'Bracketing the solution...'
print *
!----------ONLY FOR DEBUG--------------------------!
if(.false.) then
open(UNIT=12001,FILE='funcv_debug.dat',STATUS='REPLACE',ACTION='WRITE')
pmin=1.0e-15
pmax=1.0e-10
dpbold=(pmax-pmin)/1001
p=pmin
do j=1,1001
call funcv(left,right,p,VsL,VsR,fmin,left_wave,right_wave)
write(12001,*)p,fmin
p=p+dpbold
end do
CLOSE(12001)
STOP 'data plotted in funcv_debug.dat'
end if
!----------ONLY FOR DEBUG--------------------------!
do
ILOOP = ILOOP + 1
call funcv(left,right,pmin,VsL,VsR,fmin,left_wave,right_wave)
call funcv(left,right,pmax,VsL,VsR,fmax,left_wave,right_wave)
if ((abs(fmin)<accuracy).and.(abs(fmin)>0)) then !check abs(fmin)>0 to avoid NaN
!We have the solution
print *,'pmin=',pmin,'fmin=',fmin
pb=pmin
goto 1000
end if
if ((abs(fmax)<accuracy).and.(abs(fmax)>0)) then !check abs(fmax)>0 to avoid NaN
!We have the solution
print *,'pmax=',pmax,'fmax=',fmax
pb=pmax
goto 1000
end if
CHECK = fmin*fmax
print *,'p1=',pmin,'p2=',pmax
print *,'f(p1)=',fmin,'f(p2)=',fmax
print *,'f(p1)*f(p2)=',CHECK
IF (CHECK<0.0e0_dp) GOTO 5 !the root is in the interval (pmin,pmax)
!The function does not change sign in this interval, move the boundaries
if (pmin>1e-15_dp) pmin = 0.9e0_dp*pmin
pmax = 1.1e0_dp*pmax
end do
5 if (fmin < 0.0) then !Orient the search so that f(xl)<0.
xl=pmin
xh=pmax
else
xh=pmin
xl=pmax
end if
print * !blank line
!Find the pressure using the continuity of v^x at the contact discontinuity
!Use Newton-Raphson and bisection method
pb=0.5e0_dp*(pmin+pmax)
dpbold=abs(pmax-pmin)
dpb=dpbold
call funcd(left,right,pb,VsL,VsR,f,df,left_wave,right_wave)
if (f==0.0e0_dp) goto 1000
do j=1,200 !the maximum number of steps is 200
if (((pb-xh)*df-f)*((pb-xl)*df-f) > 0.0 .or. &
abs(2.0_DP*f) > abs(dpbold*df) ) then
!Bisect if Newton out of range, or not decreasing fast enough.
dpbold=dpb
dpb=0.5e0_dp*(xh-xl)
pb=xl+dpb
if (xl == pb) goto 1000 !Change in root is negligible.
else !Newton step acceptable. Take it.
dpbold=dpb
dpb=f/df
temp=pb
pb=pb-dpb
if (temp==pb) goto 1000
end if
if (abs(dpb)<tolx) goto 1000 !Convergence
call funcd(left,right,pb,VsL,VsR,f,df,left_wave,right_wave)!One new function evaluation per iteration.
print *
print *,'pb=', pb,'f=',f,'desired accuracy=',accuracy
print *
niter=j !number of iterations
write(200,*) j,f
if (abs(f)<accuracy) goto 1000 !Convergence
if (f < 0.0) then !Maintain the bracket on the root.
xl=pb
else
xh=pb
end if
end do
print *, 'I cannot find the post-shock pressure'
print *, 'Try to reduce the accuracy'
stop
1000 call funcv(left,right,pb,VsL,VsR,f,left_wave,right_wave,solution)
print *
print *,'left side of TD',solution(:,1)
print *,'right side of TD',solution(:,2)
print *
end subroutine contact
!---------------------------------------------------------------!
!-----------------------FULLCONTACT-----------------------------!
!---------------------------------------------------------------!
subroutine fullcontact(left,right,unk,VsLv3,VsRv3,fullsolution)
use type
use global !This module contains Bx, the EOS gamma, degen, niter and solmethod
use accmod!This module contains accuracy
use interfaces, only:fullfuncd,fullfuncv,ludcmp,lubksb
use wavecheck
use odeswitch
use output_grid !This module contains t,x1,x2,nx
implicit none
real(DP),dimension(7),intent(IN)::left,right
real(DP),dimension(4),intent(INOUT)::unk
real(DP),dimension(3),intent(OUT)::VsLv3,VsRv3
real(DP),dimension(7,6),intent(OUT)::fullsolution
!Given the left and right states, this subroutine finds the solution at
!the contact discontinuity in the case Bx different from 0.
!It uses the pressure as an unknown between the fast and slow waves, and
! the tangential components of the magnetic field between the slow waves (i.e.
! at the contact discontinuity)
!unk(1)= p in regions R2-R3 (total pressure)
!unk(2)= By in regions R4-R5
!unk(3)= Bz in regions R4-R5
!unk(4)= p in regions R6-R7 (total pressure)
integer(I4B) :: i,ntrial
real(DP) :: d,tolx,tolf
real(DP), dimension(size(unk)) :: fvec,p
integer(I4B),dimension(size(unk)) ::indx
real(DP), dimension(size(unk),size(unk)) :: fjac
real(DP), parameter :: EPS=epsilon(x1)
real(DP)::relax
ntrial=200 !maximum number of steps
tolx=epsilon(unk)
tolf=accuracy
niter=0
!-------------------------------------------------------------------!
!Check that the function is defined in the first point given by unk !
!-------------------------------------------------------------------!
shock=.false.
wave_error=.false.
call fullfuncv(left,right,unk,VsLv3,VsRv3,fvec,fullsolution)
!Test if the starting point is a valid one
if (wave_error) stop 'riemann.f90: fullcontact: chose a different initial guess. This isn''t valid!'
!Plot the results
call output(x1,x2,t,nx,left,right,VsLv3,VsRv3,unk,fullsolution)
if (maxval(abs(fvec)) <= tolf) goto 1000 !Check function convergence.
relax=1.0 !RELAXATION FACTOR
!compute the Jacobian
call fullfuncd(left,right,unk,VsLv3,VsRv3,fvec,fjac)
do i=1,ntrial
!The subroutine fullfuncd has supplied function values at unk in fvec and Jacobian
! matrix in fjac.
niter=niter+1 !iterations counter
if (verbose) then
print *,'unk=',unk
print *,'fvec=',fvec
print *,'fjac(1,:)=',fjac(1,:)
print *,'fjac(2,:)=',fjac(2,:)
print *,'fjac(3,:)=',fjac(3,:)
print *,'fjac(4,:)=',fjac(4,:)
print *
end if
!Newton-Raphson routine
if (maxval(abs(fvec)) <= tolf) goto 1000 !Check function convergence.
p=-fvec !Right-hand side of linear equations.
!NR routine
!Solve linear equations using LU decomposition.
call ludcmp(fjac,indx,d)
call lubksb(fjac,indx,p)
p=p*relax !use relaxation factor
unk=unk+p !Update solution.
if (sum(abs(p)) <= tolx) goto 1000 !Check root convergence.
if (verbose) then
print *
print *,'++++ These are the values suggested by the algorithm ++++'
print *,'[ptot(2), By(4), Bz(4), ptot(7)]=',unk
end if
!---------------------------------------------------------------------!
!Tring to avoid negative values for the pressure
if(unk(1)<=1.0e-4_dp) unk(1)=1.0e-4_dp
if(unk(4)<=1.0e-4_dp) unk(4)=1.0e-4_dp
!---------------------------------------------------------------------!
if (verbose) then
print *, 'and these are the values I will use:'
print *,'[ptot(2), By(4), Bz(4), ptot(7)]=',unk
print *
end if
shock=.false.
wave_error=.false.
print *,'---------------------------'
print *,'ITERATION END'
print *,'desired accuracy=',tolf
print *,'fvec =',fvec
print *,'new guess=',unk
print *,'********************************************************'
print *,'# of iterations=',niter
!Compute fvec and its Jacobian with the new guess
call fullfuncd(left,right,unk,VsLv3,VsRv3,fvec,fjac,fullsolution)
!----------------------------------------------!
!At the end of each iteration, plot the results!
!----------------------------------------------!
call output(x1,x2,t,nx,left,right,VsLv3,VsRv3,unk,fullsolution)
end do
print *, 'riemann.f90: fullcontact: unable to find the solution'
print *, 'try to reduce the accuracy'
stop
1000 print *,'***********************************************************'
print *,'accuracy',tolf,'reached!'
print *,'number of iterations=',niter
print *,'fvec =',fvec
print *,'unknowns=',unk
print *
print *,'Computing the fullsolution...'
print *
call fullfuncv(left,right,unk,VsLv3,VsRv3,fvec,fullsolution)
end subroutine fullcontact
!---------------------------------------------------------------!
!---------------------------FUNCD-------------------------------!
!---------------------------------------------------------------!
subroutine funcd(left,right,pb,VsL,VsR,f,df,left_wave,right_wave)
use type
use wavecheck
use interfaces,only: funcv
implicit none
real(DP),dimension(7),intent(IN)::left,right
real(DP),intent(IN)::pb
real(DP),intent(OUT)::VsL,VsR
real(DP),intent(OUT)::f,df
character(len=2),intent(IN)::left_wave,right_wave
!This subroutine computes the first derivative of f
real(DP)::f2,dpb,dumpVsL,dumpVsR
call funcv(left,right,pb,VsL,VsR,f,left_wave,right_wave)
dpb=1.0D-5*abs(pb)
if (dpb==0.0e0_dp) dpb=1.0D-5
call funcv(left,right,pb+dpb,dumpVsL,dumpVsR,f2,left_wave,right_wave)
!Compute the first derivative
df=(f2-f)/dpb
end subroutine funcd
!---------------------------------------------------------------!
!---------------------------FUNCV-------------------------------!
!---------------------------------------------------------------!
subroutine funcv(left,right,pb,VsL,VsR,f,left_wave,right_wave,solution)
use type
use global
use wavecheck
use interfaces,only:Rarefaction_pressure,ContactVelocity
implicit none
real(DP),dimension(7),intent(IN)::left,right
real(DP),intent(IN)::pb
real(DP),intent(OUT)::VsL,VsR
real(DP),intent(OUT)::f
character(len=2),intent(IN)::left_wave,right_wave
real(DP),dimension(7,2),intent(OUT),OPTIONAL::solution
!This subroutine computes the jump in vx at the tangential discontinuity
real(DP),dimension(7)::solutionL,solutionR
real(DP)::vx1,vx2,dump
if (pb>left(2)) then
!Shock
call ContactVelocity(pb,0.0e0_dp,0.0e0_dp,VsL,left,dump,left_wave,'P',solutionL)
if (wave_error) stop 'funcv: error solving the LF shock equations'
elseif (pb==left(2)) then
solutionL=left
else
!Rarefaction
call Rarefaction_pressure(pb,VsL,left,solutionL,left_wave)
if (wave_error) stop 'funcv: error solving the LF rarefaction equations'
end if
vx1=solutionL(3)!vx at the left of the TD
if (pb>right(2)) then
!Shock
call ContactVelocity(pb,0.0e0_dp,0.0e0_dp,VsR,right,dump,right_wave,'P',solutionR)
if (wave_error) stop 'funcv: error solving the RF shock equation'
elseif (pb==right(2)) then
solutionR=right
else
!Rarefaction
call Rarefaction_pressure(pb,VsR,right,solutionR,right_wave)
if (wave_error) stop 'funcv: error solving the LF rarefaction equations'
end if
vx2=solutionR(3)!vx at the right of the TD
f=vx1-vx2 !This is zero if the solution is exact
if (present(solution)) then
solution(:,1)=solutionL
solution(:,2)=solutionR
!output the accuracy f reached at iteration niter to funcv.dat
write(200,'(I3,2E20.8)') niter,f
end if
end subroutine funcv
!---------------------------------------------------------------!
!-------------------------FULLFUNCD-----------------------------!
!---------------------------------------------------------------!
subroutine fullfuncd(left,right,unk,VsLv3,VsRv3,fvec,fjac,fullsolution)
use type
use interfaces,only:fullfuncv
use wavecheck
use global !It contains verbo
implicit none
real(DP),dimension(7),intent(IN)::left,right
real(DP),dimension(4),intent(IN)::unk
real(DP),dimension(3),intent(OUT)::VsLv3,VsRv3
real(DP),dimension(4),intent(OUT)::fvec
real(DP),dimension(4,4),intent(OUT)::fjac
real(DP),dimension(7,6),intent(OUT),OPTIONAL::fullsolution
!Compute the Jacobian of fvec
real(DP):: EPS
real(DP),dimension(4)::x,xsav,xph,h,fvec2,dumpv3,dumpv3b
integer(I4B)::j
EPS=1.0D-5
if(present(fullsolution)) then
call fullfuncv(left,right,unk,VsLv3,VsRv3,fvec,fullsolution)
else
call fullfuncv(left,right,unk,VsLv3,VsRv3,fvec)
end if
if (wave_error) return !The function is not defined in these points
!Now computes the Jacobian fjac
x=unk
xsav=x
h=EPS*abs(xsav)
where (h == 0.0) h=EPS
xph=xsav+h !Trick to reduce finite precision error.
h=xph-xsav
do j=1,4
x(j)=xph(j)
call fullfuncv(left,right,x,dumpv3,dumpv3b,fvec2)
if (wave_error) stop 'fullfuncd: error computing the Jacobian matrix'
fjac(:,j)=(fvec2(:)-fvec(:))/h(j) !Forward difference formula
x(j)=xsav(j)
end do
end subroutine fullfuncd
!---------------------------------------------------------------!
!-------------------------FULLFUNCV-----------------------------!
!---------------------------------------------------------------!
subroutine fullfuncv(left,right,unk,VsLv3,VsRv3,fvec,fullsolution)
use type
use global !This module contains Bx and gamma (the EOS' gamma)
use interfaces,only: velocity,Rarefaction,alfven,eos_enthalpy
use interfaces,only: Rarefaction_pressure,ContactVelocity
use wavecheck !This module contains wave_check
use accmod !It contains accuracy
use odeswitch !It contains switch
use alfven_wave !guess for the alfven discontinuities (if present)
implicit none
real(DP),dimension(7),intent(IN)::left,right
real(DP),dimension(4),intent(IN)::unk
real(DP),dimension(3),intent(OUT)::VsLv3,VsRv3
real(DP),dimension(4),intent(OUT)::fvec
real(DP),dimension(7,6),intent(OUT),OPTIONAL::fullsolution
!compute vx, vy, vz and p at the left and right boundaries of the
!Contact Discontinuity; then takes the differences which should be zero
real(DP),dimension(7)::zone1a,zone1b,zone2a,zone2b,zone3a,zone3b,zonedump
real(DP)::vx1,vy1,vz1,ptot1,vx2,vy2,vz2,ptot2,vxb,B2big,eta,W2inv,b2,wtot,normB,psi,dump,enthalpy
real(DP),parameter::small_psi=1.0e-4_dp
wave_error=.false.
!LEFT GOING FAST WAVE
if(verbose) then
print *
print *,'--------------------------------------'
print *,'I''m solving equations for the'
print *,'Left Going Fast Wave'
print *,'--------------------------------------'
print *
end if
!Is it a shock?
!If it's a shock pressure increases
if (abs(unk(1)-left(2))<epsilon(unk(1))) then
if (verbose) print *,'no shock or rarefaction!'
zone1a=left
goto 10
end if
if (unk(1)>=left(2)) then
!SHOCK
if (verbose) print *,'It''s a shock!'
!compute the velocity of the left going fast shock
call ContactVelocity(unk(1),0.0e0_dp,0.0e0_dp,VsLv3(1),left,dump,'LF','P',zone1a)
if (wave_error) stop 'fullfuncv: error in the left-going fast shock'
else
!RAREFACTION
if (verbose) print *,'It''s a rarefaction!'
call Rarefaction_pressure(unk(1),VsLv3(1),left,zone1a,'LF')
if (wave_error) stop 'fullfuncv: error in the left-going fast rarefaction'
end if
!LEFT GOING ALFVEN DISCONTINUITY
10 if (verbose) then
print *
print *,'--------------------------------------'
print *,'I''m solving equations for the'
print *,'Left Going Alfven Wave'
print *,'--------------------------------------'
print *
end if
!Alfven velocity
B2big=Bx**2+zone1a(6)**2+zone1a(7)**2 !Bx**2+By**2+Bz**2
eta=Bx*zone1a(3)+zone1a(6)*zone1a(4)+zone1a(7)*zone1a(5) !Bx*vx+By*vy+Bz*vz
W2inv=1.0e0_dp-zone1a(3)**2-zone1a(4)**2-zone1a(5)**2 !Inverse of squared Lorentz factor
b2=B2big*W2inv+eta**2 !b_mu b^mu
call eos_enthalpy(zone1a(2)-0.5e0_dp*b2,zone1a(1),gamma,enthalpy)
wtot=zone1a(1)*enthalpy + b2 !Total relativistic enthalpy
VsLv3(2)= zone1a(3)+Bx*W2inv/(eta-sqrt(wtot))!Alfven velocity = vx-Bx/(W**2*(sqrt(wtot)-eta))
!First guess for the Alfven subroutine
zone1b=zone1a
if (initial_data==13) then
zone1b(3)= 0.0710516 !for the generic Alfven test (LAW)
zone1b(4)= 0.36694 !for the generic Alfven test (LAW)
zone1b(5)= 0.242851 !for the generic Alfven test (LAW)
zone1b(6)= 5.691 !for the generic Alfven test (LAW)
zone1b(7)= 0.850178 !for the generic Alfven test (LAW)