usnistgov / REFPROP-wrappers

Wrappers around NIST REFPROP for languages such as Python, MATLAB, etc.
195 stars 127 forks source link

Working C# REFPROPdll Wrapper #468

Closed ora8 closed 1 year ago

ora8 commented 2 years ago

Has any body a working wrapper in C# for REFPROPdll. My one does not work.

        /// <summary>
        ///     REFPROP method, not yet working
        /// </summary>
        [DllImport("REFPRP64.dll", CharSet = CharSet.Ansi)]
        public static extern void REFPROPdll(
              [MarshalAs(UnmanagedType.VBByRefStr)] ref string hFld,
              [MarshalAs(UnmanagedType.VBByRefStr)] ref string hIn,
              [MarshalAs(UnmanagedType.VBByRefStr)] ref string hOut,
              ref long iUnits,
              ref long iMass,
              ref long iFlag,
              ref double a,
              ref double b,
              [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] z,
              [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] output,
              [MarshalAs(UnmanagedType.VBByRefStr)] ref string hUnits,
              ref long iUCode,
              [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x,
              [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] y,
              [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x3,
              ref double q,
              ref long ierr,
              [MarshalAs(UnmanagedType.VBByRefStr)] ref string herr,
              ref long hFld_length,
              ref long hIn_length,
              ref long hOut_length,
              ref long hUnits_length,
              ref long herr_length
            );

The app always crashes.

ianhbell commented 2 years ago

Two options: 1) The CoolProp wrapper for REFPROP: http://www.coolprop.org/coolprop/wrappers/Csharp/index.html 2) https://github.com/usnistgov/REFPROP-wrappers/tree/master/wrappers/Csharp

ora8 commented 2 years ago

Thanks, but the upper routine REFPROPdll is not implement by the C# Wrapper. I think that my declaration is correct. I made a mistake in calling it.

ianhbell commented 2 years ago

Personally, and not just because I wrote it, I prefer the interface in CoolProp.

If you wanted to add the missing high-level functions, I am sure others would appreciate it. I am not a C# user.

ora8 commented 2 years ago

REFPROPddl is the only function needed in REFPROP 10. For example to get the molar mass I used this call.

                 var hfld = string.Empty;
                var x = new double[NcMax];
                var i = 0;
                foreach (var item in items)
                {
                    if (!string.IsNullOrEmpty(hfld))
                        hfld += "|";
                    hfld += item.RefPropName;
                    x[i] = item.Percentage / 100.0;
                    i++;
                }
                hfld += new string(' ', 10000 - hfld.Length);

                /// Normalize the concentrations, there could be a difference of 0.00001
                NormalizeFractions(x);

                var iErr = 0L;

                var q = 0.0;
                var iUnits = 0L;
                long iUCode = 0L;
                var iMass = 1L;
                var iFlag = 0L;
                var a = 0.0;
                var b = 0.0;
                var z = new double[NcMax];
                var output = new double[200];
                var y = new double[NcMax];
                var x3 = new double[NcMax];
                var hFld_length = 10000L;
                var hIn_length = 255L;
                var hOut_length = 255L;
                var hUnits_length = 255L;
                var herr_length = 255L;

                var hIn = new string(' ', 255);
                var hOut = "M" + new string(' ', 254);
                var hUnits = new string(' ', 255);
                var herr = new string(' ', 255);

                IRefProp64.REFPROPdll(ref hfld, ref hIn, ref hOut, ref iUnits, ref iMass, ref iFlag, ref a, ref b, z, output, ref hUnits, ref iUCode,
                    x, y, x3, ref q, ref iErr, ref herr, ref hFld_length, ref hIn_length, ref hOut_length, ref hUnits_length, ref herr_length); 
                if (iErr != 0L)
                {
                    MessageBox.Show(herr, "RefProp REFPROPdll error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return string.Empty;
                }
                var molarMass = output[0];
        private void NormalizeFractions(double[] fractions)
        {
            /// Normalize the concentrations, there could a difference of 0.00001
            var total = fractions.Sum();
            if (total != 1.0)
            {
                for (var i = 0; i < fractions.Length; i++)
                    fractions[i] /= total;
            }
        }
bigoper commented 1 year ago

@ora8 REFPROPdll is NOT part of IRefProp64. Where did you get it? did you write/implemented it yourself? Thanks!

ora8 commented 1 year ago

RefProp64.dll is Part of Refprop. See REFPROP NIST website. RefProp.dll is the 32 Bit Version.

ora8 commented 1 year ago

https://www.nist.gov/srd/refprop

bigoper commented 1 year ago

Hi @ora8 I think that you got this wrong. I know that RefProp64.dll is part of REFPROP, but in your example you are using IRefProp64.REFPROPdll, which is the interface for RefProp64.dll.

Now, if you inspect the IRefProp64 interface, you'll see that REFPROPdll is NOT implemented there. So you've either updated the interface code, or you have another/diff version of the interface.

I hope this. makes sense.

ora8 commented 1 year ago

You are right. It is some times ago, I posted this. In the Version 10 of REFPROP there is a method REFPROPdll implemented. I tried to call this method with the posted wrapper of IRefProp64. I tested it with posted sample. It seems to work At the Moment I use Version 10 with the old API amd not with the REFPROPdll method. Perhaps You can test it if my wrapper is correct. I think it is so. If I have time I will replace the old REFProp API calls with the REFPROPdll method. My co worker is afraid that the the old API (before Version 10) will not be supported in future Releases.

ianhbell commented 1 year ago

Two small comments: