User Defined Mixtures through Low Level Interface through MATLAB

384 views
Skip to first unread message

MLaP

unread,
Feb 20, 2022, 9:17:08 AM2/20/22
to coolprop-users
Hi Everyone,

I am attempting to use CoolProp 6.4.1 to supply density and viscosity for calculations I am performing in MATLAB. Generally, I am providing pressure and enthalpy inputs. I have conducted these calculations using CoolProp 4.2.5 over the last several years, but would like to migrate to the newest version of CoolProp. I am usually using TTSE backend. 

For several reasons, using the Python wrapper via MATLAB is not practical for me in this case, so I am preferring to directly access the low level interface through the DLL. I have begun with the example in the documentation here: http://www.coolprop.org/coolprop/wrappers/MATLAB/index.html and made some minor modifications for my circumstances. (See attached, ml_test1.m). I am working in Windows 11, but have also successfully tested in Linux (Ubuntu 18.04). 

This is working very well to start, and I am thrilled with the simplicity and performance of this solution. However, I am running into two difficulties:
  • Obtaining properties for user defined mixtures. I am struggling to determine whether this is possible and which syntax to use. I have been attempting to get properties for user-defined mixture by trying multiple variations of the following code.  
backend = 'HEOS'; 
fluid = 'Methane[0.5]&Ethane[0.5]';
[handle, sh] = calllib('coolprop','AbstractState_factory',backend,fluid,ierr,herr,buffer_size);

In every case, the properties returned from subsequent calls are 0 value (i.e., vectors of the proper length with all values equal to 0). 
[outputs(1,1), so1] = calllib('coolprop','get_param_index','T');
[outputs(2,1), so2] = calllib('coolprop','get_param_index','P');
[outputs(3,1), so3] = calllib('coolprop','get_param_index','D');
[outputs(4,1), so4] = calllib('coolprop','get_param_index','VISCOSITY');
[outputs(5,1), so5] = calllib('coolprop','get_param_index','SPEED_OF_SOUND');
 
As a test, I have provided a known bogus input for fluid (e.g., fluid='spolied_milk') and I get the same result. So I am suspecting that the error is with the syntax with which I am defining the mixtures. So my question is how to properly specify user defined fluids in this circumstance?
  • Using REFPROP as the backend in this configuration?
Is this possible by setting backend='REFPROP'? If so, is there are recommendation on which version of REFPROP to use? 

I can provide more details if needed.


Thank you,
Mike

ml_test1.m

Ian Bell

unread,
Feb 20, 2022, 5:15:55 PM2/20/22
to coolpro...@googlegroups.com
In the low-level interface, first you build the model (without fractions), then you call AbstractState_set_fractions (here mole fractions) to set the fractions. Best to experiment with the "normal" low-level interface in something like Python to get the hang of it. Why can you not use Python interface? Much easier to use than the limited low-level interface.

Use REFPROP 10.

Also, you should always be checking the error message in addition to the error flag.


--
You received this message because you are subscribed to the Google Groups "coolprop-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coolprop-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coolprop-users/ca65fc14-0bf2-4cbf-87ec-43003997cd35n%40googlegroups.com.

Michael LaPresti

unread,
Feb 20, 2022, 10:44:37 PM2/20/22
to coolprop-users
Thank you very much for this help. I have added the call to AbstractState_set_fractions, in my test script, and this seems to work well. For example, 

backend = 'HEOS';
fluid = 'Methane&Ethane';
fluid_fractions = [0.2,0.8];

[handle, sh] = calllib('coolprop','AbstractState_factory',backend,fluid,ierr,herr,buffer_size);
calllib('coolprop','AbstractState_set_fractions',handle, fluid_fractions, length(fluid_fractions), ierr, herr, buffer_size);

This returns values for density and viscosity that are consistent with results that I obtain from more straightforward approach in Python. My updated test script is attached.

However, when I try to use TTSE or BICUBIC interpolation (by setting backend='BICUBC&HEOS' for example), then I am again receiving arrays full of 0s, and my error flag is returning 0. So I am now trying to diagnose this.

As for why I am not using Python -- these calculations fit into a large repository of legacy Matlab code that needs to remain compatible with both Windows and Linux. I prefer to avoid  adding dependencies for Python & Numpy in this situation if possible. I am using the Python interface for other projects. 
  
Thanks again!
ml_test2.m

Ian Bell

unread,
Feb 22, 2022, 9:49:44 PM2/22/22
to coolpro...@googlegroups.com
Please check the error string and report what you get

Michael LaPresti

unread,
Feb 27, 2022, 11:28:52 PM2/27/22
to coolprop-users
I am not getting any output in the error string. But I have completed some more testing with a 0.5/0.5 mixture of Ethane and Methane. The following backend definitions all seem to work properly:
  • HEOS
  • REFPROP
  • TTSE&REFPROP
  • BICUBIC&REFPROP
The only backends that are not working are the following, which are both producing 0-value arrays for all properties
  • TTSE&HEOS
  • BICUBIC&HEOS
