Skip to content

Commit

Permalink
Merge branch 'octave_fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
lionsimbatoolbox committed Jan 19, 2020
2 parents 5978f90 + f900549 commit f32e528
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Parameters_init.m
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,14 @@
%
param.daeFormulation = 1;


% DO NOT CHANGE THE FOLLOWING LINES
% The following lines of code are used to identify whether the platform under which
% LIONSIMBA is executed is either Matlab or Octave

% Check if the code is running under Octave. If running Octave, the following
% instruction should return 5. If 0 is provided instead, then it means that Matlab
% is in execution
param.isMatlab = exist('OCTAVE_VERSION', 'builtin') == 0 ;

end
3 changes: 1 addition & 2 deletions battery_model_files/P2D_equations/electrolyteDiffusion.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
% Fix the last elements of the A_n
A_n(end,end-1:end) = [Deff_n(end-1) -Deff_n(end-1)];
%% A_tot matrix

A_tot = blkdiag(A_p,A_s,A_n);
A_tot = blockDiagonalMatrix(param,A_p,A_s,A_n);

% Divide by the deltax and the length of the positive electrode
A_tot(1:param.Np,1:param.Np) = A_tot(1:param.Np,1:param.Np)/(param.deltax_p^2*param.len_p^2);
Expand Down
2 changes: 1 addition & 1 deletion battery_model_files/P2D_equations/electrolytePotential.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
A_s = A_s./(param.deltax_s*param.len_s);
A_p = A_p./(param.deltax_p*param.len_p);

A_tot = blkdiag(A_p,A_s,A_n);
A_tot = blockDiagonalMatrix(param,A_p,A_s,A_n);

% Fix values to enforce BC on the left side of the positive electrode.
% A_tot(1,1:2) = [Keff_p_medio(1) -Keff_p_medio(1)]./(param.deltax_p*param.len_p);
Expand Down
2 changes: 1 addition & 1 deletion battery_model_files/P2D_equations/thermalModel_pde.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
A_n = A_n/(param.deltax_n*param.len_n)^2;
A_cu = A_cu/(param.deltax_cu*param.len_cu)^2;

A_tot = blkdiag(A_al,A_p,A_s,A_n,A_cu);
A_tot = blockDiagonalMatrix(param,A_al,A_p,A_s,A_n,A_cu);

%% Interfaces

Expand Down
59 changes: 59 additions & 0 deletions battery_model_files/simulator_tools/blockDiagonalMatrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function resultingMatrix = blockDiagonalMatrix(param,varargin)
% blockDiagonalMatrix provides an interface for creating block diagonal matrices

% This file is part of the LIONSIMBA Toolbox
%
% Official web-site: http://sisdin.unipv.it/labsisdin/lionsimba.php
% Official GitHUB: https://github.com/lionsimbatoolbox/LIONSIMBA
%
% LIONSIMBA: A Matlab framework based on a finite volume model suitable for Li-ion battery design, simulation, and control
% Copyright (C) 2016-2018 :Marcello Torchio, Lalo Magni, Davide Raimondo,
% University of Pavia, 27100, Pavia, Italy
% Bhushan Gopaluni, Univ. of British Columbia,
% Vancouver, BC V6T 1Z3, Canada
% Richard D. Braatz,
% Massachusetts Institute of Technology,
% Cambridge, Massachusetts 02142, USA
%
% Main code contributors to LIONSIMBA 2.0:
% Ian Campbell, Krishnakumar Gopalakrishnan,
% Imperial college London, London, UK
%
% LIONSIMBA is a free Matlab-based software distributed with an MIT
% license.
prev_class = class(varargin{1});

% Check if the matrices provided for building the block diagonal are all variables
% of the same class
for i=1:nargin-1
if(isa(varargin{i},prev_class))
prev_class=class(varargin{i});
else
error('All the inputs arguments must be of the same class')
end
end

% When running the model using symbolic variables, the creation of the block diagonal has to be carried out
% differently if Octave is used
if(isa(varargin{1},'casadi.SX') || isa(varargin{1},'casadi.MX'))
if(param.isMatlab)
resultingMatrix = blkdiag(varargin{:});
else
size_tot = [0,0];
for i=1:nargin-1
size_tot = size_tot + size(varargin{i});
end
resultingMatrix = SX.zeros(size_tot);
start_position = [1,1];
% Create the block diagonal
for i=1:nargin-1
resultingMatrix(start_position(1):start_position(1)+size(varargin{i},1)-1,start_position(2):start_position(2)+size(varargin{i},2)-1) = varargin{i};
start_position(1) = start_position(1) + size(varargin{i},1);
start_position(2) = start_position(2) + size(varargin{i},2);
end
end
else
resultingMatrix = blkdiag(varargin{:});
end

end

0 comments on commit f32e528

Please sign in to comment.