/*************************************************************************
	* 文件: detail.c
	* 描述: 该文件包含实现AGA报告No.8 1994 - 详细方法的函数，以及AGA报告No.10所需的新特性
	*       包含以下函数:
	*       Detail(), ~Detail(), compositionchange(), Run(), table(),	*       paramdl(), chardl(), braket(), bvir(), temp(), ddetail(),	*       pdetail(), zdetail(), relativedensity(), dZdT(), d2ZdT2(),	*       dZdD()  
	* 版本: ver 1.7 2002.11.17
	* 作者: W.B. Peterson
	* 修订:
	* 版权: (c) 2002 美国天然气协会
**************************************************************************/

#include "NGCal.h"
#include "Detail.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>

/**************************************************************************
	* 函数: Detail::Detail()
	* 参数: void
	* 返回: 
	* 目的: 默认构造函数; 包括初始化历史敏感变量和数据表4和6
	* 修订: 
**************************************************************************/
Detail* Detail_Construct(void)
{
    Detail* pDetail = (Detail*)malloc(sizeof(Detail));
    if (!pDetail) return NULL;
	
    memset(pDetail, 0, sizeof(Detail));
	
    // 初始化历史敏感变量
    pDetail->dOldMixID = 0.0;
    pDetail->dOldPb = 0.0;
    pDetail->dOldTb = 0.0;
    pDetail->dOldPf = 0.0;
    pDetail->dOldTf = 0.0;
	
    // 初始化该类内部使用的气体组分数组
    for (int i = 0; i < NUMBEROFCOMPONENTS; i++)
	pDetail->dXi[i] = 0;
	
    // 函数table()填充静态常量表
    Detail_table(pDetail);
    return pDetail;
}



/**************************************************************************
	* 函数: Detail::compositionchange()
	* 参数: AGA10STRUCT *
	* 返回: void
	* 目的: 通过创建半唯一数值ID比较新组成与旧组成。虽然可能性很小，但两个连续不同的组成可能产生相同的ID
	* 修订: 
**************************************************************************/
int Detail_compositionchange(Detail* pDetail, AGA10STRUCT *ptAGA10)
{
    double dMixID = 0.0;
    int i;
	
    // 为组成生成数值ID
    for (i = 0; i < NUMBEROFCOMPONENTS; i++)
	dMixID += ((i+2) * ptAGA10->adMixture[i]);
	
    // 如果与之前不同，则更新历史变量
    if (dMixID != pDetail->dOldMixID)
    {
        pDetail->dOldMixID = dMixID;
        return true;
	}
	else
	{
		return false;
	}
}

/**************************************************************************
	* 函数: Detail::Run()
	* 参数: AGA10STRUCT *
	* 返回: void
	* 目的: 公共方法，用于协调和运行完整的计算序列
	* 修订: 
**************************************************************************/
void Detail_Run(Detail* pDetail, AGA10STRUCT *ptAGA10)
{
    int i;
	
    // 检查气体组成变化
    bool bCompChange = Detail_compositionchange(pDetail, ptAGA10);
    ptAGA10->bForceUpdate = ptAGA10->bForceUpdate || bCompChange;
	
    // 分配组分ID和值
    if (ptAGA10->bForceUpdate)
    {
        pDetail->iNCC = 0;
        for (i = 0; i < NUMBEROFCOMPONENTS; i++)
        {
            if (ptAGA10->adMixture[i] > 0.0)
            {
                pDetail->aiCID[pDetail->iNCC] = i;
                pDetail->dXi[pDetail->iNCC] = ptAGA10->adMixture[i];
                pDetail->iNCC++;
			}
		}
		
        // 计算组成相关量
        Detail_paramdl(pDetail);
        Detail_chardl(pDetail, ptAGA10);
	}
	
    // 在基础压力和温度下评估T和P相关参数
    if ((fabs(ptAGA10->dPb - pDetail->dOldPb) > P_CHG_TOL) || 
        (fabs(ptAGA10->dTb - pDetail->dOldTb) > T_CHG_TOL) || 
	(ptAGA10->bForceUpdate))
    {
        pDetail->dP = ptAGA10->dPb * 1.0e-6;  // AGA 8内部使用MPa
        pDetail->dT = ptAGA10->dTb;
		
        // 计算温度相关参数
        Detail_temp(pDetail);
		
        // 确定摩尔密度
        Detail_ddetail(pDetail, ptAGA10);
        ptAGA10->dDb = pDetail->dRho;
		
        // 确定压缩系数
        ptAGA10->dZb = Detail_zdetail(pDetail, pDetail->dRho);
		
        // 计算质量密度
        pDetail->dRhoTP = (pDetail->dP * ptAGA10->dMrx) / (ptAGA10->dZb * RGASKJ * pDetail->dT);
		
        // 计算相对密度
        Detail_relativedensity(pDetail, ptAGA10);
		
        // 将密度复制到数据结构成员
        ptAGA10->dRhob = pDetail->dRhoTP;
		
        // 更新历史并清除ForceUpdate标志
        pDetail->dOldTb = ptAGA10->dTb;
        pDetail->dOldPb = ptAGA10->dPb;
        ptAGA10->bForceUpdate = true;
	}
	
    // 使用流动条件重复该过程
    pDetail->dP = ptAGA10->dPf * 1.0e-6;
    pDetail->dT = ptAGA10->dTf;
	
    // 检查是否需要计算温度相关参数
    if ((fabs(ptAGA10->dTf - pDetail->dOldTf) > T_CHG_TOL) || (ptAGA10->bForceUpdate))
    {
        Detail_temp(pDetail);
        ptAGA10->bForceUpdate = true;
	}
	
    // 检查是否需要计算其他参数
    if ((fabs(ptAGA10->dPf - pDetail->dOldPf) > P_CHG_TOL) || (ptAGA10->bForceUpdate))
    {
        // 确定摩尔密度
        Detail_ddetail(pDetail, ptAGA10);
        ptAGA10->dDf = pDetail->dRho;
		
        // 确定压缩系数
        ptAGA10->dZf = Detail_zdetail(pDetail, pDetail->dRho);
		
        // 计算质量密度
        pDetail->dRhoTP = (pDetail->dP * ptAGA10->dMrx) / (ptAGA10->dZf * RGASKJ * pDetail->dT);
        ptAGA10->dRhof = pDetail->dRhoTP;
		
        // 更新历史
        pDetail->dOldTf = ptAGA10->dTf;
        pDetail->dOldPf = ptAGA10->dPf;
	}
	
    // 计算传统因子Fpv
    if ((ptAGA10->dZb > 0.0) && (ptAGA10->dZf > 0.0))
	ptAGA10->dFpv = sqrt(ptAGA10->dZb / ptAGA10->dZf);
    else
	ptAGA10->lStatus = GENERAL_CALCULATION_FAILURE;
	
    ptAGA10->bForceUpdate = false;
}