My test file is attached. I have been manipulating line 31 to set the backend. P & T inputs are on line 40.
ml_test3.m

Ian Bell

unread,
Feb 28, 2022, 8:46:54 PM2/28/22
to coolpro...@googlegroups.com
Can you please test via the Python interface to see if that is working for this case ?

Michael LaPresti

unread,
Mar 1, 2022, 9:41:36 AM3/1/22
to coolprop-users
Yep, I will try it and report back ASAP.

Michael LaPresti

unread,
Mar 1, 2022, 9:08:17 PM3/1/22
to coolprop-users
Ok, I have performed additional testing using the Python interface via Matlab. Here is a summary of my testing:
  • Test #1
    • Interface: HLI
    • Backend: HEOS
    • call: rho(P,Q), P=101325, Q=0.3
    • Fluid: Ethane[0.2]&Methane[0.8]
    • Result: rho = 6.795308 kg.m3
  • Test #2
    • Interface: LLI
    • Backend: HEOS
    • call: rho(P,Q), P=101325, Q=0.3
    • Fluid: Ethane[0.2]&Methane[0.8]
    • Result: rho = 6.795308 kg/m3 
  • Test #3
    • Interface: LLI
    • Backend: TTSE&HEOS
    • call: rho(P,Q), P=101325, Q=0.3
    • Fluid: Ethane[0.2]&Methane[0.8]
    • Result: Python Error: ValueError: Not possible to determine whether pressure is inside or not
  • Test #4
    • Interface: LLI
    • Backend: TTSE&HEOS
    • call: rho(P,Q), P=101325, Q=0.3
    • Fluid: Water
    • Result: rho = 1.989295 kg/m3
  • Test #5
    • Interface: LLI
    • Backend: BICUBIC&HEOS
    • call: rho(P,Q), P=101325, Q=0.3
    • Fluid:  Ethane[0.2]&Methane[0.8]
    • Result: rho = 332.130565 kg/m3
Some additional notes:
  • In Test 3, ranging the Quality from 0 to 1 did not make any difference
  • In Test 3, I tried switching to PT_INPUTS with reasonable inputs and receive an error like: Python Error: ValueError: inputs are not in range, p=101325 Pa, T=373 K
  • In Test 3, BICUBIC&HEOS and BICUBIC&REFPROP successfully return a density, but the value is significant different: rho=332.130565 kg/m3 vs.  6.795308 kg/m3 for HEOS or REFPROP
  • TTSE&REFPROP fails in Test 3 with a similar error message.
Now, when I try to re-run Test #5 by directly accessing the low level interface through the DLL (like in my initial post), I get an access violation error (see attached Matlab crash dump log). 

Unfortunately, I'm not sure that this adds any clarity to the problem for me, but perhaps this information is helpful. My test script for Matlab is also attached. Please let me know if you would like other information.

Thank you,
Mike
matlab_crash_dump.9296-1
cp_lli_test.m

Ian Bell

unread,
Mar 6, 2022, 12:24:53 PM3/6/22
to coolpro...@googlegroups.com
If I remember correctly, I don't think that the tables should be allowed for 0 < Q < 1 for mixtures (because the other phase is at a different composition). I think that is at the heart of your problem, but you don't get an error either, and probably you should.

Can you look into the data stored in the saturation tables? There's code in python for that here that needs some slight modifications for your use case: http://www.coolprop.org/coolprop/Tabular.html#more-information

Michael LaPresti

unread,
Mar 10, 2022, 12:59:57 PM3/10/22
to coolprop-users
Ok, here's the test script in Python:

import msgpack, zlib, numpy as np
from io import BytesIO

with open(r'C:\Users\mikel\.CoolProp\Tables\HelmholtzEOSMixtureBackend(Methane[0.5000000000]&Ethane[0.5000000000])\single_phase_logph.bin.z','rb') as fp:
    ph = zlib.decompress(fp.read())
    values = msgpack.load(BytesIO(ph))
    revision, matrices = values[0:2]
    T,h,p,rho = np.array(matrices['T']), np.array(matrices['hmolar']), np.array(matrices['p']), np.array(matrices['rhomolar'])

The results: the tables for T, rho, p, and h are all 200x200 and all the elements have a value of inf. 

As a check, I ran this script for pure water, and get reasonable-looking results. So the script seems to work. 

Ian Bell

unread,
Mar 10, 2022, 9:54:57 PM3/10/22
to coolpro...@googlegroups.com
Of course, now I see it.  PH inputs are not supported for mixtures, but I am surprised that you didn't get a message if NO calculations succeed. Do you have access to REFPROP?

Michael LaPresti

unread,
Mar 11, 2022, 11:29:40 AM3/11/22
to coolpro...@googlegroups.com
Yes, I have refprop10 (based on your prior recommendation), which I have been using successfully.

Something else you'd like for me to test?

Ian Bell

unread,
Mar 11, 2022, 11:31:35 AM3/11/22
to coolpro...@googlegroups.com
My idea was to test REFPROP+TTSE. That *should* work, at least in principle.

Reply all
Reply to author
Forward
0 new messages