anchor.diff:  !
   !    This file is part of the NWTC Subroutine Library.
   !
  -!    ServoDyn is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as
  +!    NWTC_Num is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as
   !    published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   !
   !    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  
positive.code:    RETURN
     END SUBROUTINE MPi2Pi
  !=======================================================================
     SUBROUTINE RombergInt(f, a, b, R, err, eps, ErrStat)
  
        ! This routine is used to integrate funciton f over the interval [a, b]. This routine
        ! is useful for sufficiently smooth (e.g., analytic) integrands, integrated over
        ! intervals which contain no singularities, and where the endpoints are also nonsingular.
        !
        ! f is an external function. For example f(x) = 1 + x.
        !
        !   FUNCTION f(x)
        !      USE PRECISION
        !      IMPLICIT NONE
        !
        !      REAL(ReKi) f
        !      REAL(ReKi) x
        !
        !      f = 1 + x
        !
        !      RETURN
        !   END FUNCTION f
  
     IMPLICIT NONE
  
        ! Argument declarations:
  
     REAL(ReKi), EXTERNAL              :: f               ! Integrand function name
     REAL(ReKi), INTENT(IN)            :: a               ! Lower integration limit
     REAL(ReKi), INTENT(IN)            :: b               ! Upper integration limit
     REAL(ReKi), INTENT(IN)            :: eps             ! Absolute error bound
     REAL(ReKi), INTENT(OUT)           :: R               ! The result of integration
     REAL(ReKi), INTENT(OUT)           :: err             ! Actual absolute error
     INTEGER, INTENT(OUT), OPTIONAL    :: ErrStat         ! Error status; if present, program does not abort on error
  
        ! Local declarations:
  
     INTEGER                           :: m, i, j, k
     INTEGER, PARAMETER                :: mmax = 50       ! Maximum iteration number for m
     INTEGER, PARAMETER                :: imax = 50       ! Maximum iteration number for i
  
     REAL(ReKi), ALLOCATABLE           :: T(:,:)
     REAL(ReKi)                        :: h               ! Step length
     REAL(ReKi)                        :: sumf
  
        ! Initialize T
     ALLOCATE( T( mmax, imax ) )
     T = 0
  
     T(1, 1) = 0.5*(b - a)*( f(a) + f(b) )
  
     k = 2
     DO m = 1, mmax-2
        h = (b-a)*(0.5)**m
  
        sumf = 0
        DO i = 1, 2**(m-1)
           sumf = sumf + f(a + (2*i-1)*h)
           k = k + 1
        END DO
  
  
        T( m+1, 1) = 0.5*T( m, 1 )+ h * sumf
  
        DO j = 1, m
           T(m-j+1, j+1) = ( 4.0**j * T(m-j+2, j) - T(m-j+1, j) )/(4.0**j - 1.0)
  
              ! absolute error
           err = ABS( T(m-j+1, j+1) - T( m-j+2, j ) )
  
              ! set k >=9 to prevent early terminations
           IF( (err .LT. eps) .and. (k >= 9) ) THEN
  
                 ! return the intergration result if the conditions are met
              R = T(m-j+1, j+1)
  
              IF( ALLOCATED(T) ) DEALLOCATE(T)
  
              RETURN
           END IF
  
        END DO
  
     END DO
  
     err = ABS( T(m-j+1, j+1) - T( m-j+2, j ) )
     R = T(m-j+1, j+1)
  
     IF( ALLOCATED(T) ) DEALLOCATE(T)
  
        ! Return error message if the maximum iteration number is reached.
     CALL ProgAbort ( ' In subroutine RombergInt, the iteration reaches the maximum number. The integration did NOT converge! ', &
                      PRESENT(ErrStat) )
     IF ( PRESENT(ErrStat) ) THEN
        ErrStat = 1
        RETURN
     END IF
  
     RETURN
  END SUBROUTINE RombergInt
  !=======================================================================
     SUBROUTINE SetConstants( )
  
        ! This routine computes numeric constants stored in the NWTC Library
  
  !   USE, INTRINSIC :: ieee_arithmetic  !use this for compilers that have implemented
  
        ! local variables for getting values of NaN and Inf (not necessary when using ieee_arithmetic)
     REAL(DbKi)                          :: Neg_D          ! a negative real(DbKi) number
     REAL(ReKi)                          :: Neg            ! a negative real(ReKi) number
  
  
        ! Constants based upon Pi:
  
     Pi_D      = ACOS( -1.0_DbKi )
     D2R_D     = Pi_D/180.0_DbKi
     R2D_D     = 180.0_DbKi/Pi_D
     PiBy2_D   = Pi_D/2.0_DbKi
     RPM2RPS_D = Pi_D/30.0_DbKi
     RPS2RPM_D = 30.0_DbKi/Pi_D
     TwoByPi_D =  2.0_DbKi/Pi_D
     TwoPi_D   =  2.0_DbKi*Pi_D
     Inv2Pi_D  =  0.5_DbKi/Pi_D    ! 1.0_DbKi/TwoPi_D
  
     Pi      = ACOS( -1.0_ReKi )
     D2R     = Pi/180.0_ReKi
     R2D     = 180.0_ReKi/Pi
     PiBy2   = Pi/2.0_ReKi
     RPM2RPS = Pi/30.0_ReKi
     RPS2RPM = 30.0_ReKi/Pi
     TwoByPi =  2.0_ReKi/Pi
     TwoPi   =  2.0_ReKi*Pi
     Inv2Pi  =  0.5_ReKi/Pi        ! 1.0/TwoPi
  
  
        ! IEEE constants:
  
  !   NaN_D = ieee_value(0.0_DbKi, ieee_quiet_nan)
  !   Inf_D = ieee_value(0.0_DbKi, ieee_positive_inf)
  !
  !   NaN   = ieee_value(0.0_ReKi, ieee_quiet_nan)
  !   Inf   = ieee_value(0.0_DbKi, ieee_positive_inf)
  
  #ifndef FPE_TRAP_ENABLED
        ! set variables to negative numbers to calculate NaNs (compilers may complain when taking sqrt of negative constants)
     Neg   = -1.0_ReKi
     Neg_D = -1.0_DbKi
  
     NaN_D = SQRT ( Neg_D )
     Inf_D = Pi_D / 0.0_DbKi
  
     NaN   = SQRT ( Neg )
     Inf   = Pi / 0.0_ReKi
  #endif
  
  
  
negative.code: 
        ! Global numeric-related variables.
  
     REAL(DbKi)                                :: D2R_D                         ! Factor to convert degrees to radians in double precision
     REAL(DbKi)                                :: Inf_D                         ! IEEE value for NaN (not-a-number) in double precision
     REAL(DbKi)                                :: Inv2Pi_D                      ! 0.5/Pi (1/(2*Pi)) in double precision
     REAL(DbKi)                                :: NaN_D                         ! IEEE value for Inf (infinity) in double precision
     REAL(DbKi)                                :: Pi_D                          ! Ratio of a circle's circumference to its diameter in double precision
     REAL(DbKi)                                :: PiBy2_D                       ! Pi/2 in double precision
     REAL(DbKi)                                :: R2D_D                         ! Factor to convert radians to degrees in double precision
     REAL(DbKi)                                :: RPM2RPS_D                     ! Factor to convert revolutions per minute to radians per second in double precision
     REAL(DbKi)                                :: RPS2RPM_D                     ! Factor to convert radians per second to revolutions per minute in double precision
     REAL(DbKi)                                :: TwoByPi_D                     ! 2/Pi in double precision
     REAL(DbKi)                                :: TwoPi_D                       ! 2*Pi in double precision
  
  
     REAL(ReKi)                                :: D2R                           ! Factor to convert degrees to radians
     REAL(ReKi)                                :: Inf                           ! IEEE value for NaN (not-a-number)
     REAL(ReKi)                                :: Inv2Pi                        ! 0.5/Pi = 1 / (2*pi)
     REAL(ReKi)                                :: NaN                           ! IEEE value for Inf (infinity)
     REAL(ReKi)                                :: Pi                            ! Ratio of a circle's circumference to its diameter
     REAL(ReKi)                                :: PiBy2                         ! Pi/2
     REAL(ReKi)                                :: R2D                           ! Factor to convert radians to degrees
     REAL(ReKi)                                :: RPM2RPS                       ! Factor to convert revolutions per minute to radians per second
     REAL(ReKi)                                :: RPS2RPM                       ! Factor to convert radians per second to revolutions per minute
     REAL(ReKi)                                :: TwoByPi                       ! 2/Pi
     REAL(ReKi)                                :: TwoPi                         ! 2*Pi
  
     INTEGER, ALLOCATABLE                      :: IntIndx  (:,:)                ! The array of indices holding that last index used for interpolation in IntBlade()
     INTEGER, PARAMETER                        :: BITS_IN_ADDR  = C_INTPTR_T*8  ! The number of bits in an address (32-bit or 64-bit).
  
     TYPE, PUBLIC               :: CubSplineType                                ! This derived type is used to hold data for performing cubic splines.
        INTEGER                                :: NumPts                        ! The number of points in the XAry and YAry arrays.
        REAL(ReKi), ALLOCATABLE                :: Coef      (:,:)               ! The NumPts-1 length array of cubic coefficients.  The second dimension must be "0:3".
        REAL(ReKi), ALLOCATABLE                :: XAry      (:)                 ! The NumPts length array of x values for the interpolation.
        REAL(ReKi), ALLOCATABLE                :: YAry      (:)                 ! The NumPts length array of y values for the interpolation.
     END TYPE CubSplineType
  
     TYPE, PUBLIC               :: RegCubSplineType                             ! This derived type is used to hold data for performing cubic splines wuth regularly-spaced data.
        INTEGER                                :: NumPts                        ! The number of points in the XAry and YAry arrays.
        REAL(ReKi), ALLOCATABLE                :: Coef      (:,:)               ! The NumPts-1 length array of cubic coefficients.  The second dimension must be "0:3".
        REAL(ReKi)                             :: DelX                          ! The distance between the equally spaced points in XAry.
        REAL(ReKi), ALLOCATABLE                :: XAry      (:)                 ! The NumPts length array of x values for the interpolation.
        REAL(ReKi), ALLOCATABLE                :: YAry      (:)                 ! The NumPts length array of y values for the interpolation.
     END TYPE RegCubSplineType
  
     TYPE, PUBLIC               :: RegGridType                                  ! This derived type is used to hold the contents of a regular grid of data.
        INTEGER                                :: NumDims                       ! The number of dimensions for this grid.
        REAL(ReKi), ALLOCATABLE                :: Mins      (:)                 ! The set of minimums for the grid in each NumDims dimensions.
        REAL(ReKi), ALLOCATABLE                :: Steps     (:)                 ! The set of step sizes for the grid in each NumDims dimensions.
        REAL(ReKi), ALLOCATABLE                :: Grid      (:)                 ! The NumDims dimensional grid.
     END TYPE RegGridType
  
  
  !=======================================================================
  