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

Last change on this file since 2152 was 2152, checked in by lvollmer, 7 years ago

bugfix in wtm_read_blade_tables, Addition of a tip loss model

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