难以理解fortran arthimetic

时间:2015-08-28 16:34:33

标签: c# fortran porting fortran77

我一直试图理解下面的子程序,但似乎无法做到正确:

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

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

3 个答案:

答案 0 :(得分:2)

我不太了解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))))

答案 1 :(得分:2)

我很确定你想要这个

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参数。

答案 2 :(得分:2)

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

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

乘法和除法从左到右进行评估。