-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrism.f90
2838 lines (2397 loc) · 92.2 KB
/
rism.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
module rism
!
! Copyright (c) 2013, 2014 Alexei Matveev
! Copyright (c) 2013 Bo Li
!
use kinds, only: rk
implicit none
private
! These are all bind(c) and are not used in Fortran sources, but
! from C-side:
public :: rism_solvent
public :: rism_solvent_renorm
public :: rism_solute
! *** END OF INTERFACE ***
interface gnuplot
module procedure gnuplot1
module procedure gnuplot3
end interface gnuplot
integer, parameter :: LORENTZ_BERTHELOT = 0 ! σ(ab) = [σ(a) + σ(b)] / 2
integer, parameter :: GOOD_HOPE = 1 ! σ(ab) = [σ(a) * σ(b)]^(1/2)
contains
function rism_self_energy (n, sites, species) result (eg) bind (c)
!
! Calculate the energy summation between each pair of residues for
! a given site.
!
! Needs to be consistent with ./rism.h
!
use iso_c_binding, only: c_int, c_double
use foreign, only: site
use options, only: getopt
use lisp, only: obj, values, list, flonum
implicit none
integer (c_int), intent (in), value :: n
type (site), intent (in) :: sites(n)
integer (c_int), intent (in) :: species(n)
type (obj) :: eg ! multiple values (e, g)
! *** end of interface ***
integer :: rule
real (rk) :: e, g(3, n)
! FIXME: try not to proliferate use of "environments", function
! behaviour is better controlled via its arguments:
if (.not. getopt ("comb-rule", rule)) rule = LORENTZ_BERTHELOT
call self_energy (rule, sites, species, e, g)
eg = values (list (flonum (e), list2 (g)))
end function rism_self_energy
subroutine rism_solvent (pd, m, solvent, t_buf, x_buf, ptr) bind (c)
!
! When either t_buf or x_buf is not NULL it should be a pointer to
! a buffer with sufficient space to store nrad * m * m doubles
! where nrad = maxval (pd % n). If this is the case for t_buf then
! the real space representation of indirect correlation t is
! written there. If x_buf is not NULL then the Fourier
! representation of the solvent susceptibility (not offset by one)
! is written there.
!
! A NULL C-pointer will be cast by c_f_pointer() into an
! unassociated Fortran pointer which is isomorphic to a
! non-present optional argument down the call chain.
!
! Needs to be consistent with ./rism.h
!
use iso_c_binding, only: c_int, c_ptr, c_f_pointer
use foreign, only: problem_data, site, bgy3d_problem_data_print
use lisp, only: obj
implicit none
type (problem_data), intent (in) :: pd ! no VALUE
integer (c_int), intent (in), value :: m
type (site), intent (in) :: solvent(m)
type (c_ptr), intent (in), value :: t_buf ! double[m][m][nrad] or NULL
type (c_ptr), intent (in), value :: x_buf ! double[m][m][nrad] or NULL
type (c_ptr), intent (in), value :: ptr ! SCM* or NULL
! *** end of interface ***
integer :: nrad
real (rk), pointer :: t(:, :, :), x(:, :, :)
type (obj), pointer :: dict
nrad = pd % nrad
! FIXME: The code operates with arrays of the [m, m, nrad]
! shape. However, for historical reasons the C-interface assumes
! the transposed shape. Conversion between the two layouts is
! perfomed by main().
call c_f_pointer (t_buf, t, shape = [nrad, m, m])
call c_f_pointer (x_buf, x, shape = [nrad, m, m])
call c_f_pointer (ptr, dict)
! Fill supplied storage with solvent susceptibility. If NULL, it
! is interpreted as not present() in main() and thus ignored:
call main (pd, solvent, t=t, x=x, dict=dict)
end subroutine rism_solvent
subroutine rism_solute (pd, n, solute, m, solvent, x_buf, ptr) bind (c)
!
! A NULL C-pointer will be cast by c_f_pointer() into an
! unassociated Fortran pointer which is isomorphic to a
! non-present optional argument down the call chain.
!
! Needs to be consistent with ./rism.h
!
use iso_c_binding, only: c_int, c_ptr, c_f_pointer
use foreign, only: problem_data, site, bgy3d_problem_data_print
use lisp, only: obj
implicit none
type (problem_data), intent (in) :: pd ! no VALUE
integer (c_int), intent (in), value :: n, m
type (site), intent (in) :: solute(n)
type (site), intent (in) :: solvent(m)
type (c_ptr), intent (in), value :: x_buf ! double[m][m][nrad] or NULL
type (c_ptr), intent (in), value :: ptr ! SCM* or NULL
! *** end of interface ***
integer :: nrad
real (rk), pointer :: x(:, :, :)
type (obj), pointer :: dict
nrad = pd % nrad
! FIXME: The code operates with arrays of the [m, m, nrad]
! shape. However, for historical reasons the C-interface assumes
! the transposed shape. Conversion between the two layouts is
! perfomed by main().
call c_f_pointer (x_buf, x, shape = [nrad, m, m])
call c_f_pointer (ptr, dict)
call main (pd, solvent, solute, x=x, dict=dict)
end subroutine rism_solute
subroutine rism_solvent_renorm (m, solvent, rmax, nrad, x_kvv, alpha, s_kv) bind (c)
!
! In k-space applies convolutions with solvent site specific
! operator (dimensionless, if you think of charges so)
!
! X = Σ χ q
! i j ij j
!
! To the long range Coulomb shape
!
! v(k) = 4π exp (- k² / 4α²) / k²
!
! Note that the singular function above is dimensionless, to
! actually get the energy you would need another charge factor and
! 1/ε₀ in addition. The result
!
! s = X * v = Σ χ q * v
! i i j ij j
!
! is also dimensionless (again, if you think of charges as such).
! As radial functions of k-magnitude these m functions could be
! viewed as convolution kernels that can produce the
! renormalization term for arbitrary charge distribution,
! including but not limited to the superposition of point charges
! representing solute sites.
!
! Needs to be consistent with ./rism.h
!
use foreign, only: problem_data, site
use iso_c_binding, only: c_int
use fft, only: mkgrid
implicit none
integer (c_int), intent (in), value :: m
type (site), intent (in) :: solvent(m)
real (rk), intent (in), value :: rmax
integer (c_int), intent (in), value :: nrad
real (rk), intent (in) :: x_kvv(nrad, m, m) ! C-layout
real (rk), intent (in), value :: alpha
real (rk), intent (out) :: s_kv(nrad, m) ! C-layout
! *** end of interface ***
real (rk) :: q(m)
real (rk) :: x(nrad), v(nrad)
real (rk) :: r(nrad), dr, k(nrad), dk
integer :: i, j
! A copy of charges:
q = solvent % charge
! Reconstruct the grid (need k-values):
call mkgrid (rmax, r, dr, k, dk)
! Now tabulate the Coulomb field of a unit Gaussian on the
! k-grid. One would need to To be a potential (an energy per unit
! charge) this v(k) is missing the 1/ε₀ factor:
v = coulomb_long_fourier (k, alpha)
! Convolution with the charge-weighted potential s = χ *
! (qv). Note that χ ~ a + bk² and v ~ 1/k². Fortunately k is >=
! dk/2 in 1D code:
do i = 1, m
x = 0.0
do j = 1, m
x = x + x_kvv(:, i, j) * q(j)
enddo
s_kv(:, i) = x * v
enddo
end subroutine rism_solvent_renorm
subroutine main (pd, solvent, solute, t, x, dict)
!
! This one does not need to be interoperable.
!
use foreign, only: problem_data, site, bgy3d_problem_data_print
use lisp, only: obj, nil, acons, symbol
use drism, only: epsilon_rism
implicit none
type (problem_data), intent (in) :: pd
type (site), intent (in) :: solvent(:)
type (site), optional, intent (in) :: solute(:)
real (rk), optional, intent (out) :: t(:, :, :) ! (nrad, m, m), sic!
real (rk), optional, intent (inout) :: x(:, :, :) ! (nrad, m, m), sic!
type (obj), intent (out), optional :: dict
! *** end of interface ***
logical :: vv, uv
integer :: nrad
real (rk) :: rmax
! We do vv-caclulation if there is no solute, or if the solvent
! susceptibility was not supplied for solute/solvent calculation.
uv = present (solute)
vv = .not. uv .or. uv .and. .not. present (x)
rmax = pd % rmax
nrad = pd % nrad
if (verbosity() > 0) then
print *, "# L =", rmax, "(for 1d)"
print *, "# N =", nrad, "(for 1d)"
call bgy3d_problem_data_print (pd)
call show_sites ("Solvent", solvent)
if (present (solute)) then
call show_sites ("Solute", solute)
endif
print *, "# ε(RISM) =", epsilon_rism (pd % beta, pd % rho, solvent)
endif
! This is applicable to LJ only, and should take reduced
! density/temperature:
! call print_info (rho = pd % rho, beta = pd % beta)
block
! Indirect correlation t = h - c aka γ (hence the name):
real (rk) :: gam(size (solvent), size (solvent), nrad)
! Solvent susceptibility χ = ω + ρh:
real (rk) :: chi(size (solvent), size (solvent), nrad)
! Procedures down the call chain will set this to valid
! assiciation lists:
type (obj) :: vdict, udict
if (vv) then
call rism_vv (pd % closure, nrad, rmax, pd % beta, pd % rho, &
solvent, gam, chi, vdict)
! Output, if requested:
if (present (t)) then
t = clayout (gam)
endif
if (present (x)) then
x = clayout (chi)
endif
else
if (.not. present (x)) error stop "no way to get chi!"
chi = flayout (x)
vdict = nil
endif
if (uv) then
call rism_uv (pd % closure, nrad, rmax, pd % beta, pd % rho, &
solvent, chi, solute, udict)
endif
if (present (dict)) then
if (uv) then
! In solute/solvent calculations the returned dictionary
! will contain solute data and a solvent section with
! another dictionary of solvent results. Note that the
! latter might be empty if the vv-code was not invoked:
dict = udict
dict = acons (symbol ("solvent"), vdict, dict)
else
! FIXME: there is no solute data, should we still include
! an entry for symmetry?
dict = vdict
endif
endif
end block
end subroutine main
subroutine rism_vv (method, nrad, rmax, beta, rho, sites, gam, chi, dict)
use fft, only: mkgrid, fourier_rows, FT_FW, FT_BW
use snes, only: snes_default
use foreign, only: site
use options, only: getopt
use lisp, only: obj, acons, symbol, flonum
use drism, only: dipole_density, dipole_factor, dipole_correction, &
epsilon_rism
use closures, only: closure, chempot
implicit none
integer, intent (in) :: method ! HNC, KH, or PY
integer, intent (in) :: nrad ! grid size
real (rk), intent (in) :: rmax ! cell size
real (rk), intent (in) :: beta ! inverse temp
real (rk), intent (in) :: rho
type (site), intent (in) :: sites(:) ! (m)
real (rk), intent (out) :: gam(:, :, :) ! (m, m, nrad)
real (rk), intent (out) :: chi(:, :, :) ! (m, m, nrad)
type (obj), intent (out) :: dict
! *** end of interface ***
! Pair quantities. FIXME: they are symmetric, one should use that:
real (rk), dimension (size (sites), size (sites), nrad) :: &
vr, vk, wk, xk, t, c
! Radial grids:
real (rk) :: r(nrad), dr
real (rk) :: k(nrad), dk
integer :: m, rule
real (rk) :: eps ! zero if not supplied in the command line
real (rk) :: A ! Cummings & Stell factor [CS81]
if (.not. getopt ("comb-rule", rule)) rule = LORENTZ_BERTHELOT
! Make sure it is not used anywhere if it was not supplied. The
! logic is if eps /= 0 then do DRISM, otherwise --- do the usual
! RISM with whatever dielectric constant comes out of it:
if (.not. getopt ("dielectric", eps)) eps = 0.0
! Set Cummings & Stell semi-empiric scaling factor A for the
! long-range site-site correlation. This conflicts with DRISM
! approach. FIXME: there is currently no input control to switch
! to this method (that is why the hardwired no-op branch choice):
if (.false.) then
block
real (rk) :: y
y = dipole_density (beta=beta, rho=rho, sites=sites)
! FIXME: this factor is infinite when y -> 0 (e.g. zero
! dipole) while keeping the target ε - 1 finite:
A = dipole_factor (e=eps, y=y)
if (verbosity() > 0) then
print *, "# Scaling the long-range asymptotics by A=", A, "for e=", eps
endif
end block
else
! A = 1 has no effect:
A = 1.0
endif
m = size (sites)
! Prepare grid, dr * dk = 2π/2n:
call mkgrid (rmax, r, dr, k, dk)
! Tabulate short-range pairwise potentials v() on the r-grid and
! long-range pairwise potential vk() on the k-grid:
call force_field (rule, sites, sites, r, k, vr, vk)
! Rigid-bond correlations on the k-grid:
wk = omega_fourier (sites, k)
!
! FIXME: the dipole correction x(k) is meaningless if the dipole
! vector is zero. Watch for the warning issued by dipole_axes()
! in such cases:
!
if (eps /= 0.0) then
xk = dipole_correction (beta, rho, eps, sites, k)
else
xk = 0.0 ! maybe useful to avoid branches later
endif
! Intitial guess:
t = 0.0
! Find t such that iterate_t (t) == 0. FIXME: passing an internal
! function as a callback is an F08 feature. GFortran 4.3 on Debian
! Lenny does not support that:
!
! Transformation from f(r) to r * f(r) is linear and invertible,
! note that here minval (r) = dr / 2. The non-linear solvers
! operate with some specific norm of residual. Such an L2-norm of
! rf(r) would be proportional to the volume integral of |f(r)|²
! without unnecessarily emphasizing small r regions. At least for
! cSPC/E water with some settings, rmax = 10 A, nrad = 256,
! epsilon = 78.4, this method converges using plain Newton, and
! the original needs tweaking like adding Picard pre-optimization
! by snes-solver = trial. FIXME: maybe add Jacobian?
!
if (.false.) then
! Solve for t(r):
call snes_default (t, iterate_t)
else
! Solve for r * t(r):
block
real (rk) :: rt(m, m, nrad)
rt = to (t)
call snes_default (rt, iterate_rt)
t = from (rt)
end block
endif
! Do not assume c has a meaningfull value, it was overwritten with
! c(k):
c = closure (method, beta, vr, t)
!
! Return the indirect correlation t(r) aka γ(r) in real rep and
! solvent susceptibility χ(k) in Fourier rep:
!
gam = t
block
real (rk) :: h(m, m, nrad)
! h(k) = c(k) + t(k)
h = fourier_rows (c + t) * (dr**3 / FT_FW)
! χ = ω + ρh
chi = wk + rho * h
end block
! Done with it, print results. Here solute == solvent:
call post_process (method, rmax, beta, rho, sites, sites, &
chi, vr, t, A=A, eps=eps, dict=dict, rbc=.false.)
! Chemical potential (SCF) ...
block
real (rk) :: vl (m, m, nrad), mu
! Long range potential on the real space grid:
vl = force_field_long (sites, sites, r)
! Chemical potential as a functional of converged t:
mu = chempot (method, rmax, beta, rho, vr, vl, t)
dict = acons (symbol ("free-energy"), flonum (mu), dict)
end block
contains
function iterate_t (t) result (dt)
!
! Closure over host variables: r, k, dr, dk, v, c, beta, rho,
! ... Implements procedure(func1).
!
use closures, only: closure
implicit none
real (rk), intent (in) :: t(:, :, :) ! (m, m, nrad)
real (rk) :: dt(size (t, 1), size (t, 2), size (t, 3))
! *** end of interface ***
c = closure (method, beta, vr, t)
! Forward FT via DST:
c = fourier_rows (c) * (dr**3 / FT_FW)
!
! The real-space representation encodes only the short-range
! part of the direct corrlation. The (fixed) long range
! contribution is added here:
!
! C := C - βV
! S L
!
! For dipole solvents Cummings and Stell implied that an overall
! scaling factor A in the long range expression for direct
! correlation is apropriate [CS81]:
!
c = c - (beta * A) * vk
!
! OZ equation, involves convolutions, dont play with
! normalization here --- this is one of a few places where it is
! assumed that convolution theorem is factor-less. Considered as
! a functional of c, this operation, t = T[c], is not a linear
! one. It does involve solving a linear equation system with
! coefficients and the right hand side defined by c, though.
!
if (eps /= 0.0) then
! DRISM. This is going to break for ρ = 0 as x(k) ~ 1/ρ², it
! will also break if dipole density y = 0:
dt = oz_vv_equation_c_t (rho, c, wk + rho * xk) + xk
else
! The branch is redundand since x(k) = 0 in this case. It is
! here only not to waste CPU time:
dt = oz_vv_equation_c_t (rho, c, wk)
endif
!
! Since we plugged in the Fourier transform of the full direct
! correlation including the long range part into the OZ equation
! what we get out is the full indirect correlation including the
! long-range part. The menmonic is C + T is short range. Take
! it out:
!
! T := T - βV
! S L
!
! The factor A is due to Cummings and Stell.
!
dt = dt - (beta * A) * vk
! Inverse FT via DST:
dt = fourier_rows (dt) * (dk**3 / FT_BW)
! Return the increment that vanishes at convergence:
dt = dt - t
end function iterate_t
function iterate_rt (rt) result (drt)
implicit none
real (rk), intent (in) :: rt(:, :, :) ! (m, m, nrad)
real (rk) :: drt(size (t, 1), size (t, 2), size (t, 3))
! *** end of interface ***
drt = to (iterate_t (from (rt)))
end function iterate_rt
function to (x) result (y)
!
! y = r * x
!
implicit none
real (rk), intent (in) :: x(:, :, :)
real (rk) :: y(size (x, 1), size (x, 2), size (x, 3))
! *** end of interface ***
integer :: i, j
do i = 1, size (x, 1)
do j = 1, size (x, 2)
y(i, j, :) = r * x(i, j, :)
enddo
enddo
end function to
function from (x) result (y)
!
! y = x / r
!
implicit none
real (rk), intent (in) :: x(:, :, :)
real (rk) :: y(size (x, 1), size (x, 2), size (x, 3))
! *** end of interface ***
integer :: i, j
do i = 1, size (x, 1)
do j = 1, size (x, 2)
y(i, j, :) = x(i, j, :) / r
enddo
enddo
end function from
end subroutine rism_vv
subroutine rism_uv ( method, nrad, rmax, beta, rho, solvent, chi, solute, dict)
use snes, only: snes_default
use foreign, only: site
use lisp, only: obj, acons, symbol, flonum
use options, only: getopt
use units, only: angstrom
use fft, only: mkgrid
use closures, only: chempot
implicit none
integer, intent (in) :: method ! HNC, KH, or PY
integer, intent (in) :: nrad ! grid size
real (rk), intent (in) :: rmax ! cell size
real (rk), intent (in) :: beta ! inverse temp
real (rk), intent (in) :: rho
type (site), intent (in) :: solvent(:) ! (m)
real (rk), intent(in) :: chi(:, :, :) ! (m, m, nrad)
type (site), intent (in) :: solute(:) ! (n)
type (obj), intent (out) :: dict
! *** end of interface ***
! If true, use Ng scheme as is (in this case s_uvk is not used).
! Otherwise exploit the linearity of t = T[c] functional (see
! below):
logical, parameter :: ng = .false.
! Solute-solvent pair quantities:
real (rk), dimension (size (solute), size (solvent), nrad) :: &
v_uvr, v_uvk, t_uvx, c_uvx, s_uvk, expB ! (n, m, nrad)
! Solute-solute pair quantities:
real (rk), dimension (size (solute), size (solute), nrad) :: w_uuk ! (n, n, nrad)
! Radial grids:
real (rk) :: r(nrad), dr
real (rk) :: k(nrad), dk
integer :: m, n, rule
logical rbc
n = size (solute)
m = size (solvent)
if (.not. getopt ("comb-rule", rule)) rule = LORENTZ_BERTHELOT
! Prepare grid, dr * dk = 2π/2n:
call mkgrid (rmax, r, dr, k, dk)
! Tabulate short-range pairwise potentials v() on the r-grid and
! long-range pairwise potential vk() on the k-grid:
call force_field (rule, solute, solvent, r, k, v_uvr, v_uvk)
! Rigid-bond solute-solute correlations on the k-grid:
w_uuk = omega_fourier (solute, k)
rbc = getopt ("rbc")
if (rbc) then
call bridge (rule, solute, solvent, beta, r, dr, k, dk, expB)
else
! Not used in this case:
expB = 1.0
endif
!
! The functional t = T[c] that relates c(k) and t(k) for any given
! ω(k) and χ(k) which is implemented by
!
! oz_uv_equation_c_t (c, w, x)
!
! and is repeatedly applied during iterations is a linear
! one. Here we precompute
!
! s = -β (T[v] + v)
!
! with v being the long-range Coulomb potential as an optimization
! of the usual Ng scheme:
!
! t = T[c - βv] - βv = T[c] + s
!
! This isnt very performance critical, and is rather a test before
! implementing a similar procedure for the 3D version. Note that
! applying T[v] involves a convolution (product) of χ(k) ~ a + bk²
! with a long-range distribution v ~ 1/k². This is not very
! problematic in the current implementation where we deliberately
! avoid dealing with k = 0. But it will be a problem in 3D code.
!
if (.not. ng) then
s_uvk = -beta * (oz_uv_equation_c_t (v_uvk, w_uuk, chi) + v_uvk)
endif
! Intitial guess:
t_uvx = 0.0
! Find t such that iterate_t (t) == 0. FIXME: passing an internal
! function as a callback is an F08 feature. GFortran 4.3 on Debian
! Lenny does not support that:
call snes_default (t_uvx, iterate_t, jacobian_t)
! Done with it, print results:
call post_process (method, rmax, beta, rho, solvent, solute, &
chi, v_uvr, t_uvx, A=1.0d0, eps=0.0d0, dict=dict, rbc=rbc)
! Chemical potential (SCF) ...
block
real (rk) :: v_uvl (n, m, nrad), mu
! Long range potential on the real space grid:
v_uvl = force_field_long (solute, solvent, r)
! Chemical potential as a functional of converged t:
mu = chempot (method, rmax, beta, rho, v_uvr, v_uvl, t_uvx)
dict = acons (symbol ("free-energy"), flonum (mu), dict)
end block
! Derivatives with respect to all 3n displacements of n solute
! sites. FIXME: this is a waste of time given that some species
! are rigid and some more degrees of freedom may be frozen by the
! user:
block
logical :: flag
real (rk) :: gradients(3, n)
type (obj) :: grads
! False if unspecified:
if (.not. getopt ("derivatives", flag)) then
flag = .false.
endif
if (flag) then
call derivatives (method, rmax, beta, rho, solute, solvent, &
chi, v_uvr, v_uvk, t_uvx, jacobian_t0, gradients)
grads = list2 (gradients)
! call display (grads)
! call newline ()
dict = acons (symbol ("free-energy-gradient"), grads, dict)
endif
end block
contains
function iterate_t (t) result (dt)
!
! Closure over host variables: r, k, dr, dk, v, c, beta, rho,
! ... Implements procedure (func1).
!
use fft, only: fourier_rows, FT_FW, FT_BW
use closures, only: closure, closure_rbc
implicit none
real (rk), intent (in) :: t(:, :, :) ! (n, m, nrad)
real (rk) :: dt(size (t, 1), size (t, 2), size (t, 3))
! *** end of interface ***
if (rbc) then
c_uvx = closure_rbc (method, beta, v_uvr, t, expB)
else
c_uvx = closure (method, beta, v_uvr, t)
endif
! Forward FT via DST:
c_uvx = fourier_rows (c_uvx) * (dr**3 / FT_FW)
!
! The real-space representation encodes only the short-range
! part of the direct corrlation. The (fixed) long range
! contribution is added here:
!
! C := C - βV
! S L
!
if (ng) then
c_uvx = c_uvx - beta * v_uvk
endif
!
! Next comes the OZ equation which involves convolutions where
! we assume the convolution theorem to be factor-free. Dont
! play with normalization here.
!
! As a functional of c this is a linear relation t = T[c].
! Because of this linearity we handle the long range term added
! to c above and to resulting t below differently in the ng =
! false branch:
!
! t = T[c + x] + x = T[c] + s
!
! where
!
! s = (T[x] + x)
!
! with x = -βv. Here the second term in the square brackets
! derived from the fixed long-range assymptotics of direct
! correlation is constant. This is just one addition after
! computing T[c] instead of one before and one after.
!
dt = oz_uv_equation_c_t (c_uvx, w_uuk, chi)
!
! Since we plugged in the Fourier transform of the full direct
! correlation including the long range part into the OZ equation
! what we get out is the full indirect correlation including the
! long-range part. Note however that assymptotically C and T
! differ by just a sign --- the menmonic is C + T is short
! range. Take the assymptote of T out making it short range:
!
! T := T - βV
! S L
!
if (ng) then
dt = dt - beta * v_uvk
else
dt = dt + s_uvk
endif
! Inverse FT via DST:
dt = fourier_rows (dt) * (dk**3 / FT_BW)
! Return the increment that vanishes at convergence:
dt = dt - t
end function iterate_t
function jacobian_t (t, dt) result (ddt)
!
! Closure over host variables: r, k, dr, dk, v, c, beta, rho,
! ... Implements procedure (func2).
!
use fft, only: fourier_rows, FT_FW, FT_BW
use closures, only: closure1
implicit none
real (rk), intent (in) :: t(:, :, :) ! (n, m, nrad)
real (rk), intent (in) :: dt(:, :, :) ! (n, m, nrad)
real (rk) :: ddt(size (t, 1), size (t, 2), size (t, 3))
! *** end of interface ***
! In this procedure the variable c_uvx has a different meaning
! than in iterate_t(). Here it is a *differential* increment dc
! of c due to t -> t + dt.
if (rbc) then
! c_uvx = closure_rbc1 (method, beta, v_uvr, t, dt, expB)
stop "not implemented"
else
c_uvx = closure1 (method, beta, v_uvr, t, dt)
endif
! Forward FT via DST:
c_uvx = fourier_rows (c_uvx) * (dr**3 / FT_FW)
!
! OZ equation, involves "convolutions", take care of the
! normalization here. As a functional of c this is a linear
! relation. See the comments in interate_t() on why the constant
! long-range term does not contribute to the Jacobian.
!
ddt = oz_uv_equation_c_t (c_uvx, w_uuk, chi)
! Inverse FT via DST:
ddt = fourier_rows (ddt) * (dk**3 / FT_BW)
! Return the increment that vanishes at convergence:
ddt = ddt - dt
end function jacobian_t
function jacobian_t0 (dt) result (ddt)
!
! Closure over t0 == t_uvx. Implements procedure (func1).
!
implicit none
real (rk), intent (in) :: dt(:, :, :) ! (n, m, nrad)
real (rk) :: ddt(size (dt, 1), size (dt, 2), size (dt, 3))
! *** end of interface ***
ddt = jacobian_t (t_uvx, dt)
end function jacobian_t0
end subroutine rism_uv
subroutine derivatives (method, rmax, beta, rho, solute, solvent, &
chi_vvk, v_uvr, v_uvk, t_uvr, jacobian, gradients)
!
! Derivatives of the chemical potential with respect to geometric
! distortions of the solute.
!
use foreign, only: site, comm_rank, comm_size, comm_set_parallel_x,&
comm_allreduce
use iso_c_binding, only: c_bool
use fft, only: mkgrid, fourier_rows, FT_FW, FT_BW
use snes, only: func1, krylov
use closures, only: closure, closure1, chempot1
implicit none
integer, intent (in) :: method
real (rk), intent (in) :: rmax, beta, rho
type (site), intent (in) :: solute(:) ! (n)
type (site), intent (in) :: solvent(:) ! (m)
real (rk), intent (in) :: chi_vvk(:, :, :) ! (m, m, nrad)
real (rk), intent (in) :: v_uvr(:, :, :) ! (n, m, nrad)
real (rk), intent (in) :: v_uvk(:, :, :) ! (n, m, nrad)
real (rk), intent (in) :: t_uvr(:, :, :) ! (n, m, nrad)
procedure (func1) :: jacobian ! (n, m, nrad) -> (n, m, nrad)
real (rk), intent (out) :: gradients(:, :) ! (3, n)
! *** end of interface ***
integer :: n, m, nrad
n = size (t_uvr, 1)
m = size (t_uvr, 2)
nrad = size (t_uvr, 3)
block
logical (c_bool) :: mode
integer :: i, j, ij, np, rank
real (rk), parameter :: step = 1.0d0 ! does not need to be small
real (rk) :: dx(3)
real (rk) :: dw(n, n, nrad)
real (rk) :: c(n, m, nrad), df(n, m, nrad), dt(n, m, nrad)
real (rk) :: vl_uvr (n, m, nrad), dmu
real (rk) :: r(nrad), k(nrad), dr, dk
call mkgrid (rmax, r, dr, k, dk)
! Long range potential on the real space grid. We do not use the
! fourier representation v(k) supplied as the input in v_uvk:
vl_uvr = force_field_long (solute, solvent, r)
! Chemical potential as a functional of converged t could be
! computed like this:
!
! mu = chempot (method, rmax, beta, rho, v_uvr, vl_uvr, t_uvr)
!
! Use closure to compute convered c(k) including the long-range
! assymptotics:
c = closure (method, beta, v_uvr, t_uvr)
c = fourier_rows (c) * (dr**3 / FT_FW)
c = c - beta * v_uvk
! We assume that we are in cooperative mode at the moment. The
! rank will be unique for all workers within the current world
! consisting of one or (hopefully) more workers:
np = comm_size ()
rank = comm_rank ()
! Set the uncooperative operation mode of the workers. Each
! worker closes all doors and confines himself to smaller world
! of MPI_COMM_SELF. The control flow for each worker is about
! to depart, see conditional skipping of iterations in the
! double loop below. FIXME: This ugliness is here because the
! MPI communicator for use in PETSC calls is taken from a global
! variable.
mode = .false. ! logical (c_bool)
mode = comm_set_parallel_x (mode)
ij = 0
do i = 1, n
do j = 1, 3
ij = ij + 1
! Round robin work sharing:
if (rank /= mod (ij - 1, np)) then
gradients(j, i) = 0.0 ! for use in allreduce
cycle
endif
dx = 0.0
dx(j) = step
! This one is proportional to x * j1(x) = sinc(x) - cos(x)
! with x = kl. So it is relatively "long range" in the
! k-space. The hope is, it is applied as a convolution
! kernel to something that is sufficiently smooth (or
! differentiable):
dw = omega_fourier1 (solute, k, i, dx)
! Fix point equation we solve is this: F(t) = T(t) - t = 0
! with F(t) represented by iterate_t(). A change dw in
! parameters of this fix point problem leads to a
! linearization J(t) * dt = -df(t) with df in that
! equation being the differential due to dw only.
df = oz_uv_equation_c_h (c, dw, chi_vvk)
df = fourier_rows (df) * (dk**3 / FT_BW)
! Solving the linearized problem amounts to finding a dt
! such that jacobian(dt) == - df.
!
! This will call PETSC and here it is important that the
! current communicator is MPI_COMM_SELF, otherwise PETSC
! will try to cooperate with other workers, which are not
! willing to.
dt = krylov (jacobian, -df)
! Differential of chemical potential due to dt:
dmu = chempot1 (method, rmax, beta, rho, v_uvr, vl_uvr, t_uvr, dt)
gradients(j, i) = dmu / step
enddo
enddo
! Restore cooperative mode. From now on all workers may
! communicate again.
mode = comm_set_parallel_x (mode)
! This will be performed over the original world:
call comm_allreduce (size (gradients), gradients)
end block
end subroutine derivatives
subroutine guess_self_energy (rule, solute, dict)
!
! Adds an entry with self energy to the dictionary. FIXME: this is
! getting cumbersome.
!
use foreign, only: site, pad
use options, only: getopt
use lisp, only: obj, acons, symbol, flonum, car, cdr, int, nil, &
cons, flonum, funcall, lookup
use units, only: kcal, angstrom