Skip to content

Commit

Permalink
Fix some segfaults when using a controller shared library.
Browse files Browse the repository at this point in the history
  • Loading branch information
deslaughter committed Jan 7, 2025
1 parent 47264e3 commit 8712da3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 39 deletions.
16 changes: 12 additions & 4 deletions modules/nwtc-library/src/ModVar.f90
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ subroutine MV_AddModule(ModDataAry, ModID, ModAbbr, Instance, ModDT, SolverDT, V
character(*), parameter :: RoutineName = 'MV_AddModule'
integer(IntKi) :: ErrStat2
character(ErrMsgLen) :: ErrMsg2
type(ModDataType), allocatable :: ModDataAryTmp(:)
type(ModDataType) :: ModData
integer(IntKi) :: i, StartIndex

Expand Down Expand Up @@ -425,9 +426,12 @@ subroutine MV_AddModule(ModDataAry, ModID, ModAbbr, Instance, ModDT, SolverDT, V
!----------------------------------------------------------------------------

if (.not. allocated(ModDataAry)) then
ModDataAry = [ModData]
allocate(ModDataAry(1), source=ModData)
else
ModDataAry = [ModDataAry, ModData]
call move_alloc(ModDataAry, ModDataAryTmp)
allocate(ModDataAry(size(ModDataAryTmp) + 1))
ModDataAry(:size(ModDataAryTmp)) = ModDataAryTmp
ModDataAry(size(ModDataAry)) = ModData
end if

contains
Expand Down Expand Up @@ -888,6 +892,7 @@ subroutine MV_AddVar(VarAry, Name, Field, DL, Num, iAry, jAry, kAry, Flags, Deri
logical, optional, intent(in) :: Active
integer(IntKi) :: i
type(ModVarType) :: Var
type(ModVarType), allocatable :: VarAryTmp(:)

! If active argument specified and not active, return
if (present(Active)) then
Expand Down Expand Up @@ -940,9 +945,12 @@ subroutine MV_AddVar(VarAry, Name, Field, DL, Num, iAry, jAry, kAry, Flags, Deri

! Append Var to VarArray
if (allocated(VarAry)) then
VarAry = [VarAry, Var]
call move_alloc(VarAry, VarAryTmp)
allocate(VarAry(size(VarAryTmp) + 1))
VarAry(:size(VarAryTmp)) = VarAryTmp
VarAry(size(VarAry)) = Var
else
VarAry = [Var]
allocate(VarAry(1), source=Var)
end if

end subroutine
Expand Down
88 changes: 53 additions & 35 deletions modules/openfast-library/src/FAST_Mapping.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2012,14 +2012,14 @@ logical function Failed()

subroutine MapLoadMesh(Turbine, Mappings, SrcMod, SrcDL, SrcDispDL, &
DstMod, DstDL, DstDispDL, ErrStat, ErrMsg, Active)
type(FAST_TurbineType), target :: Turbine
type(MappingType), allocatable :: Mappings(:)
type(ModDataType), intent(inout) :: SrcMod, DstMod
type(DatLoc), intent(in) :: SrcDL, DstDL
type(DatLoc), intent(in) :: SrcDispDL, DstDispDL
integer(IntKi), intent(out) :: ErrStat
character(*), intent(out) :: ErrMsg
logical, optional, intent(in) :: Active
type(FAST_TurbineType), target :: Turbine
type(MappingType), allocatable, intent(inout) :: Mappings(:)
type(ModDataType), intent(inout) :: SrcMod, DstMod
type(DatLoc), intent(in) :: SrcDL, DstDL
type(DatLoc), intent(in) :: SrcDispDL, DstDispDL
integer(IntKi), intent(out) :: ErrStat
character(*), intent(out) :: ErrMsg
logical, optional, intent(in) :: Active

character(*), parameter :: RoutineName = 'MapLoadMesh'
integer(IntKi) :: ErrStat2
Expand Down Expand Up @@ -2128,7 +2128,7 @@ subroutine MapLoadMesh(Turbine, Mappings, SrcMod, SrcDL, SrcDispDL, &
end if

! Add mapping to array of mappings
Mappings = [Mappings, Mapping]
call AppendMapping(Mappings, Mapping)

contains
logical function Failed()
Expand Down Expand Up @@ -2158,13 +2158,13 @@ pure logical function IsSiblingMesh(MeshA, MeshB)
end subroutine

subroutine MapMotionMesh(Turbine, Mappings, SrcMod, SrcDL, DstMod, DstDL, ErrStat, ErrMsg, Active)
type(FAST_TurbineType), target :: Turbine
type(MappingType), allocatable :: Mappings(:)
type(ModDataType), intent(inout) :: SrcMod, DstMod
type(DatLoc), intent(in) :: SrcDL, DstDL
integer(IntKi), intent(out) :: ErrStat
character(*), intent(out) :: ErrMsg
logical, optional, intent(in) :: Active
type(FAST_TurbineType), target :: Turbine
type(MappingType), allocatable, intent(inout) :: Mappings(:)
type(ModDataType), intent(inout) :: SrcMod, DstMod
type(DatLoc), intent(in) :: SrcDL, DstDL
integer(IntKi), intent(out) :: ErrStat
character(*), intent(out) :: ErrMsg
logical, optional, intent(in) :: Active

character(*), parameter :: RoutineName = 'MapMotionMesh'
integer(IntKi) :: ErrStat2
Expand Down Expand Up @@ -2221,7 +2221,7 @@ subroutine MapMotionMesh(Turbine, Mappings, SrcMod, SrcDL, DstMod, DstDL, ErrSta
call MeshMapCreate(SrcMesh, DstMesh, Mapping%MeshMap, ErrStat2, ErrMsg2); if (Failed()) return

! Add mapping to array of mappings
Mappings = [Mappings, Mapping]
call AppendMapping(Mappings, Mapping)

contains
logical function Failed()
Expand All @@ -2230,15 +2230,15 @@ logical function Failed()
end function
end subroutine

subroutine MapVariable(Maps, SrcMod, SrcDL, DstMod, DstDL, ErrStat, ErrMsg, Active)
type(MappingType), allocatable :: Maps(:)
type(ModDataType), intent(inout) :: SrcMod, DstMod
type(DatLoc), intent(in) :: SrcDL, DstDL
integer(IntKi), intent(out) :: ErrStat
character(*), intent(out) :: ErrMsg
logical, optional, intent(in) :: Active
type(MappingType) :: Mapping
integer(IntKi) :: iVarSrc, iVarDst
subroutine MapVariable(Mappings, SrcMod, SrcDL, DstMod, DstDL, ErrStat, ErrMsg, Active)
type(MappingType), allocatable, intent(inout) :: Mappings(:)
type(ModDataType), intent(inout) :: SrcMod, DstMod
type(DatLoc), intent(in) :: SrcDL, DstDL
integer(IntKi), intent(out) :: ErrStat
character(*), intent(out) :: ErrMsg
logical, optional, intent(in) :: Active
type(MappingType) :: Mapping
integer(IntKi) :: iVarSrc, iVarDst

ErrStat = ErrID_None
ErrMsg = ''
Expand Down Expand Up @@ -2298,18 +2298,19 @@ subroutine MapVariable(Maps, SrcMod, SrcDL, DstMod, DstDL, ErrStat, ErrMsg, Acti
! Allocate variable data storage
call AllocAry(Mapping%VarData, max(Mapping%SrcVar%Num, Mapping%DstVar%Num), "VarData", ErrStat, ErrMsg)

Maps = [Maps, Mapping]
! Add mapping to array of mappings
call AppendMapping(Mappings, Mapping)
end subroutine

!> MapCustom creates a custom mapping that is not included in linearization.
!! Each custom mapping needs an entry in FAST_InputSolve to actually perform the transfer.
subroutine MapCustom(Maps, Desc, SrcMod, DstMod, i, Active)
type(MappingType), allocatable :: Maps(:)
character(*), intent(in) :: Desc
type(ModDataType), intent(inout) :: SrcMod, DstMod
integer(IntKi), optional, intent(in) :: i
logical, optional, intent(in) :: Active
type(MappingType) :: Mapping
subroutine MapCustom(Mappings, Desc, SrcMod, DstMod, i, Active)
type(MappingType), allocatable, intent(inout) :: Mappings(:)
character(*), intent(in) :: Desc
type(ModDataType), intent(inout) :: SrcMod, DstMod
integer(IntKi), optional, intent(in) :: i
logical, optional, intent(in) :: Active
type(MappingType) :: Mapping

if (present(Active)) then
if (.not. Active) return
Expand All @@ -2326,7 +2327,24 @@ subroutine MapCustom(Maps, Desc, SrcMod, DstMod, i, Active)
Mapping%DstIns = DstMod%Ins
if (present(i)) Mapping%i = i

Maps = [Maps, Mapping]
! Add mapping to array of mappings
call AppendMapping(Mappings, Mapping)
end subroutine

! Append mapping to array of mappings
subroutine AppendMapping(Mappings, Mapping)
type(MappingType), allocatable, intent(inout) :: Mappings(:)
type(MappingType), intent(in) :: Mapping
type(MappingType), allocatable :: MappingsTmp(:)

if (allocated(Mappings)) then
call move_alloc(Mappings, MappingsTmp)
allocate(Mappings(size(MappingsTmp) + 1))
Mappings(:size(MappingsTmp)) = MappingsTmp
Mappings(size(Mappings)) = Mapping
else
allocate(Mappings(1), source=Mapping)
end if
end subroutine

subroutine SetMapVarFlags(Mapping, SrcMod, DstMod)
Expand Down

0 comments on commit 8712da3

Please sign in to comment.