Search code examples
c#fortranportingfortran77

Difficulty in understanding fortran arthimetic


I've been trying to make sense of the following subroutine but cant seem to get it right:

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

I have followed the information on arithmetic precedence mentioned here, but can't seem to get the equivalent C# function(shown below) to spit out the similar output.

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;
}

UPDATE: The is the code in my test case that keeps failing

    [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);
    }

When I printed the values off from Fortran, this is what I got:

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

Please help! any advice would be helpful!


Solution

  • I'm pretty sure you want this

    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;
    }
    

    Then you can use it like

    float pressure_drop = calculatePressureDrop(...);
    

    You're problem was that you were not giving the highest precidence to the power operators. Also in general it's best to avoid ref parameters if you can.