/
hnc3d.c
2350 lines (1909 loc) · 75.9 KB
/
hnc3d.c
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
/* -*- mode: c; c-basic-offset: 2; -*- vim: set sw=2 tw=70 et sta ai: */
/*
Copyright (c) 2007 Lukas Jager
Copyright (c) 2013, 2014 Alexei Matveev
Copyright (c) 2013 Bo Li
*/
/*
For simple liquids the matrix OZ equation reads
h = c * [1 + ρ h]
where the expression in the square brackets is susceptibility χ = 1
+ ρh of the mixture. The matrix ρ is diagonal. Only when the
density of all sites is the same the equation simplifies to the more
recognizable form
h = c + ρ c * h.
However, consider a solute/solvent mixture in the limit of infinite
dilution where the density of the solute is vanishingly small. The
corresponding blocks of the matrix equation read:
h = c [1 + ρ h ] + c ρ h (1)
vv vv v vv vu u uv
The second term in Eq. (1) vanishes and the OZ equation for the
solvent structure becomes independent of the solute. The
solute-solute correlations are, of course, affected by the dense
medium even in the infinite dilution limit:
h = c [1 + ρ h ] + c ρ h (2)
uu uu u uu uv v vu
Now the remaining two equations
h = c [1 + ρ h ] + c ρ h (3)
uv uv v vv uu u uv
and
h = c [1 + ρ h ] + c ρ h (4)
vu vu u uu vv v vu
though necessarily redundant seem to differ. By removing the terms
proportional to the solute density one gets two different but
equivalent relations:
h = c + c ρ h (5)
uv uv uv v vv
and
h = c + c ρ h (6)
vu vu vv v vu
For practical application these two equations may be used as a
relation between the solute-solvent indirect correlation t = h - c
and a another solute-solvent property as a complement to the closure
relation. Both equation use the solvent structure as a parameter:
t = c ρ h (7)
uv uv v vv
or
t = c ρ h (8)
vu vv v vu
In the first case the structure of the pure solvent (or rather
infinitely diluted mixture) is represented by the solvent-solvent
total correlation h, whereas in the second case the solvent
structure is represented by the solvent-solvent direct correlation
c. The second of these two forms, Eq. (8), was originally proposed
for use in 3d HNC equations [1]. However other (more recent)
literature appears to prefer to start with a formulation that
resembles more Eq. (7) e.g. Ref. [2]. This alternative approach
assumes that the structure of the pure solvent is represented by the
solvent susceptibility χ that mediates the relation between the
total and direct solute-solvent correlations:
h = c χ (9)
uv uv vv
with
χ = 1 + ρ h (10)
vv v vv
which is equivalent to Eq. (7). Note that χ, as it stands, is not
symmetric, unless ρ is the same for all sites. FIXME: the code makes
use of this assumption!
The corresponding equation for radial site-site distributions in
molecular 1d RISM equation in the infinite dilution limit reads
h = ω * c * [ω + ρ h ] (11a)
uv u uv v v vv
whereas the generalization to the 3d RISM is missing the convolution
with the solute intra-molecular correlation:
h = c * [ω + ρ h ] (11b)
uv uv v v vv
Here again the term is square brackets is the property of the
molecular solvent fixed by the assumption of the infinite dilution,
and is a generalization of the solvent susceptibility for molecular
solvents:
χ = ω + ρ h (12)
vv v v vv
With this definition, the expression for the indirect solute-solvent
correlation
t = h - c = c (χ - 1) (13)
uv uv uv uv vv
does not simplify to Eq. (7) directly though still has a very
similar structure. Again, only if ρ is a scalar one can assume the
solvent susceptibility χ is symmetric in site indices.
[1] Numerical solution of the hypernetted chain equation for a
solute of arbitrary geometry in three dimensions, Dmitrii Beglov
and Benoît Roux , J. Chem. Phys. 103, 360 (1995);
http://dx.doi.org/10.1063/1.469602
[2] Three-Dimensional Molecular Theory of Solvation Coupled with
Molecular Dynamics in Amber, Tyler Luchko, Sergey Gusarov,
Daniel R. Roe, Carlos Simmerling, David A. Case , Jack Tuszynski
and Andriy Kovalenko, J. Chem. Theory Comput., 2010, 6 (3), pp
607–624 http://dx.doi.org/10.1021/ct900460m
*/
#include "bgy3d.h"
#include "bgy3d-getopt.h"
#include "bgy3d-fftw.h" /* bgy3d_fft_mat_create() */
#include "bgy3d-vec.h" /* vec_create() */
#include "bgy3d-solutes.h" /* Site, bgy3d_solute_field() */
#include "bgy3d-force.h" /* bgy3d_pair_potential() */
#include "bgy3d-pure.h" /* bgy3d_omega_fft_create() */
#include "bgy3d-snes.h" /* bgy3d_snes_default() */
#include "hnc3d-sles.h" /* hnc3d_sles_zgesv() */
#include "rism.h" /* rism_solvent() */
#include "bgy3d-potential.h" /* info() */
#include "bgy3d-impure.h" /* Restart */
#include "bgy3d-solvents.h" /* bgy3d_sites_show() */
#include "bgy3d-guile.h" /* from_double2() */
#include <math.h> /* expm1() */
#include "hnc3d.h"
static void
mayer (real beta, Vec v, Vec f)
{
void em1 (int n, real v[n], real f[n])
{
for (int i = 0; i < n; i++)
f[i] = expm1 (-beta * v[i]);
}
vec_app2 (em1, v, f);
}
static void
compute_c (ClosureEnum closure, real beta, Vec v, Vec t, Vec c)
{
/* No aliasing, we call Fortran here: */
assert (c != t);
assert (c != v);
/* Callback for vec_app3(): */
void f (int n, real v[n], real t[n], real c[n])
{
/* See Fortran implementation in closures.f90. NOTE: no aliasing
here, please! */
rism_closure (closure, beta, n, v, t, c);
}
vec_app3 (f, v, t, c);
}
static void
compute_c1 (ClosureEnum closure, real beta, Vec v, Vec t, Vec dt, Vec dc)
{
void f (int n, real v[n], real t[n], real dt[n], real dc[n])
{
rism_closure1 (closure, beta, n, v, t, dt, dc);
}
vec_app4 (f, v, t, dt, dc);
}
static int
delta (int i, int j)
{
return (i == j) ? 1 : 0;
}
/*
Use the k-representation of Ornstein-Zernike (OZ) equation
h = c + ρ c * h
to compute γ = h - c form c:
-1 -1 2
γ = (1 - ρc) c - c = (1 - ρc) ρc
If you scale c by h3 beforehand or pass rho' = rho * h3 and scale
the result by h3 in addition, you will compute exactly what the
older version of the function did:
*/
static void
compute_t2_1 (real rho, Vec c_fft, Vec t_fft)
{
void f (int n, complex c[n], complex t[n])
{
/* Same as c / (1 - rho * c) - c: */
for (int i = 0; i < n; i++)
t[n] = rho * (c[n] * c[n]) / (1.0 - rho * c[n]);
}
vec_fft_app2 (f, c_fft, t_fft);
}
/* So far rho is scalar, it could be different for all sites: */
static void
compute_t2_m (int m, real rho, Vec c_fft[m][m], Vec w_fft[m][m], Vec t_fft[m][m])
{
if (m == 0) return; /* see ref to c_fft[0][0] */
local complex *c_fft_[m][m], *w_fft_[m][m], *t_fft_[m][m];
/* Here ij and ji are aliased. We ask to put real* into complex*
slots: */
vec_get_array2 (m, c_fft, (void*) c_fft_);
vec_get_array2 (m, t_fft, (void*) t_fft_);
/* Diagonals are NULL, vec_get_array() passes them through: */
vec_get_array2 (m, w_fft, (void*) w_fft_);
const int n = vec_local_size (c_fft[0][0]);
assert (n % 2 == 0);
{
complex H[m][m], C[m][m], W[m][m], WC[m][m], T[m][m];
for (int k = 0; k < n / 2; k++)
{
/* for j <= i only: */
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
{
/*
Extract C and W for this particular momentum k from
scattered arrays into contiguous matrices.
*/
C[i][j] = C[j][i] = c_fft_[i][j][k];
/* Diagonal is implicitly 1: */
W[i][j] = W[j][i] = (i == j) ? 1.0 : w_fft_[i][j][k];
}
/* WC, an intermediate. See comment on layout of result
below. The input is symmetric, though: */
hnc3d_sles_zgemm (m, W, C, WC);
/* T := 1 - ρWC. The matrix T is used here as a free work
array: */
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
T[i][j] = delta (i, j) - rho * WC[i][j];
/*
H := WCW. Temporarily --- it will be overwritten with the
real H after solving the linear equations.
Note that the matrix multiplication is performed using the
Fortran (column major) interpetation of the matrix memory
layout. In Fortran notation:
H(i, j) = Σ WC(i, k) * W(k, j)
k
or, in C-notation:
H[j][i] = Σ WC[k][i] * W[j][k]
k
*/
hnc3d_sles_zgemm (m, WC, W, H);
/*
Solving the linear equation makes H have the literal meaning
of the total correlation matrix (input T is destroyed):
-1 -1
H := T * H == (1 - ρc) * c
*/
hnc3d_sles_zgesv (m, T, H);
/*
The same effect is achieved in 1x1 version of the code
differently:
T := H - C
*/
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
t_fft_[i][j][k] = H[i][j] - C[i][j];
}
}
/* Here vec_restore_array2() expects array of real*, we offer array
of complex* instead: */
vec_restore_array2 (m, c_fft, (void*) c_fft_);
vec_restore_array2 (m, t_fft, (void*) t_fft_);
/* The case with NULL on the diagonal is handled gracefully: */
vec_restore_array2 (m, w_fft, (void*) w_fft_);
}
static void
compute_t2 (int m, real rho, Vec c_fft[m][m], Vec w_fft[m][m], Vec t_fft[m][m])
{
if (m == 1)
/* Here w = 1, identically: */
compute_t2_1 (rho, c_fft[0][0], t_fft[0][0]); /* faster */
else
compute_t2_m (m, rho, c_fft, w_fft, t_fft); /* works for any m */
}
/*
There were historically two types of HNC iterations for pure
solvent:
a) HNC iteration for an indirect correlation t = h - c
t -> dt = t - t
in out in
where other intermediates are considered a function of that. In
particular in this case another important intermediate is y =
c(t).
b) HNC iteration for a direct correlation where the total correlation
is considered a function of that:
c -> dc = c - c
in out in
In this case the indirect correlation γ is considered a function
of c and is stored in y = t(c).
The case (a) with indirect correlation t as a primary variable won
over time. Maintaining two cases appeared infeasible at some point.
*/
typedef struct Ctx2
{
State *HD;
int m;
Vec *v_short; /* [m][m], in, real, center */
Vec *v_long_fft; /* [m][m], in, complex, center */
Vec *y; /* [m][m], work, real, y = c(t), not t(c) */
Vec *t_fft, *c_fft; /* [m][m], work, complex */
Vec *w_fft; /* [m][m], in, complex, corner, NULL diagonal */
} Ctx2;
static void
iterate_t2 (Ctx2 *ctx, Vec T, Vec dT)
{
const int m = ctx->m;
/* aliases of the correct [m][m] shape: */
Vec (*c_fft)[m] = (void*) ctx->c_fft;
Vec (*t_fft)[m] = (void*) ctx->t_fft;
Vec (*w_fft)[m] = (void*) ctx->w_fft; /* corner */
Vec (*c)[m] = (void*) ctx->y; /* y = c(t) here */
Vec (*v_short)[m] = (void*) ctx->v_short;
Vec (*v_long_fft)[m] = (void*) ctx->v_long_fft;
const State *HD = ctx->HD;
const ProblemData *PD = HD->PD;
const real rho = PD->rho;
const real beta = PD->beta;
const real L3 = volume (PD);
const real h3 = volume_element (PD);
/* Establish aliases to the subsections of the long Vec T and dT: */
local Vec t[m][m], dt[m][m];
vec_aliases_create2 (T, m, t);
vec_aliases_create2 (dT, m, dt);
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
{
compute_c (PD->closure, beta, v_short[i][j], t[i][j], c[i][j]);
MatMult (HD->fft_mat, c[i][j], c_fft[i][j]);
VecScale (c_fft[i][j], h3);
/*
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
*/
VecAXPY (c_fft[i][j], -beta, v_long_fft[i][j]);
/* Translate distribution to the grid corner. */
bgy3d_vec_fft_trans (HD->dc, PD->N, c_fft[i][j]);
}
/* Solves the OZ linear equation for t_fft[][].: */
compute_t2 (m, rho, c_fft, w_fft, t_fft);
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
{
/* Translate distribution to the grid center. */
bgy3d_vec_fft_trans (HD->dc, PD->N, t_fft[i][j]);
/*
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
*/
VecAXPY (t_fft[i][j], -beta, v_long_fft[i][j]);
MatMultTranspose (HD->fft_mat, t_fft[i][j], dt[i][j]);
VecScale (dt[i][j], 1.0/L3);
}
/*
This destroys the aliases, but does not free the memory, of
course. The actuall data is owned by Vec T and Vec dT. From now on
one may access T and dT directly again.
*/
vec_aliases_destroy2 (T, m, t);
vec_aliases_destroy2 (dT, m, dt);
VecAXPY (dT, -1.0, T);
}
/*
2
μ = h / 2 - c - h * (c + c ) / 2
S S L
This is a subexpression in the summation over all site-site pairs
where it is assumed that the long-range contribution in
- Σ c
uv uv
cancels out. This is the case when
c = -βQ q v
L,uv u v L
and either solute charges Q or the solvent charges q sum to zero.
Naturally, with an exact arithmetics one does not need to handle
short- and long-range correlations separately in this case. For
PSE-n closures the "renormalized indirect correlation" x = βv + t
appears in the expression of the chemical potential too.
The h² term contributes conditionally. Eventually, only depletion
regions (h < 0) contribute (KH). See Fortran sources for details.
*/
static void
compute_mu (ClosureEnum method, Vec x, Vec h, Vec cs, Vec cl, /* in */
Vec mu) /* out */
{
void f (int n, real x[n], real h[n], real cs[n], real cl[n], real mu[n])
{
rism_chempot_density (method, n, x, h, cs, cl, mu);
}
vec_app5 (f, x, h, cs, cl, mu);
}
static void
compute_mu1 (ClosureEnum method,
Vec x, Vec dx,
Vec h, Vec dh,
Vec c, Vec dc,
Vec cl, /* in */
Vec dm) /* out */
{
void f (int n,
real x[n], real dx[n],
real h[n], real dh[n],
real c[n], real dc[n],
real cl[n], real dm[n])
{
rism_chempot_density1 (method, n, x, dx, h, dh, c, dc, cl, dm);
}
vec_app8 (f, x, dx, h, dh, c, dc, cl, dm);
}
/*
Returns the β-scaled density of the chemical potential, βμ(r). To
get the excess chemical potential integrate it over the volume and
divide by β, cf.:
βμ = ρ ∫ [½h²(r) - c(r) - ½h(r)c(r)] d³r
Here we pass h(r) and c(r) and long-range part of c(r) for
solute-solvent pair. The solute might be the same as solvent, of
course. PSE-n closures need another quantity, the "renormalized
indirect correlation" x = -βv + t (referred to at other plasces as
t* sometimes).
Volume integral in cartesian grid is actually:
Vol(D) = ∫∫∫dxdydz
D
The shape of arrays passed here may be arbitrary, though in practice
it is either m x m or 1 x m:
*/
static void
chempot_density (ClosureEnum method, int n, int m,
Vec x[n][m], /* in */
Vec h[n][m], /* in */
Vec cs[n][m], /* in */
Vec cl[n][m], /* in, long range */
Vec mu) /* out */
{
/* increment for all solvent sites */
local Vec dmu = vec_duplicate (mu);
/* Clear accumulator: */
VecSet (mu, 0.0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
compute_mu (method, x[i][j], h[i][j], cs[i][j], cl[i][j], dmu);
VecAXPY (mu, 1.0, dmu);
}
vec_destroy (&dmu);
}
static void
chempot_density1 (ClosureEnum method, int n, int m,
Vec x[n][m], Vec dx[n][m], /* in */
Vec h[n][m], Vec dh[n][m], /* in */
Vec c[n][m], Vec dc[n][m], /* in */
Vec cl[n][m], /* in, long range */
Vec dm) /* out */
{
/* Increment for all solvent sites */
local Vec dm1 = vec_duplicate (dm);
/* Clear accumulator: */
VecSet (dm, 0.0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
compute_mu1 (method,
x[i][j], dx[i][j],
h[i][j], dh[i][j],
c[i][j], dc[i][j],
cl[i][j], dm1);
VecAXPY (dm, 1.0, dm1);
}
vec_destroy (&dm1);
}
/*
Interface to get chemical potential of solvent-solvent pair from Vec
x = βv + t, h, c, and cl, return the value of chemical potential.
The argument "closure" choses between HNC, KH and GF
functionals. The State struct also holds a setting for the closure
--- that one is ignored to allow computing any kind of functional.
*/
static real
chempot (const State *HD, ClosureEnum method, int n, int m,
Vec x[n][m], Vec h[n][m], Vec c[n][m], Vec cl[n][m]) /* in */
{
const ProblemData *PD = HD->PD;
const real beta = PD->beta;
const real h3 = volume_element (PD);
/* Vector for chemical potential density */
local Vec mu_dens = vec_create (HD->da);
/* Get β-scaled chemical potential density */
chempot_density (method, n, m, x, h, c, cl, mu_dens);
/* Volume integral scaled by a factor: */
const real mu = PD->rho * vec_sum (mu_dens) * h3 / beta;
vec_destroy (&mu_dens);
return mu;
}
static real
chempot1 (const State *HD, ClosureEnum method, int n, int m,
Vec x[n][m], Vec dx[n][m],
Vec h[n][m], Vec dh[n][m],
Vec c[n][m], Vec dc[n][m],
Vec cl[n][m]) /* in */
{
const ProblemData *PD = HD->PD;
const real beta = PD->beta;
const real h3 = volume_element (PD);
/* Vector for chemical potential density */
local Vec mu_dens = vec_create (HD->da);
/* Get β-scaled chemical potential density */
chempot_density1 (method, n, m, x, dx, h, dh, c, dc, cl, mu_dens);
/* Volume integral scaled by a factor: */
const real mu = PD->rho * vec_sum (mu_dens) * h3 / beta;
vec_destroy (&mu_dens);
return mu;
}
/*
Prints chemical potentials on tty. The default is marked with a
star.
FIXME: the idea to evaluate all functionals from the same input is
not as good as may seem on the first sight. Note that there is more
than one way to do that. Currently one evaluates h and c from the
solution t according to the native (SCF) closure and uses the form
of the chemical potential functional expressed via those x, h, and c
that corresponds to another closure. A different result would have
been obtained if one evaluated h and c using that second closure. It
is not clear which of the multiple ways is more consistent or
otherwise preferred. Basically the question boils down to what one
should consider final output of the SCF procedure to be used for
post-SCF application of the functionals.
*/
static void
print_chempot (const State *HD, int n, int m,
Vec x[n][m], Vec h[n][m], Vec c[n][m], Vec cl[n][m]) /* in */
{
/* FIXME: as implemented, for method == PY the default energy
functional is GF: */
const ClosureEnum methods[3] = {CLOSURE_HNC, CLOSURE_KH, CLOSURE_PY};
const char *names[3] = {"HNC", "KH", "GF"};
real mu[3];
/* Silent computing: */
for (int i = 0; i < 3; i++)
mu[i] = chempot (HD, methods[i], n, m, x, h, c, cl);
/* Printing only: */
PRINTF (" # Chemical potentials, default is marked with *:\n");
for (int i = 0; i < 3; i++)
PRINTF (" # mu = %f kcal (%s)%s\n", mu[i], names[i],
((methods[i] == HD->PD->closure)? "*" : ""));
}
/*
This function is used to calculate isothermal compressibility and
partial molar volume, with the formula used in Hirata's book
(pp. 148), in which the summation reads:
~
ρ∑ c (k=0) = ρ∑ ∫c (r) d³r
uv uv uv uv
Calculate the sum of volume integrals ∫c(r)d³r, scaled by ρ:
*/
static real
compute_kc (const State *HD, int n, int m, Vec c[n][m])
{
const ProblemData *PD = HD->PD;
real kc = 0.0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
kc += vec_sum (c[i][j]) * (PD->rho * volume_element (PD));
return kc;
}
/*
To calculate partial molar volume V as used by Palmer et al. (2010)
one may explore the following relation:
ρV = (ρκ/β) * [1 - ρ∫c(r)d³r],
where dimensionless ρV is a product of a dimensionless combination
(ρκ/β) with κ being pure solent isothermal compressibility and
another dimensionless integral. Solvent compressibiliy needs to be
calcualted seperately by a pure solvent calculation or inferred from
the solvent susceptibility. Here ρ is the solvent number density,
what else. This function computes the second PMV factor, 1 - ρ∫c(r)d³r:
*/
static real
pmv_factor (const State *HD, int n, int m, Vec c[n][m])
{
/* Volume integrals ρ∫c(r)d³r = ρc(k=0) summed over all solvent
sites: */
const real c0 = compute_kc (HD, n, m, c);
/* 1 - ρc(0) */
return (1 - c0);
}
/*
Calculate isothermal compressibility κ
~
κ = β / ρ[1 - ρ∑ c (k=0)]
vv' vv'
and the correction coefficient
~
a = ρ∑ c (k=0) / 2β
vv' vv'
*/
static void
print_kappa (const State *HD, int n, int m, Vec c[n][m])
{
const real rho = HD->PD->rho;
const real beta = HD->PD->beta;
/* Kernel ρ∫c(r)d³r = ρc(k=0): */
const real c0 = compute_kc (HD, n, m, c);
/* Coefficient a = ρc(0) / 2β */
PRINTF (" # Correction coefficent:\n");
PRINTF (" # a = %f kcal\n", c0 / 2 / beta);
/* κ = β / ρ[1 - c0] */
const real kappa = beta / (1 - c0) / rho;
PRINTF (" # Isothermal compressibility:\n");
PRINTF (" # kappa = %f A³/kcal\n", kappa);
}
static inline ProblemData
upscale (const ProblemData *PD)
{
/*
FIXME: how do we proceed if the user specified nrad and rmax in
the command line knowing better as he/she always does?
*/
ProblemData pd = *PD;
pd.rmax = 4 * MAX (MAX (PD->L[0], PD->L[1]), PD->L[2]) / 2;
pd.nrad = 16 * MAX (MAX (PD->N[0], PD->N[1]), PD->N[2]);
return pd;
}
/*
Solving for indirect correlation t = h - c and thus, also for direct
correlation c and other quantities of HNC equation. The indirect
correlation t appears as a primary variable here. All other
unknowns, including the direction correlation c, are functionals of
that.
Historically this code supported another mode where the direct
correlation c served as a primary variable. In that case the
indirect correlation t was a functional of that.
This 3D code operates with essentially 1D quantities represented on
3D grids (modulo finite grid artifacts). It competes with the proper
1D variant in rism.f90. The 1D code offers DRISM which this 3D code
does not support yet. See how we generate initial guess with the 1D
code here.
*/
void
hnc3d_solvent_solve (const ProblemData *PD,
int m, const Site solvent[m],
Vec g[m][m])
{
/* Code used to be verbose: */
bgy3d_problem_data_print (PD);
State *HD = bgy3d_state_make (PD); /* FIXME: rm unused fields */
PRINTF ("(iterations for γ)\n");
/*
For primary variable t there will be two exclusive ways to access
the data: via the long Vec T or m * (m + 1) / 2 shorter Vec
t[m][m] aliased to the subsections of the longer one.
*/
local Vec T = vec_pack_create2 (HD->da, m); /* long Vec */
/* Generate initial guess using much faster 1D code: */
{
/*
Problem data for use in 1d-RISM code. If you do not upscale, the
default rmax = max (PD->L) / 2 will be too low for
interpolation.
*/
ProblemData pd = upscale (PD);
const int nrad = pd.nrad;
const real rmax = pd.rmax;
real t_rad[m][m][nrad];
/* 1d-RISM calculation. Dont need neither susceptibility nor the
result dictionary, need only t(r): */
rism_solvent (&pd, m, solvent, t_rad, NULL, NULL);
{
local Vec t[m][m];
vec_aliases_create2 (T, m, t); /* aliases to subsections */
const real dr = rmax / nrad;
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
vec_rtab (HD, nrad, t_rad[i][j], dr, t[i][j]);
vec_aliases_destroy2 (T, m, t);
}
}
/*
Most of these Vecs will be a functional of primary variable t or
constant. Earlier versions supported direct correlation c as a
primary variable.
*/
local Vec c[m][m];
vec_create2 (HD->da, m, c);
/* Prepare intra-molecular correlations. The origin is at the corner
as suitable for convolutions. Diagonal will be NULL: */
local Vec w_fft[m][m];
bgy3d_omega_fft_create (HD, m, solvent, w_fft); /* creates them */
/* Solvent-solvent interaction is a sum of two terms, short-range
and long-range: */
local Vec v_short[m][m]; /* real */
vec_create2 (HD->da, m, v_short);
local Vec v_long_fft[m][m]; /* complex */
vec_create2 (HD->dc, m, v_long_fft);
/* Get solvent-solvent site-site interactions: */
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
bgy3d_pair_potential (HD, solvent[i], solvent[j],
v_short[i][j], v_long_fft[i][j]);
{
local Vec t_fft[m][m];
vec_create2 (HD->dc, m, t_fft); /* complex */
local Vec c_fft[m][m];
vec_create2 (HD->dc, m, c_fft); /* complex */
/*
Find a T such that dT as returned by iterate_t2 (&ctx, T, dT) is
zero. Cast is there to silence the mismatch in the type of
first pointer argument: struct Ctx2* vs. void*:
*/
{
Ctx2 ctx =
{
.HD = HD,
.m = m,
.v_short = (void*) v_short, /* in */
.v_long_fft = (void*) v_long_fft, /* in */
.y = (void*) c, /* work, c(t)*/
.t_fft = (void*) t_fft, /* work, fft(t) */
.c_fft = (void*) c_fft, /* work, fft(c(t)) */
.w_fft = (void*) w_fft, /* in */
};
/* FIXME: no Jacobian yet! */
bgy3d_snes_default (PD, &ctx, (VecFunc1) iterate_t2, NULL, T);
}
/* Free local stuff */
vec_destroy2 (m, t_fft);
vec_destroy2 (m, c_fft);
}
/*
The approach with the primary variable x == t won over time. The
direct correlation c(t) is a dependent quantity! FIXME: we assume
that the value of c on exit from the SNES solver corresponds to
the final t. If not, recompute it with compute_c() again.
*/
/* Now that T has converged we will post-process it using the
aliases t[][]: */
local Vec t[m][m];
vec_aliases_create2 (T, m, t); /* aliases to subsections */
Vec h[m][m]; /* FIXME: not deallocated! */
vec_create2 (HD->da, m, h);
/* Compute h = c + t: */
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
VecWAXPY (h[i][j], 1.0, c[i][j], t[i][j]);
/* Chemical potential */
{
const real beta = HD->PD->beta;
const real L3 = volume (PD);
/*
NOTE: this is the only place where one needs real-space rep of
the long-range Coulomb. So far it is computed by FFT.
Alternative is to tablulate it on the real-space grid using the
analytic expression. Surprisingly, for a couple of tests we made
(LJC, TIP3P) the difference between the two approaches appears
small.
FIXME: For the essentially 3D potentials in the solute/solvent
code we already have the option to get the real space rep
directly. Should we also do it here for consistency?
FIXME: also all of v_long_fft[][] differ only by factors, see
how we deal with that to save space in solute/solvent code.
*/
local Vec cl[m][m]; /* real-space long-range correlation */
vec_create2 (HD->da, m, cl);
/* Renormalized indirect correlation x = -βv + t appears in
expression for chemical potentials with PSE-n closures :*/
local Vec x[m][m];
vec_create2 (HD->da, m, x);
for (int i = 0; i < m; i++)
for (int j = 0; j <= i; j++)
{
/* Get real representation of long-range Coulomb potential */
MatMultTranspose (HD->fft_mat, v_long_fft[i][j], cl[i][j]);
/*
Scale Vec cl to get long-range correlation (division by
volume is part of the inverse FFT):
c = -βv
L L
*/
VecScale (cl[i][j], -beta/L3);
/* Renormalized indirect correlation: x = -βv + t: */
VecWAXPY (x[i][j], -beta, v_short[i][j], t[i][j]);
}
/* The function chempot() operates on rectangular arrays, we pass
an m x m square ones: */
print_chempot (HD, m, m, x, h, c, cl);
vec_destroy2 (m, cl);
vec_destroy2 (m, x);
}
/* Isothermal compressibility: */
print_kappa (HD, m, m, c);
/* No more used: */
vec_aliases_destroy2 (T, m, t);
vec_pack_destroy2 (&T);
vec_destroy2 (m, c);
vec_destroy2 (m, v_short);
vec_destroy2 (m, v_long_fft);
/*
Compute the solvent susceptibility, χ = ω + ρh, in Fourier