Skip to content

Unexpected behavior of function sort_gm in module_wind_mav #2234

@sgremmo

Description

@sgremmo

Describe the bug
In the current implementation of sort_gm, the tbindx array is declared as an OUT parameter. However, its contents are copied to a local array tbindx_cp without prior initialization. As a result, the values copied are/can be effectively random, leading to unpredictable behavior. From testing, this behavior is highly dependent on factors such as the compiler, compilation flags, and the size of the array,etc.,

The tbindx array is intended to store turbine IDs. Without proper initialization and validation, the values in tbindx_cp can exceed the valid range of turbine IDs. This can lead to segmentation faults or other unpredictable behavior later in the execution when these invalid IDs are accessed.

To Reproduce
Steps to reproduce the behavior:

  1. WRF version: 4.6.1 (commit d66e442)
  2. Use compiler and version: gfortran 11.4.0
  3. Use namelist options
    windfarm_opt = 0 , 0 , 2 ,
    windfarm_ij = 2,
    windfarm_wake_model = 0, 0, 3 (or any wake model including GM: for example 5)

Expected behavior
Expected behavior is tbindx to be an INPUT and OUTPUT parameter.

Proposed solution
Change its intent to inout. Could you confirm that this is the expected behavior?

Proposed Patch
diff --git a/phys/module_wind_mav.F b/phys/module_wind_mav.F
index 30225e00..43458328 100644
--- a/phys/module_wind_mav.F
+++ b/phys/module_wind_mav.F
@@ -996,7 +996,7 @@ MODULE module_wind_mav
subroutine sort_gm(nturb, tbindx, ax_dist)
implicit none
integer, intent(in) :: nturb
- integer, intent(out), dimension(nturb) :: tbindx
+ integer, intent(inout), dimension(nturb) :: tbindx
real, intent(inout), dimension(nturb) :: ax_dist
real, dimension(nturb) :: xloc
integer :: i, a(1)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions