Skip to content

Commit

Permalink
Merge pull request #29 from sandialabs/b/garbage_collection
Browse files Browse the repository at this point in the history
Smarter Garbage Collection
  • Loading branch information
kevmoor authored Nov 12, 2024
2 parents 1667ad8 + 53daa46 commit fa387c5
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/Manifest.toml
*/Manifest.toml
*.DS_Store
*.out
*.outb
Expand Down
15 changes: 15 additions & 0 deletions src/OWENSOpenFASTWrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ include("./hydrodyn.jl")
include("./inflowwind.jl")
include("./moordyn.jl")

function endAll()
if adi_active
adiEnd()
end
if hd_active
HD_End()
end
if ifw_active
ifwend()
end
if md_active
MD_End()
end
end

end
7 changes: 5 additions & 2 deletions src/aerodyn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,11 @@ function adiEnd()
adi_err.error_message)
catch
@error "AeroDyn-Inflow did not end properly"
endAll()
end

Libdl.dlclose(adilib) # Close the library explicitly.
adi_check_error()
end
end

Expand All @@ -574,8 +576,8 @@ function adi_check_error()
@error("Error status " * string(adi_err.error_status[1]) * ": " * string(adi_err.error_message))
adi_err.error_status = [0] # reset error status/message
adi_err.error_message = string(repeat(" ", 1025))
adiEnd()
error("AeroDyn-Inflow terminated prematurely.")
endAll()
error("AeroDyn-Inflow terminated prematurely, terminating all active OpenFAST libraries.")
end
end

Expand Down Expand Up @@ -1183,6 +1185,7 @@ End ADI and clear data
function endTurb()
adiEnd()
adi_initialized = false
Libdl.dlclose(adilib)
end

# Outstanding questions
Expand Down
6 changes: 3 additions & 3 deletions src/hydrodyn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ function HD_End()
hd_err.error_message)

Libdl.dlclose(hdlib) # Close the library explicitly.

hd_check_error()
end
end

Expand All @@ -509,7 +509,7 @@ function hd_check_error()
@warn("Error status " * string(hd_err.error_status[1]) * ": " * string(hd_err.error_message))
hd_err.error_status = [0] # reset error status/message
hd_err.error_message = string(repeat(" ", 1025))
HD_End()
error("HydroDyn terminated prematurely.")
endAll()
error("HydroDyn terminated prematurely, terminating all active OpenFAST libraries.")
end
end
69 changes: 41 additions & 28 deletions src/inflowwind.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ global sym_calcoutput
global sym_end
global backup_Vx

mutable struct IFW_Error
error_status
error_message
end

ifw_active = true