/**************************************************************************
	* 函数: Detail::table()
	* 参数: void
	* 返回: void
	* 目的: 构建常量表
	* 修订: 
**************************************************************************/
void Detail_table(Detail* pDetail)
{
    // 表4中的58个常量 - 列A(n)
    const double adAn[58] = {
	0.153832600,1.341953000,-2.998583000,-0.048312280,0.375796500,-1.589575000,-0.053588470,0.886594630,-0.710237040,-1.471722000, 1.321850350, -0.786659250, 2.29129E-09, 0.157672400, -0.436386400, -0.044081590, -0.003433888, 0.032059050, 0.024873550, 0.073322790, -0.001600573, 0.642470600, -0.416260100, -0.066899570, 0.279179500, -0.696605100, -0.002860589, -0.008098836, 3.150547000, 0.007224479, -0.705752900, 0.534979200, -0.079314910, -1.418465000, -5.99905E-17, 0.105840200, 0.034317290, -0.007022847, 0.024955870, 0.042968180, 0.746545300, -0.291961300, 7.294616000, -9.936757000, -0.005399808, -0.243256700, 0.049870160, 0.003733797, 1.874951000, 0.002168144, -0.658716400, 0.000205518, 0.009776195, -0.020487080, 0.015573220, 0.006862415, -0.001226752, 0.002850908 };
	
    // 表4中的58个常量 - 列Un
    const double adUn[58] = {0.0,0.5,1.0,3.5,-0.5,4.5,0.5,7.5,9.5,6.0,12.0,12.5,-6.0,2.0,3.0,2.0,2.0,11.0,-0.5,0.5,0.0,4.0,6.0,21.0,23.0,22.0,-1.0,-0.5,7.0,-1.0,6.0,4.0,1.0,9.0,-13.0,21.0,8.0,-0.5,0.0,2.0,7.0,9.0,22.0,23.0,1.0,9.0,3.0,8.0,23.0,1.5,5.0,-0.5,4.0,7.0,3.0,0.0,1.0,0.0
	};
	
    memcpy(pDetail->adAn, adAn, sizeof(adAn));
    memcpy(pDetail->adUn, adUn, sizeof(adUn));
	
    // 初始化表6为1.0
    for (int j = 0; j < NUMBEROFCOMPONENTS; j++) {
        for (int k = j; k < NUMBEROFCOMPONENTS; k++) {
            pDetail->adTable6Eij[j][k] = 1.0;
            pDetail->adTable6Uij[j][k] = 1.0;
            pDetail->adTable6Kij[j][k] = 1.0;
            pDetail->adTable6Gij[j][k] = 1.0;
		}
	}
	
    // 设置表6的非1值
	pDetail->adTable6Eij[0][1] = 0.971640;
	pDetail->adTable6Eij[0][2] = 0.960644;
	pDetail->adTable6Eij[0][4] = 0.994635;
	pDetail->adTable6Eij[0][5] = 0.708218;
	pDetail->adTable6Eij[0][6] = 0.931484;
	pDetail->adTable6Eij[0][7] = 1.170520;
	pDetail->adTable6Eij[0][8] = 0.990126;
	pDetail->adTable6Eij[0][10] = 1.019530;
	pDetail->adTable6Eij[0][11] = 0.989844;
	pDetail->adTable6Eij[0][12] = 1.002350;
	pDetail->adTable6Eij[0][13] = 0.999268;
	pDetail->adTable6Eij[0][14] = 1.107274;
	pDetail->adTable6Eij[0][15] = 0.880880;
	pDetail->adTable6Eij[0][16] = 0.880973;
	pDetail->adTable6Eij[0][17] = 0.881067;
	pDetail->adTable6Eij[0][18] = 0.881161;
	pDetail->adTable6Eij[1][2] = 1.022740;
	pDetail->adTable6Eij[1][3] = 0.970120;
	pDetail->adTable6Eij[1][4] = 0.945939;
	pDetail->adTable6Eij[1][5] = 0.746954;
	pDetail->adTable6Eij[1][6] = 0.902271;
	pDetail->adTable6Eij[1][7] = 1.086320;
	pDetail->adTable6Eij[1][8] = 1.005710;
	pDetail->adTable6Eij[1][9] = 1.021000;
	pDetail->adTable6Eij[1][10] = 0.946914;
	pDetail->adTable6Eij[1][11] = 0.973384;
	pDetail->adTable6Eij[1][12] = 0.959340;
	pDetail->adTable6Eij[1][13] = 0.945520;
	pDetail->adTable6Eij[2][3] = 0.925053;
	pDetail->adTable6Eij[2][4] = 0.960237;
	pDetail->adTable6Eij[2][5] = 0.849408;
	pDetail->adTable6Eij[2][6] = 0.955052;
	pDetail->adTable6Eij[2][7] = 1.281790;
	pDetail->adTable6Eij[2][8] = 1.500000;
	pDetail->adTable6Eij[2][10] = 0.906849;
	pDetail->adTable6Eij[2][11] = 0.897362;
	pDetail->adTable6Eij[2][12] = 0.726255;
	pDetail->adTable6Eij[2][13] = 0.859764;
	pDetail->adTable6Eij[2][14] = 0.855134;
	pDetail->adTable6Eij[2][15] = 0.831229;
	pDetail->adTable6Eij[2][16] = 0.808310;
	pDetail->adTable6Eij[2][17] = 0.786323;
	pDetail->adTable6Eij[2][18] = 0.765171;
	pDetail->adTable6Eij[3][4] = 1.022560;
	pDetail->adTable6Eij[3][5] = 0.693168;
	pDetail->adTable6Eij[3][6] = 0.946871;
	pDetail->adTable6Eij[3][7] = 1.164460;
	pDetail->adTable6Eij[3][11] = 1.013060;
	pDetail->adTable6Eij[3][13] = 1.005320;
	pDetail->adTable6Eij[4][7] = 1.034787;
	pDetail->adTable6Eij[4][11] = 1.004900;
	pDetail->adTable6Eij[6][14] = 1.008692;
	pDetail->adTable6Eij[6][15] = 1.010126;
	pDetail->adTable6Eij[6][16] = 1.011501;
	pDetail->adTable6Eij[6][17] = 1.012821;
	pDetail->adTable6Eij[6][18] = 1.014089;
	pDetail->adTable6Eij[7][8] = 1.100000;
	pDetail->adTable6Eij[7][10] = 1.300000;
	pDetail->adTable6Eij[7][11] = 1.300000;
	pDetail->adTable6Uij[0][1] = 0.886106;
	pDetail->adTable6Uij[0][2] = 0.963827;
	pDetail->adTable6Uij[0][4] = 0.990877;
	pDetail->adTable6Uij[0][6] = 0.736833;
	pDetail->adTable6Uij[0][7] = 1.156390;
	pDetail->adTable6Uij[0][11] = 0.992291;
	pDetail->adTable6Uij[0][13] = 1.003670;
	pDetail->adTable6Uij[0][14] = 1.302576;
	pDetail->adTable6Uij[0][15] = 1.191904;
	pDetail->adTable6Uij[0][16] = 1.205769;
	pDetail->adTable6Uij[0][17] = 1.219634;
	pDetail->adTable6Uij[0][18] = 1.233498;
	pDetail->adTable6Uij[1][2] = 0.835058;
	pDetail->adTable6Uij[1][3] = 0.816431;
	pDetail->adTable6Uij[1][4] = 0.915502;
	pDetail->adTable6Uij[1][6] = 0.993476;
	pDetail->adTable6Uij[1][7] = 0.408838;
	pDetail->adTable6Uij[1][11] = 0.993556;
	pDetail->adTable6Uij[2][3] = 0.969870;
	pDetail->adTable6Uij[2][6] = 1.045290;
	pDetail->adTable6Uij[2][8] = 0.900000;
	pDetail->adTable6Uij[2][14] = 1.066638;
	pDetail->adTable6Uij[2][15] = 1.077634;
	pDetail->adTable6Uij[2][16] = 1.088178;
	pDetail->adTable6Uij[2][17] = 1.098291;
	pDetail->adTable6Uij[2][18] = 1.108021;
	pDetail->adTable6Uij[3][4] = 1.065173;
	pDetail->adTable6Uij[3][6] = 0.971926;
	pDetail->adTable6Uij[3][7] = 1.616660;
	pDetail->adTable6Uij[3][10] = 1.250000;
	pDetail->adTable6Uij[3][11] = 1.250000;
	pDetail->adTable6Uij[3][12] = 1.250000;
	pDetail->adTable6Uij[3][13] = 1.250000;
	pDetail->adTable6Uij[6][14] = 1.028973;
	pDetail->adTable6Uij[6][15] = 1.033754;
	pDetail->adTable6Uij[6][16] = 1.038338;
	pDetail->adTable6Uij[6][17] = 1.042735;
	pDetail->adTable6Uij[6][18] = 1.046966;
	pDetail->adTable6Kij[0][1] = 1.003630;
	pDetail->adTable6Kij[0][2] = 0.995933;
	pDetail->adTable6Kij[0][4] = 1.007619;
	pDetail->adTable6Kij[0][6] = 1.000080;
	pDetail->adTable6Kij[0][7] = 1.023260;
	pDetail->adTable6Kij[0][11] = 0.997596;
	pDetail->adTable6Kij[0][13] = 1.002529;
	pDetail->adTable6Kij[0][14] = 0.982962;
	pDetail->adTable6Kij[0][15] = 0.983565;
	pDetail->adTable6Kij[0][16] = 0.982707;
	pDetail->adTable6Kij[0][17] = 0.981849;
	pDetail->adTable6Kij[0][18] = 0.980991;
	pDetail->adTable6Kij[1][2] = 0.982361;
	pDetail->adTable6Kij[1][3] = 1.007960;
	pDetail->adTable6Kij[1][6] = 0.942596;
	pDetail->adTable6Kij[1][7] = 1.032270;
	pDetail->adTable6Kij[2][3] = 1.008510;
	pDetail->adTable6Kij[2][6] = 1.007790;
	pDetail->adTable6Kij[2][14] = 0.910183;
	pDetail->adTable6Kij[2][15] = 0.895362;
	pDetail->adTable6Kij[2][16] = 0.881152;
	pDetail->adTable6Kij[2][17] = 0.867520;
	pDetail->adTable6Kij[2][18] = 0.854406;
	pDetail->adTable6Kij[3][4] = 0.986893;
	pDetail->adTable6Kij[3][6] = 0.999969;
	pDetail->adTable6Kij[3][7] = 1.020340;
	pDetail->adTable6Kij[6][14] = 0.968130;
	pDetail->adTable6Kij[6][15] = 0.962870;
	pDetail->adTable6Kij[6][16] = 0.957828;
	pDetail->adTable6Kij[6][17] = 0.952441;
	pDetail->adTable6Kij[6][18] = 0.948338;
	pDetail->adTable6Gij[0][2] = 0.807653;
	pDetail->adTable6Gij[0][7] = 1.957310;
	pDetail->adTable6Gij[1][2] = 0.982746;
	pDetail->adTable6Gij[2][3] = 0.370296;
	pDetail->adTable6Gij[2][5] = 1.673090;
	
	
	/* GB/T11062 的高位发热量分组分数据*/
	
	// 初始化高热值数组
    double adTableHhvMol[4][NUMBEROFCOMPONENTS] = {
        {892.97,0,0,1564.34,2224.01,45.074,562.94,286.63,282.8,0,2874.2,2883.82,3535.98,3542.89,4203.23,4862.87,5522.4,6182.91,6842.69,0,0},        {891.56,0,0,1562.14,2221.1,44.433,562.38,286.15,282.91,0,2870.58,2879.76,3531.68,3538.6,4198.24,4857.18,5516.01,6175.82,6834.9,0,0},        {891.09,0,0,1561.41,2220.13,44.224,562.19,285.99,282.95,0,2869.38,2878.57,3530.24,3537.17,4196.58,4855.29,5513.88,6173.46,6832.31,0,0},        {890.63,0,0,1560.69,2219.17,44.016,562.01,285.83,282.98,0,2868.2,2877.4,3528.83,3535.77,4194.95,4853.43,5511.8,6171.15,6829.77,0,0}
	};
    
    // 复制到结构体成员
    memcpy(pDetail->adTableHhvMol, adTableHhvMol, sizeof(adTableHhvMol));
	
    // 初始化低热值数组
    double adTableLhvMol[4][NUMBEROFCOMPONENTS] = {
        {802.82,0,0,1429.12,2043.71,0,517.87,241.56,282.8,0,2648.83,2658.45,3265.54,3272.45,3887.71,4502.28,5116.73,5732.17,6346.88,0,0},        {802.69,0,0,1428.84,2043.37,0,517.95,241.72,282.91,0,2648.42,2657.6,3265.08,3272,3887.21,4501.72,5116.11,5731.49,6346.14,0,0},        {802.65,0,0,1428.74,2043.23,0,517.97,241.76,282.95,0,2648.26,2657.45,3264.89,3271.83,3887.01,4501.49,5115.87,5731.22,6345.85,0,0},        {802.6,0,0,1428.64,2043.11,0,517.99,241.81,282.98,0,2648.12,2657.32,3264.73,3271.67,3886.84,4501.3,5115.66,5730.99,6345.59,0,0}
	};
    
    // 复制到结构体成员
    memcpy(pDetail->adTableLhvMol, adTableLhvMol, sizeof(adTableLhvMol));
    
}


