//
// Created by ldeyu on 2025/7/7.
//
#include "NGCal.h"
#include "FlowCal.h"
#include "math.h"
#include <rtthread.h>


void NGFlowCal(void){
    // 定义并初始化 FlowParSTRUCT 结构体变量
       FlowParSTRUCT flowParams = {0};
       NGParSTRUCT ngParams = {0};

       // 设置基本参数
       flowParams.dPatm = 0.0981; // 标准大气压(bar)
       flowParams.dPf = 1.48; // 压力(MPa)
       flowParams.dPfType = 0; // 0=表压，1=绝压
       flowParams.dDp = 12.50; // 差压(kPa)
       flowParams.dTf = 15; // 温度(°C)
       flowParams.dCbtj = 0; // 参比条件类型(0=标准状态)

       // 设置管道参数
       flowParams.dPipeD = 259.38; // 管道内径(mm)
       flowParams.dOrificeD = 150.25; // 孔板孔径(mm)
       flowParams.dPipeType = 0; // 管道类型
       flowParams.dPtmode = 0; // 取压方式(0=法兰取压，1=角接取压)

       // 设置材料参数
       flowParams.dPipeMaterial = 2; // 20号钢
       flowParams.dOrificeMaterial = 9; // 镍铬合金

       // 设置天然气组分(示例: 95%甲烷，5%其他)
       // 初始化天然气组分数组(GB/T 21446-2008 典型示例组成)
       for (int i = 0; i < NUMBEROFCOMPONENTS; i++) {
           flowParams.dNG_Compents[i] = 0.0; // 先全部初始化为0
       }

       // flowParams.dNG_Compents[0] = 92.47; // 甲烷(CH4)
       // flowParams.dNG_Compents[1] = 0.68; // 氮气(N2)
       // flowParams.dNG_Compents[2] = 1.75; // 二氧化碳(CO2)
       // flowParams.dNG_Compents[3] =3.5; // 乙烷(C2H6)
       // flowParams.dNG_Compents[4] = 0.98; // 丙烷(C3H8)
       // flowParams.dNG_Compents[5] = 0.00; // 水(H2O)
       // flowParams.dNG_Compents[6] = 0.00; // 硫化氢(H2S)
       // flowParams.dNG_Compents[7] = 0.0; // 氢气(H2)
       // flowParams.dNG_Compents[8] = 0.00; // 一氧化碳(CO)
       // flowParams.dNG_Compents[9] = 0.00; // 氧气(O2)
       // flowParams.dNG_Compents[10] = 0.34; // 异丁烷(i-C4H10)
       // flowParams.dNG_Compents[11] = 0.22; // 正丁烷(n-C4H10)
       // flowParams.dNG_Compents[12] = 0.0; // 异戊烷(i-C5H12)
       // flowParams.dNG_Compents[13] = 0.06; // 正戊烷(n-C5H12)
       // flowParams.dNG_Compents[14] = 0.0; // 己烷(C6H14)
       // flowParams.dNG_Compents[15] = 0.0; // 庚烷(C7H16)
       // flowParams.dNG_Compents[16] = 0.0; // 辛烷(C8H18)
       // flowParams.dNG_Compents[17] = 0.0; // 壬烷(C9H20)
       // flowParams.dNG_Compents[18] = 0.0; // 癸烷(C10H22)
       // flowParams.dNG_Compents[19] = 0.0; // 氦气(He)
       // flowParams.dNG_Compents[20] = 0.0; // 其他组分


       flowParams.dNG_Compents[0] = 88.36; // 甲烷(CH4)
       flowParams.dNG_Compents[1] = 0.68; // 氮气(N2)
       flowParams.dNG_Compents[2] = 1.57; // 二氧化碳(CO2)
       flowParams.dNG_Compents[3] =6.25; // 乙烷(C2H6)
       flowParams.dNG_Compents[4] = 2.4; // 丙烷(C3H8)
       flowParams.dNG_Compents[5] = 0.00; // 水(H2O)
       flowParams.dNG_Compents[6] = 0.00; // 硫化氢(H2S)
       flowParams.dNG_Compents[7] = 0.04; // 氢气(H2)
       flowParams.dNG_Compents[8] = 0.00; // 一氧化碳(CO)
       flowParams.dNG_Compents[9] = 0.00; // 氧气(O2)
       flowParams.dNG_Compents[10] = 0.15; // 异丁烷(i-C4H10)
       flowParams.dNG_Compents[11] = 0.35; // 正丁烷(n-C4H10)
       flowParams.dNG_Compents[12] = 0.05; // 异戊烷(i-C5H12)
       flowParams.dNG_Compents[13] = 0.1; // 正戊烷(n-C5H12)
       flowParams.dNG_Compents[14] = 0.01; // 己烷(C6H14)
       flowParams.dNG_Compents[15] = 0.0; // 庚烷(C7H16)
       flowParams.dNG_Compents[16] = 0.0; // 辛烷(C8H18)
       flowParams.dNG_Compents[17] = 0.0; // 壬烷(C9H20)
       flowParams.dNG_Compents[18] = 0.0; // 癸烷(C10H22)
       flowParams.dNG_Compents[19] = 0.04; // 氦气(He)
       flowParams.dNG_Compents[20] = 0.0; // 其他组分



       // 按照GB/T 21446-2008标准中典型天然气组分赋值(体积百分比)

       // flowParams.dNG_Compents[0] = 90.6724; // 甲烷(CH4)
       // flowParams.dNG_Compents[1] = 3.1284; // 氮气(N2)
       // flowParams.dNG_Compents[2] = 0.4676; // 二氧化碳(CO2)
       // flowParams.dNG_Compents[3] =4.5279; // 乙烷(C2H6)
       // flowParams.dNG_Compents[4] = 0.8280; // 丙烷(C3H8)
       // flowParams.dNG_Compents[5] = 0.00; // 水(H2O)
       // flowParams.dNG_Compents[6] = 0.00; // 硫化氢(H2S)
       // flowParams.dNG_Compents[7] = 0.0; // 氢气(H2)
       // flowParams.dNG_Compents[8] = 0.00; // 一氧化碳(CO)
       // flowParams.dNG_Compents[9] = 0.00; // 氧气(O2)
       // flowParams.dNG_Compents[10] = 0.1037; // 异丁烷(i-C4H10)
       // flowParams.dNG_Compents[11] = 0.1563; // 正丁烷(n-C4H10)
       // flowParams.dNG_Compents[12] = 0.0321; // 异戊烷(i-C5H12)
       // flowParams.dNG_Compents[13] = 0.0443; // 正戊烷(n-C5H12)
       // flowParams.dNG_Compents[14] = 0.0393; // 己烷(C6H14)
       // flowParams.dNG_Compents[15] = 0.0; // 庚烷(C7H16)
       // flowParams.dNG_Compents[16] = 0.0; // 辛烷(C8H18)
       // flowParams.dNG_Compents[17] = 0.0; // 壬烷(C9H20)
       // flowParams.dNG_Compents[18] = 0.0; // 癸烷(C10H22)
       // flowParams.dNG_Compents[19] = 0.0; // 氦气(He)
       // flowParams.dNG_Compents[20] = 0.0; // 其他组分

       // flowParams.dNG_Compents[0] =96.5; // 甲烷(CH4)
       // flowParams.dNG_Compents[1] =0.30; // 氮气(N2)
       // flowParams.dNG_Compents[2] =0.6; // 二氧化碳(CO2)
       // flowParams.dNG_Compents[3] =1.80; // 乙烷(C2H6)
       // flowParams.dNG_Compents[4] =0.45; // 丙烷(C3H8)
       // flowParams.dNG_Compents[5] =0; // 水(H2O)
       // flowParams.dNG_Compents[6] =0; // 硫化氢(H2S)
       // flowParams.dNG_Compents[7] =0; // 氢气(H2)
       // flowParams.dNG_Compents[8] =0; // 一氧化碳(CO)
       // flowParams.dNG_Compents[9] =0; // 氧气(O2)
       // flowParams.dNG_Compents[10]= 0.1; // 异丁烷(i-C4H10)
       // flowParams.dNG_Compents[11]= 0.1; // 正丁烷(n-C4H10)
       // flowParams.dNG_Compents[12]= 0.05; // 异戊烷(i-C5H12)
       // flowParams.dNG_Compents[13]= 0.03; // 正戊烷(n-C5H12)
       // flowParams.dNG_Compents[14]= 0.07; // 己烷(C6H14)
       // flowParams.dNG_Compents[15]= 0; // 庚烷(C7H16)
       // flowParams.dNG_Compents[16]= 0; // 辛烷(C8H18)
       // flowParams.dNG_Compents[17]= 0; // 壬烷(C9H20)
       // flowParams.dNG_Compents[18]= 0; // 癸烷(C10H22)
       // flowParams.dNG_Compents[19]= 0; // 氦气(He)
       // flowParams.dNG_Compents[20]= 0; // 其他组分

       // // 显式调用 NGCal_Init 初始化模块
       // if (NGCal_NGCal != NGCal_Init()) {
       //     printf("错误：NGCal 初始化失败！\n");
       //     return -1;  // 退出程序
       // }

       // 调用流量计算函数
       OFlowCal(&flowParams, &ngParams);

       rt_kprintf("FlowBase: %.6f Nm3/s\n", flowParams.dVFlowb);

       // 打印计算结果
/*     printf("工况条件信息:\n");
       printf("标准参比条件: %d\n", flowParams.dCbtj);
       printf("计量参比压力: %.2f\n", flowParams.dPb_M);
       printf("计量参比温度: %.2f\n", flowParams.dTb_M);
       printf("能量参比压力: %.2f\n", flowParams.dPb_E);
       printf("能量参比温度: %.2f\n", flowParams.dTb_E);
       printf("大气压力: %.2f Pa\n", flowParams.dPatm);
       printf("天然气组分:\n");
       for (int i = 0; i < 21; i++) {
           printf("  组分 %d: %.6f\n", i, flowParams.dNG_Compents[i]);
       }

       printf("\n仪表参数:\n");
       printf("仪表类型: %d\n", flowParams.dMeterType);
       printf("核心类型: %d\n", flowParams.dCoreType);
       printf("取压方式: %d\n", flowParams.dPtmode);
       printf("管道类型: %d\n", flowParams.dPipeType);
       printf("管道内径: %.2f mm\n", flowParams.dPipeD);
       printf("管道材质: %d\n", flowParams.dPipeMaterial);
       printf("孔板直径: %.2f mm\n", flowParams.dOrificeD);
       printf("孔板材质: %d\n", flowParams.dOrificeMaterial);

       printf("\n测量值:\n");
       printf("压力: %.2f Pa\n", flowParams.dPf);
       printf("压力类型: %d\n", flowParams.dPfType);
       printf("温度: %.2f K\n", flowParams.dTf);
       printf("差压: %.2f Pa\n", flowParams.dDp);
       printf("仪表系数: %.6f\n", flowParams.dMeterFactor);
       printf("脉冲数: %.2f\n", flowParams.dPulseNum);

       printf("\n计算结果:\n");
       printf("膨胀系数: %.6f\n", flowParams.dE);
       printf("相对密度系数: %.6f\n", flowParams.dFG);
       printf("超压缩系数: %.6f\n", flowParams.dFT);
       printf("动力粘度: %.6f\n", flowParams.dDViscosity);
       printf("热膨胀系数: %.6f\n", flowParams.dDExpCoefficient);
       printf("管道雷诺数: %.2f\n", flowParams.dRnPipe);
       printf("孔板弯曲系数: %.6f\n", flowParams.dBk);
       printf("管道粗糙度: %.6f\n", flowParams.dRoughNessPipe);
       printf("流出系数: %.6f\n", flowParams.dCd);
       printf("流出系数修正: %.6f\n", flowParams.dCdCorrect);
       printf("喷嘴流出系数: %.6f\n", flowParams.dCdNozell);
       printf("标况体积流量: %.6f Nm3/s\n", flowParams.dVFlowb);
       printf("工况体积流量: %.6f m3/s\n", flowParams.dVFlowf);
       printf("质量流量: %.6f t/s\n", flowParams.dMFlowb);
       printf("能量流量: %.6f MJ/s\n", flowParams.dEFlowb);
       printf("流速: %.6f m/s\n", flowParams.dVelocityFlow);
       printf("压力损失: %.6f\n", flowParams.dPressLost);
       printf("直径比: %.6f\n", flowParams.dBeta);
       printf("等熵指数: %.6f\n", flowParams.dKappa);
       printf("压缩因子: %.6f\n", flowParams.dFpv);

       printf("状态: %ld\n", ngParams.lStatus);
       printf("强制更新标志: %d\n", ngParams.bForceUpdate);
       printf("混合比:\n");
       for (int i = 0; i < 21; i++) {
           printf("  组分 %d: %.6f\n", i, ngParams.adMixture[i]);
       }
       printf("参比条件: %d\n", ngParams.dCbtj);
       printf("标准压力: %.2f Pa\n", ngParams.dPb);
       printf("标准温度: %.2f K\n", ngParams.dTb);
       printf("工作压力: %.2f Pa\n", ngParams.dPf);
       printf("工作温度: %.2f K\n", ngParams.dTf);

       printf("\nAGA 8 详细计算结果:\n");
       printf("平均分子量: %.6f\n", ngParams.dMrx);
       printf("标准条件下压缩因子: %.6f\n", ngParams.dZb);
       printf("工作条件下压缩因子: %.6f\n", ngParams.dZf);
       printf("超压缩因子: %.6f\n", ngParams.dFpv);
       printf("标准条件下摩尔密度: %.6f moles/dm3\n", ngParams.dDb);
       printf("工作条件下摩尔密度: %.6f moles/dm3\n", ngParams.dDf);
       printf("标准条件下密度: %.6f kg/m3\n", ngParams.dRhob);
       printf("工作条件下密度: %.6f kg/m3\n", ngParams.dRhof);
       printf("理想相对密度: %.6f\n", ngParams.dRD_Ideal);
       printf("实际相对密度: %.6f\n", ngParams.dRD_Real);

       printf("\n热力学性质:\n");
       printf("理想焓: %.6f\n", ngParams.dHo);
       printf("实际焓: %.6f J/kg\n", ngParams.dH);
       printf("实际熵: %.6f J/kg-mol.K\n", ngParams.dS);
       printf("理想定压比热: %.6f J/kg-mol.K\n", ngParams.dCpi);
       printf("实际定压比热: %.6f J/kg-mol.K\n", ngParams.dCp);
       printf("实际定容比热: %.6f J/kg-mol.K\n", ngParams.dCv);
       printf("比热比: %.6f\n", ngParams.dk);
       printf("等熵指数: %.6f\n", ngParams.dKappa);
       printf("声速: %.6f m/s\n", ngParams.dSOS);
       printf("临界流函数: %.6f\n", ngParams.dCstar);*/

       //printf("\n单位摩尔高热值: %.6f\n", ngParams.dHhvMol);

      // printf("单位摩尔低热值: %.6f\n", ngParams.dLhvMol);


}


double format_double(double value, int digits) {
    // 处理默认位数（4位）
    if (digits == 0) {
        digits = 4;
    }

    // 验证位数有效性
    if (digits < 1 || digits > 5) {
        fprintf(stderr, "Error: Invalid digit value (must be 1-5 or 0 for default)\n");
        return NAN; // 返回 NaN 表示错误
    }

    char format_str[10];
    char buffer[50];

    // 生成动态格式字符串（如 "%.4f"）
    snprintf(format_str, sizeof(format_str), "%%.%df", digits);

    // 格式化数值到字符串
    snprintf(buffer, sizeof(buffer), format_str, value);

    // 转换回 double
    double result = strtod(buffer, NULL);
    return result;
}

void OFlowCal(FlowParSTRUCT *ptFlowPar, NGParSTRUCT *ptNGPar) {
    double tempPatm = ptFlowPar->dPatm * 1000000;

    double tempPf = ptFlowPar->dPf * 1000000;


    double tempDP = ptFlowPar->dDp * 1000;

    double tempTf = ptFlowPar->dTf + 273.15;

    if (ptFlowPar->dPfType == 0) {
        ptFlowPar->dPf = tempPatm + tempPf;
        ptNGPar->dPf = tempPatm + tempPf;
    } else {
        ptFlowPar->dPf = tempPf;
        ptNGPar->dPf = tempPf;
    }

    ptFlowPar->dDp = tempDP;
    ptFlowPar->dTf = tempTf;
    ptNGPar->dTf = tempTf;
    ptNGPar->dCbtj = ptFlowPar->dCbtj;

    switch (ptNGPar->dCbtj) {
        case 2:
            ptNGPar->dPb = 101325;
            ptNGPar->dTb = 273.15;
            ptFlowPar->dPb_M = (101325);
            ptFlowPar->dTb_M = (273.15);
            break;
        case 1:

            ptNGPar->dPb = (101325);
            ptNGPar->dTb = (288.15);
            ptFlowPar->dPb_M = (101325);
            ptFlowPar->dTb_M = (288.15);
            break;
        case 0:

            ptNGPar->dPb = (101325);
            ptNGPar->dTb = (293.15);
            ptFlowPar->dPb_M = (101325);
            ptFlowPar->dTb_M = (293.15);
            break;
        default: ;
    }

    double ngArray[NUMBEROFCOMPONENTS];

    for (int i = 0; i < NUMBEROFCOMPONENTS; i++) {
        ngArray[i] = ptFlowPar->dNG_Compents[i] / 100;
        ptNGPar->adMixture[i] = ngArray[i];
    }
    Crit(ptNGPar, 0);


    ptFlowPar->dFpv = format_double(ptNGPar->dFpv, 4);


    ptFlowPar->dOrificeD = format_double(ptFlowPar->dOrificeD * (
                                             1 + 0.000001 * CaiLiaoPzxs(ptFlowPar->dOrificeMaterial) * (
                                                 ptFlowPar->dTf - 293.15)), 2);
    ptFlowPar->dPipeD = format_double(ptFlowPar->dPipeD * (1 + 0.000001 * CaiLiaoPzxs(ptFlowPar->dPipeMaterial) * (
                                                               ptFlowPar->dTf - 293.15)), 2);
    ptFlowPar->dBeta = format_double(ptFlowPar->dOrificeD / ptFlowPar->dPipeD, 4);

    ptFlowPar->dE = format_double(calculateE(ptFlowPar->dBeta), 4);
    ptFlowPar->dFG = format_double(calculateFG(ptNGPar->dRD_Real), 4);
    ptFlowPar->dFT = format_double(calculateFT(ptFlowPar->dTb_M, ptFlowPar->dTf), 4);

    ptFlowPar->dKappa = format_double(calculateKappa(ptNGPar->dZf), 4);
    ptFlowPar->dDViscosity = format_double(Dlndjs(ptFlowPar->dPf / 1e6, ptFlowPar->dTf), 5);
    ptFlowPar->dDExpCoefficient = format_double(calculateEpsilon(ptFlowPar->dPf, ptFlowPar->dDp,
                                                                 ptFlowPar->dBeta, ptFlowPar->dKappa), 4);

    double D = ptFlowPar->dPipeD / 1000.0;
    double d = ptFlowPar->dOrificeD / 1000.0;
    double beta = ptFlowPar->dBeta;
    double P1 = ptFlowPar->dPf;
    double deltaP = ptFlowPar->dDp;
    double Tf = ptFlowPar->dTf;


    double C_initial = 0.6;
    double Qf_initial = (C_initial * ptFlowPar->dE * ptFlowPar->dDExpCoefficient * M_PI * pow(d, 2) / 4)
                        * sqrt(2 * deltaP / (ptNGPar->dRhof * (1 - pow(beta, 4))));
    ptFlowPar->dVFlowf = Qf_initial;
    double tolerance = 1e-6;
    double currentC = C_initial;
    double currentReD = calculateReD(Qf_initial, D, ptNGPar->dRhof, ptFlowPar->dDViscosity);
    int iter = 0;
    double prevC = 0;
    do {
        int maxIter = 100;
        prevC = currentC;
        currentC = calculateCd(beta, currentReD, ptFlowPar->dPipeD, ptFlowPar->dPtmode);
        double Qf = (currentC * ptFlowPar->dDExpCoefficient * M_PI * pow(d, 2) / 4)
                    * sqrt(2 * deltaP / (ptNGPar->dRhof * (1 - pow(beta, 4))));
        ptFlowPar->dVFlowf = Qf;
        currentReD = calculateReD(Qf, D, ptNGPar->dRhof, ptFlowPar->dDViscosity);
        iter++;
        if (iter > maxIter) {
            fprintf(stderr, "\n");
        }
    } while (fabs(currentC - prevC) / currentC > tolerance);

    double K = calculateK(ptFlowPar->dPipeType);
    double G_me = calculateRoughnessFactor(ptFlowPar->dPipeD, K, currentC);
    double C_corrected = currentC * G_me;

    ptFlowPar->dCd = C_corrected;
    ptFlowPar->dRoughNessPipe = G_me;
    ptFlowPar->dRnPipe = currentReD;
    double Qn = ptFlowPar->dVFlowf * (ptFlowPar->dFpv * ptFlowPar->dFpv * P1 / ptFlowPar->dPb_M)
                * (ptFlowPar->dTb_M) / Tf;
    ptFlowPar->dVFlowb = Qn;
    ptFlowPar->dMFlowb = ptFlowPar->dVFlowb * ptNGPar->dRhob;
    ptFlowPar->dEFlowb = ptFlowPar->dVFlowb * ptNGPar->dHhvMol * ptFlowPar->dPb_M * 1e-6 / RGASKJ / ptFlowPar->dTb_M;
    ptFlowPar->dVelocityFlow = ptFlowPar->dVFlowf / (M_PI * pow((ptFlowPar->dPipeD / 2000), 2));
}


double CaiLiaoPzxs(const int tempCaiLiao) {
    double CaiLiaoPzxs = 0;
    switch (tempCaiLiao) {
        case 0:
            CaiLiaoPzxs = 11.75;
            break;

        case 1:
            CaiLiaoPzxs = 11.6;
            break;

        case 2:
            CaiLiaoPzxs = 11.16;
            break;

        case 3:
            CaiLiaoPzxs = 11.59;
            break;

        case 4:
            CaiLiaoPzxs = 10.5;
            break;

        case 5:
            CaiLiaoPzxs = 10.0;
            break;

        case 6:
            CaiLiaoPzxs = 10.2;
            break;

        case 7:
            CaiLiaoPzxs = 15.5;
            break;

        case 8:
            CaiLiaoPzxs = 11.5;
            break;

        case 9:
            CaiLiaoPzxs = 10.8;
            break;

        case 10:
            CaiLiaoPzxs = 16.6;
            break;

        case 11:
            CaiLiaoPzxs = 11.4;
            break;

        case 12:
            CaiLiaoPzxs = 16.55;
            break;

        case 13:
            CaiLiaoPzxs = 17.8;
            break;

        case 14:
            CaiLiaoPzxs = 17.2;
            break;
        default: ;
    }
    return CaiLiaoPzxs;
}


