58 N = component_names.size();
65 for (
unsigned int i = 0; i <
N; ++i){
92 std::string kij_string;
93 std::string kijT_string;
99 for (
unsigned int i = 0; i <
N; ++i) {
100 for (
unsigned int j = 0; j <
N; ++j) {
104 k_ij[i *
N + j] = atof(kij_string.c_str());
105 k_ijT[i *
N + j] = atof(kijT_string.c_str());
111 if (generate_SatL_and_SatV) {
112 bool SatLSatV =
false;
133 for (
unsigned int i = 0; i <
N; ++i){
158 std::string kij_string;
159 std::string kijT_string;
165 for (
unsigned int i = 0; i <
N; ++i) {
166 for (
unsigned int j = 0; j <
N; ++j) {
170 k_ij[i *
N + j] = atof(kij_string.c_str());
171 k_ijT[i *
N + j] = atof(kijT_string.c_str());
177 if (generate_SatL_and_SatV) {
178 bool SatLSatV =
false;
202 if (mass_fractions.size() !=
N) {
203 throw ValueError(
format(
"size of mass fraction vector [%d] does not equal that of component vector [%d]", mass_fractions.size(),
N));
205 std::vector<CoolPropDbl> moles;
208 for (
unsigned int i = 0; i <
components.size(); ++i) {
209 tmp = mass_fractions[i] /
components[i].molar_mass();
210 moles.push_back(tmp);
214 for (std::vector<CoolPropDbl>::iterator it = moles.begin(); it != moles.end(); ++it) {
248 vector<double> d(ncomp);
249 for (
int i = 0; i < ncomp; i++) {
253 for (
int i = 0; i < ncomp; i++) {
262 vector<double> zeta(4, 0);
264 for (
int i = 0; i < 4; i++) {
266 for (
int j = 0; j < ncomp; j++) {
269 zeta[i] = PI / 6 * den * summ;
272 double eta = zeta[3];
274 for (
int i = 0; i < ncomp; i++) {
278 vector<double> ghs(ncomp * ncomp, 0);
279 vector<double> e_ij(ncomp * ncomp, 0);
280 vector<double> s_ij(ncomp * ncomp, 0);
284 for (
int i = 0; i < ncomp; i++) {
285 for (
int j = 0; j < ncomp; j++) {
308 ghs[idx] = 1 / (1 - zeta[3]) + (d[i] * d[j] / (d[i] + d[j])) * 3 * zeta[2] / (1 - zeta[3]) / (1 - zeta[3])
309 + pow(d[i] * d[j] / (d[i] + d[j]), 2) * 2 * zeta[2] * zeta[2] / pow(1 - zeta[3], 3);
313 double ares_hs = 1 / zeta[0]
314 * (3 * zeta[1] * zeta[2] / (1 - zeta[3]) + pow(zeta[2], 3.) / (zeta[3] * pow(1 - zeta[3], 2))
315 + (pow(zeta[2], 3.) / pow(zeta[3], 2.) - zeta[0]) * log(1 - zeta[3]));
317 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
318 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
319 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
320 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
321 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
322 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
324 vector<double> a(7, 0);
325 vector<double> b(7, 0);
326 for (
int i = 0; i < 7; i++) {
327 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
328 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
333 for (
int i = 0; i < 7; i++) {
334 I1 += a[i] * pow(eta, i);
335 I2 += b[i] * pow(eta, i);
338 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
339 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
342 for (
int i = 0; i < ncomp; i++) {
346 double ares_hc = m_avg * ares_hs - summ;
347 double ares_disp = -2 * PI * den * I1 * m2es3 - PI * den * m_avg * C1 * I2 * m2e2s3;
350 double ares_polar = 0.;
354 vector<double> dipmSQ(ncomp, 0);
356 static double a0dip[5] = {0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308};
357 static double a1dip[5] = {0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135};
358 static double a2dip[5] = {-1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575};
359 static double b0dip[5] = {0.2187939, -1.1896431, 1.1626889, 0, 0};
360 static double b1dip[5] = {-0.5873164, 1.2489132, -0.5085280, 0, 0};
361 static double b2dip[5] = {3.4869576, -14.915974, 15.372022, 0, 0};
362 static double c0dip[5] = {-0.0646774, 0.1975882, -0.8087562, 0.6902849, 0};
363 static double c1dip[5] = {-0.9520876, 2.9924258, -2.3802636, -0.2701261, 0};
364 static double c2dip[5] = {-0.6260979, 1.2924686, 1.6542783, -3.4396744, 0};
366 const static double conv = 7242.702976750923;
368 for (
int i = 0; i < ncomp; i++) {
372 vector<double> adip(5, 0);
373 vector<double> bdip(5, 0);
374 vector<double> cdip(5, 0);
378 for (
int i = 0; i < ncomp; i++) {
379 for (
int j = 0; j < ncomp; j++) {
385 for (
int l = 0; l < 5; l++) {
386 adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
387 bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
388 J2 += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*pow(eta, l);
391 * pow(s_ij[j * ncomp + j], 3) / pow(s_ij[i * ncomp + j], 3) *
components[i].getDipnum() *
components[j].getDipnum() * dipmSQ[i]
394 for (
int k = 0; k < ncomp; k++) {
400 for (
int l = 0; l < 5; l++) {
401 cdip[l] = c0dip[l] + (m_ijk - 1) / m_ijk * c1dip[l] + (m_ijk - 1) / m_ijk * (m_ijk - 2) / m_ijk * c2dip[l];
402 J3 += cdip[l] * pow(eta, l);
405 * 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)
406 / s_ij[i * ncomp + j] / s_ij[i * ncomp + k] / s_ij[j * ncomp + k] *
components[i].getDipnum() *
components[j].getDipnum()
407 *
components[k].getDipnum() * dipmSQ[i] * dipmSQ[j] * dipmSQ[k] * J3;
413 A3 = -4 / 3. * PI * PI * den * den * A3;
416 ares_polar = A2/(1-A3/A2);
421 double ares_assoc = 0.;
427 for (
int i = 0; i < *it; i++) {
432 vector<double> x_assoc(num_sites);
433 for (
int i = 0; i < num_sites; i++) {
438 vector<double> XA (num_sites, 0);
439 vector<double> delta_ij(num_sites * num_sites, 0);
443 for (
int i = 0; i < num_sites; i++) {
444 idxi = iA[i]*ncomp+iA[i];
445 for (
int j = 0; j < num_sites; j++) {
446 idxj = iA[j]*ncomp+iA[j];
449 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
450 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
451 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
455 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
456 if (!std::isfinite(XA[i])) {
463 vector<double> XA_old = XA;
464 while ((ctr < 100) && (dif > 1e-15)) {
466 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
468 for (
int i = 0; i < num_sites; i++) {
469 dif += abs(XA[i] - XA_old[i]);
471 for (
int i = 0; i < num_sites; i++) {
472 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
477 for (
int i = 0; i < num_sites; i++) {
483 double ares_ion = 0.;
485 vector<double> q(ncomp);
486 for (
int i = 0; i < ncomp; i++) {
491 for (
int i = 0; i < ncomp; i++) {
495 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
498 vector<double> chi(ncomp);
499 vector<double> sigma_k(ncomp);
501 for (
int i = 0; i < ncomp; i++) {
502 chi[i] = 3 / pow(kappa * d[i], 3)
503 * (1.5 + log(1 + kappa * d[i]) - 2 * (1 + kappa * d[i])
504 + 0.5 * pow(1 + kappa * d[i], 2));
508 ares_ion = -1 / 12. / PI / kb /
_T / (
dielc * perm_vac) * summ;
512 CoolPropDbl ares = ares_hc + ares_disp + ares_polar + ares_assoc + ares_ion;
518 vector<double> d(ncomp), dd_dt(ncomp);
519 for (
int i = 0; i < ncomp; i++) {
524 for (
int i = 0; i < ncomp; i++) {
534 vector<double> zeta(4, 0);
536 for (
int i = 0; i < 4; i++) {
538 for (
int j = 0; j < ncomp; j++) {
541 zeta[i] = PI / 6 * den * summ;
544 vector<double> dzeta_dt(4, 0);
545 for (
int i = 1; i < 4; i++) {
547 for (
int j = 0; j < ncomp; j++) {
550 dzeta_dt[i] = PI / 6 * den * summ;
553 double eta = zeta[3];
555 for (
int i = 0; i < ncomp; i++) {
559 vector<double> ghs(ncomp * ncomp, 0);
560 vector<double> dghs_dt(ncomp * ncomp, 0);
561 vector<double> e_ij(ncomp * ncomp, 0);
562 vector<double> s_ij(ncomp * ncomp, 0);
567 for (
int i = 0; i < ncomp; i++) {
568 for (
int j = 0; j < ncomp; j++) {
589 ghs[idx] = 1 / (1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
590 pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
591 ddij_dt = (d[i]*d[j]/(d[i]+d[j]))*(dd_dt[i]/d[i]+dd_dt[j]/d[j]-(dd_dt[i]+dd_dt[j])/(d[i]+d[j]));
592 dghs_dt[idx] = dzeta_dt[3]/pow(1-zeta[3], 2.)
593 + 3*(ddij_dt*zeta[2]+(d[i]*d[j]/(d[i]+d[j]))*dzeta_dt[2])/pow(1-zeta[3], 2.)
594 + 4*(d[i]*d[j]/(d[i]+d[j]))*zeta[2]*(1.5*dzeta_dt[3]+ddij_dt*zeta[2]
595 + (d[i]*d[j]/(d[i]+d[j]))*dzeta_dt[2])/pow(1-zeta[3], 3.)
596 + 6*pow((d[i]*d[j]/(d[i]+d[j]))*zeta[2], 2.)*dzeta_dt[3]/pow(1-zeta[3], 4.);
600 double dadt_hs = 1/zeta[0]*(3*(dzeta_dt[1]*zeta[2] + zeta[1]*dzeta_dt[2])/(1-zeta[3])
601 + 3*zeta[1]*zeta[2]*dzeta_dt[3]/pow(1-zeta[3], 2.)
602 + 3*pow(zeta[2], 2.)*dzeta_dt[2]/zeta[3]/pow(1-zeta[3], 2.)
603 + pow(zeta[2],3.)*dzeta_dt[3]*(3*zeta[3]-1)/pow(zeta[3], 2.)/pow(1-zeta[3], 3.)
604 + (3*pow(zeta[2], 2.)*dzeta_dt[2]*zeta[3] - 2*pow(zeta[2], 3.)*dzeta_dt[3])/pow(zeta[3], 3.)
606 + (zeta[0]-pow(zeta[2],3)/pow(zeta[3],2.))*dzeta_dt[3]/(1-zeta[3]));
608 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
609 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
610 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
611 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
612 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
613 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
615 vector<double> a(7, 0);
616 vector<double> b(7, 0);
617 for (
int i = 0; i < 7; i++) {
618 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
619 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
624 double dI1_dt = 0.0, dI2_dt = 0.;
625 for (
int i = 0; i < 7; i++) {
626 I1 += a[i] * pow(eta, i);
627 I2 += b[i] * pow(eta, i);
628 dI1_dt += a[i] * dzeta_dt[3] * i * pow(eta, i - 1);
629 dI2_dt += b[i] * dzeta_dt[3] * i * pow(eta, i - 1);
632 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
633 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
634 double C2 = -1 * C1 * C1
635 * (m_avg * (-4 * eta * eta + 20 * eta + 8) / pow(1 - eta, 5.)
636 + (1 - m_avg) * (2 * pow(eta, 3) + 12 * eta * eta - 48 * eta + 40) / pow((1 - eta) * (2 - eta), 3));
637 double dC1_dt = C2 * dzeta_dt[3];
640 for (
int i = 0; i < ncomp; i++) {
644 double dadt_hc = m_avg * dadt_hs - summ;
645 double dadt_disp = -2 * PI * den * (dI1_dt - I1 /
_T) * m2es3 - PI * den * m_avg * (dC1_dt * I2 + C1 * dI2_dt - 2 * C1 * I2 /
_T) * m2e2s3;
648 double dadt_polar = 0.;
654 vector<double> dipmSQ(ncomp, 0);
656 static double a0dip[5] = {0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308};
657 static double a1dip[5] = {0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135};
658 static double a2dip[5] = {-1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575};
659 static double b0dip[5] = {0.2187939, -1.1896431, 1.1626889, 0, 0};
660 static double b1dip[5] = {-0.5873164, 1.2489132, -0.5085280, 0, 0};
661 static double b2dip[5] = {3.4869576, -14.915974, 15.372022, 0, 0};
662 static double c0dip[5] = {-0.0646774, 0.1975882, -0.8087562, 0.6902849, 0};
663 static double c1dip[5] = {-0.9520876, 2.9924258, -2.3802636, -0.2701261, 0};
664 static double c2dip[5] = {-0.6260979, 1.2924686, 1.6542783, -3.4396744, 0};
666 const static double conv = 7242.702976750923;
668 for (
int i = 0; i < ncomp; i++) {
672 vector<double> adip(5, 0);
673 vector<double> bdip(5, 0);
674 vector<double> cdip(5, 0);
675 double J2, J3, dJ2_dt, dJ3_dt;
678 for (
int i = 0; i < ncomp; i++) {
679 for (
int j = 0; j < ncomp; j++) {
686 for (
int l = 0; l < 5; l++) {
687 adip[l] = a0dip[l] + (m_ij - 1) / m_ij * a1dip[l] + (m_ij - 1) / m_ij * (m_ij - 2) / m_ij * a2dip[l];
688 bdip[l] = b0dip[l] + (m_ij - 1) / m_ij * b1dip[l] + (m_ij - 1) / m_ij * (m_ij - 2) / m_ij * b2dip[l];
689 J2 += (adip[l] + bdip[l] * e_ij[i * ncomp + j] /
_T) * pow(eta, l);
690 dJ2_dt += adip[l] * l * pow(eta, l - 1) * dzeta_dt[3]
691 + bdip[l] * e_ij[j * ncomp + j] * (1 /
_T * l * pow(eta, l - 1) * dzeta_dt[3]
692 - 1 / pow(
_T, 2.) * pow(eta, l));
695 * pow(s_ij[j * ncomp + j], 3) / pow(s_ij[i * ncomp + j], 3) *
components[i].getDipnum() *
components[j].getDipnum() * dipmSQ[i]
698 * pow(s_ij[j * ncomp + j], 3) / pow(s_ij[i * ncomp + j], 3) *
components[i].getDipnum() *
components[j].getDipnum()
699 * dipmSQ[i] * dipmSQ[j] * (dJ2_dt / pow(
_T, 2) - 2 * J2 / pow(
_T, 3));
701 for (
int k = 0; k < ncomp; k++) {
708 for (
int l = 0; l < 5; l++) {
709 cdip[l] = c0dip[l] + (m_ijk - 1) / m_ijk * c1dip[l] + (m_ijk - 1) / m_ijk * (m_ijk - 2) / m_ijk * c2dip[l];
710 J3 += cdip[l] * pow(eta, l);
711 dJ3_dt += cdip[l] * l * pow(eta, l - 1) * dzeta_dt[3];
714 * 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)
715 / s_ij[i * ncomp + j] / s_ij[i * ncomp + k] / s_ij[j * ncomp + k] *
components[i].getDipnum() *
components[j].getDipnum()
716 *
components[k].getDipnum() * dipmSQ[i] * dipmSQ[j] * dipmSQ[k] * J3;
718 * e_ij[k * ncomp + k] * pow(s_ij[i * ncomp + i], 3) * pow(s_ij[j * ncomp + j], 3) * pow(s_ij[k * ncomp + k], 3)
719 / s_ij[i * ncomp + j] / s_ij[i * ncomp + k] / s_ij[j * ncomp + k] *
components[i].getDipnum()
721 * (-3 * J3 / pow(
_T, 4) + dJ3_dt / pow(
_T, 3));
727 A3 = -4 / 3. * PI * PI * den * den * A3;
728 dA2_dt = -PI * den * dA2_dt;
729 dA3_dt = -4 / 3. * PI * PI * den * den * dA3_dt;
732 dadt_polar = (dA2_dt - 2 * A3 / A2 * dA2_dt + dA3_dt) / pow(1 - A3 / A2, 2.);
737 double dadt_assoc = 0.;
743 for (
int i = 0; i < *it; i++) {
748 vector<double> x_assoc(num_sites);
749 for (
int i = 0; i < num_sites; i++) {
754 vector<double> XA (num_sites, 0);
755 vector<double> delta_ij(num_sites * num_sites, 0);
756 vector<double> ddelta_dt(num_sites * num_sites, 0);
760 for (
int i = 0; i < num_sites; i++) {
761 idxi = iA[i]*ncomp+iA[i];
762 for (
int j = 0; j < num_sites; j++) {
763 idxj = iA[j]*ncomp+iA[j];
766 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
767 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
768 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
769 ddelta_dt[idxa] = pow(s_ij[idxj],3)*volABij*(-eABij/pow(
_T,2)
770 *exp(eABij/
_T)*ghs[iA[i]*ncomp+iA[j]] + dghs_dt[iA[i]*ncomp+iA[j]]
775 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
776 if (!std::isfinite(XA[i])) {
783 vector<double> XA_old = XA;
784 while ((ctr < 100) && (dif > 1e-15)) {
786 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
788 for (
int i = 0; i < num_sites; i++) {
789 dif += abs(XA[i] - XA_old[i]);
791 for (
int i = 0; i < num_sites; i++) {
792 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
796 vector<double> dXA_dt(num_sites, 0);
797 dXA_dt =
dXAdt_find(delta_ij, den, XA, ddelta_dt, x_assoc);
799 for (
int i = 0; i < num_sites; i++) {
805 double dadt_ion = 0.;
807 vector<double> q(ncomp);
808 for (
int i = 0; i < ncomp; i++) {
813 for (
int i = 0; i < ncomp; i++) {
817 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
821 vector<double> chi(ncomp);
822 vector<double> dchikap_dk(ncomp);
824 for (
int i = 0; i < ncomp; i++) {
825 chi[i] = 3 / pow(kappa * d[i], 3)
826 * (1.5 + log(1 + kappa * d[i]) - 2 * (1 + kappa * d[i])
827 + 0.5 * pow(1 + kappa * d[i], 2));
828 dchikap_dk[i] = -2 * chi[i] + 3 / (1 + kappa * d[i]);
831 dkappa_dt = -0.5 * den * E_CHRG * E_CHRG / kb /
_T /
_T / (
dielc * perm_vac) * summ / kappa;
834 for (
int i = 0; i < ncomp; i++) {
835 summ +=
mole_fractions[i] * q[i] * q[i] * (dchikap_dk[i] * dkappa_dt /
_T - kappa * chi[i] /
_T /
_T);
837 dadt_ion = -1 / 12. / PI / kb / (
dielc * perm_vac) * summ;
841 double dadt = dadt_hc + dadt_disp + dadt_assoc + dadt_polar + dadt_ion;
863 vector<double> d(ncomp);
864 for (
int i = 0; i < ncomp; i++) {
868 for (
int i = 0; i < ncomp; i++) {
877 vector<double> zeta(4, 0);
879 for (
int i = 0; i < 4; i++) {
881 for (
int j = 0; j < ncomp; j++) {
884 zeta[i] = PI / 6 * den * summ;
887 double eta = zeta[3];
889 for (
int i = 0; i < ncomp; i++) {
893 vector<double> ghs(ncomp * ncomp, 0);
894 vector<double> denghs(ncomp * ncomp, 0);
895 vector<double> e_ij(ncomp * ncomp, 0);
896 vector<double> s_ij(ncomp * ncomp, 0);
900 for (
int i = 0; i < ncomp; i++) {
901 for (
int j = 0; j < ncomp; j++) {
923 ghs[idx] = 1/(1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
924 pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
925 denghs[idx] = zeta[3]/(1-zeta[3])/(1-zeta[3]) +
926 (d[i]*d[j]/(d[i]+d[j]))*(3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
927 6*zeta[2]*zeta[3]/pow(1-zeta[3], 3)) +
928 pow(d[i]*d[j]/(d[i]+d[j]), 2)*(4*zeta[2]*zeta[2]/pow(1-zeta[3], 3) +
929 6*zeta[2]*zeta[2]*zeta[3]/pow(1-zeta[3], 4));
933 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))
934 + (pow(zeta[2], 3.)/pow(zeta[3], 2.) - zeta[0])*log(1-zeta[3]));
935 double Zhs = zeta[3]/(1-zeta[3]) + 3.*zeta[1]*zeta[2]/zeta[0]/(1.-zeta[3])/(1.-zeta[3]) +
936 (3.*pow(zeta[2], 3.) - zeta[3]*pow(zeta[2], 3.))/zeta[0]/pow(1.-zeta[3], 3.);
938 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
939 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
940 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
941 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
942 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
943 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
945 vector<double> a(7, 0);
946 vector<double> b(7, 0);
947 for (
int i = 0; i < 7; i++) {
948 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
949 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
952 double detI1_det = 0.0;
953 double detI2_det = 0.0;
956 for (
int i = 0; i < 7; i++) {
957 detI1_det += a[i] * (i + 1) * pow(eta, i);
958 detI2_det += b[i] * (i + 1) * pow(eta, i);
959 I2 += b[i] * pow(eta, i);
960 I1 += a[i] * pow(eta, i);
963 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
964 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
965 double C2 = -1. * C1 * C1
966 * (m_avg * (-4 * eta * eta + 20 * eta + 8) / pow(1 - eta, 5)
967 + (1 - m_avg) * (2 * pow(eta, 3) + 12 * eta * eta - 48 * eta + 40) / pow((1 - eta) * (2 - eta), 3.0));
970 for (
int i = 0; i < ncomp; i++) {
974 double ares_hc = m_avg * ares_hs - summ;
975 double ares_disp = -2 * PI * den * I1 * m2es3 - PI * den * m_avg * C1 * I2 * m2e2s3;
978 for (
int i = 0; i < ncomp; i++) {
982 double Zhc = m_avg * Zhs - summ;
983 double Zdisp = -2 * PI * den * detI1_det * m2es3 - PI * den * m_avg * (C1 * detI2_det + C2 * eta * I2) * m2e2s3;
985 vector<double> dghsii_dx(ncomp * ncomp, 0);
986 vector<double> dahs_dx(ncomp, 0);
987 vector<double> dzeta_dx(4, 0);
989 for (
int i = 0; i < ncomp; i++) {
990 for (
int l = 0; l < 4; l++) {
991 dzeta_dx[l] = PI / 6. * den *
components[i].getM() * pow(d[i], l);
993 for (
int j = 0; j < ncomp; j++) {
996 dzeta_dx[3] / (1 - zeta[3]) / (1 - zeta[3])
997 + (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))
998 + pow(d[j] * d[j] / (d[j] + d[j]), 2)
999 * (4 * zeta[2] * dzeta_dx[2] / pow(1 - zeta[3], 3) + 6 * zeta[2] * zeta[2] * dzeta_dx[3] / pow(1 - zeta[3], 4));
1002 -dzeta_dx[0] / zeta[0] * ares_hs
1004 * (3 * (dzeta_dx[1] * zeta[2] + zeta[1] * dzeta_dx[2]) / (1 - zeta[3])
1005 + 3 * zeta[1] * zeta[2] * dzeta_dx[3] / (1 - zeta[3]) / (1 - zeta[3])
1006 + 3 * zeta[2] * zeta[2] * dzeta_dx[2] / zeta[3] / (1 - zeta[3]) / (1 - zeta[3])
1007 + pow(zeta[2], 3) * dzeta_dx[3] * (3 * zeta[3] - 1) / zeta[3] / zeta[3] / pow(1 - zeta[3], 3)
1009 * ((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])
1010 + (zeta[0] - pow(zeta[2], 3) / zeta[3] / zeta[3]) * dzeta_dx[3] / (1 - zeta[3]));
1013 vector<double> dadisp_dx(ncomp, 0);
1014 vector<double> dahc_dx(ncomp, 0);
1015 double dzeta3_dx, daa_dx, db_dx, dI1_dx, dI2_dx, dm2es3_dx, dm2e2s3_dx, dC1_dx;
1016 for (
int i = 0; i < ncomp; i++) {
1017 dzeta3_dx = PI / 6. * den *
components[i].getM() * pow(d[i], 3);
1022 for (
int l = 0; l < 7; l++) {
1023 daa_dx =
components[i].getM() / m_avg / m_avg * a1[l] +
components[i].getM() / m_avg / m_avg * (3 - 4 / m_avg) * a2[l];
1024 db_dx =
components[i].getM() / m_avg / m_avg * b1[l] +
components[i].getM() / m_avg / m_avg * (3 - 4 / m_avg) * b2[l];
1025 dI1_dx += a[l] * l * dzeta3_dx * pow(eta, l - 1) + daa_dx * pow(eta, l);
1026 dI2_dx += b[l] * l * dzeta3_dx * pow(eta, l - 1) + db_dx * pow(eta, l);
1028 for (
int j = 0; j < ncomp; j++) {
1033 dm2es3_dx = dm2es3_dx * 2 *
components[i].getM();
1034 dm2e2s3_dx = dm2e2s3_dx * 2 *
components[i].getM();
1035 dahc_dx[i] =
components[i].getM() * ares_hs + m_avg * dahs_dx[i] - dahc_dx[i] - (
components[i].getM() - 1) * log(ghs[i * ncomp + i]);
1036 dC1_dx = C2 * dzeta3_dx
1038 * (
components[i].getM() * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
1039 -
components[i].getM() * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2));
1042 -2 * PI * den * (dI1_dx * m2es3 + I1 * dm2es3_dx)
1043 - PI * den * ((
components[i].getM() * C1 * I2 + m_avg * dC1_dx * I2 + m_avg * C1 * dI2_dx) * m2e2s3 + m_avg * C1 * I2 * dm2e2s3_dx);
1046 vector<double> mu_hc(ncomp, 0);
1047 vector<double> mu_disp(ncomp, 0);
1048 for (
int i = 0; i < ncomp; i++) {
1049 for (
int j = 0; j < ncomp; j++) {
1053 mu_hc[i] = ares_hc + Zhc + dahc_dx[i] - mu_hc[i];
1054 mu_disp[i] = ares_disp + Zdisp + dadisp_dx[i] - mu_disp[i];
1058 vector<double> mu_polar(ncomp, 0);
1062 double dA2_det = 0.;
1063 double dA3_det = 0.;
1064 vector<double> dA2_dx(ncomp, 0);
1065 vector<double> dA3_dx(ncomp, 0);
1067 static double a0dip[5] = { 0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308 };
1068 static double a1dip[5] = { 0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135 };
1069 static double a2dip[5] = { -1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575 };
1070 static double b0dip[5] = { 0.2187939, -1.1896431, 1.1626889, 0, 0 };
1071 static double b1dip[5] = { -0.5873164, 1.2489132, -0.5085280, 0, 0 };
1072 static double b2dip[5] = { 3.4869576, -14.915974, 15.372022, 0, 0 };
1073 static double c0dip[5] = { -0.0646774, 0.1975882, -0.8087562, 0.6902849, 0 };
1074 static double c1dip[5] = { -0.9520876, 2.9924258, -2.3802636, -0.2701261, 0 };
1075 static double c2dip[5] = { -0.6260979, 1.2924686, 1.6542783, -3.4396744, 0 };
1077 const static double conv = 7242.702976750923;
1079 vector<double> dipmSQ (ncomp, 0);
1080 for (
int i = 0; i < ncomp; i++) {
1084 vector<double> adip (5, 0);
1085 vector<double> bdip (5, 0);
1086 vector<double> cdip (5, 0);
1087 double J2, dJ2_det, detJ2_det, J3, dJ3_det, detJ3_det;
1090 for (
int i = 0; i < ncomp; i++) {
1091 for (
int j = 0; j < ncomp; j++) {
1099 for (
int l = 0; l < 5; l++) {
1100 adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
1101 bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
1102 J2 += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*pow(eta, l);
1103 dJ2_det += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*l*pow(eta, l-1);
1104 detJ2_det += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*(l+1)*pow(eta, l);
1107 pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*J2;
1109 pow(s_ij[j*ncomp+j],3)/pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*detJ2_det;
1111 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)
1112 /pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*
1116 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)
1117 /pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*
1121 for (
int k = 0; k < ncomp; k++) {
1129 for (
int l = 0; l < 5; l++) {
1130 cdip[l] = c0dip[l] + (m_ijk-1)/m_ijk*c1dip[l] + (m_ijk-1)/m_ijk*(m_ijk-2)/m_ijk*c2dip[l];
1131 J3 += cdip[l]*pow(eta, l);
1132 dJ3_det += cdip[l]*l*pow(eta, (l-1));
1133 detJ3_det += cdip[l]*(l+2)*pow(eta, (l+1));
1136 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]/
1138 dipmSQ[j]*dipmSQ[k]*J3;
1140 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]/
1142 dipmSQ[j]*dipmSQ[k]*detJ3_det;
1143 if ((i == j) && (i == k)) {
1144 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)
1145 *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]
1150 else if ((i == j) || (i == k)) {
1151 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)
1152 *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]
1158 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)
1159 *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]
1169 A3 = -4/3.*PI*PI*den*den*A3;
1170 dA2_det = -PI*den/eta*dA2_det;
1171 dA3_det = -4/3.*PI*PI*den/eta*den/eta*dA3_det;
1172 for (
int i = 0; i < ncomp; i++) {
1173 dA2_dx[i] = -PI*den*dA2_dx[i];
1174 dA3_dx[i] = -4/3.*PI*PI*den*den*dA3_dx[i];
1177 vector<double> dapolar_dx(ncomp);
1178 for (
int i = 0; i < ncomp; i++) {
1179 dapolar_dx[i] = (dA2_dx[i]*(1-A3/A2) + (dA3_dx[i]*A2 - A3*dA2_dx[i])/A2)/pow(1-A3/A2,2);
1183 double ares_polar = A2/(1-A3/A2);
1184 double Zpolar = eta*((dA2_det*(1-A3/A2)+(dA3_det*A2-A3*dA2_det)/A2)/(1-A3/A2)/(1-A3/A2));
1185 for (
int i = 0; i < ncomp; i++) {
1186 for (
int j = 0; j < ncomp; j++) {
1189 mu_polar[i] = ares_polar + Zpolar + dapolar_dx[i] - mu_polar[i];
1195 vector<double> mu_assoc(ncomp, 0);
1199 for(std::vector<int>::iterator it =
assoc_num.begin(); it !=
assoc_num.end(); ++it) {
1201 for (
int i = 0; i < *it; i++) {
1206 vector<double> x_assoc(num_sites);
1207 for (
int i = 0; i < num_sites; i++) {
1212 vector<double> XA (num_sites, 0);
1213 vector<double> delta_ij(num_sites * num_sites, 0);
1217 for (
int i = 0; i < num_sites; i++) {
1218 idxi = iA[i]*ncomp+iA[i];
1219 for (
int j = 0; j < num_sites; j++) {
1220 idxj = iA[j]*ncomp+iA[j];
1223 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1224 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1225 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1229 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
1230 if (!std::isfinite(XA[i])) {
1235 vector<double> ddelta_dx(num_sites * num_sites * ncomp, 0);
1237 for (
int k = 0; k < ncomp; k++) {
1241 for (
int i = 0; i < num_sites; i++) {
1242 idxi = iA[i]*ncomp+iA[i];
1243 for (
int j = 0; j < num_sites; j++) {
1244 idxj = iA[j]*ncomp+iA[j];
1247 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1248 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1249 double dghsd_dx = PI/6.*
components[k].getM()*(pow(d[k], 3)/(1-zeta[3])/(1-zeta[3]) + 3*d[iA[i]]*d[iA[j]]/
1250 (d[iA[i]]+d[iA[j]])*(d[k]*d[k]/(1-zeta[3])/(1-zeta[3])+2*pow(d[k], 3)*
1251 zeta[2]/pow(1-zeta[3], 3)) + 2*pow((d[iA[i]]*d[iA[j]]/(d[iA[i]]+d[iA[j]])), 2)*
1252 (2*d[k]*d[k]*zeta[2]/pow(1-zeta[3], 3)+3*(pow(d[k], 3)*zeta[2]*zeta[2]
1253 /pow(1-zeta[3], 4))));
1254 ddelta_dx[idx_ddelta] = dghsd_dx*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1264 vector<double> XA_old = XA;
1265 while ((ctr < 100) && (dif > 1e-15)) {
1267 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
1269 for (
int i = 0; i < num_sites; i++) {
1270 dif += abs(XA[i] - XA_old[i]);
1272 for (
int i = 0; i < num_sites; i++) {
1273 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
1277 vector<double> dXA_dx(num_sites*ncomp, 0);
1281 for (
int i = 0; i < ncomp; i++) {
1282 for (
int j = 0; j < num_sites; j++) {
1283 mu_assoc[i] +=
mole_fractions[iA[j]]*den*dXA_dx[ij]*(1/XA[j]-0.5);
1288 for (
int i = 0; i < num_sites; i++) {
1289 mu_assoc[iA[i]] += log(XA[i]) - 0.5*XA[i] + 0.5;
1294 vector<double> mu_ion(ncomp, 0);
1296 vector<double> q(ncomp);
1297 for (
int i = 0; i < ncomp; i++) {
1302 for (
int i = 0; i < ncomp; i++) {
1306 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
1309 vector<double> chi(ncomp);
1310 vector<double> sigma_k(ncomp);
1313 for (
int i = 0; i < ncomp; i++) {
1314 chi[i] = 3 / pow(kappa * d[i], 3)
1315 * (1.5 + log(1 + kappa * d[i]) - 2 * (1 + kappa * d[i])
1316 + 0.5 * pow(1 + kappa * d[i], 2));
1317 sigma_k[i] = -2 * chi[i] + 3 / (1 + kappa * d[i]);
1322 for (
int i = 0; i < ncomp; i++) {
1323 mu_ion[i] = -q[i] * q[i] * kappa / 24. / PI / kb /
_T / (
dielc * perm_vac) * (2 * chi[i] + summ1 / summ2);
1330 vector<double> mu(ncomp, 0);
1331 vector<CoolPropDbl> fugcoef(ncomp, 0);
1332 for (
int i = 0; i < ncomp; i++) {
1333 mu[i] = mu_hc[i] + mu_disp[i] + mu_polar[i] + mu_assoc[i] + mu_ion[i];
1334 fugcoef[i] = exp(mu[i] - log(Z));
1344 CoolPropDbl gres = (ares + (Z - 1) - log(Z)) * kb * N_AV *
_T;
1350 vector<double> d(ncomp);
1351 for (
int i = 0; i < ncomp; i++) {
1355 for (
int i = 0; i < ncomp; i++) {
1357 d[i] =
components[i].getSigma() * (1 - 0.12);
1364 vector<double> zeta(4, 0);
1366 for (
int i = 0; i < 4; i++) {
1368 for (
int j = 0; j < ncomp; j++) {
1371 zeta[i] = PI / 6 * den * summ;
1374 double eta = zeta[3];
1376 for (
int i = 0; i < ncomp; i++) {
1380 vector<double> ghs (ncomp*ncomp, 0);
1381 vector<double> denghs (ncomp*ncomp, 0);
1382 vector<double> e_ij (ncomp*ncomp, 0);
1383 vector<double> s_ij (ncomp*ncomp, 0);
1387 for (
int i = 0; i < ncomp; i++) {
1388 for (
int j = 0; j < ncomp; j++) {
1409 ghs[idx] = 1/(1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
1410 pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
1411 denghs[idx] = zeta[3]/(1-zeta[3])/(1-zeta[3]) +
1412 (d[i]*d[j]/(d[i]+d[j]))*(3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
1413 6*zeta[2]*zeta[3]/pow(1-zeta[3], 3)) +
1414 pow(d[i]*d[j]/(d[i]+d[j]), 2)*(4*zeta[2]*zeta[2]/pow(1-zeta[3], 3) +
1415 6*zeta[2]*zeta[2]*zeta[3]/pow(1-zeta[3], 4));
1419 double Zhs = zeta[3] / (1 - zeta[3]) + 3. * zeta[1] * zeta[2] / zeta[0] / (1. - zeta[3]) / (1. - zeta[3])
1420 + (3. * pow(zeta[2], 3.) - zeta[3] * pow(zeta[2], 3.)) / zeta[0] / pow(1. - zeta[3], 3.);
1422 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
1423 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
1424 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
1425 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
1426 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
1427 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
1429 vector<double> a(7, 0);
1430 vector<double> b(7, 0);
1431 for (
int i = 0; i < 7; i++) {
1432 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
1433 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
1436 double detI1_det = 0.0;
1437 double detI2_det = 0.0;
1439 for (
int i = 0; i < 7; i++) {
1440 detI1_det += a[i] * (i + 1) * pow(eta, i);
1441 detI2_det += b[i] * (i + 1) * pow(eta, i);
1442 I2 += b[i] * pow(eta, i);
1445 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
1446 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
1447 double C2 = -1. * C1 * C1
1448 * (m_avg * (-4 * eta * eta + 20 * eta + 8) / pow(1 - eta, 5)
1449 + (1 - m_avg) * (2 * pow(eta, 3) + 12 * eta * eta - 48 * eta + 40) / pow((1 - eta) * (2 - eta), 3.0));
1452 for (
int i = 0; i < ncomp; i++) {
1457 double Zhc = m_avg * Zhs - summ;
1458 double Zdisp = -2 * PI * den * detI1_det * m2es3 - PI * den * m_avg * (C1 * detI2_det + C2 * eta * I2) * m2e2s3;
1465 double dA2_det = 0.;
1466 double dA3_det = 0.;
1467 vector<double> adip(5, 0);
1468 vector<double> bdip(5, 0);
1469 vector<double> cdip(5, 0);
1470 vector<double> dipmSQ(ncomp, 0);
1471 double J2, detJ2_det, J3, detJ3_det;
1473 static double a0dip[5] = {0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308};
1474 static double a1dip[5] = {0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135};
1475 static double a2dip[5] = {-1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575};
1476 static double b0dip[5] = {0.2187939, -1.1896431, 1.1626889, 0, 0};
1477 static double b1dip[5] = {-0.5873164, 1.2489132, -0.5085280, 0, 0};
1478 static double b2dip[5] = {3.4869576, -14.915974, 15.372022, 0, 0};
1479 static double c0dip[5] = {-0.0646774, 0.1975882, -0.8087562, 0.6902849, 0};
1480 static double c1dip[5] = {-0.9520876, 2.9924258, -2.3802636, -0.2701261, 0};
1481 static double c2dip[5] = {-0.6260979, 1.2924686, 1.6542783, -3.4396744, 0};
1483 const static double conv = 7242.702976750923;
1485 for (
int i = 0; i < ncomp; i++) {
1490 for (
int i = 0; i < ncomp; i++) {
1491 for (
int j = 0; j < ncomp; j++) {
1498 for (
int l = 0; l < 5; l++) {
1499 adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
1500 bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
1501 J2 += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*pow(eta, l);
1502 detJ2_det += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*(l+1)*pow(eta, l);
1505 pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*J2;
1507 pow(s_ij[j*ncomp+j],3)/pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*detJ2_det;
1512 for (
int i = 0; i < ncomp; i++) {
1513 for (
int j = 0; j < ncomp; j++) {
1514 for (
int k = 0; k < ncomp; k++) {
1521 for (
int l = 0; l < 5; l++) {
1522 cdip[l] = c0dip[l] + (m_ijk-1)/m_ijk*c1dip[l] + (m_ijk-1)/m_ijk*(m_ijk-2)/m_ijk*c2dip[l];
1523 J3 += cdip[l]*pow(eta, l);
1524 detJ3_det += cdip[l]*(l+2)*pow(eta, (l+1));
1527 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]/
1529 dipmSQ[j]*dipmSQ[k]*J3;
1531 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]/
1533 dipmSQ[j]*dipmSQ[k]*detJ3_det;
1539 A3 = -4/3.*PI*PI*den*den*A3;
1540 dA2_det = -PI*den/eta*dA2_det;
1541 dA3_det = -4/3.*PI*PI*den/eta*den/eta*dA3_det;
1544 Zpolar = eta*((dA2_det*(1-A3/A2)+(dA3_det*A2-A3*dA2_det)/A2)/(1-A3/A2)/(1-A3/A2));
1553 for(std::vector<int>::iterator it =
assoc_num.begin(); it !=
assoc_num.end(); ++it) {
1555 for (
int i = 0; i < *it; i++) {
1560 vector<double> x_assoc(num_sites);
1561 for (
int i = 0; i < num_sites; i++) {
1566 vector<double> XA (num_sites, 0);
1567 vector<double> delta_ij(num_sites * num_sites, 0);
1571 for (
int i = 0; i < num_sites; i++) {
1572 idxi = iA[i]*ncomp+iA[i];
1573 for (
int j = 0; j < num_sites; j++) {
1574 idxj = iA[j]*ncomp+iA[j];
1577 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1578 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1579 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1583 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
1584 if (!std::isfinite(XA[i])) {
1589 vector<double> ddelta_dx(num_sites * num_sites * ncomp, 0);
1591 for (
int k = 0; k < ncomp; k++) {
1595 for (
int i = 0; i < num_sites; i++) {
1596 idxi = iA[i]*ncomp+iA[i];
1597 for (
int j = 0; j < num_sites; j++) {
1598 idxj = iA[j]*ncomp+iA[j];
1601 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1602 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1603 double dghsd_dx = PI/6.*
components[k].getM()*(pow(d[k], 3)/(1-zeta[3])/(1-zeta[3]) + 3*d[iA[i]]*d[iA[j]]/
1604 (d[iA[i]]+d[iA[j]])*(d[k]*d[k]/(1-zeta[3])/(1-zeta[3])+2*pow(d[k], 3)*
1605 zeta[2]/pow(1-zeta[3], 3)) + 2*pow((d[iA[i]]*d[iA[j]]/(d[iA[i]]+d[iA[j]])), 2)*
1606 (2*d[k]*d[k]*zeta[2]/pow(1-zeta[3], 3)+3*(pow(d[k], 3)*zeta[2]*zeta[2]
1607 /pow(1-zeta[3], 4))));
1608 ddelta_dx[idx_ddelta] = dghsd_dx*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1618 vector<double> XA_old = XA;
1619 while ((ctr < 100) && (dif > 1e-14)) {
1621 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
1623 for (
int i = 0; i < num_sites; i++) {
1624 dif += abs(XA[i] - XA_old[i]);
1626 for (
int i = 0; i < num_sites; i++) {
1627 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
1631 vector<double> dXA_dx(num_sites*ncomp, 0);
1636 for (
int i = 0; i < ncomp; i++) {
1637 for (
int j = 0; j < num_sites; j++) {
1649 vector<double> q(ncomp);
1650 for (
int i = 0; i < ncomp; i++) {
1655 for (
int i = 0; i < ncomp; i++) {
1660 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
1663 double chi, sigma_k;
1665 for (
int i = 0; i < ncomp; i++) {
1666 chi = 3 / pow(kappa * d[i], 3)
1667 * (1.5 + log(1 + kappa * d[i]) - 2 * (1 + kappa * d[i])
1668 + 0.5 * pow(1 + kappa * d[i], 2));
1669 sigma_k = -2 * chi + 3 / (1 + kappa * d[i]);
1672 Zion = -1 * kappa / 24. / PI / kb /
_T / (
dielc * perm_vac) * summ;
1676 double Z = Zid + Zhc + Zdisp + Zpolar + Zassoc + Zion;
1692 throw ValueError(
"rhomolar is less than zero");
1695 throw ValueError(
"rhomolar is not a valid number");
1698 if (optional_checks) {
1710 std::cout <<
format(
"%s (%d): update called with (%d: (%s), %g, %g)", __FILE__, __LINE__, input_pair,
1716 CoolPropDbl ld_value1 = value1, ld_value2 = value2;
1724 throw ValueError(
"Mole fractions must be set");
1727 if (
SatL->mole_fractions.empty()) {
1730 if (
SatV->mole_fractions.empty()) {
1733 for (
int i = 0; i <
N; i++) {
1734 if (
SatV->components[i].getZ() != 0) {
1735 SatV->mole_fractions[i] = 0;
1738 summ +=
SatV->mole_fractions[i];
1741 for (
int i = 0; i <
N; i++) {
1742 SatV->mole_fractions[i] =
SatV->mole_fractions[i] / summ;
1749 switch (input_pair) {
1795 if ((
_Q < 0) || (
_Q > 1)) {
1802 SatL->_rhomolar = value1;
SatV->_rhomolar = value1;
1803 SatL->_T = value2;
SatV->_T = value2;
1853 double p_input, rho_input;
1854 double p_bub, p_dew, p_equil;
1864 if (p_input > 1.6 * p_equil) {
1867 else if (p_input < 0.5 * p_equil) {
1887 else if (
_p == p_bub) {
1899 else if ((
_p <= p_bub) && (
_p >= p_dew)) {
1909 double rho_bub, rho_dew;
1960 bool solution_found =
false;
1966 solution_found =
true;
1972 if (!solution_found) {
1973 double p_lbound = -6;
1974 double p_ubound = 9;
1975 double p_step = 0.1;
1977 while (p_guess < p_ubound && !solution_found) {
1979 p =
outerTQ(pow(10, p_guess), PCSAFT);
1980 solution_found =
true;
1989 if (!solution_found) {
1990 throw SolutionError(
"solution could not be found for TQ flash");
2001 bool solution_found =
false;
2007 solution_found =
true;
2013 if (!solution_found) {
2014 double t_lbound = 1;
2015 double t_ubound = 800;
2022 while (t_guess > t_lbound && !solution_found) {
2025 solution_found =
true;
2034 if (!solution_found) {
2035 throw SolutionError(
"solution could not be found for PQ flash");
2049 double MAXITER = 200;
2057 vector<CoolPropDbl> u;
2060 : PCSAFT(PCSAFT), kb0(kb0), u(u){}
2065 vector<double> pp(ncomp, 0);
2067 for (
int i = 0; i < ncomp; i++) {
2069 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * exp(u[i]));
2077 error = pow((L + PCSAFT.
_Q - 1), 2.);
2083 for (
int i = 0; i < ncomp; i++) {
2090 vector<double> k(ncomp, 0), u(ncomp, 0), kprime(ncomp, 0), uprime(ncomp, 0);
2091 double Tref = t_guess - 1;
2092 double Tprime = t_guess + 1;
2095 PCSAFT.
SatL->_T = t;
2096 PCSAFT.
SatV->_T = t;
2111 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2112 throw SolutionError(
"liquid and vapor densities are the same.");
2114 vector<double> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2115 vector<double> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2119 for (
int i = 0; i < ncomp; i++) {
2121 k[i] = fugcoef_l[i] / fugcoef_v[i];
2126 xl_sum += PCSAFT.
SatL->mole_fractions[i];
2127 PCSAFT.
SatV->mole_fractions[i] = k[i] * PCSAFT.
mole_fractions[i] / (1 + PCSAFT.
_Q * (k[i] - 1));
2128 xv_sum += PCSAFT.
SatV->mole_fractions[i];
2132 for (
int i = 0; i < ncomp; i++) {
2133 PCSAFT.
SatV->mole_fractions[i] = PCSAFT.
SatV->mole_fractions[i] / xv_sum;
2138 for (
int i = 0; i < ncomp; i++) {
2139 PCSAFT.
SatL->mole_fractions[i] = PCSAFT.
SatL->mole_fractions[i] / xl_sum;
2144 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2146 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2147 for (
int i = 0; i < ncomp; i++) {
2148 k[i] = fugcoef_l[i] / fugcoef_v[i];
2151 PCSAFT.
SatL->_T = Tprime;
2152 PCSAFT.
SatV->_T = Tprime;
2156 PCSAFT.
SatL->components[
water_idx].calc_water_sigma(Tprime);
2157 PCSAFT.
SatV->components[
water_idx].calc_water_sigma(Tprime);
2163 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2165 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2166 for (
int i = 0; i < ncomp; i++) {
2167 kprime[i] = fugcoef_l[i] / fugcoef_v[i];
2170 vector<double> t_weight(ncomp);
2172 for (
int i = 0; i < ncomp; i++) {
2173 double dlnk_dt = (kprime[i] - k[i]) / (Tprime - t);
2174 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (k[i] - 1));
2175 t_sum += t_weight[i];
2179 for (
int i = 0; i < ncomp; i++) {
2180 double wi = t_weight[i] / t_sum;
2182 kb += wi * std::log(k[i]);
2188 for (
int i = 0; i < ncomp; i++) {
2189 double dlnk_dt = (kprime[i] - k[i]) / (Tprime - t);
2190 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (kprime[i] - 1));
2191 t_sum += t_weight[i];
2195 for (
int i = 0; i < ncomp; i++) {
2196 double wi = t_weight[i] / t_sum;
2198 kbprime += wi * std::log(kprime[i]);
2201 kbprime = std::exp(kbprime);
2202 double kb0 = kbprime;
2204 for (
int i = 0; i < ncomp; i++) {
2205 u[i] = std::log(k[i] / kb);
2206 uprime[i] = std::log(kprime[i] / kbprime);
2209 double B = std::log(kbprime / kb) / (1/Tprime - 1/t);
2210 double A = std::log(kb) - B * (1/t - 1/Tref);
2213 SolverInnerResid resid(*
this, kb0, u);
2215 vector<double> pp(ncomp, 0);
2216 double maxdif = 1e10 * TOL;
2218 double Rmin = 0, Rmax = 1;
2219 while (maxdif > TOL && itr < MAXITER) {
2221 vector<double> u_old = u;
2225 double R0 = kb * PCSAFT.
_Q / (kb * PCSAFT.
_Q + kb0 * (1 - PCSAFT.
_Q));
2227 if (resid.call(R) > TOL) {
2232 double eupp_sum = 0;
2233 for (
int i = 0; i < ncomp; i++) {
2234 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * std::exp(u[i]));
2237 eupp_sum += std::exp(u[i]) * pp[i];
2240 kb = pp_sum / eupp_sum;
2242 t = 1 / (1 / Tref + (std::log(kb) - A) / B);
2243 for (
int i = 0; i < ncomp; i++) {
2245 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum;
2246 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2249 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum * (1 - x_ions / (1 - PCSAFT.
_Q));
2250 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2254 PCSAFT.
SatV->mole_fractions[i] = 0;
2258 PCSAFT.
SatL->_T = t;
2259 PCSAFT.
SatV->_T = t;
2270 vector<CoolPropDbl> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2272 vector<CoolPropDbl> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2273 for (
int i = 0; i < ncomp; i++) {
2274 k[i] = fugcoef_l[i] / fugcoef_v[i];
2275 u[i] = std::log(k[i] / kb);
2279 B = std::log(kbprime / kb) / (1/Tprime - 1/t);
2284 A = std::log(kb) - B * (1/t - 1/Tref);
2286 maxdif = std::abs(A - A_old);
2287 for (
int i = 0; i < ncomp; i++) {
2289 double dif = std::abs(u[i] - u_old[i]);
2299 if (!std::isfinite(t) || maxdif > 1e-3 || t < 0) {
2300 throw SolutionError(
"outerPQ did not converge to a solution");
2311 double MAXITER = 200;
2319 vector<CoolPropDbl> u;
2322 : PCSAFT(PCSAFT), kb0(kb0), u(u){}
2327 vector<double> pp(ncomp, 0);
2330 for (
int i = 0; i < ncomp; i++) {
2332 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * exp(u[i]));
2340 error = pow((L + PCSAFT.
_Q - 1), 2.);
2346 for (
int i = 0; i < ncomp; i++) {
2353 vector<double> k(ncomp, 0), u(ncomp, 0), kprime(ncomp, 0), uprime(ncomp, 0);
2354 double Pref = p_guess - 0.01 * p_guess;
2355 double Pprime = p_guess + 0.01 * p_guess;
2356 if (p_guess > 1e6) {
2357 Pprime = p_guess - 0.005 * p_guess;
2364 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2365 throw SolutionError(
"liquid and vapor densities are the same.");
2367 vector<double> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2368 vector<double> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2372 for (
int i = 0; i < ncomp; i++) {
2374 k[i] = fugcoef_l[i] / fugcoef_v[i];
2379 xl_sum += PCSAFT.
SatL->mole_fractions[i];
2380 PCSAFT.
SatV->mole_fractions[i] = k[i] * PCSAFT.
mole_fractions[i] / (1 + PCSAFT.
_Q * (k[i] - 1));
2381 xv_sum += PCSAFT.
SatV->mole_fractions[i];
2385 for (
int i = 0; i < ncomp; i++) {
2386 PCSAFT.
SatV->mole_fractions[i] = PCSAFT.
SatV->mole_fractions[i] / xv_sum;
2391 for (
int i = 0; i < ncomp; i++) {
2392 PCSAFT.
SatL->mole_fractions[i] = PCSAFT.
SatL->mole_fractions[i] / xl_sum;
2397 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2399 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2400 for (
int i = 0; i < ncomp; i++) {
2401 k[i] = fugcoef_l[i] / fugcoef_v[i];
2402 u[i] = std::log(k[i] / kb);
2406 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2408 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2409 for (
int i = 0; i < ncomp; i++) {
2410 kprime[i] = fugcoef_l[i] / fugcoef_v[i];
2413 vector<double> t_weight(ncomp);
2415 for (
int i = 0; i < ncomp; i++) {
2416 double dlnk_dt = (kprime[i] - k[i]) / (Pprime -
p);
2417 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (k[i] - 1));
2418 t_sum += t_weight[i];
2422 for (
int i = 0; i < ncomp; i++) {
2423 double wi = t_weight[i] / t_sum;
2425 kb += wi * std::log(k[i]);
2431 for (
int i = 0; i < ncomp; i++) {
2432 double dlnk_dt = (kprime[i] - k[i]) / (Pprime -
p);
2433 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (kprime[i] - 1));
2434 t_sum += t_weight[i];
2438 for (
int i = 0; i < ncomp; i++) {
2439 double wi = t_weight[i] / t_sum;
2441 kbprime += wi * std::log(kprime[i]);
2444 kbprime = std::exp(kbprime);
2445 double kb0 = kbprime;
2447 for (
int i = 0; i < ncomp; i++) {
2448 u[i] = std::log(k[i] / kb);
2449 uprime[i] = std::log(kprime[i] / kbprime);
2452 double B = std::log(kbprime / kb) / (1/Pprime - 1/
p);
2453 double A = std::log(kb) - B * (1/
p - 1/Pref);
2460 SolverInnerResid resid(*
this, kb0, u);
2462 vector<double> pp(ncomp, 0);
2463 double maxdif = 1e10 * TOL;
2465 double Rmin = 0, Rmax = 1;
2466 while (maxdif > TOL && itr < MAXITER) {
2468 vector<double> u_old = u;
2471 double R0 = kb * PCSAFT.
_Q / (kb * PCSAFT.
_Q + kb0 * (1 - PCSAFT.
_Q));
2474 if (resid.call(R) > TOL) {
2479 double eupp_sum = 0;
2480 for (
int i = 0; i < ncomp; i++) {
2481 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * std::exp(u[i]));
2484 eupp_sum += std::exp(u[i]) * pp[i];
2487 kb = pp_sum / eupp_sum;
2489 p = 1 / (1 / Pref + (std::log(kb) - A) / B);
2490 for (
int i = 0; i < ncomp; i++) {
2492 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum;
2493 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2496 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum * (1 - x_ions/(1 - PCSAFT.
_Q));
2497 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2501 PCSAFT.
SatV->mole_fractions[i] = 0;
2506 vector<CoolPropDbl> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2508 vector<CoolPropDbl> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2509 for (
int i = 0; i < ncomp; i++) {
2510 k[i] = fugcoef_l[i] / fugcoef_v[i];
2511 u[i] = std::log(k[i] / kb);
2515 B = std::log(kbprime / kb) / (1/Pprime - 1/
p);
2517 A = std::log(kb) - B * (1/
p - 1/Pref);
2519 maxdif = std::abs(A - A_old);
2520 for (
int i = 0; i < ncomp; i++) {
2522 double dif = std::abs(u[i] - u_old[i]);
2525 }
else if (!std::isfinite(dif)) {
2533 if (!std::isfinite(
p) || !std::isfinite(maxdif) || maxdif > 0.1 ||
p < 0) {
2534 throw SolutionError(
"outerTQ did not converge to a solution");
2544 double t_guess = _HUGE;
2548 for (
int i = 0; i < ncomp; i++) {
2554 bool guess_found =
false;
2556 double t_start = 571;
2557 double t_lbound = 1;
2563 while (!guess_found && t_start > t_lbound) {
2565 double Tprime = t_start - 50;
2568 PCSAFT.
SatL->_T = t;
2569 PCSAFT.
SatV->_T = t;
2583 PCSAFT.
SatL->_T = Tprime;
2584 PCSAFT.
SatV->_T = Tprime;
2586 PCSAFT.
SatL->_T = t;
2587 PCSAFT.
SatV->_T = t;
2589 double slope = (std::log10(p1) - std::log10(p2)) / (1/t - 1/Tprime);
2590 double intercept = std::log10(p1) - slope * (1/t);
2591 t_guess = slope / (std::log10(PCSAFT.
_p) - intercept);
2599 throw SolutionError(
"an estimate for the VLE temperature could not be found");
2610 double p_guess = _HUGE;
2614 for (
int i = 0; i < ncomp; i++) {
2620 bool guess_found =
false;
2621 double p_start = 10000;
2622 while (!guess_found && p_start < 1e7) {
2624 vector<double> k(ncomp, 0), u(ncomp, 0), kprime(ncomp, 0), uprime(ncomp, 0);
2625 double Pprime = 0.99 * p_start;
2631 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2632 p_start = p_start + 2e5;
2635 vector<double> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2636 vector<double> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2641 for (
int i = 0; i < ncomp; i++) {
2643 k[i] = fugcoef_l[i] / fugcoef_v[i];
2648 xl_sum += PCSAFT.
SatL->mole_fractions[i];
2649 PCSAFT.
SatV->mole_fractions[i] = k[i] * PCSAFT.
mole_fractions[i] / (1 + PCSAFT.
_Q * (k[i] - 1));
2650 xv_sum += PCSAFT.
SatV->mole_fractions[i];
2654 for (
int i = 0; i < ncomp; i++) {
2655 PCSAFT.
SatV->mole_fractions[i] = PCSAFT.
SatV->mole_fractions[i] / xv_sum;
2660 for (
int i = 0; i < ncomp; i++) {
2661 PCSAFT.
SatL->mole_fractions[i] = PCSAFT.
SatL->mole_fractions[i] / xl_sum;
2667 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2668 p_start = p_start + 2e5;
2671 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2672 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2675 for (
int i = 0; i < ncomp; i++) {
2677 numer += PCSAFT.
SatL->mole_fractions[i] * fugcoef_l[i];
2678 denom += PCSAFT.
SatV->mole_fractions[i] * fugcoef_v[i];
2681 double ratio = numer / denom;
2685 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2686 p_start = p_start + 2e5;
2689 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2690 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2693 for (
int i = 0; i < ncomp; i++) {
2695 numer += PCSAFT.
SatL->mole_fractions[i] * fugcoef_l[i];
2696 denom += PCSAFT.
SatV->mole_fractions[i] * fugcoef_v[i];
2699 double ratio_prime = numer / denom;
2701 double slope = (std::log10(ratio) - std::log10(ratio_prime)) / (std::log10(
p) - std::log10(Pprime));
2702 double intercept = std::log10(ratio) - slope * std::log10(
p);
2703 p_guess = std::pow(10, -intercept / slope);
2709 throw SolutionError(
"an estimate for the VLE pressure could not be found");
2726 double cost = (peos -
p) /
p;
2735 SolverRhoResid resid(*
this,
T,
p);
2738 vector<double> x_lo, x_hi;
2740 double limit_lower = -8;
2741 double limit_upper = -1;
2742 double rho_guess = 1e-13;
2743 double rho_guess_prev = rho_guess;
2745 for (
int i = 0; i < num_pts; i++) {
2746 rho_guess = pow(10, (limit_upper - limit_lower) / (
double)num_pts * i + limit_lower);
2748 if (err * err_prev < 0) {
2749 x_lo.push_back(rho_guess_prev);
2750 x_hi.push_back(rho_guess);
2753 rho_guess_prev = rho_guess;
2757 limit_upper = 0.7405;
2758 for (
int i = 0; i < num_pts; i++) {
2759 rho_guess = (limit_upper - limit_lower) / (
double)num_pts * i + limit_lower;
2761 if (err * err_prev < 0) {
2762 x_lo.push_back(rho_guess_prev);
2763 x_hi.push_back(rho_guess);
2766 rho_guess_prev = rho_guess;
2771 double x_lo_molar = 1e-8, x_hi_molar = 1e7;
2773 if (x_lo.size() == 1) {
2778 }
else if (x_lo.size() <= 3 && !x_lo.empty()) {
2790 }
else if (x_lo.size() > 3) {
2792 double g_min = 1e60;
2793 for (
int i = 0; i < x_lo.size(); i++) {
2797 double rho_i =
Brent(resid, x_lo_molar, x_hi_molar,
DBL_EPSILON, 1e-8, 200);
2809 double err_min = 1e40;
2811 for (
int i = 0; i < num_pts; i++) {
2812 double rho_guess = (0.7405 - 1e-8) / (
double)num_pts * i + 1e-8;
2814 if (abs(err) < err_min) {
2826 vector<CoolPropDbl> d(
N);
2828 for (
int i = 0; i <
N; i++) {
2832 return 6 / PI * nu / summ * 1.0e30 / N_AV;
2837 for (
unsigned int i = 0; i <
N; ++i) {
2847 int num_sites = XA_guess.size();
2848 vector<double> XA = XA_guess;
2851 for (
int i = 0; i < num_sites; i++) {
2853 for (
int j = 0; j < num_sites; j++) {
2855 summ += den*x[j]*XA_guess[j]*delta_ij[idxij];
2857 XA[i] = 1./(1.+summ);
2865 vector<double> XA, vector<double> ddelta_dt, vector<double> x) {
2867 int num_sites = XA.size();
2868 Eigen::MatrixXd B = Eigen::MatrixXd::Zero(num_sites, 1);
2869 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(num_sites, num_sites);
2873 for (
int i = 0; i < num_sites; i++) {
2875 for (
int j = 0; j < num_sites; j++) {
2876 B(i) -= x[j]*XA[j]*ddelta_dt[ij];
2877 A(i,j) = x[j]*delta_ij[ij];
2878 summ += x[j]*XA[j]*delta_ij[ij];
2881 A(i,i) = pow(1+den*summ, 2.)/den;
2884 Eigen::MatrixXd solution = A.lu().solve(B);
2885 vector<double> dXA_dt(num_sites);
2886 for (
int i = 0; i < num_sites; i++) {
2887 dXA_dt[i] = solution(i);
2894 double den, vector<double> XA, vector<double> ddelta_dx, vector<double> x) {
2897 int num_sites = XA.size();
2899 Eigen::MatrixXd B(num_sites*ncomp, 1);
2900 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(num_sites*ncomp, num_sites*ncomp);
2905 for (
int i = 0; i < ncomp; i++) {
2906 for (
int j = 0; j < num_sites; j++) {
2908 for (
int k = 0; k < num_sites; k++) {
2909 sum1 = sum1 + den*x[k]*(XA[k]*ddelta_dx[i*num_sites*num_sites + j*num_sites + k]);
2910 A(ij,i*num_sites+k) = XA[j]*XA[j]*den*x[k]*delta_ij[j*num_sites+k];
2914 for (
int l = 0; l <
assoc_num[i]; l++) {
2915 sum2 = sum2 + XA[idx1+l]*delta_ij[idx1*num_sites+l*num_sites+j];
2918 A(ij,ij) = A(ij,ij) + 1;
2919 B(ij) = -1*XA[j]*XA[j]*(sum1 + sum2);
2925 Eigen::MatrixXd solution = A.lu().solve(B);
2926 vector<double> dXA_dx(num_sites*ncomp);
2927 for (
int i = 0; i < num_sites*ncomp; i++) {
2928 dXA_dx[i] = solution(i);
2937 for (
int i = 0; i <
N; i++){
2938 vector<std::string> assoc_scheme =
components[i].getAssocScheme();
2940 int num = assoc_scheme.size();
2941 for (
int j = 0; j < num; j++) {
2944 charge.push_back(0);
2949 vector<int> tmp{0, 0};
2950 charge.insert(charge.end(), tmp.begin(), tmp.end());
2955 vector<int> tmp{-1, 1};
2956 charge.insert(charge.end(), tmp.begin(), tmp.end());
2961 vector<int> tmp{0, 0, 0};
2962 charge.insert(charge.end(), tmp.begin(), tmp.end());
2967 vector<int> tmp{-1, -1, 1};
2968 charge.insert(charge.end(), tmp.begin(), tmp.end());
2973 vector<int> tmp{0, 0, 0, 0};
2974 charge.insert(charge.end(), tmp.begin(), tmp.end());
2979 vector<int> tmp{1, 1, 1, -1};
2980 charge.insert(charge.end(), tmp.begin(), tmp.end());
2985 vector<int> tmp{-1, -1, 1, 1};
2986 charge.insert(charge.end(), tmp.begin(), tmp.end());
2991 throw ValueError(
format(
"%s is not a valid association type.", assoc_scheme[j]));
2998 for (std::vector<int>::iterator
i1 = charge.begin();
i1 != charge.end();
i1++) {
2999 for (std::vector<int>::iterator i2 = charge.begin(); i2 != charge.end(); i2++) {
3000 if (*
i1 == 0 || *i2 == 0) {
3003 else if (*
i1 == 1 && *i2 == -1) {
3006 else if (*
i1 == -1 && *i2 == 1) {
3035 throw ValueError(
"The current function for the dielectric constant for water is only valid for temperatures above 263.15 K.");
3036 }
else if (t <= 368.15) {
3037 dielc = 7.6555618295E-04 *
_T *
_T - 8.1783881423E-01 *
_T + 2.5419616803E+02;
3038 }
else if (t <= 443.15) {
3039 dielc = 0.0005003272124 *
_T *
_T - 0.6285556029 *
_T + 220.4467027;
3041 throw ValueError(
"The current function for the dielectric constant for water is only valid for temperatures less than 443.15 K.");