Granular.jl

Julia package for granular dynamics simulation
git clone git://src.adamsgaard.dk/Granular.jl
Log | Files | Refs | README | LICENSE

commit 1d309104fabb6e82eeb413f8e04d5d2551ba89d0
parent a31406ece5e6cc44de7bbf15cdc4f45b19094039
Author: Anders Damsgaard <andersd@riseup.net>
Date:   Fri,  9 Jun 2017 15:06:13 -0400

add comparison functions for all custom data types

Diffstat:
Msrc/atmosphere.jl | 27+++++++++++++++++++++++++++
Msrc/icefloe.jl | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/io.jl | 37+++++++++++++++++++++++++++++++++++++
Msrc/ocean.jl | 30++++++++++++++++++++++++++++++
Msrc/simulation.jl | 26++++++++++++++++++++++++++
Atest/jdl.jl | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/runtests.jl | 1+
7 files changed, 253 insertions(+), 0 deletions(-)

diff --git a/src/atmosphere.jl b/src/atmosphere.jl @@ -207,3 +207,30 @@ function applyAtmosphereVorticityToIceFloe!(ice_floe::IceFloeCylindrical, abs(.5*atmosphere_curl - ice_floe.ang_vel)* (.5*atmosphere_curl - ice_floe.ang_vel) end + +export compareAtmospheres +""" + compareAtmospheres(atmosphere1::atmosphere, atmosphere2::atmosphere) + +Compare values of two `atmosphere` objects using the `Base.Test` framework. +""" +function compareAtmospheres(atmosphere1::Atmosphere, atmosphere2::Atmosphere) + + Base.Test.@test atmosphere1.input_file == atmosphere2.input_file + Base.Test.@test atmosphere1.time ≈ atmosphere2.time + + Base.Test.@test atmosphere1.xq ≈ atmosphere2.xq + Base.Test.@test atmosphere1.yq ≈ atmosphere2.yq + + Base.Test.@test atmosphere1.xh ≈ atmosphere2.xh + Base.Test.@test atmosphere1.yh ≈ atmosphere2.yh + + Base.Test.@test atmosphere1.zl ≈ atmosphere2.zl + + Base.Test.@test atmosphere1.u ≈ atmosphere2.u + Base.Test.@test atmosphere1.v ≈ atmosphere2.v + + if isdefined(atmosphere1.ice_floe_list, 1) + Base.Test.@test atmosphere1.ice_floe_list == atmosphere2.ice_floe_list + end +end diff --git a/src/icefloe.jl b/src/icefloe.jl @@ -416,3 +416,74 @@ function totalIceFloeKineticRotationalEnergy(simulation::Simulation) end return E_sum end + +export compareIceFloes +""" + compareIceFloes(if1::IceFloeCylindrical, if2::IceFloeCylindrical) + +Compare values of two ice floe objects using the `Base.Test` framework. +""" +function compareIceFloes(if1::IceFloeCylindrical, if2::IceFloeCylindrical) + + Base.Test.@test if1.density ≈ if2.density + Base.Test.@test if1.thickness ≈ if2.thickness + Base.Test.@test if1.contact_radius ≈ + if2.contact_radius + Base.Test.@test if1.areal_radius ≈ if2.areal_radius + Base.Test.@test if1.circumreference ≈ + if2.circumreference + Base.Test.@test if1.horizontal_surface_area ≈ if2.horizontal_surface_area + Base.Test.@test if1.side_surface_area ≈ if2.side_surface_area + Base.Test.@test if1.volume ≈ if2.volume + Base.Test.@test if1.mass ≈ if2.mass + Base.Test.@test if1.moment_of_inertia ≈ if2.moment_of_inertia + + Base.Test.@test if1.lin_pos ≈ if2.lin_pos + Base.Test.@test if1.lin_vel ≈ if2.lin_vel + Base.Test.@test if1.lin_acc ≈ if2.lin_acc + Base.Test.@test if1.force ≈ if2.force + + Base.Test.@test if1.ang_pos ≈ if2.ang_pos + Base.Test.@test if1.ang_vel ≈ if2.ang_vel + Base.Test.@test if1.ang_acc ≈ if2.ang_acc + Base.Test.@test if1.torque ≈ if2.torque + + Base.Test.@test if1.fixed == if2.fixed + Base.Test.@test if1.rotating == if2.rotating + Base.Test.@test if1.enabled == if2.enabled + + Base.Test.@test if1.contact_stiffness_normal ≈ if2.contact_stiffness_normal + Base.Test.@test if1.contact_stiffness_tangential ≈ + if2.contact_stiffness_tangential + Base.Test.@test if1.contact_viscosity_normal ≈ if2.contact_viscosity_normal + Base.Test.@test if1.contact_viscosity_tangential ≈ + if2.contact_viscosity_tangential + Base.Test.@test if1.contact_static_friction ≈ if2.contact_static_friction + Base.Test.@test if1.contact_dynamic_friction ≈ if2.contact_dynamic_friction + + Base.Test.@test if1.youngs_modulus ≈ if2.youngs_modulus + Base.Test.@test if1.poissons_ratio ≈ if2.poissons_ratio + Base.Test.@test if1.tensile_strength ≈ if2.tensile_strength + Base.Test.@test if1.tensile_heal_rate ≈ if2.tensile_heal_rate + Base.Test.@test if1.compressive_strength_prefactor ≈ + if2.compressive_strength_prefactor + + Base.Test.@test if1.ocean_drag_coeff_vert ≈ if2.ocean_drag_coeff_vert + Base.Test.@test if1.ocean_drag_coeff_horiz ≈ if2.ocean_drag_coeff_horiz + Base.Test.@test if1.atmosphere_drag_coeff_vert ≈ + if2.atmosphere_drag_coeff_vert + Base.Test.@test if1.atmosphere_drag_coeff_horiz ≈ + if2.atmosphere_drag_coeff_horiz + + Base.Test.@test if1.pressure ≈ if2.pressure + Base.Test.@test if1.n_contacts == if2.n_contacts + Base.Test.@test if1.ocean_grid_pos == if2.ocean_grid_pos + Base.Test.@test if1.contacts == if2.contacts + Base.Test.@test if1.contact_parallel_displacement == + if2.contact_parallel_displacement + Base.Test.@test if1.contact_age ≈ if2.contact_age + + Base.Test.@test if1.granular_stress ≈ if2.granular_stress + Base.Test.@test if1.ocean_stress ≈ if2.ocean_stress + Base.Test.@test if1.atmosphere_stress ≈ if2.atmosphere_stress +end diff --git a/src/io.jl b/src/io.jl @@ -1,8 +1,45 @@ import WriteVTK import NetCDF +import JLD ## IO functions +""" + writeSimulation(simulation::Simulation; + filename::String="", + folder::String=".", + verbose::Bool=true) + +Write all content from `Simulation` to disk in HDF5 format. If the `filename` +parameter is not specified, it will be saved to a subdirectory under the current +directory named after the simulation identifier `simulation.id`. +""" +function writeSimulation(simulation::Simulation; + filename::String="", + folder::String=".", + verbose::Bool=true) + if filename == "" + folder = folder * "/" * simulation.id + mkpath(folder) + filename = string(folder, "/", simulation.id, ".", + simulation.file_number, ".jld") + end + + JLD.save(filename, "simulation", simulation) + + if verbose + info("simulation written to $filename") + end +end + +function readSimulation(filename::String=""; + verbose::Bool=true) + if verbose + info("reading simulation from $filename") + end + return JLD.load(filename, "simulation") +end + export writeVTK """ Write a VTK file to disk containing all ice floes in the `simulation` in an diff --git a/src/ocean.jl b/src/ocean.jl @@ -323,3 +323,33 @@ function applyOceanVorticityToIceFloe!(ice_floe::IceFloeCylindrical, draft*ice_floe.ocean_drag_coeff_vert)* abs(.5*ocean_curl - ice_floe.ang_vel)*(.5*ocean_curl - ice_floe.ang_vel) end + +export compareOceans +""" + compareOceans(ocean1::Ocean, ocean2::Ocean) + +Compare values of two `Ocean` objects using the `Base.Test` framework. +""" +function compareOceans(ocean1::Ocean, ocean2::Ocean) + + Base.Test.@test ocean1.input_file == ocean2.input_file + Base.Test.@test ocean1.time ≈ ocean2.time + + Base.Test.@test ocean1.xq ≈ ocean2.xq + Base.Test.@test ocean1.yq ≈ ocean2.yq + + Base.Test.@test ocean1.xh ≈ ocean2.xh + Base.Test.@test ocean1.yh ≈ ocean2.yh + + Base.Test.@test ocean1.zl ≈ ocean2.zl + Base.Test.@test ocean1.zi ≈ ocean2.zi + + Base.Test.@test ocean1.u ≈ ocean2.u + Base.Test.@test ocean1.v ≈ ocean2.v + Base.Test.@test ocean1.h ≈ ocean2.h + Base.Test.@test ocean1.e ≈ ocean2.e + + if isdefined(ocean1.ice_floe_list, 1) + Base.Test.@test ocean1.ice_floe_list == ocean2.ice_floe_list + end +end diff --git a/src/simulation.jl b/src/simulation.jl @@ -196,3 +196,29 @@ function reportSimulationTimeToStdout(simulation::Simulation) print("\r t = ", simulation.time, '/', simulation.time_total, " s ") end + +export compareSimulations +""" + compareSimulations(sim1::Simulation, sim2::Simulation) + +Compare values of two `Simulation` objects using the `Base.Test` framework. +""" +function compareSimulations(sim1::Simulation, sim2::Simulation) + + Base.Test.@test sim1.id == sim2.id + + Base.Test.@test sim1.time_iteration == sim2.time_iteration + Base.Test.@test sim1.time ≈ sim2.time + Base.Test.@test sim1.time_total ≈ sim2.time_total + Base.Test.@test sim1.time_step ≈ sim2.time_step + Base.Test.@test sim1.file_time_step ≈ sim2.file_time_step + Base.Test.@test sim1.file_number == sim2.file_number + Base.Test.@test sim1.file_time_since_output_file ≈ + sim2.file_time_since_output_file + + for i=1:length(sim1.ice_floes) + compareIceFloes(sim1.ice_floes[i], sim2.ice_floes[i]) + end + compareOceans(sim1.ocean, sim2.ocean) + compareAtmospheres(sim1.atmosphere, sim2.atmosphere) +end diff --git a/test/jdl.jl b/test/jdl.jl @@ -0,0 +1,61 @@ +#!/usr/bin/env julia + +info("#### $(basename(@__FILE__)) ####") + +info("Writing simple simulation to JDL file") +sim = SeaIce.createSimulation(id="test") +SeaIce.addIceFloeCylindrical(sim, [ 0., 0.], 10., 1., verbose=false) +SeaIce.addIceFloeCylindrical(sim, [18., 0.], 10., 1., verbose=false) +sim.ocean = SeaIce.createRegularOceanGrid([10, 20, 5], [10., 25., 2.]) +SeaIce.findContacts!(sim, method="all to all") +SeaIce.writeVTK(sim, verbose=false) + +SeaIce.writeSimulation(sim) + +sim2 = SeaIce.readSimulation("./test/test.1.jld") +SeaIce.compareSimulations(sim, sim2) + + + +""" +cmd_post = "" +if Base.is_linux() + cmd = "sha256sum" +elseif Base.is_apple() + cmd = ["shasum", "-a", "256"] +elseif Base.is_windows() + info("checksum verification not yet implemented on Windows") + exit() + cmd = ["powershell", "-Command", "\"Get-FileHash", "-Algorithm", "SHA256"] + cmd_post = "\"" +else + error("checksum verification of VTK file not supported on this platform") +end + +icefloepath = "test/test.icefloes.1.vtu" +icefloechecksum = +"c75ffde29fbdd80161dafd524e690fbcbae2136d4f68c29f725d2d2454c6a162 " * +icefloepath * "\n" + +oceanpath = "test/test.ocean.1.vts" +oceanchecksum = +"d56ffb109841a803f2b2b94c74c87f7a497237204841d557d2b1043694d51f0d " * +oceanpath * "\n" + +@test readstring(`$(cmd) $(icefloepath)$(cmd_post)`) == icefloechecksum +@test readstring(`$(cmd) $(oceanpath)$(cmd_post)`) == oceanchecksum + +SeaIce.removeSimulationFiles(sim) + +info("Testing VTK write during run!()") +SeaIce.setOutputFileInterval!(sim, 1e-9) +SeaIce.setTotalTime!(sim, 1.5) +SeaIce.setTimeStep!(sim) +sim.file_number = 0 +SeaIce.run!(sim, single_step=true) + +@test readstring(`$(cmd) $(icefloepath)$(cmd_post)`) == icefloechecksum +@test readstring(`$(cmd) $(oceanpath)$(cmd_post)`) == oceanchecksum + +SeaIce.removeSimulationFiles(sim) +""" diff --git a/test/runtests.jl b/test/runtests.jl @@ -10,6 +10,7 @@ include("collision-2floes-oblique.jl") include("cohesion.jl") include("netcdf.jl") include("vtk.jl") +include("jdl.jl") include("grid.jl") include("ocean.jl") include("atmosphere.jl")