subroutine RTable_OnModeSolver
    
    use SimulationVariables !@
    
	IMPLICIT NONE
    
    REAL :: RT_OldSpeed, RT_OldRpmGauge
   
    
    
    RT_OldSpeed    = data%State%RTable%Speed
    RT_OldRpmGauge = data%State%RTable%RpmGaugeOutput
    

    
    if ( any(data%State%RTable%RotaryMode==(/1,4/)) ) then                           !if rotary connected to string
        !==> RPM
        data%State%RTable%Speed             = data%State%RTable%N_ref/data%State%RTable%Conv_Ratio
        data%State%RTable%Speed             = min(data%State%RTable%Speed,data%State%RTable%MaxRPM)        !Speed [RPM]
        data%State%RTable%RpmGaugeOutput    = data%State%RTable%Speed
        !==> Sound
        data%State%RTable%SoundRPM          = INT(data%State%RTable%Speed)
        !==> Torque
        data%State%RTable%Torque            = ( ((data%State%RTable%J_coef+data%State%RTable%String_JCoef)*((((pi*data%State%RTable%Speed)/30.d0)-((pi*RT_OldSpeed)/30.d0))/data%State%RTable%time_step))+(data%State%RTable%String_Torque) )*0.73756215d0   ![N.m]*0.73756215 = [ft.lbf]   ![kg.m^2]*[radians/s^2]+[N.m]=[N.m]
        data%State%RTable%Torque            = min(data%State%RTable%Torque,data%State%RTable%MaxTorque)
        data%State%RTable%TorqueGaugeOutput = data%State%RTable%Torque
    else if ( any(data%State%RTable%RotaryMode==(/2,5/)) ) then
        !==> RPM
        data%State%RTable%Speed             = 0.d0
        data%State%RTable%RpmGaugeOutput    = data%State%RTable%N_ref/data%State%RTable%Conv_Ratio
        data%State%RTable%RpmGaugeOutput    = min(data%State%RTable%RpmGaugeOutput,data%State%RTable%MaxRPM)    !inja bayad bashe ya na???
        !==> Sound
        data%State%RTable%SoundRPM          = INT(data%State%RTable%RpmGaugeOutput)
        !==> Torque
        data%State%RTable%Torque            = 0.d0
        data%State%RTable%TorqueGaugeOutput = ( ((data%State%RTable%J_coef+data%State%RTable%String_JCoef)*((((pi*data%State%RTable%RpmGaugeOutput)/30.d0)-((pi*RT_OldRpmGauge)/30.d0))/data%State%RTable%time_step))+(data%State%RTable%String_Torque) )*0.73756215d0   ![N.m]*0.73756215 = [ft.lbf]   ![kg.m^2]*[radians/s^2]+[N.m]=[N.m]
        data%State%RTable%TorqueGaugeOutput = min(data%State%RTable%TorqueGaugeOutput,data%State%RTable%MaxTorque)
    else if ( data%State%RTable%RotaryMode==3 ) then
        !==> RPM
        data%State%RTable%Speed             = 0.d0
        data%State%RTable%RpmGaugeOutput    = 0.d0
        !==> Sound
        data%State%RTable%SoundRPM          = INT(data%State%RTable%RpmGaugeOutput)
        !==> Torque
        data%State%RTable%Torque            = 0.d0
        data%State%RTable%TorqueGaugeOutput = 0.d0
    end if
    
    
    
END subroutine RTable_OnModeSolver