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

Last change on this file since 2693 was 2669, checked in by raasch, 6 years ago

file attributes and activation strings in .palm.iofiles revised, file extensions for nesting, masked output, wind turbine data, etc. changed

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