SUBROUTINE vspump743(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
!C-----------------------------------------------------------------------------------------------------------------------
!C    DESCRIPTION:
!C    THIS ROUTINE MODELS A SIMPLE VARIABLE SPEED PUMP WHERE THE FLOW RATE IS PROVIDED AS AN INPUT.  THIS
!C    COMPONENT DOES NOT SET THE FLOW RATE LIKE OTHER STANDARD TRNSYS PUMP MODELS. THE USER ENTERS COEFFICIENTS 
!C    THAT RELATE THE DIMENSIONLESS POWER TO THE NORMALIZED FLOW RATE.
!C
!C    REVISION HISTORY
!C     1/04 - JWT - UPDATED AND READIED FOR V16
!C-----------------------------------------------------------------------------------------------------------------------
! Copyright © 2004 Thermal Energy System Specialists, LLC. All rights reserved.

!    DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES
Parameter N_COEFFS_MAX = 6
real(DP), intent(in) :: par(11),xin(4)
real(DP) :: FLOW_RATED,CP_FLUID,POWER_RATED,F_MOTORLOSS,Y(N_COEFFS_MAX)
real(DP) :: T_IN,FLOW_IN,EFF_TOTAL,EFF_MOTOR
real(DP), intent(out) :: out(5)
real(DP) :: T_OUT,FLOW_OUT,POWER,Q_FLUID,Q_AMBIENT
real(DP) :: FRAC_RATED_FLOW,Y_POWER,POW_SHAFT,EFF_PUMP 
INTEGER :: N_COEFFS
integer :: J
logical :: first_run
save

!if(.not.first_run)then
    FLOW_RATED = par(1)
    CP_FLUID = par(2)
    POWER_RATED = par(3)
    F_MOTORLOSS = par(4)
    N_COEFFS = par(5)
    do j = 1,n_coeffs
        Y(j) = par(5+j)
    enddo  
    
    out = 0
    out(1) = xin(1)  
    first_run = .true.

    IF((N_COEFFS < 0).OR.(N_COEFFS > N_COEFFS_MAX)) THEN
      write(*,510) "number of coefficients out of range"
      RETURN
    ENDIF
    510 format(a)

    IF(FLOW_RATED < 0.0) write(*,510) "rated flow < 0"
    IF(CP_FLUID <= 0.0) write(*,510) "specific heat is <= 0"
    IF(POWER_RATED < 0.0) write(*,510) "rated power < 0"
    IF((F_MOTORLOSS < 0.0).OR.(F_MOTORLOSS > 1.0)) write(*,510) "motor loss out of range"

    IF(FLOW_IN < 0.) write(*,510) "flow < 0"
    IF(EFF_TOTAL < 0.) write(*,510) "total efficiency < 0"
    IF(EFF_TOTAL > 1.) write(*,510) "total efficiency > 1"
    IF(EFF_MOTOR < 0.) write(*,510) "motor efficiency < 0"
    IF(EFF_MOTOR > 1.) write(*,510) "motor efficiency > 1"
    IF(EFF_MOTOR < EFF_TOTAL) write(*,510) "motor efficiency < total efficiency"
!else
    T_IN = xin(1)
    FLOW_IN = xin(2)
    EFF_TOTAL = xin(3)
    EFF_MOTOR = xin(4)
    
!    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 MASS FLOW RATE VARIABLES
        FLOW_OUT = FLOW_IN
        FRAC_RATED_FLOW = FLOW_OUT/FLOW_RATED

    !       CALCULATE THE POWER MULTIPLIER
        IF(FRAC_RATED_FLOW > 0.) THEN
            Y_POWER = 0.
            DO J = 1,N_COEFFS
              Y_POWER = Y_POWER+Y(J)*(FRAC_RATED_FLOW**DBLE(J-1))
            ENDDO
        ELSE
            Y_POWER = 0.
        ENDIF

    !       SET THE POWER CONSUMPTION
        POWER = Y_POWER*POWER_RATED

    !       SET THE SHAFT POWER
        POW_SHAFT=POWER*EFF_MOTOR

    !       CALCULATE THE PUMPING EFFICIENCY
        EFF_PUMP=EFF_TOTAL/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 vspump743
