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

Last change on this file since 2000 was 2000, checked in by knoop, 8 years ago

Forced header and separation lines into 80 columns

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