usnistgov / REFPROP-issues

A repository solely used for reporting issues with NIST REFPROP
26 stars 13 forks source link

REFPROP subroutine in FORTRAN changes the composition array (z), even though it is an Input and not an Output. #554

Open KayvanA opened 1 year ago

KayvanA commented 1 year ago

I tried calculating the enthalpy of evaporation (h_fg) with REFPROP subroutine in Fortran; Molar and Mass-based calculations were consistent with the molecular weight. Therefore, I agree that the difference in REFPROP call in Excel and Fortran was in the reference state.

However, I noticed a new issue, which you may want to consider this bug fix in a future version of REFPROP:

When I use 'MASS BASE SI' and I call REFPROP Subroutine, it changes the composition from [0.697614699375863 0.302385300624138] to [0.5 0.5]. It is probably changing the composition from molar basis to mass basis. This results in the next call to REFPROP subroutine not working: ierr = 813 herr = '[REFPROP error 813] The input composition must match that of the predefined mixture when the mixture name is included in the call to REFPROP. To use a different composition, leave the fluid name blank. '

To fix this, I had to set the composition again before the next REFPROP call, which is obviously time-consuming and redundant: call SETMIXTURE (Refrigerant, z_composition, ierr) ! If I turn this SETMIXTURE Off, then z_composition will be messed up and next REFPROP call won't work.

According to the Refprop documentation, the composition (z) is an INPUT and not an OUTPUT. image https://refprop-docs.readthedocs.io/_/downloads/en/latest/pdf/ The subroutine REFPROP under REFPROP.FOR also lists z as an input.

Therefore, one would expect the composition to remain constant after the call to REFPROP. I should mention that I do set iMass=1 for my call to 'MASS BASE SI', in both REFPROP calls. Probably, somewhere in the subroutine, the composition gets altered and it is overwritten on the input value, which could be easily fixed by adding a new variable in the subroutine.

Lastly, this issue happens when I use 'MASS BASE SI'. When I switch back to 'MOLAR BASE SI', the composition remains the same before and after calling REFPROP subroutine.

I will paste the relevant parts of my FORTRAN code for reference in the next comment. Please let me know if you need the full code.

ianhbell commented 1 year ago

The post is wordy, I think it can be summarized to:

The REFPROP function changes the composition array z when it is passed in and the unit system is MASS BASE SI (or any mass-based system).

ianhbell commented 1 year ago

The more focused the post, the easier to figure out what is wrong.

This problem doesn't happen in Python because the Python interface copies all inputs, and z is considered to be an input.

ianhbell commented 1 year ago

Can you simplify your example to what is actually needed to demonstrate this bug?

ianhbell commented 1 year ago

It should for instance print out z, call REFPROP, then print out z. That should be enough to demonstrate the problem

Also, your code is not copy-paste able.

ianhbell commented 1 year ago

Sure it is copy-paste able, but I still need to define all the variables.

ianhbell commented 1 year ago

We need the minimal working example that demonstrates the failure. As short as possible, with no extra fluff, and we can compile the code directly.

KayvanA commented 1 year ago

FORTRAN CODE:

    subroutine test_call3()

      implicit none

      integer, parameter :: ncmax=20, ipropmax=200
      double precision x, xcalc, z_composition(ncmax), Output(ipropmax) 
      double precision xx(ncmax), yy(ncmax), zz(ncmax)
      integer iUnit, iMass, ierr, iUCode, iFlag 
      character*255 Refrigerant, hUnits, herr
      double precision T, Hf_REFPROP_Mass , Hg_REFPROP_Mass     

      Refrigerant='R410A'

      ! Calling REFPROP with MASS BASE SI
      iMass = 1 ! The inputs variables are mass fraction based
      call GETENUM(0,'MASS BASE SI',iUnit,ierr,herr) ! This returns iUnit = 21
      call SETMIXTURE (Refrigerant, z_composition, ierr)

      print *,'Composition before calling REFPROP is: ', z_composition 
      call REFPROP (Refrigerant,'TQ','H',iUnit,iMass,iFlag,T,x,z_composition,Output,hUnits,iUCode,xx,yy,zz,xcalc,ierr,herr)
      Hf_REFPROP_Mass = Output(1)
      print *,'Composition after calling REFPROP is: ', z_composition

    end subroutine

Yielding:

Composition before calling REFPROP is:  [0.697614699375863 0.302385300624138]
Composition after calling REFPROP is: [0.5 0.5]
ianhbell commented 1 year ago

Please remove all the unused variables, and focus on only this bug report.

ianhbell commented 1 year ago

And please also show the output of the code

ianhbell commented 1 year ago

Sort of thing we need: https://github.com/usnistgov/REFPROP-wrappers/issues/487#issuecomment-1292332289

KayvanA commented 1 year ago

Ian, I have updated the code per your example. It is as short as possible, with the bare minimum variables, and the outputs are shown separately. Please let me know if you need anything else.

ianhbell commented 1 year ago

That is perfect. Thank you!

ianhbell commented 1 year ago

@EricLemmon Can you please look into this?

KayvanA commented 1 year ago

You are welcome, Ian. Thank you for your help earlier.

ianhbell commented 1 year ago

You can also tag the language in the code block, I added that to your example

KayvanA commented 1 year ago

I see, thank you!

ianhbell commented 1 month ago

Same as #644