-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
69 changed files
with
2,229 additions
and
2,653 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
531 changes: 273 additions & 258 deletions
531
_sources/publishedExamples/battMoTutorial_source.rst.txt
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
392 changes: 203 additions & 189 deletions
392
_sources/publishedExamples/runBatteryP2D_source.rst.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,189 +1,203 @@ | ||
:orphan: | ||
|
||
.. _runBatteryP2D_source: | ||
|
||
Source code for runBatteryP2D | ||
----------------------------- | ||
|
||
.. code:: matlab | ||
%% Pseudo-Two-Dimensional (P2D) Lithium-Ion Battery Model | ||
% This example demonstrates how to setup a P2D model of a Li-ion battery | ||
% and run a simple simulation. | ||
% Clear the workspace and close open figures | ||
clear | ||
close all | ||
%% Import the required modules from MRST | ||
% load MRST modules | ||
mrstModule add ad-core mrst-gui mpfa agmg linearsolvers | ||
%% Setup the properties of Li-ion battery materials and cell design | ||
% The properties and parameters of the battery cell, including the | ||
% architecture and materials, are set using an instance of | ||
% :class:`BatteryInputParams <Battery.BatteryInputParams>`. This class is | ||
% used to initialize the simulation and it propagates all the parameters | ||
% throughout the submodels. The input parameters can be set manually or | ||
% provided in json format. All the parameters for the model are stored in | ||
% the inputparams object. | ||
jsonstruct = parseBattmoJson(fullfile('ParameterData','BatteryCellParameters','LithiumIonBatteryCell','lithium_ion_battery_nmc_graphite.json')); | ||
% We define some shorthand names for simplicity. | ||
ne = 'NegativeElectrode'; | ||
pe = 'PositiveElectrode'; | ||
elyte = 'Electrolyte'; | ||
thermal = 'ThermalModel'; | ||
co = 'Coating'; | ||
am = 'ActiveMaterial'; | ||
itf = 'Interface'; | ||
sd = 'SolidDiffusion'; | ||
ctrl = 'Control'; | ||
cc = 'CurrentCollector'; | ||
jsonstruct.use_thermal = false; | ||
jsonstruct.include_current_collectors = false; | ||
inputparams = BatteryInputParams(jsonstruct); | ||
use_cccv = false; | ||
if use_cccv | ||
cccvstruct = struct( 'controlPolicy' , 'CCCV', ... | ||
'initialControl' , 'discharging', ... | ||
'numberOfCycles' , 1 , ... | ||
'CRate' , 1 , ... | ||
'lowerCutoffVoltage', 2.4 , ... | ||
'upperCutoffVoltage', 4.1 , ... | ||
'dIdtLimit' , 0.01 , ... | ||
'dEdtLimit' , 0.01); | ||
cccvinputparams = CcCvControlModelInputParams(cccvstruct); | ||
inputparams.Control = cccvinputparams; | ||
end | ||
%% Setup the geometry and computational grid | ||
% Here, we setup the 1D computational grid that will be used for the | ||
% simulation. The required discretization parameters are already included | ||
% in the class BatteryGeneratorP2D. | ||
gen = BatteryGeneratorP2D(); | ||
% Now, we update the inputparams with the properties of the grid. | ||
inputparams = gen.updateBatteryInputParams(inputparams); | ||
%% Initialize the battery model. | ||
% The battery model is initialized by sending inputparams to the Battery class | ||
% constructor. see :class:`Battery <Battery.Battery>`. | ||
model = Battery(inputparams); | ||
inspectgraph = false; | ||
if inspectgraph | ||
cgt = model.computationalGraph; | ||
return | ||
end | ||
%% Compute the nominal cell capacity and choose a C-Rate | ||
% The nominal capacity of the cell is calculated from the active materials. | ||
% This value is then combined with the user-defined C-Rate to set the cell | ||
% operational current. | ||
CRate = model.Control.CRate; | ||
%% Setup the schedule | ||
% | ||
timestep.numberOfTimeSteps = 20; | ||
step = model.Control.setupScheduleStep(timestep); | ||
control = model.Control.setupScheduleControl(); | ||
% This control is used to set up the schedule | ||
schedule = struct('control', control, 'step', step); | ||
%% Setup the initial state of the model | ||
% The initial state of the model is setup using the model.setupInitialState() method. | ||
initstate = model.setupInitialState(); | ||
%% Setup the properties of the nonlinear solver | ||
nls = NonLinearSolver(); | ||
linearsolver = 'direct'; | ||
switch linearsolver | ||
case 'amgcl' | ||
nls.LinearSolver = AMGCLSolverAD('verbose', true, 'reduceToCell', false); | ||
nls.LinearSolver.tolerance = 1e-4; | ||
nls.LinearSolver.maxIterations = 30; | ||
nls.maxIterations = 10; | ||
nls.verbose = 10; | ||
case 'battery' | ||
nls.LinearSolver = LinearSolverBatteryExtra('verbose' , false, ... | ||
'reduceToCell', true, ... | ||
'verbosity' , 3 , ... | ||
'reuse_setup' , false, ... | ||
'method' , 'direct'); | ||
nls.LinearSolver.tolerance = 1e-4; | ||
case 'direct' | ||
disp('standard direct solver') | ||
otherwise | ||
error('Unknown solver %s', linearsolver); | ||
end | ||
% Change default maximum iteration number in nonlinear solver | ||
nls.maxIterations = 10; | ||
% Change default behavior of nonlinear solver, in case of error | ||
nls.errorOnFailure = false; | ||
nls.timeStepSelector = StateChangeTimeStepSelector('TargetProps', {{'Control','E'}}, 'targetChangeAbs', 0.03); | ||
% Change default tolerance for nonlinear solver | ||
model.nonlinearTolerance = 1e-3*model.Control.Imax; | ||
% Set verbosity | ||
model.verbose = true; | ||
%% Run the simulation | ||
[~, states, report] = simulateScheduleAD(initstate, model, schedule, 'OutputMinisteps', true, 'NonLinearSolver', nls); | ||
%% Process output and recover the output voltage and current from the output states. | ||
ind = cellfun(@(x) not(isempty(x)), states); | ||
states = states(ind); | ||
E = cellfun(@(x) x.Control.E, states); | ||
I = cellfun(@(x) x.Control.I, states); | ||
T = cellfun(@(x) max(x.(thermal).T), states); | ||
Tmax = cellfun(@(x) max(x.ThermalModel.T), states); | ||
% [SOCN, SOCP] = cellfun(@(x) model.calculateSOC(x), states); | ||
time = cellfun(@(x) x.time, states); | ||
figure | ||
plot(time/hour, E); | ||
grid on | ||
xlabel 'time / h'; | ||
ylabel 'potential / V'; | ||
writeh5 = false; | ||
if writeh5 | ||
writeOutput(model, states, 'output.h5'); | ||
end | ||
%{ | ||
Copyright 2021-2024 SINTEF Industry, Sustainable Energy Technology | ||
and SINTEF Digital, Mathematics & Cybernetics. | ||
This file is part of The Battery Modeling Toolbox BattMo | ||
BattMo is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
BattMo is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with BattMo. If not, see <http://www.gnu.org/licenses/>. | ||
%} | ||
:orphan: | ||
|
||
.. _runBatteryP2D_source: | ||
|
||
Source code for runBatteryP2D | ||
----------------------------- | ||
|
||
.. code:: matlab | ||
%% Pseudo-Two-Dimensional (P2D) Lithium-Ion Battery Model | ||
% This example demonstrates how to setup a P2D model of a Li-ion battery | ||
% and run a simple simulation. | ||
% Clear the workspace and close open figures | ||
clear | ||
close all | ||
%% Import the required modules from MRST | ||
% load MRST modules | ||
mrstModule add ad-core mrst-gui mpfa agmg linearsolvers | ||
%% Setup the properties of Li-ion battery materials and cell design | ||
% The properties and parameters of the battery cell, including the | ||
% architecture and materials, are set using an instance of | ||
% :class:`BatteryInputParams <Battery.BatteryInputParams>`. This class is | ||
% used to initialize the simulation and it propagates all the parameters | ||
% throughout the submodels. The input parameters can be set manually or | ||
% provided in json format. All the parameters for the model are stored in | ||
% the paramobj object. | ||
jsonstruct = parseBattmoJson(fullfile('ParameterData','BatteryCellParameters','LithiumIonBatteryCell','lithium_ion_battery_nmc_graphite.json')); | ||
% We define some shorthand names for simplicity. | ||
ne = 'NegativeElectrode'; | ||
pe = 'PositiveElectrode'; | ||
elyte = 'Electrolyte'; | ||
thermal = 'ThermalModel'; | ||
co = 'Coating'; | ||
am = 'ActiveMaterial'; | ||
itf = 'Interface'; | ||
sd = 'SolidDiffusion'; | ||
ctrl = 'Control'; | ||
cc = 'CurrentCollector'; | ||
jsonstruct.use_thermal = false; | ||
jsonstruct.include_current_collectors = false; | ||
paramobj = BatteryInputParams(jsonstruct); | ||
use_cccv = false; | ||
if use_cccv | ||
cccvstruct = struct( 'controlPolicy' , 'CCCV', ... | ||
'initialControl' , 'discharging', ... | ||
'CRate' , 1 , ... | ||
'lowerCutoffVoltage', 2.4 , ... | ||
'upperCutoffVoltage', 4.1 , ... | ||
'dIdtLimit' , 0.01 , ... | ||
'dEdtLimit' , 0.01); | ||
cccvparamobj = CcCvControlModelInputParams(cccvstruct); | ||
paramobj.Control = cccvparamobj; | ||
end | ||
%% Setup the geometry and computational grid | ||
% Here, we setup the 1D computational grid that will be used for the | ||
% simulation. The required discretization parameters are already included | ||
% in the class BatteryGeneratorP2D. | ||
gen = BatteryGeneratorP2D(); | ||
% Now, we update the paramobj with the properties of the grid. | ||
paramobj = gen.updateBatteryInputParams(paramobj); | ||
%% Initialize the battery model. | ||
% The battery model is initialized by sending paramobj to the Battery class | ||
% constructor. see :class:`Battery <Battery.Battery>`. | ||
model = Battery(paramobj); | ||
model.AutoDiffBackend= AutoDiffBackend(); | ||
inspectgraph = false; | ||
if inspectgraph | ||
cgt = model.computationalGraph; | ||
return | ||
end | ||
%% Compute the nominal cell capacity and choose a C-Rate | ||
% The nominal capacity of the cell is calculated from the active materials. | ||
% This value is then combined with the user-defined C-Rate to set the cell | ||
% operational current. | ||
CRate = model.Control.CRate; | ||
%% Setup the time step schedule | ||
% Smaller time steps are used to ramp up the current from zero to its | ||
% operational value. Larger time steps are then used for the normal | ||
% operation. | ||
switch model.(ctrl).controlPolicy | ||
case 'CCCV' | ||
total = 3.5*hour/CRate; | ||
case 'CCDischarge' | ||
total = 1.4*hour/CRate; | ||
otherwise | ||
error('control policy not recognized'); | ||
end | ||
n = 100; | ||
dt = total/n; | ||
step = struct('val', dt*ones(n, 1), 'control', ones(n, 1)); | ||
% we setup the control by assigning a source and stop function. | ||
control = model.Control.setupScheduleControl(); | ||
% This control is used to set up the schedule | ||
schedule = struct('control', control, 'step', step); | ||
%% Setup the initial state of the model | ||
% The initial state of the model is setup using the model.setupInitialState() method. | ||
initstate = model.setupInitialState(); | ||
%% Setup the properties of the nonlinear solver | ||
nls = NonLinearSolver(); | ||
linearsolver = 'direct'; | ||
switch linearsolver | ||
case 'amgcl' | ||
nls.LinearSolver = AMGCLSolverAD('verbose', true, 'reduceToCell', false); | ||
nls.LinearSolver.tolerance = 1e-4; | ||
nls.LinearSolver.maxIterations = 30; | ||
nls.maxIterations = 10; | ||
nls.verbose = 10; | ||
case 'battery' | ||
nls.LinearSolver = LinearSolverBatteryExtra('verbose' , false, ... | ||
'reduceToCell', true, ... | ||
'verbosity' , 3 , ... | ||
'reuse_setup' , false, ... | ||
'method' , 'direct'); | ||
nls.LinearSolver.tolerance = 1e-4; | ||
case 'direct' | ||
disp('standard direct solver') | ||
otherwise | ||
error('Unknown solver %s', linearsolver); | ||
end | ||
% Change default maximum iteration number in nonlinear solver | ||
nls.maxIterations = 10; | ||
% Change default behavior of nonlinear solver, in case of error | ||
nls.errorOnFailure = false; | ||
nls.timeStepSelector = StateChangeTimeStepSelector('TargetProps', {{'Control','E'}}, 'targetChangeAbs', 0.03); | ||
% Change default tolerance for nonlinear solver | ||
model.nonlinearTolerance = 1e-3*model.Control.Imax; | ||
% Set verbosity | ||
model.verbose = true; | ||
%% Run the simulation | ||
[~, states, report] = simulateScheduleAD(initstate, model, schedule, 'OutputMinisteps', true, 'NonLinearSolver', nls); | ||
%% Process output and recover the output voltage and current from the output states. | ||
ind = cellfun(@(x) not(isempty(x)), states); | ||
states = states(ind); | ||
E = cellfun(@(x) x.Control.E, states); | ||
I = cellfun(@(x) x.Control.I, states); | ||
T = cellfun(@(x) max(x.(thermal).T), states); | ||
Tmax = cellfun(@(x) max(x.ThermalModel.T), states); | ||
% [SOCN, SOCP] = cellfun(@(x) model.calculateSOC(x), states); | ||
time = cellfun(@(x) x.time, states); | ||
figure | ||
plot(time/hour, E); | ||
grid on | ||
xlabel 'time / h'; | ||
ylabel 'potential / V'; | ||
writeh5 = false; | ||
if writeh5 | ||
writeOutput(model, states, 'output.h5'); | ||
end | ||
%{ | ||
Copyright 2021-2023 SINTEF Industry, Sustainable Energy Technology | ||
and SINTEF Digital, Mathematics & Cybernetics. | ||
This file is part of The Battery Modeling Toolbox BattMo | ||
BattMo is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
BattMo is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with BattMo. If not, see <http://www.gnu.org/licenses/>. | ||
%} | ||
Oops, something went wrong.