Skip to content

Commit

Permalink
Waitbar during model generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcBerliner committed Mar 23, 2022
1 parent 799e467 commit 77c0b91
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 20 deletions.
6 changes: 6 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ version = "0.2.0"
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[deps.ProgressMeter]]
deps = ["Distributed", "Printf"]
git-tree-sha1 = "afadeba63d90ff223a6a48d2009434ecee2ec9e8"
uuid = "92933f4c-e287-5a05-a399-4b506db050ca"
version = "1.7.1"

[[deps.QuadGK]]
deps = ["DataStructures", "LinearAlgebra"]
git-tree-sha1 = "78aadffb3efd2155af139781b8a8df1ef279ea39"
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PETLION"
uuid = "5e0a28e4-193c-47fa-bbb8-9c901cc1ac2c"
authors = ["Marc D. Berliner", "Richard D. Braatz"]
version = "0.2.4"
version = "0.2.5"

[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Expand All @@ -12,6 +12,7 @@ InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
KLU = "ef3ab10e-7fda-4108-b977-705223b18434"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PkgVersion = "eebad327-c553-4316-9ea0-9fa01ccd7688"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Expand All @@ -30,6 +31,7 @@ Dierckx = "0.5"
GeneralizedGenerated = "0.3"
IfElse = "0.1.0"
KLU = "0.3"
ProgressMeter = "1"
PkgVersion = "0.1"
RecipesBase = "1"
RecursiveArrayTools = "2"
Expand Down
1 change: 1 addition & 0 deletions src/PETLION.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using SpecialFunctions: erf
using SHA: sha1

import PkgVersion
import ProgressMeter
import SuiteSparse
import Sundials
import LinearAlgebra
Expand Down
6 changes: 3 additions & 3 deletions src/external.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ function state_indices(N, numerics)
return ind, N_diff, N_alg, N_tot
end

@inline function guess_init(p::AbstractModel, X_applied=0.0)
@inline function guess_init(p::AbstractModel, X_applied=0.0; SOC=p.opts.SOC)
"""
Get the initial guess in the DAE initialization.
This function is made symbolic by Symbolics and saved as
Expand All @@ -351,8 +351,8 @@ end

build_T!(states, p)

states[:c_s_avg].p .= p.θ[:c_max_p] * (p.opts.SOC*(p.θ[:θ_max_p] - p.θ[:θ_min_p]) + p.θ[:θ_min_p])
states[:c_s_avg].n .= p.θ[:c_max_n] * (p.opts.SOC*(p.θ[:θ_max_n] - p.θ[:θ_min_n]) + p.θ[:θ_min_n])
states[:c_s_avg].p .= p.θ[:c_max_p] * (SOC*(p.θ[:θ_max_p] - p.θ[:θ_min_p]) + p.θ[:θ_min_p])
states[:c_s_avg].n .= p.θ[:c_max_n] * (SOC*(p.θ[:θ_max_n] - p.θ[:θ_min_n]) + p.θ[:θ_min_n])

build_c_s_star!(states, p)

Expand Down
45 changes: 31 additions & 14 deletions src/generate_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ function load_functions_symbolic(p::AbstractModel)
## Jacobian
@load dir * "J_sp.jl" J_y_sp θ_keys
else
Y0Func,res_algFunc,res_diffFunc,jacFunc,jac_algFunc,J_y_sp,θ_keys = generate_functions_symbolic(p)
Y0Func,res_algFunc,res_diffFunc,jacFunc,jac_algFunc,J_y_sp,θ_keys,progress = generate_functions_symbolic(p)

initial_guess! = eval(Y0Func)
f_alg! = eval(res_algFunc)
f_diff! = eval(res_diffFunc)
J_y!_func = eval(jacFunc)
J_y_alg!_func = eval(jac_algFunc)
fillpercent!(progress, 5)

J_y!_func = eval(jacFunc)
fillpercent!(progress, 5)
ProgressMeter.finish!(progress)
end

J_y!_sp = sparse(J_y_sp...)
Expand All @@ -89,33 +93,43 @@ function load_functions_symbolic(p::AbstractModel)
return initial_guess!, f_alg!, f_diff!, J_y!, J_y_alg!, θ_keys
end

ProgressMeter.finish!(::Nothing;kw...) = nothing
fillpercent!(progress::ProgressMeter.Progress, n::Int) = (for _ in 1:(n*progress.n÷100); ProgressMeter.next!(progress); end)
fillpercent!(::Nothing,::Int) = nothing
function generate_functions_symbolic(p::AbstractModel; verbose=options[:SAVE_SYMBOLIC_FUNCTIONS])

if verbose println("Creating the functions for $p\n\nMay take a few minutes...") end

if verbose
println("Creating the functions for $p\n")
progress = ProgressMeter.Progress(200)
fillpercent!(progress, 1)
ProgressMeter.update!(progress)
else
progress = nothing
end

θ_sym, Y_sym, YP_sym, t_sym, SOC_sym, X_applied, γ_sym, p_sym, θ_keys = get_symbolic_vars(p)

## Y0 function
if verbose println("1/4: Making initial guess function") end
Y0_sym = _symbolic_initial_guess(p_sym, SOC_sym, θ_sym, X_applied)
fillpercent!(progress, 4)

## batteryModel function
if verbose println("2/4: Making symbolic sol") end
## PET_residuals function
res = _symbolic_residuals(p_sym, t_sym, Y_sym, YP_sym)
fillpercent!(progress, 15)

θ_sym_slim, θ_keys_slim = get_only_θ_used_in_model(θ_sym, θ_keys, res, Y0_sym)

Y0Func = build_function(Y0_sym, SOC_sym, θ_sym_slim, X_applied, fillzeros=false, checkbounds=false)[2]

## jacobian
if verbose println("3/4: Making symbolic Jacobian") end
Jac, jacFunc, J_sp = _symbolic_jacobian(p, res, t_sym, Y_sym, YP_sym, γ_sym, θ_sym, θ_sym_slim, verbose=verbose)
Jac, jacFunc, J_sp = _symbolic_jacobian(p, res, t_sym, Y_sym, YP_sym, γ_sym, θ_sym, θ_sym_slim, progress=progress)

if verbose println("4/4: Making initial condition functions") end
res_algFunc, res_diffFunc = _symbolic_initial_conditions_res(p, res, t_sym, Y_sym, YP_sym, θ_sym, θ_sym_slim)
fillpercent!(progress, 5)

jac_algFunc = _symbolic_initial_conditions_jac(p, Jac, t_sym, Y_sym, YP_sym, γ_sym, θ_sym_slim)

fillpercent!(progress, 10 + (options[:SAVE_SYMBOLIC_FUNCTIONS] ? 0 : 5))

J_y_sp = (findnz(J_sp)..., p.N.tot-1, p.N.tot)

θ_keys = θ_keys_slim
Expand All @@ -141,9 +155,10 @@ function generate_functions_symbolic(p::AbstractModel; verbose=options[:SAVE_SYM
@save dir * "J_sp.jl" J_y_sp θ_keys
end

if verbose println("Finished\n") end
fillpercent!(progress, (options[:SAVE_SYMBOLIC_FUNCTIONS] ? 5 : 0))
ProgressMeter.finish!(progress)

return Y0Func, res_algFunc, res_diffFunc, jacFunc, jac_algFunc, J_y_sp, θ_keys
return Y0Func, res_algFunc, res_diffFunc, jacFunc, jac_algFunc, J_y_sp, θ_keys, progress
end

function load_functions_forward_diff(p::AbstractModel)
Expand Down Expand Up @@ -266,20 +281,22 @@ function _symbolic_jacobian(p::AbstractModel=petlion(LCO);inds::T=1:p.N.tot) whe

return Jac
end
function _symbolic_jacobian(p::AbstractModel, res, t_sym, Y_sym, YP_sym, γ_sym, θ_sym, θ_sym_slim; verbose=false)
function _symbolic_jacobian(p::AbstractModel, res, t_sym, Y_sym, YP_sym, γ_sym, θ_sym, θ_sym_slim; progress=nothing)
J_sp, sp_x, sp_xp = _Jacobian_sparsity_pattern(p, res, Y_sym, YP_sym)

## symbolic jacobian
Jac_x = sparsejacobian_multithread(res, Y_sym; sp = sp_x, simplify=false)
Jac_xp = sparsejacobian_multithread(res, YP_sym; sp = sp_xp, simplify=false)

fillpercent!(progress, 25)
Jac = Jac_x
@inbounds for (i,j) in zip(Jac_xp.rowval,Jac_xp.colptr)
Jac[i,j] += γ_sym*Jac_xp[i,j]
end

# building the sparse jacobian
jacFunc = build_function(Jac.nzval, t_sym, Y_sym, YP_sym, γ_sym, θ_sym_slim)[2]
fillpercent!(progress, 25)

return Jac, jacFunc, J_sp
end
Expand Down
5 changes: 5 additions & 0 deletions src/model_evaluation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ end
end
=#
end

@inline function newtons_method!(p::model,Y,YP,run::AbstractRun,opts=p.opts;kw...)
funcs = p.funcs(run)
newtons_method!(p,Y,YP,run,opts,funcs.R_alg,funcs.R_diff,funcs.J_alg;kw...)
end
@inline function newtons_method!(p::model,Y::R1,YP::R1,run,opts::AbstractOptionsModel,R_alg::T1,R_diff::T2,J_alg::T3;
itermax::Int64=100, t::Float64=0.0
) where {R1<:Vector{Float64},T1<:residual_combined,T2<:residual_combined,T3<:jacobian_combined}
Expand Down
4 changes: 2 additions & 2 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ function time_units(t::Number)
return t, time_unit, time_scale
end

function C_rate_string(I::Number;digits::Int64=4)
function C_rate_string(I::Number;digits::Int64=4,show_sign::Bool=true)
I_rat = rationalize(Float64(I))
num = abs(I_rat.num)
den = I_rat.den
Expand All @@ -476,7 +476,7 @@ function C_rate_string(I::Number;digits::Int64=4)
return "$(round(I;digits=digits))C"
end

str = I < 0 ? "-" : ""
str = I < 0 && show_sign ? "-" : ""

if isone(num) && !isone(den)
str *= "C"
Expand Down

2 comments on commit 77c0b91

@MarcBerliner
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/57200

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.5 -m "<description of version>" 77c0b912d1f61c3ee1ae338214788fd931a761cf
git push origin v0.2.5

Please sign in to comment.