/**************************************************************************
	* 函数: Detail::paramdl()
	* 参数: void
	* 返回: void
	* 目的: 设置特性和二元交互参数
	* 修订: 
**************************************************************************/
void Detail_paramdl(Detail* pDetail)
{
    // 表5参数
    const double adTable5Mri[21] = {
		16.0430, 28.0135, 44.0100, 30.0700, 44.0970, 18.0153, 34.0820, 2.0159, 28.0100, 31.9988, 58.1230, 58.1230, 72.1500, 72.1500, 86.1770, 100.2040,114.2310,128.2580,142.2850,4.0026, 39.9480
	};
	
    const double adTable5Ei[21] = {151.318300, 99.737780, 241.960600, 244.166700, 298.118300, 514.015600, 296.355000, 26.957940, 105.534800, 122.766700, 324.068900, 337.638900, 365.599900, 370.682300, 402.636293, 427.722630, 450.325022, 470.840891, 489.558373, 2.610111, 119.629900
	};
	
    const double adTable5Ki[21] = {0.4619255, 0.4479153, 0.4557489, 0.5279209, 0.5837490, 0.3825868, 0.4618263, 0.3514916, 0.4533894, 0.4186954, 0.6406937, 0.6341423, 0.6738577, 0.6798307, 0.7175118, 0.7525189, 0.7849550, 0.8152731, 0.8437826, 0.3589888, 0.4216551
	};
	
    const double adTable5Gi[21] = {0.000000,0.027815,0.189065,0.079300,0.141239, 0.332500,0.088500,0.034369,0.038953,0.021000, 0.256692,0.281835,0.332267,0.366911,0.289731, 0.337542,0.383381,0.427354,0.469659,0.000000, 0.000000
	};
	
    // 初始化表5参数为零
    for (int j = 0; j < NUMBEROFCOMPONENTS; j++) {
        pDetail->adTable5Qi[j] = 0.0;
        pDetail->adTable5Fi[j] = 0.0;
        pDetail->adTable5Si[j] = 0.0;
        pDetail->adTable5Wi[j] = 0.0;
	}
	
    // 设置例外值
    pDetail->adTable5Qi[2] = 0.690000;
    pDetail->adTable5Qi[5] = 1.067750;
    pDetail->adTable5Qi[6] = 0.633276;
    pDetail->adTable5Fi[7] = 1.0000;
    pDetail->adTable5Si[5] = 1.5822;
    pDetail->adTable5Si[6] = 0.3900;
    pDetail->adTable5Wi[5] = 1.0000;
	
    // 为非零组分设置特性参数
	for (int j= pDetail->iNCC-1; j >= 0; j--)
	{
		pDetail->dMri[j] = adTable5Mri[ pDetail->aiCID[j]];
		pDetail->dKi[j] = adTable5Ki[ pDetail->aiCID[j]];
	}
	
	for (int j=0; j <  pDetail->iNCC; j++)
	{
		pDetail->dGi[j] = adTable5Gi[ pDetail->aiCID[j]];
		pDetail->dEi[j] = adTable5Ei[ pDetail->aiCID[j]];
	}
	
	for (int j=0; j <  pDetail->iNCC; j++)
	{
		pDetail->dQi[j] =   pDetail->adTable5Qi[pDetail->aiCID[j]];
		pDetail->dFi[j] = 0.0;
		
		if ( pDetail->aiCID[j] == 7)  pDetail->dFi[j] =  pDetail->adTable5Fi[7];
		pDetail->dSi[j] =  pDetail->adTable5Si[ pDetail->aiCID[j]];
		
		pDetail->dWi[j] =  pDetail->adTable5Wi[ pDetail->aiCID[j]];
	}
	
	for (int j=0; j <  pDetail->iNCC; j++)
	{
		for (int k=j; k <  pDetail->iNCC; k++)
		{
			pDetail->dUij[j][k] =  pDetail->adTable6Uij[ pDetail->aiCID[j]][ pDetail->aiCID[k]];
			pDetail->dKij[j][k] =  pDetail->adTable6Kij[ pDetail->aiCID[j]][ pDetail->aiCID[k]];
			pDetail->dEij[j][k] =  pDetail->adTable6Eij[ pDetail->aiCID[j]][ pDetail->aiCID[k]];
			pDetail->dGij[j][k] =  pDetail->adTable6Gij[ pDetail->aiCID[j]][ pDetail->aiCID[k]];
		}
	}
}


