tAdd `color` field to grains - 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 c5ffb485728badec9ab61be35670289b2a24efac
 (DIR) parent bfdbba1d09a0acec0586cb0de27b5b90112d6ec8
 (HTM) Author: Anders Damsgaard <andersd@riseup.net>
       Date:   Fri, 22 Dec 2017 15:55:22 -0500
       
       Add `color` field to grains
       
       Diffstat:
         M src/datatypes.jl                    |       5 +++++
         M src/grain.jl                        |      19 +++++++++++++++++--
         M src/io.jl                           |      34 ++++++++++++++++++++++++++-----
       
       3 files changed, 51 insertions(+), 7 deletions(-)
       ---
 (DIR) diff --git a/src/datatypes.jl b/src/datatypes.jl
       t@@ -69,6 +69,9 @@ mutable struct GrainCylindrical
            granular_stress::Vector{Float64}
            ocean_stress::Vector{Float64}
            atmosphere_stress::Vector{Float64}
       +
       +    # Visualization parameters
       +    color::Int
        end
        
        # Type for linear (flat) and frictionless dynamic walls
       t@@ -149,6 +152,8 @@ mutable struct GrainArrays
            granular_stress::Array{Float64, 2}
            ocean_stress::Array{Float64, 2}
            atmosphere_stress::Array{Float64, 2}
       +
       +    color::Vector{Int}
        end
        
        #=
 (DIR) diff --git a/src/grain.jl b/src/grain.jl
       t@@ -98,6 +98,8 @@ are optional, and come with default values.  The only required arguments are
            ocean drag [Pa].
        * `atmosphere_stress::Vector{Float64} = [0., 0.]`: resultant stress on grain
            from atmosphere drag [Pa].
       +* `color::Int=0`: type number, usually used for associating a color to the grain
       +    during visualization.
        
        # Examples
        The most basic example adds a new grain to the simulation `sim`, with a 
       t@@ -171,7 +173,8 @@ function addGrainCylindrical!(simulation::Simulation,
                                        n_contacts::Int = 0,
                                        granular_stress::Vector{Float64} = [0., 0.],
                                        ocean_stress::Vector{Float64} = [0., 0.],
       -                                atmosphere_stress::Vector{Float64} = [0., 0.])
       +                                atmosphere_stress::Vector{Float64} = [0., 0.],
       +                                color::Int = 0)
        
            # Check input values
            if length(lin_pos) != 2
       t@@ -266,7 +269,9 @@ function addGrainCylindrical!(simulation::Simulation,
        
                                         granular_stress,
                                         ocean_stress,
       -                                 atmosphere_stress
       +                                 atmosphere_stress,
       +
       +                                 color
                                        )
        
            # Overwrite previous placeholder values
       t@@ -379,6 +384,9 @@ function convertGrainDataToArrays(simulation::Simulation)
                                  zeros(Float64, 3, length(simulation.grains)),
                                  zeros(Float64, 3, length(simulation.grains)),
                                  zeros(Float64, 3, length(simulation.grains)),
       +
       +                          Array{Int}(length(simulation.grains)),
       +
                                 )
        
            # fill arrays
       t@@ -450,6 +458,8 @@ function convertGrainDataToArrays(simulation::Simulation)
                ifarr.ocean_stress[1:2, i] = simulation.grains[i].ocean_stress
                ifarr.atmosphere_stress[1:2, i] =
                    simulation.grains[i].atmosphere_stress
       +
       +        ifarr.color[i] = simulation.grains[i].color
            end
        
            return ifarr
       t@@ -514,6 +524,7 @@ function deleteGrainArrays!(ifarr::GrainArrays)
            ifarr.ocean_stress = f2
            ifarr.atmosphere_stress = f2
        
       +    ifarr.color = i1
            gc()
            nothing
        end
       t@@ -579,6 +590,8 @@ function printGrainInfo(f::GrainCylindrical)
            println("  granular_stress:   $(f.granular_stress) Pa")
            println("  ocean_stress:      $(f.ocean_stress) Pa")
            println("  atmosphere_stress: $(f.atmosphere_stress) Pa")
       +
       +    println("  color:  $(f.color)\n")
            nothing
        end
        
       t@@ -723,6 +736,8 @@ function compareGrains(if1::GrainCylindrical, if2::GrainCylindrical)
            @test if1.granular_stress ≈ if2.granular_stress
            @test if1.ocean_stress ≈ if2.ocean_stress
            @test if1.atmosphere_stress ≈ if2.atmosphere_stress
       +
       +    @test if1.color ≈ if2.color
            nothing
        end
        
 (DIR) diff --git a/src/io.jl b/src/io.jl
       t@@ -431,6 +431,9 @@ function writeGrainVTK(simulation::Simulation,
            WriteVTK.vtk_point_data(vtkfile, ifarr.atmosphere_stress,
                                    "Atmosphere stress [Pa]")
        
       +    WriteVTK.vtk_point_data(vtkfile, ifarr.color,
       +                            "Color [-]")
       +
            deleteGrainArrays!(ifarr)
            ifarr = 0
            gc()
       t@@ -834,7 +837,8 @@ imagegrains.PointArrayStatus = [
        'Number of contacts [-]',
        'Granular stress [Pa]',
        'Ocean stress [Pa]',
       -'Atmosphere stress [Pa]']
       +'Atmosphere stress [Pa]',
       +'Color [-]']
        
        animationScene1 = GetAnimationScene()
        
       t@@ -1204,6 +1208,8 @@ function plotGrains(sim::Simulation;
                            filetype::String = "png",
                            gnuplot_terminal::String = "png crop size 1200,1200",
                            plot_interactions::Bool = true,
       +                    palette_scalar::String = "contact_radius",
       +                    cbrange::Vector{Float64} = [NaN, NaN],
                            show_figure::Bool = true,
                            verbose::Bool = true)
        
       t@@ -1215,10 +1221,24 @@ function plotGrains(sim::Simulation;
            x = Float64[]
            y = Float64[]
            r = Float64[]
       +    scalars = Float64[]
            for grain in sim.grains
                push!(x, grain.lin_pos[1])
                push!(y, grain.lin_pos[2])
                push!(r, grain.contact_radius)
       +
       +        if palette_scalar == "contact_radius"
       +            push!(scalars, grain.contact_radius)
       +
       +        elseif palette_scalar == "areal_radius"
       +            push!(scalars, grain.areal_radius)
       +
       +        elseif palette_scalar == "color"
       +            push!(scalars, grain.color)
       +
       +        else
       +            error("palette_scalar = '$palette_scalar' not understood.")
       +        end
            end
        
            # prepare interaction data
       t@@ -1268,7 +1288,6 @@ function plotGrains(sim::Simulation;
                                                   contact_stiffness_normal)
                            end
        
       -                    
                            push!(i1, i)
                            push!(i2, j)
                            push!(inter_particle_vector, p)
       t@@ -1290,7 +1309,7 @@ function plotGrains(sim::Simulation;
        
            # write grain data to temporary file on disk
            datafile = Base.Filesystem.tempname()
       -    writedlm(datafile, [x y r])
       +    writedlm(datafile, [x y r scalars])
            gnuplotscript = Base.Filesystem.tempname()
        
            #=
       t@@ -1335,11 +1354,15 @@ function plotGrains(sim::Simulation;
        
                # light gray to black
                write(f, "set palette defined ( 1 '#d3d3d3', 2 '#000000')\n")
       +        
       +        if !isnan(cbrange[1])
       +            write(f, "set cbrange [$(cbrange[1]):$(cbrange[2])]\n")
       +        end
        
                # gray to white
                #write(f, "set palette defined (0 'gray', 1 'white')\n")
        
       -        write(f, """set cblabel "Diameter [m]"
       +        write(f, """set cblabel "$palette_scalar"
                      set size ratio -1
                      set key off\n""")
        
       t@@ -1366,7 +1389,8 @@ function plotGrains(sim::Simulation;
                    end
                end
        
       -        write(f,"""plot "$(datafile)" with circles palette fs lt 1 lc rgb "black" t "Particle"
       +        #write(f,"""plot "$(datafile)" with circles lt 1 lc rgb "black" t "Particle"
       +        write(f,"""plot "$(datafile)" with circles fill solid fillcolor palette lw 0 title "Particle"
                      """)
            end