难以理解fortran算术

Difficulty in understanding fortran arthimetic

我一直在努力理解以下子例程,但似乎无法理解:

subroutine press(ptes,Re,densm,void,svel,vis,dhyd,ht,p)
a=1.30
p=a*Re**(-1.63)*svel**2*ht/dhyd/void/densm/1000.
p=p*ptes
return
end

我已经遵循了提到的算术优先级的信息 here,但似乎无法获得等效的 C# 函数(如下所示)来吐出类似的输出。

public static void calculatePressureDrop(
    float pressure_drop_coeff, 
    float re, 
    float density_massecuite, 
    float voidage, 
    float superficial_mass_velocity, 
    float viscosity, 
    float hydraulic_diameter, 
    float height_of_section, 
    ref float pressure_drop)
{
    float sel_exp_val = (float)(2 * height_of_section / hydraulic_diameter / voidage / density_massecuite / 1000.0);
    float sel_exp = (float)Math.Pow(superficial_mass_velocity, sel_exp_val);
    float sel_exp_prod = -1.63F * sel_exp;
    float re_exp = (float)Math.Pow(re, sel_exp_prod);
    pressure_drop = (float)1.30 * re_exp;
    pressure_drop = pressure_drop * pressure_drop_coeff;
}

更新:这是我的测试用例中不断失败的代码

    [TestMethod]
    public void TestPressureDropMethodV2() // Uses call by ref
    {
        float pressure_drop_coeff = 1.20000005F;
        float re = 0.000365345448F;
        float density_massecuite = 1460;
        float voidage = 0.757799625F;
        float superficial_mass_velocity = 1.53357923F;
        float hydraulic_diameter = 0.151121646F;
        float viscosity = 634.350342F;
        float height_of_section = 0.0850000009F;
        float pressure_drop = 0;
        Finless5Lib.calculatePressureDrop(pressure_drop_coeff, re, density_massecuite, voidage, superficial_mass_velocity, viscosity, hydraulic_diameter, height_of_section,ref pressure_drop);
        float expectedResult = 0.74733448F;
        //// Verify the result:
        Assert.AreEqual(expectedResult, pressure_drop);
    }

当我从 Fortran 打印值时,这就是我得到的:

ptes = 1.20000005
Re = 0.000379003613
densm = 1460.
void = 0.757799625
svel = 1.53357923
vis = 611.490234
dhyd = 0.151121646
ht = 0.0850000009
p = 0.703936338

请帮忙!任何建议都会有所帮助!

我不太了解 FORTRAN,但是如果 ** 的优先级高于 *,那么:

p=a*(Re**(-1.63))*(svel**2)*ht/dhyd/void/densm/1000

你的 C# 似乎在做更像的事情:

p=a*(Re**((-1.63)*(svel**(2*ht/dhyd/void/densm/1000))))

我很确定你想要这个

public static float calculatePressureDrop(
    float pressure_drop_coeff,
    float re,
    float density_massecuite,
    float voidage,
    float superficial_mass_velocity,
    float viscosity,
    float hydraulic_diameter,
    float height_of_section)
{
    return pressure_drop_coeff * 1.3F * (float)Math.Pow(re, -1.63)
           * (float)Math.Pow(superficial_mass_velocity , 2) * height_of_section 
           / hydraulic_diameter / voidage / density_massecuite / 1000F;
}

那你就可以像这样使用了

float pressure_drop = calculatePressureDrop(...);

你的问题是你没有给电力运营商最高的优先级。同样,一般来说,如果可以的话,最好避免使用 ref 参数。

与此表达式相关的运算符(ISO/IEC 1539-1:2010 aka Fortran 2008 cl. 7.1.3)的优先级为 ***/, 一元 +-, 二元 +-.

Fortran 表达式

p=a*Re**(-1.63)*svel**2*ht/dhyd/void/densm/1000.

等同于数学表达式

p = a * (Re**(-1.63)) * (svel**2) * ht / dhyd / void / densm / 1000.0

结合其他语句(a=1.30 之前和 p=p*ptes 之后)产生:

p = 1.30 * (Re**(-1.63)) * (svel**2) * ht / dhyd / void / densm / 1000.0 * ptes

乘法和除法从左到右求值。