Skip to content

Commit

Permalink
Added code to handle inputs with PackageCompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
markowkes committed Jul 26, 2024
1 parent 96606f1 commit 0e0cda8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README_CompilingApp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Steps to compile app

>> cd "directory containing Biofilm"
>> julia -q --project
julia> using PackageCompiler
julia> create_app("Biofilm","BiofilmCompiled",force=true,include_transitive_dependencies=false)
10 changes: 10 additions & 0 deletions src/Biofilm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ include("computes.jl")
include("postprocess.jl")
include("precompile.jl")

function julia_main()::Cint

# Load and process input file
p = process_command_line_args(ARGS)

# Run solver
t,zm,Xt,St,Pb,Sb,Lf,sol = BiofilmSolver(p)

return 0 # if things finished successfully
end

end
62 changes: 62 additions & 0 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,66 @@ function checkParameters(p)
rem(plotPeriod,outPeriod) 0.0 || paramError("plotPeriod should be a multiple of outPerid.")

return nothing
end


"""
Reads a file (ARGS[1]), extracts p, and processes p
"""
function process_command_line_args(ARGS)

# Check ARGS
length(ARGS) >= 1 || error("Provide name of paramater file as command line argument e.g. `>> julia inputs.jl`")

# Assume filename is the first arguement
filename = ARGS[1]

# Load file containing parameters
include(filename)

# Check file loaded parameter variable p
(@isdefined p) || error("Parameter file should contain a NamedTuple with name `p`")
(p isa NamedTuple) || error("Parameter file contains p, but p is not a NamedTuple")

# Copy variables into new tuple and process functions
d = NamedTuple()
for (key,val) in zip(keys(p), p)
# Check if this parameter contains a vector
if val isa Vector
# Process vector entries
val_vec = Any[]
for n in eachindex(val)
# Process each entry in the vector
push!(val_vec,process_var(val[n]))
end
# Copy entries into d
d = merge(d, NamedTuple{(key,)}((val_vec,)))
else
# Process value and copy into d
d = merge(d, NamedTuple{(key,)}((process_var(val),)))
end
end
return d
end

"""
Takes a variable, and makes functions executable
"""
function process_var(var)
if var isa Function
# Determine number of function arguments
m = first(methods(var))
n = m.nargs - 1 # remove 1 because includes function name as 1st arg
if n==0; return ( ) -> Base.invokelatest(var, )
elseif n==1; return (a ) -> Base.invokelatest(var, a )
elseif n==2; return (a,b ) -> Base.invokelatest(var, a,b )
elseif n==3; return (a,b,c ) -> Base.invokelatest(var, a,b,c )
elseif n==4; return (a,b,c,d ) -> Base.invokelatest(var, a,b,c,d )
elseif n==5; return (a,b,c,d,e ) -> Base.invokelatest(var, a,b,c,d,e )
elseif n==6; return (a,b,c,d,e,f) -> Base.invokelatest(var, a,b,c,d,e,f)
else; error("Processing a function with $n arguments is not programmed")
end
else
return var
end
end

0 comments on commit 0e0cda8

Please sign in to comment.