Skip to content

Commit

Permalink
Delete unused functions + clean code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
VEZY committed Apr 4, 2024
1 parent d625e77 commit dced471
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 394 deletions.
13 changes: 12 additions & 1 deletion src/Abstract_model_structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ process(x::A) where {A<:AbstractModel} = process_(supertype(A))

# For the models given with their process name:
process(x::Pair{Symbol,A}) where {A<:AbstractModel} = first(x)
process_(x) = error("process() is not defined for $(x)")
process_(x) = error("process() is not defined for $(x)")

"""
model_(m::AbstractModel)
Get the model of an AbstractModel (it is the model itself if it is not a MultiScaleModel).
"""
model_(m::AbstractModel) = m
get_models(m::AbstractModel) = [model_(m)] # Get the models of an AbstractModel
# Note: it is returning a vector of models, because in this case the user provided a single model instead of a vector of.
get_status(m::AbstractModel) = nothing
get_mapping(m::AbstractModel) = Pair{Symbol,String}[]
7 changes: 6 additions & 1 deletion src/PlantSimEngine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ include("dependencies/soft_dependencies.jl")
include("dependencies/hard_dependencies.jl")
include("dependencies/dependencies.jl")

# UninitializedVar:
include("processes/uninitialized_var.jl")

# MTG compatibility:
include("mtg/GraphSimulation.jl")
include("mtg/mapping/getters.jl")
include("mtg/mapping/mapping.jl")
include("mtg/mapping/new_mapping.jl") #! to change in mapping.jl
include("mtg/mapping/compute_mapping.jl")
include("mtg/mapping/reverse_mapping.jl")
include("mtg/initialisation.jl")
include("mtg/save_results.jl")
include("mtg/add_organ.jl")
Expand Down
8 changes: 7 additions & 1 deletion src/mtg/MultiScaleModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@ end
MultiScaleModel(; model, mapping) = MultiScaleModel(model, mapping)

mapping_(m::MultiScaleModel) = m.mapping
model_(m::MultiScaleModel) = m.model
model_(m::MultiScaleModel) = m.model
inputs_(m::MultiScaleModel) = inputs_(m.model)
outputs_(m::MultiScaleModel) = outputs_(m.model)
get_models(m::MultiScaleModel) = [model_(m)] # Get the models of a MultiScaleModel:
# Note: it is returning a vector of models, because in this case the user provided a single MultiScaleModel instead of a vector of.
get_status(m::MultiScaleModel) = nothing
get_mapping(m::MultiScaleModel{T,S}) where {T,S} = mapping_(m)
43 changes: 43 additions & 0 deletions src/mtg/initialisation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,49 @@ function status_from_template(d::Dict{Symbol,T} where {T})
Status(NamedTuple(first(i) => ref_var(last(i)) for i in d))
end

"""
ref_var(v)
Create a reference to a variable. If the variable is already a `Base.RefValue`,
it is returned as is, else it is returned as a Ref to the copy of the value,
or a Ref to the `RefVector` (in case `v` is a `RefVector`).
# Examples
```jldoctest mylabel
julia> using PlantSimEngine;
```
```jldoctest mylabel
julia> PlantSimEngine.ref_var(1.0)
Base.RefValue{Float64}(1.0)
```
```jldoctest mylabel
julia> PlantSimEngine.ref_var([1.0])
Base.RefValue{Vector{Float64}}([1.0])
```
```jldoctest mylabel
julia> PlantSimEngine.ref_var(Base.RefValue(1.0))
Base.RefValue{Float64}(1.0)
```
```jldoctest mylabel
julia> PlantSimEngine.ref_var(Base.RefValue([1.0]))
Base.RefValue{Vector{Float64}}([1.0])
```
```jldoctest mylabel
julia> PlantSimEngine.ref_var(PlantSimEngine.RefVector([Ref(1.0), Ref(2.0), Ref(3.0)]))
Base.RefValue{PlantSimEngine.RefVector{Float64}}(RefVector{Float64}[1.0, 2.0, 3.0])
```
"""
ref_var(v) = Base.Ref(copy(v))
ref_var(v::T) where {T<:AbstractString} = Base.Ref(v) # No copy method for strings, so directly making a Ref out of it
ref_var(v::T) where {T<:Base.RefValue} = v
ref_var(v::T) where {T<:RefVector} = Base.Ref(v)

"""
init_simulation(mtg, mapping; nsteps=1, outputs=nothing, type_promotion=nothing, check=true, verbose=true)
Expand Down
File renamed without changes.
113 changes: 113 additions & 0 deletions src/mtg/mapping/getters.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
get_models(m)
Get the models of a dictionary of model mapping.
# Arguments
- `m::Dict{String,Any}`: a dictionary of model mapping
Returns a vector of models
# Examples
```jldoctest mylabel
julia> using PlantSimEngine;
```
Import example models (can be found in the `examples` folder of the package, or in the `Examples` sub-modules):
```jldoctest mylabel
julia> using PlantSimEngine.Examples;
```
If we just give a MultiScaleModel, we get its model as a one-element vector:
```jldoctest mylabel
julia> models = MultiScaleModel( \
model=ToyCAllocationModel(), \
mapping=[ \
:carbon_assimilation => ["Leaf"], \
:carbon_demand => ["Leaf", "Internode"], \
:carbon_allocation => ["Leaf", "Internode"] \
], \
);
```
```jldoctest mylabel
julia> PlantSimEngine.get_models(models)
1-element Vector{ToyCAllocationModel}:
ToyCAllocationModel()
```
If we give a tuple of models, we get each model in a vector:
```jldoctest mylabel
julia> models2 = ( \
MultiScaleModel( \
model=ToyAssimModel(), \
mapping=[:soil_water_content => "Soil",], \
), \
ToyCDemandModel(optimal_biomass=10.0, development_duration=200.0), \
Status(aPPFD=1300.0, TT=10.0), \
);
```
Notice that we provide "Soil", not ["Soil"] in the mapping because a single value is expected for the mapping here.
```jldoctest mylabel
julia> PlantSimEngine.get_models(models2)
2-element Vector{AbstractModel}:
ToyAssimModel{Float64}(0.2)
ToyCDemandModel{Float64}(10.0, 200.0)
```
"""
get_models(m) = [model_(i) for i in m if !isa(i, Status)]


# Same, for the status (if any provided):

"""
get_status(m)
Get the status of a dictionary of model mapping.
# Arguments
- `m::Dict{String,Any}`: a dictionary of model mapping
Returns a [`Status`](@ref) or `nothing`.
# Examples
See [`get_models`](@ref) for examples.
"""
function get_status(m)
st = Status[i for i in m if isa(i, Status)]
@assert length(st) <= 1 "Only one status can be provided for each organ type."
length(st) == 0 && return nothing
return first(st)
end

"""
get_mapping(m)
Get the mapping of a dictionary of model mapping.
# Arguments
- `m::Dict{String,Any}`: a dictionary of model mapping
Returns a vector of pairs of symbols and strings or vectors of strings
# Examples
See [`get_models`](@ref) for examples.
"""
function get_mapping(m)
mod_mapping = [mapping_(i) for i in m if isa(i, MultiScaleModel)]
if length(mod_mapping) == 0
return Pair{Symbol,String}[]
end
return reduce(vcat, mod_mapping) |> unique
end
Loading

0 comments on commit dced471

Please sign in to comment.