Programming - usnistgov/REFPROP-docs GitHub Wiki

Programming

Linking with Other Applications

Users in the REFPROP community (as well as NIST staff) have provided samples of integrating REFPROP with other programming environments. These wrappers are stored in the REFPROP-wrappers repository on github. Currently examples are available of how to interface REFPROP with:

  • MATLAB
  • Microsoft Excel
  • C#
  • C++
  • Labview

Changing Fluids and Calling SETUP Multiple Times.

Calling SETUP (or SETUPdll) many many times can result in a memory loss error and in a substantial increase of computation speed. In many situations, it is better to load all of the fluids at the start of the program, calling SETUP only once. You can then switch between fluids through the use of calls to SETNC and PUREFLD. The example below shows how this can be done for a mixture combined with several pure fluids. If two different mixtures are required, load all fluids and set the composition to 0 for those fluids not involved in a particular application. This could easily be done by using two different arrays for the composition.

EX-MULTI.FOR

Calculation of the Critical Point and Saturation States in the Critical Region.

The calculation of saturation states requires complex algorithms and significant processor time. Version 9.1 introduced the subroutine SATSPLN, which can be called directly after the call to SETUP, and generates spline curves that represent the various properties that are required as initial guesses to the saturation algorithms in order to increase convergence and speed. This new subroutine, however, can take several seconds to implement, and should only be done once at the very beginning. If the composition of the fluid changes significantly, and if you notice problems with convergence, you may need to call it again with the new composition. Although the routine gives estimates only for saturation states, single phase calculations will also be much faster since a call to the saturation routine is required to determine the phase of the input state. The routine is called like this:

call SATSPLNdll (x, ierr, herr, 255)

(without the “dll” and 255 if calling directly from FORTRAN). Once the splines are generated, you can call the CRITP, MAXT, and MAXP routines to get the critical point and maximum values along the saturation lines (see the SAT_SUB.FOR file for details). The new subroutine SATGV can be called when the SATT, SATP, or SATTP routines fail in the critical region. For graphing the saturation lines without the need for states at any specific temperature or pressure, the SATGV routine can be called with density as the input, ranging from very low values in the gas phase, through the critical region, and then to high densities in the liquid.

In Excel or MATLAB, the call to SATSPLN is included in the code, but is deactivated. To activate this call, enter the code (press Alt-F11 in Excel) and search for this subroutine in the file (under “Modules” in Excel). Then read the comments above the line and activate the routine by removing the first character on that line. You may need to exit and restart Excel or MATLAB for this to work.

Calculation of Phase Diagrams for Plotting Purposes.

The calculation of the entire phase diagram can be difficult with the routines that require either temperature or pressure as inputs due to the second root in the retrograde region. An alternative routine called SATGV is available that makes this process simple through the use of density as the input; see the file Manual.txt in your Refprop\Fortran directory for a list of the inputs and outputs. In order to use this routine, you must first call subroutine SATSPLNdll, see the details above. Subroutine SATGVdll can then be called like this:

call SATGV (t,p,z,vf,d,1,6,1,rhox,rhoy,x,y,ierr,herr)

The inputs are composition in the z array and density (either vapor or liquid phase) in the variable d. The variable vf is double precision and should be set to 1.0. The routine will then return the temperature and pressure at this density. To obtain data points for the entire phase diagram, call this routine starting at a very low density in the vapor phase, and slowly increment the density until you are deep in the liquid phase at low temperatures. The other outputs are not necessary for this application.

FLSH Routines and Metastable States.

For subroutines such as ENTHALdll, ENTROdll, CVCPdll, etc., where temperature and density are the input properties, the output property data will differ from those returned when using routines such as PHFLSHdll or TPFLSHdll when in the two-phase region. Any routine (except TDFLSHdll) that uses temperature and density as inputs will return what appears to be erroneous results in the two-phase region. These results actually show calculations directly from the equation of state without taking into account the fact that the mixture has split into two phases. The results would be valid for such situations where a substance is heated beyond its boiling point (a metastable state), but without boiling (such as water in a glass container being heated in a microwave). The results eventually end up at the spinodal, beyond which it is no longer possible to increase the temperature without boiling the liquid. Thus, for typical results these routines should never be used for two-phase calculations, rather the FLSH routines should be called. The flash routines return the properties in the liquid and vapor states, and these can be used to call routines such as THERMdll, CVCPdll, TRNPRPdll, etc., with the associated liquid or vapor density at the saturated temperature.