JuliaMolSim / Molly.jl

Molecular simulation in Julia
Other
393 stars 53 forks source link

Incompatibility of types -> bug #103

Closed Leticia-maria closed 2 years ago

Leticia-maria commented 2 years ago

Hi everyone, I was testing Molly in a new computational resource. I've just tried to reproduce the main example (the fluid in a Lennard Jones potential). An important information is: I was running on Julia 1.8.1.

All was going good,

using Molly

n_atoms = 100
boundary = CubicBoundary(2.0u"nm", 2.0u"nm", 2.0u"nm")
temp = 298.0u"K"
atom_mass = 10.0u"u"

atoms = [Atom(mass=atom_mass, σ=0.3u"nm", ϵ=0.2u"kJ * mol^-1") for i in 1:n_atoms]

when the code crashed because I've tried to run this line:

coords = place_atoms(n_atoms, boundary; min_dist=0.3u"nm")

I got the following error:

ERROR: MethodError: no method matching place_atoms(::Int64, ::CubicBoundary{Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}}; min_dist=0.3 nm)
Closest candidates are:
  place_atoms(::Integer, ::Any, ::Any; max_attempts) at ~/.julia/packages/Molly/RD5GY/src/setup.jl:22 got unsupported keyword argument "min_dist"
Stacktrace:
 [1] top-level scope
   @ REPL[11]:1
 [2] top-level scope
   @ ~/.julia/packages/CUDA/DfvRa/src/initialization.jl:52

As the error said, I took a look in the file setup.jl (line 25).

https://github.com/JuliaMolSim/Molly.jl/blob/47f3e1fd41a9a9daa33732ef7a5c115a3c2e607f/src/setup.jl#L25

As you can see, the method place_atoms receives an argument of type Integer, but n_atoms is an Int64. If you go to the Julia REPL, you will see that Int64 is a subtype of Integer, but they are not equal.

julia> Int64 == Integer
false
julia> Int64 <: Integer
true

The solution is to change the syntax of the function:

function place_atoms(n_atoms::T ...) where T <: Integer
...
end

I can help solving it by sending a PR.

All my best regards Leticia Madureira

JaydevSR commented 2 years ago

Here, the problem is not actually with n_atoms but rather with the min_dist keyword argument. It was just added in a newer version of Molly. The problem will be solved once you update Molly using:

pkg> update Molly
jgreener64 commented 2 years ago

To add to Jaydev's correct answer: you are right that Int64 is a subtype of Integer but they are not equal. However the line function place_atoms(n_atoms::Integer ...) basically means the same thing as function place_atoms(n_atoms::T ...) where T <: Integer, with the second being useful if you need to use the type T within the function. Hope that helps!