Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on invalid LAMMPS instance #62

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/LAMMPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include("api.jl")

export LMP, command, get_natoms, extract_atom, extract_compute, extract_global,
extract_setting, gather, gather_bonds, gather_angles, gather_dihedrals, gather_impropers,
scatter!, group_to_atom_ids, get_category_ids, extract_variable,
scatter!, group_to_atom_ids, get_category_ids, extract_variable, LAMMPSError,

# _LMP_DATATYPE
LAMMPS_NONE,
Expand Down Expand Up @@ -140,11 +140,24 @@ mutable struct LMP
end
end

if API.lammps_has_error(handle) != 0
buf = zeros(UInt8, 100)
API.lammps_get_last_error_message(handle, buf, length(buf))
msg = replace(rstrip(String(buf), '\0'), "ERROR: " => "")
throw(LAMMPSError(msg))
end

this = new(handle)
finalizer(close!, this)
return this
end
end

function Base.cconvert(::Type{Ptr{Cvoid}}, lmp::LMP)
lmp.handle == C_NULL && error("The LMP object doesn't point to a valid LAMMPS instance! "
* "This is usually caused by calling `LAMMPS.close!` or through serialization and deserialization.")
return lmp
end
Base.unsafe_convert(::Type{Ptr{Cvoid}}, lmp::LMP) = lmp.handle

"""
Expand Down Expand Up @@ -174,17 +187,29 @@ function version(lmp::LMP)
API.lammps_version(lmp)
end

struct LAMMPSError <: Exception
msg::String
end

function LAMMPSError(lmp::LMP)
buf = zeros(UInt8, 100)
API.lammps_get_last_error_message(lmp, buf, length(buf))
msg = replace(rstrip(String(buf), '\0'), "ERROR: " => "")
LAMMPSError(msg)
end

function Base.showerror(io::IO, err::LAMMPSError)
print(io, "LAMMPSError: ", err.msg)
end

function check(lmp::LMP)
err = API.lammps_has_error(lmp)
# TODO: Check err == 1 or err == 2 (MPI)
if err != 0
# TODO: Check err == 1 or err == 2 (MPI)
buf = zeros(UInt8, 100)
API.lammps_get_last_error_message(lmp, buf, length(buf))
error(rstrip(String(buf), '\0'))
throw(LAMMPSError(lmp))
end
end


"""
command(lmp::LMP, cmd::Union{String, Array{String}})

Expand Down
11 changes: 10 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ LMP(["-screen", "none"]) do lmp
@test LAMMPS.version(lmp) >= 0
command(lmp, "clear")

@test_throws ErrorException command(lmp, "nonsense")
@test_throws LAMMPSError command(lmp, "nonsense")

LAMMPS.close!(lmp)

expected_error = ErrorException("The LMP object doesn't point to a valid LAMMPS instance! "
* "This is usually caused by calling `LAMMPS.close!` or through serialization and deserialization.")

@test_throws expected_error command(lmp, "")
end

@test_throws LAMMPSError LMP(["-nonesense"])

@testset "Extract Setting/Global" begin
LMP(["-screen", "none"]) do lmp
command(lmp, """
Expand Down
Loading