m3g / CellListMap.jl

Flexible implementation of cell lists to map the calculations of particle-pair dependent functions, such as forces, energies, neighbor lists, etc.
https://m3g.github.io/CellListMap.jl/
MIT License
87 stars 4 forks source link

Units issue with triclinic box #66

Closed jgreener64 closed 2 years ago

jgreener64 commented 2 years ago

With Julia v1.7.2 and CellListMap v0.7.20, the example from the docs works fine:

using CellListMap, Unitful, StaticArrays
cutoff = 0.1u"nm"
box = Box([1.0, 1.0, 1.0]u"nm", cutoff)
x = [rand(typeof(cutoff), 3) for _ in 1:1000]
cl = CellList(x, box; parallel=false)

However when trying it with a triclinic box:

box2 = Box([1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 1.0]u"nm", cutoff)
cl2 = CellList(x, box2; parallel=false)
ERROR: LoadError: DimensionError: nm^-2 and 0.8425463402211867 are not dimensionally compatible.
Stacktrace:
  [1] convert(#unused#::Type{Quantity{Float64, 𝐋^-2, Unitful.FreeUnits{(nm^-2,), 𝐋^-2, nothing}}}, x::Float64)
    @ Unitful ~/.julia/packages/Unitful/SUQzL/src/conversion.jl:112
  [2] macro expansion
    @ ~/.julia/packages/StaticArraysCore/gkLqH/src/StaticArraysCore.jl:79 [inlined]
  [3] convert_ntuple
    @ ~/.julia/packages/StaticArraysCore/gkLqH/src/StaticArraysCore.jl:75 [inlined]
  [4] SVector{3, Quantity{Float64, 𝐋^-2, Unitful.FreeUnits{(nm^-2,), 𝐋^-2, nothing}}}(x::Tuple{Float64, Float64, Float64})
    @ StaticArraysCore ~/.julia/packages/StaticArraysCore/gkLqH/src/StaticArraysCore.jl:111
  [5] StaticArray
    @ ~/.julia/packages/StaticArrays/8Dz3j/src/convert.jl:160 [inlined]
  [6] _solve
    @ ~/.julia/packages/StaticArrays/8Dz3j/src/solve.jl:17 [inlined]
  [7] \
    @ ~/.julia/packages/StaticArrays/8Dz3j/src/solve.jl:1 [inlined]
  [8] wrap_cell_fraction
    @ ~/.julia/packages/CellListMap/zdcxg/src/CellOperations.jl:46 [inlined]
  [9] wrap_to_first
    @ ~/.julia/packages/CellListMap/zdcxg/src/CellOperations.jl:80 [inlined]
 [10] wrap_to_first
    @ ~/.julia/packages/CellListMap/zdcxg/src/CellOperations.jl:98 [inlined]
 [11] add_particles!(x::Vector{Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}}}, box::Box{TriclinicCell, 3, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}, Quantity{Float64, 𝐋^2, Unitful.FreeUnits{(nm^2,), 𝐋^2, nothing}}, 9}, ishift::Int64, cl::CellList{3, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}})
    @ CellListMap ~/.julia/packages/CellListMap/zdcxg/src/CellLists.jl:871
 [12] UpdateCellList!(x::Vector{Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}}}, box::Box{TriclinicCell, 3, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}, Quantity{Float64, 𝐋^2, Unitful.FreeUnits{(nm^2,), 𝐋^2, nothing}}, 9}, cl::CellList{3, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}}, aux::Nothing; parallel::Bool)
    @ CellListMap ~/.julia/packages/CellListMap/zdcxg/src/CellLists.jl:787
 [13] #UpdateCellList!#49
    @ ~/.julia/packages/CellListMap/zdcxg/src/CellLists.jl:679 [inlined]
 [14] CellList(x::Vector{Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}}}, box::Box{TriclinicCell, 3, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}, Quantity{Float64, 𝐋^2, Unitful.FreeUnits{(nm^2,), 𝐋^2, nothing}}, 9}; parallel::Bool, nbatches::Tuple{Int64, Int64})
    @ CellListMap ~/.julia/packages/CellListMap/zdcxg/src/CellLists.jl:495
 [15] top-level scope
    @ ~/dms/molly_dev/cnf_err.jl:11
in expression starting at /home/jgreener/dms/molly_dev/cnf_err.jl:11

Stripping the units in the triclinic case also works fine.

lmiq commented 2 years ago

The problem is this one:

https://github.com/PainterQubits/Unitful.jl/issues/46

I'll see what is the best alternative or workaround.

lmiq commented 2 years ago

Should be working on v0.7.21 to appear at any moment.

jgreener64 commented 2 years ago

Thanks for this.