Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InPartS.jl - Agents.jl interop #1097

Open
JonasIsensee opened this issue Oct 19, 2024 · 0 comments
Open

InPartS.jl - Agents.jl interop #1097

JonasIsensee opened this issue Oct 19, 2024 · 0 comments
Labels
support Question about usage or clarity of features/documentation

Comments

@JonasIsensee
Copy link
Member

JonasIsensee commented Oct 19, 2024

Hi,
I tried to implement the API glue for InPartS.jl with Agents.jl.
I made some progress but also encountered the following things:

  • Plotting seems to rely on agent.pos to give the position. InPartS uses :centerpos. So I had to implement a wrapper type.
    I (of course) need to implement a Model wrapper type as well. This seems to need to have be of the form InPartSModel{Agents.ContinuousSpace{2}} <: Agents.AgentBasedModel{ContinuousSpace{2}}

  • I don't understand how time works in Agents. The data-collection for abmexploration appears to implicitly require time::Int (?) unless it is event-based but conceptually our models just have continuous time (adaptive time steppers)
    As a consequence, what should I do to add something like the time stepping API?

  • side remark: In InPartS we found that is way more efficient if we provide self-generated meshes to Makie (rather than providing polygons)

  • In InPartS, there is by default, no mutable global set of parameters. I'm wondering if that defeats most of the purpose of the interactive tools.

Anyway, Here"s my WIP

using InPartS, Agents
using InPartSBiome
using GLMakie, InPartSMakie
using Random

## Agents Model APINothing

mutable struct InPartSModelv4{S} <: Agents.AgentBasedModel{S}
    sim::InPartS.Simulation
end
InPartSModel = InPartSModelv4{Agents.ContinuousSpace{2}}


mutable struct AgentWrapper <: Agents.AbstractAgent
    cell::InPartS.AbstractParticle
end
function Base.getproperty(a::AgentWrapper, s::Symbol)
    cell = getfield(a, :cell)
    if s == :pos
        return cell.centerpos
    end
    getproperty(cell, s)
end
Agents.abmproperties(model::InPartSModel) = nothing

Agents.abmrng(model::InPartSModel) = model.sim.rng

function Agents.abmspace(model::InPartSModel) 
    domain =  getfield(model, :sim).domain
    ContinuousSpace(domainsize(domain)) 
end
Agents.abmtime(model::InPartSModel) = getfield(model, :sim).step

Agents.agent_container(model::InPartSModel) = AgentWrapper.(particles(getfield(model, :sim)))

Agents.agents_space_dimensionality(::Domain2D) = 2

Agents.agenttype(model::InPartSModel) = AgentWrapper#InPartS.particletype(getfield(model, :sim))

#nextid(model::InPartSModel) = model.sim.nextid
function Agents.add_agent_to_model!(model::InPartSModel, agent)
    push!(sim._addition_buffer, agent)
    InPartS.flushadditions!(sim)
    return agent.id
end

function Agents.remove_agent_from_model!(agent, model::InPartSModel)
    push!(sim._deletion_buffer, agent.id)
    InPartS.flushdeletions!(sim)
end

mod = Base.get_extension(Agents, :AgentsVisualizations)
mod._default_dts_from_model(::InPartSModel) = 0.01:0.01:0.1

using Statistics: mean
black(a) = a.growthrate < 1.0
white(a) = a.growthrate >= 1.0
adata = [(black, count), (white, count)]
temperature(model) = length(particles(getfield(model,:sim)))#mean(model.temperature)
mdata = [temperature]
fig, abmobs = abmexploration(ipsmodel;
    agent_color=:blue,
    agent_size = 20,
    agent_marker = 'o',
    agentsplotkwargs = (; strokewidth = 1.0,),
    adata, alabels = ["Black daisys", "White daisys"],
    mdata, mlabels = ["T"]
)


##

sim = Simulation(
    SimpleParticleContainer{2, RodCell{AreaMobility}}();
    domainsize = (30, 15),
    boundaries = (PeriodicBoundary, Boundary),
    rng = Random.Xoshiro(42)
)

addparticle!(sim;
    centerpos = SA[20.0, 10.0],
    φ = π*rand(sim.rng),
    params = RodCellParams(maxlength = 3, youngsmodulus = 1e4)
)
InPartS.flushadditions!(sim)
evolve!(sim, 1.0, tss = SimpleAdaptiveStepper(0.01))


ipsmodel = InPartSModel(sim)


fig, ax, abmobs = abmplot(ipsmodel; add_controls = true, agent_marker='o')
fig

# integration using InPartS
dxmax = 0.005
dtmax = 0.01
evolve!(sim, 0.1; tss = SimpleAdaptiveStepper(dxmax, dtmax = dtmax))

# Regular InPartSMakie plot for reference
fig = Figure();
ax = Axis(fig[1,1], aspect = DataAspect())
plt = plot!(ax, sim, particles = (;colorfunction = InPartSBiome.orientationcolor))
fig
@Tortar Tortar added example-integration A new example or showcase of integration with the Julia ecosystem support Question about usage or clarity of features/documentation and removed example-integration A new example or showcase of integration with the Julia ecosystem labels Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Question about usage or clarity of features/documentation
Projects
None yet
Development

No branches or pull requests

2 participants