Skip to content

Commit

Permalink
Merge branch 'dimensional' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
aabills authored Mar 2, 2023
2 parents 27b5d32 + 6fa206a commit 7ea2b47
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[deps]
pybamm = "23.1"
networkx = ""
casadi = ""
python = "=3.9"

[pip.deps]
pybamm = "@ git+https://github.com/pybamm-team/PyBaMM@develop"
qtconsole = ""
dfo-ls = ""
2 changes: 1 addition & 1 deletion pysrc/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __init__(
sim.built_model.concatenated_rhs, sim.built_model.concatenated_algebraic
)

self.timescale = sim.built_model.timescale
self.timescale = 1

self.len_cell_algebraic = sim.built_model.len_alg

Expand Down
19 changes: 16 additions & 3 deletions scripts/jl/pack_from_circuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ else
end


timescale = pyconvert(Float64,pybamm_pack.timescale.evaluate())
timescale = 1
cellconverter = pybamm2julia.JuliaConverter(cache_type = "symbolic", inplace=true)
cellconverter.convert_tree_to_intermediate(pybamm_pack.cell_model)
cell_str = cellconverter.build_julia_code()
Expand Down Expand Up @@ -105,9 +105,22 @@ cells = repeat(vcat(cell_rhs,cell_algebraic),pyconvert(Int, pybamm_pack.num_cell
thermals = trues(pyconvert(Int,pybamm_pack.len_thermal_eqs))
differential_vars = vcat(pack_eqs,cells, thermals)
mass_matrix = sparse(diagm(differential_vars))
func = ODEFunction(jl_func, mass_matrix=mass_matrix,jac_prototype=jac_sparsity)
func = ODEFunction(jl_func, mass_matrix=mass_matrix, jac_prototype=jac_sparsity)
prob = ODEProblem(func, jl_vec, (0.0, 3600/timescale), p)

using IncompleteLU
function incompletelu(W,du,u,p,t,newW,Plprev,Prprev,solverdata)
if newW === nothing || newW
Pl = ilu(convert(AbstractMatrix,W), τ = 50.0)
else
Pl = Plprev
end
Pl,nothing
end


Base.eltype(::IncompleteLU.ILUFactorization{Tv,Ti}) where {Tv,Ti} = Tv


sol = solve(prob, QNDF(linsolve=KLUFactorization(),concrete_jac=true), saveat = collect(range(0,stop=3600/timescale, length=100)))
sol = solve(prob, QNDF(linsolve=KrylovJL_GMRES(),precs=incompletelu,concrete_jac=true), save_everystep=false)

12 changes: 6 additions & 6 deletions scripts/pack_prototyping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pack = PyBaMM.pack
pybamm2julia = PyBaMM.pybamm2julia
setup_circuit = PyBaMM.setup_circuit

Np = 5
Ns = 5
Np = 3
Ns = 3
curr = 1.8
p = nothing
t = 0.0
Expand All @@ -16,11 +16,11 @@ model = pybamm.lithium_ion.DFN(name="DFN", options=options)
netlist = setup_circuit.setup_circuit(Np, Ns, I=curr)


pybamm_pack = pack.Pack(model, netlist, functional=functional, thermal=true, thermal_type="legacy", top_bc="symmetry")
pybamm_pack = pack.Pack(model, netlist, functional=functional, thermals=true, thermal_type="legacy", top_bc="symmetry")
pybamm_pack.build_pack()

#=
timescale = pyconvert(Float64,pybamm_pack.timescale.evaluate())

timescale = 1
cellconverter = pybamm2julia.JuliaConverter(cache_type = "symbolic", inplace=true)
cellconverter.convert_tree_to_intermediate(pybamm_pack.cell_model)
cell_str = cellconverter.build_julia_code()
Expand Down Expand Up @@ -76,4 +76,4 @@ prob = ODEProblem(func, jl_vec, (0.0, 3600/timescale), nothing)


sol = solve(prob, QNDF(linsolve=KLUFactorization(),concrete_jac=true), saveat = collect(range(0,stop=3600/timescale, length=100)))
=#

2 changes: 1 addition & 1 deletion src/diffeq_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function _problem_setup(sim, tend, inputs;dae_type="implicit",preallocate=true,c
end

# Scale the time
tau = pyconvert(Float64,sim.built_model.timescale.evaluate())
tau = 1
tspan = (0, tend/tau)


Expand Down
17 changes: 9 additions & 8 deletions test/test_caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using OrdinaryDiffEq


pybamm=pyimport("pybamm")
np = pyimport("numpy")

@testset "Dual Cache ODE" begin
# load model
Expand All @@ -18,10 +19,10 @@ pybamm=pyimport("pybamm")

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = sol.t

# Solve in python
sol_pybamm = sim.solve(t)
sol_pybamm = sim.solve(np.array(sol.t))
V_pybamm = pyconvert(Array{Float64},get(sol_pybamm, "Terminal voltage [V]",nothing).data)
@test all(isapprox.(V_pybamm, V, atol=1e-2))
end
Expand All @@ -39,10 +40,10 @@ end

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = sol.t

# Solve in python
sol_pybamm = sim.solve(t)
sol_pybamm = sim.solve(np.array(sol.t))
V_pybamm = pyconvert(Array{Float64},get(sol_pybamm, "Terminal voltage [V]",nothing).data)
@test all(isapprox.(V_pybamm, V, atol=1e-2))
end
Expand All @@ -59,10 +60,10 @@ end

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = sol.t

# Solve in python
sol_pybamm = sim.solve(t)
sol_pybamm = sim.solve(np.array(sol.t))
V_pybamm = pyconvert(Array{Float64},get(sol_pybamm, "Terminal voltage [V]",nothing).data)
@test all(isapprox.(V_pybamm, V, atol=1e-2))
end
Expand All @@ -78,10 +79,10 @@ end

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = sol.t

# Solve in python
sol_pybamm = sim.solve(t)
sol_pybamm = sim.solve(np.array(sol.t))
V_pybamm = pyconvert(Array{Float64},get(sol_pybamm, "Terminal voltage [V]",nothing).data)
@test all(isapprox.(V_pybamm, V, atol=1e-2))
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pybamm = pyimport("pybamm")
model = pybamm.lithium_ion.SPMe(name="SPMe")
sim = pybamm.Simulation(model)
prob,cbs = get_ode_problem(sim)
prob = remake(prob,tspan=(0,0.5))
prob = remake(prob,tspan=(0,7200))
event_to_test = sim.built_model.events[3]
problem_size = length(prob.u0)
sol = solve(prob,Rodas5(autodiff=false),callback=cbs)
Expand Down
11 changes: 6 additions & 5 deletions test/test_full_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Sundials
using OrdinaryDiffEq

pybamm = pyimport("pybamm")
np = pyimport("numpy")

@testset "Compare with PyBaMM: SPM" begin
# load model
Expand All @@ -17,7 +18,7 @@ pybamm = pyimport("pybamm")

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = np.array(sol.t)

# Solve in python
sol_pybamm = sim.solve(t)
Expand All @@ -36,12 +37,12 @@ end

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = np.array(sol.t)

# Solve in python
sol_pybamm = sim.solve(t)
V_pybamm = pyconvert(Array{Float64},get(sol_pybamm, "Terminal voltage [V]",nothing).data)
@test all(isapprox.(V_pybamm, V, atol=1e-4))
@test all(isapprox.(V_pybamm, V, atol=1e-3))
end

@testset "Compare with PyBaMM: DFN" begin
Expand All @@ -55,7 +56,7 @@ end

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = np.array(sol.t)

# Solve in python
sol_pybamm = sim.solve(t)
Expand All @@ -75,7 +76,7 @@ end

# Calculate voltage in Julia
V = get_variable(sim, sol, "Terminal voltage [V]")
t = get_variable(sim, sol, "Time [s]")
t = np.array(sol.t)

# Solve in python
sol_pybamm = sim.solve(t)
Expand Down
4 changes: 2 additions & 2 deletions test/test_pack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using PyBaMM
pybamm_pack.build_pack()


timescale = pyconvert(Float64,pybamm_pack.timescale.evaluate())
timescale = 1
cellconverter = pybamm2julia.JuliaConverter(cache_type = "symbolic", inplace=true)
cellconverter.convert_tree_to_intermediate(pybamm_pack.cell_model)
cell_str = cellconverter.build_julia_code()
Expand Down Expand Up @@ -122,7 +122,7 @@ end

pybamm_pack = pack.Pack(model, circuit_graph, functional=functional, thermals=thermals, operating_mode = "CV")
pybamm_pack.build_pack()
timescale = pyconvert(Float64,pybamm_pack.timescale.evaluate())
timescale = 1
cellconverter = pybamm2julia.JuliaConverter(cache_type = "symbolic", inplace=true)
cellconverter.convert_tree_to_intermediate(pybamm_pack.cell_model)
cell_str = cellconverter.build_julia_code()
Expand Down

0 comments on commit 7ea2b47

Please sign in to comment.