Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application breaks when UpdatePT is called in loop after using SetFraction #35

Open
nthanhhao96 opened this issue Mar 2, 2022 · 5 comments

Comments

@nthanhhao96
Copy link

nthanhhao96 commented Mar 2, 2022

I'm trying to build an app to get specific heat, density and dynamic viscosity of a mixture based on temperature, pressure and fraction. The way I do it is first SetFraction then call UpdatePT in a loop. However, the application always enter break mode after UpdatePT is called for a number of time.

Here's some simple code that I use to debug the problem:
`
using EngineeringUnits;
using EngineeringUnits.Units;
using SharpFluids;

Fluid Meg = new Fluid(FluidList.MixEthyleneGlycolAQ);
Meg.SetFraction(0.1);
for (var i = 0; i <= 100; i++)
{
Meg.UpdatePT(Pressure.FromAtmospheres(1), Temperature.FromDegreesCelsius(i));
Console.WriteLine("Specific Heat of MEG at " + i + " °C and fraction 0.1: " + Meg.Cp.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin));
Console.WriteLine("Density of MEG at " + i + " °C and fraction 0.1: " + Meg.Density.KilogramsPerCubicMeter);
Console.WriteLine("Dynamic Viscosity of MEG at " + i + " °C and fraction 0.1: " + Meg.DynamicViscosity.MillipascalSeconds);
}

Meg.SetFraction(0.2);
for (var i = 0; i <= 100; i++)
{
Meg.UpdatePT(Pressure.FromAtmospheres(1), Temperature.FromDegreesCelsius(i));
Console.WriteLine("Specific Heat of MEG at " + i + " °C and fraction 0.2: " + Meg.Cp.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin));
Console.WriteLine("Density of MEG at " + i + " °C and fraction 0.2: " + Meg.Density.KilogramsPerCubicMeter);
Console.WriteLine("Dynamic Viscosity of MEG at " + i + " °C and fraction 0.2: " + Meg.DynamicViscosity.MillipascalSeconds);
}
`

The error is:
`
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'CoolPropPINVOKE' threw an exception.
Source=SharpFluids
StackTrace:
at CoolPropPINVOKE.delete_DoubleVector(HandleRef jarg1)
at DoubleVector.Dispose()
at DoubleVector.Finalize()

This exception was originally thrown at this call stack:
[External Code]

Inner Exception 1:
TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception.

Inner Exception 2:
BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)
`

It seems that the application will enter break mode when the second loop reach ~51st time. Even if I use a different fluid, the result is still the same.
Am I doing something wrong or is it a bug?

@MadsKirkFoged
Copy link
Owner

I dont get any error running the same code, so it is hard to tell what is wrong.
Do you get the same error if you run other type of SharpFluid code?

@nthanhhao96
Copy link
Author

No, I don't have any problem running other SharpFluids code.

@nthanhhao96
Copy link
Author

nthanhhao96 commented Mar 4, 2022

I just updated SharpFluids version to 3.0.259 and it doesn't produce the error with the above code anymore. However, it will have problem if I increase the number of loops. For example, I add 3 more loops below (I'm using .NET 6)

`
using EngineeringUnits;
using EngineeringUnits.Units;
using SharpFluids;

Fluid Meg = new Fluid(FluidList.MixEthyleneGlycolAQ);
Meg.SetFraction(0.1);
for (var i = 0; i <= 100; i++)
{
Meg.UpdatePT(Pressure.FromAtmospheres(1), Temperature.FromDegreesCelsius(i));
Console.WriteLine("Specific Heat of MEG at " + i + " °C and fraction 0.1: " + Meg.Cp.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin));
Console.WriteLine("Density of MEG at " + i + " °C and fraction 0.1: " + Meg.Density.KilogramsPerCubicMeter);
Console.WriteLine("Dynamic Viscosity of MEG at " + i + " °C and fraction 0.1: " + Meg.DynamicViscosity.MillipascalSeconds);
}

Meg.SetFraction(0.2);
for (var i = 0; i <= 100; i++)
{
Meg.UpdatePT(Pressure.FromAtmospheres(1), Temperature.FromDegreesCelsius(i));
Console.WriteLine("Specific Heat of MEG at " + i + " °C and fraction 0.2: " + Meg.Cp.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin));
Console.WriteLine("Density of MEG at " + i + " °C and fraction 0.2: " + Meg.Density.KilogramsPerCubicMeter);
Console.WriteLine("Dynamic Viscosity of MEG at " + i + " °C and fraction 0.2: " + Meg.DynamicViscosity.MillipascalSeconds);
}

Meg.SetFraction(0.3);
for (var i = 0; i <= 100; i++)
{
Meg.UpdatePT(Pressure.FromAtmospheres(1), Temperature.FromDegreesCelsius(i));
Console.WriteLine("Specific Heat of MEG at " + i + " °C and fraction 0.3: " + Meg.Cp.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin));
Console.WriteLine("Density of MEG at " + i + " °C and fraction 0.3: " + Meg.Density.KilogramsPerCubicMeter);
Console.WriteLine("Dynamic Viscosity of MEG at " + i + " °C and fraction 0.3: " + Meg.DynamicViscosity.MillipascalSeconds);
}

Meg.SetFraction(0.4);
for (var i = 0; i <= 100; i++)
{
Meg.UpdatePT(Pressure.FromAtmospheres(1), Temperature.FromDegreesCelsius(i));
Console.WriteLine("Specific Heat of MEG at " + i + " °C and fraction 0.4: " + Meg.Cp.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin));
Console.WriteLine("Density of MEG at " + i + " °C and fraction 0.4: " + Meg.Density.KilogramsPerCubicMeter);
Console.WriteLine("Dynamic Viscosity of MEG at " + i + " °C and fraction 0.4: " + Meg.DynamicViscosity.MillipascalSeconds);
}

Meg.SetFraction(0.5);
for (var i = 0; i <= 100; i++)
{
Meg.UpdatePT(Pressure.FromAtmospheres(1), Temperature.FromDegreesCelsius(i));
Console.WriteLine("Specific Heat of MEG at " + i + " °C and fraction 0.5: " + Meg.Cp.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin));
Console.WriteLine("Density of MEG at " + i + " °C and fraction 0.5: " + Meg.Density.KilogramsPerCubicMeter);
Console.WriteLine("Dynamic Viscosity of MEG at " + i + " °C and fraction 0.5: " + Meg.DynamicViscosity.MillipascalSeconds);
}
`

@nthanhhao96
Copy link
Author

The above code seems to work ok with .NET 5. So I think the problem only exist in .NET 6

@Attemainio
Copy link
Contributor

This issue has been resolved. The issue was that, if you were using 64-bit operating system, disposing of DoubleVector did not check the environment, and assumed 32-but version in every system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants