-
Notifications
You must be signed in to change notification settings - Fork 6
/
helmholtz.f90
1459 lines (1144 loc) · 44.4 KB
/
helmholtz.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
program teos
include 'implno.dek'
include 'vector_eos.dek'
! tests the eos routine
!
! ionmax = number of isotopes in the network
! xmass = mass fraction of isotope i
! aion = number of nucleons in isotope i
! zion = number of protons in isotope i
integer ionmax
parameter (ionmax=3)
double precision xmass(ionmax),aion(ionmax),zion(ionmax),temp,den,abar,zbar
! set the mass fractions, z's and a's of the composition
! hydrogen, helium, and carbon
xmass(1) = 0.75d0 ; aion(1) = 1.0d0 ; zion(1) = 1.0d0
xmass(2) = 0.23d0 ; aion(2) = 4.0d0 ; zion(2) = 2.0d0
xmass(3) = 0.02d0 ; aion(3) = 12.0d0 ; zion(3) = 6.0d0
! average atomic weight and charge
abar = 1.0d0/sum(xmass(1:ionmax)/aion(1:ionmax))
zbar = abar * sum(xmass(1:ionmax) * zion(1:ionmax)/aion(1:ionmax))
! set the input vector. pipeline is only 1 element long in this example
temp_row(1) = 1.0d8 ; den_row(1) = 1.0d6 ; abar_row(1) = abar ; zbar_row(1) = zbar
jlo_eos = 1 ; jhi_eos = 1
! read the helmholtz free energy data table - only once
call read_helm_table
! call the eos
call helmeos
! write out the results
call pretty_eos_out('helm: ')
end
! here is the tabular helmholtz free energy eos:
!
! routine read_helm_table reads an electron helm free energy table
! routine helmeos computes the pressure, energy and entropy via tables
subroutine read_helm_table
include 'implno.dek'
include 'helm_table_storage.dek'
! this routine reads the helmholtz eos file, and
! must be called once before the helmeos routine is invoked.
! declare local variables
integer i,j
double precision tsav,dsav,dth,dt2,dti,dt2i,dt3i, &
dd,dd2,ddi,dd2i,dd3i
! open the file (use softlinks to input the desired table)
open(unit=19,file=TBLPATH,status='old')
! for standard table limits
tlo = 3.0d0
thi = 13.0d0
tstp = (thi - tlo)/float(jmax-1)
tstpi = 1.0d0/tstp
dlo = -12.0d0
dhi = 15.0d0
dstp = (dhi - dlo)/float(imax-1)
dstpi = 1.0d0/dstp
! read the helmholtz free energy and its derivatives
do j=1,jmax
tsav = tlo + (j-1)*tstp
t(j) = 10.0d0**(tsav)
do i=1,imax
dsav = dlo + (i-1)*dstp
d(i) = 10.0d0**(dsav)
read(19,*) f(i,j),fd(i,j),ft(i,j),fdd(i,j),ftt(i,j),fdt(i,j), &
fddt(i,j),fdtt(i,j),fddtt(i,j)
enddo
enddo
! write(6,*) 'read main table'
! read the pressure derivative with density table
do j=1,jmax
do i=1,imax
read(19,*) dpdf(i,j),dpdfd(i,j),dpdft(i,j),dpdfdt(i,j)
enddo
enddo
! write(6,*) 'read dpdd table'
! read the electron chemical potential table
do j=1,jmax
do i=1,imax
read(19,*) ef(i,j),efd(i,j),eft(i,j),efdt(i,j)
enddo
enddo
! write(6,*) 'read eta table'
! read the number density table
do j=1,jmax
do i=1,imax
read(19,*) xf(i,j),xfd(i,j),xft(i,j),xfdt(i,j)
enddo
enddo
! write(6,*) 'read xne table'
! close the file
close(unit=19)
! construct the temperature and density deltas and their inverses
do j=1,jmax-1
dth = t(j+1) - t(j)
dt2 = dth * dth
dti = 1.0d0/dth
dt2i = 1.0d0/dt2
dt3i = dt2i*dti
dt_sav(j) = dth
dt2_sav(j) = dt2
dti_sav(j) = dti
dt2i_sav(j) = dt2i
dt3i_sav(j) = dt3i
end do
do i=1,imax-1
dd = d(i+1) - d(i)
dd2 = dd * dd
ddi = 1.0d0/dd
dd2i = 1.0d0/dd2
dd3i = dd2i*ddi
dd_sav(i) = dd
dd2_sav(i) = dd2
ddi_sav(i) = ddi
dd2i_sav(i) = dd2i
dd3i_sav(i) = dd3i
enddo
! write(6,*)
! write(6,*) 'finished reading eos table'
! write(6,04) 'imax=',imax,' jmax=',jmax
!04 format(1x,4(a,i4))
! write(6,03) 'temp(1) =',t(1),' temp(jmax) =',t(jmax)
! write(6,03) 'ye*den(1) =',d(1),' ye*den(imax) =',d(imax)
!03 format(1x,4(a,1pe11.3))
! write(6,*)
return
end
subroutine read_helm_iontable
include 'implno.dek'
include 'helm_table_storage.dek'
! this routine reads the helmholtz eos file, and
! must be called once before the helmeos routine is invoked.
! declare local variables
integer i,j
double precision tsav,dsav,dth,dt2,dti,dt2i,dt3i, &
dd,dd2,ddi,dd2i,dd3i
! open the file (use softlinks to input the desired table)
open(unit=19,file='helm_iontable.dat',status='old')
! for the standard table
tion_lo = 3.0d0
tion_hi = 13.0d0
tion_stp = (thi - tlo)/float(jmax-1)
tion_stpi = 1.0d0/tstp
dion_lo = -12.0d0
dion_hi = 15.0d0
dion_stp = (dhi - dlo)/float(imax-1)
dion_stpi = 1.0d0/dstp
! read the helmholtz free energy and its derivatives
do j=1,jmax
tsav = tion_lo + (j-1)*tion_stp
tion(j) = 10.0d0**(tsav)
do i=1,imax
dsav = dion_lo + (i-1)*dion_stp
dion(i) = 10.0d0**(dsav)
read(19,*) fion(i,j),fiond(i,j),fiont(i,j),fiondd(i,j), &
fiontt(i,j),fiondt(i,j),fionddt(i,j),fiondtt(i,j), &
fionddtt(i,j)
enddo
enddo
! read the pressure derivative with density table
do j=1,jmax
do i=1,imax
read(19,*) dpiondf(i,j),dpiondfd(i,j), &
dpiondft(i,j),dpiondfdt(i,j)
enddo
enddo
! read the electron chemical potential table
do j=1,jmax
do i=1,imax
read(19,*) efion(i,j),efiond(i,j),efiont(i,j),efiondt(i,j)
enddo
enddo
! read the number density table
do j=1,jmax
do i=1,imax
read(19,*) xfion(i,j),xfiond(i,j),xfiont(i,j),xfiondt(i,j)
enddo
enddo
! close the file
close(unit=19)
! construct the temperature and density deltas and their inverses
do j=1,jmax-1
dth = t(j+1) - t(j)
dt2 = dth * dth
dti = 1.0d0/dth
dt2i = 1.0d0/dt2
dt3i = dt2i*dti
dt_sav_ion(j) = dth
dt2_sav_ion(j) = dt2
dti_sav_ion(j) = dti
dt2i_sav_ion(j) = dt2i
dt3i_sav_ion(j) = dt3i
end do
do i=1,imax-1
dd = d(i+1) - d(i)
dd2 = dd * dd
ddi = 1.0d0/dd
dd2i = 1.0d0/dd2
dd3i = dd2i*ddi
dd_sav_ion(i) = dd
dd2_sav_ion(i) = dd2
ddi_sav_ion(i) = ddi
dd2i_sav_ion(i) = dd2i
dd3i_sav_ion(i) = dd3i
enddo
! write(6,*)
! write(6,*) 'finished reading eos ion table'
! write(6,04) 'imax=',imax,' jmax=',jmax
!04 format(1x,4(a,i4))
! write(6,03) 'temp(1) =',tion(1),' temp(jmax) =',tion(jmax)
! write(6,03) 'ytot*den(1) =',dion(1),' ytot*den(imax) =',dion(imax)
!03 format(1x,4(a,1pe11.3))
! write(6,*)
return
end
subroutine helmeos
include 'implno.dek'
include 'const.dek'
include 'vector_eos.dek'
include 'helm_table_storage.dek'
! given a temperature temp [K], density den [g/cm**3], and a composition
! characterized by abar and zbar, this routine returns most of the other
! thermodynamic quantities. of prime interest is the pressure [erg/cm**3],
! specific thermal energy [erg/gr], the entropy [erg/g/K], along with
! their derivatives with respect to temperature, density, abar, and zbar.
! other quantites such the normalized chemical potential eta (plus its
! derivatives), number density of electrons and positron pair (along
! with their derivatives), adiabatic indices, specific heats, and
! relativistically correct sound speed are also returned.
!
! this routine assumes planckian photons, an ideal gas of ions,
! and an electron-positron gas with an arbitrary degree of relativity
! and degeneracy. interpolation in a table of the helmholtz free energy
! is used to return the electron-positron thermodynamic quantities.
! all other derivatives are analytic.
!
! references: cox & giuli chapter 24 ; timmes & swesty apj 1999
! declare
integer i,j
double precision temp,den,abar,zbar,ytot1,ye, &
x,y,zz,zzi,deni,tempi,xni,dxnidd,dxnida, &
dpepdt,dpepdd,deepdt,deepdd,dsepdd,dsepdt, &
dpraddd,dpraddt,deraddd,deraddt,dpiondd,dpiondt, &
deiondd,deiondt,dsraddd,dsraddt,dsiondd,dsiondt, &
dse,dpe,dsp,kt,ktinv,prad,erad,srad,pion,eion, &
sion,xnem,pele,eele,sele,pres,ener,entr,dpresdd, &
dpresdt,denerdd,denerdt,dentrdd,dentrdt,cv,cp, &
gam1,gam2,gam3,chit,chid,nabad,sound,etaele, &
detadt,detadd,xnefer,dxnedt,dxnedd,s
double precision pgas,dpgasdd,dpgasdt,dpgasda,dpgasdz, &
egas,degasdd,degasdt,degasda,degasdz, &
sgas,dsgasdd,dsgasdt,dsgasda,dsgasdz, &
cv_gas,cp_gas,gam1_gas,gam2_gas,gam3_gas, &
chit_gas,chid_gas,nabad_gas,sound_gas
double precision sioncon,forth,forpi,kergavo,ikavo,asoli3,light2
parameter (sioncon = (2.0d0 * pi * amu * kerg)/(h*h), &
forth = 4.0d0/3.0d0, &
forpi = 4.0d0 * pi, &
kergavo = kerg * avo, &
ikavo = 1.0d0/kergavo, &
asoli3 = asol/3.0d0, &
light2 = clight * clight)
! for the abar derivatives
double precision dpradda,deradda,dsradda, &
dpionda,deionda,dsionda, &
dpepda,deepda,dsepda, &
dpresda,denerda,dentrda, &
detada,dxneda
! for the zbar derivatives
double precision dpraddz,deraddz,dsraddz, &
dpiondz,deiondz,dsiondz, &
dpepdz,deepdz,dsepdz, &
dpresdz,denerdz,dentrdz, &
detadz,dxnedz
! for the interpolations
integer iat,jat
double precision free,df_d,df_t,df_dd,df_tt,df_dt
double precision xt,xd,mxt,mxd, &
si0t,si1t,si2t,si0mt,si1mt,si2mt, &
si0d,si1d,si2d,si0md,si1md,si2md, &
dsi0t,dsi1t,dsi2t,dsi0mt,dsi1mt,dsi2mt, &
dsi0d,dsi1d,dsi2d,dsi0md,dsi1md,dsi2md, &
ddsi0t,ddsi1t,ddsi2t,ddsi0mt,ddsi1mt,ddsi2mt, &
ddsi0d,ddsi1d,ddsi2d,ddsi0md,ddsi1md,ddsi2md, &
z,psi0,dpsi0,ddpsi0,psi1,dpsi1,ddpsi1,psi2, &
dpsi2,ddpsi2,din,h5,fi(36), &
xpsi0,xdpsi0,xpsi1,xdpsi1,h3, &
w0t,w1t,w2t,w0mt,w1mt,w2mt, &
w0d,w1d,w2d,w0md,w1md,w2md
! for the uniform background coulomb correction
double precision dsdd,dsda,lami,inv_lami,lamida,lamidd, &
plasg,plasgdd,plasgdt,plasgda,plasgdz, &
ecoul,decouldd,decouldt,decoulda,decouldz, &
pcoul,dpcouldd,dpcouldt,dpcoulda,dpcouldz, &
scoul,dscouldd,dscouldt,dscoulda,dscouldz, &
a1,b1,c1,d1,e1,a2,b2,c2,third,esqu
parameter (a1 = -0.898004d0, &
b1 = 0.96786d0, &
c1 = 0.220703d0, &
d1 = -0.86097d0, &
e1 = 2.5269d0, &
a2 = 0.29561d0, &
b2 = 1.9885d0, &
c2 = 0.288675d0, &
third = 1.0d0/3.0d0, &
esqu = qe * qe)
! quintic hermite polynomial statement functions
! psi0 and its derivatives
psi0(z) = z**3 * ( z * (-6.0d0*z + 15.0d0) -10.0d0) + 1.0d0
dpsi0(z) = z**2 * ( z * (-30.0d0*z + 60.0d0) - 30.0d0)
ddpsi0(z) = z* ( z*( -120.0d0*z + 180.0d0) -60.0d0)
! psi1 and its derivatives
psi1(z) = z* ( z**2 * ( z * (-3.0d0*z + 8.0d0) - 6.0d0) + 1.0d0)
dpsi1(z) = z*z * ( z * (-15.0d0*z + 32.0d0) - 18.0d0) +1.0d0
ddpsi1(z) = z * (z * (-60.0d0*z + 96.0d0) -36.0d0)
! psi2 and its derivatives
psi2(z) = 0.5d0*z*z*( z* ( z * (-z + 3.0d0) - 3.0d0) + 1.0d0)
dpsi2(z) = 0.5d0*z*( z*(z*(-5.0d0*z + 12.0d0) - 9.0d0) + 2.0d0)
ddpsi2(z) = 0.5d0*(z*( z * (-20.0d0*z + 36.0d0) - 18.0d0) + 2.0d0)
! biquintic hermite polynomial statement function
h5(i,j,w0t,w1t,w2t,w0mt,w1mt,w2mt,w0d,w1d,w2d,w0md,w1md,w2md)= &
fi(1) *w0d*w0t + fi(2) *w0md*w0t &
+ fi(3) *w0d*w0mt + fi(4) *w0md*w0mt &
+ fi(5) *w0d*w1t + fi(6) *w0md*w1t &
+ fi(7) *w0d*w1mt + fi(8) *w0md*w1mt &
+ fi(9) *w0d*w2t + fi(10) *w0md*w2t &
+ fi(11) *w0d*w2mt + fi(12) *w0md*w2mt &
+ fi(13) *w1d*w0t + fi(14) *w1md*w0t &
+ fi(15) *w1d*w0mt + fi(16) *w1md*w0mt &
+ fi(17) *w2d*w0t + fi(18) *w2md*w0t &
+ fi(19) *w2d*w0mt + fi(20) *w2md*w0mt &
+ fi(21) *w1d*w1t + fi(22) *w1md*w1t &
+ fi(23) *w1d*w1mt + fi(24) *w1md*w1mt &
+ fi(25) *w2d*w1t + fi(26) *w2md*w1t &
+ fi(27) *w2d*w1mt + fi(28) *w2md*w1mt &
+ fi(29) *w1d*w2t + fi(30) *w1md*w2t &
+ fi(31) *w1d*w2mt + fi(32) *w1md*w2mt &
+ fi(33) *w2d*w2t + fi(34) *w2md*w2t &
+ fi(35) *w2d*w2mt + fi(36) *w2md*w2mt
! cubic hermite polynomial statement functions
! psi0 & derivatives
xpsi0(z) = z * z * (2.0d0*z - 3.0d0) + 1.0
xdpsi0(z) = z * (6.0d0*z - 6.0d0)
! psi1 & derivatives
xpsi1(z) = z * ( z * (z - 2.0d0) + 1.0d0)
xdpsi1(z) = z * (3.0d0*z - 4.0d0) + 1.0d0
! bicubic hermite polynomial statement function
h3(i,j,w0t,w1t,w0mt,w1mt,w0d,w1d,w0md,w1md) = &
fi(1) *w0d*w0t + fi(2) *w0md*w0t &
+ fi(3) *w0d*w0mt + fi(4) *w0md*w0mt &
+ fi(5) *w0d*w1t + fi(6) *w0md*w1t &
+ fi(7) *w0d*w1mt + fi(8) *w0md*w1mt &
+ fi(9) *w1d*w0t + fi(10) *w1md*w0t &
+ fi(11) *w1d*w0mt + fi(12) *w1md*w0mt &
+ fi(13) *w1d*w1t + fi(14) *w1md*w1t &
+ fi(15) *w1d*w1mt + fi(16) *w1md*w1mt
! popular format statements
01 format(1x,5(a,1pe11.3))
02 format(1x,a,1p4e16.8)
03 format(1x,4(a,1pe11.3))
04 format(1x,4(a,i4))
! start of pipeline loop, normal execution starts here
eosfail = .false.
do j=jlo_eos,jhi_eos
! if (temp_row(j) .le. 0.0) stop 'temp less than 0 in helmeos'
! if (den_row(j) .le. 0.0) stop 'den less than 0 in helmeos'
temp = temp_row(j)
den = den_row(j)
abar = abar_row(j)
zbar = zbar_row(j)
ytot1 = 1.0d0/abar
ye = max(1.0d-16,ytot1 * zbar)
! initialize
deni = 1.0d0/den
tempi = 1.0d0/temp
kt = kerg * temp
ktinv = 1.0d0/kt
! radiation section:
prad = asoli3 * temp * temp * temp * temp
dpraddd = 0.0d0
dpraddt = 4.0d0 * prad*tempi
dpradda = 0.0d0
dpraddz = 0.0d0
erad = 3.0d0 * prad*deni
deraddd = -erad*deni
deraddt = 3.0d0 * dpraddt*deni
deradda = 0.0d0
deraddz = 0.0d0
srad = (prad*deni + erad)*tempi
dsraddd = (dpraddd*deni - prad*deni*deni + deraddd)*tempi
dsraddt = (dpraddt*deni + deraddt - srad)*tempi
dsradda = 0.0d0
dsraddz = 0.0d0
! ion section:
xni = avo * ytot1 * den
dxnidd = avo * ytot1
dxnida = -xni * ytot1
pion = xni * kt
dpiondd = dxnidd * kt
dpiondt = xni * kerg
dpionda = dxnida * kt
dpiondz = 0.0d0
eion = 1.5d0 * pion*deni
deiondd = (1.5d0 * dpiondd - eion)*deni
deiondt = 1.5d0 * dpiondt*deni
deionda = 1.5d0 * dpionda*deni
deiondz = 0.0d0
! sackur-tetrode equation for the ion entropy of
! a single ideal gas characterized by abar
x = abar*abar*sqrt(abar) * deni/avo
s = sioncon * temp
z = x * s * sqrt(s)
y = log(z)
! y = 1.0d0/(abar*kt)
! yy = y * sqrt(y)
! z = xni * sifac * yy
! etaion = log(z)
sion = (pion*deni + eion)*tempi + kergavo * ytot1 * y
dsiondd = (dpiondd*deni - pion*deni*deni + deiondd)*tempi &
- kergavo * deni * ytot1
dsiondt = (dpiondt*deni + deiondt)*tempi - &
(pion*deni + eion) * tempi*tempi &
+ 1.5d0 * kergavo * tempi*ytot1
x = avo*kerg/abar
dsionda = (dpionda*deni + deionda)*tempi &
+ kergavo*ytot1*ytot1* (2.5d0 - y)
dsiondz = 0.0d0
! electron-positron section:
! assume complete ionization
xnem = xni * zbar
! enter the table with ye*den
din = ye*den
! bomb proof the input
if (temp .gt. t(jmax)) then
write(6,01) 'temp=',temp,' t(jmax)=',t(jmax)
write(6,*) 'temp too hot, off grid'
write(6,*) 'setting eosfail to true and returning'
eosfail = .true.
return
end if
if (temp .lt. t(1)) then
write(6,01) 'temp=',temp,' t(1)=',t(1)
write(6,*) 'temp too cold, off grid'
write(6,*) 'setting eosfail to true and returning'
eosfail = .true.
return
end if
if (din .gt. d(imax)) then
write(6,01) 'den*ye=',din,' d(imax)=',d(imax)
write(6,*) 'ye*den too big, off grid'
write(6,*) 'setting eosfail to true and returning'
eosfail = .true.
return
end if
if (din .lt. d(1)) then
write(6,01) 'ye*den=',din,' d(1)=',d(1)
write(6,*) 'ye*den too small, off grid'
write(6,*) 'setting eosfail to true and returning'
eosfail = .true.
return
end if
! hash locate this temperature and density
jat = int((log10(temp) - tlo)*tstpi) + 1
jat = max(1,min(jat,jmax-1))
iat = int((log10(din) - dlo)*dstpi) + 1
iat = max(1,min(iat,imax-1))
! access the table locations only once
fi(1) = f(iat,jat)
fi(2) = f(iat+1,jat)
fi(3) = f(iat,jat+1)
fi(4) = f(iat+1,jat+1)
fi(5) = ft(iat,jat)
fi(6) = ft(iat+1,jat)
fi(7) = ft(iat,jat+1)
fi(8) = ft(iat+1,jat+1)
fi(9) = ftt(iat,jat)
fi(10) = ftt(iat+1,jat)
fi(11) = ftt(iat,jat+1)
fi(12) = ftt(iat+1,jat+1)
fi(13) = fd(iat,jat)
fi(14) = fd(iat+1,jat)
fi(15) = fd(iat,jat+1)
fi(16) = fd(iat+1,jat+1)
fi(17) = fdd(iat,jat)
fi(18) = fdd(iat+1,jat)
fi(19) = fdd(iat,jat+1)
fi(20) = fdd(iat+1,jat+1)
fi(21) = fdt(iat,jat)
fi(22) = fdt(iat+1,jat)
fi(23) = fdt(iat,jat+1)
fi(24) = fdt(iat+1,jat+1)
fi(25) = fddt(iat,jat)
fi(26) = fddt(iat+1,jat)
fi(27) = fddt(iat,jat+1)
fi(28) = fddt(iat+1,jat+1)
fi(29) = fdtt(iat,jat)
fi(30) = fdtt(iat+1,jat)
fi(31) = fdtt(iat,jat+1)
fi(32) = fdtt(iat+1,jat+1)
fi(33) = fddtt(iat,jat)
fi(34) = fddtt(iat+1,jat)
fi(35) = fddtt(iat,jat+1)
fi(36) = fddtt(iat+1,jat+1)
! various differences
xt = max( (temp - t(jat))*dti_sav(jat), 0.0d0)
xd = max( (din - d(iat))*ddi_sav(iat), 0.0d0)
mxt = 1.0d0 - xt
mxd = 1.0d0 - xd
! the six density and six temperature basis functions
si0t = psi0(xt)
si1t = psi1(xt)*dt_sav(jat)
si2t = psi2(xt)*dt2_sav(jat)
si0mt = psi0(mxt)
si1mt = -psi1(mxt)*dt_sav(jat)
si2mt = psi2(mxt)*dt2_sav(jat)
si0d = psi0(xd)
si1d = psi1(xd)*dd_sav(iat)
si2d = psi2(xd)*dd2_sav(iat)
si0md = psi0(mxd)
si1md = -psi1(mxd)*dd_sav(iat)
si2md = psi2(mxd)*dd2_sav(iat)
! derivatives of the weight functions
dsi0t = dpsi0(xt)*dti_sav(jat)
dsi1t = dpsi1(xt)
dsi2t = dpsi2(xt)*dt_sav(jat)
dsi0mt = -dpsi0(mxt)*dti_sav(jat)
dsi1mt = dpsi1(mxt)
dsi2mt = -dpsi2(mxt)*dt_sav(jat)
dsi0d = dpsi0(xd)*ddi_sav(iat)
dsi1d = dpsi1(xd)
dsi2d = dpsi2(xd)*dd_sav(iat)
dsi0md = -dpsi0(mxd)*ddi_sav(iat)
dsi1md = dpsi1(mxd)
dsi2md = -dpsi2(mxd)*dd_sav(iat)
! second derivatives of the weight functions
ddsi0t = ddpsi0(xt)*dt2i_sav(jat)
ddsi1t = ddpsi1(xt)*dti_sav(jat)
ddsi2t = ddpsi2(xt)
ddsi0mt = ddpsi0(mxt)*dt2i_sav(jat)
ddsi1mt = -ddpsi1(mxt)*dti_sav(jat)
ddsi2mt = ddpsi2(mxt)
! ddsi0d = ddpsi0(xd)*dd2i_sav(iat)
! ddsi1d = ddpsi1(xd)*ddi_sav(iat)
! ddsi2d = ddpsi2(xd)
! ddsi0md = ddpsi0(mxd)*dd2i_sav(iat)
! ddsi1md = -ddpsi1(mxd)*ddi_sav(iat)
! ddsi2md = ddpsi2(mxd)
! the free energy
free = h5(iat,jat, &
si0t, si1t, si2t, si0mt, si1mt, si2mt, &
si0d, si1d, si2d, si0md, si1md, si2md)
! derivative with respect to density
df_d = h5(iat,jat, &
si0t, si1t, si2t, si0mt, si1mt, si2mt, &
dsi0d, dsi1d, dsi2d, dsi0md, dsi1md, dsi2md)
! derivative with respect to temperature
df_t = h5(iat,jat, &
dsi0t, dsi1t, dsi2t, dsi0mt, dsi1mt, dsi2mt, &
si0d, si1d, si2d, si0md, si1md, si2md)
! derivative with respect to density**2
! df_dd = h5(iat,jat,
! 1 si0t, si1t, si2t, si0mt, si1mt, si2mt,
! 2 ddsi0d, ddsi1d, ddsi2d, ddsi0md, ddsi1md, ddsi2md)
! derivative with respect to temperature**2
df_tt = h5(iat,jat, &
ddsi0t, ddsi1t, ddsi2t, ddsi0mt, ddsi1mt, ddsi2mt, &
si0d, si1d, si2d, si0md, si1md, si2md)
! derivative with respect to temperature and density
df_dt = h5(iat,jat, &
dsi0t, dsi1t, dsi2t, dsi0mt, dsi1mt, dsi2mt, &
dsi0d, dsi1d, dsi2d, dsi0md, dsi1md, dsi2md)
! now get the pressure derivative with density, chemical potential, and
! electron positron number densities
! get the interpolation weight functions
si0t = xpsi0(xt)
si1t = xpsi1(xt)*dt_sav(jat)
si0mt = xpsi0(mxt)
si1mt = -xpsi1(mxt)*dt_sav(jat)
si0d = xpsi0(xd)
si1d = xpsi1(xd)*dd_sav(iat)
si0md = xpsi0(mxd)
si1md = -xpsi1(mxd)*dd_sav(iat)
! derivatives of weight functions
dsi0t = xdpsi0(xt)*dti_sav(jat)
dsi1t = xdpsi1(xt)
dsi0mt = -xdpsi0(mxt)*dti_sav(jat)
dsi1mt = xdpsi1(mxt)
dsi0d = xdpsi0(xd)*ddi_sav(iat)
dsi1d = xdpsi1(xd)
dsi0md = -xdpsi0(mxd)*ddi_sav(iat)
dsi1md = xdpsi1(mxd)
! look in the pressure derivative only once
fi(1) = dpdf(iat,jat)
fi(2) = dpdf(iat+1,jat)
fi(3) = dpdf(iat,jat+1)
fi(4) = dpdf(iat+1,jat+1)
fi(5) = dpdft(iat,jat)
fi(6) = dpdft(iat+1,jat)
fi(7) = dpdft(iat,jat+1)
fi(8) = dpdft(iat+1,jat+1)
fi(9) = dpdfd(iat,jat)
fi(10) = dpdfd(iat+1,jat)
fi(11) = dpdfd(iat,jat+1)
fi(12) = dpdfd(iat+1,jat+1)
fi(13) = dpdfdt(iat,jat)
fi(14) = dpdfdt(iat+1,jat)
fi(15) = dpdfdt(iat,jat+1)
fi(16) = dpdfdt(iat+1,jat+1)
! pressure derivative with density
dpepdd = h3(iat,jat, &
si0t, si1t, si0mt, si1mt, &
si0d, si1d, si0md, si1md)
dpepdd = max(ye * dpepdd,1.0d-30)
! look in the electron chemical potential table only once
fi(1) = ef(iat,jat)
fi(2) = ef(iat+1,jat)
fi(3) = ef(iat,jat+1)
fi(4) = ef(iat+1,jat+1)
fi(5) = eft(iat,jat)
fi(6) = eft(iat+1,jat)
fi(7) = eft(iat,jat+1)
fi(8) = eft(iat+1,jat+1)
fi(9) = efd(iat,jat)
fi(10) = efd(iat+1,jat)
fi(11) = efd(iat,jat+1)
fi(12) = efd(iat+1,jat+1)
fi(13) = efdt(iat,jat)
fi(14) = efdt(iat+1,jat)
fi(15) = efdt(iat,jat+1)
fi(16) = efdt(iat+1,jat+1)
! electron chemical potential etaele
etaele = h3(iat,jat, &
si0t, si1t, si0mt, si1mt, &
si0d, si1d, si0md, si1md)
! derivative with respect to density
x = h3(iat,jat, &
si0t, si1t, si0mt, si1mt, &
dsi0d, dsi1d, dsi0md, dsi1md)
detadd = ye * x
! derivative with respect to temperature
detadt = h3(iat,jat, &
dsi0t, dsi1t, dsi0mt, dsi1mt, &
si0d, si1d, si0md, si1md)
! derivative with respect to abar and zbar
detada = -x * din * ytot1
detadz = x * den * ytot1
! look in the number density table only once
fi(1) = xf(iat,jat)
fi(2) = xf(iat+1,jat)
fi(3) = xf(iat,jat+1)
fi(4) = xf(iat+1,jat+1)
fi(5) = xft(iat,jat)
fi(6) = xft(iat+1,jat)
fi(7) = xft(iat,jat+1)
fi(8) = xft(iat+1,jat+1)
fi(9) = xfd(iat,jat)
fi(10) = xfd(iat+1,jat)
fi(11) = xfd(iat,jat+1)
fi(12) = xfd(iat+1,jat+1)
fi(13) = xfdt(iat,jat)
fi(14) = xfdt(iat+1,jat)
fi(15) = xfdt(iat,jat+1)
fi(16) = xfdt(iat+1,jat+1)
! electron + positron number densities
xnefer = h3(iat,jat, &
si0t, si1t, si0mt, si1mt, &
si0d, si1d, si0md, si1md)
! derivative with respect to density
x = h3(iat,jat, &
si0t, si1t, si0mt, si1mt, &
dsi0d, dsi1d, dsi0md, dsi1md)
x = max(x,1.0d-30)
dxnedd = ye * x
! derivative with respect to temperature
dxnedt = h3(iat,jat, &
dsi0t, dsi1t, dsi0mt, dsi1mt, &
si0d, si1d, si0md, si1md)
! derivative with respect to abar and zbar
dxneda = -x * din * ytot1
dxnedz = x * den * ytot1
! the desired electron-positron thermodynamic quantities
! dpepdd at high temperatures and low densities is below the
! floating point limit of the subtraction of two large terms.
! since dpresdd doesn't enter the maxwell relations at all, use the
! bicubic interpolation done above instead of the formally correct expression
x = din * din
pele = x * df_d
dpepdt = x * df_dt
! dpepdd = ye * (x * df_dd + 2.0d0 * din * df_d)
s = dpepdd/ye - 2.0d0 * din * df_d
dpepda = -ytot1 * (2.0d0 * pele + s * din)
dpepdz = den*ytot1*(2.0d0 * din * df_d + s)
x = ye * ye
sele = -df_t * ye
dsepdt = -df_tt * ye
dsepdd = -df_dt * x
dsepda = ytot1 * (ye * df_dt * din - sele)
dsepdz = -ytot1 * (ye * df_dt * den + df_t)
eele = ye*free + temp * sele
deepdt = temp * dsepdt
deepdd = x * df_d + temp * dsepdd
deepda = -ye * ytot1 * (free + df_d * din) + temp * dsepda
deepdz = ytot1* (free + ye * df_d * den) + temp * dsepdz
! coulomb section:
! uniform background corrections only
! from yakovlev & shalybkov 1989
! lami is the average ion seperation
! plasg is the plasma coupling parameter
z = forth * pi
s = z * xni
dsdd = z * dxnidd
dsda = z * dxnida
lami = 1.0d0/s**third
inv_lami = 1.0d0/lami
z = -third * lami
lamidd = z * dsdd/s
lamida = z * dsda/s
plasg = zbar*zbar*esqu*ktinv*inv_lami
z = -plasg * inv_lami
plasgdd = z * lamidd
plasgda = z * lamida
plasgdt = -plasg*ktinv * kerg
plasgdz = 2.0d0 * plasg/zbar
! yakovlev & shalybkov 1989 equations 82, 85, 86, 87
if (plasg .ge. 1.0) then
x = plasg**(0.25d0)
y = avo * ytot1 * kerg
ecoul = y * temp * (a1*plasg + b1*x + c1/x + d1)
pcoul = third * den * ecoul
scoul = -y * (3.0d0*b1*x - 5.0d0*c1/x &
+ d1 * (log(plasg) - 1.0d0) - e1)
y = avo*ytot1*kt*(a1 + 0.25d0/plasg*(b1*x - c1/x))
decouldd = y * plasgdd
decouldt = y * plasgdt + ecoul/temp
decoulda = y * plasgda - ecoul/abar
decouldz = y * plasgdz
y = third * den
dpcouldd = third * ecoul + y*decouldd
dpcouldt = y * decouldt
dpcoulda = y * decoulda
dpcouldz = y * decouldz
y = -avo*kerg/(abar*plasg)*(0.75d0*b1*x+1.25d0*c1/x+d1)
dscouldd = y * plasgdd
dscouldt = y * plasgdt
dscoulda = y * plasgda - scoul/abar
dscouldz = y * plasgdz
! yakovlev & shalybkov 1989 equations 102, 103, 104
else if (plasg .lt. 1.0) then
x = plasg*sqrt(plasg)
y = plasg**b2
z = c2 * x - third * a2 * y
pcoul = -pion * z
ecoul = 3.0d0 * pcoul/den
scoul = -avo/abar*kerg*(c2*x -a2*(b2-1.0d0)/b2*y)
s = 1.5d0*c2*x/plasg - third*a2*b2*y/plasg
dpcouldd = -dpiondd*z - pion*s*plasgdd
dpcouldt = -dpiondt*z - pion*s*plasgdt
dpcoulda = -dpionda*z - pion*s*plasgda
dpcouldz = -dpiondz*z - pion*s*plasgdz
s = 3.0d0/den
decouldd = s * dpcouldd - ecoul/den
decouldt = s * dpcouldt
decoulda = s * dpcoulda
decouldz = s * dpcouldz
s = -avo*kerg/(abar*plasg)*(1.5d0*c2*x-a2*(b2-1.0d0)*y)
dscouldd = s * plasgdd
dscouldt = s * plasgdt
dscoulda = s * plasgda - scoul/abar
dscouldz = s * plasgdz
end if
! bomb proof
x = prad + pion + pele + pcoul
y = erad + eion + eele + ecoul
z = srad + sion + sele + scoul
! write(6,*) x,y,z
! if (x .le. 0.0 .or. y .le. 0.0 .or. z .le. 0.0) then
if (x .le. 0.0 .or. y .le. 0.0) then
! if (x .le. 0.0) then
! write(6,*)
! write(6,*) 'coulomb corrections are causing a negative pressure'
! write(6,*) 'setting all coulomb corrections to zero'
! write(6,*)
pcoul = 0.0d0
dpcouldd = 0.0d0
dpcouldt = 0.0d0
dpcoulda = 0.0d0
dpcouldz = 0.0d0
ecoul = 0.0d0
decouldd = 0.0d0