/**************************************************************************
	* 函数: Detail::chardl()
	* 参数: AGA10STRUCT *
	* 返回: void
	* 目的: 计算组成相关量
	* 修订: 
**************************************************************************/
void Detail_chardl(Detail* pDetail, AGA10STRUCT *ptAGA10)
{
    double tmfrac = 0.0;
    for (int j = 0; j < pDetail->iNCC; j++) {
        tmfrac += pDetail->dXi[j];
	}
	
    for (int j = 0; j < pDetail->iNCC; j++) {
        pDetail->dXi[j] /= tmfrac;
	}
	
    // 重置维里系数
    for (int j = 0; j < 18; j++) {
        pDetail->adBcoef[j] = 0.0;
	}
	
    double k5p0 = 0.0, k2p5 = 0.0, u5p0 = 0.0, u2p5 = 0.0;
    pDetail->dW = 0.0;
    double q1p0 = 0.0;
    pDetail->dF = 0.0;
	
    // 计算气体分子量
    ptAGA10->dMrx = 0.0;
	//高位发热量
	ptAGA10->dHhvMol=0.0;
	//低位发热量
	ptAGA10->dLhvMol=0.0;
	
	
	
    for (int i = 0; i < pDetail->iNCC; i++) {
        ptAGA10->dMrx += pDetail->dXi[i] * pDetail->dMri[i];
		
		switch (ptAGA10->dCbtj) {
			case 2:
			ptAGA10->dHhvMol +=  pDetail->adTableHhvMol[0][i] * ptAGA10->adMixture[i];
			ptAGA10->dLhvMol += pDetail-> adTableHhvMol[0][i] * ptAGA10->adMixture[i];
			break;
			case 1:
			ptAGA10->dHhvMol += pDetail->adTableHhvMol[1][i] * ptAGA10->adMixture[i];
			ptAGA10->dLhvMol += pDetail->adTableLhvMol[1][i] * ptAGA10->adMixture[i];
			break;
			case 0:
			ptAGA10->dHhvMol += pDetail->adTableHhvMol[2][i] * ptAGA10->adMixture[i];
			ptAGA10->dLhvMol += pDetail->adTableLhvMol[2][i] * ptAGA10->adMixture[i];
			break;
		}
		
        k2p5 += pDetail->dXi[i] * pow(pDetail->dKi[i], 2.5);
        u2p5 += pDetail->dXi[i] * pow(pDetail->dEi[i], 2.5);
        pDetail->dW += pDetail->dXi[i] * pDetail->dGi[i];
        q1p0 += pDetail->dXi[i] * pDetail->dQi[i];
        pDetail->dF += pDetail->dXi[i] * pDetail->dXi[i] * pDetail->dFi[i];
	}
	
	ptAGA10->dHhvMol = ptAGA10->dHhvMol / ptAGA10->dMrx;
	ptAGA10->dLhvMol = ptAGA10->dLhvMol / ptAGA10->dMrx;
	
    for (int i = 0; i < pDetail->iNCC; i++) {
        for (int j = i; j < pDetail->iNCC; j++) {
            double Xij = (i == j) ? pDetail->dXi[i] * pDetail->dXi[j] : 2.0 * pDetail->dXi[i] * pDetail->dXi[j];
            
            if (pDetail->dKij[i][j] != 1.0) {
                double term = pow(pDetail->dKi[i] * pDetail->dKi[j], 2.5);
                k5p0 += Xij * (pow(pDetail->dKij[i][j], 5.0) - 1.0) * term;
			}
            
            if (pDetail->dUij[i][j] != 1.0) {
                double term = pow(pDetail->dEi[i] * pDetail->dEi[j], 2.5);
                u5p0 += Xij * (pow(pDetail->dUij[i][j], 5.0) - 1.0) * term;
			}
            
            if (pDetail->dGij[i][j] != 1.0) {
                double avgG = (pDetail->dGi[i] + pDetail->dGi[j]) / 2.0;
                pDetail->dW += Xij * (pDetail->dGij[i][j] - 1.0) * avgG;
			}
			
            double Eij = pDetail->dEij[i][j] * sqrt(pDetail->dEi[i] * pDetail->dEi[j]);
            double Gij = pDetail->dGij[i][j] * (pDetail->dGi[i] + pDetail->dGi[j]) / 2.0;
			
            double e0p5 = sqrt(Eij);
            double e2p0 = Eij * Eij;
            double e3p0 = Eij * e2p0;
            double e3p5 = e3p0 * e0p5;
            double e4p5 = Eij * e3p5;
            double e6p0 = e3p0 * e3p0;
            double e7p5 = e4p5 * Eij * e2p0;
            double e9p5 = e7p5 * e2p0;
            double e11p0 = e4p5 * e4p5 * e2p0;
            double e12p0 = e11p0 * Eij;
            double e12p5 = e12p0 * e0p5;
			
            double s3 = Xij * pow(pow(pDetail->dKi[i], 3.0) * pow(pDetail->dKi[j], 3.0), 0.5);
			
            pDetail->adBcoef[0] += s3;
            pDetail->adBcoef[1] += s3 * e0p5;
            pDetail->adBcoef[2] += s3 * Eij;
            pDetail->adBcoef[3] += s3 * e3p5;
            pDetail->adBcoef[4] += s3 * Gij / e0p5;
            pDetail->adBcoef[5] += s3 * Gij * e4p5;
            pDetail->adBcoef[6] += s3 * pDetail->dQi[i] * pDetail->dQi[j] * e0p5;
            pDetail->adBcoef[7] += s3 * pDetail->dSi[i] * pDetail->dSi[j] * e7p5;
            pDetail->adBcoef[8] += s3 * pDetail->dSi[i] * pDetail->dSi[j] * e9p5;
            pDetail->adBcoef[9] += s3 * pDetail->dWi[i] * pDetail->dWi[j] * e6p0;
            pDetail->adBcoef[10] += s3 * pDetail->dWi[i] * pDetail->dWi[j] * e12p0;
            pDetail->adBcoef[11] += s3 * pDetail->dWi[i] * pDetail->dWi[j] * e12p5;
            pDetail->adBcoef[12] += s3 * pDetail->dFi[i] * pDetail->dFi[j] / e6p0;
            pDetail->adBcoef[13] += s3 * e2p0;
            pDetail->adBcoef[14] += s3 * e3p0;
            pDetail->adBcoef[15] += s3 * pDetail->dQi[i] * pDetail->dQi[j] * e2p0;
            pDetail->adBcoef[16] += s3 * e2p0;
            pDetail->adBcoef[17] += s3 * e11p0;
		}
	}
	
    // 应用An系数
    for (int i = 0; i < 18; i++) {
        pDetail->adBcoef[i] *= pDetail->adAn[i];
	}
	
    // 计算混合物参数
    pDetail->dKp3 = pow(k5p0 + pow(k2p5, 2.0), 0.6);
    pDetail->dU = pow(u5p0 + pow(u2p5, 2.0), 0.2);
    pDetail->dQp2 = q1p0 * q1p0;
}


/**************************************************************************
	* 函数: Detail::bvir()
	* 参数: void
	* 返回: void
	* 目的: 计算第二维里系数及其偏导数
	* 修订: 
**************************************************************************/
void Detail_bvir(Detail* pDetail)
{
    pDetail->dB = pDetail->ddBdT = pDetail->dd2BdT2 = 0.0;
	
    double t = pDetail->dT;
    double t0p5 = sqrt(t);
    double t2p0 = t * t;
    double t3p0 = t * t2p0;
    double t3p5 = t3p0 * t0p5;
    double t4p5 = t * t3p5;
    double t6p0 = t3p0 * t3p0;
    double t11p0 = t4p5 * t4p5 * t2p0;
    double t7p5 = t6p0 * t * t0p5;
    double t9p5 = t7p5 * t2p0;
    double t12p0 = t9p5 * t0p5 * t2p0;
    double t12p5 = t12p0 * t0p5;
    double t1p5 = t * t0p5;
    double t4p0 = t2p0 * t2p0;
	
    double Bx[18];
    for (int i = 0; i < 18; i++) {
		// double un = pDetail->adUn[i];
		// double an = pDetail->adAn[i];
        double bcoef = pDetail->adBcoef[i];
        
        switch (i) {
            case 0: Bx[i] = bcoef; break;
            case 1: Bx[i] = bcoef / t0p5; break;
            case 2: Bx[i] = bcoef / t; break;
            case 3: Bx[i] = bcoef / t3p5; break;
            case 4: Bx[i] = bcoef * t0p5; break;
            case 5: Bx[i] = bcoef / t4p5; break;
            case 6: Bx[i] = bcoef / t0p5; break;
            case 7: Bx[i] = bcoef / t7p5; break;
            case 8: Bx[i] = bcoef / t9p5; break;
            case 9: Bx[i] = bcoef / t6p0; break;
            case 10: Bx[i] = bcoef / t12p0; break;
            case 11: Bx[i] = bcoef / t12p5; break;
            case 12: Bx[i] = bcoef * t6p0; break;
            case 13: Bx[i] = bcoef / t2p0; break;
            case 14: Bx[i] = bcoef / t3p0; break;
            case 15: Bx[i] = bcoef / t2p0; break;
            case 16: Bx[i] = bcoef / t2p0; break;
            case 17: Bx[i] = bcoef / t11p0; break;
            default: Bx[i] = 0.0;
		}
        pDetail->dB += Bx[i];
	}
	
    // 计算一阶导数
    for (int i = 0; i < 18; i++) {
        if (pDetail->adUn[i] != 0.0) {
            Bx[i] *= pDetail->adUn[i] / t;
		}
	}
    for (int i = 0; i < 18; i++) {
        if (pDetail->adUn[i] != 0.0) {
            pDetail->ddBdT -= Bx[i];
		}
	}
	
    // 计算二阶导数
    for (int i = 0; i < 18; i++) {
        if (pDetail->adUn[i] != 0.0 && pDetail->adUn[i] != -1.0) {
            Bx[i] *= (pDetail->adUn[i] + 1.0) / t;
		}
	}
    for (int i = 0; i < 18; i++) {
        if (pDetail->adUn[i] != 0.0 && pDetail->adUn[i] != -1.0) {
            pDetail->dd2BdT2 += Bx[i] ;
		}
	}
}


