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

Last change on this file since 2776 was 2776, checked in by Giersch, 6 years ago

Skipping of module related restart data changed + adapting synthetic turbulence generator to current restart procedure

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