SUBROUTINE vspump742(par,xin,out)

implicit none
    INTEGER, PARAMETER :: iknd=SELECTED_INT_KIND(8)  ! nobody does this
    INTEGER, PARAMETER :: DP=SELECTED_REAL_KIND(14)  ! this is double precision
    INTEGER, PARAMETER :: SP=SELECTED_REAL_KIND(6)  ! this is single precision
!-----------------------------------------------------------------------------------------------------------------------
!    DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES
real(DP) F_MOTORLOSS,T_IN,RHO_FLUID,T_OUT,FLOW_OUT, &
  CP_FLUID,EFF_MOTOR,FLOW_IN,POWER,Q_FLUID,Q_AMBIENT,EFF_TOTAL, &
  PRESSURE_DROP,WORK,EFF_PUMP,POW_SHAFT
real(DP) :: par(3),xin(5)
real(DP) :: out(5)
logical :: first_run
save

!if(.not.first_run)then
    !allocate(out(5))
    CP_FLUID = Par(1)
    RHO_FLUID = Par(2)
    F_MOTORLOSS = Par(3)
    
    out = 0
    out(1) = xin(1)

    IF(CP_FLUID <= 0.0) write(*,10) "specific heat < 0"
    IF(RHO_FLUID < 0.0) write(*,10) "density < 0"
    IF((F_MOTORLOSS < 0.0) .OR. (F_MOTORLOSS > 1.0)) write(*,10) "motor efficiency < 0 or > 1"
    10 format(a)
!    first_run = .true.
!else
!-----------------------------------------------------------------------------------------------------------------------
!    RETRIEVE THE CURRENT VALUES OF THE INPUTS TO THIS MODEL FROM THE XIN ARRAY IN SEQUENTIAL ORDER
    T_IN = xin(1)
    FLOW_IN = xin(2)
    EFF_TOTAL = xin(3)
    EFF_MOTOR = xin(4)
    PRESSURE_DROP = xin(5)
    
    IF(FLOW_IN < 0.) write(*,20) "flow < 0"
    20 format(a)
    IF((EFF_TOTAL < 0.D0).OR.(EFF_TOTAL > 1.D0)) write(*,10) "total efficiency < 0 or > 1"

    IF((EFF_MOTOR < 0.D0).OR.(EFF_MOTOR > 1.D0)) write(*,10) "motor efficiency < 0 or > 1"

    IF(EFF_MOTOR < EFF_TOTAL) write(*,10) "motor efficiency < total efficiency"
    IF(PRESSURE_DROP < 0.) write(*,10) "pressure drop < 0"

    !    PUMP IS OFF
    IF(FLOW_IN <= 0.) THEN
        T_OUT =T_IN
        FLOW_OUT =0.
        POWER =0.
        Q_FLUID =0.
        Q_AMBIENT =0.

        !    PUMP IS ON
    ELSE

        !       SET THE OUTLET FLOW RATE
         FLOW_OUT=FLOW_IN

        !       SET THE IDEAL POWER CONSUMPTION
         WORK=PRESSURE_DROP*FLOW_OUT/RHO_FLUID

        !       CALCULATE THE PUMPING EFFICIENCY
         EFF_PUMP=EFF_TOTAL/EFF_MOTOR

        !       CALCULATE THE SHAFT POWER
         POW_SHAFT=WORK/EFF_PUMP

        !       CALCULATE THE TOTAL POWER
         POWER=POW_SHAFT/EFF_MOTOR

        !       SET THE HEAT TRANSFER TO THE FLUID AND THE POWER CONSUMPTION
        Q_FLUID=POW_SHAFT*(1.-EFF_PUMP)+(POWER-POW_SHAFT)*F_MOTORLOSS
        Q_AMBIENT=POWER-Q_FLUID

        !       CALCULATE THE OUTLET TEMPERATURE
        T_OUT=T_IN+Q_FLUID/FLOW_OUT/CP_FLUID
        
    ENDIF
        out(1) = T_OUT
        out(2) = FLOW_OUT
        out(3) = Power
        out(4) = Q_FLUID
        out(5) = Q_AMBIENT

!endif

RETURN 
END subroutine vspump742
