tdo not generate grains that protrude outside of the grid boundaries - Granular.jl - Julia package for granular dynamics simulation
 (HTM) git clone git://src.adamsgaard.dk/Granular.jl
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 118b5f914de15690331b7484b3cc6c3478363bd4
 (DIR) parent 4148921ec0aeb783a3cb3b7a3432918434e57101
 (HTM) Author: Anders Damsgaard <andersd@riseup.net>
       Date:   Wed, 22 Nov 2017 11:01:13 -0500
       
       do not generate grains that protrude outside of the grid boundaries
       
       Diffstat:
         M src/grid.jl                         |      15 ++++++++++-----
         M test/grid.jl                        |      10 +++++-----
       
       2 files changed, 15 insertions(+), 10 deletions(-)
       ---
 (DIR) diff --git a/src/grid.jl b/src/grid.jl
       t@@ -529,7 +529,7 @@ grid (e.g., using `sortGrainsInGrid()`).
        * `i::Int`: the grid-cell index along x.
        * `j::Int`: the grid-cell index along y.
        * `r::Float64`: the desired grain radius to fit into the cell.
       -* `n_iter::Int = 10`: the number of attempts for finding an empty spot.
       +* `n_iter::Int = 30`: the number of attempts for finding an empty spot.
        * `seed::Int = 1`: seed for the pseudo-random number generator.
        * `verbose::Bool = false`: print diagnostic information.
        """
       t@@ -538,7 +538,7 @@ function findEmptyPositionInGridCell(simulation::Simulation,
                                             i::Int,
                                             j::Int,
                                             r::Float64;
       -                                     n_iter::Int = 10,
       +                                     n_iter::Int = 30,
                                             seed::Int = 1,
                                             verbose::Bool = false)
            overlap_found = false
       t@@ -561,12 +561,16 @@ function findEmptyPositionInGridCell(simulation::Simulation,
        
                # do not penetrate outside of grid boundaries
                if i == 1 && pos[1] - r < grid.xq[1,1]
       +            pos .= [NaN, NaN]
                    continue
                elseif i == nx && pos[1] + r > grid.xq[end,end]
       +            pos .= [NaN, NaN]
                    continue
                elseif j == 1 && pos[2] - r < grid.yq[1,1]
       +            pos .= [NaN, NaN]
                    continue
                elseif j == ny && pos[2] + r > grid.yq[end,end]
       +            pos .= [NaN, NaN]
                    continue
                end
        
       t@@ -607,15 +611,16 @@ function findEmptyPositionInGridCell(simulation::Simulation,
                    break
                end
            end
       -    if verbose && overlap_found == false
       +    if verbose && !overlap_found
                info("Found position $pos in cell $i,$j")
            elseif verbose && overlap_found
                info("Free position not found in cell $i,$j")
            end
        
       -    if overlap_found == false
       +    if !overlap_found
                if isnan(pos[1]) || isnan(pos[2])
       -            error("fatal error: could not determine free position in cell")
       +            warn("could not determine free position in cell")
       +            return false
                end
                return pos
            else
 (DIR) diff --git a/test/grid.jl b/test/grid.jl
       t@@ -251,17 +251,17 @@ sim.ocean = Granular.createRegularOceanGrid([4, 4, 2], [4., 4., 2.])
        Granular.addGrainCylindrical!(sim, [.25, .25], .25, 1., verbose=verbose)
        Granular.addGrainCylindrical!(sim, [.75, .75], .25, 1., verbose=verbose)
        Granular.sortGrainsInGrid!(sim, sim.ocean, verbose=verbose)
       -pos = Granular.findEmptyPositionInGridCell(sim, sim.ocean, 1, 1, .25, 
       -                                         verbose=true)
       +pos = Granular.findEmptyPositionInGridCell(sim, sim.ocean, 1, 1, .25, n_iter=30,
       +                                           verbose=true)
        @test pos != false
        @test Granular.isPointInCell(sim.ocean, 1, 1, pos) == true
        
        info("# Insert into full cell")
        sim = Granular.createSimulation()
        sim.ocean = Granular.createRegularOceanGrid([4, 4, 2], [4., 4., 2.])
       -Granular.addGrainCylindrical!(sim, [.5, .5], 1., 1., verbose=verbose)
       -Granular.addGrainCylindrical!(sim, [.75, .5], 1., 1., verbose=verbose)
       -Granular.addGrainCylindrical!(sim, [.5, .75], 1., 1., verbose=verbose)
       +Granular.addGrainCylindrical!(sim, [.25, .25], 1., 1., verbose=verbose)
       +Granular.addGrainCylindrical!(sim, [.75, .25], 1., 1., verbose=verbose)
       +Granular.addGrainCylindrical!(sim, [.25, .75], 1., 1., verbose=verbose)
        Granular.addGrainCylindrical!(sim, [.75, .75], 1., 1., verbose=verbose)
        Granular.sortGrainsInGrid!(sim, sim.ocean, verbose=verbose)
        pos = Granular.findEmptyPositionInGridCell(sim, sim.ocean, 1, 1, 0.5,