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++) {
263 vector<double> zeta(4, 0);
265 for (
int i = 0; i < 4; i++) {
267 for (
int j = 0; j < ncomp; j++) {
270 zeta[i] = PI / 6 * den * summ;
273 double eta = zeta[3];
275 for (
int i = 0; i < ncomp; i++) {
279 vector<double> ghs(ncomp * ncomp, 0);
280 vector<double> e_ij(ncomp * ncomp, 0);
281 vector<double> s_ij(ncomp * ncomp, 0);
285 for (
int i = 0; i < ncomp; i++) {
286 for (
int j = 0; j < ncomp; j++) {
309 ghs[idx] = 1 / (1 - zeta[3]) + (d[i] * d[j] / (d[i] + d[j])) * 3 * zeta[2] / (1 - zeta[3]) / (1 - zeta[3])
310 + pow(d[i] * d[j] / (d[i] + d[j]), 2) * 2 * zeta[2] * zeta[2] / pow(1 - zeta[3], 3);
314 double ares_hs = 1 / zeta[0]
315 * (3 * zeta[1] * zeta[2] / (1 - zeta[3]) + pow(zeta[2], 3.) / (zeta[3] * pow(1 - zeta[3], 2))
316 + (pow(zeta[2], 3.) / pow(zeta[3], 2.) - zeta[0]) * log(1 - zeta[3]));
318 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
319 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
320 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
321 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
322 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
323 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
325 vector<double> a(7, 0);
326 vector<double> b(7, 0);
327 for (
int i = 0; i < 7; i++) {
328 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
329 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
334 for (
int i = 0; i < 7; i++) {
335 I1 += a[i] * pow(eta, i);
336 I2 += b[i] * pow(eta, i);
339 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
340 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
343 for (
int i = 0; i < ncomp; i++) {
347 double ares_hc = m_avg * ares_hs - summ;
348 double ares_disp = -2 * PI * den * I1 * m2es3 - PI * den * m_avg * C1 * I2 * m2e2s3;
351 double ares_polar = 0.;
355 vector<double> dipmSQ(ncomp, 0);
357 static double a0dip[5] = {0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308};
358 static double a1dip[5] = {0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135};
359 static double a2dip[5] = {-1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575};
360 static double b0dip[5] = {0.2187939, -1.1896431, 1.1626889, 0, 0};
361 static double b1dip[5] = {-0.5873164, 1.2489132, -0.5085280, 0, 0};
362 static double b2dip[5] = {3.4869576, -14.915974, 15.372022, 0, 0};
363 static double c0dip[5] = {-0.0646774, 0.1975882, -0.8087562, 0.6902849, 0};
364 static double c1dip[5] = {-0.9520876, 2.9924258, -2.3802636, -0.2701261, 0};
365 static double c2dip[5] = {-0.6260979, 1.2924686, 1.6542783, -3.4396744, 0};
367 const static double conv = 7242.702976750923;
369 for (
int i = 0; i < ncomp; i++) {
373 vector<double> adip(5, 0);
374 vector<double> bdip(5, 0);
375 vector<double> cdip(5, 0);
379 for (
int i = 0; i < ncomp; i++) {
380 for (
int j = 0; j < ncomp; j++) {
386 for (
int l = 0; l < 5; l++) {
387 adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
388 bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
389 J2 += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*pow(eta, l);
392 * pow(s_ij[j * ncomp + j], 3) / pow(s_ij[i * ncomp + j], 3) *
components[i].getDipnum() *
components[j].getDipnum() * dipmSQ[i]
395 for (
int k = 0; k < ncomp; k++) {
401 for (
int l = 0; l < 5; l++) {
402 cdip[l] = c0dip[l] + (m_ijk - 1) / m_ijk * c1dip[l] + (m_ijk - 1) / m_ijk * (m_ijk - 2) / m_ijk * c2dip[l];
403 J3 += cdip[l] * pow(eta, l);
406 * 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)
407 / s_ij[i * ncomp + j] / s_ij[i * ncomp + k] / s_ij[j * ncomp + k] *
components[i].getDipnum() *
components[j].getDipnum()
408 *
components[k].getDipnum() * dipmSQ[i] * dipmSQ[j] * dipmSQ[k] * J3;
414 A3 = -4 / 3. * PI * PI * den * den * A3;
417 ares_polar = A2/(1-A3/A2);
422 double ares_assoc = 0.;
428 for (
int i = 0; i < *it; i++) {
433 vector<double> x_assoc(num_sites);
434 for (
int i = 0; i < num_sites; i++) {
439 vector<double> XA (num_sites, 0);
440 vector<double> delta_ij(num_sites * num_sites, 0);
444 for (
int i = 0; i < num_sites; i++) {
445 idxi = iA[i]*ncomp+iA[i];
446 for (
int j = 0; j < num_sites; j++) {
447 idxj = iA[j]*ncomp+iA[j];
450 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
451 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
452 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
456 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
457 if (!std::isfinite(XA[i])) {
464 vector<double> XA_old = XA;
465 while ((ctr < 100) && (dif > 1e-15)) {
467 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
469 for (
int i = 0; i < num_sites; i++) {
470 dif += abs(XA[i] - XA_old[i]);
472 for (
int i = 0; i < num_sites; i++) {
473 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
478 for (
int i = 0; i < num_sites; i++) {
484 double ares_ion = 0.;
486 vector<double> q(ncomp);
487 for (
int i = 0; i < ncomp; i++) {
492 for (
int i = 0; i < ncomp; i++) {
496 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
499 vector<double> chi(ncomp);
500 vector<double> sigma_k(ncomp);
502 for (
int i = 0; i < ncomp; i++) {
503 chi[i] = 3 / pow(kappa *
components[i].getSigma(), 3)
505 + 0.5 * pow(1 + kappa *
components[i].getSigma(), 2));
509 ares_ion = -1 / 12. / PI / kb /
_T / (
dielc * perm_vac) * summ;
513 CoolPropDbl ares = ares_hc + ares_disp + ares_polar + ares_assoc + ares_ion;
519 vector<double> d(ncomp), dd_dt(ncomp);
520 for (
int i = 0; i < ncomp; i++) {
525 for (
int i = 0; i < ncomp; i++) {
536 vector<double> zeta(4, 0);
538 for (
int i = 0; i < 4; i++) {
540 for (
int j = 0; j < ncomp; j++) {
543 zeta[i] = PI / 6 * den * summ;
546 vector<double> dzeta_dt(4, 0);
547 for (
int i = 1; i < 4; i++) {
549 for (
int j = 0; j < ncomp; j++) {
552 dzeta_dt[i] = PI / 6 * den * summ;
555 double eta = zeta[3];
557 for (
int i = 0; i < ncomp; i++) {
561 vector<double> ghs(ncomp * ncomp, 0);
562 vector<double> dghs_dt(ncomp * ncomp, 0);
563 vector<double> e_ij(ncomp * ncomp, 0);
564 vector<double> s_ij(ncomp * ncomp, 0);
569 for (
int i = 0; i < ncomp; i++) {
570 for (
int j = 0; j < ncomp; j++) {
591 ghs[idx] = 1 / (1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
592 pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
593 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]));
594 dghs_dt[idx] = dzeta_dt[3]/pow(1-zeta[3], 2.)
595 + 3*(ddij_dt*zeta[2]+(d[i]*d[j]/(d[i]+d[j]))*dzeta_dt[2])/pow(1-zeta[3], 2.)
596 + 4*(d[i]*d[j]/(d[i]+d[j]))*zeta[2]*(1.5*dzeta_dt[3]+ddij_dt*zeta[2]
597 + (d[i]*d[j]/(d[i]+d[j]))*dzeta_dt[2])/pow(1-zeta[3], 3.)
598 + 6*pow((d[i]*d[j]/(d[i]+d[j]))*zeta[2], 2.)*dzeta_dt[3]/pow(1-zeta[3], 4.);
602 double dadt_hs = 1/zeta[0]*(3*(dzeta_dt[1]*zeta[2] + zeta[1]*dzeta_dt[2])/(1-zeta[3])
603 + 3*zeta[1]*zeta[2]*dzeta_dt[3]/pow(1-zeta[3], 2.)
604 + 3*pow(zeta[2], 2.)*dzeta_dt[2]/zeta[3]/pow(1-zeta[3], 2.)
605 + pow(zeta[2],3.)*dzeta_dt[3]*(3*zeta[3]-1)/pow(zeta[3], 2.)/pow(1-zeta[3], 3.)
606 + (3*pow(zeta[2], 2.)*dzeta_dt[2]*zeta[3] - 2*pow(zeta[2], 3.)*dzeta_dt[3])/pow(zeta[3], 3.)
608 + (zeta[0]-pow(zeta[2],3)/pow(zeta[3],2.))*dzeta_dt[3]/(1-zeta[3]));
610 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
611 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
612 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
613 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
614 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
615 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
617 vector<double> a(7, 0);
618 vector<double> b(7, 0);
619 for (
int i = 0; i < 7; i++) {
620 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
621 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
626 double dI1_dt = 0.0, dI2_dt = 0.;
627 for (
int i = 0; i < 7; i++) {
628 I1 += a[i] * pow(eta, i);
629 I2 += b[i] * pow(eta, i);
630 dI1_dt += a[i] * dzeta_dt[3] * i * pow(eta, i - 1);
631 dI2_dt += b[i] * dzeta_dt[3] * i * pow(eta, i - 1);
634 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
635 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
636 double C2 = -1 * C1 * C1
637 * (m_avg * (-4 * eta * eta + 20 * eta + 8) / pow(1 - eta, 5.)
638 + (1 - m_avg) * (2 * pow(eta, 3) + 12 * eta * eta - 48 * eta + 40) / pow((1 - eta) * (2 - eta), 3));
639 double dC1_dt = C2 * dzeta_dt[3];
642 for (
int i = 0; i < ncomp; i++) {
646 double dadt_hc = m_avg * dadt_hs - summ;
647 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;
650 double dadt_polar = 0.;
656 vector<double> dipmSQ(ncomp, 0);
658 static double a0dip[5] = {0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308};
659 static double a1dip[5] = {0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135};
660 static double a2dip[5] = {-1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575};
661 static double b0dip[5] = {0.2187939, -1.1896431, 1.1626889, 0, 0};
662 static double b1dip[5] = {-0.5873164, 1.2489132, -0.5085280, 0, 0};
663 static double b2dip[5] = {3.4869576, -14.915974, 15.372022, 0, 0};
664 static double c0dip[5] = {-0.0646774, 0.1975882, -0.8087562, 0.6902849, 0};
665 static double c1dip[5] = {-0.9520876, 2.9924258, -2.3802636, -0.2701261, 0};
666 static double c2dip[5] = {-0.6260979, 1.2924686, 1.6542783, -3.4396744, 0};
668 const static double conv = 7242.702976750923;
670 for (
int i = 0; i < ncomp; i++) {
674 vector<double> adip(5, 0);
675 vector<double> bdip(5, 0);
676 vector<double> cdip(5, 0);
677 double J2, J3, dJ2_dt, dJ3_dt;
680 for (
int i = 0; i < ncomp; i++) {
681 for (
int j = 0; j < ncomp; j++) {
688 for (
int l = 0; l < 5; l++) {
689 adip[l] = a0dip[l] + (m_ij - 1) / m_ij * a1dip[l] + (m_ij - 1) / m_ij * (m_ij - 2) / m_ij * a2dip[l];
690 bdip[l] = b0dip[l] + (m_ij - 1) / m_ij * b1dip[l] + (m_ij - 1) / m_ij * (m_ij - 2) / m_ij * b2dip[l];
691 J2 += (adip[l] + bdip[l] * e_ij[i * ncomp + j] /
_T) * pow(eta, l);
692 dJ2_dt += adip[l] * l * pow(eta, l - 1) * dzeta_dt[3]
693 + bdip[l] * e_ij[j * ncomp + j] * (1 /
_T * l * pow(eta, l - 1) * dzeta_dt[3]
694 - 1 / pow(
_T, 2.) * pow(eta, l));
697 * pow(s_ij[j * ncomp + j], 3) / pow(s_ij[i * ncomp + j], 3) *
components[i].getDipnum() *
components[j].getDipnum() * dipmSQ[i]
700 * pow(s_ij[j * ncomp + j], 3) / pow(s_ij[i * ncomp + j], 3) *
components[i].getDipnum() *
components[j].getDipnum()
701 * dipmSQ[i] * dipmSQ[j] * (dJ2_dt / pow(
_T, 2) - 2 * J2 / pow(
_T, 3));
703 for (
int k = 0; k < ncomp; k++) {
710 for (
int l = 0; l < 5; l++) {
711 cdip[l] = c0dip[l] + (m_ijk - 1) / m_ijk * c1dip[l] + (m_ijk - 1) / m_ijk * (m_ijk - 2) / m_ijk * c2dip[l];
712 J3 += cdip[l] * pow(eta, l);
713 dJ3_dt += cdip[l] * l * pow(eta, l - 1) * dzeta_dt[3];
716 * 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)
717 / s_ij[i * ncomp + j] / s_ij[i * ncomp + k] / s_ij[j * ncomp + k] *
components[i].getDipnum() *
components[j].getDipnum()
718 *
components[k].getDipnum() * dipmSQ[i] * dipmSQ[j] * dipmSQ[k] * J3;
720 * 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)
721 / s_ij[i * ncomp + j] / s_ij[i * ncomp + k] / s_ij[j * ncomp + k] *
components[i].getDipnum()
723 * (-3 * J3 / pow(
_T, 4) + dJ3_dt / pow(
_T, 3));
729 A3 = -4 / 3. * PI * PI * den * den * A3;
730 dA2_dt = -PI * den * dA2_dt;
731 dA3_dt = -4 / 3. * PI * PI * den * den * dA3_dt;
734 dadt_polar = (dA2_dt - 2 * A3 / A2 * dA2_dt + dA3_dt) / pow(1 - A3 / A2, 2.);
739 double dadt_assoc = 0.;
745 for (
int i = 0; i < *it; i++) {
750 vector<double> x_assoc(num_sites);
751 for (
int i = 0; i < num_sites; i++) {
756 vector<double> XA (num_sites, 0);
757 vector<double> delta_ij(num_sites * num_sites, 0);
758 vector<double> ddelta_dt(num_sites * num_sites, 0);
762 for (
int i = 0; i < num_sites; i++) {
763 idxi = iA[i]*ncomp+iA[i];
764 for (
int j = 0; j < num_sites; j++) {
765 idxj = iA[j]*ncomp+iA[j];
768 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
769 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
770 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
771 ddelta_dt[idxa] = pow(s_ij[idxj],3)*volABij*(-eABij/pow(
_T,2)
772 *exp(eABij/
_T)*ghs[iA[i]*ncomp+iA[j]] + dghs_dt[iA[i]*ncomp+iA[j]]
777 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
778 if (!std::isfinite(XA[i])) {
785 vector<double> XA_old = XA;
786 while ((ctr < 100) && (dif > 1e-15)) {
788 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
790 for (
int i = 0; i < num_sites; i++) {
791 dif += abs(XA[i] - XA_old[i]);
793 for (
int i = 0; i < num_sites; i++) {
794 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
798 vector<double> dXA_dt(num_sites, 0);
799 dXA_dt =
dXAdt_find(delta_ij, den, XA, ddelta_dt, x_assoc);
801 for (
int i = 0; i < num_sites; i++) {
807 double dadt_ion = 0.;
809 vector<double> q(ncomp);
810 for (
int i = 0; i < ncomp; i++) {
815 for (
int i = 0; i < ncomp; i++) {
819 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
823 vector<double> chi(ncomp);
824 vector<double> dchikap_dk(ncomp);
826 for (
int i = 0; i < ncomp; i++) {
827 chi[i] = 3 / pow(kappa *
components[i].getSigma(), 3)
829 + 0.5 * pow(1 + kappa *
components[i].getSigma(), 2));
830 dchikap_dk[i] = -2 * chi[i] + 3 / (1 + kappa *
components[i].getSigma());
833 dkappa_dt = -0.5 * den * E_CHRG * E_CHRG / kb /
_T /
_T / (
dielc * perm_vac) * summ / kappa;
836 for (
int i = 0; i < ncomp; i++) {
837 summ +=
mole_fractions[i] * q[i] * q[i] * (dchikap_dk[i] * dkappa_dt /
_T - kappa * chi[i] /
_T /
_T);
839 dadt_ion = -1 / 12. / PI / kb / (
dielc * perm_vac) * summ;
843 double dadt = dadt_hc + dadt_disp + dadt_assoc + dadt_polar + dadt_ion;
865 vector<double> d(ncomp);
866 for (
int i = 0; i < ncomp; i++) {
870 for (
int i = 0; i < ncomp; i++) {
880 vector<double> zeta(4, 0);
882 for (
int i = 0; i < 4; i++) {
884 for (
int j = 0; j < ncomp; j++) {
887 zeta[i] = PI / 6 * den * summ;
890 double eta = zeta[3];
892 for (
int i = 0; i < ncomp; i++) {
896 vector<double> ghs(ncomp * ncomp, 0);
897 vector<double> denghs(ncomp * ncomp, 0);
898 vector<double> e_ij(ncomp * ncomp, 0);
899 vector<double> s_ij(ncomp * ncomp, 0);
903 for (
int i = 0; i < ncomp; i++) {
904 for (
int j = 0; j < ncomp; j++) {
926 ghs[idx] = 1/(1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
927 pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
928 denghs[idx] = zeta[3]/(1-zeta[3])/(1-zeta[3]) +
929 (d[i]*d[j]/(d[i]+d[j]))*(3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
930 6*zeta[2]*zeta[3]/pow(1-zeta[3], 3)) +
931 pow(d[i]*d[j]/(d[i]+d[j]), 2)*(4*zeta[2]*zeta[2]/pow(1-zeta[3], 3) +
932 6*zeta[2]*zeta[2]*zeta[3]/pow(1-zeta[3], 4));
936 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))
937 + (pow(zeta[2], 3.)/pow(zeta[3], 2.) - zeta[0])*log(1-zeta[3]));
938 double Zhs = zeta[3]/(1-zeta[3]) + 3.*zeta[1]*zeta[2]/zeta[0]/(1.-zeta[3])/(1.-zeta[3]) +
939 (3.*pow(zeta[2], 3.) - zeta[3]*pow(zeta[2], 3.))/zeta[0]/pow(1.-zeta[3], 3.);
941 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
942 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
943 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
944 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
945 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
946 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
948 vector<double> a(7, 0);
949 vector<double> b(7, 0);
950 for (
int i = 0; i < 7; i++) {
951 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
952 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
955 double detI1_det = 0.0;
956 double detI2_det = 0.0;
959 for (
int i = 0; i < 7; i++) {
960 detI1_det += a[i] * (i + 1) * pow(eta, i);
961 detI2_det += b[i] * (i + 1) * pow(eta, i);
962 I2 += b[i] * pow(eta, i);
963 I1 += a[i] * pow(eta, i);
966 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
967 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
968 double C2 = -1. * C1 * C1
969 * (m_avg * (-4 * eta * eta + 20 * eta + 8) / pow(1 - eta, 5)
970 + (1 - m_avg) * (2 * pow(eta, 3) + 12 * eta * eta - 48 * eta + 40) / pow((1 - eta) * (2 - eta), 3.0));
973 for (
int i = 0; i < ncomp; i++) {
977 double ares_hc = m_avg * ares_hs - summ;
978 double ares_disp = -2 * PI * den * I1 * m2es3 - PI * den * m_avg * C1 * I2 * m2e2s3;
981 for (
int i = 0; i < ncomp; i++) {
985 double Zhc = m_avg * Zhs - summ;
986 double Zdisp = -2 * PI * den * detI1_det * m2es3 - PI * den * m_avg * (C1 * detI2_det + C2 * eta * I2) * m2e2s3;
988 vector<double> dghsii_dx(ncomp * ncomp, 0);
989 vector<double> dahs_dx(ncomp, 0);
990 vector<double> dzeta_dx(4, 0);
992 for (
int i = 0; i < ncomp; i++) {
993 for (
int l = 0; l < 4; l++) {
994 dzeta_dx[l] = PI / 6. * den *
components[i].getM() * pow(d[i], l);
996 for (
int j = 0; j < ncomp; j++) {
999 dzeta_dx[3] / (1 - zeta[3]) / (1 - zeta[3])
1000 + (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))
1001 + pow(d[j] * d[j] / (d[j] + d[j]), 2)
1002 * (4 * zeta[2] * dzeta_dx[2] / pow(1 - zeta[3], 3) + 6 * zeta[2] * zeta[2] * dzeta_dx[3] / pow(1 - zeta[3], 4));
1005 -dzeta_dx[0] / zeta[0] * ares_hs
1007 * (3 * (dzeta_dx[1] * zeta[2] + zeta[1] * dzeta_dx[2]) / (1 - zeta[3])
1008 + 3 * zeta[1] * zeta[2] * dzeta_dx[3] / (1 - zeta[3]) / (1 - zeta[3])
1009 + 3 * zeta[2] * zeta[2] * dzeta_dx[2] / zeta[3] / (1 - zeta[3]) / (1 - zeta[3])
1010 + pow(zeta[2], 3) * dzeta_dx[3] * (3 * zeta[3] - 1) / zeta[3] / zeta[3] / pow(1 - zeta[3], 3)
1012 * ((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])
1013 + (zeta[0] - pow(zeta[2], 3) / zeta[3] / zeta[3]) * dzeta_dx[3] / (1 - zeta[3]));
1016 vector<double> dadisp_dx(ncomp, 0);
1017 vector<double> dahc_dx(ncomp, 0);
1018 double dzeta3_dx, daa_dx, db_dx, dI1_dx, dI2_dx, dm2es3_dx, dm2e2s3_dx, dC1_dx;
1019 for (
int i = 0; i < ncomp; i++) {
1020 dzeta3_dx = PI / 6. * den *
components[i].getM() * pow(d[i], 3);
1025 for (
int l = 0; l < 7; l++) {
1026 daa_dx =
components[i].getM() / m_avg / m_avg * a1[l] +
components[i].getM() / m_avg / m_avg * (3 - 4 / m_avg) * a2[l];
1027 db_dx =
components[i].getM() / m_avg / m_avg * b1[l] +
components[i].getM() / m_avg / m_avg * (3 - 4 / m_avg) * b2[l];
1028 dI1_dx += a[l] * l * dzeta3_dx * pow(eta, l - 1) + daa_dx * pow(eta, l);
1029 dI2_dx += b[l] * l * dzeta3_dx * pow(eta, l - 1) + db_dx * pow(eta, l);
1031 for (
int j = 0; j < ncomp; j++) {
1036 dm2es3_dx = dm2es3_dx * 2 *
components[i].getM();
1037 dm2e2s3_dx = dm2e2s3_dx * 2 *
components[i].getM();
1038 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]);
1039 dC1_dx = C2 * dzeta3_dx
1041 * (
components[i].getM() * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
1042 -
components[i].getM() * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2));
1045 -2 * PI * den * (dI1_dx * m2es3 + I1 * dm2es3_dx)
1046 - PI * den * ((
components[i].getM() * C1 * I2 + m_avg * dC1_dx * I2 + m_avg * C1 * dI2_dx) * m2e2s3 + m_avg * C1 * I2 * dm2e2s3_dx);
1049 vector<double> mu_hc(ncomp, 0);
1050 vector<double> mu_disp(ncomp, 0);
1051 for (
int i = 0; i < ncomp; i++) {
1052 for (
int j = 0; j < ncomp; j++) {
1056 mu_hc[i] = ares_hc + Zhc + dahc_dx[i] - mu_hc[i];
1057 mu_disp[i] = ares_disp + Zdisp + dadisp_dx[i] - mu_disp[i];
1061 vector<double> mu_polar(ncomp, 0);
1065 double dA2_det = 0.;
1066 double dA3_det = 0.;
1067 vector<double> dA2_dx(ncomp, 0);
1068 vector<double> dA3_dx(ncomp, 0);
1070 static double a0dip[5] = { 0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308 };
1071 static double a1dip[5] = { 0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135 };
1072 static double a2dip[5] = { -1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575 };
1073 static double b0dip[5] = { 0.2187939, -1.1896431, 1.1626889, 0, 0 };
1074 static double b1dip[5] = { -0.5873164, 1.2489132, -0.5085280, 0, 0 };
1075 static double b2dip[5] = { 3.4869576, -14.915974, 15.372022, 0, 0 };
1076 static double c0dip[5] = { -0.0646774, 0.1975882, -0.8087562, 0.6902849, 0 };
1077 static double c1dip[5] = { -0.9520876, 2.9924258, -2.3802636, -0.2701261, 0 };
1078 static double c2dip[5] = { -0.6260979, 1.2924686, 1.6542783, -3.4396744, 0 };
1080 const static double conv = 7242.702976750923;
1082 vector<double> dipmSQ (ncomp, 0);
1083 for (
int i = 0; i < ncomp; i++) {
1087 vector<double> adip (5, 0);
1088 vector<double> bdip (5, 0);
1089 vector<double> cdip (5, 0);
1090 double J2, dJ2_det, detJ2_det, J3, dJ3_det, detJ3_det;
1093 for (
int i = 0; i < ncomp; i++) {
1094 for (
int j = 0; j < ncomp; j++) {
1102 for (
int l = 0; l < 5; l++) {
1103 adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
1104 bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
1105 J2 += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*pow(eta, l);
1106 dJ2_det += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*l*pow(eta, l-1);
1107 detJ2_det += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*(l+1)*pow(eta, l);
1110 pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*J2;
1112 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;
1114 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)
1115 /pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*
1119 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)
1120 /pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*
1124 for (
int k = 0; k < ncomp; k++) {
1132 for (
int l = 0; l < 5; l++) {
1133 cdip[l] = c0dip[l] + (m_ijk-1)/m_ijk*c1dip[l] + (m_ijk-1)/m_ijk*(m_ijk-2)/m_ijk*c2dip[l];
1134 J3 += cdip[l]*pow(eta, l);
1135 dJ3_det += cdip[l]*l*pow(eta, (l-1));
1136 detJ3_det += cdip[l]*(l+2)*pow(eta, (l+1));
1139 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]/
1141 dipmSQ[j]*dipmSQ[k]*J3;
1143 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]/
1145 dipmSQ[j]*dipmSQ[k]*detJ3_det;
1146 if ((i == j) && (i == k)) {
1147 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)
1148 *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]
1153 else if ((i == j) || (i == k)) {
1154 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)
1155 *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]
1161 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)
1162 *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]
1172 A3 = -4/3.*PI*PI*den*den*A3;
1173 dA2_det = -PI*den/eta*dA2_det;
1174 dA3_det = -4/3.*PI*PI*den/eta*den/eta*dA3_det;
1175 for (
int i = 0; i < ncomp; i++) {
1176 dA2_dx[i] = -PI*den*dA2_dx[i];
1177 dA3_dx[i] = -4/3.*PI*PI*den*den*dA3_dx[i];
1180 vector<double> dapolar_dx(ncomp);
1181 for (
int i = 0; i < ncomp; i++) {
1182 dapolar_dx[i] = (dA2_dx[i]*(1-A3/A2) + (dA3_dx[i]*A2 - A3*dA2_dx[i])/A2)/pow(1-A3/A2,2);
1186 double ares_polar = A2/(1-A3/A2);
1187 double Zpolar = eta*((dA2_det*(1-A3/A2)+(dA3_det*A2-A3*dA2_det)/A2)/(1-A3/A2)/(1-A3/A2));
1188 for (
int i = 0; i < ncomp; i++) {
1189 for (
int j = 0; j < ncomp; j++) {
1192 mu_polar[i] = ares_polar + Zpolar + dapolar_dx[i] - mu_polar[i];
1198 vector<double> mu_assoc(ncomp, 0);
1202 for(std::vector<int>::iterator it =
assoc_num.begin(); it !=
assoc_num.end(); ++it) {
1204 for (
int i = 0; i < *it; i++) {
1209 vector<double> x_assoc(num_sites);
1210 for (
int i = 0; i < num_sites; i++) {
1215 vector<double> XA (num_sites, 0);
1216 vector<double> delta_ij(num_sites * num_sites, 0);
1220 for (
int i = 0; i < num_sites; i++) {
1221 idxi = iA[i]*ncomp+iA[i];
1222 for (
int j = 0; j < num_sites; j++) {
1223 idxj = iA[j]*ncomp+iA[j];
1226 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1227 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1228 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1232 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
1233 if (!std::isfinite(XA[i])) {
1238 vector<double> ddelta_dx(num_sites * num_sites * ncomp, 0);
1240 for (
int k = 0; k < ncomp; k++) {
1244 for (
int i = 0; i < num_sites; i++) {
1245 idxi = iA[i]*ncomp+iA[i];
1246 for (
int j = 0; j < num_sites; j++) {
1247 idxj = iA[j]*ncomp+iA[j];
1250 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1251 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1252 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]]/
1253 (d[iA[i]]+d[iA[j]])*(d[k]*d[k]/(1-zeta[3])/(1-zeta[3])+2*pow(d[k], 3)*
1254 zeta[2]/pow(1-zeta[3], 3)) + 2*pow((d[iA[i]]*d[iA[j]]/(d[iA[i]]+d[iA[j]])), 2)*
1255 (2*d[k]*d[k]*zeta[2]/pow(1-zeta[3], 3)+3*(pow(d[k], 3)*zeta[2]*zeta[2]
1256 /pow(1-zeta[3], 4))));
1257 ddelta_dx[idx_ddelta] = dghsd_dx*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1267 vector<double> XA_old = XA;
1268 while ((ctr < 100) && (dif > 1e-15)) {
1270 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
1272 for (
int i = 0; i < num_sites; i++) {
1273 dif += abs(XA[i] - XA_old[i]);
1275 for (
int i = 0; i < num_sites; i++) {
1276 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
1280 vector<double> dXA_dx(num_sites*ncomp, 0);
1284 for (
int i = 0; i < ncomp; i++) {
1285 for (
int j = 0; j < num_sites; j++) {
1286 mu_assoc[i] +=
mole_fractions[iA[j]]*den*dXA_dx[ij]*(1/XA[j]-0.5);
1291 for (
int i = 0; i < num_sites; i++) {
1292 mu_assoc[iA[i]] += log(XA[i]) - 0.5*XA[i] + 0.5;
1297 vector<double> mu_ion(ncomp, 0);
1299 vector<double> q(ncomp);
1300 for (
int i = 0; i < ncomp; i++) {
1305 for (
int i = 0; i < ncomp; i++) {
1309 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
1312 vector<double> chi(ncomp);
1313 vector<double> sigma_k(ncomp);
1316 for (
int i = 0; i < ncomp; i++) {
1317 chi[i] = 3 / pow(kappa *
components[i].getSigma(), 3)
1318 * (1.5 + log(1 + kappa *
components[i].getSigma()) - 2 * (1 + kappa *
components[i].getSigma())
1319 + 0.5 * pow(1 + kappa *
components[i].getSigma(), 2));
1320 sigma_k[i] = -2 * chi[i] + 3 / (1 + kappa *
components[i].getSigma());
1325 for (
int i = 0; i < ncomp; i++) {
1326 mu_ion[i] = -q[i] * q[i] * kappa / 24. / PI / kb /
_T / (
dielc * perm_vac) * (2 * chi[i] + summ1 / summ2);
1333 vector<double> mu(ncomp, 0);
1334 vector<CoolPropDbl> fugcoef(ncomp, 0);
1335 for (
int i = 0; i < ncomp; i++) {
1336 mu[i] = mu_hc[i] + mu_disp[i] + mu_polar[i] + mu_assoc[i] + mu_ion[i];
1337 fugcoef[i] = exp(mu[i] - log(Z));
1347 CoolPropDbl gres = (ares + (Z - 1) - log(Z)) * kb * N_AV *
_T;
1353 vector<double> d(ncomp);
1354 for (
int i = 0; i < ncomp; i++) {
1358 for (
int i = 0; i < ncomp; i++) {
1368 vector<double> zeta(4, 0);
1370 for (
int i = 0; i < 4; i++) {
1372 for (
int j = 0; j < ncomp; j++) {
1375 zeta[i] = PI / 6 * den * summ;
1378 double eta = zeta[3];
1380 for (
int i = 0; i < ncomp; i++) {
1384 vector<double> ghs (ncomp*ncomp, 0);
1385 vector<double> denghs (ncomp*ncomp, 0);
1386 vector<double> e_ij (ncomp*ncomp, 0);
1387 vector<double> s_ij (ncomp*ncomp, 0);
1391 for (
int i = 0; i < ncomp; i++) {
1392 for (
int j = 0; j < ncomp; j++) {
1413 ghs[idx] = 1/(1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
1414 pow(d[i]*d[j]/(d[i]+d[j]), 2)*2*zeta[2]*zeta[2]/pow(1-zeta[3], 3);
1415 denghs[idx] = zeta[3]/(1-zeta[3])/(1-zeta[3]) +
1416 (d[i]*d[j]/(d[i]+d[j]))*(3*zeta[2]/(1-zeta[3])/(1-zeta[3]) +
1417 6*zeta[2]*zeta[3]/pow(1-zeta[3], 3)) +
1418 pow(d[i]*d[j]/(d[i]+d[j]), 2)*(4*zeta[2]*zeta[2]/pow(1-zeta[3], 3) +
1419 6*zeta[2]*zeta[2]*zeta[3]/pow(1-zeta[3], 4));
1423 double Zhs = zeta[3] / (1 - zeta[3]) + 3. * zeta[1] * zeta[2] / zeta[0] / (1. - zeta[3]) / (1. - zeta[3])
1424 + (3. * pow(zeta[2], 3.) - zeta[3] * pow(zeta[2], 3.)) / zeta[0] / pow(1. - zeta[3], 3.);
1426 static double a0[7] = { 0.9105631445, 0.6361281449, 2.6861347891, -26.547362491, 97.759208784, -159.59154087, 91.297774084 };
1427 static double a1[7] = { -0.3084016918, 0.1860531159, -2.5030047259, 21.419793629, -65.255885330, 83.318680481, -33.746922930 };
1428 static double a2[7] = { -0.0906148351, 0.4527842806, 0.5962700728, -1.7241829131, -4.1302112531, 13.776631870, -8.6728470368 };
1429 static double b0[7] = { 0.7240946941, 2.2382791861, -4.0025849485, -21.003576815, 26.855641363, 206.55133841, -355.60235612 };
1430 static double b1[7] = { -0.5755498075, 0.6995095521, 3.8925673390, -17.215471648, 192.67226447, -161.82646165, -165.20769346 };
1431 static double b2[7] = { 0.0976883116, -0.2557574982, -9.1558561530, 20.642075974, -38.804430052, 93.626774077, -29.666905585 };
1433 vector<double> a(7, 0);
1434 vector<double> b(7, 0);
1435 for (
int i = 0; i < 7; i++) {
1436 a[i] = a0[i] + (m_avg - 1.) / m_avg * a1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * a2[i];
1437 b[i] = b0[i] + (m_avg - 1.) / m_avg * b1[i] + (m_avg - 1.) / m_avg * (m_avg - 2.) / m_avg * b2[i];
1440 double detI1_det = 0.0;
1441 double detI2_det = 0.0;
1443 for (
int i = 0; i < 7; i++) {
1444 detI1_det += a[i] * (i + 1) * pow(eta, i);
1445 detI2_det += b[i] * (i + 1) * pow(eta, i);
1446 I2 += b[i] * pow(eta, i);
1449 / (1. + m_avg * (8 * eta - 2 * eta * eta) / pow(1 - eta, 4)
1450 + (1 - m_avg) * (20 * eta - 27 * eta * eta + 12 * pow(eta, 3) - 2 * pow(eta, 4)) / pow((1 - eta) * (2 - eta), 2.0));
1451 double C2 = -1. * C1 * C1
1452 * (m_avg * (-4 * eta * eta + 20 * eta + 8) / pow(1 - eta, 5)
1453 + (1 - m_avg) * (2 * pow(eta, 3) + 12 * eta * eta - 48 * eta + 40) / pow((1 - eta) * (2 - eta), 3.0));
1456 for (
int i = 0; i < ncomp; i++) {
1461 double Zhc = m_avg * Zhs - summ;
1462 double Zdisp = -2 * PI * den * detI1_det * m2es3 - PI * den * m_avg * (C1 * detI2_det + C2 * eta * I2) * m2e2s3;
1469 double dA2_det = 0.;
1470 double dA3_det = 0.;
1471 vector<double> adip(5, 0);
1472 vector<double> bdip(5, 0);
1473 vector<double> cdip(5, 0);
1474 vector<double> dipmSQ(ncomp, 0);
1475 double J2, detJ2_det, J3, detJ3_det;
1477 static double a0dip[5] = {0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308};
1478 static double a1dip[5] = {0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135};
1479 static double a2dip[5] = {-1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575};
1480 static double b0dip[5] = {0.2187939, -1.1896431, 1.1626889, 0, 0};
1481 static double b1dip[5] = {-0.5873164, 1.2489132, -0.5085280, 0, 0};
1482 static double b2dip[5] = {3.4869576, -14.915974, 15.372022, 0, 0};
1483 static double c0dip[5] = {-0.0646774, 0.1975882, -0.8087562, 0.6902849, 0};
1484 static double c1dip[5] = {-0.9520876, 2.9924258, -2.3802636, -0.2701261, 0};
1485 static double c2dip[5] = {-0.6260979, 1.2924686, 1.6542783, -3.4396744, 0};
1487 const static double conv = 7242.702976750923;
1489 for (
int i = 0; i < ncomp; i++) {
1494 for (
int i = 0; i < ncomp; i++) {
1495 for (
int j = 0; j < ncomp; j++) {
1502 for (
int l = 0; l < 5; l++) {
1503 adip[l] = a0dip[l] + (m_ij-1)/m_ij*a1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip[l];
1504 bdip[l] = b0dip[l] + (m_ij-1)/m_ij*b1dip[l] + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip[l];
1505 J2 += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*pow(eta, l);
1506 detJ2_det += (adip[l] + bdip[l]*e_ij[i*ncomp+j]/
_T)*(l+1)*pow(eta, l);
1509 pow(s_ij[i*ncomp+j],3)*
components[i].getDipnum()*
components[j].getDipnum()*dipmSQ[i]*dipmSQ[j]*J2;
1511 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;
1516 for (
int i = 0; i < ncomp; i++) {
1517 for (
int j = 0; j < ncomp; j++) {
1518 for (
int k = 0; k < ncomp; k++) {
1525 for (
int l = 0; l < 5; l++) {
1526 cdip[l] = c0dip[l] + (m_ijk-1)/m_ijk*c1dip[l] + (m_ijk-1)/m_ijk*(m_ijk-2)/m_ijk*c2dip[l];
1527 J3 += cdip[l]*pow(eta, l);
1528 detJ3_det += cdip[l]*(l+2)*pow(eta, (l+1));
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]*J3;
1535 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]/
1537 dipmSQ[j]*dipmSQ[k]*detJ3_det;
1543 A3 = -4/3.*PI*PI*den*den*A3;
1544 dA2_det = -PI*den/eta*dA2_det;
1545 dA3_det = -4/3.*PI*PI*den/eta*den/eta*dA3_det;
1548 Zpolar = eta*((dA2_det*(1-A3/A2)+(dA3_det*A2-A3*dA2_det)/A2)/(1-A3/A2)/(1-A3/A2));
1557 for(std::vector<int>::iterator it =
assoc_num.begin(); it !=
assoc_num.end(); ++it) {
1559 for (
int i = 0; i < *it; i++) {
1564 vector<double> x_assoc(num_sites);
1565 for (
int i = 0; i < num_sites; i++) {
1570 vector<double> XA (num_sites, 0);
1571 vector<double> delta_ij(num_sites * num_sites, 0);
1575 for (
int i = 0; i < num_sites; i++) {
1576 idxi = iA[i]*ncomp+iA[i];
1577 for (
int j = 0; j < num_sites; j++) {
1578 idxj = iA[j]*ncomp+iA[j];
1581 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1582 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1583 delta_ij[idxa] = ghs[iA[i]*ncomp+iA[j]]*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1587 XA[i] = (-1 + sqrt(1+8*den*delta_ij[i*num_sites+i]))/(4*den*delta_ij[i*num_sites+i]);
1588 if (!std::isfinite(XA[i])) {
1593 vector<double> ddelta_dx(num_sites * num_sites * ncomp, 0);
1595 for (
int k = 0; k < ncomp; k++) {
1599 for (
int i = 0; i < num_sites; i++) {
1600 idxi = iA[i]*ncomp+iA[i];
1601 for (
int j = 0; j < num_sites; j++) {
1602 idxj = iA[j]*ncomp+iA[j];
1605 double volABij = sqrt(
components[iA[i]].getVolA()*
components[iA[j]].getVolA())*pow(sqrt(s_ij[idxi]*
1606 s_ij[idxj])/(0.5*(s_ij[idxi]+s_ij[idxj])), 3);
1607 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]]/
1608 (d[iA[i]]+d[iA[j]])*(d[k]*d[k]/(1-zeta[3])/(1-zeta[3])+2*pow(d[k], 3)*
1609 zeta[2]/pow(1-zeta[3], 3)) + 2*pow((d[iA[i]]*d[iA[j]]/(d[iA[i]]+d[iA[j]])), 2)*
1610 (2*d[k]*d[k]*zeta[2]/pow(1-zeta[3], 3)+3*(pow(d[k], 3)*zeta[2]*zeta[2]
1611 /pow(1-zeta[3], 4))));
1612 ddelta_dx[idx_ddelta] = dghsd_dx*(exp(eABij/
_T)-1)*pow(s_ij[iA[i]*ncomp+iA[j]], 3)*volABij;
1622 vector<double> XA_old = XA;
1623 while ((ctr < 100) && (dif > 1e-14)) {
1625 XA =
XA_find(XA_old, delta_ij, den, x_assoc);
1627 for (
int i = 0; i < num_sites; i++) {
1628 dif += abs(XA[i] - XA_old[i]);
1630 for (
int i = 0; i < num_sites; i++) {
1631 XA_old[i] = (XA[i] + XA_old[i]) / 2.0;
1635 vector<double> dXA_dx(num_sites*ncomp, 0);
1640 for (
int i = 0; i < ncomp; i++) {
1641 for (
int j = 0; j < num_sites; j++) {
1653 vector<double> q(ncomp);
1654 for (
int i = 0; i < ncomp; i++) {
1659 for (
int i = 0; i < ncomp; i++) {
1664 sqrt(den * E_CHRG * E_CHRG / kb /
_T / (
dielc * perm_vac) * summ);
1667 double chi, sigma_k;
1669 for (
int i = 0; i < ncomp; i++) {
1670 chi = 3 / pow(kappa *
components[i].getSigma(), 3)
1671 * (1.5 + log(1 + kappa *
components[i].getSigma()) - 2 * (1 + kappa *
components[i].getSigma())
1672 + 0.5 * pow(1 + kappa *
components[i].getSigma(), 2));
1673 sigma_k = -2 * chi + 3 / (1 + kappa *
components[i].getSigma());
1676 Zion = -1 * kappa / 24. / PI / kb /
_T / (
dielc * perm_vac) * summ;
1680 double Z = Zid + Zhc + Zdisp + Zpolar + Zassoc + Zion;
1696 throw ValueError(
"rhomolar is less than zero");
1699 throw ValueError(
"rhomolar is not a valid number");
1702 if (optional_checks) {
1714 std::cout <<
format(
"%s (%d): update called with (%d: (%s), %g, %g)", __FILE__, __LINE__, input_pair,
1720 CoolPropDbl ld_value1 = value1, ld_value2 = value2;
1728 throw ValueError(
"Mole fractions must be set");
1731 if (
SatL->mole_fractions.empty()) {
1734 if (
SatV->mole_fractions.empty()) {
1737 for (
int i = 0; i <
N; i++) {
1738 if (
SatV->components[i].getZ() != 0) {
1739 SatV->mole_fractions[i] = 0;
1742 summ +=
SatV->mole_fractions[i];
1745 for (
int i = 0; i <
N; i++) {
1746 SatV->mole_fractions[i] =
SatV->mole_fractions[i] / summ;
1753 switch (input_pair) {
1799 if ((
_Q < 0) || (
_Q > 1)) {
1806 SatL->_rhomolar = value1;
SatV->_rhomolar = value1;
1807 SatL->_T = value2;
SatV->_T = value2;
1857 double p_input, rho_input;
1858 double p_bub, p_dew, p_equil;
1868 if (p_input > 1.6 * p_equil) {
1871 else if (p_input < 0.5 * p_equil) {
1891 else if (
_p == p_bub) {
1903 else if ((
_p <= p_bub) && (
_p >= p_dew)) {
1913 double rho_bub, rho_dew;
1964 bool solution_found =
false;
1970 solution_found =
true;
1976 if (!solution_found) {
1977 double p_lbound = -6;
1978 double p_ubound = 9;
1979 double p_step = 0.1;
1981 while (p_guess < p_ubound && !solution_found) {
1983 p =
outerTQ(pow(10, p_guess), PCSAFT);
1984 solution_found =
true;
1993 if (!solution_found) {
1994 throw SolutionError(
"solution could not be found for TQ flash");
2005 bool solution_found =
false;
2011 solution_found =
true;
2017 if (!solution_found) {
2018 double t_lbound = 1;
2019 double t_ubound = 800;
2026 while (t_guess > t_lbound && !solution_found) {
2029 solution_found =
true;
2038 if (!solution_found) {
2039 throw SolutionError(
"solution could not be found for PQ flash");
2053 double MAXITER = 200;
2061 vector<CoolPropDbl> u;
2064 : PCSAFT(PCSAFT), kb0(kb0), u(u){}
2069 vector<double> pp(ncomp, 0);
2071 for (
int i = 0; i < ncomp; i++) {
2073 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * exp(u[i]));
2081 error = pow((L + PCSAFT.
_Q - 1), 2.);
2087 for (
int i = 0; i < ncomp; i++) {
2094 vector<double> k(ncomp, 0), u(ncomp, 0), kprime(ncomp, 0), uprime(ncomp, 0);
2095 double Tref = t_guess - 1;
2096 double Tprime = t_guess + 1;
2099 PCSAFT.
SatL->_T = t;
2100 PCSAFT.
SatV->_T = t;
2115 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2116 throw SolutionError(
"liquid and vapor densities are the same.");
2118 vector<double> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2119 vector<double> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2123 for (
int i = 0; i < ncomp; i++) {
2125 k[i] = fugcoef_l[i] / fugcoef_v[i];
2130 xl_sum += PCSAFT.
SatL->mole_fractions[i];
2131 PCSAFT.
SatV->mole_fractions[i] = k[i] * PCSAFT.
mole_fractions[i] / (1 + PCSAFT.
_Q * (k[i] - 1));
2132 xv_sum += PCSAFT.
SatV->mole_fractions[i];
2136 for (
int i = 0; i < ncomp; i++) {
2137 PCSAFT.
SatV->mole_fractions[i] = PCSAFT.
SatV->mole_fractions[i] / xv_sum;
2142 for (
int i = 0; i < ncomp; i++) {
2143 PCSAFT.
SatL->mole_fractions[i] = PCSAFT.
SatL->mole_fractions[i] / xl_sum;
2148 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2150 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2151 for (
int i = 0; i < ncomp; i++) {
2152 k[i] = fugcoef_l[i] / fugcoef_v[i];
2155 PCSAFT.
SatL->_T = Tprime;
2156 PCSAFT.
SatV->_T = Tprime;
2160 PCSAFT.
SatL->components[
water_idx].calc_water_sigma(Tprime);
2161 PCSAFT.
SatV->components[
water_idx].calc_water_sigma(Tprime);
2167 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2169 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2170 for (
int i = 0; i < ncomp; i++) {
2171 kprime[i] = fugcoef_l[i] / fugcoef_v[i];
2174 vector<double> t_weight(ncomp);
2176 for (
int i = 0; i < ncomp; i++) {
2177 double dlnk_dt = (kprime[i] - k[i]) / (Tprime - t);
2178 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (k[i] - 1));
2179 t_sum += t_weight[i];
2183 for (
int i = 0; i < ncomp; i++) {
2184 double wi = t_weight[i] / t_sum;
2186 kb += wi * std::log(k[i]);
2192 for (
int i = 0; i < ncomp; i++) {
2193 double dlnk_dt = (kprime[i] - k[i]) / (Tprime - t);
2194 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (kprime[i] - 1));
2195 t_sum += t_weight[i];
2199 for (
int i = 0; i < ncomp; i++) {
2200 double wi = t_weight[i] / t_sum;
2202 kbprime += wi * std::log(kprime[i]);
2205 kbprime = std::exp(kbprime);
2206 double kb0 = kbprime;
2208 for (
int i = 0; i < ncomp; i++) {
2209 u[i] = std::log(k[i] / kb);
2210 uprime[i] = std::log(kprime[i] / kbprime);
2213 double B = std::log(kbprime / kb) / (1/Tprime - 1/t);
2214 double A = std::log(kb) - B * (1/t - 1/Tref);
2217 SolverInnerResid resid(*
this, kb0, u);
2219 vector<double> pp(ncomp, 0);
2220 double maxdif = 1e10 * TOL;
2222 double Rmin = 0, Rmax = 1;
2223 while (maxdif > TOL && itr < MAXITER) {
2225 vector<double> u_old = u;
2229 double R0 = kb * PCSAFT.
_Q / (kb * PCSAFT.
_Q + kb0 * (1 - PCSAFT.
_Q));
2231 if (resid.call(R) > TOL) {
2236 double eupp_sum = 0;
2237 for (
int i = 0; i < ncomp; i++) {
2238 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * std::exp(u[i]));
2241 eupp_sum += std::exp(u[i]) * pp[i];
2244 kb = pp_sum / eupp_sum;
2246 t = 1 / (1 / Tref + (std::log(kb) - A) / B);
2247 for (
int i = 0; i < ncomp; i++) {
2249 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum;
2250 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2253 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum * (1 - x_ions / (1 - PCSAFT.
_Q));
2254 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2258 PCSAFT.
SatV->mole_fractions[i] = 0;
2262 PCSAFT.
SatL->_T = t;
2263 PCSAFT.
SatV->_T = t;
2274 vector<CoolPropDbl> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2276 vector<CoolPropDbl> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2277 for (
int i = 0; i < ncomp; i++) {
2278 k[i] = fugcoef_l[i] / fugcoef_v[i];
2279 u[i] = std::log(k[i] / kb);
2283 B = std::log(kbprime / kb) / (1/Tprime - 1/t);
2288 A = std::log(kb) - B * (1/t - 1/Tref);
2290 maxdif = std::abs(A - A_old);
2291 for (
int i = 0; i < ncomp; i++) {
2293 double dif = std::abs(u[i] - u_old[i]);
2303 if (!std::isfinite(t) || maxdif > 1e-3 || t < 0) {
2304 throw SolutionError(
"outerPQ did not converge to a solution");
2315 double MAXITER = 200;
2323 vector<CoolPropDbl> u;
2326 : PCSAFT(PCSAFT), kb0(kb0), u(u){}
2331 vector<double> pp(ncomp, 0);
2334 for (
int i = 0; i < ncomp; i++) {
2336 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * exp(u[i]));
2344 error = pow((L + PCSAFT.
_Q - 1), 2.);
2350 for (
int i = 0; i < ncomp; i++) {
2357 vector<double> k(ncomp, 0), u(ncomp, 0), kprime(ncomp, 0), uprime(ncomp, 0);
2358 double Pref = p_guess - 0.01 * p_guess;
2359 double Pprime = p_guess + 0.01 * p_guess;
2360 if (p_guess > 1e6) {
2361 Pprime = p_guess - 0.005 * p_guess;
2368 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2369 throw SolutionError(
"liquid and vapor densities are the same.");
2371 vector<double> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2372 vector<double> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2376 for (
int i = 0; i < ncomp; i++) {
2378 k[i] = fugcoef_l[i] / fugcoef_v[i];
2383 xl_sum += PCSAFT.
SatL->mole_fractions[i];
2384 PCSAFT.
SatV->mole_fractions[i] = k[i] * PCSAFT.
mole_fractions[i] / (1 + PCSAFT.
_Q * (k[i] - 1));
2385 xv_sum += PCSAFT.
SatV->mole_fractions[i];
2389 for (
int i = 0; i < ncomp; i++) {
2390 PCSAFT.
SatV->mole_fractions[i] = PCSAFT.
SatV->mole_fractions[i] / xv_sum;
2395 for (
int i = 0; i < ncomp; i++) {
2396 PCSAFT.
SatL->mole_fractions[i] = PCSAFT.
SatL->mole_fractions[i] / xl_sum;
2401 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2403 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2404 for (
int i = 0; i < ncomp; i++) {
2405 k[i] = fugcoef_l[i] / fugcoef_v[i];
2406 u[i] = std::log(k[i] / kb);
2410 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2412 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2413 for (
int i = 0; i < ncomp; i++) {
2414 kprime[i] = fugcoef_l[i] / fugcoef_v[i];
2417 vector<double> t_weight(ncomp);
2419 for (
int i = 0; i < ncomp; i++) {
2420 double dlnk_dt = (kprime[i] - k[i]) / (Pprime -
p);
2421 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (k[i] - 1));
2422 t_sum += t_weight[i];
2426 for (
int i = 0; i < ncomp; i++) {
2427 double wi = t_weight[i] / t_sum;
2429 kb += wi * std::log(k[i]);
2435 for (
int i = 0; i < ncomp; i++) {
2436 double dlnk_dt = (kprime[i] - k[i]) / (Pprime -
p);
2437 t_weight[i] = PCSAFT.
SatV->mole_fractions[i] * dlnk_dt / (1 + PCSAFT.
_Q * (kprime[i] - 1));
2438 t_sum += t_weight[i];
2442 for (
int i = 0; i < ncomp; i++) {
2443 double wi = t_weight[i] / t_sum;
2445 kbprime += wi * std::log(kprime[i]);
2448 kbprime = std::exp(kbprime);
2449 double kb0 = kbprime;
2451 for (
int i = 0; i < ncomp; i++) {
2452 u[i] = std::log(k[i] / kb);
2453 uprime[i] = std::log(kprime[i] / kbprime);
2456 double B = std::log(kbprime / kb) / (1/Pprime - 1/
p);
2457 double A = std::log(kb) - B * (1/
p - 1/Pref);
2464 SolverInnerResid resid(*
this, kb0, u);
2466 vector<double> pp(ncomp, 0);
2467 double maxdif = 1e10 * TOL;
2469 double Rmin = 0, Rmax = 1;
2470 while (maxdif > TOL && itr < MAXITER) {
2472 vector<double> u_old = u;
2475 double R0 = kb * PCSAFT.
_Q / (kb * PCSAFT.
_Q + kb0 * (1 - PCSAFT.
_Q));
2478 if (resid.call(R) > TOL) {
2483 double eupp_sum = 0;
2484 for (
int i = 0; i < ncomp; i++) {
2485 pp[i] = PCSAFT.
mole_fractions[i] / (1 - R + kb0 * R * std::exp(u[i]));
2488 eupp_sum += std::exp(u[i]) * pp[i];
2491 kb = pp_sum / eupp_sum;
2493 p = 1 / (1 / Pref + (std::log(kb) - A) / B);
2494 for (
int i = 0; i < ncomp; i++) {
2496 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum;
2497 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2500 PCSAFT.
SatL->mole_fractions[i] = pp[i] / pp_sum * (1 - x_ions/(1 - PCSAFT.
_Q));
2501 PCSAFT.
SatV->mole_fractions[i] = std::exp(u[i]) * pp[i] / eupp_sum;
2505 PCSAFT.
SatV->mole_fractions[i] = 0;
2510 vector<CoolPropDbl> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2512 vector<CoolPropDbl> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2513 for (
int i = 0; i < ncomp; i++) {
2514 k[i] = fugcoef_l[i] / fugcoef_v[i];
2515 u[i] = std::log(k[i] / kb);
2519 B = std::log(kbprime / kb) / (1/Pprime - 1/
p);
2521 A = std::log(kb) - B * (1/
p - 1/Pref);
2523 maxdif = std::abs(A - A_old);
2524 for (
int i = 0; i < ncomp; i++) {
2526 double dif = std::abs(u[i] - u_old[i]);
2529 }
else if (!std::isfinite(dif)) {
2537 if (!std::isfinite(
p) || !std::isfinite(maxdif) || maxdif > 0.1 ||
p < 0) {
2538 throw SolutionError(
"outerTQ did not converge to a solution");
2548 double t_guess = _HUGE;
2552 for (
int i = 0; i < ncomp; i++) {
2558 bool guess_found =
false;
2560 double t_start = 571;
2561 double t_lbound = 1;
2567 while (!guess_found && t_start > t_lbound) {
2569 double Tprime = t_start - 50;
2572 PCSAFT.
SatL->_T = t;
2573 PCSAFT.
SatV->_T = t;
2587 PCSAFT.
SatL->_T = Tprime;
2588 PCSAFT.
SatV->_T = Tprime;
2590 PCSAFT.
SatL->_T = t;
2591 PCSAFT.
SatV->_T = t;
2593 double slope = (std::log10(p1) - std::log10(p2)) / (1/t - 1/Tprime);
2594 double intercept = std::log10(p1) - slope * (1/t);
2595 t_guess = slope / (std::log10(PCSAFT.
_p) - intercept);
2603 throw SolutionError(
"an estimate for the VLE temperature could not be found");
2614 double p_guess = _HUGE;
2618 for (
int i = 0; i < ncomp; i++) {
2624 bool guess_found =
false;
2625 double p_start = 10000;
2626 while (!guess_found && p_start < 1e7) {
2628 vector<double> k(ncomp, 0), u(ncomp, 0), kprime(ncomp, 0), uprime(ncomp, 0);
2629 double Pprime = 0.99 * p_start;
2635 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2636 p_start = p_start + 2e5;
2639 vector<double> fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2640 vector<double> fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2645 for (
int i = 0; i < ncomp; i++) {
2647 k[i] = fugcoef_l[i] / fugcoef_v[i];
2652 xl_sum += PCSAFT.
SatL->mole_fractions[i];
2653 PCSAFT.
SatV->mole_fractions[i] = k[i] * PCSAFT.
mole_fractions[i] / (1 + PCSAFT.
_Q * (k[i] - 1));
2654 xv_sum += PCSAFT.
SatV->mole_fractions[i];
2658 for (
int i = 0; i < ncomp; i++) {
2659 PCSAFT.
SatV->mole_fractions[i] = PCSAFT.
SatV->mole_fractions[i] / xv_sum;
2664 for (
int i = 0; i < ncomp; i++) {
2665 PCSAFT.
SatL->mole_fractions[i] = PCSAFT.
SatL->mole_fractions[i] / xl_sum;
2671 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2672 p_start = p_start + 2e5;
2675 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2676 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2679 for (
int i = 0; i < ncomp; i++) {
2681 numer += PCSAFT.
SatL->mole_fractions[i] * fugcoef_l[i];
2682 denom += PCSAFT.
SatV->mole_fractions[i] * fugcoef_v[i];
2685 double ratio = numer / denom;
2689 if ((PCSAFT.
SatL->_rhomolar - PCSAFT.
SatV->_rhomolar) < 1e-4) {
2690 p_start = p_start + 2e5;
2693 fugcoef_l = PCSAFT.
SatL->calc_fugacity_coefficients();
2694 fugcoef_v = PCSAFT.
SatV->calc_fugacity_coefficients();
2697 for (
int i = 0; i < ncomp; i++) {
2699 numer += PCSAFT.
SatL->mole_fractions[i] * fugcoef_l[i];
2700 denom += PCSAFT.
SatV->mole_fractions[i] * fugcoef_v[i];
2703 double ratio_prime = numer / denom;
2705 double slope = (std::log10(ratio) - std::log10(ratio_prime)) / (std::log10(
p) - std::log10(Pprime));
2706 double intercept = std::log10(ratio) - slope * std::log10(
p);
2707 p_guess = std::pow(10, -intercept / slope);
2713 throw SolutionError(
"an estimate for the VLE pressure could not be found");
2730 double cost = (peos -
p) /
p;
2739 SolverRhoResid resid(*
this,
T,
p);
2742 vector<double> x_lo, x_hi;
2744 double limit_lower = -8;
2745 double limit_upper = -1;
2746 double rho_guess = 1e-13;
2747 double rho_guess_prev = rho_guess;
2749 for (
int i = 0; i < num_pts; i++) {
2750 rho_guess = pow(10, (limit_upper - limit_lower) / (
double)num_pts * i + limit_lower);
2752 if (err * err_prev < 0) {
2753 x_lo.push_back(rho_guess_prev);
2754 x_hi.push_back(rho_guess);
2757 rho_guess_prev = rho_guess;
2761 limit_upper = 0.7405;
2762 for (
int i = 0; i < num_pts; i++) {
2763 rho_guess = (limit_upper - limit_lower) / (
double)num_pts * i + limit_lower;
2765 if (err * err_prev < 0) {
2766 x_lo.push_back(rho_guess_prev);
2767 x_hi.push_back(rho_guess);
2770 rho_guess_prev = rho_guess;
2775 double x_lo_molar = 1e-8, x_hi_molar = 1e7;
2777 if (x_lo.size() == 1) {
2782 }
else if (x_lo.size() <= 3 && !x_lo.empty()) {
2794 }
else if (x_lo.size() > 3) {
2796 double g_min = 1e60;
2797 for (
int i = 0; i < x_lo.size(); i++) {
2801 double rho_i =
Brent(resid, x_lo_molar, x_hi_molar,
DBL_EPSILON, 1e-8, 200);
2813 double err_min = 1e40;
2815 for (
int i = 0; i < num_pts; i++) {
2816 double rho_guess = (0.7405 - 1e-8) / (
double)num_pts * i + 1e-8;
2818 if (abs(err) < err_min) {
2830 vector<CoolPropDbl> d(
N);
2832 for (
int i = 0; i <
N; i++) {
2836 return 6 / PI * nu / summ * 1.0e30 / N_AV;
2841 for (
unsigned int i = 0; i <
N; ++i) {
2851 int num_sites = XA_guess.size();
2852 vector<double> XA = XA_guess;
2855 for (
int i = 0; i < num_sites; i++) {
2857 for (
int j = 0; j < num_sites; j++) {
2859 summ += den*x[j]*XA_guess[j]*delta_ij[idxij];
2861 XA[i] = 1./(1.+summ);
2869 vector<double> XA, vector<double> ddelta_dt, vector<double> x) {
2871 int num_sites = XA.size();
2872 Eigen::MatrixXd B = Eigen::MatrixXd::Zero(num_sites, 1);
2873 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(num_sites, num_sites);
2877 for (
int i = 0; i < num_sites; i++) {
2879 for (
int j = 0; j < num_sites; j++) {
2880 B(i) -= x[j]*XA[j]*ddelta_dt[ij];
2881 A(i,j) = x[j]*delta_ij[ij];
2882 summ += x[j]*XA[j]*delta_ij[ij];
2885 A(i,i) = pow(1+den*summ, 2.)/den;
2888 Eigen::MatrixXd solution = A.lu().solve(B);
2889 vector<double> dXA_dt(num_sites);
2890 for (
int i = 0; i < num_sites; i++) {
2891 dXA_dt[i] = solution(i);
2898 double den, vector<double> XA, vector<double> ddelta_dx, vector<double> x) {
2901 int num_sites = XA.size();
2903 Eigen::MatrixXd B(num_sites*ncomp, 1);
2904 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(num_sites*ncomp, num_sites*ncomp);
2909 for (
int i = 0; i < ncomp; i++) {
2910 for (
int j = 0; j < num_sites; j++) {
2912 for (
int k = 0; k < num_sites; k++) {
2913 sum1 = sum1 + den*x[k]*(XA[k]*ddelta_dx[i*num_sites*num_sites + j*num_sites + k]);
2914 A(ij,i*num_sites+k) = XA[j]*XA[j]*den*x[k]*delta_ij[j*num_sites+k];
2918 for (
int l = 0; l <
assoc_num[i]; l++) {
2919 sum2 = sum2 + XA[idx1+l]*delta_ij[idx1*num_sites+l*num_sites+j];
2922 A(ij,ij) = A(ij,ij) + 1;
2923 B(ij) = -1*XA[j]*XA[j]*(sum1 + sum2);
2929 Eigen::MatrixXd solution = A.lu().solve(B);
2930 vector<double> dXA_dx(num_sites*ncomp);
2931 for (
int i = 0; i < num_sites*ncomp; i++) {
2932 dXA_dx[i] = solution(i);
2941 for (
int i = 0; i <
N; i++){
2942 vector<std::string> assoc_scheme =
components[i].getAssocScheme();
2944 int num = assoc_scheme.size();
2945 for (
int j = 0; j < num; j++) {
2948 charge.push_back(0);
2953 vector<int> tmp{0, 0};
2954 charge.insert(charge.end(), tmp.begin(), tmp.end());
2959 vector<int> tmp{-1, 1};
2960 charge.insert(charge.end(), tmp.begin(), tmp.end());
2965 vector<int> tmp{0, 0, 0};
2966 charge.insert(charge.end(), tmp.begin(), tmp.end());
2971 vector<int> tmp{-1, -1, 1};
2972 charge.insert(charge.end(), tmp.begin(), tmp.end());
2977 vector<int> tmp{0, 0, 0, 0};
2978 charge.insert(charge.end(), tmp.begin(), tmp.end());
2983 vector<int> tmp{1, 1, 1, -1};
2984 charge.insert(charge.end(), tmp.begin(), tmp.end());
2989 vector<int> tmp{-1, -1, 1, 1};
2990 charge.insert(charge.end(), tmp.begin(), tmp.end());
2995 throw ValueError(
format(
"%s is not a valid association type.", assoc_scheme[j]));
3002 for (std::vector<int>::iterator
i1 = charge.begin();
i1 != charge.end();
i1++) {
3003 for (std::vector<int>::iterator i2 = charge.begin(); i2 != charge.end(); i2++) {
3004 if (*
i1 == 0 || *i2 == 0) {
3007 else if (*
i1 == 1 && *i2 == -1) {
3010 else if (*
i1 == -1 && *i2 == 1) {
3039 throw ValueError(
"The current function for the dielectric constant for water is only valid for temperatures above 263.15 K.");
3040 }
else if (t <= 368.15) {
3041 dielc = 7.6555618295E-04 *
_T *
_T - 8.1783881423E-01 *
_T + 2.5419616803E+02;
3042 }
else if (t <= 443.15) {
3043 dielc = 0.0005003272124 *
_T *
_T - 0.6285556029 *
_T + 220.4467027;
3045 throw ValueError(
"The current function for the dielectric constant for water is only valid for temperatures less than 443.15 K.");