double calculateK(int dPipeType) {
    double Jdccd;
    switch (dPipeType) {
        case 0:
            Jdccd = 0.029;
            break;
        case 1:
        case 2:
        case 3:
            Jdccd = 0.075;
            break;
        case 4:
            Jdccd = 0.1;
            break;
        case 5:
            Jdccd = 0.15;
            break;
        case 6:
            Jdccd = 1;
            break;
        case 7:
            Jdccd = 2.1;
            break;
        case 8:
            Jdccd = 0.04;
            break;
        case 9:
            Jdccd = 0.15;
            break;
        case 10:
            Jdccd = 0.13;
            break;
        case 11:
            Jdccd = 0.25;
            break;
        default:

            fprintf(stderr, "δ֪<EFBFBD>Ĺܵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", dPipeType);
            return FLOW_CALC_ERROR;
    }
    return Jdccd;
}


double calculateRoughnessFactor(double D_pipe, double K, double C) {
    double K_over_D = K / D_pipe;


    if (K_over_D <= 0.0004) {
        return 1.0000;
    }


    double term = (K_over_D * 1e6) - 400;
    if (term < 0) {
        fprintf(
            stderr,
            "K/D <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>÷<EFBFBD>Χ\n");
        return FLOW_CALC_ERROR;
    }

    double G_me = 1 + (0.011 / C) * sqrt(term);
    return G_me;
}


