Code Monkey home page Code Monkey logo

Comments (3)

zahachtah avatar zahachtah commented on September 21, 2024

Just had a thought, could one do it as a view of a SArray?

xpositions = rand(SVector{2,Float64},10)

alive=[2,5,6,7]

system = PeriodicSystem(
           xpositions = view(xpositions,alive), 
           unitcell=[1.0,1.0], 
           cutoff = 0.1, 
           output = 0.0,
           output_name = :organism
       );

Now one could update the alive vector and this should make CellListMap.map_pairwise! skip pairwise calculations of any non-existing organisms....maybe....
(late now, will test tomorrow if you haven't commented yet :-) )

from celllistmap.jl.

lmiq avatar lmiq commented on September 21, 2024

That won't really bring you any benefit, because the coordinates are internally copied anyway to make them local to each computation.

The simplest approach is to simply put the live individuals at the beginning of the array and resize it. Partition the array in live/death is a relatively cheap operation (actually there is an internal function in CellListMap that can almost do that, here is a variation of it:

julia> function keep_alive!(alive, x::AbstractVector)
           iswap = 1
           @inbounds for i in eachindex(alive,x)
               if alive[i]
                   if iswap != i
                       x[iswap], x[i] = x[i], x[iswap]
                       alive[iswap], alive[i] = alive[i], alive[iswap]
                   end
                   iswap += 1
               end
           end
           return resize!(x,iswap - 1), resize!(alive,iswap - 1)
       end
keep_alive! (generic function with 1 method)

julia> x = rand(SVector{2,Float64}, 100); alive = rand(Bool, 100);

julia> keep_alive!(alive, x)
(SVector{2, Float64}[[0.29119093606094626, 0.14799284331418305], [0.4972730380281174, 0.09269265678798855], [0.9159907295858599, 0.023425334184397073], [0.6070998442833461, 0.5991916701778053], [0.3160812299339907, 0.3164821161774142], [0.7439114803685659, 0.11559108939285456], [0.6670382934847978, 0.9839740944475307], [0.657358664500978, 0.32044636378412406], [0.6082878186504177, 0.50068291214486], [0.08662133450538845, 0.4684000312786505]    [0.3151220417919539, 0.4605952237829716], [0.2875938461476413, 0.5834502287377114], [0.030323370160795338, 0.6514632971028673], [0.11688254221953154, 0.6127507999882679], [0.44709649959706943, 0.41760496956657467], [0.7726898156341978, 0.6743612022838364], [0.189673060868509, 0.2515943452830405], [0.6597804541954473, 0.028298360650823362], [0.002315181581732384, 0.30165618551226736], [0.9019060110665619, 0.9487908898003208]], Bool[1, 1, 1, 1, 1, 1, 1, 1, 1, 1    1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

Then, for example, let us say that you initialized the system with the original coordinates:

julia> x = rand(SVector{2,Float64}, 100); alive = rand(Bool, 100);

julia> sys = PeriodicSystem(xpositions=x,unitcell=[1,1],cutoff=0.05,output=0.0)
PeriodicSystem1{output} of dimension 2, composed of:
    Box{OrthorhombicCell, 2}
      unit cell matrix = [ 1.0, 0.0; 0.0, 1.0 ]
      cutoff = 0.05
      number of computing cells on each dimension = [22, 22]
      computing cell sizes = [0.05, 0.05] (lcell: 1)
      Total number of cells = 484
    CellList{2, Float64}
      100 real particles.
      87 cells with real particles.
      123 particles in computing box, including images.
    Parallelization auxiliary data set for: 
      Number of batches for cell list construction: 1
      Number of batches for function mapping: 1
    Type of output variable (output): Float64

Note that the system has 100 real particles.

If you now "kill" the individuals:

julia> keep_alive!(alive, x);

The next time map_pairwise! is called the system will be updated for the new set of coordinates:

julia> PeriodicSystems.map_pairwise!((x,y,i,j,d2,out) -> out += d2, sys)
0.00416479370378711

julia> sys
PeriodicSystem1{output} of dimension 2, composed of:
    Box{OrthorhombicCell, 2}
      unit cell matrix = [ 1.0, 0.0; 0.0, 1.0 ]
      cutoff = 0.05
      number of computing cells on each dimension = [22, 22]
      computing cell sizes = [0.05, 0.05] (lcell: 1)
      Total number of cells = 484
    CellList{2, Float64}
      45 real particles.
      41 cells with real particles.
      55 particles in computing box, including images.
    Parallelization auxiliary data set for: 
      Number of batches for cell list construction: 1
      Number of batches for function mapping: 1
    Type of output variable (output): Float64

(note now that there are 45 real particles, the ones that were alive).

Note that that partition and resizing is cheap, and won't affect the overall performance of almost any simulation. Here with 10_000 particles:

julia> x = rand(SVector{2,Float64}, 10^4); alive = rand(Bool, 10^4);

julia> sys = PeriodicSystem(xpositions=x,unitcell=[1,1],cutoff=0.05,output=0.0);

julia> @btime PeriodicSystems.map_pairwise!((x,y,i,j,d2,out) -> out += d2, $sys)
  1.049 ms (144 allocations: 14.78 KiB)
489.7633383677577

julia> @btime keep_alive!(alive, x) setup=(x = rand(SVector{2,Float64}, 10^4); alive = rand(Bool, 10^4)) evals=1;
  46.064 μs (0 allocations: 0 bytes)

julia> keep_alive!(alive, x);

julia> @btime PeriodicSystems.map_pairwise!((x,y,i,j,d2,out) -> out += d2, $sys)
  499.010 μs (144 allocations: 14.78 KiB)
119.80104258632134

from celllistmap.jl.

zahachtah avatar zahachtah commented on September 21, 2024

Great explanation,thank you very much. I think I can adapt this to birth events too. Enjoying the package, very efficient!

from celllistmap.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.