Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeziere authored Aug 14, 2024
2 parents 2e9ea49 + c838e7e commit 715a4c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
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, create_atoms, 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 @@ -158,11 +158,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 @@ -192,17 +205,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

0 comments on commit 715a4c7

Please sign in to comment.