Closed SivaVeeranki closed 5 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.
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);
}
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.
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.
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.
Hi Ian, Now we got the Refprop 10, but now we are getting attached error. This issue we are getting in windows application.
Please suggest any solution to resolve this error for C# windows & Web application.
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?
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.
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.
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.
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.
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.
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.
What inputs do you call 100 times to get this error?
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
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.
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
— 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.
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.
If we execute the exe direct from folder, then we are getting below error
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.
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.
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?
Implementation
/***************************************************************************************
* Calculate the properties identified in the hOut string for the inputs specified in the
* hIn string for the fluid or mixture given in the hFld string.
**************************************************************************************/
[DllImport(@"C:\Program Files (x86)\REFPROP\REFPRP64.dll", CharSet = CharSet.Ansi)]
public static extern void REFPROPdll
(
[MarshalAs(UnmanagedType.VBByRefStr)] ref string hFld,
[MarshalAs(UnmanagedType.VBByRefStr)] ref string hIn,
ref string hOut, // (OUTPUT) hOut [char,in] :: Various flags are available to gain access to all other features of the Refprop program.
ref int iUnits, // (OUTPUT) • iUnits [int,in] :: The unit system to be used for the input and output properties (such as SI, English, etc.)
ref int iMass, // (OUTPUT) • iMass [int,in] :: Specifies if the input composition is mole or mass based
ref int iFlag, // (OUTPUT) iFlag [int,in] :: Flag to specify if the routine SATSPLN should be called (where a value of 1 activates the call).
ref int a, // (OUTPUT) a[double,in] :: First input property as specified in the hIn variable
ref int b, // (OUTPUT) b [double,in] :: Second input property as specified in the hIn variable
ref double[] z, // (OUTPUT) z (20) [double,in] :: Composition on a mole or mass basis depending on the value sent in iMass (array of size ncmax= 20).
ref double[] Output, // (OUTPUT) Output (200) [double,out] :: Array of properties specified by the hOut string (array of size 200 dimensioned as double precision).
ref string hUnits, // (OUTPUT) hUnits [char,out] :: The units for the first property in the Output array.
ref double iUCode, // (OUTPUT) iUCode [int,out] :: Unit code that represents the units of the first property in the Output array
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x, // (OUTPUT) x (20) [double,out] :: Composition of the liquid phase (array of mole fractions of size 20) for two-phase states on a mole or mass basis depending on the value of iMass.
ref double[] y, // (OUTPUT) y (20) [double,out] :: Composition of the vapor phase (array of mole fractions of size 20) for two-phase states on a mole or mass basis depending on the value of iMass.
ref double []x3, // (OUTPUT) x3 (20) [double,out] :: Reserved for returning the composition of a second liquid phase for LLE or VLLE.
ref double q, // (OUTPUT) q [double,out] :: Vapor quality on a mole or mass basis depending on the value of iMass.
ref long ierr, // (OUTPUT) ierr [int,out] :: Error flag
[MarshalAs(UnmanagedType.VBByRefStr)] ref string herr, // (OUTPUT) error string
ref long hFld_length, // (OUTPUT) hFld_length [int] :: length of variable hFld (default: 10000)
ref int hIn_length, // (OUTPUT) hIn_length [int] :: length of variable hIn (default: 255)
ref int hOut_length, // (OUTPUT) hOut_length [int] :: length of variable hOut (default: 255)
ref int hUnits_length, // (OUTPUT) hUnits_length [int] :: length of variable hUnits(default: 255)
ref int herr_length // (OUTPUT) herr_length [int] :: length of variable herr (default: 255)
);
Usage
string hfld = "methane|ethane|propane|butane|isobutan|pentane|ipentane|nitrogen";
double[] x = new double[20];
x[0] = Convert.ToDouble(1);
x[1] = Convert.ToDouble(0);
x[2] = Convert.ToDouble(0);
x[3] = Convert.ToDouble(0);
x[4] = Convert.ToDouble(0);
x[5] = Convert.ToDouble(0);
x[6] = Convert.ToDouble(0);
x[7] = Convert.ToDouble(0);
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;
int iUnits = 0;
double iUCode = 0;
int iMass = 1;
int iFlag = 0;
int a = 0;
int b = 0;
var z = new double[255];
var output = new double[200];
var y = new double[255];
var x3 = new double[255];
var hFld_length = 10000L;
int hIn_length = 255;
int hOut_length = 255;
int hUnits_length = 255;
int herr_length = 255;
var hIn = new string(' ', 255);
var hOut = "M" + new string(' ', 254);
var hUnits = new string(' ', 255);
var herr = new string(' ', 255);
try
{
IRefProp64.REFPROPdll(ref hfld, ref hIn, ref hOut, ref iUnits, ref iMass, ref iFlag, ref a, ref b, ref z, ref output, ref hUnits, ref iUCode,
x, ref y, ref 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", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var molarMass = output[0];
}
catch (ApplicationException ae)
{
MessageBox.Show(ae.ToString());
}
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;
}
}
Thanks!!!
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!
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.
Dear GitHub team,
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.