double calculateE(double beta) {
    return 1 / sqrt(1 - pow(beta, 4));
}


double calculateFG(double dRD_Real) {
    return 1 / sqrt(dRD_Real);
}


double calculateFT(double dTb_M, double dTf) {
    return sqrt(dTb_M / dTf);
}


double calculateEpsilon(double dPf, double dDp, double beta, double dKappa) {
    double tau = (dPf - dDp) / dPf;
    double epsilon = 1 - (0.351 + 0.256 * pow(beta, 4) + 0.93 * pow(beta, 8)) * (1 - pow(tau, 1 / dKappa));
    return epsilon;
}


double calculateKappa(double dZf) {
    double gamma = 1.3;
    double Z = dZf;


    double kappa = gamma / (1 - (gamma - 1) * (1 / Z - 1));
    return kappa;
}


double calculateReD(double Qf, double D, double rho, double mu) {
    return (4 * Qf * rho * 1000) / (M_PI * D * mu);
}


double calculateCd(double beta, double ReD, double D_mm, int ptMode) {
    double L1, L2;

    switch (ptMode) {
        case 1:
            L1 = L2 = 0;
            break;
        case 0:
            L1 = L2 = 25.4 / D_mm;
            break;
        case 2:
            L1 = 1.0;
            L2 = 0.47;
            break;
        default:
            fprintf(stderr, "<EFBFBD><EFBFBD>֧<EFBFBD>ֵ<EFBFBD>ȡѹ<EFBFBD><EFBFBD>ʽ: %d\n", ptMode);
            return FLOW_CALC_ERROR;
    }

    double term1 = 0.5961 + 0.0261 * pow(beta, 2) - 0.216 * pow(beta, 8);
    double term2 = 0.000521 * pow(1e6 * beta / ReD, 0.7);
    double A = pow(19000 * beta / ReD, 0.8);
    double term3 = (0.0188 + 0.0063 * A) * pow(beta, 3.5) * pow(1e6 / ReD, 0.3);
    double term4 = (0.043 + 0.08 * exp(-10 * L1) - 0.123 * exp(-7 * L1))
                   * (1 - 0.11 * A) * pow(beta, 4) / (1 - pow(beta, 4));
    double term5 = -0.031 * (2 * L2 / (1 - beta) - 0.8 * pow(2 * L2 / (1 - beta), 1.1))
                   * pow(beta, 1.3);

    double Cd = term1 + term2 + term3 + term4 + term5;


    if (D_mm < 71.12) {
        Cd += 0.011 * (0.75 - beta) * (2.8 - D_mm / 25.4);
    }
    return Cd;
}


double Dlndjs(double tempP_jy, double tempT) {
    double Dlndjs_Dlnd_Data[8][11] = {
        {976, 991, 1014, 1044, 1073, 1114, 1156, 1207, 1261, 1331, 1405},
        {1027, 1040, 1063, 1091, 1118, 1151, 1185, 1230, 1276, 1331, 1389},
        {1071, 1082, 1106, 1127, 1149, 1180, 1211, 1250, 1289, 1335, 1383},
        {1123, 1135, 1153, 1174, 1195, 1224, 1253, 1289, 1324, 1366, 1409},
        {1167, 1178, 1196, 1216, 1236, 1261, 1287, 1318, 1350, 1385, 1421},
        {1213, 1224, 1239, 1257, 1275, 1297, 1320, 1346, 1373, 1403, 1435},
        {1260, 1270, 1281, 1297, 1313, 1333, 1352, 1374, 1396, 1424, 1451},
        {1303, 1312, 1323, 1338, 1352, 1372, 1391, 1412, 1432, 1456, 1482}
    };

    double Dlndjs_Dlnd_T[8] = {
        -15 + 273.15, 0 + 273.15, 15 + 273.15, 30 + 273.15,
        45 + 273.15, 60 + 273.15, 75 + 273.15, 90 + 273.15
    };

    double Dlndjs_Dlnd_P[11] = {0.1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    double ky, kx;
    int i, m = 0, n = 0;

    if (tempT < Dlndjs_Dlnd_T[0]) {
        tempT = Dlndjs_Dlnd_T[0];
    }
    if (tempT > Dlndjs_Dlnd_T[7]) {
        tempT = Dlndjs_Dlnd_T[7];
    }
    if (tempP_jy < Dlndjs_Dlnd_P[0]) {
        tempP_jy = Dlndjs_Dlnd_P[0];
    }
    if (tempP_jy > Dlndjs_Dlnd_P[10]) {
        tempP_jy = Dlndjs_Dlnd_P[10];
    }

    for (i = 0; i <= 6; i++) {
        if (tempT >= Dlndjs_Dlnd_T[i] && tempT <= Dlndjs_Dlnd_T[i + 1]) {
            m = i;
            break;
        }
    }

    for (i = 0; i <= 9; i++) {
        if (tempP_jy >= Dlndjs_Dlnd_P[i] && tempP_jy <= Dlndjs_Dlnd_P[i + 1]) {
            n = i;
            break;
        }
    }

    if (Dlndjs_Dlnd_P[n + 1] - Dlndjs_Dlnd_P[n] != 0) {
        ky = (tempP_jy - Dlndjs_Dlnd_P[n]) / (Dlndjs_Dlnd_P[n + 1] - Dlndjs_Dlnd_P[n]);
    } else {
        ky = 0;
    }

    if (Dlndjs_Dlnd_T[m + 1] - Dlndjs_Dlnd_T[m] != 0) {
        kx = (tempT - Dlndjs_Dlnd_T[m]) / (Dlndjs_Dlnd_T[m + 1] - Dlndjs_Dlnd_T[m]);
    } else {
        kx = 0;
    }

    double s1 = Dlndjs_Dlnd_Data[m][n] + (Dlndjs_Dlnd_Data[m][n + 1] - Dlndjs_Dlnd_Data[m][n]) * ky;
    double s2 = Dlndjs_Dlnd_Data[m + 1][n] + (Dlndjs_Dlnd_Data[m + 1][n + 1] - Dlndjs_Dlnd_Data[m + 1][n]) * ky;
    return (s1 + (s2 - s1) * kx) / 100000.0;
}

// 压力损失计算
double YaLiSunShi(double tempLiuChuXiShu, double tempZjb, double tempDp, int JieLiuZhuangZhi) {
    double ylss = 0;
    switch (JieLiuZhuangZhi) {
        case 0:
        case 1:
        case 2:
            ylss = tempDp * (sqrt(1 - tempZjb) - tempLiuChuXiShu * pow(tempZjb, 2))
                   / (sqrt(1 - tempZjb) + tempLiuChuXiShu * pow(tempZjb, 2));
            break;
        default: ;
    }
    return ylss;
}


// 标况转工况流量转换
double FlowConvert_BaseToWork(double Pf, double Tf, double Zb, double Zf, double FlowBase, int Cbtj) {
    double tempPn = 0;
    double tempTn = 0;


    switch (Cbtj) {
        case 2:
            tempPn = 101325;
            tempTn = 273.15;
            break;
        case 1:
            tempPn = 101325;
            tempTn = 288.15;
            break;
        case 0:
            tempPn = 101325;
            tempTn = 293.15;
            break;
        case 3:
            tempPn = 10155981;
            tempTn = 288.7055555;
            break;
        default: ;
    }
    return FlowBase * tempPn * Tf * Zf / Pf / tempTn / Zb;
}

// 工况转标况流量转换
double FlowConvert_WorkToBase(double Pf, double Tf, double Zb, double Zf, double FlowWork, int Cbtj) {
    double tempPn = 0;
    double tempTn = 0;

    switch (Cbtj) {
        case 2:
            tempPn = 101325;
            tempTn = 273.15;
            break;
        case 1:
            tempPn = 101325;
            tempTn = 288.15;
            break;
        case 0:
            tempPn = 101325;
            tempTn = 293.15;
            break;
        case 3:
            tempPn = 10155981;
            tempTn = 288.7055555;
            break;
        default: ;
    }
    return FlowWork * Pf * tempTn * Zb / tempPn / Tf / Zf;
}