/**************************************************************************
	* 函数: Detail::temp()
	* 参数: void
	* 返回: void
	* 目的: 计算温度相关量
	* 修订: 
**************************************************************************/
void Detail_temp(Detail* pDetail)
{
    Detail_bvir(pDetail);
    
    double tr = pDetail->dT / pDetail->dU;
    double tr0p5 = sqrt(tr);
    double tr1p5 = tr * tr0p5;
    double tr2p0 = tr * tr;
    double tr3p0 = tr * tr2p0;
    double tr4p0 = tr * tr3p0;
    double tr5p0 = tr * tr4p0;
    double tr6p0 = tr * tr5p0;
    double tr7p0 = tr * tr6p0;
    double tr8p0 = tr * tr7p0;
    double tr9p0 = tr * tr8p0;
    double tr11p0 = tr6p0 * tr5p0;
    double tr13p0 = tr6p0 * tr7p0;
    double tr21p0 = tr9p0 * tr9p0 * tr3p0;
    double tr22p0 = tr * tr21p0;
    double tr23p0 = tr * tr22p0;
	
    for (int i = 12; i < 58; i++) {
        double un = pDetail->adUn[i];
        double an = pDetail->adAn[i];
        double tr_exp = pow(tr, -un);
        pDetail->adFn[i] = an * tr_exp;
	}
	
}

/**************************************************************************
	* 函数: Detail::ddetail()
	* 参数: AGA10STRUCT *
	* 返回: void
	* 目的: 计算密度
	* 修订: 
**************************************************************************/
void Detail_ddetail(Detail* pDetail,AGA10STRUCT *ptAGA10)
{
    int imax, i;
    double epsp, epsr, epsmin;
    double x1, x2, x3, y1, y2, y3;
    double delx, delprv, delmin, delbis, xnumer, xdenom, sgndel;
    double y2my3, y3my1, y1my2, boundn;
	
    // 初始化收敛容差
    imax = 150;
    epsp = 1.e-6;
    epsr = 1.e-6;
    epsmin = 1.e-7;
    pDetail->dRho =0.0;
	
    // 调用子程序braket来包围密度解
    Detail_braket(pDetail,ptAGA10);
	
    // 检查从子程序braket返回的"lStatus"值
    if (ptAGA10->lStatus == MAX_NUM_OF_ITERATIONS_EXCEEDED ||
	ptAGA10->lStatus == NEGATIVE_DENSITY_DERIVATIVE)
    {
        return;
	}
	
    // 设置开始Brent方法
    // x是自变量，y是因变量
    // delx是当前迭代中x的变化
    // delprv是前一次迭代中x的变化
    x1 = pDetail->dRhoL;
    x2 = pDetail->dRhoH;
    y1 = pDetail->dPRhoL - pDetail->dP;
    y2 = pDetail->dPRhoH - pDetail->dP;
    delx = x1 - x2;
    delprv = delx;
	
    // 解被包围在x1和x2之间
    // 引入第三个点x3用于二次插值
    x3 = x1;
    y3 = y1;
	
    for (i=0; i < imax; i++)
    {
        // y3必须与y2符号相反，所以解在x2,x3之间
        if (y2 * y3 > 0.0)
        {
            x3 = x1;
            y3 = y1;
            delx = x1 - x2;
            delprv = delx;
		}
		
        // y2必须是y最接近y=0.0的值，然后x2new=x2old+delx
        if (fabs(y3) < fabs(y2))
        {
            x1 = x2;
            x2 = x3;
            x3 = x1;
            y1 = y2;
            y2 = y3;
            y3 = y1;
		}
		
        // delmin是未收敛迭代允许的最小步长
		delmin = epsmin * fabs(x2) ;
        
        // sgndel，x2变化的符号是delbis的符号
		delbis = 0.5 * (x3 - x2) ;
		
		if (fabs(delprv) < delmin || fabs(y1) < fabs(y2)) {
			// use bisection
			delx = delbis;
			delprv = delbis;
		} else
		{
			if (x3 != x1) {
				// use inverse quadratic interpolation
				y2my3 = y2 - y3;
				y3my1 = y3 - y1;
				y1my2 = y1 - y2;
				xdenom = -(y1my2) * (y2my3) * (y3my1);
				xnumer = x1 * y2 * y3 * (y2my3)
				+ x2 * y3 * y1 * (y3my1)
				+ x3 * y1 * y2 * (y1my2) - x2 * xdenom;
                } else {
				// use inverse linear interpolation
				xnumer = (x2 - x1) * y2;
				xdenom = y1 - y2;
			}
			// before calculating delx check delx=xnumer/xdenom is not out of bounds
			if (2.0 * fabs(xnumer) < fabs(delprv * xdenom)) {
				// procedure converging, use interpolation
				delprv = delx;
				delx = xnumer / xdenom;
                } else {
				// procedure diverging, use bisection
				delx = delbis;
				delprv = delbis;
			}
			
		}
		if ((fabs(y2) < epsp * pDetail->dP) && (fabs(delx) < epsr * fabs(x2))) {
			pDetail->dRho = x2 + delx;
			return;
		}
		
        if (fabs(delx) < delmin)
        {
            sgndel = delbis / fabs(delbis);
            delx = 1.0000009 * sgndel * delmin;
            delprv = delx;
		}
		
        // 最终检查以确保新的x2在旧x2和x3的范围内
        // boundn为负表示新的x2在旧x2和x3的范围内
        boundn = delx * (x2 + delx - x3);
        if (boundn > 0.0)
        {
            // 过程超出界限，使用二分法
            delx = delbis;
            delprv = delbis;
		}
		
        // 为下一次迭代重新标记变量
        // x1new = x2old, y1new=y2old
        x1 = x2;
        y1 = y2;
		
        // 下一次迭代的x2, y2值
        x2 = x2 + delx;
		Detail_pdetail(pDetail,x2);
        y2 = pDetail->dPCalc - pDetail->dP;
	}
	
    // ddetail: 超过最大迭代次数
    ptAGA10->lStatus=MAX_NUM_OF_ITERATIONS_EXCEEDED;
	pDetail->dRho = x2;
}

/**************************************************************************
	* 函数: Detail::braket()
	* 参数: AGA10STRUCT *
	* 返回: void
	* 目的: 包围密度解
	* 修订: 
**************************************************************************/
void Detail_braket(Detail* pDetail,AGA10STRUCT *ptAGA10)
{
    // 函数局部变量
    int imax, it;
    double del, rhomax, videal;
    double rho1, rho2, p1, p2;
	
    // 初始化
    imax = 200;
    rho1 = 0.0;
    p1 = 0.0;
    rhomax = 1.0 / pDetail->dKp3;
	
    if (pDetail->dT > 1.2593 * pDetail->dU) 
	rhomax = 20.0 * rhomax;
    
    videal = RGASKJ * pDetail->dT / pDetail->dP;
	
    if (fabs(pDetail->dB) < (0.167 * videal))
    {
        rho2 = 0.95 / (videal + pDetail->dB);
	}
    else
    {
        rho2 = 1.15 / videal;
	}
	
    del = rho2 / 20.0;
	
    // 开始迭代密度搜索循环
    for (it = 0; it < imax; it++)
    {
        if (rho2 > rhomax && ptAGA10->lStatus != MAX_DENSITY_IN_BRAKET_EXCEEDED)
        {
            // braket中的密度超过最大允许密度
            ptAGA10->lStatus = MAX_DENSITY_IN_BRAKET_EXCEEDED;
            del = 0.01 * (rhomax - rho1) + (pDetail->dP / (RGASKJ * pDetail->dT)) / 20.0;
            rho2 = rho1 + del;
            continue;
		}
		
        // 计算密度rho2处的压力p2
        Detail_pdetail(pDetail,rho2);
        p2 = pDetail->dPCalc;
		
        // 测试p2相对于p和相对于p1的值
        if (p2 > pDetail->dP)
        {
            // 密度根被包围(p1<p且p2>p)
            pDetail->dRhoL = rho1;
            pDetail->dPRhoL = p1;
            pDetail->dRhoH = rho2;
            pDetail->dPRhoH = p2;
            ptAGA10->lStatus = NORMAL;
            return;
		}
        else if (p2 > p1)
        {
            if (ptAGA10->lStatus == MAX_DENSITY_IN_BRAKET_EXCEEDED)
			del *= 2.0;
            rho1 = rho2;
            p1 = p2;
            rho2 = rho1 + del;
            continue;
		}
        else
        {
            // lStatus= NEGATIVE_DENSITY_DERIVATIVE表示
            // 压力有负的密度导数，因为p2小于某个先前的压力
            ptAGA10->lStatus = NEGATIVE_DENSITY_DERIVATIVE;
            pDetail->dRho = rho1;
            return;
		}
	}
	
    // 如果从底部退出，则超过最大迭代次数
    ptAGA10->lStatus = MAX_NUM_OF_ITERATIONS_EXCEEDED;
    pDetail->dRho = rho2;
    return;
}

