usnistgov / REFPROP-wrappers

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

Window application ( C#.Net) is getting closed, if we call the refprop methods multiple times. #41

Closed SivaVeeranki closed 5 years ago

SivaVeeranki commented 6 years ago

Dear GitHub team,

             Currently we are developing  the WINDOWS application using C# Dot net (using Refprop wrapper)  which uses the REFPROP dll.

We are able to get the output values from REFPROP for first 3-4 times. If we call the Reprop methods in multiple times ( using for each loop), application is getting closed with below error message.

**Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in exe path

Additional information: The runtime has encountered a fatal error. The address of the error was at 0x5ff94572, on thread 0x28f0. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

An unhandled exception of type 'System.ExecutionEngineException' occurred in Unknown Module.**

Currently we are using Refprop v9.1

We need help on this issue.

ianhbell commented 6 years ago

Can you please file a minimal working example of code that causes this problem?

To be clear, REFPROP is NOT threadsafe, and this smells like a threading problem to me.

SivaVeeranki commented 6 years ago

Hi Ian, Thank you for your reply. Please find the below code. few methods taken from REFPROP.XLS file vba code (ex: setup, calc, convertUnits methods)

We call the GetCalcProp method from button click. it is work for up to 50 iterations for Windows applications, but for web application (#40 issue) it is not working even for single iteration (for loop). In web application we are getting the value but application is closing (#40 issue).

private void button1_Click(object sender, EventArgs e)
        {
            int ival, Jval = 0;
            double Prop1 = 70, Prop2 = 1, dblValueProp = 0;
            for (ival = 0; ival < 50; ival++)
            {
                for (Jval = 0; Jval < 50; Jval++)
                {
GetCalcProp("R407A.MIX", "TQ", "E", ref Prop1, ref Prop2, "-P", ref dblValueProp);
                     Prop1 = 70; Prop2 = 1; dblValueProp = 0;
GetCalcProp("R407A.MIX", "PQ", "E", ref Prop1, ref Prop2, "-t", ref dblValueProp);
                    Prop1 = 70; Prop2 = 1; dblValueProp = 0;
GetCalcProp("R407A.MIX", "PT", "E", ref Prop1, ref Prop2, "-H", ref dblValueProp);
                }                
            }          
        }
////////////////////////////////////////////////////////////////////////////////
public double GetCalcProp(string FluidName, string InpCode, string Units, ref double Prop1, ref double Prop2, string InpValueCode, ref double dblValueProp)
{
    Setup(FluidName);  // Setup method taken from Refpop.xls document
    if (ierr > 0)
           return dblError;
    try
    {
// ConvertUnits taken from Refpop.xls document
       ConvertUnits(InpCode, Units, ref Prop1, ref Prop2);  
       herr = "";  q = 0; t = 0; p = 0; d = 0; Dl = 0;
       Dv = 0; e = 0; h = 0; s = 0; Cvcalc = 0; Cpcalc = 0;
       w = 0;
// Calc method taken from Refpop.xls document              
    Calc(InpCode, ref Prop1, ref Prop2); 
    }
    catch (Exception ex)
    {}
     double dblProp2 = 0.0;

// ConvertUnits taken from Refpop.xls document
     return ConvertUnits(InpValueCode, Units, ref dblValueProp, ref dblProp2);
}
ianhbell commented 6 years ago

Can you please give me a copy-pastable example (a zip file is fine) that demonstrates this problem? I need to be able to replicate exactly the problem you are having. Have you purchased version 10 yet? It should be much more reliable for fluid loading issues like this.

SivaVeeranki commented 6 years ago

Calculations.zip Please note that we have not included mixtures and fluids in the attached file. please include them in debug folder. Now we are using Refprop version 9.1. please check with same dll.

ianhbell commented 6 years ago

First off - you should call SETMIXdll rather than parsing the .MIX file - REFPROP does that all for you.

In REFPROP 10, this whole exercise is a one-liner, including unit conversions, etc. Highly recommend that you upgrade.

In web application, how are you handling the fact that REFPROP is NOT thread-safe? You need to fork separate process for each request to web server. Also you need separate fluid files for each request to web server to avoid overlapping file reads.

SivaVeeranki commented 6 years ago

Hi Ian, Now we got the Refprop 10, but now we are getting attached error. This issue we are getting in windows application. capture

SivaVeeranki commented 6 years ago

Please suggest any solution to resolve this error for C# windows & Web application.

ibell commented 6 years ago

First obvious question, are you able to call SETUPdll? I think you are not allocating your strings to the right size it looks like, and that is the most common source of problems when calling the DLL. Can you please use the already written and tested C# interface to see how to do things: https://github.com/usnistgov/REFPROP-wrappers/tree/master/wrappers/Csharp

How do you propose to handle the fact that REFPROP is not thread-safe in your application? Are you going to fork processes and copy the files into each process?

SivaVeeranki commented 5 years ago

Thank you for your response,

    We are able to call the SETUPdll.

In old reprop 9.0 we need to use the convertunits method for pressure and density... calculations. In New reprop 10, also we need to use convertunits method or not required? or directly we will get it from IRefProp64.TQFLSHdll , IRefProp64.TPFLSHdll ,IRefProp64.PQFLSHdll methods.

We are tried in both ways, but values are not matching Please suggest me.

SivaVeeranki commented 5 years ago

If we call the refprop functions in loop (loop length is 100 times) we are getting the below error. I am getting access violation error after execute from the source on Windows 7, Visual Studio version 2015.

image

ianhbell commented 5 years ago

In REFPROP 10, your best bet is to use the REFPROPdll function, it handles the unit conversions for you, and removes the need for you to manage unit conversions (and other unpleasant things). Please start off with the C# wrapper that is online : https://github.com/usnistgov/REFPROP-wrappers/tree/master/wrappers/Csharp and then make sure your code reproduces those values.


From: SivaVeeranki notifications@github.com Sent: Thursday, September 27, 2018 5:04:48 AM To: usnistgov/REFPROP-wrappers Cc: Bell, Ian (Fed); Comment Subject: Re: [usnistgov/REFPROP-wrappers] Window application ( C#.Net) is getting closed, if we call the refprop methods multiple times. (#41)

Reopened #41https://github.com/usnistgov/REFPROP-wrappers/issues/41.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/usnistgov/REFPROP-wrappers/issues/41#event-1870751007, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ALaZLfV3vmsb3tBUHmYAo-FIBk4TxOWXks5ufLDPgaJpZM4UchSR.

SivaVeeranki commented 5 years ago

Thank you Ian,

But in the given C# wrapper, there is no direct methods for calculating the pressure and density, enthalpy, etc... If we try with out unit conversion method, we are getting wrong values. Before calling the IRefProp64.TQFLSHdll , IRefProp64.TPFLSHdll ,IRefProp64.PQFLSHdll methods, we need to call the ConvertUnits(InpCode, Units, ref Prop1, ref Prop2). In this method calculating the p,t,h,d values using input code "TP","PQ",TQ etc.

ianhbell commented 5 years ago

Your best bet is to update the interface to add the REFPROPdll function, following the model of the other functions. That allows for unit conversion internally.

SivaVeeranki commented 5 years ago

If we call the refprop functions in loop (loop length is 100 times) we are getting the below error. I am getting access violation error after execute from the source on Windows 7, Visual Studio version 2015.

image

ianhbell commented 5 years ago

What inputs do you call 100 times to get this error?

SivaVeeranki commented 5 years ago

Hi Ian,

We are getting the output values from Refprop functions, after getting these values we are placing in data table. Then we are trying to export those values to excel or binding to data grid view from data table, then we are getting above error and attached error

image

this issue is comming only if we call the refprop functions other wise it is not comming. It seems that it is COM component error.

ianhbell commented 5 years ago

Can you please specify exactly what inputs cause this problem? Otherwise it is impossible for us to debug given this amount of detail.


From: SivaVeeranki notifications@github.com Sent: Friday, October 12, 2018 4:51:09 AM To: usnistgov/REFPROP-wrappers Cc: Bell, Ian (Fed); Comment Subject: Re: [usnistgov/REFPROP-wrappers] Window application ( C#.Net) is getting closed, if we call the refprop methods multiple times. (#41)

Hi Ian,

We are getting the output values from Refprop functions, after getting these values we are placing in data table. Then we are trying to export those values to excel or binding to data grid view from data table, then we are getting above error and attached error

[image]https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F39990921%2F46865329-cdbde780-ce3a-11e8-8f83-36f746b391f9.png&data=02%7C01%7Cian.bell%40nist.gov%7C1d0a30f213ca438e344108d63030a5aa%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C636749382832661690&sdata=WtiVcCMexaS4POQrZvXjO%2FUwJp3iuzNJocYD0ToIDPU%3D&reserved=0

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/usnistgov/REFPROP-wrappers/issues/41#issuecomment-429286084, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ALaZLdZM_wUJiQLNiuTTvfPUywEoLEBGks5ukHQdgaJpZM4UchSR.

SivaVeeranki commented 5 years ago

Hi Ian,

Please check the attached sample for your reference, RefCalc.zip

Problem 1: If user clicks on First "RUN" button values are populating on grid, but application is going to crash while closing the application ( click on X icon in window)

Problem 2: If user clicks on Second "RUN" button, values are comming ( in debugging we are able to see) but unable to bind the data to data table.

If we execute the exe from code, then we are getting below error please find the below error message.

image

error

If we execute the exe direct from folder, then we are getting below error

image

image

ianhbell commented 5 years ago

I see what you mean, and I can reproduce the same behavior. I think this is the problem...

If I replace the line 71 in RefOperations.cs that used to read

herr = ""

with

herr = new String(' ', 255);

It works fine. A classic mistake, the buffer is too small, and elements are written beyond the allocated length in FORTRAN. Mixed-language programming is full of potential pitfalls like this.

SivaVeeranki commented 5 years ago

Thank you Ian, Now it is working fine.

In C# we used to initialize the string variables like herr ="", we missed this herr = new String(' ', 255);.

But finally we are getting values. Thank you so much for your quick response and solutions.

bigoper commented 1 year ago

Hi @SivaVeeranki

Thank you for providing us with your code sample.

I noticed that you've implemented a few additional methods in IRefProp64, which is awesome! I was wondering if you were also able to implement the IRefProp64.REFPROPdll method?

I tried to do so, but failed. I was wondering if you could take a look at it?



Thanks!!!
bigoper commented 1 year ago

Hi @EricLemmon and @ianhbell I was wondering if you guys had the opportunity to come by an implementation of IRefProp64.REFPROPdll that actually works?

For some reason I can't figure out why mine is not working.

Thanks!

ianhbell commented 1 year ago

Neither of us are C# users. I am afraid you are on your own. I recommend the CoolProp interface to REFPROP again, which I believe should work nicely.