Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/literate/game_of_life.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ match_coords(birth, G)
# This is easy! Pass in our model and our initial state. We optionally could
# limit the run via some maximum number of steps or time, but this one will
# achieve steady state within 3 time steps.
res = run!(GoL_coords, G);
res = run!(GoL_coords, G; maxevent=100);
@test length(res) == 13

# ## View results
Expand Down
11 changes: 8 additions & 3 deletions src/ABMs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ Base.isempty(t::Traj) = isempty(t.events)

Base.length(t::Traj) = length(t.events)

const MAXEVENT = 100
const MAXEVENT = typemax(Int)

"""
Run an ABM, creating a fresh runtime + trajectory.
Expand All @@ -480,13 +480,18 @@ dt - timestep for checking discrete events when running ODE dynamics.
"""
function run!(abm::ABM, init::T; save=_->nothing, maxevent=MAXEVENT,
maxtime=Inf, kw...) where T<:ACSet
maxevent == typemax(Int) && isinf(maxtime) && error(
"run! requires at least one finite bound; specify maxevent and/or maxtime."
)
run!(abm::ABM, RuntimeABM(abm, init; kw...), Traj(init);
save, maxtime, maxevent)
end

function run!(abm::ABM, rt::RuntimeABM, output::Traj;
save=_->nothing, maxevent=MAXEVENT, maxtime=Inf, dt=0.1)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the default for maxevent should be typemax(Int) and the default for maxtime can be Inf. There should be an error in the first line if neither has been specified by the caller.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that, once this is done, any tests which do not supply maxevent or maxtime will be broken, and they would have to be changed (e.g. to add the kwarg maxevent=100) in order to work again.

maxevent = isinf(maxtime) ? maxevent : typemax(Int)
maxevent == typemax(Int) && isinf(maxtime) && error(
"run! requires at least one finite bound; specify maxevent and/or maxtime."
)
# Helper functions that automatically incorporate the runtime `rt`
getname(rule::Int)::String =
string(isnothing(abm.rules[rule].name) ? rule : abm.rules[rule].name)
Expand Down Expand Up @@ -606,4 +611,4 @@ function run!(abm::ABM, rt::RuntimeABM, output::Traj;
return output
end

end # module
end # module
19 changes: 17 additions & 2 deletions test/ABMs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,28 @@ push!(new_abm, do_nothing)

traj = run!(abm, G; maxevent=10);

traj = run!(ABM([rem_edge]), G);
traj = run!(ABM([rem_edge]), G; maxevent=10);
@test length(traj) == 3


traj = run!(ABM([add_loop]), G);
traj = run!(ABM([add_loop]), G; maxevent=10);
@test length(traj) > 3 # after we add a loop, the match persists + is resampled

let traj = run!(ABM([create_loop]), Graph(); maxevent=2, maxtime=10.0)
@test length(traj) == 2
@test traj.events[end][1] == 2.0
end

let err = try
run!(ABM([create_loop]), Graph())
nothing
catch e
e
end
@test err isa ErrorException
@test occursin("specify maxevent and/or maxtime", sprint(showerror, err))
end



# Test events in parallel
Expand Down