/**************************************************************************
	* 函数: Detail::pdetail()
	* 参数: double
	* 返回: void
	* 目的: 给定D和T计算压力。调用zdetail()
	* 修订: 
**************************************************************************/
void Detail_pdetail(Detail* pDetail,double dD)
{
    pDetail->dPCalc = Detail_zdetail(pDetail,dD) * dD * RGASKJ * pDetail->dT;
}

/**************************************************************************
	* 函数: Detail::zdetail()
	* 参数: double
	* 返回: void
	* 目的: 计算压缩性
	* 修订: 
**************************************************************************/
double Detail_zdetail(Detail* pDetail,double d)
{
    // 函数局部变量
    double D1, D2, D3, D4, D5, D6, D7, D8, D9, exp1, exp2, exp3, exp4;
	
    // 约化密度的幂
    D1 = pDetail->dKp3 * d;
    D2 = D1 * D1;
    D3 = D2 * D1;
    D4 = D3 * D1;
    D5 = D4 * D1;
    D6 = D5 * D1;
    D7 = D6 * D1;
    D8 = D7 * D1;
    D9 = D8 * D1;
	
    exp1 = exp(-D1);
    exp2 = exp(-D2);
    exp3 = exp(-D3);
    exp4 = exp(-D4);
	
    // 以下Z的表达式从AGA8的FORTRAN示例中采用
    pDetail->dZ = 1.0 + pDetail->dB * d
	+pDetail->adFn[12] * D1 * (exp3 - 1.0 - 3.0 * D3 * exp3)
	+(pDetail->adFn[13] + pDetail->adFn[14] + pDetail->adFn[15]) * D1 * (exp2 - 1.0 - 2.0 * D2 * exp2)
	+(pDetail->adFn[16] + pDetail->adFn[17]) * D1 * (exp4 - 1.0 - 4.0 * D4 * exp4)
	+(pDetail->adFn[18] + pDetail->adFn[19]) * D2 * 2.0
	+(pDetail->adFn[20] + pDetail->adFn[21] + pDetail->adFn[22]) * D2 * (2.0 - 2.0 * D2) * exp2
	+(pDetail->adFn[23] + pDetail->adFn[24] + pDetail->adFn[25]) * D2 * (2.0 - 4.0 * D4) * exp4
	+pDetail->adFn[26] * D2 * (2.0 - 4.0 * D4) * exp4
	+pDetail->adFn[27] * D3 * 3.0
	+(pDetail->adFn[28] + pDetail->adFn[29]) * D3 * (3.0 - D1) * exp1
	+(pDetail->adFn[30] + pDetail->adFn[31]) * D3 * (3.0 - 2.0 * D2) * exp2
	+(pDetail->adFn[32] + pDetail->adFn[33]) * D3 * (3.0 - 3.0 * D3) * exp3
	+(pDetail->adFn[34] + pDetail->adFn[35] + pDetail->adFn[36]) * D3 * (3.0 - 4.0 * D4) * exp4
	+(pDetail->adFn[37] + pDetail->adFn[38]) * D4 * 4.0
	+(pDetail->adFn[39] + pDetail->adFn[40] + pDetail->adFn[41]) * D4 * (4.0 - 2.0 * D2) * exp2
	+(pDetail->adFn[42] + pDetail->adFn[43]) * D4 * (4.0 - 4.0 * D4) * exp4
	+pDetail->adFn[44] * D5 * 5.0
	+(pDetail->adFn[45] + pDetail->adFn[46]) * D5 * (5.0 - 2.0 * D2) * exp2
	+(pDetail->adFn[47] + pDetail->adFn[48]) * D5 * (5.0 - 4.0 * D4) * exp4
	+pDetail->adFn[49] * D6 * 6.0
	+pDetail->adFn[50] * D6 * (6.0 - 2.0 * D2) * exp2
	+pDetail->adFn[51] * D7 * 7.0
	+pDetail->adFn[52] * D7 * (7.0 - 2.0 * D2) * exp2
	+pDetail->adFn[53] * D8 * (8.0 - D1) * exp1
	+(pDetail->adFn[54] + pDetail->adFn[55]) * D8 * (8.0 - 2.0 * D2) * exp2
	+(pDetail->adFn[56] + pDetail->adFn[57]) * D9 * (9.0 - 2.0 * D2) * exp2;
	
    return pDetail->dZ;
}

/**************************************************************************
	* 函数: Detail::dZdT()
	* 参数: double
	* 返回: double
	* 目的: 计算Z对T的一阶偏导数
	* 修订: 
**************************************************************************/
double Detail_dZdT(Detail* pDetail,double d)
{
    // 函数局部变量
    double tmp;
    int i;
    double D1, D2, D3, D4, D5, D6, D7, D8, exp1, exp2, exp3, exp4;
	
    // 设置约化密度的幂
    D1 = pDetail->dKp3 * d;
    D2 = D1 * D1;
    D3 = D2 * D1;
    D4 = D3 * D1;
    D5 = D4 * D1;
    D6 = D5 * D1;
    D7 = D6 * D1;
    D8 = D7 * D1;
	// D9 = D8 * D1;
	
    exp1 = exp(-D1);
    exp2 = exp(-D2);
    exp3 = exp(-D3);
    exp4 = exp(-D4);
	
    // 从我们已经计算的系数(An[n])创建项uC*T^-(un+1)
    for (i=12; i < 58; i++)
    {
        if (pDetail->adUn[i] && pDetail->adFn[i])
        {
			pDetail-> fx[i] = (pDetail->adFn[i] * pDetail->adUn[i] * D1) / pDetail->dT;
		}
        else
        {
            pDetail->fx[i] = 0.0;
		}
	}
	
    // 方程的初始部分
	pDetail-> ddZdT = d * pDetail->ddBdT;
	
    // n=13对氢以外的所有物质评估为零，对氢fn=1
    if (pDetail->dF) 
	pDetail->ddZdT += pDetail->fx[12] - (pDetail->fx[12] * (1.0 - 3.0 * D3) * exp3);
	
    tmp = (1.0 - 2.0 * D2) * exp2;
    pDetail->ddZdT += (pDetail->fx[13] - (pDetail->fx[13] * tmp));
    pDetail->ddZdT += pDetail->fx[14] - (pDetail->fx[14] * tmp);
    pDetail->ddZdT += pDetail->fx[15] - (pDetail->fx[15] * tmp);
	
    tmp = (1.0 - 4.0 * D4) * exp4;
    pDetail->ddZdT += pDetail->fx[16] - (pDetail->fx[16] * tmp);
    pDetail->ddZdT += pDetail->fx[17] - (pDetail->fx[17] * tmp);
	
	pDetail-> ddZdT = pDetail->ddZdT - (pDetail->fx[18] + pDetail->fx[19]) * D1 * 2.0
	-(pDetail->fx[21] + pDetail->fx[22]) * D1 * (2.0 - 2.0 * D2) * exp2
	-(pDetail->fx[23] + pDetail->fx[24] + pDetail->fx[25]) * D1 * (2.0 - 4.0 * D4) * exp4
	-pDetail->fx[26] * D1 * (2.0 - 4.0 * D4) * exp4
	-pDetail->fx[27] * D2 * 3.0
	-(pDetail->fx[28] +pDetail-> fx[29]) * D2 * (3.0 - D1) * exp1
	-(pDetail->fx[30] + pDetail->fx[31]) * D2 * (3.0 - 2.0 * D2) * exp2
	-(pDetail->fx[32] + pDetail->fx[33]) * D2 * (3.0 - 3.0 * D3) * exp3
	-(pDetail->fx[34] + pDetail->fx[35] + pDetail->fx[36]) * D2 * (3.0 - 4.0 * D4) * exp4
	-pDetail->fx[37] * D3 * 4.0
	-(pDetail->fx[39] + pDetail->fx[40] + pDetail->fx[41]) * D3 * (4.0 - 2.0 * D2) * exp2
	-(pDetail->fx[42] + pDetail->fx[43]) * D3 * (4.0 - 4.0 * D4) * exp4
	-pDetail->fx[44] * D4 * 5.0
	-(pDetail->fx[45] + pDetail->fx[46]) * D4 * (5.0 - 2.0 * D2) * exp2
	-(pDetail->fx[47] + pDetail->fx[48]) * D4 * (5.0 - 4.0 * D4) * exp4
	-pDetail->fx[49] * D5 * 6.0
	-pDetail->fx[50] * D5 * (6.0 - 2.0 * D2) * exp2
	-pDetail->fx[51] * D6 * 7.0
	-pDetail->fx[52] * D6 * (7.0 - 2.0 * D2) * exp2
	-pDetail->fx[53] * D7 * (8.0 - D1) * exp1
	-pDetail->fx[54] * D7 * (8.0 - 2.0 * D2) * exp2
	-pDetail->fx[56] * D8 * (9.0 - 2.0 * D2) * exp2;
	
    return pDetail->ddZdT;
}

/**************************************************************************
	* 函数: Detail::d2ZdT2()
	* 参数: double
	* 返回: double
	* 目的: 计算Z对T的二阶偏导数
	* 修订: 
**************************************************************************/
double Detail_d2ZdT2(Detail* pDetail,double d)
{
    // 函数局部变量
    double tmp;
    int i;
    double D1, D2, D3, D4, D5, D6, D7, D8,  exp1, exp2, exp3, exp4;
	
    // 设置约化密度的幂
    D1 = pDetail->dKp3 * d;
    D2 = D1 * D1;
    D3 = D2 * D1;
    D4 = D3 * D1;
    D5 = D4 * D1;
    D6 = D5 * D1;
    D7 = D6 * D1;
    D8 = D7 * D1;
    //D9 = D8 * D1;
	
    exp1 = exp(-D1);
    exp2 = exp(-D2);
    exp3 = exp(-D3);
    exp4 = exp(-D4);
	
    // 从我们已经计算的系数(An[n])创建项uC*T^-(un+1)
    for (i=12; i < 58; i++)
    {
        if (pDetail->adUn[i] && pDetail->adFn[i])
        {
            pDetail->fx[i] = (pDetail->adFn[i] * D1 * pDetail->adUn[i] * (pDetail->adUn[i] + 1.0)) / (pDetail->dT * pDetail->dT);
		}
        else
        {
            pDetail->fx[i] = 0.0;
		}
	}
	
    // 方程的初始部分
    pDetail->dd2ZdT2 = d * pDetail->dd2BdT2;
	
    // n=13对氢以外的所有物质评估为零，对氢fn=1
    if (pDetail->dF) 
	pDetail->dd2ZdT2 += pDetail->fx[12] - (pDetail->fx[12] * (1.0 - 3.0 * D3) * exp3);
	
    tmp = (1.0 - 2.0 * D2) * exp2;
    pDetail->dd2ZdT2 += -pDetail->fx[13] + (pDetail->fx[13] * tmp);
    pDetail->dd2ZdT2 += -pDetail->fx[14] + (pDetail->fx[14] * tmp);
    pDetail->dd2ZdT2 += -pDetail->fx[15] + (pDetail->fx[15] * tmp);
	
    tmp = (1.0 - 4.0 * D4) * exp4;
    pDetail->dd2ZdT2 += -pDetail->fx[16] + (pDetail->fx[16] * tmp);
    pDetail->dd2ZdT2 += -pDetail->fx[17] + (pDetail->fx[17] * tmp);
	
    pDetail->dd2ZdT2 = pDetail->dd2ZdT2 + (pDetail->fx[18] + pDetail->fx[19]) * D1 * 2.0
	+(pDetail->fx[21] + pDetail->fx[22]) * D1 * (2.0 - 2.0 * D2) * exp2
	+(pDetail->fx[23] + pDetail->fx[24] + pDetail->fx[25]) * D1 * (2.0 - 4.0 * D4) * exp4
	+pDetail->fx[26] * D1 * (2.0 - 4.0 * D4) * exp4
	+pDetail->fx[27] * D2 * 3.0
	+(pDetail->fx[28] + pDetail->fx[29]) * D2 * (3.0 - D1) * exp1
	+(pDetail->fx[30] + pDetail->fx[31]) * D2 * (3.0 - 2.0 * D2) * exp2
	+(pDetail->fx[32] + pDetail->fx[33]) * D2 * (3.0 - 3.0 * D3) * exp3
	+(pDetail->fx[34] + pDetail->fx[35] + pDetail->fx[36]) * D2 * (3.0 - 4.0 * D4) * exp4
	+pDetail->fx[37] * D3 * 4.0
	+(pDetail->fx[39] + pDetail->fx[40] + pDetail->fx[41]) * D3 * (4.0 - 2.0 * D2) * exp2
	+(pDetail->fx[42] + pDetail->fx[43]) * D3 * (4.0 - 4.0 * D4) * exp4
	+pDetail->fx[44] * D4 * 5.0
	+(pDetail->fx[45] + pDetail->fx[46]) * D4 * (5.0 - 2.0 * D2) * exp2
	+(pDetail->fx[47] + pDetail->fx[48]) * D4 * (5.0 - 4.0 * D4) * exp4
	+pDetail->fx[49] * D5 * 6.0
	+pDetail->fx[50] * D5 * (6.0 - 2.0 * D2) * exp2
	+pDetail->fx[51] * D6 * 7.0
	+pDetail->fx[52] * D6 * (7.0 - 2.0 * D2) * exp2
	+pDetail->fx[53] * D7 * (8.0 - D1) * exp1
	+pDetail->fx[54] * D7 * (8.0 - 2.0 * D2) * exp2
	+pDetail->fx[56] * D8 * (9.0 - 2.0 * D2) * exp2;
	
    return pDetail->dd2ZdT2;
}

/**************************************************************************
	* 函数: Detail::dZdD()
	* 参数: double
	* 返回: double
	* 目的: 计算Z对D的一阶偏导数
	* 修订: 
**************************************************************************/
double Detail_dZdD(Detail* pDetail,double d)
{
    double temp, temp1, temp2, temp3;
    int i;
    double D1, D2, D3, D4, D5, D6, D7, D8,exp1,  exp2, exp3, exp4;
	
    // 设置约化密度的幂
    D1 = pDetail->dKp3 * d;
    D2 = D1 * D1;
    D3 = D2 * D1;
    D4 = D3 * D1;
    D5 = D4 * D1;
    D6 = D5 * D1;
    D7 = D6 * D1;
    D8 = D7 * D1;
    //D9 = D8 * D1;
	
    exp1 = exp(-D1);
    exp2 = exp(-D2);
    exp3 = exp(-D3);
    exp4 = exp(-D4);
	
    // 从我们已经计算的系数(An[n])创建项uC*T^-(un+1)
    for (i=12; i < 58; i++)
    {
        pDetail->fx[i] = pDetail->adFn[i];
	}
	
    // 方程的初始部分
    pDetail->ddZdD =  pDetail->dB / pDetail->dKp3;
	
    // 评估所有剩余项，尽可能简化
	
    // n=13对氢以外的所有物质评估为零，对氢fn=1
    if (pDetail->dF)
    {
        temp1 = -9.0 * D3 * exp3;
        temp2 = (1.0 - 3.0 * D3) * exp3;
        temp3 = -temp2 * 3.0 * D6;
        temp = temp1 + temp2 + temp3;
        pDetail->ddZdD += -pDetail->fx[12] + pDetail->fx[12] * temp;
	}
	
	//n =  14..16
	
	temp1 = -4.0 * D2 * exp2 ;
	
	temp2 = (1.0 - 2.0 * D2) * exp2 ; temp3 = -temp2 * 2.0 * D2;
	
	temp = temp1 + temp2 + temp3 ; pDetail->ddZdD += -pDetail->fx[13] + pDetail->fx[13] * temp ; pDetail->ddZdD += -pDetail->fx[14] + pDetail->fx[14] * temp ; pDetail->ddZdD += -pDetail->fx[15] + pDetail->fx[15] * temp ;
	
	// n =	17..18		
	temp1 =	-16.0 * D4	* exp4 ;
	temp2 =	(1.0 - 4.0	*	D4) * exp4 ;
	temp3 =	-temp2 * 4.0	* D4 ;
	temp = temp1 + temp2 + temp3 ; pDetail->ddZdD += -pDetail->fx[16] + pDetail->fx[16] * temp ; pDetail->ddZdD += -pDetail->fx[17] + pDetail->fx[17] * temp ;
	
	// n = 19..20 temp = 4.0 * D1 ;
	
	pDetail->ddZdD += pDetail->fx[18] * temp ; pDetail->ddZdD += pDetail->fx[19] * temp ;
	
	
	
	
	// n =	21..23			
	temp1 =	-4.0	*	D3	* exp2 ;
	temp2 =	(2.0	-	2.0 * D2)	* 2.0 * D1 * exp2 ;
	temp3 =	-temp2	* D2;	
	temp = temp1 +	temp2 + temp3 ;
	pDetail->ddZdD += pDetail->fx[20]	* temp ;	
	pDetail->ddZdD += pDetail->fx[21]	* temp ;	
	pDetail->ddZdD += pDetail->fx[22] * temp ;	
	// n =	24..27			
	temp1 =	-16.0 *	D5 * exp4 ;
	temp2 =	(2.0	-	4.0 * D4)	* 2.0 * D1 * exp4 ;
	temp3 =	-temp2	* 2.0 * D4 ;
	temp = temp1 +	temp2 + temp3 ;
	pDetail->ddZdD += pDetail->fx[23]	* temp ;	
	pDetail->ddZdD += pDetail->fx[24]	* temp ;	
	pDetail->ddZdD += pDetail->fx[25]	* temp ;	
	pDetail->ddZdD += pDetail->fx[26] * temp ;	
	// n =	28				
	temp = 9.0 *	D2	;		
	pDetail->ddZdD += pDetail->fx[27] * temp ;	
	// n =	29..30			
	temp = -D3 * exp1 + (3.0	- D1) * 3.0 * D2 * exp1 ;
	temp -=	(3.0	-	D1) * D3 *	exp1 ;
	pDetail->ddZdD += pDetail->fx[28] * temp ;	
	pDetail->ddZdD += pDetail->fx[29] * temp ;	
	// n =	31..32			
	temp1 =	-4.0	*	D4 * exp2 ;
	temp2 =	(3.0	-	2.0 * D2)	* 3.0 * D2 * exp2 ;
	temp3 =	-(3.0 - 2.0 * D2) * 2.0 * D4 * exp2 ;
	temp = temp1 + temp2 + temp3 ;
	pDetail->ddZdD += pDetail->fx[30] * temp ;	
	pDetail->ddZdD += pDetail->fx[31] * temp ;	
	// n =	33..34			
	temp1 =	-9.0	*	D5 * exp3 ;
	
	
	
	
	temp2 = (3.0 - 3.0 * D3) * 3.0 * D2 * exp3 ; temp3 = -(3.0 - 3.0 * D3) * 3.0 * D5 * exp3 ; temp = temp1 + temp2 + temp3 ;
	
	pDetail->ddZdD += pDetail->fx[32] * temp ; pDetail->ddZdD += pDetail->fx[33] * temp ;
	
	// n =	35..37
	
	temp1 = -16.0 * D6 * exp4 ;
	temp2 = (3.0 - 4.0 * D4) * 3.0 * D2 * exp4 ; temp3 = -(3.0 - 4.0 * D4) * D6 * 4.0 * exp4 ; temp = temp1 + temp2 + temp3 ;
	
	pDetail->ddZdD += pDetail->fx[34] * temp ; pDetail->ddZdD += pDetail->fx[35] * temp ; pDetail->ddZdD += pDetail->fx[36] * temp ;
	
	//n = 38..39 temp = 16.0 * D3 ;
	pDetail->ddZdD += pDetail->fx[37] * temp ; pDetail->ddZdD += pDetail->fx[38] * temp ;
	
	//n =  40..42
	
	temp1 = -4.0 * D5 * exp2 ;
	temp2 = (4.0 - 2.0 * D2) * 4.0 * D3 * exp2 ; temp3 = -(4.0 - 2.0 * D2) * 2.0 * D5 * exp2 ; temp = temp1 + temp2 + temp3 ;
	
	pDetail->ddZdD += pDetail->fx[39] * temp ; pDetail->ddZdD += pDetail->fx[40] * temp ; pDetail->ddZdD += pDetail->fx[41] * temp ;
	
	// n =	43..44
	
	temp = -16.0 * D7 * exp4 + (4.0 - 4.0 * D4) * 4.0 * D3 * exp4 ; temp -= (4.0 - 4.0 * D4) * D7 * 4.0 * exp4 ;
	
	pDetail->ddZdD += pDetail->fx[42] * temp ; pDetail->ddZdD += pDetail->fx[43] * temp ;
	
	// n =	45
	
	temp = 25.0 * D4 ; pDetail->ddZdD += pDetail->fx[44] * temp ;
	
	// n =	46..47
	
	
	
	
	temp = -4.0 * D6 * exp2 + (5.0 - 2.0 * D2) * 5.0 * D4 * exp2 ; temp -= (5.0 - 2.0 * D2) * D6 * 2.0 * exp2 ;
	
	pDetail->ddZdD += pDetail->fx[45] * temp ; pDetail->ddZdD += pDetail->fx[46] * temp ;
	
	// n =	48..49
	
	temp = -16.0 * D8 * exp4 + (5.0 - 4.0 * D4) * 5.0 * D4 * exp4 ; temp -= (5.0 - 4.0 * D4) * D8 * 4.0 * exp4 ;
	
	pDetail->ddZdD += pDetail->fx[47] * temp ; pDetail->ddZdD += pDetail->fx[48] * temp ;
	
	// n =	50
	
	temp = 36.0 * D5 ; pDetail->ddZdD += pDetail->fx[49] * temp ;
	
	// n =	51
	
	temp = -4.0 * D7 * exp2 + (6.0 - 2.0 * D2) * 6.0 * D5 * exp2 ; temp -= (6.0 - 2.0 * D2) * D7 * 2.0 * exp2 ;
	
	pDetail->ddZdD += pDetail->fx[50] * temp ;
	
	// n =	52
	
	temp = 49.0 * D6 ; pDetail->ddZdD += pDetail->fx[51] * temp ;
	
	// n =	53
	
	temp = -4.0 * D8 * exp2 + (7.0 - 2.0 * D2) * 7.0 * D6 * exp2 ; temp -= (7.0 - 2.0 * D2) * D8 * 2.0 * exp2 ;
	pDetail->ddZdD += pDetail->fx[52] * temp ;
	
	// n =	54
	
	temp = -1.0 * D8 * exp1 + (8.0 - D1) * 8.0 * D7 * exp1 ; temp -= (8.0 - D1) * D8 * exp1 ;
	
	pDetail->ddZdD += pDetail->fx[53] * temp ;
	
	// n =	55..56
	
	temp = -4.0 * D1 * D8 * exp2 + (8.0 - 2.0 * D2) * 8.0 * D7 * exp2 ; temp -= (8.0 - 2.0 * D2) * D8 * 2.0 * D1 * exp2 ;
	
	pDetail->ddZdD += pDetail->fx[54] * temp ; pDetail->ddZdD += pDetail->fx[55] * temp ;
	
	
	
	
	
	// n =	57..58
	
	temp = -4.0 * D2 * D8 * exp2 + (9.0 - 2.0 * D2) * 9.0 * D8 * exp2 ; temp -= (9.0 - 2.0 * D2) * D2 * D8 * 2.0 * exp2 ;
	
	pDetail->ddZdD += pDetail->fx[56] * temp ; pDetail->ddZdD += pDetail->fx[57] * temp ;
	
	pDetail->ddZdD *= pDetail->dKp3 ;
	
	
    return pDetail->ddZdD;
}

/**************************************************************************
	* 函数: Detail::relativedensity()
	* 参数: AGA10STRUCT *
	* 返回: void
	* 目的: 通过AGA 8中列出的方法计算相对密度
	* 修订: 
**************************************************************************/
void Detail_relativedensity(Detail* pDetail,AGA10STRUCT *ptAGA10)
{
    double dBX, dZa;
    const double dMWair = 28.96256;
	
    // 计算空气的第二维里系数
    dBX = -0.12527 + 5.91e-4 * ptAGA10->dTb - 6.62e-7 * ptAGA10->dTb * ptAGA10->dTb;
	
    // 计算空气的压缩系数
    dZa = 1.0 + (dBX * pDetail->dP) / (RGASKJ * ptAGA10->dTb);
	
    // 计算理想气体和真实气体的相对密度
    ptAGA10->dRD_Ideal = ptAGA10->dMrx / dMWair;
    ptAGA10->dRD_Real = ptAGA10->dRD_Ideal * (dZa / ptAGA10->dZb);
}
