// File: /workspace/github_cpp_makefile_star2_O2/JorisCaze[P]NASG-EOS_Calibration/src/calib_liq-vap.cpp
// Function: computePinfL(std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>> const&, double, double, double)
double computePinfL(vector<double> const& psatExp, vector<double> const& Texp, vector<double> const& vLexp, vector<double> const& hLexp, double p0, double ro0, double c0)
{
    // Purpose : compute pinfL parameter of liquid phase using the Newton-Raphson procedure
    // More : liquid reference state and experimental data are used
    // See eq. (68)
    double fp, dfp, dfp1, dfp2, pinf1(2.e5), pinf2(0.), err(1.);
    double mp, mT, mvL, mhL, mTp, mTp2;
    double diffC, bL, cpL, dDiffC, dbL, dcpL;
    int count(0);

    mp = meanValue(psatExp);
    mT = meanValue(Texp);
    mvL = meanValue(vLexp);
    mhL = meanValue(hLexp);

    while (err > 1.e-5 && count < 50) {
        mTp = computeMeanTp(psatExp,Texp,pinf1);
        mTp2 = computeMeanTp2(psatExp,Texp,pinf1);

        diffC = computeHeatCapDiffL(psatExp,Texp,vLexp,mvL,mTp,pinf1);
        bL = computebL(mvL,mTp,diffC);
        cpL = computeCpL(Texp,hLexp,psatExp,mhL,mp,mT,bL);
        
        dDiffC = computeDHeatCapDiffL(psatExp,Texp,vLexp,mvL,mTp,mTp2,pinf1);
        dbL = computeDbL(mTp,mTp2,diffC,dDiffC); 
        dcpL = computeDcpL(Texp,psatExp,mp,mT,dbL); 
        
        dfp1 = -ro0*dbL;
        dfp2 = (dDiffC*cpL-diffC*dcpL)/(cpL*cpL);

        fp = p0 + pinf1 - (1.-diffC/cpL)*ro0*c0*c0*(1.-bL*ro0);
        dfp = 1. - ro0*c0*c0*dfp1 + dfp2*ro0*c0*c0*(1.-bL*ro0) + (diffC/cpL)*ro0*c0*c0*dfp1;

        pinf2 = pinf1 - fp/dfp;
        err = fabs(pinf2-pinf1)/(0.5*(pinf1+pinf2));
        pinf1 = pinf2;
        count++;
        if (count >= 50) {
            cout << "Warning : newton-raphson of Psat(T) function not converged\n"; exit(0);
        }
    }
    // cout << "Number of iteration NR pinfL : " << count << endl;
    return pinf1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xe0, %rsp
movaps %xmm2, 0x70(%rsp)
movaps %xmm1, 0x60(%rsp)
movsd %xmm0, 0x48(%rsp)
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
callq 0x464f
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
callq 0x464f
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
callq 0x464f
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
callq 0x464f
movsd %xmm0, 0x40(%rsp)
movapd 0x2531(%rip), %xmm0     # 0x5010
movapd 0x60(%rsp), %xmm2
xorpd %xmm2, %xmm0
movapd %xmm0, 0xc0(%rsp)
movapd 0x70(%rsp), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm2, %xmm0
unpcklpd %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
movapd %xmm0, 0xa0(%rsp)
movapd %xmm1, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movapd %xmm0, 0x90(%rsp)
movsd 0x2535(%rip), %xmm1     # 0x5068
movsd 0x2535(%rip), %xmm0     # 0x5070
pushq $0x32
popq %rbp
ucomisd 0x2532(%rip), %xmm0     # 0x5078
jbe 0x2d8c
movq %r12, %rdi
movq %r15, %rsi
movapd %xmm1, %xmm0
movsd %xmm1, 0x10(%rsp)
callq 0x276d
movsd %xmm0, 0x8(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x27b3
movaps %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movsd 0x18(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
callq 0x27fd
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, %xmm1
mulsd 0x8(%rsp), %xmm1
movsd 0x18(%rsp), %xmm3
subsd %xmm1, %xmm3
movsd %xmm3, 0x58(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movsd 0x40(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
callq 0x2990
movsd %xmm0, 0x50(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movsd 0x18(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movaps 0x30(%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
callq 0x2862
movapd %xmm0, 0xd0(%rsp)
movapd 0x30(%rsp), %xmm2
mulsd 0x80(%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm2
movapd %xmm2, 0x30(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
callq 0x2a05
movapd 0x80(%rsp), %xmm5
mulsd %xmm5, %xmm0
movapd 0xd0(%rsp), %xmm7
movsd 0x50(%rsp), %xmm1
mulsd %xmm1, %xmm7
subsd %xmm0, %xmm7
movapd 0x30(%rsp), %xmm4
mulsd 0xc0(%rsp), %xmm4
divsd %xmm1, %xmm5
mulsd %xmm1, %xmm1
divsd %xmm1, %xmm7
movapd 0x60(%rsp), %xmm2
movsd 0x58(%rsp), %xmm1
mulsd %xmm2, %xmm1
movsd 0x23cb(%rip), %xmm3     # 0x5070
movapd %xmm3, %xmm0
subsd %xmm1, %xmm0
movapd 0xb0(%rsp), %xmm1
mulsd %xmm4, %xmm1
movapd %xmm4, %xmm6
addsd %xmm3, %xmm1
movapd %xmm7, %xmm4
mulsd %xmm2, %xmm4
movapd 0x70(%rsp), %xmm2
mulsd %xmm2, %xmm4
mulsd %xmm2, %xmm4
mulsd %xmm0, %xmm4
addsd %xmm1, %xmm4
movapd %xmm4, %xmm2
movsd 0x10(%rsp), %xmm4
movapd %xmm4, %xmm1
addsd 0x48(%rsp), %xmm1
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
movapd %xmm3, %xmm2
subsd %xmm5, %xmm2
xorpd 0x2308(%rip), %xmm2     # 0x5010
unpcklpd %xmm5, %xmm2            # xmm2 = xmm2[0],xmm5[0]
unpcklpd %xmm6, %xmm0            # xmm0 = xmm0[0],xmm6[0]
mulpd 0xa0(%rsp), %xmm2
movapd 0x90(%rsp), %xmm3
mulpd %xmm3, %xmm2
mulpd %xmm3, %xmm2
mulpd %xmm2, %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
divsd %xmm1, %xmm0
movapd %xmm4, %xmm2
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
subsd %xmm4, %xmm0
andpd 0x22e6(%rip), %xmm0     # 0x5040
addsd %xmm1, %xmm2
mulsd 0x231a(%rip), %xmm2     # 0x5080
divsd %xmm2, %xmm0
decl %ebp
jne 0x2b3e
movq 0x525f(%rip), %rdi      # 0x7fd8
leaq 0x234e(%rip), %rsi      # 0x50ce
callq 0x2150
xorl %edi, %edi
callq 0x2190
movapd %xmm1, %xmm0
addq $0xe0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq