Skip to content
Snippets Groups Projects
Commit 7ab4db8b authored by xo30xoqa's avatar xo30xoqa
Browse files

Fixed output functions

parent c5877402
No related branches found
No related tags found
No related merge requests found
...@@ -173,7 +173,7 @@ function stepsimulation!(model::SimulationModel) ...@@ -173,7 +173,7 @@ function stepsimulation!(model::SimulationModel)
end end
updatenature!(model) updatenature!(model)
updateevents!(model) updateevents!(model)
#outputdata(model) #FIXME outputdata(model)
model.date += Day(1) model.date += Day(1)
model model
end end
......
...@@ -24,14 +24,13 @@ daily, monthly, yearly, or at the end of a simulation, depending on the paramete ...@@ -24,14 +24,13 @@ daily, monthly, yearly, or at the end of a simulation, depending on the paramete
`nature.popoutfreq`. `nature.popoutfreq`.
""" """
function savepopulationdata(model::SimulationModel) function savepopulationdata(model::SimulationModel)
#FIXME
pops = Dict{String,Int}(s=>0 for s = @param(nature.targetspecies)) pops = Dict{String,Int}(s=>0 for s = @param(nature.targetspecies))
for a in model.animals for a in model.animals
isnothing(a) && continue isnothing(a) && continue
pops[string(typeof(a))] += 1 pops[speciesof(a)] += 1
end end
for m in model.migrants for m in model.migrants
pops[string(typeof(m.first))] += 1 pops[speciesof(m.first)] += 1
end end
data = [] data = []
for p in keys(pops) for p in keys(pops)
...@@ -53,7 +52,7 @@ function saveindividualdata(model::SimulationModel) ...@@ -53,7 +52,7 @@ function saveindividualdata(model::SimulationModel)
data = [] data = []
for a in model.animals for a in model.animals
isnothing(a) && continue isnothing(a) && continue
push!(data, [model.date,a.id,a.pos[1],a.pos[2],a.traits["name"],a.sex,a.age]) push!(data, [model.date,a.id,a.pos[1],a.pos[2],speciesof(a),a.sex,a.age])
end end
data data
end end
......
...@@ -350,19 +350,7 @@ Return an iterator over all conspecific animals in the given radius around this ...@@ -350,19 +350,7 @@ Return an iterator over all conspecific animals in the given radius around this
excluding itself. This can only be used nested within [`@phase`](@ref). excluding itself. This can only be used nested within [`@phase`](@ref).
""" """
macro neighbours(args...) macro neighbours(args...)
:(neighbours($(esc(:self)), $(esc(:model)); $(map(esc, args)...))) :(neighbours($(esc(:self)), $(esc(:model)), $(map(esc, args)...)))
end
"""
@countanimals(radius=0, species="")
Count the number of animals of the given species in this location.
This is a utility wrapper that can only be used nested within
[`@phase`](@ref) or [`@habitat`](@ref).
"""
macro countanimals(args...)
#XXX do I want/need this?
:(countanimals($(esc(:pos)), $(esc(:model)); $(map(esc, args)...)))
end end
##TODO test movement macros ##TODO test movement macros
......
...@@ -39,17 +39,20 @@ function speciesof(a::Animal) ...@@ -39,17 +39,20 @@ function speciesof(a::Animal)
end end
""" """
speciestype(animal) speciestype(name)
Return the Type of this species. Return the Type of this species.
""" """
function speciestype(species::String) function speciestype(name::String)
# get the species Type from its namestring by looking in the module namespace # get the species Type from its namestring by looking in the module namespace
speciestype = nothing speciestype = nothing
for name in names(Persefone, all=true) for var in names(Persefone, all=true)
string(name) == species && (speciestype = getfield(Persefone, name)) if string(var) == name && getfield(Persefone, var) isa Type
speciestype = getfield(Persefone, var)
break
end
end end
isnothing(speciestype) && @error("Species $species is not defined.") isnothing(speciestype) && @error("Species $name is not defined.")
speciestype speciestype
end end
......
...@@ -214,26 +214,15 @@ function nearby_animals(pos::Tuple{Int64,Int64}, model::SimulationModel; ...@@ -214,26 +214,15 @@ function nearby_animals(pos::Tuple{Int64,Int64}, model::SimulationModel;
end end
""" """
neighbours(animal, model; radius=0) neighbours(animal, model, radius=0)
Return a list of conspecific animals in the given radius around this animal, excluding itself. Return a list of conspecific animals in the given radius around this animal, excluding itself.
""" """
function neighbours(animal::Animal, model::SimulationModel; radius::Int64=0) function neighbours(animal::Animal, model::SimulationModel, radius::Int64=0)
filter(a -> a.id != animal.id, filter(a -> a.id != animal.id,
nearby_animals(animal.pos, model, radius=radius, species=speciesof(animal))) nearby_animals(animal.pos, model, radius=radius, species=speciesof(animal)))
end end
"""
countanimals(pos, model; radius= 0, species="")
Count the number of animals in this location (optionally supplying a species name and radius).
"""
function countanimals(pos::Tuple{Int64,Int64}, model::SimulationModel;
radius::Int64=0, species="")
#XXX do I want/need this?
length(nearby_animals(pos, model, radius=radius, species=species))
end
""" """
followanimal!(follower, leader, model, distance=0) followanimal!(follower, leader, model, distance=0)
......
...@@ -27,8 +27,7 @@ and occasionally reproduce by spontaneous parthenogenesis... ...@@ -27,8 +27,7 @@ and occasionally reproduce by spontaneous parthenogenesis...
#walk!(animal, direction, model; ifempty=false) #walk!(animal, direction, model; ifempty=false)
end end
if @rand() < self.fecundity && if @rand() < self.fecundity && length(@neighbours(10)) < self.crowding
@countanimals(species="Wolpertinger") < self.crowding
@reproduce() @reproduce()
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment