2 #define _CRT_SECURE_NO_WARNINGS
20 static const bool use_cache =
true;
28 if (!itrC->IsString())
43 std::vector<double> v;
48 v.push_back((
double)itrC->GetInt());
52 v.push_back(itrC->GetDouble());
59 std::vector<std::string> v;
62 v.push_back(itrC->GetString());
66 bool match_CAS_entry(std::vector<std::string> CAS1, std::vector<std::string> CAS2, std::string FluidiCAS, std::string FluidjCAS)
68 for (
unsigned int k = 0; k < CAS1.size(); k++)
71 (!FluidiCAS.compare(CAS1[k]) && !FluidjCAS.compare(CAS2[k]))
73 (!FluidjCAS.compare(CAS1[k]) && !FluidiCAS.compare(CAS2[k]))
81 std::map<std::string, double > outputmap;
92 if (itr->HasMember(
"Model") && (*itr)[
"Model"].IsString())
98 throw ValueError(
"Model not included for reducing term");
102 if (itr->HasMember(
"Coeffs") && (*itr)[
"Coeffs"].IsArray())
109 std::string Name1,Name2,CAS1,CAS2;
110 std::vector<std::string> Names1, Names2, CASs1, CASs2;
112 if (itrC->HasMember(
"Name1") && (*itrC)[
"Name1"].IsString() && itrC->HasMember(
"Name2") && (*itrC)[
"Name2"].IsString())
115 Name2 = (*itrC)[
"Name2"].GetString();
116 CAS1 = (*itrC)[
"CAS1"].GetString();
117 CAS2 = (*itrC)[
"CAS2"].GetString();
119 else if (itrC->HasMember(
"Names1") && (*itrC)[
"Names1"].IsArray() && itrC->HasMember(
"Names2") && (*itrC)[
"Names2"].IsArray())
121 std::cout <<
format(
"Not currently supporting lists of components in excess terms\n").c_str();
124 std::string FluidiCAS =
pFluids[i]->params.CAS, FluidjCAS =
pFluids[j]->params.CAS;
128 (!(FluidiCAS.compare(CAS1)) && !(FluidjCAS.compare(CAS2)))
130 (!(FluidjCAS.compare(CAS1)) && !(FluidiCAS.compare(CAS2)))
134 if (!Model.compare(
"Kunz-JCED-2012"))
141 if (!(FluidiCAS.compare(CAS1)) && !(FluidjCAS.compare(CAS2)))
143 outputmap.insert(std::pair<std::string, double >(
"betaT",(*itrC)[
"betaT"].GetDouble()));
144 outputmap.insert(std::pair<std::string, double >(
"betaV",(*itrC)[
"betaV"].GetDouble()));
148 outputmap.insert(std::pair<std::string, double >(
"betaT",1/(*itrC)[
"betaT"].GetDouble()));
149 outputmap.insert(std::pair<std::string, double >(
"betaV",1/(*itrC)[
"betaV"].GetDouble()));
151 outputmap.insert(std::pair<std::string, double >(
"gammaT",(*itrC)[
"gammaT"].GetDouble()));
152 outputmap.insert(std::pair<std::string, double >(
"gammaV",(*itrC)[
"gammaV"].GetDouble()));
153 outputmap.insert(std::pair<std::string, double >(
"F",(*itrC)[
"F"].GetDouble()));
157 else if (!Model.compare(
"Lemmon-JPCRD-2000") || !Model.compare(
"Lemmon-JPCRD-2004"))
164 outputmap.insert(std::pair<std::string,double>(
"xi",(*itrC)[
"xi"].GetDouble()));
165 outputmap.insert(std::pair<std::string,double>(
"zeta",(*itrC)[
"zeta"].GetDouble()));
166 outputmap.insert(std::pair<std::string,double>(
"F",(*itrC)[
"F"].GetDouble()));
171 throw ValueError(
format(
"This model [%s] is not currently supported\n",Model.c_str()).c_str());
178 throw ValueError(
"Coeffs not included for reducing term");
187 std::map<std::string,std::vector<double> > outputmap;
189 std::string FluidiCAS =
pFluids[i]->params.CAS,
190 FluidjCAS =
pFluids[j]->params.CAS;
201 if (itr->HasMember(
"Model") && (*itr)[
"Model"].IsString())
208 throw ValueError(
"Model not included for excess term");
212 if (itr->HasMember(
"Coeffs") && (*itr)[
"Coeffs"].IsArray())
219 std::vector<std::string> Names1, Names2, CASs1, CASs2;
221 if (itrC->HasMember(
"Name1") && (*itrC)[
"Name1"].IsString() && itrC->HasMember(
"Name2") && (*itrC)[
"Name2"].IsString())
223 Names1 = std::vector<std::string>(1, (*itrC)[
"Name1"].GetString());
224 Names2 = std::vector<std::string>(1, (*itrC)[
"Name2"].GetString());
225 CASs1 = std::vector<std::string>(1, (*itrC)[
"CAS1"].GetString());
226 CASs2 = std::vector<std::string>(1, (*itrC)[
"CAS2"].GetString());
241 if (!Model.compare(
"Kunz-JCED-2012"))
247 outputmap.insert(std::pair<std::string,std::vector<double> >(
"n",
JSON_double_array(_n)));
249 outputmap.insert(std::pair<std::string,std::vector<double> >(
"t",
JSON_double_array(_t)));
251 outputmap.insert(std::pair<std::string,std::vector<double> >(
"d",
JSON_double_array(_d)));
253 outputmap.insert(std::pair<std::string,std::vector<double> >(
"eta",
JSON_double_array(_eta)));
255 outputmap.insert(std::pair<std::string,std::vector<double> >(
"epsilon",
JSON_double_array(_epsilon)));
257 outputmap.insert(std::pair<std::string,std::vector<double> >(
"beta",
JSON_double_array(_beta)));
259 outputmap.insert(std::pair<std::string,std::vector<double> >(
"gamma",
JSON_double_array(_gamma)));
265 else if (!Model.compare(
"Lemmon-JPCRD-2004"))
271 outputmap.insert(std::pair<std::string,std::vector<double> >(
"n",
JSON_double_array(_n)));
273 outputmap.insert(std::pair<std::string,std::vector<double> >(
"t",
JSON_double_array(_t)));
275 outputmap.insert(std::pair<std::string,std::vector<double> >(
"d",
JSON_double_array(_d)));
277 outputmap.insert(std::pair<std::string,std::vector<double> >(
"l",
JSON_double_array(_l)));
285 throw ValueError(
format(
"This model [%s] is not currently supported\n",Model.c_str()).c_str());
292 throw ValueError(
"Coeffs not included for excess term");
295 throw ValueError(
"No excess parameters loaded for this binary pair");
300 double sumx = std::accumulate( x.begin(), x.end(), (double)0 );
302 for (
unsigned int i = 0; i < x.size(); i++)
313 for (
unsigned int i = 0; i <
N; i++)
315 R += x[i]*
pFluids[i]->params.R_u;
323 this->
N = pFluids.size();
332 std::vector<std::string> fluids =
strsplit(FluidsString,
'|');
335 this->
N = fluids.size();
341 for (
unsigned int i = 0; i < fluids.size(); i++)
371 for (
unsigned int i = 0; i <
pFluids.size(); i++)
373 for (
unsigned int j = 0; j <
pFluids.size(); j++)
380 pExcess->
F[i][j] = reducing_map.find(
"F")->second;
393 std::vector<double>
x,y,z(2, 0.5);
408 double delta = rhobar/rhorbar;
436 for (
double psat = 1e6; psat < 6.9e6; psat += 1e6)
438 std::cout <<
"psat: " << psat <<
" Pa" << std::endl;
439 for (
double x0 = 0; x0 <= 1.000000000000001; x0 += 0.01)
441 z[0] = x0; z[1] = 1-x0;
443 std::cout <<
format(
"%g %g %0.9g %0.9g",x0,Tsat,y[0],y[1]).c_str();
445 std::cout <<
format(
" %g %0.9g %0.9g",Tsat,x[0],x[1]).c_str() ;
446 std::cout << std::endl;
464 std::vector<double> z(2, 0.5);
471 double Tr_RP91 = 574.10640748981632;
472 if (fabs(Tr/Tr_RP91-1) > tol){
throw ValueError();}
475 double rhorbar_RP91 = 11090.627577544581;
476 if (fabs(rhorbar/rhorbar_RP91-1) > tol){
throw ValueError();}
479 double rhobar = 868.40163264165199;
482 double delta = rhobar/rhorbar;
484 double RT =
Rbar(z)*T;
485 double p = rhobar*RT*(1+delta*
dphir_dDelta(tau, delta,z));
486 double p_RP91 = 3011336.2097271665;
487 if (fabs(p/p_RP91-1) > tol){
throw ValueError();}
490 double dtdn0_RP91 = -80.606224987885071;
491 if (fabs(dtdn0/dtdn0_RP91-1) > tol){
throw ValueError();}
494 double dtdn1_RP91 = 53.737483325256562;
495 if (fabs(dtdn1/dtdn1_RP91-1) > tol){
throw ValueError();}
498 double drhodn0_RP91 = -7259.8385028178765;
499 if (fabs(drhodn0/drhodn0_RP91-1) > tol){
throw ValueError();}
502 double drhodn1_RP91 = 4839.8923352119168;
503 if (fabs(drhodn1/drhodn1_RP91-1) > tol){
throw ValueError();}
506 double ddrdxn00_RP91 = 57050.801427407002;
507 if (fabs(ddrdxn00/ddrdxn00_RP91-1) > tol){
throw ValueError();}
510 double ddrdxn01_RP91 = 35234.083487633299;
511 if (fabs(ddrdxn01/ddrdxn01_RP91-1) > tol){
throw ValueError();}
514 double ddrdxn10_RP91 = 11034.621811573714;
515 if (fabs(ddrdxn10/ddrdxn10_RP91-1) > tol){
throw ValueError();}
518 double ddrdxn11_RP91 = 5412.8823747065340;
519 if (fabs(ddrdxn11/ddrdxn11_RP91-1) > tol){
throw ValueError();}
522 double dtrdxn00_RP91 = -1078.6599487910798;
523 if (fabs(dtrdxn00/dtrdxn00_RP91-1) > tol){
throw ValueError();}
526 double dtrdxn01_RP91 = -1328.9251007518092;
527 if (fabs(dtrdxn01/dtrdxn01_RP91-1) > tol){
throw ValueError();}
530 double dtrdxn10_RP91 = -1060.2376841255259;
531 if (fabs(dtrdxn10/dtrdxn10_RP91-1) > tol){
throw ValueError();}
534 double dtrdxn11_RP91 = -1117.3004300069424;
535 if (fabs(dtrdxn11/dtrdxn11_RP91-1) > tol){
throw ValueError();}
546 double daddx0_RP91 = -1.9898117177651518;
547 if (fabs(daddx0/daddx0_RP91-1) > tol){
throw ValueError();}
550 double daddx1_RP91 = -2.1704856931980134;
551 if (fabs(daddx1/daddx1_RP91-1) > tol){
throw ValueError();}
554 double dadtx0_RP91 = -0.54706966169729132;
555 if (fabs(dadtx0/dadtx0_RP91-1) > tol){
throw ValueError();}
558 double dadtx1_RP91 = -0.45648092575441646;
559 if (fabs(dadtx1/dadtx1_RP91-1) > tol){
throw ValueError();}
562 double dadn0_RP91 = -0.18827619536857490;
563 if (fabs(dadn0/dadn0_RP91-1) > tol){
throw ValueError();}
566 double dadn1_RP91 = -0.15092181712014577;
567 if (fabs(dadn1/dadn1_RP91-1) > tol){
throw ValueError();}
570 double dpdn0_RP91 = 2359472.7345531628;
571 if (fabs(dpdn0/dpdn0_RP91-1) > tol){
throw ValueError();}
574 double dpdn1_RP91 = 2459536.5646813584;
575 if (fabs(dpdn1/dpdn1_RP91-1) > tol){
throw ValueError();}
578 double vhat0_RP91 = 1.1229663043954532/1000;
579 if (fabs(vhat0/vhat0_RP91-1) > tol){
throw ValueError();}
582 double vhat1_RP91 = 1.1705906349830217/1000;
583 if (fabs(vhat1/vhat1_RP91-1) > tol){
throw ValueError();}
594 double dphidT0_RP91 = 1.80275151505261211E-003;
595 if (fabs(dphidT0/dphidT0_RP91-1) > tol){
throw ValueError();}
598 double dphidT1_RP91 = 1.24216502708919410E-003;
599 if (fabs(dphidT1/dphidT1_RP91-1) > tol){
throw ValueError();}
602 double dphidP0_RP91 = -6.19532350116346726E-005/1000;
603 if (fabs(dphidP0/dphidP0_RP91-1) > tol){
throw ValueError();}
606 double dphidP1_RP91 = -5.04973839435411730E-005/1000;
607 if (fabs(dphidP1/dphidP1_RP91-1) > tol){
throw ValueError();}
610 double dadxij00_RP91 = 0.0;
611 if (fabs(dadxij00-dadxij00_RP91) > tol){
throw ValueError();}
614 double dadxij01_RP91 = 2.95156019175951646E-003;
615 if (fabs(dadxij01/dadxij01_RP91-1) > tol){
throw ValueError();}
618 double dadxij10_RP91 = 2.95156019175951646E-003;
619 if (fabs(dadxij10/dadxij10_RP91-1) > tol){
throw ValueError();}
622 double dadxij11_RP91 = 0.0;
623 if (fabs(dadxij11-dadxij11_RP91) > tol){
throw ValueError();}
626 double d2adxn00_RP91 = 1.4701046408163372;
627 if (fabs(d2adxn00-d2adxn00_RP91) > tol){
throw ValueError();}
630 double d2adxn01_RP91 = 1.4673267962070480;
631 if (fabs(d2adxn01/d2adxn01_RP91-1) > tol){
throw ValueError();}
634 double d2adxn10_RP91 = 1.5168952199179448;
635 if (fabs(d2adxn10/d2adxn10_RP91-1) > tol){
throw ValueError();}
638 double d2adxn11_RP91 = 1.4329117162994811;
639 if (fabs(d2adxn11/d2adxn11_RP91-1) > tol){
throw ValueError();}
642 double d2addn0_RP91 = -2.3060567022876386;
643 if (fabs(d2addn0/d2addn0_RP91-1) > tol){
throw ValueError();}
646 double d2addn1_RP91 = -1.9520671401909093;
647 if (fabs(d2addn1/d2addn1_RP91-1) > tol){
throw ValueError();}
650 double d2adtn0_RP91 = -0.62562519155517959;
651 if (fabs(d2adtn0/d2adtn0_RP91-1) > tol){
throw ValueError();}
654 double d2adtn1_RP91 = -0.43264230263327769;
655 if (fabs(d2adtn1/d2adtn1_RP91-1) > tol){
throw ValueError();}
658 double d2dnn00_RP91 = -0.38451299457845400;
659 if (fabs(d2ann00/d2dnn00_RP91-1) > tol){
throw ValueError();}
662 double d2dnn01_RP91 = -0.32103958765767326;
663 if (fabs(d2ann01/d2dnn01_RP91-1) > tol){
throw ValueError();}
666 double d2dnn10_RP91 = -0.32103958765767304;
667 if (fabs(d2ann10/d2dnn10_RP91-1) > tol){
throw ValueError();}
670 double d2dnn11_RP91 = -0.31715926219249480;
671 if (fabs(d2ann11/d2dnn11_RP91-1) > tol){
throw ValueError();}
674 double dphidnj00_RP91 = -2.18661832047073457E-002;
675 if (fabs(dphidnj00/dphidnj00_RP91-1) > tol){
throw ValueError();}
678 double dphidnj01_RP91 = 1.45774554698049341E-002;
679 if (fabs(dphidnj01/dphidnj01_RP91-1) > tol){
throw ValueError();}
682 double dphidnj10_RP91 = 1.45774554698051562E-002;
683 if (fabs(dphidnj10/dphidnj10_RP91-1) > tol){
throw ValueError();}
686 double dphidnj11_RP91 = -9.71830364653669676E-003;
687 if (fabs(dphidnj11/dphidnj11_RP91-1) > tol){
throw ValueError();}
693 std::vector<double> z(2, 0.5);
700 double Tr_RP91 = 250.57185990876351;
701 if (fabs(Tr/Tr_RP91-1) > tol){
throw ValueError();}
704 double rhorbar_RP91 = 8205.7858837320694;
705 if (fabs(rhorbar/rhorbar_RP91-1) > tol){
throw ValueError();}
711 double delta = rhobar/rhorbar;
713 double RT =
Rbar(z)*T;
714 double p = rhobar*RT*(1+delta*
dphir_dDelta(tau, delta,z));
715 double p_RP91 = 4814.1558068139991;
716 if (fabs(p/p_RP91-1) > tol){
throw ValueError();}
719 double dtdn0_RP91 = -56.914351037292874;
720 if (fabs(dtdn0/dtdn0_RP91-1) > tol){
throw ValueError();}
723 double dtdn1_RP91 = 56.914351037292874;
724 if (fabs(dtdn1/dtdn1_RP91-1) > tol){
throw ValueError();}
727 double drhodn0_RP91 = 1579.4307575322835;
728 if (fabs(drhodn0/drhodn0_RP91-1) > tol){
throw ValueError();}
731 double drhodn1_RP91 = -1579.4307575322843;
732 if (fabs(drhodn1/drhodn1_RP91-1) > tol){
throw ValueError();}
735 double ddrdxn00_RP91 = 10652.242638037194;
736 if (fabs(ddrdxn00/ddrdxn00_RP91-1) > tol){
throw ValueError();}
739 double ddrdxn01_RP91 = 12694.316351697381;
740 if (fabs(ddrdxn01/ddrdxn01_RP91-1) > tol){
throw ValueError();}
743 double ddrdxn10_RP91 = 19012.039381826526;
744 if (fabs(ddrdxn10/ddrdxn10_RP91-1) > tol){
throw ValueError();}
747 double ddrdxn11_RP91 = 23287.688698295469;
748 if (fabs(ddrdxn11/ddrdxn11_RP91-1) > tol){
throw ValueError();}
751 double dtrdxn00_RP91 = -506.39802892344619;
752 if (fabs(dtrdxn00/dtrdxn00_RP91-1) > tol){
throw ValueError();}
755 double dtrdxn01_RP91 = -609.71811278619361;
756 if (fabs(dtrdxn01/dtrdxn01_RP91-1) > tol){
throw ValueError();}
759 double dtrdxn10_RP91 = -382.06070863702217;
760 if (fabs(dtrdxn10/dtrdxn10_RP91-1) > tol){
throw ValueError();}
763 double dtrdxn11_RP91 = -506.39802892344630;
764 if (fabs(dtrdxn11/dtrdxn11_RP91-1) > tol){
throw ValueError();}
766 double dadxi0 = this->
dphir_dxi(tau, delta, z, 0);
767 double dadxi0_RP91 = -1.66733159546361936E-003;
768 if (fabs(dadxi0/dadxi0_RP91-1) > tol){
throw ValueError();}
770 double dadxi1 = this->
dphir_dxi(tau, delta, z, 1);
771 double dadxi1_RP91 = -1.82138497681156902E-003;
772 if (fabs(dadxi1/dadxi1_RP91-1) > tol){
throw ValueError();}
775 double daddx0_RP91 = -3.4250061506157587;
776 if (fabs(daddx0/daddx0_RP91-1) > tol){
throw ValueError();}
779 double dadtx0_RP91 = -1.94597122635832131E-003;
780 if (fabs(dadtx0/dadtx0_RP91-1) > tol){
throw ValueError();}
783 double daddx1_RP91 = -3.7448162669516916;
784 if (fabs(daddx1/daddx1_RP91-1) > tol){
throw ValueError();}
787 double dadtx1_RP91 = -2.15974774776696646E-003;
788 if (fabs(dadtx1/dadtx1_RP91-1) > tol){
throw ValueError();}
791 double dadn0_RP91 = -5.24342982584834368E-004;
792 if (fabs(dadn0/dadn0_RP91-1) > tol){
throw ValueError();}
795 double dadn1_RP91 = -2.89676062124885614E-003;
796 if (fabs(dadn1/dadn1_RP91-1) > tol){
throw ValueError();}
799 double dpdn0_RP91 = 4811.6359520642318;
800 if (fabs(dpdn0/dpdn0_RP91-1) > tol){
throw ValueError();}
803 double dpdn1_RP91 = 4800.1319544625067;
804 if (fabs(dpdn1/dpdn1_RP91-1) > tol){
throw ValueError();}
807 double vhat0_RP91 = 0.25029921648425145;
808 if (fabs(vhat0/vhat0_RP91-1) > tol){
throw ValueError();}
811 double vhat1_RP91 = 0.24970078351574867;
812 if (fabs(vhat1/vhat1_RP91-1) > tol){
throw ValueError();}
815 double d2adbn0_RP91 = 2.91400791470335643E-005;
816 if (fabs(d2adbn0/d2adbn0_RP91-1) > tol){
throw ValueError();}
819 double d2adbn1_RP91 = 6.58714026367381391E-005;
820 if (fabs(d2adbn1/d2adbn1_RP91-1) > tol){
throw ValueError();}
823 double dphidT0_RP91 = 8.84377505112714235E-006;
824 if (fabs(dphidT0/dphidT0_RP91-1) > tol){
throw ValueError();}
827 double dphidT1_RP91 = 6.21123852185909847E-005;
828 if (fabs(dphidT1/dphidT1_RP91-1) > tol){
throw ValueError();}
831 double dphidP0_RP91 = -1.07128474189810419E-007;
832 if (fabs(dphidP0/dphidP0_RP91-1) > tol){
throw ValueError();}
835 double dphidP1_RP91 = -6.03505693277411881E-007;
836 if (fabs(dphidP1/dphidP1_RP91-1) > tol){
throw ValueError();}
839 double dadxij00_RP91 = 0.0;
840 if (fabs(dadxij00-dadxij00_RP91) > tol){
throw ValueError();}
843 double dadxij01_RP91 = -1.44900341276804124E-004;
844 if (fabs(dadxij01/dadxij01_RP91-1) > tol){
throw ValueError();}
847 double dadxij10_RP91 = -1.44900341276804124E-004;
848 if (fabs(dadxij10/dadxij10_RP91-1) > tol){
throw ValueError();}
851 double dadxij11_RP91 = 0.0;
852 if (fabs(dadxij11-dadxij11_RP91) > tol){
throw ValueError();}
855 double d2adxn00_RP91 = 9.52786141739760811E-003;
856 if (fabs(d2adxn00-d2adxn00_RP91) > tol){
throw ValueError();}
859 double d2adxn01_RP91 = 1.11090273394917772E-002;
860 if (fabs(d2adxn01/d2adxn01_RP91-1) > tol){
throw ValueError();}
863 double d2adxn10_RP91 = 8.82661064281056729E-003;
864 if (fabs(d2adxn10/d2adxn10_RP91-1) > tol){
throw ValueError();}
867 double d2adxn11_RP91 = 1.16784901260031035E-002;
868 if (fabs(d2adxn11/d2adxn11_RP91-1) > tol){
throw ValueError();}
871 double d2addn0_RP91 = -1.0719438474166139;
872 if (fabs(d2addn0/d2addn0_RP91-1) > tol){
throw ValueError();}
875 double d2addn1_RP91 = -5.9657336386754745;
876 if (fabs(d2addn1/d2addn1_RP91-1) > tol){
throw ValueError();}
879 double d2adtn0_RP91 = -4.58046408525892678E-004;
880 if (fabs(d2adtn0/d2adtn0_RP91-1) > tol){
throw ValueError();}
883 double d2adtn1_RP91 = -3.54010070086436396E-003;
884 if (fabs(d2adtn1/d2adtn1_RP91-1) > tol){
throw ValueError();}
887 double d2dnn00_RP91 = -1.55709211017489475E-003;
888 if (fabs(d2ann00/d2dnn00_RP91-1) > tol){
throw ValueError();}
891 double d2dnn01_RP91 = -2.90907297842576788E-003;
892 if (fabs(d2ann01/d2dnn01_RP91-1) > tol){
throw ValueError();}
895 double d2dnn10_RP91 = -2.90907297842577049E-003;
896 if (fabs(d2ann10/d2dnn10_RP91-1) > tol){
throw ValueError();}
899 double d2dnn11_RP91 = -6.32815473413224101E-003;
900 if (fabs(d2ann11/d2dnn11_RP91-1) > tol){
throw ValueError();}
903 double dphidnj00_RP91 = -5.18202802448741728E-004;
904 if (fabs(dphidnj00/dphidnj00_RP91-1) > tol){
throw ValueError();}
907 double dphidnj01_RP91 = 5.18202802448741728E-004;
908 if (fabs(dphidnj01/dphidnj01_RP91-1) > tol){
throw ValueError();}
911 double dphidnj10_RP91 = 5.18202802448741728E-004;
912 if (fabs(dphidnj10/dphidnj10_RP91-1) > tol){
throw ValueError();}
915 double dphidnj11_RP91 = -5.18202802448741728E-004;
916 if (fabs(dphidnj11/dphidnj11_RP91-1) > tol){
throw ValueError();}
933 double pci =
pFluids[i]->reduce.p.Pa;
934 double wi =
pFluids[i]->params.accentricfactor;
935 double Tci =
pFluids[i]->reduce.T;
936 return log(pci/p)+5.373*(1 + wi)*(1-Tci/T);
942 double T = Tr/tau, rhobar = rhorbar*delta,
Rbar = 8.314472;
946 double f_i = x[i]*rhobar*Rbar*T*exp(dnphir_dni);
957 double dtau_dT = -tau/T;
988 double rhobar = rhorbar*delta;
989 double RT =
Rbar(x)*T;
990 double p = rhobar*RT*(1.0+delta*
dphir_dDelta(tau, delta, x));
992 double term1 = partial_molar_volume/RT;
993 double term2 = 1.0/p;
994 return term1 - term2;
1007 return d2nphir_dni_dxj__constT_V(tau, delta, x, i, j) - this->
partial_molar_volume(tau,delta,x,i)/(
Rbar(x)*T)*
dpdxj__constT_V_xi(tau,delta,x,j);
1013 double rhobar = rhorbar*delta;
1035 return dphir_dDelta(tau, delta, x)*
ddelta_dxj__constT_V_xi(tau, delta, x, j)+
dphir_dTau(tau, delta, x)*
dtau_dxj__constT_V_xi(tau, delta, x, j)+
dphir_dxi(tau, delta, x, j);
1063 double rhobar = rhorbar*delta;
1069 double rhobar = rhorbar*delta;
1078 double rhobar = rhorbar*delta;
1084 for (
unsigned int k = 0; k < x.size(); k++)
1089 return rhobar*
Rbar(x)*T*(1+delta*
dphir_dDelta(tau,delta,x)*(2-1/rhorbar*ndrhorbar_dni__constnj)+delta*nd2phir_dni_dDelta);
1101 for (
unsigned int k = 0; k < x.size(); k++)
1105 double term3 =
dphir_dxi(tau,delta,x,i);
1106 return term1 + term2 + term3 - s;
1112 return nd2nphirdnidnj__constT_V(tau, delta, x, j, i) + 1 -
partial_molar_volume(tau,delta,x,i)/(
Rbar(x)*T)*
ndpdni__constT_V_nj(tau,delta,x,j);
1134 for (
unsigned int m = 0; m < x.size(); m++)
1139 return line1+line2+line3+line4+line5;
1147 for (
unsigned int k = 0; k < x.size(); k++)
1152 return line0 + line1 + line2 + line3;
1167 for (
unsigned int k = 0; k < x.size(); k++)
1171 return term1 + term2 + term3;
1187 for (
unsigned int k = 0; k < x.size(); k++)
1191 return term1 + term2 + term3;
1239 const std::vector<double> *
z;
1240 const std::vector<double> *
lnK;
1254 const std::vector<double> *
x;
1258 this->x = &
x; this->T =
T; this->p =
p; this->Mix =
Mix;
1266 double delta = rhobar/
rhorbar;
1272 double delta = rhobar/
rhorbar;
1290 rhobar =
Newton(&Resid, rhobar0, 1e-16, 100, &errstr);
1293 rhobar =
Secant(&Resid, rhobar0, 1.001*rhobar0, 1e-16, 100, &errstr);
1312 const std::vector<double> *
z;
1313 std::vector<double>
K;
1317 this->z = &
z; this->p =
p; this->Mix =
Mix, this->beta =
beta;
1318 K = std::vector<double>(z.size(),_HUGE);
1322 for (
unsigned int i = 0; i< (*z).size(); i++) {
1324 summer += (*z)[i]*(
K[i]-1)/(1-
beta+
beta*
K[i]);
1331 double pseudo_ptriple = 0;
1332 double pseudo_pcrit = 0;
1333 double pseudo_Ttriple = 0;
1334 double pseudo_Tcrit = 0;
1337 for (
unsigned int i = 0; i <
N; i++)
1339 pseudo_ptriple +=
pFluids[i]->params.ptriple*z[i];
1340 pseudo_pcrit +=
pFluids[i]->crit.p.Pa*z[i];
1341 pseudo_Ttriple +=
pFluids[i]->params.Ttriple*z[i];
1342 pseudo_Tcrit +=
pFluids[i]->crit.T*z[i];
1346 return (pseudo_Tcrit-pseudo_Ttriple)/(pseudo_pcrit/pseudo_ptriple)*(p/pseudo_ptriple)+pseudo_Ttriple;
1356 T =
Secant(&Resid, T_guess, 0.001, 1e-10, 100, &errstr);
1363 double Mixture::saturation_p(
double beta,
double p, std::vector<double>
const& z, std::vector<double> &
x, std::vector<double> &y)
1366 unsigned int N = z.size();
1367 std::vector<double> K(N), ln_phi_liq(N), ln_phi_vap(N);
1370 for (
unsigned int i = 0; i <
N; i++)
1378 double TL=0, TV=0, rhoL=0, rhoV=0;
1379 pFluids[i]->saturation_p(p,
false, TL, TV, rhoL, rhoV);
1393 T =
SS.
call(beta, T, p, z, K);
1400 void Mixture::TpzFlash(
double T,
double p,
const std::vector<double> &z,
double &rhobar, std::vector<double> &
x, std::vector<double> &y)
1402 unsigned int N = z.size();
1403 double beta, change = 0;
1404 std::vector<double> lnK(N);
1410 for (
unsigned int i = 0; i <
N; i++)
1441 beta =
Newton(&Resid,0,1e-10,300,&errstr);
1444 for (
unsigned int i = 0; i <
N; i++)
1446 double Ki = exp(lnK[i]);
1447 double den = (1 - beta + beta*Ki);
1462 double delta_liq = rhobar_liq/rhorbar_liq;
1463 double delta_vap = rhobar_vap/rhorbar_vap;
1466 for (
unsigned int i = 0; i <
N; i++)
1471 double lnKold = lnK[i];
1473 lnK[i] = ln_phi_liq - ln_phi_vap;
1475 change = lnK[i] - lnKold;
1478 while( fabs(change) > 1e-7);
1482 void Mixture::x_and_y_from_K(
double beta,
const std::vector<double> &K,
const std::vector<double> &z, std::vector<double> &
x, std::vector<double> &y)
1484 for (
unsigned int i=0; i <
N; i++)
1486 double denominator = (1-beta+beta*K[i]);
1487 x[i] = z[i]/denominator;
1488 y[i] = K[i]*z[i]/denominator;
1495 double A = 0, B = 0, a = 0, b = 0, m_i, m_j, a_i, a_j, b_i, R =
Rbar(x), k_ij;
1497 for (
unsigned int i = 0; i <
N; i++)
1500 b_i = 0.077796074*(R*
pFluids[i]->reduce.T)/(
pFluids[i]->reduce.p.Pa);
1503 double omega_i =
pFluids[i]->params.accentricfactor;
1505 m_i = 0.37464 + 1.54226*omega_i-0.26992*pow(omega_i,(
int)2);
1507 for (
unsigned int j = 0; j <
N; j++)
1509 double omega_j =
pFluids[j]->params.accentricfactor;
1510 m_j = 0.37464 + 1.54226*omega_j-0.26992*pow(omega_j,(
int)2);
1512 a_i = 0.45724*pow(R*
pFluids[i]->reduce.T,2)/
pFluids[i]->reduce.p.Pa*pow(1+m_i*(1-sqrt(T/
pFluids[i]->reduce.T)),2);
1515 a_j = 0.45724*pow(R*
pFluids[j]->reduce.T,2)/
pFluids[j]->reduce.p.Pa*pow(1+m_j*(1-sqrt(T/
pFluids[j]->reduce.T)),2);
1524 a += x[i]*x[j]*sqrt(a_i*a_j)*(1-k_ij);
1531 solve_cubic(1, -1+B, A-3*B*B-2*B, -A*B+B*B+B*B*B, &Z0, &Z1, &Z2);
1535 return p/(
min3(Z0,Z1,Z2)*R*T);
1539 return p/(
max3(Z0,Z1,Z2)*R*T);
1543 throw ValueError(
"Solution should be one of PR_SATL, PR_SATV");
1551 for (
unsigned int i = 0; i < z.size(); i++)
1553 double Ki = exp(lnK[i]);
1554 summer += z[i]*(Ki-1)/(1-beta+beta*Ki);
1562 for (
unsigned int i = 0; i < z.size(); i++)
1564 double Ki = exp(lnK[i]);
1565 summer += -z[i]*pow((Ki-1)/(1-beta+beta*Ki),2);
1573 for (
unsigned int i = 0; i <
N; i++)
1575 double xi = x[i], Tci =
pFluids[i]->reduce.T;
1579 if (i==N-1){
break; }
1581 for (
unsigned int j = i+1; j <
N; j++)
1592 double dTr_dxi = 2*xi*
pFluids[i]->reduce.T;
1593 for (
int k = 0; k < i; k++)
1597 for (
unsigned int k = i+1; k <
N; k++)
1606 double d2Tr_dxi2 = 2*
pFluids[i]->reduce.T;
1607 for (
int k = 0; k < i; k++)
1611 for (
unsigned int k = i+1; k <
N; k++)
1633 double dvrbar_dxi = 2*xi/
pFluids[i]->reduce.rhobar;
1635 for (
int k = 0; k < i; k++)
1639 for (
unsigned int k = i+1; k <
N; k++)
1663 double d2vrbardxi2 = 2/
pFluids[i]->reduce.rhobar;
1665 for (
int k = 0; k < i; k++)
1669 for (
unsigned int k = i+1; k <
N; k++)
1677 double rhor = this->
rhorbar(x);
1679 return 2*pow(rhor,(
int)3)*pow(dvrbardxi,(
int)2)-pow(rhor,(
int)2)*this->
d2vrbardxi2__constxj(x,i);
1683 double rhor = this->
rhorbar(x);
1686 return 2*pow(rhor,(
int)3)*dvrbardxi*dvrbardxj-pow(rhor,(
int)2)*this->
d2vrbardxidxj(x,i,j);
1692 for (
unsigned int i = 0; i <
N; i++)
1695 vrbar += xi*xi/
pFluids[i]->reduce.rhobar;
1697 if (i == N-1){
break; }
1699 for (
unsigned int j = i+1; j <
N; j++)
1708 double xk = x[k], xi = x[i], beta_Y = (*beta)[k][i];
1709 return xk*(xk+xi)/(beta_Y*beta_Y*xk+xi)+xk*xi/(beta_Y*beta_Y*xk+xi)*(1-(xk+xi)/(beta_Y*beta_Y*xk+xi));
1713 double xk = x[k], xi = x[i], beta_Y = (*beta)[i][k];
1714 return xk*(xi+xk)/(beta_Y*beta_Y*xi+xk)+xi*xk/(beta_Y*beta_Y*xi+xk)*(1-beta_Y*beta_Y*(xi+xk)/(beta_Y*beta_Y*xi+xk));
1716 double GERG2008ReducingFunction::c_Y_ij(
int i,
int j, std::vector< std::vector< double> > * beta, std::vector< std::vector< double> > *gamma, std::vector< std::vector< double> > *Y_c)
1718 return 2*((*beta)[i][j])*((*gamma)[i][j])*((*Y_c)[i][j]);
1720 double GERG2008ReducingFunction::c_Y_ji(
int j,
int i, std::vector< std::vector< double> > * beta, std::vector< std::vector< double> > *gamma, std::vector< std::vector< double> > *Y_c)
1722 return 2/((*beta)[i][j])*((*gamma)[i][j])*((*Y_c)[i][j]);
1726 double xi = x[i], xj = x[j], beta_Y = (*beta)[i][j];
1727 return xi*xj*(xi+xj)/(beta_Y*beta_Y*xi+xj);
1731 double xi = x[i], xk = x[k], beta_Y = (*beta)[i][k];
1732 return 1/(beta_Y*beta_Y*xi+xk)*(1-beta_Y*beta_Y*(xi+xk)/(beta_Y*beta_Y*xi+xk))*(2*xk-xi*xk*2*beta_Y*beta_Y/(beta_Y*beta_Y*xi+xk));
1736 double xi = x[i], xk = x[k], beta_Y = (*beta)[k][i];
1737 return 1/(beta_Y*beta_Y*xk+xi)*(1-(xk+xi)/(beta_Y*beta_Y*xk+xi))*(2*xk-xk*xi*2/(beta_Y*beta_Y*xk+xi));
1741 double xi = x[i], xj = x[j], beta_Y = (*beta)[i][j], beta_Y2 = beta_Y*beta_Y;
1742 return (xi+xj)/(beta_Y2*xi+xj) + xj/(beta_Y2*xi+xj)*(1-(xi+xj)/(beta_Y2*xi+xj))
1743 +xi/(beta_Y2*xi+xj)*(1-beta_Y2*(xi+xj)/(beta_Y2*xi+xj))
1744 -xi*xj/pow(beta_Y2*xi+xj,(
int)2)*(1+beta_Y2-2*beta_Y2*(xi+xj)/(beta_Y2*xi+xj));
1748 beta_v[i][j] = m.find(
"betaV")->second;
1749 beta_T[i][j] = m.find(
"betaT")->second;
1751 gamma_v[i][j] = m.find(
"gammaV")->second;
1752 gamma_T[i][j] = m.find(
"gammaT")->second;
1912 std::vector<double> n = m.find(
"n")->second;
1913 std::vector<double> t = m.find(
"t")->second;
1914 std::vector<double> d = m.find(
"d")->second;
1915 std::vector<double> eta = m.find(
"eta")->second;
1916 std::vector<double> epsilon = m.find(
"epsilon")->second;
1917 std::vector<double> beta = m.find(
"beta")->second;
1918 std::vector<double> gamma = m.find(
"gamma")->second;
1920 unsigned int iStart;
1923 if (
double_equal(std::accumulate(eta.begin(), eta.end(), 0.0),0.0))
1926 iStart = eta.size()-1;
1932 while (eta[iStart+1] < 0.25)
1966 double sum =
phi1.
base(tau, delta);
2027 double sum =
phi1.
dTau(tau, delta);
2053 std::vector<double> n = m.find(
"n")->second;
2054 std::vector<double> t = m.find(
"t")->second;
2055 std::vector<double> d = m.find(
"d")->second;
2056 std::vector<double> l = m.find(
"l")->second;
2064 double xi_ij = m.find(
"xi")->second;
2065 double zeta_ij = m.find(
"zeta")->second;
2069 double v_i = 1/
pFluids[i]->reduce.rhobar;
2070 double v_j = 1/
pFluids[j]->reduce.rhobar;
2071 double one_third = 0.33333333333333333;
2072 gamma_v[i][j] = (v_i + v_j + zeta_ij)/(0.25*pow(pow(v_i, one_third)+pow(v_j, one_third),(int)3));
2077 F.resize(N,std::vector<double>(N,1.0));
2083 for (
unsigned int i = 0; i <
N; i++)
2085 for (
unsigned int j = 0; j <
N; j++)
2098 for (
unsigned int i = 0; i <
N-1; i++)
2100 for (
unsigned int j = i + 1; j <
N; j++)
2110 for (
unsigned int i = 0; i <
N-1; i++)
2112 for (
unsigned int j = i + 1; j <
N; j++)
2122 for (
unsigned int i = 0; i <
N-1; i++)
2124 for (
unsigned int j = i + 1; j <
N; j++)
2134 for (
unsigned int i = 0; i <
N-1; i++)
2136 for (
unsigned int j = i + 1; j <
N; j++)
2146 for (
unsigned int i = 0; i <
N-1; i++)
2148 for (
unsigned int j = i + 1; j <
N; j++)
2158 for (
unsigned int i = 0; i <
N-1; i++)
2160 for (
unsigned int j = i + 1; j <
N; j++)
2170 for (
unsigned int k = 0; k <
N; k++)
2193 for (
unsigned int k = 0; k <
N; k++)
2205 for (
unsigned int k = 0; k <
N; k++)
2222 this->
N = pFluids.size();
2227 for (
unsigned int i = 0; i <
N; i++)
2229 summer += x[i]*
pFluids[i]->phir(tau,delta);
2236 for (
unsigned int i = 0; i <
N; i++)
2238 summer += x[i]*
pFluids[i]->dphir_dDelta(tau,delta);
2245 for (
unsigned int i = 0; i <
N; i++)
2247 summer += x[i]*
pFluids[i]->d2phir_dDelta2(tau,delta);
2254 for (
unsigned int i = 0; i <
N; i++)
2256 summer += x[i]*
pFluids[i]->d2phir_dDelta_dTau(tau,delta);
2263 for (
unsigned int i = 0; i <
N; i++)
2265 summer += x[i]*
pFluids[i]->dphir_dTau(tau,delta);
2272 for (
unsigned int i = 0; i <
N; i++)
2274 summer += x[i]*
pFluids[i]->d2phir_dTau2(tau,delta);
2281 for (
unsigned int k = 0; k <
N; k++)
2290 for (
unsigned int k = 0; k <
N; k++)
2298 double summer_term1 = 0;
2299 for (
unsigned int j = 0; j <
N; j++)
2308 double summer_term1 = 0;
2309 for (
unsigned int j = 0; j <
N; j++)
2319 double change, f, dfdT, rhobar_liq_new, rhobar_vap_new;
2320 unsigned int N = z.size();
2337 double tau_liq = Tr_liq/T;
2338 double tau_vap = Tr_vap/T;
2348 for (
unsigned int i=0; i < N; i++)
2361 f += z[i]*(K[i]-1)/(1-beta+beta*K[i]);
2363 double dfdK = K[i]*z[i]/pow(1-beta+beta*K[i],(
int)2);
2364 dfdT += dfdK*(dln_phi_liq_dT-dln_phi_vap_dT);
2381 if (fabs(change) > 3)
2387 while(fabs(f) > 1e-3 && iter <
Nstep_max);
2414 J.resize(N+2, std::vector<double>(N+2, 0));
2424 double NewtonRaphsonVLE::call(
double beta,
double T,
double p,
double rhobar_liq,
double rhobar_vap,
const std::vector<double> &z, std::vector<double> & K,
int spec_index,
double spec_value)
2438 std::vector<double> old_K =
K;
2452 for (
unsigned int i = 0; i <
N; i++)
2454 K[i] = exp(log(K[i]) + v[i]);
2460 T = exp(log(T) + v[N]);
2461 p = exp(log(p) + v[N+1]);
2463 if (fabs(T) > 1e6 || fabs(p) > 1e10)
2467 throw ValueError(
"Temperature or p has bad value");
2506 std::cout <<
format(
"Phase inversion");
2518 double tau_liq = Tr_liq/
T;
2519 double tau_vap = Tr_vap/
T;
2523 double delta_liq = this->
rhobar_liq/rhorbar_liq;
2524 double delta_vap = this->
rhobar_vap/rhorbar_vap;
2534 for (
unsigned int i = 0; i <
N; i++)
2539 for (
unsigned int j = 0; j <
N; j++)
2547 for (
unsigned int j = 0; j <
N; j++)
2552 r[i] = ln_phi_vap - ln_phi_liq + log(K[i]);
2553 for (
unsigned int j = 0; j <
N; j++)
2558 J[i][
N] = T*(phi_iT_vap-phi_iT_liq);
2560 J[i][N+1] = p*(phi_ip_vap-phi_ip_liq);
2564 for (
unsigned int i = 0; i <
N; i++)
2569 summer1 += z[i]*(K[i]-1)/(1-beta+beta*K[i]);
2575 for (
unsigned int j = 0; j <
N; j++)
2577 J[
N][j] = K[j]*z[j]/pow(1-beta+beta*K[j],(
int)2);
2581 if (spec_index == N)
2582 {
r[N+1] = log(T) - spec_value;}
2583 else if (spec_index == N+1)
2584 {
r[N+1] = log(p) - spec_value;}
2586 {
r[N+1] = log(K[spec_index]) - spec_value;}
2589 for (
unsigned int i = 0; i < N+2; i++)
2597 for (
unsigned int i = 0; i < N+2; i++)
2615 double S, Sold, DELTAS, abs_Kdeviation;
2626 for (
unsigned int betaI = 0; betaI < 2; betaI++)
2629 double beta = (betaI==0) ? beta_envelope : 1-beta_envelope;
2647 double lnT = log(T);
2648 double lnP = log(p0);
2651 double max_abs_val = -1;
2653 for (
unsigned int i = 0; i <
Mix->
N+1; i++)
2662 if (i_S >= (
int)
Mix->
N)
2698 bool step_accepted =
false;
2702 std::vector<double> Kold =
K;
2705 while (step_accepted ==
false)
2708 std::vector<double> DELTAX(dXdSold.size()), dXdS = dXdSold;
2713 for (
unsigned int i = 0; i <
Mix->
N+2; i++)
2715 DELTAX[i] = dXdS[i]*DELTAS;
2719 double DELTALNT = DELTAX[
Mix->
N];
2720 T = exp(log(Told)+DELTALNT);
2724 double DELTALNP = DELTAX[
Mix->
N+1];
2725 p = exp(log(pold)+DELTALNP);
2729 for (
unsigned int i = 0; i <
Mix->
N; i++)
2731 K[i] = exp(log(Kold[i])+DELTAX[i]);
2741 if (data.
T.size() > 2)
2743 int M = data.
T.size();
2750 else if (data.
T.size() > 3)
2752 int M = data.
T.size();
2762 data.
lnT[M-4], data.
lnT[M-3], data.
lnT[M-2], data.
lnT[M-1]);
2768 data.
lnp[M-4], data.
lnp[M-3], data.
lnp[M-2], data.
lnp[M-1]);
2819 printf(
"error: %s\n",e.
what());
2821 printf(
"Failure; step downsize\n");
2833 d.
add(
"color",
"r");
2834 d.
add(
"linestyle",
"-");
2835 d.
add(
"marker",
"o");
2846 step_accepted =
true;
2850 std::cout <<
"Downsizing, too many iterations\n";
2858 double _max_abs_val = -1;
2860 for (
unsigned int i = 0; i <
Mix->
N+1; i++)
2882 std::vector<double> Kdeviation(
K.size(),0);
2883 for (
unsigned int i = 0; i <
Mix->
N; i++)
2885 Kdeviation[i] = std::abs(
K[i]-1);
2887 abs_Kdeviation = *std::max_element(Kdeviation.begin(), Kdeviation.end());
2889 while ((p > p0 && abs_Kdeviation > 0.01 && iter < 10000) || iter < 3);
2896 for (
unsigned int i = 0; i < 2; i++)
2902 fname = base_fname+std::string(
"_bubble.py");
2905 fname = base_fname+std::string(
"_dew.py");
2907 fp = fopen(fname.c_str(),
"w");
2909 fprintf(fp,
"import matplotlib.pyplot as plt\n");
2914 fprintf(fp,
"K0 = %s\n",
vec_to_string(data.
K[0],
"%10.9g").c_str());
2915 fprintf(fp,
"K1 = %s\n",
vec_to_string(data.
K[1],
"%10.9g").c_str());
2918 fprintf(fp,
"if __name__=='__main__':\n\tplt.plot(T,p,'o-')\n\tplt.show()");
2919 fprintf(fp,
"\n\tplt.plot(K0,p,'o-')\n\tplt.show()");
2928 #ifndef DISABLE_CATCH
2932 Mixture Mix(
"Methane|Ethane");
2933 std::vector<double> z(2,0.5);
2937 double epsT = sqrt(
DBL_EPSILON), epsp = 0.1, epsz = 1e-5;
2938 double T0 = 300, p0 = 100000;
2940 double delta0 = rho0/rhorbar;
2941 double tau0 = Tr/T0;
2943 SECTION(
"check against RP9.1")
2949 double p1 = rho0*Mix.
Rbar(z)*T0*(1+delta0*Mix.
dphir_dDelta(tau0, delta0, z));
2952 REQUIRE(fabs(p0/p1-1) < 1e-8);
2958 double T1 = T0+epsT;
2959 double delta1 = Mix.
rhobar_Tpz(T1,p0,z, 5)/rhorbar;
2960 double tau1 = Tr/T1;
2963 double T2 = T0-epsT;
2964 double delta2 = Mix.
rhobar_Tpz(T2,p0,z, 5)/rhorbar;
2965 double tau2 = Tr/T2;
2968 double NUM = (f1-f2)/(2*epsT);
2972 REQUIRE(fabs(NUM/ANA-1) < 1e-8);
2978 double p1 = p0+epsp;
2979 double delta1 = Mix.
rhobar_Tpz(T0,p1,z, 5)/rhorbar;
2980 double tau1 = Tr/T0;
2983 double p2 = p0-epsp;
2984 double delta2 = Mix.
rhobar_Tpz(T0,p2,z, 5)/rhorbar;
2985 double tau2 = Tr/T0;
2988 double NUM = (f1-f2)/(2*epsp);
2992 REQUIRE(fabs(NUM/ANA-1) < 1e-7);
2994 SECTION(
"dln_fugacity_coefficient_dxj__constT_p_xi")
2996 for (
int i = 0; i < 2; i++)
2998 for (
int j = 0; j < 2; j++)
3002 std::vector<double> z1 = z;
3008 std::vector<double> z2 = z;
3014 double NUM = (f1-f2)/(2*epsz);
3020 CHECK(fabs(NUM-ANA) < 1e-7);
3024 SECTION(
"d_ndphirdni_dxj__constdelta_tau_xi")
3026 for (
int i = 0; i < 2; i++)
3028 for (
int j = 0; j < 2; j++)
3031 std::vector<double> z1 = z, z2 = z;
3039 double NUM = (f1-f2)/(2*epsz);
3044 CHECK(fabs(NUM/ANA-1) < 1e-10);
3048 SECTION(
"dphir_dxj__constT_V_xi")
3050 for (
int j = 0; j < 2; j++)
3054 std::vector<double> z1 = z;
3058 double f1 = Mix.
phir(tau1, delta1, z1);
3060 std::vector<double> z2 = z;
3064 double f2 = Mix.
phir(tau2, delta2, z2);
3066 double NUM = (f1-f2)/(2*epsz);
3070 CHECK(fabs(NUM/ANA-1) < 1e-9);
3073 SECTION(
"d_dphirddelta_dxj__constT_V_xi")
3075 for (
int j = 0; j < 2; j++)
3079 std::vector<double> z1 = z;
3085 std::vector<double> z2 = z;
3091 double NUM = (f1-f2)/(2*epsz);
3095 CHECK(fabs(NUM/ANA-1) < 1e-9);
3098 SECTION(
"d2nphir_dni_dxj__constT_V")
3100 for (
int i = 0; i < 2; i++)
3102 for (
int j = 0; j < 2; j++)
3106 std::vector<double> z1 = z;
3112 std::vector<double> z2 = z;
3118 double NUM = (f1-f2)/(2*epsz);
3122 CHECK(fabs(NUM/ANA-1) < 1e-8);
3128 for (
int j = 0; j < 2; j++)
3132 std::vector<double> z1 = z;
3136 double p1 = rho0*Mix.
Rbar(z1)*T0*(1+delta1*Mix.
dphir_dDelta(tau1,delta1,z1));
3138 std::vector<double> z2 = z;
3142 double p2 = rho0*Mix.
Rbar(z2)*T0*(1+delta2*Mix.
dphir_dDelta(tau2,delta2,z2));
3144 double NUM = (p1-p2)/(2*epsz);
3148 CHECK(fabs(NUM/ANA-1) < 1e-9);
3151 SECTION(
"ddelta_dxj__constT_V_xi")
3153 for (
int j = 0; j < 2; j++)
3157 std::vector<double> z1 = z;
3161 std::vector<double> z2 = z;
3165 double NUM = (f1-f2)/(2*epsz);
3169 CHECK(fabs(NUM/ANA-1) < 1e-10);
3172 SECTION(
"ddtau_dxj__constT_V_xi")
3174 for (
int j = 0; j < 2; j++)
3178 std::vector<double> z1 = z;
3182 std::vector<double> z2 = z;
3186 double NUM = (f1-f2)/(2*epsz);
3190 CHECK(fabs(NUM/ANA-1) < 1e-10);
double d_ndphirdni_dxj__constdelta_tau_xi(double tau, double delta, const std::vector< double > &x, int i, int j)
double ndtaudni__constT_V_nj(double tau, double delta, const std::vector< double > &x, int i)
Fluid * get_fluid(long iFluid)
double rhobar_pengrobinson(double T, double p, const std::vector< double > &x, int solution)
double f_Y_ij(const std::vector< double > &x, int i, int j, std::vector< std::vector< double > > *beta)
double nd2nphirdnidnj__constT_V(double tau, double delta, const std::vector< double > &x, int i, int j)
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string.
std::vector< double > phi_ij_vap
double d2phir_dxi_dDelta(double tau, double delta, const std::vector< double > &x, unsigned int i)
void build_arrays(double beta, double T, double p, const std::vector< double > &z, std::vector< double > &K, int spec_index, double spec_value)
void set_coeffs_from_map(int i, int j, std::map< std::string, double >)
Set the coefficients based on reducing parameters loaded from JSON.
double dphir_dxi(double tau, double delta, const std::vector< double > &x, int i)
double dphir_dDelta(double tau, double delta, const std::vector< double > &x)
ValueIterator Begin()
Element iterator.
double dln_fugacity_coefficient_dxj__constT_p_xi(double tau, double delta, const std::vector< double > &x, int i, int j)
Gernert Equation 3.115.
double ddelta_dxj__constT_V_xi(double tau, double delta, const std::vector< double > &x, int j)
DepartureFunctionCacheElement dphir_dDelta
double saturation_p_preconditioner(double p, const std::vector< double > &z)
std::vector< Fluid * > pFluids
std::vector< std::vector< double > > K
double rhobar_vap
The molar density of the vapor phase [mol/m^3].
double ndTrdni__constnj(const std::vector< double > &x, int i)
std::vector< double > dXdS
std::string mixture_excess_JSON
double d2phir_dDelta_dTau(double tau, double delta)
double dphir_dTau(double tau, double delta, const std::vector< double > &x)
double g_RachfordRice(const std::vector< double > &z, const std::vector< double > &lnK, double beta)
long get_Fluid_index(std::string FluidName)
double dphir_dTau(double tau, double delta, const std::vector< double > &x)
double call(double rhobar)
std::vector< double > lnp
void x_and_y_from_K(double beta, const std::vector< double > &K, const std::vector< double > &z, std::vector< double > &x, std::vector< double > &y)
double d2rhorbardxi2__constxj(const std::vector< double > &x, int i)
const std::vector< double > * z
double d_ndphirdni_dDelta(double tau, double delta, const std::vector< double > &x, int i)
double d2fYijdxidxj(const std::vector< double > &x, int i, int k, std::vector< std::vector< double > > *beta)
double ndln_fugacity_coefficient_dnj__constT_p(double tau, double delta, const std::vector< double > &x, int i, int j)
Set the coefficients based on reducing parameters loaded from JSON void set_coeffs_from_map(int i, int j, std::map< std::string, double >)
Set the coefficients based on reducing parameters loaded from JSON.
ReducingFunction * pReducing
double saturation_p(double beta, double p, const std::vector< double > &z, std::vector< double > &x, std::vector< double > &y)
std::vector< double > rhobar_vap
double dDelta(double tau, double delta)
double dDelta_dTau(double tau, double delta)
double d_ndphirdni_dxj__constT_V_xi(double tau, double delta, const std::vector< double > &x, int i, int j)
double dvrbardxi__constxj(const std::vector< double > &x, int i)
virtual double d2rhorbardxidxj(const std::vector< double > &x, int i, int j)=0
double d2phir_dDelta2(double tau, double delta, const std::vector< double > &x)
phir_GERG2008_gaussian phi2
double dtau_dxj__constT_V_xi(double tau, double delta, const std::vector< double > &x, int j)
double d2phir_dDelta2(double tau, double delta)
const std::vector< double > * lnK
double dDelta(double tau, double delta)
double c_Y_ij(int i, int j, std::vector< std::vector< double > > *beta, std::vector< std::vector< double > > *gamma, std::vector< std::vector< double > > *Y_c)
double d_ndTrdni_dxj__constxi(const std::vector< double > &x, int i, int j)
double dnphir_dni__constT_V_nj(double tau, double delta, const std::vector< double > &x, int i)
double dTau(double tau, double delta)
double ndrhorbardni__constnj(const std::vector< double > &x, int i)
double d2phir_dTau2(double tau, double delta, const std::vector< double > &x)
SuccessiveSubstitutionVLE SS
double dDelta_dTau(double tau, double delta)
virtual double rhorbar(const std::vector< double > &x)=0
The molar reducing density.
DepartureFunctionCacheElement dphir_dTau
double dDelta2(double tau, double delta)
double nddeltadni__constT_V_nj(double tau, double delta, const std::vector< double > &x, int i)
double fugacity(double tau, double delta, const std::vector< double > &x, int i)
Returns the fugacity for the given component for the given total reduced density and reciprocal reduc...
double d2vrbardxi2__constxj(const std::vector< double > &x, int i)
double d2nphir_dni_dT(double tau, double delta, const std::vector< double > &x, int i)
std::vector< std::vector< double > > lnK
gRR_resid(Mixture *Mix, std::vector< double > const &z, std::vector< double > const &lnK)
double d2nphir_dni_dxj__constT_V(double tau, double delta, const std::vector< double > &x, int i, int j)
Gernert Equation 3.117.
double dgdbeta_RachfordRice(const std::vector< double > &z, const std::vector< double > &lnK, double beta)
Mixture * Mix
Pointer to the Mixture class instance.
int Nstep_max
The maximum number of steps to take, can be changed as needed.
double d2phir_dDelta2(double tau, double delta, const std::vector< double > &x)
std::vector< std::vector< double > > F
std::vector< std::vector< DepartureFunction * > > DepartureFunctionMatrix
double phir(double tau, double delta, const std::vector< double > &x)
ResidualIdealMixture(std::vector< Fluid * > pFluids)
std::vector< std::vector< double > > y
double dphir_dTau(double tau, double delta)
double d2phir_dTau2(double tau, double delta)
void check_WaterEthanol()
The derivative of reduced temperature with respect to component i mole fraction double dTrdxi__constxj(const std::vector< double > &x, int i)
The derivative of reduced temperature with respect to component i mole fraction.
double d2Trdxidxj(const std::vector< double > &x, int i, int j)
void build(double p0, const std::vector< double > &z, double beta_envelope)
double dln_fugacity_coefficient_dp__constT_n(double tau, double delta, const std::vector< double > &x, int i)
double d2phir_dTau2(double tau, double delta)
void resize(unsigned int N)
void add(std::string key, double value)
Add a floating point value to the dictionary.
double dphir_dDelta(double tau, double delta)
std::string mixture_reducing_JSON
double deriv(double beta)
double dpdT__constV_n(double tau, double delta, const std::vector< double > &x, int i)
double d2phir_dDelta_dTau(double tau, double delta)
virtual void set_coeffs_from_map(int i, int j, std::map< std::string, double >)=0
Set the coefficients based on reducing parameters loaded from JSON.
DepartureFunctionCache cache
double rhobar_liq
The molar density of the liquid phase [mol/m^3].
double dfYikdxi__constxk(const std::vector< double > &x, int i, int k, std::vector< std::vector< double > > *beta)
std::vector< std::vector< double > > STLMatrix
TEST_CASE("Mixture derivative checks","")
STLMatrix gamma_T
from GERG-2008
double Wilson_lnK_factor(double T, double p, int i)
void TpzFlash(double T, double p, const std::vector< double > &z, double &rhobar, std::vector< double > &x, std::vector< double > &y)
std::vector< double > ln_phi_liq
void store_variables(const double T, const double p, const double rhobar_liq, const double rhobar_vap, const std::vector< double > &K, const int iS, const std::vector< double > &x, const std::vector< double > &y, const unsigned int N)
std::map< std::string, double > load_reducing_values(int i, int j)
virtual double Tr(const std::vector< double > &x)=0
The reduced temperature.
double d2fYkidxi2__constxk(const std::vector< double > &x, int k, int i, std::vector< std::vector< double > > *beta)
std::vector< double > x(ncmax, 0)
double dphir_dDelta(double tau, double delta, const std::vector< double > &x)
const std::vector< double > * z
double dphir_dTau(double tau, double delta)
double ndpdV__constT_n(double tau, double delta, const std::vector< double > &x, int i)
ResidualIdealMixture * pResidualIdealMix
DepartureFunctionCache cache
std::vector< std::vector< double > > linsolve(std::vector< std::vector< double > > const &A, std::vector< std::vector< double > > const &B)
double d2phir_dDelta2(double tau, double delta)
std::vector< Fluid * > pFluids
double d2phir_dDelta_dTau(double tau, double delta, const std::vector< double > &x)
double evaluate(double x)
double dTau2(double tau, double delta)
std::vector< double > lnT
double d2phir_dTau2(double tau, double delta, const std::vector< double > &x)
double phir(double tau, double delta)
double d_dphirddelta_dxj__constT_V_xi(double tau, double delta, const std::vector< double > &x, int j)
DepartureFunctionCacheElement d2phir_dTau2
double rhobar_Tpz(double T, double p, const std::vector< double > &z, double rhobar0)
A document for parsing JSON text as DOM.
double dDelta2(double tau, double delta)
double dphir_dxi(double tau, double delta, const std::vector< double > &x, unsigned int i)
void add_4value_constraints(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4)
double Rbar(const std::vector< double > &x)
double ndpdni__constT_V_nj(double tau, double delta, const std::vector< double > &x, int i)
std::vector< double > rhobar_liq
virtual double drhorbardxi__constxj(const std::vector< double > &x, int i)=0
Derivative of the molar reducing density with respect to component i mole fraction.
A wrapper function around the density(T,p,x) residual.
double Newton(FuncWrapper1D *f, double x0, double ftol, int maxiter, std::string *errstring)
double dln_fugacity_coefficient_dT__constp_n(double tau, double delta, const std::vector< double > &x, int i)
std::vector< double > lnrhobar_vap
double d2phir_dDelta_dTau(double tau, double delta, const std::vector< double > &x)
virtual double d2Trdxidxj(const std::vector< double > &x, int i, int j)=0
std::string vec_to_string(double const &a)
void set_coeffs_from_map(std::map< std::string, std::vector< double > >)
double d2vrbardxidxj(const std::vector< double > &x, int i, int j)
double d2Trdxi2__constxj(const std::vector< double > &x, int i)
void to_python_files(std::string fname)
virtual double dTrdxi__constxj(const std::vector< double > &x, int i)=0
The derivative of reduced temperature with respect to component i mole fraction.
bool has_string_array_member(const rapidjson::Value &a, const char *member)
double d2phir_dDelta2(double tau, double delta, const std::vector< double > &x)
std::vector< Fluid * > pFluids
List of pointers to fluids.
double dfYkidxi__constxk(const std::vector< double > &x, int k, int i, std::vector< std::vector< double > > *beta)
DepartureFunctionCacheElement d2phir_dDelta2
double d_ndphirdni_dTau(double tau, double delta, const std::vector< double > &x, int i)
double dpdxj__constT_V_xi(double tau, double delta, const std::vector< double > &x, int j)
REQUIRE(fabs(CPWater.p()-RPWater.p())< 1e-4)
double dphir_dDelta(double tau, double delta, const std::vector< double > &x)
double dTau2(double tau, double delta)
void load_excess_values(int i, int j)
double d2phirdxidxj(double tau, double delta, const std::vector< double > &x, int i, int j)
std::vector< double > neg_dFdS
double d2phir_dxi_dTau(double tau, double delta, const std::vector< double > &x, int i)
A wrapper function around the Rachford-Rice residual.
double dphir_dTau(double tau, double delta, const std::vector< double > &x)
double partial_molar_volume(double tau, double delta, const std::vector< double > &x, int i)
STLMatrix beta_T
from GERG-2008
double base(double tau, double delta)
std::vector< double > phi_ij_liq
std::vector< double > JSON_double_array(const rapidjson::Value &a)
double Secant(FuncWrapper1D *f, double x0, double dx, double tol, int maxiter, std::string *errstring)
The reduced temperature double Tr(const std::vector< double > &x)
The reduced temperature.
bool HasMember(const Ch *name) const
Check whether a member exists in the object.
double dln_fugacity_coefficient_dT__constrho(double tau, double delta, const std::vector< double > &x, int i)
const std::vector< double > * x
double d2rhorbardxidxj(const std::vector< double > &x, int i, int j)
DepartureFunctionCacheElement phir
double d2phir_dDelta_dTau(double tau, double delta, const std::vector< double > &x)
void set_coeffs_from_map(std::map< std::string, std::vector< double > >)
double saturation_p_Wilson(double beta, double p, const std::vector< double > &z, double T_guess, std::vector< double > &K)
double phir(double tau, double delta, const std::vector< double > &x)
Mixture(std::vector< Fluid * > pFluids)
double d2fYikdxi2__constxk(const std::vector< double > &x, int i, int k, std::vector< std::vector< double > > *beta)
#define SECTION(name, description)
STLMatrix beta_v
from GERG-2008
void check_MethaneEthane()
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
void set_coeffs_from_map(int i, int j, std::map< std::string, std::vector< double > >)
std::vector< Fluid * > pFluids
List of pointers to fluids.
double call(double beta, double T, double p, double rhobar_liq, double rhobar_vap, const std::vector< double > &z, std::vector< double > &K, int spec_index, double spec_value)
STLMatrix gamma_v
from GERG-2008
double d_ndrhorbardni_dxj__constxi(const std::vector< double > &x, int i, int j)
void normalize_vector(std::vector< double > &x)
std::vector< std::string > JSON_string_array(const rapidjson::Value &a)
double call(double beta, double T, double p, const std::vector< double > &z, std::vector< double > &K)
bool match_CAS_entry(std::vector< std::string > CAS1, std::vector< std::string > CAS2, std::string FluidiCAS, std::string FluidjCAS)
double c_Y_ji(int j, int i, std::vector< std::vector< double > > *beta, std::vector< std::vector< double > > *gamma, std::vector< std::vector< double > > *Y_c)
rho_Tpz_resid(Mixture *Mix, double T, double p, const std::vector< double > &x)
double deriv(double rhobar)
double d2phir_dTau2(double tau, double delta, const std::vector< double > &x)
double phir(double tau, double delta)
const Ch * GetString() const
double phir(double tau, double delta, const std::vector< double > &x)
std::vector< std::vector< double > > x
The molar reducing density double rhorbar(const std::vector< double > &x)
The molar reducing density.
Derivative of the molar reducing density with respect to component i mole fraction double drhorbardxi__constxj(const std::vector< double > &x, int i)
Derivative of the molar reducing density with respect to component i mole fraction.
double d2phirdxidxj(double tau, double delta, const std::vector< double > &x, unsigned int i, unsigned int j)
double base(double tau, double delta)
double dphir_dxj__constT_V_xi(double tau, double delta, const std::vector< double > &x, int j)
double d2phir_dxi_dDelta(double tau, double delta, const std::vector< double > &x, int i)
double dphir_dDelta(double tau, double delta)
double ln_fugacity_coefficient(double tau, double delta, const std::vector< double > &x, int i)
bool useNR
If true (default is false), will call Newton-Raphson after either solving to tolerance, or taking Nmax steps.
double ndphir_dni__constT_V_nj(double tau, double delta, const std::vector< double > &x, int i)
double d2phir_dxi_dTau(double tau, double delta, const std::vector< double > &x, unsigned int i)
DepartureFunctionCacheElement d2phir_dDelta_dTau
WilsonK_resid(Mixture *Mix, double beta, double p, std::vector< double > const &z)
std::vector< double > ln_phi_vap
double dTau(double tau, double delta)
virtual const char * what() const
void plot(std::vector< double > x, std::vector< double > y, Dictionary *dict=NULL)