source: palm/trunk/SOURCE/wind_turbine_model_mod.f90 @ 2233

Last change on this file since 2233 was 2233, checked in by suehring, 7 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 108.9 KB
Line 
1!> @file wind_turbine_model_mod.f90
2!------------------------------------------------------------------------------!
3! This file is part of PALM.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2017 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: wind_turbine_model_mod.f90 2233 2017-05-30 18:08:54Z suehring $
27!
28! 2232 2017-05-30 17:47:52Z suehring
29! Adjustments to new topography concept
30!
31! 2152 2017-02-17 13:27:24Z lvollmer
32! Bugfix in subroutine wtm_read_blade_tables
33! Addition of a tip loss model
34!
35! 2015 2016-09-28 08:45:18Z lvollmer
36! Bugfix of pitch control
37!
38! 2000 2016-08-20 18:09:15Z knoop
39! Forced header and separation lines into 80 columns
40!
41! 1929 2016-06-09 16:25:25Z suehring
42! Bugfix: added preprocessor directives for parallel and serial mode
43!
44! 1914 2016-05-26 14:44:07Z witha
45! Initial revision
46!
47!
48! Description:
49! ------------
50!> This module calculates the effect of wind turbines on the flow fields. The
51!> initial version contains only the advanced actuator disk with rotation method
52!> (ADM-R).
53!> The wind turbines include the tower effect, can be yawed and tilted.
54!> The wind turbine model includes controllers for rotational speed, pitch and
55!> yaw.
56!> Currently some specifications of the NREL 5 MW reference turbine
57!> are hardcoded whereas most input data comes from separate files (currently
58!> external, planned to be included as namelist which will be read in
59!> automatically).
60!>
61!> @todo Revise code according to PALM Coding Standard
62!> @todo Implement ADM and ALM turbine models
63!> @todo Generate header information
64!> @todo Implement further parameter checks and error messages
65!> @todo Revise and add code documentation
66!> @todo Output turbine parameters as timeseries
67!> @todo Include additional output variables
68!> @todo Revise smearing the forces for turbines in yaw
69!> @todo Revise nacelle and tower parameterization
70!> @todo Allow different turbine types in one simulation
71!
72!------------------------------------------------------------------------------!
73 MODULE wind_turbine_model_mod
74
75    USE arrays_3d,                                                             &
76        ONLY:  dd2zu, tend, u, v, w, zu, zw
77
78    USE constants
79
80    USE control_parameters,                                                    &
81        ONLY:  dt_3d, dz, message_string, simulated_time
82
83    USE cpulog,                                                                &
84        ONLY:  cpu_log, log_point_s
85
86    USE grid_variables,                                                        &
87        ONLY:  ddx, dx, ddy, dy
88
89    USE indices,                                                               &
90        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nz,   &
91               nzb, nzt, wall_flags_0
92
93    USE kinds
94
95    USE pegrid
96
97
98    IMPLICIT NONE
99
100    PRIVATE
101
102    LOGICAL ::  wind_turbine=.FALSE.   !< switch for use of wind turbine model
103
104!
105!-- Variables specified in the namelist wind_turbine_par
106
107    INTEGER(iwp) ::  nairfoils = 8   !< number of airfoils of the used turbine model (for ADM-R and ALM)
108    INTEGER(iwp) ::  nturbines = 1   !< number of turbines
109
110    LOGICAL ::  pitch_control = .FALSE.   !< switch for use of pitch controller
111    LOGICAL ::  speed_control = .FALSE.   !< switch for use of speed controller
112    LOGICAL ::  yaw_control   = .FALSE.   !< switch for use of yaw controller
113    LOGICAL ::  tl_cor        = .FALSE.    !< switch for use of tip loss correct.
114
115    REAL(wp) ::  segment_length  = 1.0_wp          !< length of the segments, the rotor area is divided into
116                                                   !< (in tangential direction, as factor of MIN(dx,dy,dz))
117    REAL(wp) ::  segment_width   = 0.5_wp          !< width of the segments, the rotor area is divided into
118                                                   !< (in radial direction, as factor of MIN(dx,dy,dz))
119    REAL(wp) ::  time_turbine_on = 0.0_wp          !< time at which turbines are started
120    REAL(wp) ::  tilt            = 0.0_wp          !< vertical tilt of the rotor [degree] ( positive = backwards )
121
122    REAL(wp), DIMENSION(1:100) ::  dtow             = 0.0_wp  !< tower diameter [m]
123    REAL(wp), DIMENSION(1:100) ::  omega_rot        = 0.0_wp  !< inital or constant rotor speed [rad/s]
124    REAL(wp), DIMENSION(1:100) ::  phi_yaw          = 0.0_wp  !< yaw angle [degree] ( clockwise, 0 = facing west )
125    REAL(wp), DIMENSION(1:100) ::  pitch_add        = 0.0_wp  !< constant pitch angle
126    REAL(wp), DIMENSION(1:100) ::  rcx        = 9999999.9_wp  !< position of hub in x-direction
127    REAL(wp), DIMENSION(1:100) ::  rcy        = 9999999.9_wp  !< position of hub in y-direction
128    REAL(wp), DIMENSION(1:100) ::  rcz        = 9999999.9_wp  !< position of hub in z-direction
129    REAL(wp), DIMENSION(1:100) ::  rnac             = 0.0_wp  !< nacelle diameter [m]
130    REAL(wp), DIMENSION(1:100) ::  rr              = 63.0_wp  !< rotor radius [m]
131    REAL(wp), DIMENSION(1:100) ::  turb_cd_nacelle = 0.85_wp  !< drag coefficient for nacelle
132    REAL(wp), DIMENSION(1:100) ::  turb_cd_tower    = 1.2_wp  !< drag coefficient for tower
133
134!
135!-- Variables specified in the namelist for speed controller
136!-- Default values are from the NREL 5MW research turbine (Jonkman, 2008)
137
138    REAL(wp) ::  rated_power    = 5296610.0_wp    !< rated turbine power [W]
139    REAL(wp) ::  gear_ratio     = 97.0_wp         !< Gear ratio from rotor to generator
140    REAL(wp) ::  inertia_rot    = 34784179.0_wp   !< Inertia of the rotor [kg/m2]
141    REAL(wp) ::  inertia_gen    = 534.116_wp      !< Inertia of the generator [kg/m2]
142    REAL(wp) ::  gen_eff        = 0.944_wp        !< Electric efficiency of the generator
143    REAL(wp) ::  gear_eff       = 1.0_wp          !< Loss between rotor and generator
144    REAL(wp) ::  air_dens       = 1.225_wp        !< Air density to convert to W [kg/m3]
145    REAL(wp) ::  rated_genspeed = 121.6805_wp     !< Rated generator speed [rad/s]
146    REAL(wp) ::  max_torque_gen = 47402.91_wp     !< Maximum of the generator torque [Nm]
147    REAL(wp) ::  slope2         = 2.332287_wp     !< Slope constant for region 2
148    REAL(wp) ::  min_reg2       = 91.21091_wp     !< Lower generator speed boundary of region 2 [rad/s]
149    REAL(wp) ::  min_reg15      = 70.16224_wp     !< Lower generator speed boundary of region 1.5 [rad/s]
150    REAL(wp) ::  max_trq_rate   = 15000.0_wp      !< Max generator torque increase [Nm/s]
151    REAL(wp) ::  pitch_rate     = 8.0_wp          !< Max pitch rate [degree/s]
152
153
154!
155!-- Variables specified in the namelist for yaw control
156
157    REAL(wp) ::  yaw_speed = 0.005236_wp   !< speed of the yaw actuator [rad/s]
158    REAL(wp) ::  max_miss = 0.08726_wp     !< maximum tolerated yaw missalignment [rad]
159    REAL(wp) ::  min_miss = 0.008726_wp    !< minimum yaw missalignment for which the actuator stops [rad]
160
161!
162!-- Set flag for output files TURBINE_PARAMETERS
163    TYPE file_status
164       LOGICAL ::  opened, opened_before
165    END TYPE file_status
166   
167    TYPE(file_status), DIMENSION(500) :: openfile_turb_mod =                   &
168                                         file_status(.FALSE.,.FALSE.)
169
170!
171!-- Variables for initialization of the turbine model
172
173    INTEGER(iwp) ::  inot         !< turbine loop index (turbine id)
174    INTEGER(iwp) ::  nsegs_max    !< maximum number of segments (all turbines, required for allocation of arrays)
175    INTEGER(iwp) ::  nrings_max   !< maximum number of rings (all turbines, required for allocation of arrays)
176    INTEGER(iwp) ::  ring         !< ring loop index (ring number)
177    INTEGER(iwp) ::  rr_int       !<
178    INTEGER(iwp) ::  upper_end    !<
179
180    INTEGER(iwp), DIMENSION(1) ::  lct   !<
181
182    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  i_hub     !< index belonging to x-position of the turbine
183    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  i_smear   !< index defining the area for the smearing of the forces (x-direction)
184    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  j_hub     !< index belonging to y-position of the turbine
185    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  j_smear   !< index defining the area for the smearing of the forces (y-direction)
186    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  k_hub     !< index belonging to hub height
187    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  k_smear   !< index defining the area for the smearing of the forces (z-direction)
188    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nrings    !< number of rings per turbine
189    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nsegs_total !< total number of segments per turbine
190
191    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nsegs   !< number of segments per ring and turbine
192
193!
194!-  parameters for the smearing from the rotor to the cartesian grid   
195    REAL(wp) ::  pol_a            !< parameter for the polynomial smearing fct
196    REAL(wp) ::  pol_b            !< parameter for the polynomial smearing fct
197    REAL(wp) ::  delta_t_factor   !<
198    REAL(wp) ::  eps_factor       !< 
199    REAL(wp) ::  eps_min          !<
200    REAL(wp) ::  eps_min2         !<
201    REAL(wp) ::  sqrt_arg         !<
202
203!
204!-- Variables for the calculation of lift and drag coefficients
205    REAL(wp), DIMENSION(:), ALLOCATABLE  ::  ard     !<
206    REAL(wp), DIMENSION(:), ALLOCATABLE  ::  crd     !<
207    REAL(wp), DIMENSION(:), ALLOCATABLE  ::  delta_r !< radial segment length
208    REAL(wp), DIMENSION(:), ALLOCATABLE  ::  lrd     !<
209   
210    REAL(wp) ::  accu_cl_cd_tab = 0.1_wp  !< Accuracy of the interpolation of
211                                          !< the lift and drag coeff [deg]
212
213    REAL(wp), DIMENSION(:,:), ALLOCATABLE :: turb_cd_tab   !< table of the blade drag coefficient
214    REAL(wp), DIMENSION(:,:), ALLOCATABLE :: turb_cl_tab   !< table of the blade lift coefficient
215
216    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  nac_cd_surf  !< 3d field of the tower drag coefficient
217    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tow_cd_surf  !< 3d field of the nacelle drag coefficient
218
219!
220!-- Variables for the calculation of the forces
221     
222    REAL(wp) ::  cur_r                       !<
223    REAL(wp) ::  delta_t                     !<  tangential segment length
224    REAL(wp) ::  phi_rotor                   !<
225    REAL(wp) ::  pre_factor                  !< 
226    REAL(wp) ::  torque_seg                  !<
227    REAL(wp) ::  u_int_l                     !<
228    REAL(wp) ::  u_int_u                     !<
229    REAL(wp) ::  u_rot                       !<
230    REAL(wp) ::  v_int_l                     !<
231    REAL(wp) ::  v_int_u                     !<
232    REAL(wp) ::  w_int_l                     !<
233    REAL(wp) ::  w_int_u                     !<
234!
235!-  Tendencies from the nacelle and tower thrust
236    REAL(wp) ::  tend_nac_x = 0.0_wp  !<
237    REAL(wp) ::  tend_tow_x = 0.0_wp  !<
238    REAL(wp) ::  tend_nac_y = 0.0_wp  !<
239    REAL(wp) ::  tend_tow_y = 0.0_wp  !<
240
241    REAL(wp), DIMENSION(:), ALLOCATABLE ::  alpha_attack !<
242    REAL(wp), DIMENSION(:), ALLOCATABLE ::  chord        !<
243    REAL(wp), DIMENSION(:), ALLOCATABLE ::  omega_gen    !< curr. generator speed
244    REAL(wp), DIMENSION(:), ALLOCATABLE ::  phi_rel      !<
245    REAL(wp), DIMENSION(:), ALLOCATABLE ::  pitch_add_old!<
246    REAL(wp), DIMENSION(:), ALLOCATABLE ::  torque_total !<
247    REAL(wp), DIMENSION(:), ALLOCATABLE ::  thrust_rotor !<
248    REAL(wp), DIMENSION(:), ALLOCATABLE ::  turb_cl      !<
249    REAL(wp), DIMENSION(:), ALLOCATABLE ::  turb_cd      !<
250    REAL(wp), DIMENSION(:), ALLOCATABLE ::  vrel         !<
251    REAL(wp), DIMENSION(:), ALLOCATABLE ::  vtheta       !<
252
253    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rbx, rby, rbz     !< coordinates of the blade elements
254    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rotx, roty, rotz  !< normal vectors to the rotor coordinates
255
256!
257!-  Fields for the interpolation of velocities on the rotor grid
258    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_int       !<
259    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_int_1_l   !<
260    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_int       !<
261    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_int_1_l   !<
262    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_int       !<
263    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_int_1_l   !<
264   
265!
266!-  rotor tendencies on the segments
267    REAL(wp), DIMENSION(:), ALLOCATABLE :: thrust_seg   !<
268    REAL(wp), DIMENSION(:), ALLOCATABLE :: torque_seg_y !<
269    REAL(wp), DIMENSION(:), ALLOCATABLE :: torque_seg_z !<   
270
271!
272!-  rotor tendencies on the rings
273    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  thrust_ring       !<
274    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  torque_ring_y     !<
275    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  torque_ring_z     !<
276   
277!
278!-  rotor tendencies on rotor grids for all turbines
279    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  thrust      !<
280    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  torque_y    !<
281    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  torque_z    !<
282
283!
284!-  rotor tendencies on coordinate grid
285    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  rot_tend_x  !<
286    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  rot_tend_y  !<
287    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  rot_tend_z  !<
288!   
289!-  variables for the rotation of the rotor coordinates       
290    REAL(wp), DIMENSION(1:100,1:3,1:3) ::  rot_coord_trans  !< matrix for rotation of rotor coordinates
291   
292    REAL(wp), DIMENSION(1:3) ::  rot_eigen_rad   !<
293    REAL(wp), DIMENSION(1:3) ::  rot_eigen_azi   !<
294    REAL(wp), DIMENSION(1:3) ::  rot_eigen_nor   !<
295    REAL(wp), DIMENSION(1:3) ::  re              !<
296    REAL(wp), DIMENSION(1:3) ::  rea             !<
297    REAL(wp), DIMENSION(1:3) ::  ren             !<
298    REAL(wp), DIMENSION(1:3) ::  rote            !<
299    REAL(wp), DIMENSION(1:3) ::  rota            !<
300    REAL(wp), DIMENSION(1:3) ::  rotn            !<
301
302!
303!-- Fixed variables for the speed controller
304
305    LOGICAL  ::  start_up = .TRUE.   !<
306   
307    REAL(wp) ::  Fcorner             !< corner freq for the controller low pass filter
308    REAL(wp) ::  min_reg25           !< min region 2.5
309    REAL(wp) ::  om_rate             !< rotor speed change
310    REAL(wp) ::  slope15             !< slope in region 1.5
311    REAL(wp) ::  slope25             !< slope in region 2.5
312    REAL(wp) ::  trq_rate            !< torque change
313    REAL(wp) ::  vs_sysp             !<
314    REAL(wp) ::  lp_coeff            !< coeff for the controller low pass filter
315   
316    REAL(wp), DIMENSION(:), ALLOCATABLE :: omega_gen_old   !< last gen. speed
317    REAL(wp), DIMENSION(:), ALLOCATABLE :: omega_gen_f     !< filtered gen. sp
318    REAL(wp), DIMENSION(:), ALLOCATABLE :: omega_gen_f_old !< last filt. gen. sp
319    REAL(wp), DIMENSION(:), ALLOCATABLE :: torque_gen      !< generator torque
320    REAL(wp), DIMENSION(:), ALLOCATABLE :: torque_gen_old  !< last gen. torque
321
322    REAL(wp), DIMENSION(100) :: omega_rot_l = 0.0_wp !< local rot speed [rad/s]
323!
324!-- Fixed variables for the yaw controller
325
326    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  yawdir           !< direction to yaw
327    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  phi_yaw_l        !< local (cpu) yaw angle
328    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  wd30_l           !< local (cpu) long running avg of the wd
329    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  wd2_l            !< local (cpu) short running avg of the wd
330    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  wdir             !< wind direction at hub
331    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  u_inflow         !< wind speed at hub
332    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  wdir_l           !<
333    REAL(wp), DIMENSION(:)  , ALLOCATABLE ::  u_inflow_l       !<
334    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  wd30             !<
335    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  wd2              !<
336    LOGICAL,  DIMENSION(1:100)            ::  doyaw = .FALSE.  !<
337    INTEGER(iwp)                          ::  WDLON            !<
338    INTEGER(iwp)                          ::  WDSHO            !<
339
340
341    SAVE
342
343
344    INTERFACE wtm_parin
345       MODULE PROCEDURE wtm_parin
346    END INTERFACE wtm_parin
347   
348    INTERFACE wtm_check_parameters
349       MODULE PROCEDURE wtm_check_parameters
350    END INTERFACE wtm_check_parameters
351       
352    INTERFACE wtm_init_arrays
353       MODULE PROCEDURE wtm_init_arrays
354    END INTERFACE wtm_init_arrays
355
356    INTERFACE wtm_init
357       MODULE PROCEDURE wtm_init
358    END INTERFACE wtm_init
359   
360    INTERFACE wtm_read_blade_tables
361       MODULE PROCEDURE wtm_read_blade_tables
362    END INTERFACE wtm_read_blade_tables
363           
364    INTERFACE wtm_forces
365       MODULE PROCEDURE wtm_forces
366       MODULE PROCEDURE wtm_yawcontrol
367    END INTERFACE wtm_forces
368   
369    INTERFACE wtm_rotate_rotor
370       MODULE PROCEDURE wtm_rotate_rotor
371    END INTERFACE wtm_rotate_rotor
372   
373    INTERFACE wtm_speed_control
374       MODULE PROCEDURE wtm_init_speed_control
375       MODULE PROCEDURE wtm_speed_control
376    END INTERFACE wtm_speed_control
377
378    INTERFACE wtm_tendencies
379       MODULE PROCEDURE wtm_tendencies
380       MODULE PROCEDURE wtm_tendencies_ij
381    END INTERFACE wtm_tendencies
382   
383   
384    PUBLIC wtm_check_parameters, wtm_forces, wtm_init, wtm_init_arrays,        &
385           wtm_parin, wtm_tendencies, wtm_tendencies_ij, wind_turbine 
386
387 CONTAINS
388
389
390!------------------------------------------------------------------------------!
391! Description:
392! ------------
393!> Parin for &wind_turbine_par for wind turbine model
394!------------------------------------------------------------------------------!
395    SUBROUTINE wtm_parin
396
397
398       IMPLICIT NONE
399       
400       INTEGER(iwp) ::  ierrn       !<
401
402       CHARACTER (LEN=80) ::  line  !< dummy string that contains the current line of the parameter file
403
404       NAMELIST /wind_turbine_par/   air_dens, dtow, gear_eff, gear_ratio,     &
405                                  gen_eff, inertia_gen, inertia_rot, max_miss, &
406                                  max_torque_gen, max_trq_rate, min_miss,      &
407                                  min_reg15, min_reg2, nairfoils, nturbines,   &
408                                  omega_rot, phi_yaw, pitch_add, pitch_control,&
409                                  rated_genspeed, rated_power, rcx, rcy, rcz,  &
410                                  rnac, rr, segment_length, segment_width,     &
411                                  slope2, speed_control, tilt, time_turbine_on,&
412                                  turb_cd_nacelle, turb_cd_tower,              &
413                                  yaw_control, yaw_speed, tl_cor
414
415!
416!--    Try to find wind turbine model package
417       REWIND ( 11 )
418       line = ' '
419       DO  WHILE ( INDEX( line, '&wind_turbine_par' ) == 0 )
420          READ ( 11, '(A)', END=10 )  line
421       ENDDO
422       BACKSPACE ( 11 )
423
424!
425!--    Read user-defined namelist
426       READ ( 11, wind_turbine_par, IOSTAT=ierrn )
427       
428       IF ( ierrn < 0 )  THEN
429          message_string = 'errors in \$wind_turbine_par'
430          CALL message( 'wtm_parin', 'PA0???', 1, 2, 0, 6, 0 )
431       ENDIF
432       
433!
434!--    Set flag that indicates that the wind turbine model is switched on
435       wind_turbine = .TRUE.
436
437 10    CONTINUE   ! TBD Change from continue, mit ierrn machen
438
439
440    END SUBROUTINE wtm_parin
441
442    SUBROUTINE wtm_check_parameters
443
444   
445       IMPLICIT NONE
446   
447       IF ( ( .NOT.speed_control ) .AND. pitch_control )  THEN
448          message_string = 'pitch_control = .TRUE. requires '//                &
449                           'speed_control = .TRUE.'
450          CALL message( 'wtm_check_parameters', 'PA0???', 1, 2, 0, 6, 0 )
451       ENDIF
452       
453       IF ( ANY( omega_rot(1:nturbines) <= 0.0 ) )  THEN
454          message_string = 'omega_rot <= 0.0, Please set omega_rot to '     // &
455                           'a value larger than zero'
456          CALL message( 'wtm_check_parameters', 'PA0???', 1, 2, 0, 6, 0 )
457       ENDIF
458       
459       
460       IF ( ANY( rcx(1:nturbines) == 9999999.9_wp ) .OR.                       &
461            ANY( rcy(1:nturbines) == 9999999.9_wp ) .OR.                       &
462            ANY( rcz(1:nturbines) == 9999999.9_wp ) )  THEN
463         
464          message_string = 'rcx, rcy, rcz '                                 // &
465                           'have to be given for each turbine.'         
466          CALL message( 'wtm_check_parameters', 'PA0???', 1, 2, 0, 6, 0 )         
467         
468       ENDIF
469
470 
471    END SUBROUTINE wtm_check_parameters 
472   
473                                       
474!------------------------------------------------------------------------------!
475! Description:
476! ------------
477!> Allocate wind turbine model arrays
478!------------------------------------------------------------------------------!
479    SUBROUTINE wtm_init_arrays
480
481
482       IMPLICIT NONE
483
484       REAL(wp) ::  delta_r_factor   !<
485       REAL(wp) ::  delta_r_init     !<
486
487!
488!--    To be able to allocate arrays with dimension of rotor rings and segments,
489!--    the maximum possible numbers of rings and segments have to be calculated:
490
491       ALLOCATE( nrings(1:nturbines) )
492       ALLOCATE( delta_r(1:nturbines) )
493
494       nrings(:)  = 0
495       delta_r(:) = 0.0_wp
496
497!
498!--    Thickness (radial) of each ring and length (tangential) of each segment:
499       delta_r_factor = segment_width
500       delta_t_factor = segment_length
501       delta_r_init   = delta_r_factor * MIN( dx, dy, dz)
502       delta_t        = delta_t_factor * MIN( dx, dy, dz)
503
504       DO inot = 1, nturbines
505!
506!--       Determine number of rings:
507          nrings(inot) = NINT( rr(inot) / delta_r_init )
508
509          delta_r(inot) = rr(inot) / nrings(inot)
510
511       ENDDO
512
513       nrings_max = MAXVAL(nrings)
514
515       ALLOCATE( nsegs(1:nrings_max,1:nturbines) )
516       ALLOCATE( nsegs_total(1:nturbines) )
517
518       nsegs(:,:)     = 0
519       nsegs_total(:) = 0
520
521
522       DO inot = 1, nturbines
523          DO ring = 1, nrings(inot)
524!
525!--          Determine number of segments for each ring:
526             nsegs(ring,inot) = MAX( 8, CEILING( delta_r_factor * pi *         &
527                                                 ( 2.0_wp * ring - 1.0_wp ) /  &
528                                                 delta_t_factor ) )
529          ENDDO
530!
531!--       Total sum of all rotor segments:
532          nsegs_total(inot) = SUM( nsegs(:,inot) )
533
534       ENDDO
535
536!
537!--    Maximum number of segments per ring:
538       nsegs_max = MAXVAL(nsegs)
539
540!!
541!!--    TODO: Folgendes im Header ausgeben!
542!       IF ( myid == 0 )  THEN
543!          PRINT*, 'nrings(1) = ', nrings(1)
544!          PRINT*, '--------------------------------------------------'
545!          PRINT*, 'nsegs(:,1) = ', nsegs(:,1)
546!          PRINT*, '--------------------------------------------------'
547!          PRINT*, 'nrings_max = ', nrings_max
548!          PRINT*, 'nsegs_max = ', nsegs_max
549!          PRINT*, 'nsegs_total(1) = ', nsegs_total(1)
550!       ENDIF
551
552
553!
554!--    Allocate 1D arrays (dimension = number of turbines)
555       ALLOCATE( i_hub(1:nturbines) )
556       ALLOCATE( i_smear(1:nturbines) )
557       ALLOCATE( j_hub(1:nturbines) )
558       ALLOCATE( j_smear(1:nturbines) )
559       ALLOCATE( k_hub(1:nturbines) )
560       ALLOCATE( k_smear(1:nturbines) )
561       ALLOCATE( torque_total(1:nturbines) )
562       ALLOCATE( thrust_rotor(1:nturbines) )
563
564!
565!--    Allocation of the 1D arrays for speed pitch_control
566       ALLOCATE( omega_gen(1:nturbines) )
567       ALLOCATE( omega_gen_old(1:nturbines) )
568       ALLOCATE( omega_gen_f(1:nturbines) )
569       ALLOCATE( omega_gen_f_old(1:nturbines) )
570       ALLOCATE( pitch_add_old(1:nturbines) )
571       ALLOCATE( torque_gen(1:nturbines) )
572       ALLOCATE( torque_gen_old(1:nturbines) )
573
574!
575!--    Allocation of the 1D arrays for yaw control
576       ALLOCATE( yawdir(1:nturbines) )
577       ALLOCATE( u_inflow(1:nturbines) )
578       ALLOCATE( wdir(1:nturbines) )
579       ALLOCATE( u_inflow_l(1:nturbines) )
580       ALLOCATE( wdir_l(1:nturbines) )
581       ALLOCATE( phi_yaw_l(1:nturbines) )
582       
583!
584!--    Allocate 1D arrays (dimension = number of rotor segments)
585       ALLOCATE( alpha_attack(1:nsegs_max) )
586       ALLOCATE( chord(1:nsegs_max) )
587       ALLOCATE( phi_rel(1:nsegs_max) )
588       ALLOCATE( thrust_seg(1:nsegs_max) )
589       ALLOCATE( torque_seg_y(1:nsegs_max) )
590       ALLOCATE( torque_seg_z(1:nsegs_max) )
591       ALLOCATE( turb_cd(1:nsegs_max) )
592       ALLOCATE( turb_cl(1:nsegs_max) )
593       ALLOCATE( vrel(1:nsegs_max) )
594       ALLOCATE( vtheta(1:nsegs_max) )
595
596!
597!--    Allocate 2D arrays (dimension = number of rotor rings and segments)
598       ALLOCATE( rbx(1:nrings_max,1:nsegs_max) )
599       ALLOCATE( rby(1:nrings_max,1:nsegs_max) )
600       ALLOCATE( rbz(1:nrings_max,1:nsegs_max) )
601       ALLOCATE( thrust_ring(1:nrings_max,1:nsegs_max) )
602       ALLOCATE( torque_ring_y(1:nrings_max,1:nsegs_max) )
603       ALLOCATE( torque_ring_z(1:nrings_max,1:nsegs_max) )
604
605!
606!--    Allocate additional 2D arrays
607       ALLOCATE( rotx(1:nturbines,1:3) )
608       ALLOCATE( roty(1:nturbines,1:3) )
609       ALLOCATE( rotz(1:nturbines,1:3) )
610
611!
612!--    Allocate 3D arrays (dimension = number of grid points)
613       ALLOCATE( nac_cd_surf(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
614       ALLOCATE( rot_tend_x(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
615       ALLOCATE( rot_tend_y(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
616       ALLOCATE( rot_tend_z(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
617       ALLOCATE( thrust(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
618       ALLOCATE( torque_y(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
619       ALLOCATE( torque_z(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
620       ALLOCATE( tow_cd_surf(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
621
622!
623!--    Allocate additional 3D arrays
624       ALLOCATE( u_int(1:nturbines,1:nrings_max,1:nsegs_max) )
625       ALLOCATE( u_int_1_l(1:nturbines,1:nrings_max,1:nsegs_max) )
626       ALLOCATE( v_int(1:nturbines,1:nrings_max,1:nsegs_max) )
627       ALLOCATE( v_int_1_l(1:nturbines,1:nrings_max,1:nsegs_max) )
628       ALLOCATE( w_int(1:nturbines,1:nrings_max,1:nsegs_max) )
629       ALLOCATE( w_int_1_l(1:nturbines,1:nrings_max,1:nsegs_max) )
630
631!
632!--    All of the arrays are initialized with a value of zero:
633       i_hub(:)                 = 0
634       i_smear(:)               = 0
635       j_hub(:)                 = 0
636       j_smear(:)               = 0
637       k_hub(:)                 = 0
638       k_smear(:)               = 0
639       
640       torque_total(:)          = 0.0_wp
641       thrust_rotor(:)          = 0.0_wp
642
643       omega_gen(:)             = 0.0_wp
644       omega_gen_old(:)         = 0.0_wp
645       omega_gen_f(:)           = 0.0_wp
646       omega_gen_f_old(:)       = 0.0_wp
647       pitch_add_old(:)         = 0.0_wp
648       torque_gen(:)            = 0.0_wp
649       torque_gen_old(:)        = 0.0_wp
650       
651       yawdir(:)                = 0.0_wp
652       wdir(:)                  = 0.0_wp
653       u_inflow(:)              = 0.0_wp
654
655!
656!--    Allocate 1D arrays (dimension = number of rotor segments)
657       alpha_attack(:)          = 0.0_wp
658       chord(:)                 = 0.0_wp
659       phi_rel(:)               = 0.0_wp
660       thrust_seg(:)            = 0.0_wp
661       torque_seg_y(:)          = 0.0_wp
662       torque_seg_z(:)          = 0.0_wp
663       turb_cd(:)               = 0.0_wp
664       turb_cl(:)               = 0.0_wp
665       vrel(:)                  = 0.0_wp
666       vtheta(:)                = 0.0_wp
667
668       rbx(:,:)                 = 0.0_wp
669       rby(:,:)                 = 0.0_wp
670       rbz(:,:)                 = 0.0_wp
671       thrust_ring(:,:)         = 0.0_wp
672       torque_ring_y(:,:)       = 0.0_wp
673       torque_ring_z(:,:)       = 0.0_wp
674
675       rotx(:,:)                = 0.0_wp
676       roty(:,:)                = 0.0_wp
677       rotz(:,:)                = 0.0_wp
678       turb_cl_tab(:,:)         = 0.0_wp
679       turb_cd_tab(:,:)         = 0.0_wp
680
681       nac_cd_surf(:,:,:)       = 0.0_wp
682       rot_tend_x(:,:,:)        = 0.0_wp
683       rot_tend_y(:,:,:)        = 0.0_wp
684       rot_tend_z(:,:,:)        = 0.0_wp
685       thrust(:,:,:)            = 0.0_wp
686       torque_y(:,:,:)          = 0.0_wp
687       torque_z(:,:,:)          = 0.0_wp
688       tow_cd_surf(:,:,:)       = 0.0_wp
689
690       u_int(:,:,:)             = 0.0_wp
691       u_int_1_l(:,:,:)         = 0.0_wp
692       v_int(:,:,:)             = 0.0_wp
693       v_int_1_l(:,:,:)         = 0.0_wp
694       w_int(:,:,:)             = 0.0_wp
695       w_int_1_l(:,:,:)         = 0.0_wp
696
697
698    END SUBROUTINE wtm_init_arrays
699
700
701!------------------------------------------------------------------------------!
702! Description:
703! ------------
704!> Initialization of the wind turbine model
705!------------------------------------------------------------------------------!
706    SUBROUTINE wtm_init
707
708   
709       IMPLICIT NONE
710
711       INTEGER(iwp) ::  i  !< running index
712       INTEGER(iwp) ::  j  !< running index
713       INTEGER(iwp) ::  k  !< running index
714       
715!
716!--    Help variables for the smearing function       
717       REAL(wp) ::  eps_kernel       !<       
718       
719!
720!--    Help variables for calculation of the tower drag       
721       INTEGER(iwp) ::  tower_n      !<
722       INTEGER(iwp) ::  tower_s      !<
723!
724!--    Help variables for the calulaction of the nacelle drag
725       INTEGER(iwp) ::  i_ip         !<
726       INTEGER(iwp) ::  i_ipg        !<
727       
728       REAL(wp) ::  yvalue               
729       REAL(wp) ::  dy_int           !<
730       REAL(wp) ::  dz_int           !<
731       
732       REAL(wp), DIMENSION(:,:), ALLOCATABLE :: circle_points  !<
733             
734       INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: index_nacb       !<
735       INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: index_nacl       !<
736       INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: index_nacr       !<
737       INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: index_nact       !<
738       
739       ALLOCATE( index_nacb(1:nturbines) )
740       ALLOCATE( index_nacl(1:nturbines) )
741       ALLOCATE( index_nacr(1:nturbines) )
742       ALLOCATE( index_nact(1:nturbines) )
743
744
745       IF ( speed_control)  THEN
746       
747          CALL wtm_speed_control
748
749       ENDIF
750
751!
752!------------------------------------------------------------------------------!
753!--    Calculation of parameters for the regularization kernel
754!--    (smearing of the forces)
755!------------------------------------------------------------------------------!
756!
757!--    In the following, some of the required parameters for the smearing will
758!--    be calculated:
759
760!--    The kernel is set equal to twice the grid spacing which has turned out to
761!--    be a reasonable value (see e.g. Troldborg et al. (2013), Wind Energy,
762!--    DOI: 10.1002/we.1608):
763       eps_kernel = 2.0_wp * dx
764!
765!--    The zero point (eps_min) of the polynomial function must be the following
766!--    if the integral of the polynomial function (for values < eps_min) shall
767!--    be equal to the integral of the Gaussian function used before:
768       eps_min = ( 105.0_wp / 32.0_wp )**( 1.0_wp / 3.0_wp ) *                 &
769                 pi**( 1.0_wp / 6.0_wp ) * eps_kernel
770!
771!--    Square of eps_min:
772       eps_min2 = eps_min**2
773!
774!--    Parameters in the polynomial function:
775       pol_a = 1.0_wp / eps_min**4
776       pol_b = 2.0_wp / eps_min**2
777!
778!--    Normalization factor which is the inverse of the integral of the smearing
779!--    function:
780       eps_factor = 105.0_wp / ( 32.0_wp * pi * eps_min**3 )
781       
782!--    Change tilt angle to rad:
783       tilt = tilt * pi / 180.0_wp
784     
785!
786!--    Change yaw angle to rad:
787       phi_yaw(:) = phi_yaw(:) * pi / 180.0_wp
788
789
790       DO inot = 1, nturbines
791!
792!--       Rotate the rotor coordinates in case yaw and tilt are defined
793          CALL wtm_rotate_rotor( inot )
794         
795!
796!--       Determine the indices of the hub height
797          i_hub(inot) = INT(   rcx(inot)                 / dx )
798          j_hub(inot) = INT( ( rcy(inot) + 0.5_wp * dy ) / dy )
799          k_hub(inot) = INT( ( rcz(inot) + 0.5_wp * dz ) / dz )
800
801!
802!--       Determining the area to which the smearing of the forces is applied.
803!--       As smearing now is effectively applied only for distances smaller than
804!--       eps_min, the smearing area can be further limited and regarded as a
805!--       function of eps_min:
806          i_smear(inot) = CEILING( ( rr(inot) + eps_min ) / dx )
807          j_smear(inot) = CEILING( ( rr(inot) + eps_min ) / dy )
808          k_smear(inot) = CEILING( ( rr(inot) + eps_min ) / dz )
809       
810       ENDDO
811
812!
813!------------------------------------------------------------------------------!
814!--    Determine the area within each grid cell that overlaps with the area
815!--    of the nacelle and the tower (needed for calculation of the forces)
816!------------------------------------------------------------------------------!
817!
818!--    Note: so far this is only a 2D version, in that the mean flow is
819!--    perpendicular to the rotor area.
820
821!
822!--    Allocation of the array containing information on the intersection points
823!--    between rotor disk and the numerical grid:
824       upper_end = ( ny + 1 ) * 10000 
825
826       ALLOCATE( circle_points(1:2,1:upper_end) )
827       
828       circle_points(:,:) = 0.0_wp
829
830       
831       DO inot = 1, nturbines                     ! loop over number of turbines
832!
833!--       Determine the grid index (u-grid) that corresponds to the location of
834!--       the rotor center (reduces the amount of calculations in the case that
835!--       the mean flow is perpendicular to the rotor area):
836          i = i_hub(inot)
837
838!
839!--       Determine the left and the right edge of the nacelle (corresponding
840!--       grid point indices):
841          index_nacl(inot) = INT( ( rcy(inot) - rnac(inot) + 0.5_wp * dy ) / dy )
842          index_nacr(inot) = INT( ( rcy(inot) + rnac(inot) + 0.5_wp * dy ) / dy )
843!
844!--       Determine the bottom and the top edge of the nacelle (corresponding
845!--       grid point indices).The grid point index has to be increased by 1, as
846!--       the first level for the u-component (index 0) is situated below the
847!--       surface. All points between z=0 and z=dz/s would already be contained
848!--       in grid box 1.
849          index_nacb(inot) = INT( ( rcz(inot) - rnac(inot) ) / dz ) + 1
850          index_nact(inot) = INT( ( rcz(inot) + rnac(inot) ) / dz ) + 1
851
852!
853!--       Determine the indices of the grid boxes containing the left and
854!--       the right boundaries of the tower:
855          tower_n = ( rcy(inot) + 0.5_wp * dtow(inot) - 0.5_wp * dy ) / dy
856          tower_s = ( rcy(inot) - 0.5_wp * dtow(inot) - 0.5_wp * dy ) / dy
857
858!
859!--       Determine the fraction of the grid box area overlapping with the tower
860!--       area and multiply it with the drag of the tower:
861          IF ( ( nxlg <= i )  .AND.  ( nxrg >= i ) )  THEN
862
863             DO  j = nys, nyn
864!
865!--             Loop from south to north boundary of tower
866                IF ( ( j >= tower_s )  .AND.  ( j <= tower_n ) )  THEN
867
868                   DO  k = nzb, nzt
869
870                      IF ( k == k_hub(inot) )  THEN
871                         IF ( tower_n - tower_s >= 1 )  THEN
872!
873!--                      leftmost and rightmost grid box:
874                            IF ( j == tower_s )  THEN
875                               tow_cd_surf(k,j,i) = ( rcz(inot) -              &
876                                    ( k_hub(inot) * dz - 0.5_wp * dz ) )  *    & ! extension in z-direction
877                                  ( ( tower_s + 1.0_wp + 0.5_wp ) * dy    -    &
878                                    ( rcy(inot) - 0.5_wp * dtow(inot) ) ) *    & ! extension in y-direction
879                                  turb_cd_tower(inot)
880                            ELSEIF ( j == tower_n )  THEN
881                               tow_cd_surf(k,j,i) = ( rcz(inot)            -   &
882                                    ( k_hub(inot) * dz - 0.5_wp * dz ) )  *    & ! extension in z-direction
883                                  ( ( rcy(inot) + 0.5_wp * dtow(inot) )   -    &
884                                    ( tower_n + 0.5_wp ) * dy )           *    & ! extension in y-direction
885                                  turb_cd_tower(inot)
886!
887!--                         grid boxes inbetween
888!--                         (where tow_cd_surf = grid box area):
889                            ELSE
890                               tow_cd_surf(k,j,i) = ( rcz(inot) -              &
891                                    ( k_hub(inot) * dz - 0.5_wp * dz ) )  *    &
892                                    dy * turb_cd_tower(inot)
893                            ENDIF
894!
895!--                      tower lies completely within one grid box:
896                         ELSE
897                            tow_cd_surf(k,j,i) = ( rcz(inot)                 - &
898                                       ( k_hub(inot) * dz - 0.5_wp * dz ) ) *  &
899                                       dtow(inot) * turb_cd_tower(inot)
900                         ENDIF
901!
902!--                  In case that k is smaller than k_hub the following actions
903!--                  are carried out:
904                      ELSEIF ( k < k_hub(inot) )  THEN
905                     
906                         IF ( ( tower_n - tower_s ) >= 1 )  THEN
907!
908!--                         leftmost and rightmost grid box:
909                            IF ( j == tower_s )  THEN                         
910                               tow_cd_surf(k,j,i) = dz * (                     &
911                                      ( tower_s + 1 + 0.5_wp ) * dy         -  &
912                                      ( rcy(inot) - 0.5_wp * dtow(inot) )      &
913                                                        ) * turb_cd_tower(inot)
914                            ELSEIF ( j == tower_n )  THEN
915                               tow_cd_surf(k,j,i) = dz * (                     &
916                                      ( rcy(inot) + 0.5_wp * dtow(inot) )   -  &
917                                      ( tower_n + 0.5_wp ) * dy                &
918                                                         ) * turb_cd_tower(inot)
919!
920!--                         grid boxes inbetween
921!--                         (where tow_cd_surf = grid box area):
922                            ELSE
923                               tow_cd_surf(k,j,i) = dz * dy * turb_cd_tower(inot)
924                            ENDIF
925!
926!--                         tower lies completely within one grid box:
927                         ELSE
928                            tow_cd_surf(k,j,i) = dz * dtow(inot) *             &
929                                                turb_cd_tower(inot)
930                         ENDIF ! end if larger than grid box
931
932                      ENDIF    ! end if k == k_hub
933
934                   ENDDO       ! end loop over k
935
936                ENDIF          ! end if inside north and south boundary of tower
937
938             ENDDO             ! end loop over j
939
940          ENDIF                ! end if hub inside domain + ghostpoints
941       
942         
943          CALL exchange_horiz( tow_cd_surf, nbgp )
944
945!
946!--       Calculation of the nacelle area
947!--       CAUTION: Currently disabled due to segmentation faults on the FLOW HPC
948!--                cluster (Oldenburg)
949!!
950!!--       Tabulate the points on the circle that are required in the following for
951!!--       the calculation of the Riemann integral (node points; they are called
952!!--       circle_points in the following):
953!
954!          dy_int = dy / 10000.0_wp
955!
956!          DO  i_ip = 1, upper_end
957!             yvalue   = dy_int * ( i_ip - 0.5_wp ) + 0.5_wp * dy           !<--- segmentation fault
958!             sqrt_arg = rnac(inot)**2 - ( yvalue - rcy(inot) )**2          !<--- segmentation fault
959!             IF ( sqrt_arg >= 0.0_wp )  THEN
960!!
961!!--             bottom intersection point
962!                circle_points(1,i_ip) = rcz(inot) - SQRT( sqrt_arg )
963!!
964!!--             top intersection point
965!                circle_points(2,i_ip) = rcz(inot) + SQRT( sqrt_arg )       !<--- segmentation fault
966!             ELSE
967!                circle_points(:,i_ip) = -111111                            !<--- segmentation fault
968!             ENDIF
969!          ENDDO
970!
971!
972!          DO  j = nys, nyn
973!!
974!!--          In case that the grid box is located completely outside the nacelle
975!!--          (y) it can automatically be stated that there is no overlap between
976!!--          the grid box and the nacelle and consequently we can set
977!!--          nac_cd_surf(:,j,i) = 0.0:
978!             IF ( ( j >= index_nacl(inot) )  .AND.  ( j <= index_nacr(inot) ) )  THEN
979!                DO  k = nzb+1, nzt
980!!
981!!--                In case that the grid box is located completely outside the
982!!--                nacelle (z) it can automatically be stated that there is no
983!!--                overlap between the grid box and the nacelle and consequently
984!!--                we can set nac_cd_surf(k,j,i) = 0.0:
985!                   IF ( ( k >= index_nacb(inot) )  .OR.                           &
986!                        ( k <= index_nact(inot) ) )  THEN
987!!
988!!--                   For all other cases Riemann integrals are calculated.
989!!--                   Here, the points on the circle that have been determined
990!!--                   above are used in order to calculate the overlap between the
991!!--                   gridbox and the nacelle area (area approached by 10000
992!!--                   rectangulars dz_int * dy_int):
993!                      DO  i_ipg = 1, 10000
994!                         dz_int = dz
995!                         i_ip = j * 10000 + i_ipg
996!!
997!!--                      Determine the vertical extension dz_int of the circle
998!!--                      within the current grid box:
999!                         IF ( ( circle_points(2,i_ip) < zw(k) ) .AND.          &  !<--- segmentation fault
1000!                              ( circle_points(2,i_ip) >= zw(k-1) ) ) THEN
1001!                            dz_int = dz_int -                                  &  !<--- segmentation fault
1002!                                     ( zw(k) - circle_points(2,i_ip) )
1003!                         ENDIF
1004!                         IF ( ( circle_points(1,i_ip) <= zw(k) ) .AND.         &  !<--- segmentation fault
1005!                              ( circle_points(1,i_ip) > zw(k-1) ) ) THEN
1006!                            dz_int = dz_int -                                  &
1007!                                     ( circle_points(1,i_ip) - zw(k-1) )
1008!                         ENDIF
1009!                         IF ( zw(k-1) > circle_points(2,i_ip) ) THEN
1010!                            dz_int = 0.0_wp
1011!                         ENDIF
1012!                         IF ( zw(k) < circle_points(1,i_ip) ) THEN
1013!                            dz_int = 0.0_wp                     
1014!                         ENDIF
1015!                         IF ( ( nxlg <= i ) .AND. ( nxrg >= i ) ) THEN
1016!                            nac_cd_surf(k,j,i) = nac_cd_surf(k,j,i) +        &  !<--- segmentation fault
1017!                                                  dy_int * dz_int * turb_cd_nacelle(inot)
1018!                         ENDIF   
1019!                      ENDDO
1020!                   ENDIF
1021!                ENDDO
1022!             ENDIF
1023!
1024!          ENDDO
1025!       
1026!          CALL exchange_horiz( nac_cd_surf, nbgp )                                !<---  segmentation fault
1027
1028       ENDDO   ! end of loop over turbines
1029
1030       tow_cd_surf   = tow_cd_surf   / ( dx * dy * dz )      ! Normalize tower drag
1031       nac_cd_surf = nac_cd_surf / ( dx * dy * dz )      ! Normalize nacelle drag
1032
1033       CALL wtm_read_blade_tables
1034 
1035    END SUBROUTINE wtm_init
1036
1037
1038!------------------------------------------------------------------------------!
1039! Description:
1040! ------------
1041!> Read in layout of the rotor blade , the lift and drag tables
1042!> and the distribution of lift and drag tables along the blade
1043!------------------------------------------------------------------------------!
1044!
1045    SUBROUTINE wtm_read_blade_tables
1046
1047
1048       IMPLICIT NONE
1049
1050       INTEGER(iwp) ::  ii   !< running index
1051       INTEGER(iwp) ::  jj   !< running index
1052   
1053       INTEGER(iwp) ::  ierrn       !<
1054   
1055       CHARACTER(200) :: chmess     !< Read in string
1056
1057       INTEGER(iwp) ::  dlen        !< no. rows of local table
1058       INTEGER(iwp) ::  dlenbl      !< no. rows of cd, cl table
1059       INTEGER(iwp) ::  ialpha      !< table position of current alpha value
1060       INTEGER(iwp) ::  iialpha     !<
1061       INTEGER(iwp) ::  iir         !<
1062       INTEGER(iwp) ::  radres      !< radial resolution
1063       INTEGER(iwp) ::  t1          !< no. of airfoil
1064       INTEGER(iwp) ::  t2          !< no. of airfoil
1065       INTEGER(iwp) ::  trow        !<
1066       INTEGER(iwp) ::  dlenbl_int  !< no. rows of interpolated cd, cl tables
1067   
1068       REAL(wp) :: alpha_attack_i   !<
1069       REAL(wp) :: weight_a         !<
1070       REAL(wp) :: weight_b         !<
1071
1072       INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: ttoint1    !<
1073       INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: ttoint2    !<
1074   
1075       REAL(wp), DIMENSION(:), ALLOCATABLE :: turb_cd_sel1   !<
1076       REAL(wp), DIMENSION(:), ALLOCATABLE :: turb_cd_sel2   !<
1077       REAL(wp), DIMENSION(:), ALLOCATABLE :: turb_cl_sel1   !<
1078       REAL(wp), DIMENSION(:), ALLOCATABLE :: turb_cl_sel2   !<
1079       REAL(wp), DIMENSION(:), ALLOCATABLE :: read_cl_cd     !< read in var array
1080             
1081       REAL(wp), DIMENSION(:), ALLOCATABLE    :: alpha_attack_tab   !<
1082       REAL(wp), DIMENSION(:), ALLOCATABLE    :: trad1              !<
1083       REAL(wp), DIMENSION(:), ALLOCATABLE    :: trad2              !<         
1084       REAL(wp), DIMENSION(:,:), ALLOCATABLE  :: turb_cd_table      !<
1085       REAL(wp), DIMENSION(:,:), ALLOCATABLE  :: turb_cl_table      !<
1086                                         
1087       ALLOCATE ( read_cl_cd(1:2*nairfoils+1) )
1088
1089!
1090!--    Read in the distribution of lift and drag tables along the blade, the
1091!--    layout of the rotor blade and the lift and drag tables:
1092
1093       OPEN ( 201, FILE='WTM_DATA', STATUS='OLD', FORM='FORMATTED', IOSTAT=ierrn )
1094
1095       IF ( ierrn /= 0 )  THEN
1096          message_string = 'file WTM_DATA does not exist'
1097          CALL message( 'wtm_init', 'PA0???', 1, 2, 0, 6, 0 )
1098       ENDIF
1099!
1100!--    Read distribution table:
1101
1102       dlen = 0
1103
1104       READ ( 201, '(3/)' )
1105
1106       rloop3: DO
1107          READ ( 201, *, IOSTAT=ierrn ) chmess
1108          IF ( ierrn < 0  .OR.  chmess == '#'  .OR.  chmess == '')  EXIT rloop3
1109          dlen = dlen + 1
1110       ENDDO rloop3
1111
1112       ALLOCATE( trad1(1:dlen), trad2(1:dlen), ttoint1(1:dlen), ttoint2(1:dlen))
1113
1114       DO jj = 1,dlen+1
1115          BACKSPACE ( 201, IOSTAT=ierrn )
1116       ENDDO
1117
1118       DO jj = 1,dlen
1119          READ ( 201, * ) trad1(jj), trad2(jj), ttoint1(jj), ttoint2(jj)
1120       ENDDO
1121
1122!
1123!--    Read layout table:
1124
1125       dlen = 0 
1126
1127       READ ( 201, '(3/)')
1128
1129       rloop1: DO
1130          READ ( 201, *, IOSTAT=ierrn ) chmess
1131          IF ( ierrn < 0  .OR.  chmess == '#'  .OR.  chmess == '')  EXIT rloop1
1132          dlen = dlen + 1
1133       ENDDO rloop1
1134
1135       ALLOCATE( lrd(1:dlen), ard(1:dlen), crd(1:dlen) )
1136       DO jj = 1, dlen+1
1137          BACKSPACE ( 201, IOSTAT=ierrn )
1138       ENDDO             
1139       DO jj = 1, dlen
1140          READ ( 201, * ) lrd(jj), ard(jj), crd(jj) 
1141       ENDDO
1142
1143!
1144!--    Read tables (turb_cl(alpha),turb_cd(alpha) for the different profiles:
1145
1146       dlen = 0
1147
1148       READ ( 201, '(3/)' )
1149
1150       rloop2: DO
1151          READ ( 201, *, IOSTAT=ierrn ) chmess
1152          IF ( ierrn < 0  .OR.  chmess == '#'  .OR.  chmess == '')  EXIT rloop2
1153          dlen = dlen + 1
1154       ENDDO rloop2 
1155
1156       ALLOCATE( alpha_attack_tab(1:dlen), turb_cl_table(1:dlen,1:nairfoils),  &
1157                 turb_cd_table(1:dlen,1:nairfoils) )
1158
1159       DO jj = 1,dlen+1
1160          BACKSPACE ( 201, IOSTAT=ierrn )
1161       ENDDO 
1162
1163       DO jj = 1,dlen
1164          READ ( 201, * ) read_cl_cd
1165          alpha_attack_tab(jj) = read_cl_cd(1)
1166          DO ii= 1, nairfoils
1167             turb_cl_table(jj,ii) = read_cl_cd(ii*2)
1168             turb_cd_table(jj,ii) = read_cl_cd(ii*2+1)
1169          ENDDO
1170
1171       ENDDO
1172
1173       dlenbl = dlen 
1174
1175       CLOSE ( 201 )
1176
1177!
1178!--    For each possible radial position (resolution: 0.1 m --> 630 values) and
1179!--    each possible angle of attack (resolution: 0.01 degrees --> 36000 values!)
1180!--    determine the lift and drag coefficient by interpolating between the
1181!--    tabulated values of each table (interpolate to current angle of attack)
1182!--    and between the tables (interpolate to current radial position):
1183
1184       ALLOCATE( turb_cl_sel1(0:dlenbl) ) 
1185       ALLOCATE( turb_cl_sel2(0:dlenbl) ) 
1186       ALLOCATE( turb_cd_sel1(0:dlenbl) )
1187       ALLOCATE( turb_cd_sel2(0:dlenbl) )
1188
1189       radres     = INT( rr(1) * 10.0_wp ) + 1_iwp
1190       dlenbl_int = INT( 360.0_wp / accu_cl_cd_tab ) + 1_iwp
1191
1192
1193       ALLOCATE( turb_cl_tab(0:dlenbl_int,1:radres) )
1194       ALLOCATE( turb_cd_tab(0:dlenbl_int,1:radres) )
1195
1196
1197       DO iir = 1, radres ! loop over radius
1198
1199          DO iialpha = 1, dlenbl_int  ! loop over angles
1200
1201             cur_r = ( iir - 1_iwp ) * 0.1_wp             
1202             alpha_attack_i = -180.0_wp + REAL( iialpha-1 ) * accu_cl_cd_tab
1203             ialpha = 1
1204
1205             DO WHILE ( ( alpha_attack_i > alpha_attack_tab(ialpha) ) .AND. (ialpha <= dlen ) )
1206                ialpha = ialpha + 1
1207             ENDDO
1208!
1209!--          Find position in table
1210             lct = MINLOC( ABS( trad1 - cur_r ) )
1211!                lct(1) = lct(1)
1212
1213             IF ( ( trad1(lct(1)) - cur_r ) .GT. 0.0 )  THEN
1214                lct(1) = lct(1) - 1
1215             ENDIF
1216
1217             trow = lct(1)
1218!
1219!--          Calculate weights for interpolation
1220             weight_a = ( trad2(trow) - cur_r ) / ( trad2(trow) - trad1(trow) )
1221             weight_b = ( cur_r - trad1(trow) ) / ( trad2(trow) - trad1(trow) )
1222             t1 = ttoint1(trow)
1223             t2 = ttoint2(trow)
1224
1225             IF ( t1 .EQ. t2 ) THEN  ! if both are the same, the weights are NaN
1226                weight_a = 0.5_wp    ! then do interpolate in between same twice
1227                weight_b = 0.5_wp    ! using 0.5 as weight
1228             ENDIF
1229
1230             IF ( t1 == 0 .AND. t2 == 0 ) THEN
1231                turb_cd_sel1 = 0.0_wp
1232                turb_cd_sel2 = 0.0_wp
1233                turb_cl_sel1 = 0.0_wp
1234                turb_cl_sel2 = 0.0_wp
1235             ELSE
1236                turb_cd_sel1 = turb_cd_table(:,t1)
1237                turb_cd_sel2 = turb_cd_table(:,t2)
1238                turb_cl_sel1 = turb_cl_table(:,t1)
1239                turb_cl_sel2 = turb_cl_table(:,t2)
1240             ENDIF
1241
1242!
1243!--          Interpolation of lift and drag coefficiencts on fine grid of radius
1244!--          segments and angles of attack
1245
1246             turb_cl_tab(iialpha,iir) = ( alpha_attack_tab(ialpha) -           &
1247                                          alpha_attack_i ) /                   &
1248                                        ( alpha_attack_tab(ialpha) -           &
1249                                          alpha_attack_tab(ialpha-1) ) *       &
1250                                        ( weight_a * turb_cl_sel1(ialpha-1) +  &
1251                                          weight_b * turb_cl_sel2(ialpha-1) ) +&
1252                                        ( alpha_attack_i             -         &
1253                                          alpha_attack_tab(ialpha-1) ) /       &
1254                                        ( alpha_attack_tab(ialpha) -           &
1255                                          alpha_attack_tab(ialpha-1) ) *       &
1256                                        ( weight_a * turb_cl_sel1(ialpha) +    &
1257                                          weight_b * turb_cl_sel2(ialpha) )
1258             turb_cd_tab(iialpha,iir) = ( alpha_attack_tab(ialpha) -           &
1259                                          alpha_attack_i ) /                   &
1260                                        ( alpha_attack_tab(ialpha) -           &
1261                                          alpha_attack_tab(ialpha-1) ) *       &
1262                                        ( weight_a * turb_cd_sel1(ialpha-1) +  &
1263                                          weight_b * turb_cd_sel2(ialpha-1) ) +&
1264                                        ( alpha_attack_i             -         &
1265                                          alpha_attack_tab(ialpha-1) ) /       &
1266                                        ( alpha_attack_tab(ialpha) -           &
1267                                          alpha_attack_tab(ialpha-1) ) *       &
1268                                        ( weight_a * turb_cd_sel1(ialpha) +    &
1269                                          weight_b * turb_cd_sel2(ialpha) )
1270   
1271          ENDDO   ! end loop over angles of attack
1272
1273       ENDDO   ! end loop over radius
1274   
1275    END SUBROUTINE wtm_read_blade_tables
1276
1277
1278!------------------------------------------------------------------------------!
1279! Description:
1280! ------------
1281!> The projection matrix for the coordinate system of therotor disc in respect
1282!> to the yaw and tilt angle of the rotor is calculated
1283!------------------------------------------------------------------------------!
1284    SUBROUTINE wtm_rotate_rotor( inot )
1285
1286
1287       IMPLICIT NONE
1288
1289       INTEGER(iwp) :: inot
1290!
1291!--    Calculation of the rotation matrix for the application of the tilt to
1292!--    the rotors
1293       rot_eigen_rad(1) = SIN( phi_yaw(inot) )    ! x-component of the radial eigenvector
1294       rot_eigen_rad(2) = COS( phi_yaw(inot) )    ! y-component of the radial eigenvector
1295       rot_eigen_rad(3) = 0.0_wp                  ! z-component of the radial eigenvector
1296
1297       rot_eigen_azi(1) = 0.0_wp                  ! x-component of the azimuth eigenvector
1298       rot_eigen_azi(2) = 0.0_wp                  ! y-component of the azimuth eigenvector
1299       rot_eigen_azi(3) = 1.0_wp                  ! z-component of the azimuth eigenvector
1300
1301       rot_eigen_nor(1) =  COS( phi_yaw(inot) )   ! x-component of the normal eigenvector
1302       rot_eigen_nor(2) = -SIN( phi_yaw(inot) )   ! y-component of the normal eigenvector
1303       rot_eigen_nor(3) = 0.0_wp                  ! z-component of the normal eigenvector
1304   
1305!
1306!--    Calculation of the coordinate transformation matrix to apply a tilt to
1307!--    the rotor. If tilt = 0, rot_coord_trans is a unit matrix.
1308
1309       rot_coord_trans(inot,1,1) = rot_eigen_rad(1)**2                   *     &
1310                                   ( 1.0_wp - COS( tilt ) ) + COS( tilt ) 
1311       rot_coord_trans(inot,1,2) = rot_eigen_rad(1) * rot_eigen_rad(2)   *     &
1312                                   ( 1.0_wp - COS( tilt ) )              -     &
1313                                   rot_eigen_rad(3) * SIN( tilt )
1314       rot_coord_trans(inot,1,3) = rot_eigen_rad(1) * rot_eigen_rad(3)   *     &
1315                                   ( 1.0_wp - COS( tilt ) )              +     &
1316                                   rot_eigen_rad(2) * SIN( tilt )
1317       rot_coord_trans(inot,2,1) = rot_eigen_rad(2) * rot_eigen_rad(1)   *     &
1318                                   ( 1.0_wp - COS( tilt ) )              +     &
1319                                   rot_eigen_rad(3) * SIN( tilt )
1320       rot_coord_trans(inot,2,2) = rot_eigen_rad(2)**2                   *     &
1321                                   ( 1.0_wp - COS( tilt ) ) + COS( tilt ) 
1322       rot_coord_trans(inot,2,3) = rot_eigen_rad(2) * rot_eigen_rad(3)   *     &
1323                                   ( 1.0_wp - COS( tilt ) )              -     &
1324                                   rot_eigen_rad(1) * SIN( tilt )
1325       rot_coord_trans(inot,3,1) = rot_eigen_rad(3) * rot_eigen_rad(1)   *     &
1326                                   ( 1.0_wp - COS( tilt ) )              -     &
1327                                   rot_eigen_rad(2) * SIN( tilt )
1328       rot_coord_trans(inot,3,2) = rot_eigen_rad(3) * rot_eigen_rad(2)   *     &
1329                                   ( 1.0_wp - COS( tilt ) )              +     &
1330                                   rot_eigen_rad(1) * SIN( tilt )
1331       rot_coord_trans(inot,3,3) = rot_eigen_rad(3)**2                   *     &
1332                                   ( 1.0_wp - COS( tilt ) ) + COS( tilt )
1333
1334!
1335!--    Vectors for the Transformation of forces from the rotor's spheric
1336!--    coordinate system to the cartesian coordinate system
1337       rotx(inot,:) = MATMUL( rot_coord_trans(inot,:,:), rot_eigen_nor )
1338       roty(inot,:) = MATMUL( rot_coord_trans(inot,:,:), rot_eigen_rad )
1339       rotz(inot,:) = MATMUL( rot_coord_trans(inot,:,:), rot_eigen_azi )
1340   
1341    END SUBROUTINE wtm_rotate_rotor
1342
1343
1344!------------------------------------------------------------------------------!
1345! Description:
1346! ------------
1347!> Calculation of the forces generated by the wind turbine
1348!------------------------------------------------------------------------------!
1349    SUBROUTINE wtm_forces
1350
1351
1352       IMPLICIT NONE
1353
1354       CHARACTER (LEN=2) ::  turbine_id
1355
1356       INTEGER(iwp) ::  i, j, k          !< loop indices
1357       INTEGER(iwp) ::  inot             !< turbine loop index (turbine id)
1358       INTEGER(iwp) ::  iialpha, iir     !<
1359       INTEGER(iwp) ::  rseg, rseg_int   !<
1360       INTEGER(iwp) ::  ring, ring_int   !<
1361       INTEGER(iwp) ::  ii, jj, kk       !<
1362   
1363       REAL(wp)     ::  flag               !< flag to mask topography grid points
1364       REAL(wp)     ::  sin_rot, cos_rot   !<
1365       REAL(wp)     ::  sin_yaw, cos_yaw   !<
1366       
1367       REAL(wp) ::  aa, bb, cc, dd  !< interpolation distances
1368       REAL(wp) ::  gg              !< interpolation volume var 
1369       
1370       REAL(wp) ::  dist_u_3d, dist_v_3d, dist_w_3d  !< smearing distances
1371
1372       
1373!
1374!      Variables for pitch control
1375       REAL(wp)     ::  torque_max=0.0_wp
1376       LOGICAL      ::  pitch_sw=.FALSE.
1377
1378       INTEGER(iwp), DIMENSION(1) :: lct=0
1379       REAL(wp), DIMENSION(1)     :: rad_d=0.0_wp
1380       
1381       REAL(wp) :: tl_factor !< factor for tip loss correction
1382
1383
1384       CALL cpu_log( log_point_s(61), 'wtm_forces', 'start' )
1385
1386
1387       IF ( simulated_time >= time_turbine_on ) THEN
1388
1389!
1390!--       Set forces to zero for each new time step:
1391          thrust(:,:,:)         = 0.0_wp
1392          torque_y(:,:,:)       = 0.0_wp
1393          torque_z(:,:,:)       = 0.0_wp
1394          torque_total(:)       = 0.0_wp
1395          rot_tend_x(:,:,:)     = 0.0_wp
1396          rot_tend_y(:,:,:)     = 0.0_wp
1397          rot_tend_z(:,:,:)     = 0.0_wp
1398          thrust_rotor(:)       = 0.0_wp
1399!
1400!--       Loop over number of turbines:
1401          DO inot = 1, nturbines
1402
1403             cos_yaw = COS(phi_yaw(inot))
1404             sin_yaw = SIN(phi_yaw(inot))
1405!
1406!--          Loop over rings of each turbine:
1407             DO ring = 1, nrings(inot)
1408
1409                thrust_seg(:)   = 0.0_wp
1410                torque_seg_y(:) = 0.0_wp
1411                torque_seg_z(:) = 0.0_wp
1412!
1413!--             Determine distance between each ring (center) and the hub:
1414                cur_r = (ring - 0.5_wp) * delta_r(inot)
1415
1416!
1417!--             Loop over segments of each ring of each turbine:
1418                DO rseg = 1, nsegs(ring,inot)
1419!
1420!--                !-----------------------------------------------------------!
1421!--                !-- Determine coordinates of the ring segments            --!
1422!--                !-----------------------------------------------------------!
1423!
1424!--                Determine angle of ring segment towards zero degree angle of
1425!--                rotor system (at zero degree rotor direction vectors aligned
1426!--                with y-axis):
1427                   phi_rotor = rseg * 2.0_wp * pi / nsegs(ring,inot)
1428                   cos_rot   = COS( phi_rotor )
1429                   sin_rot   = SIN( phi_rotor )
1430
1431!--                Now the direction vectors can be determined with respect to
1432!--                the yaw and tilt angle:
1433                   re(1) = cos_rot * sin_yaw
1434                   re(2) = cos_rot * cos_yaw   
1435                   re(3) = sin_rot
1436
1437                   rote = MATMUL( rot_coord_trans(inot,:,:), re )
1438!
1439!--                Coordinates of the single segments (center points):
1440                   rbx(ring,rseg) = rcx(inot) + cur_r * rote(1)
1441                   rby(ring,rseg) = rcy(inot) + cur_r * rote(2)
1442                   rbz(ring,rseg) = rcz(inot) + cur_r * rote(3)
1443
1444!--                !-----------------------------------------------------------!
1445!--                !-- Interpolation of the velocity components from the     --!
1446!--                !-- cartesian grid point to the coordinates of each ring  --!
1447!--                !-- segment (follows a method used in the particle model) --!
1448!--                !-----------------------------------------------------------!
1449
1450                   u_int(inot,ring,rseg)     = 0.0_wp
1451                   u_int_1_l(inot,ring,rseg) = 0.0_wp
1452
1453                   v_int(inot,ring,rseg)     = 0.0_wp
1454                   v_int_1_l(inot,ring,rseg) = 0.0_wp
1455
1456                   w_int(inot,ring,rseg)     = 0.0_wp
1457                   w_int_1_l(inot,ring,rseg) = 0.0_wp
1458
1459!
1460!--                Interpolation of the u-component:
1461
1462                   ii =   rbx(ring,rseg) * ddx
1463                   jj = ( rby(ring,rseg) - 0.5_wp * dy ) * ddy
1464                   kk = ( rbz(ring,rseg) - 0.5_wp * dz ) / dz
1465!
1466!--                Interpolate only if all required information is available on
1467!--                the current PE:
1468                   IF ( ( ii >= nxl )  .AND.  ( ii <= nxr ) )  THEN
1469                      IF ( ( jj >= nys )  .AND.  ( jj <= nyn ) )  THEN
1470
1471                         aa = ( ( ii + 1          ) * dx - rbx(ring,rseg) ) *  &
1472                              ( ( jj + 1 + 0.5_wp ) * dy - rby(ring,rseg) )
1473                         bb = ( rbx(ring,rseg) - ii * dx )                  *  &
1474                              ( ( jj + 1 + 0.5_wp ) * dy - rby(ring,rseg) )
1475                         cc = ( ( ii+1            ) * dx - rbx(ring,rseg) ) *  &
1476                              ( rby(ring,rseg) - ( jj + 0.5_wp ) * dy )
1477                         dd = ( rbx(ring,rseg) -              ii * dx )     *  &
1478                              ( rby(ring,rseg) - ( jj + 0.5_wp ) * dy ) 
1479                         gg = dx * dy
1480
1481                         u_int_l = ( aa * u(kk,jj,ii)     +                    &
1482                                     bb * u(kk,jj,ii+1)   +                    &
1483                                     cc * u(kk,jj+1,ii)   +                    &
1484                                     dd * u(kk,jj+1,ii+1)                      &
1485                                   ) / gg
1486
1487                         u_int_u = ( aa * u(kk+1,jj,ii)     +                  &
1488                                     bb * u(kk+1,jj,ii+1)   +                  &
1489                                     cc * u(kk+1,jj+1,ii)   +                  &
1490                                     dd * u(kk+1,jj+1,ii+1)                    &
1491                                   ) / gg
1492
1493                         u_int_1_l(inot,ring,rseg) = u_int_l          +        &
1494                                     ( rbz(ring,rseg) - zu(kk) ) / dz *        &
1495                                     ( u_int_u - u_int_l )
1496
1497                      ELSE
1498                         u_int_1_l(inot,ring,rseg) = 0.0_wp
1499                      ENDIF
1500                   ELSE
1501                      u_int_1_l(inot,ring,rseg) = 0.0_wp
1502                   ENDIF
1503
1504
1505!
1506!--                Interpolation of the v-component:
1507                   ii = ( rbx(ring,rseg) - 0.5_wp * dx ) * ddx
1508                   jj =   rby(ring,rseg)                 * ddy
1509                   kk = ( rbz(ring,rseg) + 0.5_wp * dz ) / dz 
1510!
1511!--                Interpolate only if all required information is available on
1512!--                the current PE:
1513                   IF ( ( ii >= nxl )  .AND.  ( ii <= nxr ) )  THEN
1514                      IF ( ( jj >= nys )  .AND.  ( jj <= nyn ) )  THEN
1515
1516                         aa = ( ( ii + 1 + 0.5_wp ) * dx - rbx(ring,rseg) ) *  &
1517                              ( ( jj + 1 )          * dy - rby(ring,rseg) )
1518                         bb = ( rbx(ring,rseg)     - ( ii + 0.5_wp ) * dx ) *  &
1519                              ( ( jj + 1 ) * dy          - rby(ring,rseg) )
1520                         cc = ( ( ii + 1 + 0.5_wp ) * dx - rbx(ring,rseg) ) *  &
1521                              ( rby(ring,rseg)           -        jj * dy )
1522                         dd = ( rbx(ring,rseg)     - ( ii + 0.5_wp ) * dx ) *  &
1523                              ( rby(ring,rseg)           -        jj * dy )
1524                         gg = dx * dy
1525
1526                         v_int_l = ( aa * v(kk,jj,ii)     +                    &
1527                                     bb * v(kk,jj,ii+1)   +                    &
1528                                     cc * v(kk,jj+1,ii)   +                    &
1529                                     dd * v(kk,jj+1,ii+1)                      &
1530                                   ) / gg
1531
1532                         v_int_u = ( aa * v(kk+1,jj,ii)     +                  &
1533                                     bb * v(kk+1,jj,ii+1)   +                  &
1534                                     cc * v(kk+1,jj+1,ii)   +                  &
1535                                     dd * v(kk+1,jj+1,ii+1)                    &
1536                                  ) / gg
1537
1538                         v_int_1_l(inot,ring,rseg) = v_int_l +                 &
1539                                     ( rbz(ring,rseg) - zu(kk) ) / dz *        &
1540                                     ( v_int_u - v_int_l )
1541
1542                      ELSE
1543                         v_int_1_l(inot,ring,rseg) = 0.0_wp
1544                      ENDIF
1545                   ELSE
1546                      v_int_1_l(inot,ring,rseg) = 0.0_wp
1547                   ENDIF
1548
1549
1550!
1551!--                Interpolation of the w-component:
1552                   ii = ( rbx(ring,rseg) - 0.5_wp * dx ) * ddx
1553                   jj = ( rby(ring,rseg) - 0.5_wp * dy ) * ddy
1554                   kk =   rbz(ring,rseg)                 / dz
1555!
1556!--                Interpolate only if all required information is available on
1557!--                the current PE:
1558                   IF ( ( ii >= nxl )  .AND.  ( ii <= nxr ) )  THEN
1559                      IF ( ( jj >= nys )  .AND.  ( jj <= nyn ) )  THEN
1560
1561                         aa = ( ( ii + 1 + 0.5_wp ) * dx - rbx(ring,rseg) ) *  &
1562                              ( ( jj + 1 + 0.5_wp ) * dy - rby(ring,rseg) )
1563                         bb = ( rbx(ring,rseg)     - ( ii + 0.5_wp ) * dx ) *  &
1564                              ( ( jj + 1 + 0.5_wp ) * dy - rby(ring,rseg) )
1565                         cc = ( ( ii + 1 + 0.5_wp ) * dx - rbx(ring,rseg) ) *  &
1566                              ( rby(ring,rseg)     - ( jj + 0.5_wp ) * dy )
1567                         dd = ( rbx(ring,rseg)     - ( ii + 0.5_wp ) * dx ) *  &
1568                              ( rby(ring,rseg)     - ( jj + 0.5_wp ) * dy )
1569                         gg = dx * dy
1570
1571                         w_int_l = ( aa * w(kk,jj,ii)     +                    &
1572                                     bb * w(kk,jj,ii+1)   +                    &
1573                                     cc * w(kk,jj+1,ii)   +                    &
1574                                     dd * w(kk,jj+1,ii+1)                      &
1575                                   ) / gg
1576
1577                         w_int_u = ( aa * w(kk+1,jj,ii)     +                  &
1578                                     bb * w(kk+1,jj,ii+1)   +                  &
1579                                     cc * w(kk+1,jj+1,ii)   +                  &
1580                                     dd * w(kk+1,jj+1,ii+1)                    &
1581                                    ) / gg
1582
1583                         w_int_1_l(inot,ring,rseg) = w_int_l +                 &
1584                                     ( rbz(ring,rseg) - zw(kk) ) / dz *        &
1585                                     ( w_int_u - w_int_l )
1586                      ELSE
1587                         w_int_1_l(inot,ring,rseg) = 0.0_wp
1588                      ENDIF
1589                   ELSE
1590                      w_int_1_l(inot,ring,rseg) = 0.0_wp
1591                   ENDIF
1592
1593                ENDDO
1594             ENDDO
1595
1596          ENDDO
1597
1598!
1599!--       Exchange between PEs (information required on each PE):
1600#if defined( __parallel )
1601          CALL MPI_ALLREDUCE( u_int_1_l, u_int, nturbines * MAXVAL(nrings) *   &
1602                              MAXVAL(nsegs), MPI_REAL, MPI_SUM, comm2d, ierr )
1603          CALL MPI_ALLREDUCE( v_int_1_l, v_int, nturbines * MAXVAL(nrings) *   &
1604                              MAXVAL(nsegs), MPI_REAL, MPI_SUM, comm2d, ierr )
1605          CALL MPI_ALLREDUCE( w_int_1_l, w_int, nturbines * MAXVAL(nrings) *   &
1606                              MAXVAL(nsegs), MPI_REAL, MPI_SUM, comm2d, ierr )
1607#else
1608          u_int = u_int_1_l
1609          v_int = v_int_1_l
1610          w_int = w_int_1_l
1611#endif
1612
1613
1614!
1615!--       Loop over number of turbines:
1616
1617          DO inot = 1, nturbines
1618pit_loop: DO
1619
1620             IF ( pitch_sw )  THEN
1621                torque_total(inot) = 0.0_wp
1622                thrust_rotor(inot) = 0.0_wp
1623                pitch_add(inot)    = pitch_add(inot) + 0.25_wp
1624!                 IF ( myid == 0 ) PRINT*, 'Pitch', inot, pitch_add(inot)
1625             ELSE
1626                cos_yaw = COS(phi_yaw(inot))
1627                sin_yaw = SIN(phi_yaw(inot))
1628                IF ( pitch_control )  THEN
1629                   pitch_add(inot) = MAX(pitch_add_old(inot) - pitch_rate *    &
1630                                         dt_3d , 0.0_wp )
1631                ENDIF
1632             ENDIF
1633
1634!
1635!--          Loop over rings of each turbine:
1636             DO ring = 1, nrings(inot)
1637!
1638!--             Determine distance between each ring (center) and the hub:
1639                cur_r = (ring - 0.5_wp) * delta_r(inot)
1640!
1641!--             Loop over segments of each ring of each turbine:
1642                DO rseg = 1, nsegs(ring,inot)
1643!
1644!--                Determine angle of ring segment towards zero degree angle of
1645!--                rotor system (at zero degree rotor direction vectors aligned
1646!--                with y-axis):
1647                   phi_rotor = rseg * 2.0_wp * pi / nsegs(ring,inot)
1648                   cos_rot   = COS(phi_rotor)
1649                   sin_rot   = SIN(phi_rotor)
1650!
1651!--                Now the direction vectors can be determined with respect to
1652!--                the yaw and tilt angle:
1653                   re(1) = cos_rot * sin_yaw
1654                   re(2) = cos_rot * cos_yaw
1655                   re(3) = sin_rot
1656
1657!                  The current unit vector in azimuthal direction:                         
1658                   rea(1) = - sin_rot * sin_yaw
1659                   rea(2) = - sin_rot * cos_yaw
1660                   rea(3) =   cos_rot
1661
1662!
1663!--                To respect the yawing angle for the calculations of
1664!--                velocities and forces the unit vectors perpendicular to the
1665!--                rotor area in direction of the positive yaw angle are defined:
1666                   ren(1) =   cos_yaw
1667                   ren(2) = - sin_yaw
1668                   ren(3) = 0.0_wp
1669!
1670!--                Multiplication with the coordinate transformation matrix
1671!--                gives the final unit vector with consideration of the rotor
1672!--                tilt:
1673                   rote = MATMUL( rot_coord_trans(inot,:,:), re )
1674                   rota = MATMUL( rot_coord_trans(inot,:,:), rea )
1675                   rotn = MATMUL( rot_coord_trans(inot,:,:), ren )
1676!
1677!--                Coordinates of the single segments (center points):
1678                   rbx(ring,rseg) = rcx(inot) + cur_r * rote(1)
1679
1680                   rby(ring,rseg) = rcy(inot) + cur_r * rote(2)
1681
1682                   rbz(ring,rseg) = rcz(inot) + cur_r * rote(3)
1683
1684!
1685!--                !-----------------------------------------------------------!
1686!--                !-- Calculation of various angles and relative velocities --!
1687!--                !-----------------------------------------------------------!
1688!
1689!--                In the following the 3D-velocity field is projected its
1690!--                components perpedicular and parallel to the rotor area
1691!--                The calculation of forces will be done in the rotor-
1692!--                coordinates y' and z.
1693!--                The yaw angle will be reintroduced when the force is applied
1694!--                on the hydrodynamic equations
1695!
1696!--                Projection of the xy-velocities relative to the rotor area
1697!
1698!--                Velocity perpendicular to the rotor area:
1699                   u_rot = u_int(inot,ring,rseg)*rotn(1) +                     &
1700                   v_int(inot,ring,rseg)*rotn(2) +                             &
1701                   w_int(inot,ring,rseg)*rotn(3)
1702!
1703!--                Projection of the 3D-velocity vector in the azimuthal
1704!--                direction:
1705                   vtheta(rseg) = rota(1) * u_int(inot,ring,rseg) +            & 
1706                                  rota(2) * v_int(inot,ring,rseg) +            &
1707                                  rota(3) * w_int(inot,ring,rseg)
1708!
1709!--                Determination of the angle phi_rel between the rotor plane
1710!--                and the direction of the flow relative to the rotor:
1711
1712                   phi_rel(rseg) = ATAN( u_rot /                               &
1713                                         ( omega_rot(inot) * cur_r -           &
1714                                           vtheta(rseg) ) )
1715
1716!
1717!--                Interpolation of the local pitch angle from tabulated values
1718!--                to the current radial position:
1719
1720                   lct=minloc(ABS(cur_r-lrd))
1721                   rad_d=cur_r-lrd(lct)
1722                   
1723                   IF (cur_r == 0.0_wp) THEN
1724                      alpha_attack(rseg) = 0.0_wp
1725                   ELSE IF (cur_r >= lrd(size(ard))) THEN
1726                      alpha_attack(rseg) = ( ard(size(ard)) +                  &
1727                                             ard(size(ard)-1) ) / 2.0_wp
1728                   ELSE
1729                      alpha_attack(rseg) = ( ard(lct(1)) *  &
1730                                             ( ( lrd(lct(1)+1) - cur_r ) /     &
1731                                               ( lrd(lct(1)+1) - lrd(lct(1)) ) &
1732                                             ) ) + ( ard(lct(1)+1) *           &
1733                                             ( ( cur_r - lrd(lct(1)) ) /       &
1734                                               ( lrd(lct(1)+1) - lrd(lct(1)) ) ) )
1735                   ENDIF
1736
1737!
1738!--                In Fortran radian instead of degree is used as unit for all
1739!--                angles. Therefore, a transformation from angles given in
1740!--                degree to angles given in radian is necessary here:
1741                   alpha_attack(rseg) = alpha_attack(rseg) *                   &
1742                                        ( (2.0_wp*pi) / 360.0_wp )
1743!
1744!--                Substraction of the local pitch angle to obtain the local
1745!--                angle of attack:
1746                   alpha_attack(rseg) = phi_rel(rseg) - alpha_attack(rseg)
1747!
1748!--                Preliminary transformation back from angles given in radian
1749!--                to angles given in degree:
1750                   alpha_attack(rseg) = alpha_attack(rseg) *                   &
1751                                        ( 360.0_wp / (2.0_wp*pi) )
1752!
1753!--                Correct with collective pitch angle:
1754                   alpha_attack = alpha_attack - pitch_add(inot)
1755
1756!
1757!--                Determination of the magnitude of the flow velocity relative
1758!--                to the rotor:
1759                   vrel(rseg) = SQRT( u_rot**2 +                               &
1760                                      ( omega_rot(inot) * cur_r -              &
1761                                        vtheta(rseg) )**2 )
1762
1763!
1764!--                !-----------------------------------------------------------!
1765!--                !-- Interpolation of chord as well as lift and drag       --!
1766!--                !-- coefficients from tabulated values                    --!
1767!--                !-----------------------------------------------------------!
1768
1769!
1770!--                Interpolation of the chord_length from tabulated values to
1771!--                the current radial position:
1772
1773                   IF (cur_r == 0.0_wp) THEN
1774                      chord(rseg) = 0.0_wp
1775                   ELSE IF (cur_r >= lrd(size(crd))) THEN
1776                      chord(rseg) = (crd(size(crd)) + ard(size(crd)-1)) / 2.0_wp
1777                   ELSE
1778                      chord(rseg) = ( crd(lct(1)) *                            &
1779                            ( ( lrd(lct(1)+1) - cur_r ) /                      &
1780                              ( lrd(lct(1)+1) - lrd(lct(1)) ) ) ) +            &
1781                            ( crd(lct(1)+1) *                                  &
1782                            ( ( cur_r-lrd(lct(1)) ) /                          &
1783                              ( lrd(lct(1)+1) - lrd(lct(1)) ) ) )
1784                   ENDIF
1785
1786!
1787!--                Determine index of current angle of attack, needed for
1788!--                finding the appropriate interpolated values of the lift and
1789!--                drag coefficients (-180.0 degrees = 0, +180.0 degrees = 36000,
1790!--                so one index every 0.01 degrees):
1791                   iialpha = CEILING( ( alpha_attack(rseg) + 180.0_wp )        &
1792                                      * ( 1.0_wp / accu_cl_cd_tab ) )
1793!
1794!--                Determine index of current radial position, needed for
1795!--                finding the appropriate interpolated values of the lift and
1796!--                drag coefficients (one index every 0.1 m):
1797                   iir = CEILING( cur_r * 10.0_wp )
1798!
1799!--                Read in interpolated values of the lift and drag coefficients
1800!--                for the current radial position and angle of attack:
1801                   turb_cl(rseg) = turb_cl_tab(iialpha,iir)
1802                   turb_cd(rseg) = turb_cd_tab(iialpha,iir)
1803
1804!
1805!--                Final transformation back from angles given in degree to
1806!--                angles given in radian:
1807                   alpha_attack(rseg) = alpha_attack(rseg) *                   &
1808                                        ( (2.0_wp*pi) / 360.0_wp )
1809
1810                   IF ( tl_cor )  THEN
1811                   
1812!--                  Tip loss correction following Schito
1813!--                  Schito applies the tip loss correction only to the lift force
1814!--                  Therefore, the tip loss correction is only applied to the lift
1815!--                  coefficient and not to the drag coefficient in our case
1816!--                 
1817                     tl_factor = ( 2.0 / pi ) *                                &
1818                          ACOS( EXP( -1.0 * ( 3.0 * ( rr(inot) - cur_r ) /     &
1819                          ( 2.0 * cur_r * abs( sin( phi_rel(rseg) ) ) ) ) ) )
1820                         
1821                     turb_cl(rseg)  = tl_factor * turb_cl(rseg)                                 
1822                                 
1823                   END IF               
1824!
1825!--                !-----------------------------------------------------!
1826!--                !-- Calculation of the forces                       --!
1827!--                !-----------------------------------------------------!
1828
1829!
1830!--                Calculate the pre_factor for the thrust and torque forces:
1831
1832                   pre_factor = 0.5_wp * (vrel(rseg)**2) * 3.0_wp *  &
1833                                chord(rseg) * delta_r(inot) / nsegs(ring,inot)
1834
1835!
1836!--                Calculate the thrust force (x-component of the total force)
1837!--                for each ring segment:
1838                   thrust_seg(rseg) = pre_factor *                             &
1839                                      ( turb_cl(rseg) * COS(phi_rel(rseg)) +   &
1840                                        turb_cd(rseg) * SIN(phi_rel(rseg)) )
1841
1842!
1843!--                Determination of the second of the additional forces acting
1844!--                on the flow in the azimuthal direction: force vector as basis
1845!--                for torque (torque itself would be the vector product of the
1846!--                radius vector and the force vector):
1847                   torque_seg = pre_factor *                                   &
1848                                ( turb_cl(rseg) * SIN(phi_rel(rseg)) -         &
1849                                  turb_cd(rseg) * COS(phi_rel(rseg)) )
1850!
1851!--                Decomposition of the force vector into two parts:
1852!--                One acting along the y-direction and one acting along the
1853!--                z-direction of the rotor coordinate system:
1854
1855                   torque_seg_y(rseg) = -torque_seg * sin_rot
1856                   torque_seg_z(rseg) =  torque_seg * cos_rot
1857
1858!
1859!--                Add the segment thrust to the thrust of the whole rotor
1860                   thrust_rotor(inot) = thrust_rotor(inot) +                   &
1861                                        thrust_seg(rseg)                   
1862                   
1863
1864                   torque_total(inot) = torque_total(inot) + (torque_seg * cur_r)
1865
1866                ENDDO   !-- end of loop over ring segments
1867
1868!
1869!--             Restore the forces into arrays containing all the segments of
1870!--             each ring:
1871                thrust_ring(ring,:)   = thrust_seg(:)
1872                torque_ring_y(ring,:) = torque_seg_y(:)
1873                torque_ring_z(ring,:) = torque_seg_z(:)
1874
1875
1876             ENDDO   !-- end of loop over rings
1877
1878
1879             CALL cpu_log( log_point_s(62), 'wtm_controller', 'start' )
1880
1881             
1882             IF ( speed_control )  THEN
1883!
1884!--             Calculation of the current generator speed for rotor speed control
1885             
1886!                                     
1887!--             The acceleration of the rotor speed is calculated from
1888!--             the force balance of the accelerating torque
1889!--             and the torque of the rotating rotor and generator
1890                om_rate = ( torque_total(inot) * air_dens * gear_eff -         &
1891                            gear_ratio * torque_gen_old(inot) ) /              &
1892                          ( inertia_rot +                                      & 
1893                            gear_ratio * gear_ratio * inertia_gen ) * dt_3d
1894
1895!
1896!--             The generator speed is given by the product of gear gear_ratio
1897!--             and rotor speed
1898                omega_gen(inot) = gear_ratio * ( omega_rot(inot) + om_rate )     
1899             
1900             ENDIF
1901             
1902             IF ( pitch_control )  THEN
1903
1904!
1905!--             If the current generator speed is above rated, the pitch is not
1906!--             saturated and the change from the last time step is within the
1907!--             maximum pitch rate, then the pitch loop is repeated with a pitch
1908!--             gain
1909                IF ( (  omega_gen(inot)  > rated_genspeed   )  .AND.           &
1910                     ( pitch_add(inot) < 25.0_wp ) .AND.                       &
1911                     ( pitch_add(inot) < pitch_add_old(inot) +                 & 
1912                       pitch_rate * dt_3d  ) ) THEN
1913                   pitch_sw = .TRUE.
1914!
1915!--                Go back to beginning of pit_loop                   
1916                   CYCLE pit_loop
1917                ENDIF
1918               
1919!
1920!--             The current pitch is saved for the next time step
1921                pitch_add_old(inot) = pitch_add(inot)
1922                pitch_sw = .FALSE.
1923             ENDIF
1924             EXIT pit_loop             
1925          ENDDO pit_loop ! Recursive pitch control loop
1926
1927
1928!
1929!--          Call the rotor speed controller
1930             
1931             IF ( speed_control )  THEN
1932!
1933!--             Find processor at i_hub, j_hub             
1934                IF ( ( nxl <= i_hub(inot) )  .AND.  ( nxr >= i_hub(inot) ) )   &
1935                   THEN
1936                   IF ( ( nys <= j_hub(inot) )  .AND.  ( nyn >= j_hub(inot) ) )&
1937                      THEN
1938                      CALL wtm_speed_control( inot )
1939                   ENDIF
1940                ENDIF
1941                               
1942             ENDIF
1943
1944
1945             CALL cpu_log( log_point_s(62), 'wtm_controller', 'stop' )
1946
1947             CALL cpu_log( log_point_s(63), 'wtm_smearing', 'start' )
1948
1949
1950!--          !-----------------------------------------------------------------!
1951!--          !--                  Regularization kernel                      --!
1952!--          !-- Smearing of the forces and interpolation to cartesian grid  --!
1953!--          !-----------------------------------------------------------------!
1954!
1955!--          The aerodynamic blade forces need to be distributed smoothly on
1956!--          several mesh points in order to avoid singular behaviour
1957!
1958!--          Summation over sum of weighted forces. The weighting factor
1959!--          (calculated in user_init) includes information on the distance
1960!--          between the center of the grid cell and the rotor segment under
1961!--          consideration
1962!
1963!--          To save computing time, apply smearing only for the relevant part
1964!--          of the model domain:
1965!
1966!--
1967!--          Calculation of the boundaries:
1968             i_smear(inot) = CEILING( ( rr(inot) * ABS( roty(inot,1) ) +       &
1969                                        eps_min ) / dx )
1970             j_smear(inot) = CEILING( ( rr(inot) * ABS( roty(inot,2) ) +       &
1971                                        eps_min ) / dy )
1972
1973             DO i = MAX( nxl, i_hub(inot) - i_smear(inot) ),                   &
1974                    MIN( nxr, i_hub(inot) + i_smear(inot) )
1975                DO j = MAX( nys, j_hub(inot) - j_smear(inot) ),                &
1976                        MIN( nyn, j_hub(inot) + j_smear(inot) )
1977!                    DO k = MAX( nzb_u_inner(j,i)+1, k_hub(inot) - k_smear(inot) ), &
1978!                                 k_hub(inot) + k_smear(inot)
1979                   DO  k = nzb+1, k_hub(inot) + k_smear(inot)
1980                      DO ring = 1, nrings(inot)
1981                         DO rseg = 1, nsegs(ring,inot)
1982!
1983!--                         Predetermine flag to mask topography
1984                            flag = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 1 ) )
1985     
1986!
1987!--                         Determine the square of the distance between the
1988!--                         current grid point and each rotor area segment:
1989                            dist_u_3d = ( i * dx               - rbx(ring,rseg) )**2 + &
1990                                        ( j * dy + 0.5_wp * dy - rby(ring,rseg) )**2 + &
1991                                        ( k * dz - 0.5_wp * dz - rbz(ring,rseg) )**2
1992                            dist_v_3d = ( i * dx + 0.5_wp * dx - rbx(ring,rseg) )**2 + &
1993                                        ( j * dy               - rby(ring,rseg) )**2 + &
1994                                        ( k * dz - 0.5_wp * dz - rbz(ring,rseg) )**2
1995                            dist_w_3d = ( i * dx + 0.5_wp * dx - rbx(ring,rseg) )**2 + &
1996                                        ( j * dy + 0.5_wp * dy - rby(ring,rseg) )**2 + &
1997                                        ( k * dz               - rbz(ring,rseg) )**2
1998
1999!
2000!--                         3D-smearing of the forces with a polynomial function
2001!--                         (much faster than the old Gaussian function), using
2002!--                         some parameters that have been calculated in user_init.
2003!--                         The function is only similar to Gaussian function for
2004!--                         squared distances <= eps_min2:
2005                            IF ( dist_u_3d <= eps_min2 ) THEN
2006                               thrust(k,j,i) = thrust(k,j,i) +                     &
2007                                               thrust_ring(ring,rseg) *            &
2008                                               ( ( pol_a * dist_u_3d - pol_b ) *   & 
2009                                                dist_u_3d + 1.0_wp ) * eps_factor *&
2010                                                                       flag
2011                            ENDIF
2012                            IF ( dist_v_3d <= eps_min2 ) THEN
2013                               torque_y(k,j,i) = torque_y(k,j,i) +                    &
2014                                                 torque_ring_y(ring,rseg) *           &
2015                                                 ( ( pol_a * dist_v_3d - pol_b ) *    &
2016                                                  dist_v_3d + 1.0_wp ) * eps_factor * &
2017                                                                         flag
2018                            ENDIF
2019                            IF ( dist_w_3d <= eps_min2 ) THEN
2020                               torque_z(k,j,i) = torque_z(k,j,i) +                    &
2021                                                 torque_ring_z(ring,rseg) *           &
2022                                                 ( ( pol_a * dist_w_3d - pol_b ) *    &
2023                                                  dist_w_3d + 1.0_wp ) * eps_factor * &
2024                                                                         flag
2025                            ENDIF
2026
2027                         ENDDO  ! End of loop over rseg
2028                      ENDDO     ! End of loop over ring
2029             
2030!
2031!--                   Rotation of force components:
2032                      rot_tend_x(k,j,i) = rot_tend_x(k,j,i) + (                &
2033                                      thrust(k,j,i)*rotx(inot,1) +             &
2034                                      torque_y(k,j,i)*roty(inot,1) +           &
2035                                      torque_z(k,j,i)*rotz(inot,1)             &
2036                                                              ) * flag
2037                               
2038                      rot_tend_y(k,j,i) = rot_tend_y(k,j,i) + (                &
2039                                      thrust(k,j,i)*rotx(inot,2) +             &
2040                                      torque_y(k,j,i)*roty(inot,2) +           &
2041                                      torque_z(k,j,i)*rotz(inot,2)             &
2042                                                              ) * flag
2043                               
2044                      rot_tend_z(k,j,i) = rot_tend_z(k,j,i) + (                &
2045                                      thrust(k,j,i)*rotx(inot,3) +             &
2046                                      torque_y(k,j,i)*roty(inot,3) +           &
2047                                      torque_z(k,j,i)*rotz(inot,3)             &
2048                                                              ) * flag                   
2049
2050                   ENDDO        ! End of loop over k
2051                ENDDO           ! End of loop over j
2052             ENDDO              ! End of loop over i
2053
2054             CALL cpu_log( log_point_s(63), 'wtm_smearing', 'stop' )         
2055                   
2056          ENDDO                  !-- end of loop over turbines
2057
2058               
2059          IF ( yaw_control )  THEN
2060!
2061!--          Allocate arrays for yaw control at first call
2062!--          Can't be allocated before dt_3d is set
2063             IF ( start_up )  THEN
2064                WDLON = NINT( 30.0_wp / dt_3d )  ! 30s running mean array
2065                ALLOCATE( wd30(1:nturbines,1:WDLON) )
2066                wd30 = 999.0_wp                  ! Set to dummy value
2067                ALLOCATE( wd30_l(1:WDLON) )
2068               
2069                WDSHO = NINT( 2.0_wp / dt_3d )   ! 2s running mean array
2070                ALLOCATE( wd2(1:nturbines,1:WDSHO) )
2071                wd2 = 999.0_wp                   ! Set to dummy value
2072                ALLOCATE( wd2_l(1:WDSHO) )
2073                start_up = .FALSE.
2074             ENDIF         
2075
2076!
2077!--          Calculate the inflow wind speed
2078!--
2079!--          Loop over number of turbines:
2080             DO inot = 1, nturbines
2081!
2082!--             Find processor at i_hub, j_hub             
2083                IF ( ( nxl <= i_hub(inot) )  .AND.  ( nxr >= i_hub(inot) ) )   &
2084                   THEN
2085                   IF ( ( nys <= j_hub(inot) )  .AND.  ( nyn >= j_hub(inot) ) )&
2086                      THEN
2087
2088                      u_inflow_l(inot) = u(k_hub(inot),j_hub(inot),i_hub(inot))
2089
2090                      wdir_l(inot) = -1.0_wp * ATAN2(                          &
2091                         0.5_wp * ( v(k_hub(inot),j_hub(inot),i_hub(inot)+1) + &
2092                                    v(k_hub(inot),j_hub(inot),i_hub(inot)) ) , &
2093                         0.5_wp * ( u(k_hub(inot),j_hub(inot)+1,i_hub(inot)) + &
2094                                    u(k_hub(inot),j_hub(inot),i_hub(inot)) ) )
2095
2096                      CALL wtm_yawcontrol( inot )
2097
2098                      phi_yaw_l(inot) = phi_yaw(inot)
2099                     
2100                   ENDIF
2101                ENDIF
2102                   
2103             ENDDO                                 !-- end of loop over turbines
2104
2105!
2106!--          Transfer of information to the other cpus
2107#if defined( __parallel )         
2108             CALL MPI_ALLREDUCE( u_inflow_l, u_inflow, nturbines, MPI_REAL,    &
2109                                 MPI_SUM, comm2d, ierr )
2110             CALL MPI_ALLREDUCE( wdir_l, wdir, nturbines, MPI_REAL, MPI_SUM,   &
2111                                 comm2d, ierr )
2112             CALL MPI_ALLREDUCE( phi_yaw_l, phi_yaw, nturbines, MPI_REAL,      &
2113                                 MPI_SUM, comm2d, ierr )
2114#else
2115             u_inflow = u_inflow_l
2116             wdir     = wdir_l
2117             phi_yaw  = phi_yaw_l
2118             
2119             
2120#endif
2121             DO inot = 1, nturbines
2122!             
2123!--             Update rotor orientation               
2124                CALL wtm_rotate_rotor( inot )
2125
2126             ENDDO ! End of loop over turbines
2127                           
2128          END IF  ! end of yaw control
2129         
2130          IF ( speed_control )  THEN
2131!
2132!--          Transfer of information to the other cpus
2133!              CALL MPI_ALLREDUCE( omega_gen, omega_gen_old, nturbines,        &
2134!                                  MPI_REAL,MPI_SUM, comm2d, ierr )
2135#if defined( __parallel )   
2136             CALL MPI_ALLREDUCE( torque_gen, torque_gen_old, nturbines,        &
2137                                 MPI_REAL, MPI_SUM, comm2d, ierr )
2138             CALL MPI_ALLREDUCE( omega_rot_l, omega_rot, nturbines,            &
2139                                 MPI_REAL, MPI_SUM, comm2d, ierr )
2140             CALL MPI_ALLREDUCE( omega_gen_f, omega_gen_f_old, nturbines,      &
2141                                 MPI_REAL, MPI_SUM, comm2d, ierr )
2142#else
2143             torque_gen_old  = torque_gen
2144             omega_rot       = omega_rot_l
2145             omega_gen_f_old = omega_gen_f
2146#endif
2147           
2148          ENDIF
2149
2150          DO inot = 1, nturbines
2151
2152             IF ( myid == 0 ) THEN
2153                IF ( openfile_turb_mod(400+inot)%opened )  THEN
2154                   WRITE ( 400+inot, 106 ) simulated_time, omega_rot(inot),    &
2155                             omega_gen(inot), torque_gen_old(inot),            &
2156                             torque_total(inot), pitch_add(inot),              &
2157                             torque_gen_old(inot)*omega_gen(inot)*gen_eff,     &
2158                             torque_total(inot)*omega_rot(inot)*air_dens,      &
2159                             thrust_rotor(inot),                               & 
2160                             wdir(inot)*180.0_wp/pi,                           &
2161                             (phi_yaw(inot))*180.0_wp/pi                   
2162                             
2163                ELSE
2164
2165                   WRITE ( turbine_id,'(I2.2)')  inot
2166                   OPEN ( 400+inot, FILE=( 'TURBINE_PARAMETERS'//turbine_id ), &
2167                                            FORM='FORMATTED' )
2168                   WRITE ( 400+inot, 105 ) inot
2169                   WRITE ( 400+inot, 106 ) simulated_time, omega_rot(inot),    &
2170                             omega_gen(inot), torque_gen_old(inot),            &
2171                             torque_total(inot), pitch_add(inot),              &
2172                             torque_gen_old(inot)*omega_gen(inot)*gen_eff,     &
2173                             torque_total(inot)*omega_rot(inot)*air_dens,      &
2174                             thrust_rotor(inot),                               & 
2175                             wdir(inot)*180.0_wp/pi,                           &                   
2176                             (phi_yaw(inot))*180.0_wp/pi
2177                ENDIF
2178             ENDIF
2179
2180!--          Set open flag
2181             openfile_turb_mod(400+inot)%opened = .TRUE.
2182          ENDDO                                    !-- end of loop over turbines
2183
2184       ENDIF
2185
2186       CALL cpu_log( log_point_s(61), 'wtm_forces', 'stop' )
2187       
2188!
2189!--    Formats
2190       105 FORMAT ('Turbine control data for turbine ',I2,1X,':'/ &
2191              &'----------------------------------------'/ &
2192              &'   Time   RSpeed  GSpeed  ', &
2193               'GenTorque  AeroTorque  Pitch  Power(Gen)  Power(Rot)  ',       &
2194               'RotThrust  WDirection  YawOrient')
2195
2196       106 FORMAT (F9.3,2X,F7.3,2X,F7.2,2X,F9.1,3X,F9.1,1X,F6.2,2X,F10.1,2X,   &
2197                   F10.1,1X,F9.1,2X,F7.2,1X,F7.2)
2198
2199
2200    END SUBROUTINE wtm_forces
2201
2202   
2203!------------------------------------------------------------------------------!
2204! Description:
2205! ------------
2206!> Yaw controller for the wind turbine model
2207!------------------------------------------------------------------------------!
2208    SUBROUTINE wtm_yawcontrol( inot )
2209   
2210       USE constants
2211       USE kinds
2212               
2213       IMPLICIT NONE
2214     
2215       INTEGER(iwp)             :: inot
2216       INTEGER(iwp)             :: i_wd_30
2217       REAL(wp)                 :: missal
2218
2219       i_wd_30 = 0_iwp
2220
2221
2222!--    The yaw controller computes a 30s running mean of the wind direction.
2223!--    If the difference between turbine alignment and wind direction exceeds
2224!--    5°, the turbine is yawed. The mechanism stops as soon as the 2s-running
2225!--    mean of the missalignment is smaller than 0.5°.
2226!--    Attention: If the timestep during the simulation changes significantly
2227!--    the lengths of the running means change and it does not correspond to
2228!--    30s/2s anymore.
2229!--    ! Needs to be modified for these situations !
2230!--    For wind from the east, the averaging of the wind direction could cause
2231!--    problems and the yaw controller is probably flawed. -> Routine for
2232!--    averaging needs to be improved!
2233!
2234!--    Check if turbine is not yawing
2235       IF ( .NOT. doyaw(inot) )  THEN
2236!
2237!--       Write current wind direction into array
2238          wd30_l    = wd30(inot,:)
2239          wd30_l    = CSHIFT( wd30_l, SHIFT=-1 )
2240          wd30_l(1) = wdir(inot)
2241!
2242!--       Check if array is full ( no more dummies )
2243          IF ( .NOT. ANY( wd30_l == 999.) ) THEN
2244
2245             missal = SUM( wd30_l ) / SIZE( wd30_l ) - phi_yaw(inot)
2246!
2247!--          Check if turbine is missaligned by more than max_miss
2248             IF ( ABS( missal ) > max_miss )  THEN
2249!
2250!--             Check in which direction to yaw         
2251                yawdir(inot) = SIGN( 1.0_wp, missal )
2252!
2253!--             Start yawing of turbine
2254                phi_yaw(inot) = phi_yaw(inot) + yawdir(inot) * yaw_speed * dt_3d
2255                doyaw(inot) = .TRUE.
2256                wd30_l = 999.  ! fill with dummies again
2257             ENDIF
2258          ENDIF
2259         
2260          wd30(inot,:) = wd30_l
2261
2262!     
2263!--    If turbine is already yawing:
2264!--    Initialize 2 s running mean and yaw until the missalignment is smaller
2265!--    than min_miss
2266
2267       ELSE
2268!
2269!--       Initialize 2 s running mean
2270          wd2_l = wd2(inot,:)
2271          wd2_l = CSHIFT( wd2_l, SHIFT = -1 )
2272          wd2_l(1) = wdir(inot)
2273!     
2274!--       Check if array is full ( no more dummies )
2275          IF ( .NOT. ANY( wd2_l == 999.0_wp ) ) THEN
2276!
2277!--          Calculate missalignment of turbine       
2278             missal = SUM( wd2_l - phi_yaw(inot) ) / SIZE( wd2_l )
2279!
2280!--          Check if missalignment is still larger than 0.5 degree and if the
2281!--          yaw direction is still right
2282             IF ( ( ABS( missal ) > min_miss )  .AND.                          &
2283                  ( yawdir(inot) == SIGN( 1.0_wp, missal ) ) )  THEN
2284!
2285!--             Continue yawing       
2286                phi_yaw(inot) = phi_yaw(inot) + yawdir(inot) * yaw_speed * dt_3d
2287             ELSE
2288!
2289!--             Stop yawing       
2290                doyaw(inot) = .FALSE.
2291                wd2_l = 999.0_wp ! fill with dummies again
2292             ENDIF
2293          ELSE
2294!
2295!--          Continue yawing
2296             phi_yaw(inot) = phi_yaw(inot) + yawdir(inot) * yaw_speed * dt_3d
2297          ENDIF
2298     
2299          wd2(inot,:) = wd2_l
2300           
2301       ENDIF
2302     
2303    END SUBROUTINE wtm_yawcontrol 
2304
2305
2306!------------------------------------------------------------------------------!
2307! Description:
2308! ------------
2309!> Initialization of the speed control
2310!------------------------------------------------------------------------------!
2311    SUBROUTINE wtm_init_speed_control
2312
2313
2314       IMPLICIT NONE
2315
2316!
2317!--    If speed control is set, remaining variables and control_parameters for
2318!--    the control algorithm are calculated
2319!
2320!--    Calculate slope constant for region 15
2321       slope15   = ( slope2 * min_reg2 * min_reg2 ) / ( min_reg2 - min_reg15 )
2322!
2323!--    Calculate upper limit of slipage region
2324       vs_sysp   = rated_genspeed / 1.1_wp
2325!
2326!--    Calculate slope of slipage region
2327       slope25   = ( rated_power / rated_genspeed ) /                          &
2328                   ( rated_genspeed - vs_sysp )
2329!
2330!--    Calculate lower limit of slipage region
2331       min_reg25 = ( slope25 - SQRT( slope25 * ( slope25 - 4.0_wp *            &
2332                                                 slope2 * vs_sysp ) ) ) /      &
2333                   ( 2.0_wp * slope2 )
2334!
2335!--    Frequency for the simple low pass filter
2336       Fcorner   = 0.25_wp
2337!
2338!--    At the first timestep the torque is set to its maximum to prevent
2339!--    an overspeeding of the rotor
2340       torque_gen_old(:) = max_torque_gen 
2341     
2342    END SUBROUTINE wtm_init_speed_control
2343
2344
2345!------------------------------------------------------------------------------!
2346! Description:
2347! ------------
2348!> Simple controller for the regulation of the rotor speed
2349!------------------------------------------------------------------------------!
2350    SUBROUTINE wtm_speed_control( inot )
2351
2352
2353       IMPLICIT NONE
2354
2355       INTEGER(iwp)             :: inot
2356       
2357         
2358
2359!
2360!--    The controller is based on the fortran script from Jonkman
2361!--    et al. 2009 "Definition of a 5 MW Reference Wind Turbine for
2362!--    offshore system developement"
2363
2364!
2365!--    The generator speed is filtered by a low pass filter
2366!--    for the control of the generator torque       
2367       lp_coeff = EXP( -2.0_wp * 3.14_wp * dt_3d * Fcorner )
2368       omega_gen_f(inot) = ( 1.0_wp - lp_coeff ) * omega_gen(inot) + lp_coeff *&
2369                           omega_gen_f_old(inot)
2370
2371       IF ( omega_gen_f(inot) <= min_reg15 )  THEN
2372!                       
2373!--       Region 1: Generator torque is set to zero to accelerate the rotor:
2374          torque_gen(inot) = 0
2375       
2376       ELSEIF ( omega_gen_f(inot) <= min_reg2 )  THEN
2377!                       
2378!--       Region 1.5: Generator torque is increasing linearly with rotor speed:
2379          torque_gen(inot) = slope15 * ( omega_gen_f(inot) - min_reg15 )
2380                         
2381       ELSEIF ( omega_gen_f(inot) <= min_reg25 )  THEN
2382!
2383!--       Region 2: Generator torque is increased by the square of the generator
2384!--                 speed to keep the TSR optimal:
2385          torque_gen(inot) = slope2 * omega_gen_f(inot) * omega_gen_f(inot)
2386       
2387       ELSEIF ( omega_gen_f(inot) < rated_genspeed )  THEN
2388!                       
2389!--       Region 2.5: Slipage region between 2 and 3:
2390          torque_gen(inot) = slope25 * ( omega_gen_f(inot) - vs_sysp )
2391       
2392       ELSE
2393!                       
2394!--       Region 3: Generator torque is antiproportional to the rotor speed to
2395!--                 keep the power constant:
2396          torque_gen(inot) = rated_power / omega_gen_f(inot)
2397       
2398       ENDIF
2399!                       
2400!--    Calculate torque rate and confine with a max
2401       trq_rate = ( torque_gen(inot) - torque_gen_old(inot) ) / dt_3d
2402       trq_rate = MIN( MAX( trq_rate, -1.0_wp * max_trq_rate ), max_trq_rate )
2403!                       
2404!--    Calculate new gen torque and confine with max torque                         
2405       torque_gen(inot) = torque_gen_old(inot) + trq_rate * dt_3d
2406       torque_gen(inot) = MIN( torque_gen(inot), max_torque_gen )                                             
2407!
2408!--    Overwrite values for next timestep                       
2409       omega_rot_l(inot) = omega_gen(inot) / gear_ratio
2410
2411   
2412    END SUBROUTINE wtm_speed_control   
2413
2414
2415!------------------------------------------------------------------------------!
2416! Description:
2417! ------------
2418!> Application of the additional forces generated by the wind turbine on the
2419!> flow components (tendency terms)
2420!> Call for all grid points
2421!------------------------------------------------------------------------------!
2422    SUBROUTINE wtm_tendencies( component )
2423
2424   
2425       IMPLICIT NONE
2426
2427       INTEGER(iwp) ::  component   !< prognostic variable (u,v,w)
2428       INTEGER(iwp) ::  i           !< running index
2429       INTEGER(iwp) ::  j           !< running index
2430       INTEGER(iwp) ::  k           !< running index
2431
2432
2433       SELECT CASE ( component )
2434
2435       CASE ( 1 )
2436!
2437!--       Apply the x-component of the force to the u-component of the flow:
2438          IF ( simulated_time >= time_turbine_on )  THEN
2439             DO  i = nxlg, nxrg
2440                DO  j = nysg, nyng
2441                   DO  k = nzb+1, k_hub(1) + k_smear(1)
2442!
2443!--                   Calculate the thrust generated by the nacelle and the tower
2444                      tend_nac_x = 0.5_wp * nac_cd_surf(k,j,i) *               &
2445                                         SIGN( u(k,j,i)**2 , u(k,j,i) )     
2446                      tend_tow_x   = 0.5_wp * tow_cd_surf(k,j,i) *             &
2447                                         SIGN( u(k,j,i)**2 , u(k,j,i) ) 
2448                                                   
2449                      tend(k,j,i) = tend(k,j,i) + ( - rot_tend_x(k,j,i)        &
2450                                  - tend_nac_x - tend_tow_x )                  &
2451                                      * MERGE( 1.0_wp, 0.0_wp,                 &
2452                                               BTEST( wall_flags_0(k,j,i), 1 ) )
2453                   ENDDO
2454                ENDDO
2455             ENDDO
2456          ENDIF
2457
2458       CASE ( 2 )
2459!
2460!--       Apply the y-component of the force to the v-component of the flow:
2461          IF ( simulated_time >= time_turbine_on )  THEN
2462             DO  i = nxlg, nxrg
2463                DO  j = nysg, nyng
2464                   DO  k = nzb+1, k_hub(1) + k_smear(1)
2465                      tend_nac_y = 0.5_wp * nac_cd_surf(k,j,i) *               &
2466                                         SIGN( v(k,j,i)**2 , v(k,j,i) )     
2467                      tend_tow_y   = 0.5_wp * tow_cd_surf(k,j,i) *             &
2468                                         SIGN( v(k,j,i)**2 , v(k,j,i) )                     
2469                      tend(k,j,i) = tend(k,j,i) + ( - rot_tend_y(k,j,i)        &
2470                                  - tend_nac_y - tend_tow_y )                  &
2471                                      * MERGE( 1.0_wp, 0.0_wp,                 &
2472                                               BTEST( wall_flags_0(k,j,i), 2 ) )
2473                   ENDDO
2474                ENDDO
2475             ENDDO
2476          ENDIF
2477
2478       CASE ( 3 )
2479!
2480!--       Apply the z-component of the force to the w-component of the flow:
2481          IF ( simulated_time >= time_turbine_on )  THEN
2482             DO  i = nxlg, nxrg
2483                DO  j = nysg, nyng
2484                   DO  k = nzb+1,  k_hub(1) + k_smear(1)
2485                      tend(k,j,i) = tend(k,j,i) - rot_tend_z(k,j,i)            &
2486                                      * MERGE( 1.0_wp, 0.0_wp,                 &
2487                                               BTEST( wall_flags_0(k,j,i), 3 ) )
2488                   ENDDO
2489                ENDDO
2490             ENDDO
2491          ENDIF
2492
2493
2494       CASE DEFAULT
2495
2496          WRITE( message_string, * ) 'unknown prognostic variable: ', component
2497          CALL message( 'wtm_tendencies', 'PA04??', 1, 2, 0, 6, 0 ) 
2498
2499       END SELECT
2500
2501
2502    END SUBROUTINE wtm_tendencies
2503
2504
2505!------------------------------------------------------------------------------!
2506! Description:
2507! ------------
2508!> Application of the additional forces generated by the wind turbine on the
2509!> flow components (tendency terms)
2510!> Call for grid point i,j
2511!------------------------------------------------------------------------------!
2512    SUBROUTINE wtm_tendencies_ij( i, j, component )
2513
2514
2515       IMPLICIT NONE
2516
2517       INTEGER(iwp) ::  component   !< prognostic variable (u,v,w)
2518       INTEGER(iwp) ::  i           !< running index
2519       INTEGER(iwp) ::  j           !< running index
2520       INTEGER(iwp) ::  k           !< running index
2521
2522       SELECT CASE ( component )
2523
2524       CASE ( 1 )
2525!
2526!--       Apply the x-component of the force to the u-component of the flow:
2527          IF ( simulated_time >= time_turbine_on )  THEN
2528
2529             DO  k = nzb+1,  k_hub(1) + k_smear(1)
2530!
2531!--             Calculate the thrust generated by the nacelle and the tower
2532                tend_nac_x = 0.5_wp * nac_cd_surf(k,j,i) *                     &
2533                                   SIGN( u(k,j,i)**2 , u(k,j,i) )     
2534                tend_tow_x   = 0.5_wp * tow_cd_surf(k,j,i) *                   &
2535                                   SIGN( u(k,j,i)**2 , u(k,j,i) ) 
2536                tend(k,j,i) = tend(k,j,i) + ( - rot_tend_x(k,j,i)              &
2537                            - tend_nac_x - tend_tow_x )                        &
2538                                      * MERGE( 1.0_wp, 0.0_wp,                 &
2539                                               BTEST( wall_flags_0(k,j,i), 1 ) )
2540             ENDDO
2541          ENDIF
2542
2543       CASE ( 2 )
2544!
2545!--       Apply the y-component of the force to the v-component of the flow:
2546          IF ( simulated_time >= time_turbine_on )  THEN
2547             DO  k = nzb+1,  k_hub(1) + k_smear(1)
2548                tend_nac_y = 0.5_wp * nac_cd_surf(k,j,i) *                     &
2549                                   SIGN( v(k,j,i)**2 , v(k,j,i) )     
2550                tend_tow_y   = 0.5_wp * tow_cd_surf(k,j,i) *                   &
2551                                   SIGN( v(k,j,i)**2 , v(k,j,i) )                     
2552                tend(k,j,i) = tend(k,j,i) + ( - rot_tend_y(k,j,i)              &
2553                            - tend_nac_y - tend_tow_y )                        &
2554                                      * MERGE( 1.0_wp, 0.0_wp,                 &
2555                                               BTEST( wall_flags_0(k,j,i), 2 ) )
2556             ENDDO
2557          ENDIF
2558
2559       CASE ( 3 )
2560!
2561!--       Apply the z-component of the force to the w-component of the flow:
2562          IF ( simulated_time >= time_turbine_on )  THEN
2563             DO  k = nzb+1,  k_hub(1) + k_smear(1)
2564                tend(k,j,i) = tend(k,j,i) - rot_tend_z(k,j,i)                  &
2565                                      * MERGE( 1.0_wp, 0.0_wp,                 &
2566                                               BTEST( wall_flags_0(k,j,i), 3 ) )
2567             ENDDO
2568          ENDIF
2569
2570
2571       CASE DEFAULT
2572
2573          WRITE( message_string, * ) 'unknown prognostic variable: ', component
2574          CALL message( 'wtm_tendencies', 'PA04??', 1, 2, 0, 6, 0 ) 
2575
2576       END SELECT
2577
2578
2579    END SUBROUTINE wtm_tendencies_ij
2580
2581 END MODULE wind_turbine_model_mod
Note: See TracBrowser for help on using the repository browser.