Expand Down Expand Up @@ -154,9 +159,6 @@ function ifwinit(;inflowlib_filename=libifw_c_binding,HWindSpeed=10.125,turbsim_
numChannels = [0]
channel_names = string(repeat(" ", 20 * 4000))
channel_units = string(repeat(" ", 20 * 4000))
error_status = [0]
error_message = string(repeat(" ", 1025))


# try
# TODO: No need to explicitly dlopen the library and look up the symbols. It is more
Expand All @@ -166,6 +168,8 @@ function ifwinit(;inflowlib_filename=libifw_c_binding,HWindSpeed=10.125,turbsim_
sym_init = Libdl.dlsym(inflowlib, :IfW_C_Init) # Get a symbol for the function to call.
global sym_calcoutput = Libdl.dlsym(inflowlib, :IfW_C_CalcOutput) # Get a symbol for the function to call.
global sym_end = Libdl.dlsym(inflowlib, :IfW_C_End)
global ifw_err = IFW_Error([0], string(repeat(" ", 1025)))
global ifw_abort_error_level = 4

ccall(sym_init,Cint,
(Ptr{Ptr{Cchar}}, # IN: input_string_array
Expand All @@ -177,8 +181,8 @@ function ifwinit(;inflowlib_filename=libifw_c_binding,HWindSpeed=10.125,turbsim_
Ptr{Cint}, # OUT: numChannels
Cstring, # OUT: channel_names
Cstring, # OUT: channel_units
Ptr{Cint}, # OUT: error_status
Cstring), # OUT: error_message
Ptr{Cint}, # OUT: ifw_err.error_status
Cstring), # OUT: ifw_err.error_message
[input_string], #InputFileStrings_C
input_string_length,
[uniform_string], #InputUniformStrings_C
Expand All @@ -188,12 +192,10 @@ function ifwinit(;inflowlib_filename=libifw_c_binding,HWindSpeed=10.125,turbsim_
numChannels,#NumChannels_C
channel_names, #OutputChannelNames_C
channel_units, #OutputChannelUnits_C
error_status, #ErrStat_C
error_message) #ErrMsg_C
if error_status[1] != 0
@warn "Inflow Wind Init Error"
println(error_message)
end
ifw_err.error_status, #ErrStat_C
ifw_err.error_message) #ErrMsg_C

ifw_check_error()
# catch
# global backup_Vx = HWindSpeed
# @warn "inflow wind library is not configured properly, using a steady wind profile [$backup_Vx,0.0,0.0] for Vx, Vy, Vz"
Expand Down Expand Up @@ -221,8 +223,8 @@ function ifwcalcoutput(position,time)
outputChannelValues = zeros(3)

#Reset error message
error_message = string(repeat(" ", 1025))
error_status = [0]
ifw_err.error_message = string(repeat(" ", 1025))
ifw_err.error_status = [0]
velocities = zeros(Float32,3) # output velocities (N x 3)

if ifw_active
Expand All @@ -238,13 +240,10 @@ function ifwcalcoutput(position,time)
Float32.(position), # IN: positions - specified by user
velocities, # OUT: velocities at desired positions
outputChannelValues, # OUT: output channel values as described in input file
error_status, # ErrStat_C
error_message) # ErrMsg_C
ifw_err.error_status, # ErrStat_C
ifw_err.error_message) # ErrMsg_C

if error_status[1] != 0
@warn "Inflow Wind Calcoutput Error"
println(error_message)
end
ifw_check_error()

return velocities
else
Expand All @@ -253,29 +252,43 @@ function ifwcalcoutput(position,time)
end
end

function ifw_check_error()
if ifw_err.error_status[1] == 0
ifw_err.error_status = [0] # reset error status/message
ifw_err.error_message = string(repeat(" ", 1025))
elseif ifw_err.error_status[1] < ifw_abort_error_level
@warn("Error status " * string(ifw_err.error_status[1]) * ": " * string(ifw_err.error_message))
ifw_err.error_status = [0] # reset error status/message
ifw_err.error_message = string(repeat(" ", 1025))
else
@warn("Error status " * string(ifw_err.error_status[1]) * ": " * string(ifw_err.error_message))
ifw_err.error_status = [0] # reset error status/message
ifw_err.error_message = string(repeat(" ", 1025))
endAll()
error("MoorDyn terminated prematurely, terminating all active OpenFAST libraries.")
end
end

"""
ifwend()
calls inflow wind end function and cleanup
"""
function ifwend()
#Reset error message
error_message = string(repeat(" ", 1025))
error_status = [0]
ifw_err.error_message = string(repeat(" ", 1025))
ifw_err.error_status = [0]

if ifw_active
ccall( sym_end,Cint,
(Ptr{Cint}, #ErrStat_C
Cstring), #ErrMsg_C
error_status,
error_message)

if error_status[1] != 0
@warn "Inflow Wind End Error"
println(error_message)
end
ifw_err.error_status,
ifw_err.error_message)

Libdl.dlclose(inflowlib) # Close the library explicitly.

ifw_check_error()

end
end
6 changes: 3 additions & 3 deletions src/moordyn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function MD_End()
md_err.error_message)

Libdl.dlclose(mdlib) # Close the library explicitly.

md_check_error()
end
end

Expand All @@ -212,7 +212,7 @@ function md_check_error()
@warn("Error status " * string(md_err.error_status[1]) * ": " * string(md_err.error_message))
md_err.error_status = [0] # reset error status/message
md_err.error_message = string(repeat(" ", 1025))
MD_End()
error("MoorDyn terminated prematurely.")
endAll()
error("MoorDyn terminated prematurely, terminating all active OpenFAST libraries.")
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ end

@testset "AeroDyn" begin
include("aerodyn_run.jl")
include("aerodyn_run.jl") #Test running twice
end

0 comments on commit fa387c5

Please sign in to comment.