+ case ttCulp: {
+
+ vTurbPQR(eP) = wind_from_clockwise;
+ if (TurbGain == 0.0) return;
+
+ // keep the inputs within allowable limts for this model
+ if (TurbGain < 0.0) TurbGain = 0.0;
+ if (TurbGain > 1.0) TurbGain = 1.0;
+ if (TurbRate < 0.0) TurbRate = 0.0;
+ if (TurbRate > 30.0) TurbRate = 30.0;
+ if (Rhythmicity < 0.0) Rhythmicity = 0.0;
+ if (Rhythmicity > 1.0) Rhythmicity = 1.0;
+
+ // generate a sine wave corresponding to turbulence rate in hertz
+ double time = FDMExec->GetSimTime();
+ double sinewave = sin( time * TurbRate * 6.283185307 );
+
+ double random = 0.0;
+ if (target_time == 0.0) {
+ strength = random = 1 - 2.0*(double(rand())/double(RAND_MAX));
+ target_time = time + 0.71 + (random * 0.5);
+ }
+ if (time > target_time) {
+ spike = 1.0;
+ target_time = 0.0;
+ }
+
+ // max vertical wind speed in fps, corresponds to TurbGain = 1.0
+ double max_vs = 40;
+
+ vTurbulenceNED(1) = vTurbulenceNED(2) = vTurbulenceNED(3) = 0.0;
+ double delta = strength * max_vs * TurbGain * (1-Rhythmicity) * spike;
+
+ // Vertical component of turbulence.
+ vTurbulenceNED(3) = sinewave * max_vs * TurbGain * Rhythmicity;
+ vTurbulenceNED(3)+= delta;
+ if (HOverBMAC < 3.0)
+ vTurbulenceNED(3) *= HOverBMAC * 0.3333;
+
+ // Yaw component of turbulence.
+ vTurbulenceNED(1) = sin( delta * 3.0 );
+ vTurbulenceNED(2) = cos( delta * 3.0 );
+
+ // Roll component of turbulence. Clockwise vortex causes left roll.
+ vTurbPQR(eP) += delta * 0.04;
+
+ spike = spike * 0.9;
+ break;
+ }
+ case ttMilspec:
+ case ttTustin: {
+ double V = FDMExec->GetAuxiliary()->GetVt(); // true airspeed in ft/s
+
+ // an index of zero means turbulence is disabled
+ // airspeed occurs as divisor in the code below
+ if (probability_of_exceedence_index == 0 || V == 0) {
+ vTurbulenceNED(1) = vTurbulenceNED(2) = vTurbulenceNED(3) = 0.0;
+ vTurbPQR(1) = vTurbPQR(2) = vTurbPQR(3) = 0.0;
+ return;
+ }
+
+ // Turbulence model according to MIL-F-8785C (Flying Qualities of Piloted Aircraft)
+ double
+ h = FDMExec->GetPropagate()->GetDistanceAGL(),
+ b_w = wingspan,
+ L_u, L_w, sig_u, sig_w;
+
+ if (b_w == 0.) b_w = 30.;
+
+ // clip height functions at 10 ft
+ if (h <= 10.) h = 10;
+
+ // Scale lengths L and amplitudes sigma as function of height
+ if (h <= 1000) {
+ L_u = h/pow(0.177 + 0.000823*h, 1.2); // MIL-F-8785c, Fig. 10, p. 55
+ L_w = h;
+ sig_w = 0.1*windspeed_at_20ft;
+ sig_u = sig_w/pow(0.177 + 0.000823*h, 0.4); // MIL-F-8785c, Fig. 11, p. 56
+ } else if (h <= 2000) {
+ // linear interpolation between low altitude and high altitude models
+ L_u = L_w = 1000 + (h-1000.)/1000.*750.;
+ sig_u = sig_w = 0.1*windspeed_at_20ft
+ + (h-1000.)/1000.*(POE_Table->GetValue(probability_of_exceedence_index, h) - 0.1*windspeed_at_20ft);
+ } else {
+ L_u = L_w = 1750.; // MIL-F-8785c, Sec. 3.7.2.1, p. 48
+ sig_u = sig_w = POE_Table->GetValue(probability_of_exceedence_index, h);
+ }
+
+ // keep values from last timesteps
+ // TODO maybe use deque?
+ static double
+ xi_u_km1 = 0, nu_u_km1 = 0,
+ xi_v_km1 = 0, xi_v_km2 = 0, nu_v_km1 = 0, nu_v_km2 = 0,
+ xi_w_km1 = 0, xi_w_km2 = 0, nu_w_km1 = 0, nu_w_km2 = 0,
+ xi_p_km1 = 0, nu_p_km1 = 0,
+ xi_q_km1 = 0, xi_r_km1 = 0;
+
+
+ double
+ T_V = DeltaT, // for compatibility of nomenclature
+ sig_p = 1.9/sqrt(L_w*b_w)*sig_w, // Yeager1998, eq. (8)
+ sig_q = sqrt(M_PI/2/L_w/b_w), // eq. (14)
+ sig_r = sqrt(2*M_PI/3/L_w/b_w), // eq. (17)
+ L_p = sqrt(L_w*b_w)/2.6, // eq. (10)
+ tau_u = L_u/V, // eq. (6)
+ tau_w = L_w/V, // eq. (3)
+ tau_p = L_p/V, // eq. (9)
+ tau_q = 4*b_w/M_PI/V, // eq. (13)
+ tau_r =3*b_w/M_PI/V, // eq. (17)
+ nu_u = GaussianRandomNumber(),
+ nu_v = GaussianRandomNumber(),
+ nu_w = GaussianRandomNumber(),
+ nu_p = GaussianRandomNumber(),
+ xi_u=0, xi_v=0, xi_w=0, xi_p=0, xi_q=0, xi_r=0;
+
+ // values of turbulence NED velocities
+
+ if (turbType == ttTustin) {
+ // the following is the Tustin formulation of Yeager's report
+ double
+ omega_w = V/L_w, // hidden in nomenclature p. 3
+ omega_v = V/L_u, // this is defined nowhere
+ C_BL = 1/tau_u/tan(T_V/2/tau_u), // eq. (19)
+ C_BLp = 1/tau_p/tan(T_V/2/tau_p), // eq. (22)
+ C_BLq = 1/tau_q/tan(T_V/2/tau_q), // eq. (24)
+ C_BLr = 1/tau_r/tan(T_V/2/tau_r); // eq. (26)
+
+ // all values calculated so far are strictly positive, except for
+ // the random numbers nu_*. This means that in the code below, all
+ // divisors are strictly positive, too, and no floating point
+ // exception should occur.
+ xi_u = -(1 - C_BL*tau_u)/(1 + C_BL*tau_u)*xi_u_km1
+ + sig_u*sqrt(2*tau_u/T_V)/(1 + C_BL*tau_u)*(nu_u + nu_u_km1); // eq. (18)
+ xi_v = -2*(sqr(omega_v) - sqr(C_BL))/sqr(omega_v + C_BL)*xi_v_km1
+ - sqr(omega_v - C_BL)/sqr(omega_v + C_BL) * xi_v_km2
+ + sig_u*sqrt(3*omega_v/T_V)/sqr(omega_v + C_BL)*(
+ (C_BL + omega_v/sqrt(3.))*nu_v
+ + 2/sqrt(3.)*omega_v*nu_v_km1
+ + (omega_v/sqrt(3.) - C_BL)*nu_v_km2); // eq. (20) for v
+ xi_w = -2*(sqr(omega_w) - sqr(C_BL))/sqr(omega_w + C_BL)*xi_w_km1
+ - sqr(omega_w - C_BL)/sqr(omega_w + C_BL) * xi_w_km2
+ + sig_w*sqrt(3*omega_w/T_V)/sqr(omega_w + C_BL)*(
+ (C_BL + omega_w/sqrt(3.))*nu_w
+ + 2/sqrt(3.)*omega_w*nu_w_km1
+ + (omega_w/sqrt(3.) - C_BL)*nu_w_km2); // eq. (20) for w
+ xi_p = -(1 - C_BLp*tau_p)/(1 + C_BLp*tau_p)*xi_p_km1
+ + sig_p*sqrt(2*tau_p/T_V)/(1 + C_BLp*tau_p) * (nu_p + nu_p_km1); // eq. (21)
+ xi_q = -(1 - 4*b_w*C_BLq/M_PI/V)/(1 + 4*b_w*C_BLq/M_PI/V) * xi_q_km1
+ + C_BLq/V/(1 + 4*b_w*C_BLq/M_PI/V) * (xi_w - xi_w_km1); // eq. (23)
+ xi_r = - (1 - 3*b_w*C_BLr/M_PI/V)/(1 + 3*b_w*C_BLr/M_PI/V) * xi_r_km1
+ + C_BLr/V/(1 + 3*b_w*C_BLr/M_PI/V) * (xi_v - xi_v_km1); // eq. (25)
+
+ } else if (turbType == ttMilspec) {
+ // the following is the MIL-STD-1797A formulation
+ // as cited in Yeager's report
+ xi_u = (1 - T_V/tau_u) *xi_u_km1 + sig_u*sqrt(2*T_V/tau_u)*nu_u; // eq. (30)
+ xi_v = (1 - 2*T_V/tau_u)*xi_v_km1 + sig_u*sqrt(4*T_V/tau_u)*nu_v; // eq. (31)
+ xi_w = (1 - 2*T_V/tau_w)*xi_w_km1 + sig_w*sqrt(4*T_V/tau_w)*nu_w; // eq. (32)
+ xi_p = (1 - T_V/tau_p) *xi_p_km1 + sig_p*sqrt(2*T_V/tau_p)*nu_p; // eq. (33)
+ xi_q = (1 - T_V/tau_q) *xi_q_km1 + M_PI/4/b_w*(xi_w - xi_w_km1); // eq. (34)
+ xi_r = (1 - T_V/tau_r) *xi_r_km1 + M_PI/3/b_w*(xi_v - xi_v_km1); // eq. (35)
+ }
+
+ // rotate by wind azimuth and assign the velocities
+ double cospsi = cos(psiw), sinpsi = sin(psiw);
+ vTurbulenceNED(1) = cospsi*xi_u + sinpsi*xi_v;
+ vTurbulenceNED(2) = -sinpsi*xi_u + cospsi*xi_v;
+ vTurbulenceNED(3) = xi_w;
+
+ vTurbPQR(1) = cospsi*xi_p + sinpsi*xi_q;
+ vTurbPQR(2) = -sinpsi*xi_p + cospsi*xi_q;
+ vTurbPQR(3) = xi_r;
+
+ // vTurbPQR is in the body fixed frame, not NED
+ vTurbPQR = Tl2b*vTurbPQR;
+
+ // hand on the values for the next timestep
+ xi_u_km1 = xi_u; nu_u_km1 = nu_u;
+ xi_v_km2 = xi_v_km1; xi_v_km1 = xi_v; nu_v_km2 = nu_v_km1; nu_v_km1 = nu_v;
+ xi_w_km2 = xi_w_km1; xi_w_km1 = xi_w; nu_w_km2 = nu_w_km1; nu_w_km1 = nu_w;
+ xi_p_km1 = xi_p; nu_p_km1 = nu_p;
+ xi_q_km1 = xi_q;
+ xi_r_km1 = xi_r;
+
+ }