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

Last change on this file since 1957 was 1930, checked in by suehring, 8 years ago

last commit documented

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