Skip to content

Commit

Permalink
Add Julia 1.11 public keyword
Browse files Browse the repository at this point in the history
- define public API as list of public methods etc in PALEOboxes.jl
- move deprecated methods to new deprecated.jl file
- remove unused parameters Vector in ReactionBase (duplicates pars::ParametersTuple field)
  and replace add_par method with and error message (only used within PALEOboxes.jl)
  • Loading branch information
sjdaines committed Jan 14, 2025
1 parent a41cb13 commit d1529a7
Show file tree
Hide file tree
Showing 32 changed files with 222 additions and 125 deletions.
2 changes: 0 additions & 2 deletions docs/src/DomainsVariablesFields.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ allocate_values
check_values
zero_values!
dof_values
get_values_output
init_values!
copyfieldto!
Expand Down
1 change: 0 additions & 1 deletion docs/src/Reaction API.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Parameter
VecParameter
VecVecParameter
ParametersTuple
add_par
setvalue!
```

Expand Down
1 change: 0 additions & 1 deletion docs/src/Solver API.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,4 @@ CurrentModule = PALEOboxes
```@docs
set_data!
get_data
get_data_output
```
2 changes: 1 addition & 1 deletion src/Domain.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Infiltrator

"""
Domain
Expand Down
11 changes: 1 addition & 10 deletions src/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ Defines a Data type that can be composed with an [`AbstractSpace`](@ref) to form
Concrete subtypes should implement:
[`allocate_values`](@ref), [`check_values`](@ref), [`zero_values!`](@ref), [`dof_values`](@ref),
[`get_values_output`](@ref)
[`allocate_values`](@ref), [`check_values`](@ref), [`zero_values!`](@ref), [`dof_values`](@ref)
If the subtype needs to provide values for a numerical solver (eg as a state variable), it also needs to implement:
Expand Down Expand Up @@ -326,10 +325,6 @@ Convert Field `values` to a Vector of components
"""
function get_components(values, FieldData::Type{<:AbstractData}) end

"Optional: sanitize `values` for storing as model output.
Default implementation is usually OK - only implement for custom types that should be converted to standard types for storage"
get_values_output(values, data_type::Type{<:AbstractData}, data_dims::Tuple{Vararg{NamedDimension}}, Space, mesh) = values



################################################################
Expand Down Expand Up @@ -470,10 +465,6 @@ function add_field_vec!(dest::Field{FieldData, Space, V, N, Mesh}, a, cellrange,
return add_field_vec!(dest.values, FieldData, dest.data_dims, Space, a, cellrange, srcvalues, soff)
end

"sanitized version of `values`, suitable for storing as output"
function get_values_output(field::Field{FieldData, Space, V, N, Mesh}) where {FieldData, Space, V, N, Mesh}
return get_values_output(field.values, FieldData, field.data_dims, Space, field.mesh)
end


# get values from `linkvar_field`, optionally applying view defined by `linksubdomain`
Expand Down
11 changes: 8 additions & 3 deletions src/Grids.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
module Grids

import NCDatasets
import PALEOboxes as PB

import Infiltrator # Julia debugger
import ...PALEOboxes as PB
using ...PALEOboxes: @public

@public available_spaces
@public create_default_cellrange, get_tiled_cellranges
@public BoundarySubdomains, InteriorSubdomain, set_subdomain!, get_subdomain
@public UnstructuredVectorGrid, UnstructuredColumnGrid, CartesianLinearGrid, CartesianArrayGrid
@public cartesian_to_internal, internal_to_cartesian, substitute_cell_names, column_indices

###########################
# Subdomains
Expand Down
1 change: 0 additions & 1 deletion src/Model.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Infiltrator

"""
Model
Expand Down
94 changes: 80 additions & 14 deletions src/PALEOboxes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,84 @@ import Atomix
import PrecompileTools
import TimerOutputs: @timeit, @timeit_debug

# https://discourse.julialang.org/t/is-compat-jl-worth-it-for-the-public-keyword/119041/22
# define @public for backwards compatibility with Julia < v1.11
macro public(ex)
if VERSION >= v"1.11"
args = ex isa Symbol ? (ex,) : Base.isexpr(ex, :tuple) ? ex.args : error("@public parse error")
esc(Expr(:public, args...))
else
nothing
end
end

@public DocStrings

@public AbstractModel, Model, create_model_from_config, check_variable_links, check_ready, initialize_reactiondata!
@public DispatchMethodLists, dispatch_setup, create_dispatch_methodlists, do_deriv, dispatch_methodlist
@public get_num_domains, show_methods_setup, show_methods_initialize, show_methods_do, show_variables, show_links, show_parameters
@public get_data, get_table

@public AbstractDomain, get_domain, AbstractSubdomain, set_data_dimension!, get_data_dimension

@public VariableDomain, VariableDomPropDep, VariableDomContribTarget, set_data!, allocate_variables!
@public VariableAggregator, VariableAggregatorNamed, ParameterAggregator

@public NamedDimension, get_dimensions, get_dimension, set_coordinates!, get_coordinates

@public AbstractMesh, AbstractMeshOrNothing, AbstractSubdomain, get_mesh, has_internal_cartesian, internal_size, cartesian_size, Grids

@public AbstractCellRange, create_default_cellrange, CellRange, CellRangeColumns

@public Reservoirs, Fluxes, FluxPerturb, Forcings, GridForcings, GridReactions

@public AbstractReaction, ReactionBase, get_reaction, set_model_geometry, register_methods!, register_dynamic_methods!,
add_method_setup!, add_method_initialize!, add_method_do!, create_reaction
@public show_all_reactions, doc_reaction

@public AbstractParameter, Parameter, ParametersTuple,
ParBool, ParDouble, ParInt, ParString, ParEnum, ParDoubleVec, ParDoubleVecVec, ParIntVec, ParStringVec,
setvalue!, setvalueanddefault!, setfrozen!, set_parameter_value!, get_parameter_value

@public VariableBase, show_variable, get_variable, get_variables, has_variable, get_reaction_variables
@public VariableType, VariableFunction, VariablePhase
@public Attribute, StandardAttributes, get_attribute, set_attribute!, has_attribute, set_variable_attribute!, get_variable_attribute
@public VariableReaction,
VarProp, VarPropScalar, VarPropStateIndep, VarPropScalarStateIndep,
VarDep, VarDepColumn, VarDepScalar, VarDepStateIndep, VarDepColumnStateIndep, VarDepScalarStateIndep,
VarTarget, VarTargetScalar,
VarContrib, VarContribColumn, VarContribScalar,
VarStateExplicit, VarStateExplicitScalar,
VarTotal, VarTotalScalar,
VarStateTotal, VarStateTotalScalar,
VarDeriv, VarDerivScalar,
VarState, VarStateScalar,
VarConstraint, VarConstraintScalar
@public AbstractVarList, Varlist_single, VarList_namedtuple, VarList_namedtuple_fields, VarList_components, VarList_tuple, VarList_vector,
VarList_ttuple, VarList_vvector, VarList_nothing, VarList_tuple_nothing, VarList_fields

@public AbstractReactionMethod, ReactionMethod
@public add_method_setup_initialvalue_vars_default!, add_method_initialize_zero_vars_default!, add_method_do_totals_default!
@public RateStoich, create_ratestoich_method, parse_number_name
@public LinInterp, interp, value_ad, zero_ad

@public AbstractField, Field, get_field, add_field!

@public AbstractSpace, ScalarSpace, CellSpace, ColumnSpace

@public AbstractData, allocate_values, check_values, check_data_type, init_values!, zero_values!, dof_values!,
copyfieldto!, copytofield!, add_field_vec!, num_components, get_components
@public UndefinedData, ScalarData, AbstractIsotopeScalar, IsotopeLinear, ArrayScalarData
@public isotope_totaldelta, get_total, get_delta

@public AbstractModelData, ModelData, create_modeldata, add_arrays_data!

@public Constants, TestUtils, IteratorUtils, ChemistryUtils, DocStrings, SIMDutils

@public collate_markdown, precompile_reaction, run_model



include("utils/DocStrings.jl")

include("Types.jl")
Expand All @@ -47,7 +125,7 @@ include("VariableReaction.jl")
include("VariableDomain.jl")

include("Parameter.jl")
include("ParameterAggregator.jl")
include("variableaggregators/ParameterAggregator.jl")

include("ReactionMethodSorting.jl")
include("Model.jl")
Expand Down Expand Up @@ -76,20 +154,8 @@ include("variableaggregators/VariableAggregator.jl")

include("reactioncatalog/Reactions.jl")

# Deprecated functions
"""
get_statevar
DEPRECATED - moved to PALEOmodel
"""
function get_statevar end
include("deprecated.jl")

"""
get_statevar_norm
DEPRECATED - moved to PALEOmodel
"""
function get_statevar_norm end

#####################################################
# Precompilation
Expand Down
63 changes: 20 additions & 43 deletions src/Reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Derived types should implement [`register_methods!`](@ref), and may optionally i
[`set_model_geometry`](@ref), [`check_configuration`](@ref), [`register_dynamic_methods!`](@ref).
Methods should be registered using [`add_method_setup!`](@ref), [`add_method_initialize!`](@ref), [`add_method_do!`](@ref).
Any parameters not included in `pars` should be added explicitly with [`add_par`](@ref) (this is rarely needed).
"""
AbstractReaction

Expand All @@ -46,8 +44,6 @@ Base.@kwdef mutable struct ReactionBase
classname::String
"external parameters and values supplied from Model or Domain"
external_parameters::Dict{String, Any} = Dict{String, Any}()
"Reaction parameters"
parameters::Vector{AbstractParameter} = Vector{AbstractParameter}()

methods_setup::Vector{AbstractReactionMethod} = Vector{AbstractReactionMethod}()
methods_initialize::Vector{AbstractReactionMethod}=Vector{AbstractReactionMethod}()
Expand Down Expand Up @@ -101,20 +97,29 @@ end
##########################################################

"Get all parameters"
get_parameters(@nospecialize(reaction::AbstractReaction)) = reaction.base.parameters
function get_parameters(@nospecialize(reaction::AbstractReaction))
if hasfield(typeof(reaction), :pars)
all_parameters = AbstractParameter[v for v in getfield(reaction, :pars)::NamedTuple]
else
all_parameters = AbstractParameter[]
end

return all_parameters
end

"Get parameter by name"
function get_parameter(@nospecialize(reaction::AbstractReaction), parname::AbstractString; allow_not_found=false)
matchpars = filter(p -> p.name==parname, reaction.base.parameters)

length(matchpars) <= 1 ||
error("coding error: duplicate parameter name '$(name)' for Reaction: ", reaction)

!isempty(matchpars) || allow_not_found ||
error("configuration error, Reaction $(fullname(reaction)) $(reaction)\n",
"has no parameter name='$(parname)' (available parameters $([p.name for p in get_parameters(reaction)]))")
function get_parameter(reaction::AbstractReaction, parname::AbstractString; allow_not_found=false)

return isempty(matchpars) ? nothing : matchpars[1]
if hasfield(typeof(reaction), :pars) && hasfield(typeof(reaction.pars), Symbol(parname))
par = getfield(reaction.pars, Symbol(parname))
else
allow_not_found ||
error("configuration error, Reaction $(fullname(reaction)) $(reaction)\n",
"has no parameter name='$(parname)' (available parameters $([p.name for p in get_parameters(reaction)]))")
par = nothing
end

return par
end

set_parameter_value!(@nospecialize(reaction::AbstractReaction), parname::AbstractString, value) =
Expand Down Expand Up @@ -248,34 +253,6 @@ end
# Helper functions to allow a Reaction implementation to populate Parameter and ReactionMethod lists
#########################################################################################

"""
add_par(reaction::AbstractReaction, par::AbstractParameter)
add_par(reaction::AbstractReaction, objectwithpars)
Add a single parameter or parameters from fields of `objectwithpars` to a new Reaction.
Not usually needed: Parameters in `pars::ParametersTuple`` will be added automatically, only needed if there are additional
Parameters that are not members of `pars`.
"""
function add_par(@nospecialize(reaction::AbstractReaction), par::AbstractParameter)

if isnothing(get_parameter(reaction, par.name, allow_not_found=true))
push!(reaction.base.parameters, par)
else
error("attempt to add duplicate parameter name=''", par.name, "'' to reaction", reaction)
end

return nothing
end

function add_par(@nospecialize(reaction::AbstractReaction), objectwithpars)
for f in fieldnames(typeof(objectwithpars))
if getfield(objectwithpars, f) isa AbstractParameter
add_par(reaction, getfield(objectwithpars, f))
end
end
end


"""
add_method_setup!(reaction::AbstractReaction, method::AbstractReactionMethod)
Expand Down
11 changes: 2 additions & 9 deletions src/ReactionFactory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,15 @@ end
Create and configure a reaction.
Sets `ReactionBase` with name, classname, external_parameters, and list of `Parameters` from `pars` field (if present)
Sets `ReactionBase` with name, classname, external_parameters
"""
function create_reaction(
ReactionType::Type{<:AbstractReaction}, name::String, external_parameters::Dict{String, Any};
classname=_classname(ReactionType),
)
base=ReactionBase(;name, classname, external_parameters)
rj = create_reaction(ReactionType, base)
# Add parameters from pars field
if hasproperty(rj, :pars)
add_par(rj, rj.pars)
end

return rj
end

Expand All @@ -126,10 +123,6 @@ function create_reaction(
end


function add_reaction_factory(ReactionType::Type{<:AbstractReaction})
Base.depwarn("call to deprecated add_reaction_factory($ReactionType), this does nothing and can be removed", :add_reaction_factory, force=true)
end

"""
show_all_reactions(classfilter="", typenamefilter="")
Expand Down
8 changes: 0 additions & 8 deletions src/VariableDomain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,7 @@ Get Variable `var` data array from [`Field`](@ref).values
get_data(var::VariableDomain, modeldata::AbstractModelData, arrays_idx::Int=1) = get_field(var, modeldata, arrays_idx).values
get_data(var::VariableDomain, domaindata::AbstractDomainData) = get_field(var, domaindata).values

"""
get_data_output(var::VariableDomain, modeldata::AbstractModelData, arrays_idx::Int) -> get_values_output(field.values)
get_data_output(var::VariableDomain, domaindata::AbstractDomainData) -> get_values_output(field.values)

Get a sanitized version of Variable `var` data array for storing as output
from [`get_values_output`]@ref)`(`[`Field`](@ref).values`)`
"""
get_data_output(var::VariableDomain, modeldata::AbstractModelData, arrays_idx::Int=1) = get_values_output(get_field(var, modeldata, arrays_idx))
get_data_output(var::VariableDomain, domaindata::AbstractDomainData) = get_values_output(get_field(var, domaindata))

####################################################################
# Create and add to Domain
Expand Down
1 change: 0 additions & 1 deletion src/VariableReaction.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# import Infiltrator

#############################################
# VariableReaction
Expand Down
1 change: 0 additions & 1 deletion src/data/IsotopeData.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# import Infiltrator

import StructArrays

Expand Down
Loading

0 comments on commit d1529a7

Please sign in to comment.