forked from zmeri/PC-SAFT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcsaft_electrolyte.cpp
2475 lines (2217 loc) · 99.7 KB
/
pcsaft_electrolyte.cpp
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
#include <vector>
#include <string>
#include <cmath>
#include "math.h"
#include "externals/eigen/Eigen/Dense"
#include "pcsaft_electrolyte.h"
using std::vector;
#if defined(HUGE_VAL) && !defined(_HUGE)
# define _HUGE HUGE_VAL
#else
// GCC Version of huge value macro
#if defined(HUGE) && !defined(_HUGE)
# define _HUGE HUGE
#endif
#endif
vector<double> XA_find(vector<double> XA_guess, vector<double> delta_ij, double den,
vector<double> x) {
/**Iterate over this function in order to solve for XA*/
int num_sites = XA_guess.size();
vector<double> XA = XA_guess;
int idxij = -1; // index for delta_ij
for (int i = 0; i < num_sites; i++) {
double summ = 0.;
for (int j = 0; j < num_sites; j++) {
idxij += 1;
summ += den*x[j]*XA_guess[j]*delta_ij[idxij];
}
XA[i] = 1./(1.+summ);
}
return XA;
}
vector<double> dXAdt_find(vector<double> delta_ij, double den,
vector<double> XA, vector<double> ddelta_dt, vector<double> x) {
/**Solve for the derivative of XA with respect to temperature.*/
int num_sites = XA.size();
Eigen::MatrixXd B = Eigen::MatrixXd::Zero(num_sites, 1);
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(num_sites, num_sites);
double summ;
int ij = 0;
for (int i = 0; i < num_sites; i++) {
summ = 0;
for (int j = 0; j < num_sites; j++) {
B(i) -= x[j]*XA[j]*ddelta_dt[ij];
A(i,j) = x[j]*delta_ij[ij];
summ += x[j]*XA[j]*delta_ij[ij];
ij += 1;
}
A(i,i) = pow(1+den*summ, 2.)/den;
}
Eigen::MatrixXd solution = A.lu().solve(B); //Solves linear system of equations
vector<double> dXA_dt(num_sites);
for (int i = 0; i < num_sites; i++) {
dXA_dt[i] = solution(i);
}
return dXA_dt;
}
vector<double> dXAdx_find(vector<int> assoc_num, vector<double> delta_ij,
double den, vector<double> XA, vector<double> ddelta_dx, vector<double> x) {
/**Solve for the derivative of XA with respect to composition, or actually
rho_i (the molar density of component i, which equals x_i * rho).*/
int num_sites = XA.size();
int ncomp = assoc_num.size();
Eigen::MatrixXd B(num_sites*ncomp, 1);
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(num_sites*ncomp, num_sites*ncomp);
double sum1, sum2;
int idx1 = 0;
int ij = 0;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < num_sites; j++) {
sum1 = 0;
for (int k = 0; k < num_sites; k++) {
sum1 = sum1 + den*x[k]*(XA[k]*ddelta_dx[i*num_sites*num_sites + j*num_sites + k]);
A(ij,i*num_sites+k) = XA[j]*XA[j]*den*x[k]*delta_ij[j*num_sites+k];
}
sum2 = 0;
for (int l = 0; l < assoc_num[i]; l++) {
sum2 = sum2 + XA[idx1+l]*delta_ij[idx1*num_sites+l*num_sites+j];
}
A(ij,ij) = A(ij,ij) + 1;
B(ij) = -1*XA[j]*XA[j]*(sum1 + sum2);
ij += 1;
}
idx1 += assoc_num[i];
}
Eigen::MatrixXd solution = A.lu().solve(B); //Solves linear system of equations
vector<double> dXA_dx(num_sites*ncomp);
for (int i = 0; i < num_sites*ncomp; i++) {
dXA_dx[i] = solution(i);
}
return dXA_dx;
}
double pcsaft_Z_cpp(double t, double rho, vector<double> x, add_args &cppargs) {
/**
Calculate the compressibility factor.
*/
int ncomp = x.size(); // number of components
vector<double> d (ncomp);
for (int i = 0; i < ncomp; i++) {
d[i] = cppargs.s[i]*(1-0.12*exp(-3*cppargs.e[i]/t));
}
if (!cppargs.z.empty()) {
for (int i = 0; i < ncomp; i++) {
if (cppargs.z[i] != 0) {
d[i] = cppargs.s[i]*(1-0.12); // for ions the diameter is assumed to be temperature independent (see Held et al. 2014)
}
}
}
double den = rho*N_AV/1.0e30;
vector<double> zeta (4, 0);
double summ;
for (int i = 0; i < 4; i++) {
summ = 0;
for (int j = 0; j < ncomp; j++) {
summ += x[j]*cppargs.m[j]*pow(d[j], i);
}
zeta[i] = PI/6*den*summ;
}
double eta = zeta[3];
double m_avg = 0;
for (int i = 0; i < ncomp; i++) {
m_avg += x[i]*cppargs.m[i];
}
vector<double> ghs (ncomp*ncomp, 0);
vector<double> denghs (ncomp*ncomp, 0);
vector<double> e_ij (ncomp*ncomp, 0);
vector<double> s_ij (ncomp*ncomp, 0);
double m2es3 = 0.;
double m2e2s3 = 0.;
int idx = -1;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {
idx += 1;
if (cppargs.l_ij.empty()) {
s_ij[idx] = (cppargs.s[i] + cppargs.s[j])/2.;
}
else {
s_ij[idx] = (cppargs.s[i] + cppargs.s[j])/2.*(1-cppargs.l_ij[idx]);
}
if (!cppargs.z.empty()) {
if (cppargs.z[i]*cppargs.z[j] <= 0) { // for two cations or two anions e_ij is kept at zero to avoid dispersion between like ions (see Held et al. 2014)
if (cppargs.k_ij.empty()) {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j]);
}
else {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j])*(1-cppargs.k_ij[idx]);
}
}
} else {
if (cppargs.k_ij.empty()) {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j]);
}
else {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j])*(1-cppargs.k_ij[idx]);
}
}
m2es3 = m2es3 + x[i]*x[j]*cppargs.m[i]*cppargs.m[j]*e_ij[idx]/t*pow(s_ij[idx], 3);
m2e2s3 = m2e2s3 + x[i]*x[j]*cppargs.m[i]*cppargs.m[j]*pow(e_ij[idx]/t,2)*pow(s_ij[idx], 3);
ghs[idx] = 1/(1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
denghs[idx] = zeta[3]/(1-zeta[3])/(1-zeta[3]) +
(d[i]*d[j]/(d[i]+d[j]))*(3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
6*zeta[2]*zeta[3]/pow(1-zeta[3], 3)) +
pow(d[i]*d[j]/(d[i]+d[j]), 2)*(4*zeta[2]*zeta[2]/pow(1-zeta[3], 3) +
6*zeta[2]*zeta[2]*zeta[3]/pow(1-zeta[3], 4));
}
}
double Zhs = zeta[3]/(1-zeta[3]) + 3.*zeta[1]*zeta[2]/zeta[0]/(1.-zeta[3])/(1.-zeta[3]) +
(3.*pow(zeta[2], 3.) - zeta[3]*pow(zeta[2], 3.))/zeta[0]/pow(1.-zeta[3], 3.);
static double a0[7] = { 0.910563145, 0.636128145, 2.686134789, -26.54736249, 97.75920878, -159.5915409, 91.29777408 };
static double a1[7] = { -0.308401692, 0.186053116, -2.503004726, 21.41979363, -65.25588533, 83.31868048, -33.74692293 };
static double a2[7] = { -0.090614835, 0.452784281, 0.596270073, -1.724182913, -4.130211253, 13.77663187, -8.672847037 };
static double b0[7] = { 0.724094694, 2.238279186, -4.002584949, -21.00357682, 26.85564136, 206.5513384, -355.6023561 };
static double b1[7] = { -0.575549808, 0.699509552, 3.892567339, -17.21547165, 192.6722645, -161.8264617, -165.2076935 };
static double b2[7] = { 0.097688312, -0.255757498, -9.155856153, 20.64207597, -38.80443005, 93.62677408, -29.66690559 };
vector<double> a (7, 0);
vector<double> b (7, 0);
for (int i = 0; i < 7; i++) {
a[i] = a0[i] + (m_avg-1.)/m_avg*a1[i] + (m_avg-1.)/m_avg*(m_avg-2.)/m_avg*a2[i];
b[i] = b0[i] + (m_avg-1.)/m_avg*b1[i] + (m_avg-1.)/m_avg*(m_avg-2.)/m_avg*b2[i];
}
double detI1_det = 0.0;
double detI2_det = 0.0;
double I2 = 0.0;
for (int i = 0; i < 7; i++) {
detI1_det += a[i]*(i+1)*pow(eta, i);
detI2_det += b[i]*(i+1)*pow(eta, i);
I2 += b[i]*pow(eta, i);
}
double C1 = 1./(1. + m_avg*(8*eta-2*eta*eta)/pow(1-eta, 4) + (1-m_avg)*(20*eta-27*eta*eta+12*pow(eta, 3)-2*pow(eta, 4))/pow((1-eta)*(2-eta), 2.0));
double C2 = -1.*C1*C1*(m_avg*(-4*eta*eta+20*eta+8)/pow(1-eta, 5) + (1-m_avg)*(2*pow(eta, 3)+12*eta*eta-48*eta+40)/pow((1-eta)*(2-eta), 3.0));
summ = 0.0;
for (int i = 0; i < ncomp; i++) {
summ += x[i]*(cppargs.m[i]-1)/ghs[i*ncomp+i]*denghs[i*ncomp+i];
}
double Zid = 1.0;
double Zhc = m_avg*Zhs - summ;
double Zdisp = -2*PI*den*detI1_det*m2es3 - PI*den*m_avg*(C1*detI2_det + C2*eta*I2)*m2e2s3;
// Dipole term (Gross and Vrabec term) --------------------------------------
double Zpolar = 0;
if (!cppargs.dipm.empty()) {
double A2 = 0.;
double A3 = 0.;
double dA2_det = 0.;
double dA3_det = 0.;
vector<double> adip (5, 0);
vector<double> bdip (5, 0);
vector<double> cdip (5, 0);
vector<double> dipmSQ (ncomp, 0);
double J2, dJ2_det, J3, dJ3_det;
static double a0dip[5] = { 0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308 };
static double a1dip[5] = { 0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135 };
static double a2dip[5] = { -1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575 };
static double b0dip[5] = { 0.2187939, -1.1896431, 1.1626889, 0, 0 };
static double b1dip[5] = { -0.5873164, 1.2489132, -0.5085280, 0, 0 };
static double b2dip[5] = { 3.4869576, -14.915974, 15.372022, 0, 0 };
static double c0dip[5] = { -0.0646774, 0.1975882, -0.8087562, 0.6902849, 0 };
static double c1dip[5] = { -0.9520876, 2.9924258, -2.3802636, -0.2701261, 0 };
static double c2dip[5] = { -0.6260979, 1.2924686, 1.6542783, -3.4396744, 0 };
const static double conv = 7242.702976750923; // conversion factor, see the note below Table 2 in Gross and Vrabec 2006
for (int i = 0; i < ncomp; i++) {
dipmSQ[i] = pow(cppargs.dipm[i], 2.)/(cppargs.m[i]*cppargs.e[i]*pow(cppargs.s[i],3.))*conv;
}
double m_ij;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {
m_ij = sqrt(cppargs.m[i]*cppargs.m[j]);
if (m_ij > 2) {
m_ij = 2;
}
J2 = 0.;
dJ2_det = 0.;
for (int l = 0; l < 5; l++) {
adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
J2 += (adip[l] + bdip[l]*e_ij[j*ncomp+j]/t)*pow(eta, l); // j*ncomp+j needs to be used for e_ij because it is formatted as a 1D vector
dJ2_det += (adip[l] + bdip[l]*e_ij[j*ncomp+j]/t)*l*pow(eta, l-1);
}
A2 += x[i]*x[j]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)/
pow(s_ij[i*ncomp+j],3)*cppargs.dip_num[i]*cppargs.dip_num[j]*dipmSQ[i]*dipmSQ[j]*J2;
dA2_det += x[i]*x[j]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*pow(s_ij[i*ncomp+i],3)*
pow(s_ij[j*ncomp+j],3)/pow(s_ij[i*ncomp+j],3)*cppargs.dip_num[i]*cppargs.dip_num[j]*dipmSQ[i]*dipmSQ[j]*dJ2_det;
}
}
double m_ijk;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {
for (int k = 0; k < ncomp; k++) {
m_ijk = pow((cppargs.m[i]*cppargs.m[j]*cppargs.m[k]),1/3.);
if (m_ijk > 2) {
m_ijk = 2;
}
J3 = 0.;
dJ3_det = 0.;
for (int l = 0; l < 5; l++) {
cdip[l] = c0dip[l] + (m_ijk-1)/m_ijk*c1dip[l] + (m_ijk-1)/m_ijk*(m_ijk-2)/m_ijk*c2dip[l];
J3 += cdip[l]*pow(eta, l);
dJ3_det += cdip[l]*l*pow(eta, (l-1));
}
A3 += x[i]*x[j]*x[k]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*e_ij[k*ncomp+k]/t*
pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)*pow(s_ij[k*ncomp+k],3)/s_ij[i*ncomp+j]/s_ij[i*ncomp+k]/
s_ij[j*ncomp+k]*cppargs.dip_num[i]*cppargs.dip_num[j]*cppargs.dip_num[k]*dipmSQ[i]*
dipmSQ[j]*dipmSQ[k]*J3;
dA3_det += x[i]*x[j]*x[k]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*e_ij[k*ncomp+k]/t*
pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)*pow(s_ij[k*ncomp+k],3)/s_ij[i*ncomp+j]/s_ij[i*ncomp+k]/
s_ij[j*ncomp+k]*cppargs.dip_num[i]*cppargs.dip_num[j]*cppargs.dip_num[k]*dipmSQ[i]*
dipmSQ[j]*dipmSQ[k]*dJ3_det;
}
}
}
A2 = -PI*den*A2;
A3 = -4/3.*PI*PI*den*den*A3;
dA2_det = -PI*den*dA2_det;
dA3_det = -4/3.*PI*PI*den*den*dA3_det;
if (A2 != 0) { // when the mole fraction of the polar compounds is 0 then A2 = 0 and division by 0 occurs
Zpolar = eta*((dA2_det*(1-A3/A2)+(dA3_det*A2-A3*dA2_det)/A2)/(1-A3/A2)/(1-A3/A2));
}
}
// Association term -------------------------------------------------------
double Zassoc = 0;
if (!cppargs.e_assoc.empty()) {
int num_sites = 0;
vector<int> iA; //indices of associating compounds
for(std::vector<int>::iterator it = cppargs.assoc_num.begin(); it != cppargs.assoc_num.end(); ++it) {
num_sites += *it;
for (int i = 0; i < *it; i++) {
iA.push_back(it - cppargs.assoc_num.begin());
}
}
vector<double> x_assoc(num_sites); // mole fractions of only the associating compounds
for (int i = 0; i < num_sites; i++) {
x_assoc[i] = x[iA[i]];
}
vector<double> XA (num_sites, 0);
vector<double> delta_ij(num_sites * num_sites, 0);
int idxa = 0;
int idxi = 0; // index for the ii-th compound
int idxj = 0; // index for the jj-th compound
for (int i = 0; i < num_sites; i++) {
idxi = iA[i]*ncomp+iA[i];
for (int j = 0; j < num_sites; j++) {
idxj = iA[j]*ncomp+iA[j];
if (cppargs.assoc_matrix[idxa] != 0) {
double eABij = (cppargs.e_assoc[iA[i]]+cppargs.e_assoc[iA[j]])/2.;
double volABij = _HUGE;
if (cppargs.k_hb.empty()) {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
}
else {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3)*(1-cppargs.k_hb[iA[i]*ncomp+iA[j]]);
}
delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/t)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
}
idxa += 1;
}
XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
if (!std::isfinite(XA[i])) {
XA[i] = 0.02;
}
}
vector<double> ddelta_dx(num_sites * num_sites * ncomp, 0);
int idx_ddelta = 0;
for (int k = 0; k < ncomp; k++) {
int idxi = 0; // index for the ii-th compound
int idxj = 0; // index for the jj-th compound
idxa = 0;
for (int i = 0; i < num_sites; i++) {
idxi = iA[i]*ncomp+iA[i];
for (int j = 0; j < num_sites; j++) {
idxj = iA[j]*ncomp+iA[j];
if (cppargs.assoc_matrix[idxa] != 0) {
double eABij = (cppargs.e_assoc[iA[i]]+cppargs.e_assoc[iA[j]])/2.;
double volABij = _HUGE;
if (cppargs.k_hb.empty()) {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
}
else {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3)*(1-cppargs.k_hb[iA[i]*ncomp+iA[j]]);
}
double dghsd_dx = PI/6.*cppargs.m[k]*(pow(d[k], 3)/(1-zeta[3])/(1-zeta[3]) + 3*d[iA[i]]*d[iA[j]]/
(d[iA[i]]+d[iA[j]])*(d[k]*d[k]/(1-zeta[3])/(1-zeta[3])+2*pow(d[k], 3)*
zeta[2]/pow(1-zeta[3], 3)) + 2*pow((d[iA[i]]*d[iA[j]]/(d[iA[i]]+d[iA[j]])), 2)*
(2*d[k]*d[k]*zeta[2]/pow(1-zeta[3], 3)+3*(pow(d[k], 3)*zeta[2]*zeta[2]
/pow(1-zeta[3], 4))));
ddelta_dx[idx_ddelta] = dghsd_dx*(exp(eABij/t)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
}
idx_ddelta += 1;
idxa += 1;
}
}
}
int ctr = 0;
double dif = 1000.;
vector<double> XA_old = XA;
while ((ctr < 100) && (dif > 1e-15)) {
ctr += 1;
XA = XA_find(XA_old, delta_ij, den, x_assoc);
dif = 0.;
for (int i = 0; i < num_sites; i++) {
dif += std::abs(XA[i] - XA_old[i]);
}
for (int i = 0; i < num_sites; i++) {
XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
}
}
vector<double> dXA_dx(num_sites*ncomp, 0);
dXA_dx = dXAdx_find(cppargs.assoc_num, delta_ij, den, XA, ddelta_dx, x_assoc);
summ = 0.;
int ij = 0;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < num_sites; j++) {
summ += x[i]*den*x[iA[j]]*(1/XA[j]-0.5)*dXA_dx[ij];
ij += 1;
}
}
Zassoc = summ;
}
// Ion term ---------------------------------------------------------------
double Zion = 0;
if (!cppargs.z.empty()) {
vector<double> q(cppargs.z.begin(), cppargs.z.end());
for (int i = 0; i < ncomp; i++) {
q[i] = q[i]*E_CHRG;
}
summ = 0.;
for (int i = 0; i < ncomp; i++) {
summ += cppargs.z[i]*cppargs.z[i]*x[i];
}
double kappa = sqrt(den*E_CHRG*E_CHRG/kb/t/(cppargs.dielc*perm_vac)*summ); // the inverse Debye screening length. Equation 4 in Held et al. 2008.
if (kappa != 0) {
double chi, sigma_k;
summ = 0.;
for (int i = 0; i < ncomp; i++) {
chi = 3/pow(kappa*cppargs.s[i], 3)*(1.5 + log(1+kappa*cppargs.s[i]) - 2*(1+kappa*cppargs.s[i]) +
0.5*pow(1+kappa*cppargs.s[i], 2));
sigma_k = -2*chi+3/(1+kappa*cppargs.s[i]);
summ += q[i]*q[i]*x[i]*sigma_k;
}
Zion = -1*kappa/24./PI/kb/t/(cppargs.dielc*perm_vac)*summ;
}
}
double Z = Zid + Zhc + Zdisp + Zpolar + Zassoc + Zion;
return Z;
}
vector<double> pcsaft_fugcoef_cpp(double t, double rho, vector<double> x, add_args &cppargs) {
/**
Calculate the fugacity coefficients for one phase of the system.
*/
int ncomp = x.size(); // number of components
vector<double> d (ncomp);
for (int i = 0; i < ncomp; i++) {
d[i] = cppargs.s[i]*(1-0.12*exp(-3*cppargs.e[i]/t));
}
if (!cppargs.z.empty()) {
for (int i = 0; i < ncomp; i++) {
if (cppargs.z[i] != 0) {
d[i] = cppargs.s[i]*(1-0.12); // for ions the diameter is assumed to be temperature independent (see Held et al. 2014)
}
}
}
double den = rho*N_AV/1.0e30;
vector<double> zeta (4, 0);
double summ;
for (int i = 0; i < 4; i++) {
summ = 0;
for (int j = 0; j < ncomp; j++) {
summ += x[j]*cppargs.m[j]*pow(d[j], i);
}
zeta[i] = PI/6*den*summ;
}
double eta = zeta[3];
double m_avg = 0;
for (int i = 0; i < ncomp; i++) {
m_avg += x[i]*cppargs.m[i];
}
vector<double> ghs(ncomp*ncomp, 0);
vector<double> denghs(ncomp*ncomp, 0);
vector<double> e_ij(ncomp*ncomp, 0);
vector<double> s_ij(ncomp*ncomp, 0);
double m2es3 = 0.;
double m2e2s3 = 0.;
int idx = -1;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {
idx += 1;
if (cppargs.l_ij.empty()) {
s_ij[idx] = (cppargs.s[i] + cppargs.s[j])/2.;
}
else {
s_ij[idx] = (cppargs.s[i] + cppargs.s[j])/2.*(1-cppargs.l_ij[idx]);
}
if (!cppargs.z.empty()) {
if (cppargs.z[i]*cppargs.z[j] <= 0) { // for two cations or two anions e_ij is kept at zero to avoid dispersion between like ions (see Held et al. 2014)
if (cppargs.k_ij.empty()) {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j]);
}
else {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j])*(1-cppargs.k_ij[idx]);
}
}
} else {
if (cppargs.k_ij.empty()) {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j]);
}
else {
e_ij[idx] = sqrt(cppargs.e[i]*cppargs.e[j])*(1-cppargs.k_ij[idx]);
}
}
m2es3 = m2es3 + x[i]*x[j]*cppargs.m[i]*cppargs.m[j]*e_ij[idx]/t*pow(s_ij[idx], 3);
m2e2s3 = m2e2s3 + x[i]*x[j]*cppargs.m[i]*cppargs.m[j]*pow(e_ij[idx]/t,2)*pow(s_ij[idx], 3);
ghs[idx] = 1/(1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
denghs[idx] = zeta[3]/(1-zeta[3])/(1-zeta[3]) +
(d[i]*d[j]/(d[i]+d[j]))*(3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
6*zeta[2]*zeta[3]/pow(1-zeta[3], 3)) +
pow(d[i]*d[j]/(d[i]+d[j]), 2)*(4*zeta[2]*zeta[2]/pow(1-zeta[3], 3) +
6*zeta[2]*zeta[2]*zeta[3]/pow(1-zeta[3], 4));
}
}
double ares_hs = 1/zeta[0]*(3*zeta[1]*zeta[2]/(1-zeta[3]) + pow(zeta[2], 3.)/(zeta[3]*pow(1-zeta[3],2))
+ (pow(zeta[2], 3.)/pow(zeta[3], 2.) - zeta[0])*log(1-zeta[3]));
double Zhs = zeta[3]/(1-zeta[3]) + 3.*zeta[1]*zeta[2]/zeta[0]/(1.-zeta[3])/(1.-zeta[3]) +
(3.*pow(zeta[2], 3.) - zeta[3]*pow(zeta[2], 3.))/zeta[0]/pow(1.-zeta[3], 3.);
static double a0[7] = { 0.910563145, 0.636128145, 2.686134789, -26.54736249, 97.75920878, -159.5915409, 91.29777408 };
static double a1[7] = { -0.308401692, 0.186053116, -2.503004726, 21.41979363, -65.25588533, 83.31868048, -33.74692293 };
static double a2[7] = { -0.090614835, 0.452784281, 0.596270073, -1.724182913, -4.130211253, 13.77663187, -8.672847037 };
static double b0[7] = { 0.724094694, 2.238279186, -4.002584949, -21.00357682, 26.85564136, 206.5513384, -355.6023561 };
static double b1[7] = { -0.575549808, 0.699509552, 3.892567339, -17.21547165, 192.6722645, -161.8264617, -165.2076935 };
static double b2[7] = { 0.097688312, -0.255757498, -9.155856153, 20.64207597, -38.80443005, 93.62677408, -29.66690559 };
vector<double> a (7, 0);
vector<double> b (7, 0);
for (int i = 0; i < 7; i++) {
a[i] = a0[i] + (m_avg-1.)/m_avg*a1[i] + (m_avg-1.)/m_avg*(m_avg-2.)/m_avg*a2[i];
b[i] = b0[i] + (m_avg-1.)/m_avg*b1[i] + (m_avg-1.)/m_avg*(m_avg-2.)/m_avg*b2[i];
}
double detI1_det = 0.0;
double detI2_det = 0.0;
double I1 = 0.0;
double I2 = 0.0;
for (int i = 0; i < 7; i++) {
detI1_det += a[i]*(i+1)*pow(eta, i);
detI2_det += b[i]*(i+1)*pow(eta, i);
I2 += b[i]*pow(eta, i);
I1 += a[i]*pow(eta, i);
}
double C1 = 1./(1. + m_avg*(8*eta-2*eta*eta)/pow(1-eta, 4) + (1-m_avg)*(20*eta-27*eta*eta+12*pow(eta, 3)-2*pow(eta, 4))/pow((1-eta)*(2-eta), 2.0));
double C2 = -1.*C1*C1*(m_avg*(-4*eta*eta+20*eta+8)/pow(1-eta, 5) + (1-m_avg)*(2*pow(eta, 3)+12*eta*eta-48*eta+40)/pow((1-eta)*(2-eta), 3.0));
summ = 0.0;
for (int i = 0; i < ncomp; i++) {
summ += x[i]*(cppargs.m[i]-1)*log(ghs[i*ncomp+i]);
}
double ares_hc = m_avg*ares_hs - summ;
double ares_disp = -2*PI*den*I1*m2es3 - PI*den*m_avg*C1*I2*m2e2s3;
summ = 0.0;
for (int i = 0; i < ncomp; i++) {
summ += x[i]*(cppargs.m[i]-1)/ghs[i*ncomp+i]*denghs[i*ncomp+i];
}
double Zhc = m_avg*Zhs - summ;
double Zdisp = -2*PI*den*detI1_det*m2es3 - PI*den*m_avg*(C1*detI2_det + C2*eta*I2)*m2e2s3;
vector<double> dghsii_dx(ncomp*ncomp, 0);
vector<double> dahs_dx(ncomp, 0);
vector<double> dzeta_dx(4, 0);
idx = -1;
for (int i = 0; i < ncomp; i++) {
for (int l = 0; l < 4; l++) {
dzeta_dx[l] = PI/6.*den*cppargs.m[i]*pow(d[i],l);
}
for (int j = 0; j < ncomp; j++) {
idx += 1;
dghsii_dx[idx] = dzeta_dx[3]/(1-zeta[3])/(1-zeta[3]) + (d[j]*d[j]/(d[j]+d[j]))*
(3*dzeta_dx[2]/(1-zeta[3])/(1-zeta[3]) + 6*zeta[2]*dzeta_dx[3]/pow(1-zeta[3],3))
+ pow(d[j]*d[j]/(d[j]+d[j]),2)*(4*zeta[2]*dzeta_dx[2]/pow(1-zeta[3],3)
+ 6*zeta[2]*zeta[2]*dzeta_dx[3]/pow(1-zeta[3],4));
}
dahs_dx[i] = -dzeta_dx[0]/zeta[0]*ares_hs + 1/zeta[0]*(3*(dzeta_dx[1]*zeta[2]
+ zeta[1]*dzeta_dx[2])/(1-zeta[3]) + 3*zeta[1]*zeta[2]*dzeta_dx[3]
/(1-zeta[3])/(1-zeta[3]) + 3*zeta[2]*zeta[2]*dzeta_dx[2]/zeta[3]/(1-zeta[3])/(1-zeta[3])
+ pow(zeta[2],3)*dzeta_dx[3]*(3*zeta[3]-1)/zeta[3]/zeta[3]/pow(1-zeta[3],3)
+ log(1-zeta[3])*((3*zeta[2]*zeta[2]*dzeta_dx[2]*zeta[3] -
2*pow(zeta[2],3)*dzeta_dx[3])/pow(zeta[3],3) - dzeta_dx[0]) +
(zeta[0]-pow(zeta[2],3)/zeta[3]/zeta[3])*dzeta_dx[3]/(1-zeta[3]));
}
vector<double> dadisp_dx(ncomp, 0);
vector<double> dahc_dx(ncomp, 0);
double dzeta3_dx, daa_dx, db_dx, dI1_dx, dI2_dx, dm2es3_dx, dm2e2s3_dx, dC1_dx;
for (int i = 0; i < ncomp; i++) {
dzeta3_dx = PI/6.*den*cppargs.m[i]*pow(d[i],3);
dI1_dx = 0.0;
dI2_dx = 0.0;
dm2es3_dx = 0.0;
dm2e2s3_dx = 0.0;
for (int l = 0; l < 7; l++) {
daa_dx = cppargs.m[i]/m_avg/m_avg*a1[l] + cppargs.m[i]/m_avg/m_avg*(3-4/m_avg)*a2[l];
db_dx = cppargs.m[i]/m_avg/m_avg*b1[l] + cppargs.m[i]/m_avg/m_avg*(3-4/m_avg)*b2[l];
dI1_dx += a[l]*l*dzeta3_dx*pow(eta,l-1) + daa_dx*pow(eta,l);
dI2_dx += b[l]*l*dzeta3_dx*pow(eta,l-1) + db_dx*pow(eta,l);
}
for (int j = 0; j < ncomp; j++) {
dm2es3_dx += x[j]*cppargs.m[j]*(e_ij[i*ncomp+j]/t)*pow(s_ij[i*ncomp+j],3);
dm2e2s3_dx += x[j]*cppargs.m[j]*pow(e_ij[i*ncomp+j]/t,2)*pow(s_ij[i*ncomp+j],3);
dahc_dx[i] += x[j]*(cppargs.m[j]-1)/ghs[j*ncomp+j]*dghsii_dx[i*ncomp+j];
}
dm2es3_dx = dm2es3_dx*2*cppargs.m[i];
dm2e2s3_dx = dm2e2s3_dx*2*cppargs.m[i];
dahc_dx[i] = cppargs.m[i]*ares_hs + m_avg*dahs_dx[i] - dahc_dx[i] - (cppargs.m[i]-1)*log(ghs[i*ncomp+i]);
dC1_dx = C2*dzeta3_dx - C1*C1*(cppargs.m[i]*(8*eta-2*eta*eta)/pow(1-eta,4) -
cppargs.m[i]*(20*eta-27*eta*eta+12*pow(eta,3)-2*pow(eta,4))/pow((1-eta)*(2-eta),2));
dadisp_dx[i] = -2*PI*den*(dI1_dx*m2es3 + I1*dm2es3_dx) - PI*den
*((cppargs.m[i]*C1*I2 + m_avg*dC1_dx*I2 + m_avg*C1*dI2_dx)*m2e2s3
+ m_avg*C1*I2*dm2e2s3_dx);
}
vector<double> mu_hc(ncomp, 0);
vector<double> mu_disp(ncomp, 0);
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {
mu_hc[i] += x[j]*dahc_dx[j];
mu_disp[i] += x[j]*dadisp_dx[j];
}
mu_hc[i] = ares_hc + Zhc + dahc_dx[i] - mu_hc[i];
mu_disp[i] = ares_disp + Zdisp + dadisp_dx[i] - mu_disp[i];
}
// Dipole term (Gross and Vrabec term) --------------------------------------
vector<double> mu_polar(ncomp, 0);
if (!cppargs.dipm.empty()) {
double A2 = 0.;
double A3 = 0.;
double dA2_det = 0.;
double dA3_det = 0.;
vector<double> dA2_dx(ncomp, 0);
vector<double> dA3_dx(ncomp, 0);
static double a0dip[5] = { 0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308 };
static double a1dip[5] = { 0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135 };
static double a2dip[5] = { -1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575 };
static double b0dip[5] = { 0.2187939, -1.1896431, 1.1626889, 0, 0 };
static double b1dip[5] = { -0.5873164, 1.2489132, -0.5085280, 0, 0 };
static double b2dip[5] = { 3.4869576, -14.915974, 15.372022, 0, 0 };
static double c0dip[5] = { -0.0646774, 0.1975882, -0.8087562, 0.6902849, 0 };
static double c1dip[5] = { -0.9520876, 2.9924258, -2.3802636, -0.2701261, 0 };
static double c2dip[5] = { -0.6260979, 1.2924686, 1.6542783, -3.4396744, 0 };
const static double conv = 7242.702976750923; // conversion factor, see the note below Table 2 in Gross and Vrabec 2006
vector<double> dipmSQ (ncomp, 0);
for (int i = 0; i < ncomp; i++) {
dipmSQ[i] = pow(cppargs.dipm[i], 2.)/(cppargs.m[i]*cppargs.e[i]*pow(cppargs.s[i],3.))*conv;
}
vector<double> adip (5, 0);
vector<double> bdip (5, 0);
vector<double> cdip (5, 0);
double J2, dJ2_det, J3, dJ3_det;
double m_ij;
double m_ijk;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {
m_ij = sqrt(cppargs.m[i]*cppargs.m[j]);
if (m_ij > 2) {
m_ij = 2;
}
J2 = 0.;
dJ2_det = 0.;
for (int l = 0; l < 5; l++) {
adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
J2 += (adip[l] + bdip[l]*e_ij[j*ncomp+j]/t)*pow(eta, l); // j*ncomp+j needs to be used for e_ij because it is formatted as a 1D vector
dJ2_det += (adip[l] + bdip[l]*e_ij[j*ncomp+j]/t)*l*pow(eta, l-1);
}
A2 += x[i]*x[j]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)/
pow(s_ij[i*ncomp+j],3)*cppargs.dip_num[i]*cppargs.dip_num[j]*dipmSQ[i]*dipmSQ[j]*J2;
dA2_det += x[i]*x[j]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*pow(s_ij[i*ncomp+i],3)*
pow(s_ij[j*ncomp+j],3)/pow(s_ij[i*ncomp+j],3)*cppargs.dip_num[i]*cppargs.dip_num[j]*dipmSQ[i]*dipmSQ[j]*dJ2_det;
if (i == j) {
dA2_dx[i] += e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)
/pow(s_ij[i*ncomp+j],3)*cppargs.dip_num[i]*cppargs.dip_num[j]*dipmSQ[i]*dipmSQ[j]*
(x[i]*x[j]*dJ2_det*PI/6.*den*cppargs.m[i]*pow(d[i],3) + 2*x[j]*J2);
}
else {
dA2_dx[i] += e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)
/pow(s_ij[i*ncomp+j],3)*cppargs.dip_num[i]*cppargs.dip_num[j]*dipmSQ[i]*dipmSQ[j]*
(x[i]*x[j]*dJ2_det*PI/6.*den*cppargs.m[i]*pow(d[i],3) + x[j]*J2);
}
for (int k = 0; k < ncomp; k++) {
m_ijk = pow((cppargs.m[i]*cppargs.m[j]*cppargs.m[k]),1/3.);
if (m_ijk > 2) {
m_ijk = 2;
}
J3 = 0.;
dJ3_det = 0.;
for (int l = 0; l < 5; l++) {
cdip[l] = c0dip[l] + (m_ijk-1)/m_ijk*c1dip[l] + (m_ijk-1)/m_ijk*(m_ijk-2)/m_ijk*c2dip[l];
J3 += cdip[l]*pow(eta, l);
dJ3_det += cdip[l]*l*pow(eta, (l-1));
}
A3 += x[i]*x[j]*x[k]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*e_ij[k*ncomp+k]/t*
pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)*pow(s_ij[k*ncomp+k],3)/s_ij[i*ncomp+j]/s_ij[i*ncomp+k]/
s_ij[j*ncomp+k]*cppargs.dip_num[i]*cppargs.dip_num[j]*cppargs.dip_num[k]*dipmSQ[i]*
dipmSQ[j]*dipmSQ[k]*J3;
dA3_det += x[i]*x[j]*x[k]*e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*e_ij[k*ncomp+k]/t*
pow(s_ij[i*ncomp+i],3)*pow(s_ij[j*ncomp+j],3)*pow(s_ij[k*ncomp+k],3)/s_ij[i*ncomp+j]/s_ij[i*ncomp+k]/
s_ij[j*ncomp+k]*cppargs.dip_num[i]*cppargs.dip_num[j]*cppargs.dip_num[k]*dipmSQ[i]*
dipmSQ[j]*dipmSQ[k]*dJ3_det;
if ((i == j) && (i == k)) {
dA3_dx[i] += e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*e_ij[k*ncomp+k]/t*pow(s_ij[i*ncomp+i],3)
*pow(s_ij[j*ncomp+j],3)*pow(s_ij[k*ncomp+k],3)/s_ij[i*ncomp+j]/s_ij[i*ncomp+k]/s_ij[j*ncomp+k]
*cppargs.dip_num[i]*cppargs.dip_num[j]*cppargs.dip_num[k]*dipmSQ[i]*dipmSQ[j]
*dipmSQ[k]*(x[i]*x[j]*x[k]*dJ3_det*PI/6.*den*cppargs.m[i]*pow(d[i],3)
+ 3*x[j]*x[k]*J3);
}
else if ((i == j) || (i == k)) {
dA3_dx[i] += e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*e_ij[k*ncomp+k]/t*pow(s_ij[i*ncomp+i],3)
*pow(s_ij[j*ncomp+j],3)*pow(s_ij[k*ncomp+k],3)/s_ij[i*ncomp+j]/s_ij[i*ncomp+k]/s_ij[j*ncomp+k]
*cppargs.dip_num[i]*cppargs.dip_num[j]*cppargs.dip_num[k]*dipmSQ[i]*dipmSQ[j]
*dipmSQ[k]*(x[i]*x[j]*x[k]*dJ3_det*PI/6.*den*cppargs.m[i]*pow(d[i],3)
+ 2*x[j]*x[k]*J3);
}
else {
dA3_dx[i] += e_ij[i*ncomp+i]/t*e_ij[j*ncomp+j]/t*e_ij[k*ncomp+k]/t*pow(s_ij[i*ncomp+i],3)
*pow(s_ij[j*ncomp+j],3)*pow(s_ij[k*ncomp+k],3)/s_ij[i*ncomp+j]/s_ij[i*ncomp+k]/s_ij[j*ncomp+k]
*cppargs.dip_num[i]*cppargs.dip_num[j]*cppargs.dip_num[k]*dipmSQ[i]*dipmSQ[j]
*dipmSQ[k]*(x[i]*x[j]*x[k]*dJ3_det*PI/6.*den*cppargs.m[i]*pow(d[i],3)
+ x[j]*x[k]*J3);
}
}
}
}
A2 = -PI*den*A2;
A3 = -4/3.*PI*PI*den*den*A3;
dA2_det = -PI*den*dA2_det;
dA3_det = -4/3.*PI*PI*den*den*dA3_det;
for (int i = 0; i < ncomp; i++) {
dA2_dx[i] = -PI*den*dA2_dx[i];
dA3_dx[i] = -4/3.*PI*PI*den*den*dA3_dx[i];
}
vector<double> dapolar_dx(ncomp);
for (int i = 0; i < ncomp; i++) {
dapolar_dx[i] = (dA2_dx[i]*(1-A3/A2) + (dA3_dx[i]*A2 - A3*dA2_dx[i])/A2)/pow(1-A3/A2,2);
}
if (A2 != 0) { // when the mole fraction of the polar compounds is 0 then A2 = 0 and division by 0 occurs
double ares_polar = A2/(1-A3/A2);
double Zpolar = eta*((dA2_det*(1-A3/A2)+(dA3_det*A2-A3*dA2_det)/A2)/(1-A3/A2)/(1-A3/A2));
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {
mu_polar[i] += x[j]*dapolar_dx[j];
}
mu_polar[i] = ares_polar + Zpolar + dapolar_dx[i] - mu_polar[i];
}
}
}
// Association term -------------------------------------------------------
vector<double> mu_assoc(ncomp, 0);
if (!cppargs.e_assoc.empty()) {
int num_sites = 0;
vector<int> iA; //indices of associating compounds
for(std::vector<int>::iterator it = cppargs.assoc_num.begin(); it != cppargs.assoc_num.end(); ++it) {
num_sites += *it;
for (int i = 0; i < *it; i++) {
iA.push_back(it - cppargs.assoc_num.begin());
}
}
vector<double> x_assoc(num_sites); // mole fractions of only the associating compounds
for (int i = 0; i < num_sites; i++) {
x_assoc[i] = x[iA[i]];
}
vector<double> XA (num_sites, 0);
vector<double> delta_ij(num_sites * num_sites, 0);
int idxa = 0;
int idxi = 0; // index for the ii-th compound
int idxj = 0; // index for the jj-th compound
for (int i = 0; i < num_sites; i++) {
idxi = iA[i]*ncomp+iA[i];
for (int j = 0; j < num_sites; j++) {
idxj = iA[j]*ncomp+iA[j];
if (cppargs.assoc_matrix[idxa] != 0) {
double eABij = (cppargs.e_assoc[iA[i]]+cppargs.e_assoc[iA[j]])/2.;
double volABij = _HUGE;
if (cppargs.k_hb.empty()) {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
}
else {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3)*(1-cppargs.k_hb[iA[i]*ncomp+iA[j]]);
}
delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/t)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
}
idxa += 1;
}
XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
if (!std::isfinite(XA[i])) {
XA[i] = 0.02;
}
}
vector<double> ddelta_dx(num_sites * num_sites * ncomp, 0);
int idx_ddelta = 0;
for (int k = 0; k < ncomp; k++) {
int idxi = 0; // index for the ii-th compound
int idxj = 0; // index for the jj-th compound
idxa = 0;
for (int i = 0; i < num_sites; i++) {
idxi = iA[i]*ncomp+iA[i];
for (int j = 0; j < num_sites; j++) {
idxj = iA[j]*ncomp+iA[j];
if (cppargs.assoc_matrix[idxa] != 0) {
double eABij = (cppargs.e_assoc[iA[i]]+cppargs.e_assoc[iA[j]])/2.;
double volABij = _HUGE;
if (cppargs.k_hb.empty()) {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
}
else {
volABij = sqrt(cppargs.vol_a[iA[i]]*cppargs.vol_a[iA[j]])*pow(sqrt(s_ij[idxi]*
s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3)*(1-cppargs.k_hb[iA[i]*ncomp+iA[j]]);
}
double dghsd_dx = PI/6.*cppargs.m[k]*(pow(d[k], 3)/(1-zeta[3])/(1-zeta[3]) + 3*d[iA[i]]*d[iA[j]]/
(d[iA[i]]+d[iA[j]])*(d[k]*d[k]/(1-zeta[3])/(1-zeta[3])+2*pow(d[k], 3)*
zeta[2]/pow(1-zeta[3], 3)) + 2*pow((d[iA[i]]*d[iA[j]]/(d[iA[i]]+d[iA[j]])), 2)*
(2*d[k]*d[k]*zeta[2]/pow(1-zeta[3], 3)+3*(pow(d[k], 3)*zeta[2]*zeta[2]
/pow(1-zeta[3], 4))));
ddelta_dx[idx_ddelta] = dghsd_dx*(exp(eABij/t)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
}
idx_ddelta += 1;
idxa += 1;
}
}
}
int ctr = 0;
double dif = 1000.;
vector<double> XA_old = XA;
while ((ctr < 100) && (dif > 1e-15)) {
ctr += 1;
XA = XA_find(XA_old, delta_ij, den, x_assoc);
dif = 0.;
for (int i = 0; i < num_sites; i++) {
dif += std::abs(XA[i] - XA_old[i]);
}
for (int i = 0; i < num_sites; i++) {
XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
}
}
vector<double> dXA_dx(num_sites*ncomp, 0);
dXA_dx = dXAdx_find(cppargs.assoc_num, delta_ij, den, XA, ddelta_dx, x_assoc);
int ij = 0;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < num_sites; j++) {
mu_assoc[i] += x[iA[j]]*den*dXA_dx[ij]*(1/XA[j]-0.5);
ij += 1;
}
}
for (int i = 0; i < num_sites; i++) {
mu_assoc[iA[i]] += log(XA[i]) - 0.5*XA[i] + 0.5;
}
}
// Ion term ---------------------------------------------------------------
vector<double> mu_ion(ncomp, 0);
if (!cppargs.z.empty()) {
vector<double> q(cppargs.z.begin(), cppargs.z.end());
for (int i = 0; i < ncomp; i++) {
q[i] = q[i]*E_CHRG;
}
summ = 0.;
for (int i = 0; i < ncomp; i++) {
summ += cppargs.z[i]*cppargs.z[i]*x[i];
}
double kappa = sqrt(den*E_CHRG*E_CHRG/kb/t/(cppargs.dielc*perm_vac)*summ); // the inverse Debye screening length. Equation 4 in Held et al. 2008.
if (kappa != 0) {
vector<double> chi(ncomp);
vector<double> sigma_k(ncomp);
double summ1 = 0.;
double summ2 = 0.;
for (int i = 0; i < ncomp; i++) {
chi[i] = 3/pow(kappa*cppargs.s[i], 3)*(1.5 + log(1+kappa*cppargs.s[i]) - 2*(1+kappa*cppargs.s[i]) +
0.5*pow(1+kappa*cppargs.s[i], 2));
sigma_k[i] = -2*chi[i]+3/(1+kappa*cppargs.s[i]);
summ1 += q[i]*q[i]*x[i]*sigma_k[i];
summ2 += x[i]*q[i]*q[i];
}
for (int i = 0; i < ncomp; i++) {
mu_ion[i] = -q[i]*q[i]*kappa/24./PI/kb/t/(cppargs.dielc*perm_vac)*
(2*chi[i] + summ1/summ2);
}
}
}
double Z = pcsaft_Z_cpp(t, rho, x, cppargs);
vector<double> mu(ncomp, 0);
vector<double> fugcoef(ncomp, 0);
for (int i = 0; i < ncomp; i++) {
mu[i] = mu_hc[i] + mu_disp[i] + mu_polar[i] + mu_assoc[i] + mu_ion[i];
fugcoef[i] = exp(mu[i] - log(Z)); // the fugacity coefficients
}
return fugcoef;
}
double pcsaft_p_cpp(double t, double rho, vector<double> x, add_args &cppargs) {
/**
Calculate pressure
*/
double den = rho*N_AV/1.0e30;
double Z = pcsaft_Z_cpp(t, rho, x, cppargs);
double P = Z*kb*t*den*1.0e30; // Pa
return P;
}
double pcsaft_ares_cpp(double t, double rho, vector<double> x, add_args &cppargs) {
/**
Calculate the residual Helmholtz energy
*/
int ncomp = x.size(); // number of components
vector<double> d (ncomp);
for (int i = 0; i < ncomp; i++) {
d[i] = cppargs.s[i]*(1-0.12*exp(-3*cppargs.e[i]/t));
}
if (!cppargs.z.empty()) {
for (int i = 0; i < ncomp; i++) {
if (cppargs.z[i] != 0) {
d[i] = cppargs.s[i]*(1-0.12); // for ions the diameter is assumed to be temperature independent (see Held et al. 2014)
}
}
}
double den = rho*N_AV/1.0e30;
vector<double> zeta (4, 0);
double summ;
for (int i = 0; i < 4; i++) {
summ = 0;
for (int j = 0; j < ncomp; j++) {
summ += x[j]*cppargs.m[j]*pow(d[j], i);
}
zeta[i] = PI/6*den*summ;
}
double eta = zeta[3];
double m_avg = 0;
for (int i = 0; i < ncomp; i++) {
m_avg += x[i]*cppargs.m[i];
}
vector<double> ghs (ncomp*ncomp, 0);
vector<double> e_ij (ncomp*ncomp, 0);
vector<double> s_ij (ncomp*ncomp, 0);
double m2es3 = 0.;
double m2e2s3 = 0.;
int idx = -1;
for (int i = 0; i < ncomp; i++) {
for (int j = 0; j < ncomp; j++) {