BradyPlanden / LiiBRA.jl

Create reduced-order state-space models for lithium-ion batteries utilising realisation algorithms.
MIT License
32 stars 6 forks source link

Domain error in Simulate #48

Open DarioSlaifsteinSk opened 1 month ago

DarioSlaifsteinSk commented 1 month ago

Hi! I'm getting

DomainError with -2.8947206947632367:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.

from line 224 in Simulate.jl. This is probably because one of the concentrations Cseₙ, Ce or (cs_max .- Cseₙ) is going negative.

DarioSlaifsteinSk commented 3 weeks ago

So, apparently the (cs_max .- Cseₙ) is going negative which means that the following is not working properly:

Results.Cseₙ[i + 1, :] = (SOCₙ .* Cell.Neg.cs_max .+
                                  Results.y[i + 1, CseNegInd]) >
                                 ones(size(Results.Cseₙ, 2)) * Cell.Neg.cs_max ?
                                 ones(size(Results.Cseₙ, 2)) * Cell.Neg.cs_max :
                                 (SOCₙ .* Cell.Neg.cs_max .+
                                  Results.y[i + 1, CseNegInd])
DarioSlaifsteinSk commented 3 weeks ago

Two possible fixes. If we want to keep replacing the whole row:

 Results.Cseₙ[i + 1, :] = any((SOCₙ .* Cell.Neg.cs_max .+
                                   Results.y[i + 1, CseNegInd]) .> Cell.Neg.cs_max) ?
                                  ones(size(Results.Cseₙ, 2)) * Cell.Neg.cs_max :
                                  (SOCₙ .* Cell.Neg.cs_max .+
                                   Results.y[i + 1, CseNegInd])

If we only want to replace the values out of the bounds:

# first define the concentration
Results.Cseₙ[i + 1, :] = SOCₙ .* Cell.Neg.cs_max .+ Results.y[i + 1, CseNegInd]
# then check and correct
any(Results.Cseₙ[i + 1, :] .> Cell.Neg.cs_max) ?
             Results.Cseₙ[i+1, Results.Cseₙ[i + 1, :] .> Cell.Neg.cs_max] .= Cell.Neg.cs_max :
             nothing

Was there any reason to replace the whole row?