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

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

last commit documented

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