Skip to content

Commit

Permalink
fixed error in control switch
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierr committed Sep 9, 2024
1 parent 7d32caf commit f5fb9cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 52 deletions.
84 changes: 34 additions & 50 deletions Control/CcCvControlModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,6 @@
end



function state = prepareStepControl(model, state, state0, dt)

rsw0 = model.setupRegionSwitchFlags(state0, state0.ctrlType);

if ~rsw0.beforeSwitchRegion && ~rsw0.afterSwitchRegion
state.ctrlType = model.getNextCtrlType(state0.ctrlType);
end

end


function state = updateControlState(model, state, state0, dt)

Expand All @@ -136,45 +125,29 @@
return
end

rsw0 = model.setupRegionSwitchFlags(state0, state0.ctrlType);
rsw00 = model.setupRegionSwitchFlags(state0, state0.ctrlType);
ctrlType0 = state0.ctrlType;

if rsw0.beforeSwitchRegion
% We have not entered the switching region in the previous iteration. We are not going to change control
% in this step
if rsw00.beforeSwitchRegion

% We have not entered the switching region in the time step. We are not going to change control
% in this step.
ctrlType = ctrlType0;

else

currentCtrlType = state.ctrlType;
% We entered the switch region in previous time step.

nextCtrlType0 = model.getNextCtrlType(ctrlType0);
currentCtrlType = state.ctrlType; % current control in the the Newton iteration
nextCtrlType0 = model.getNextCtrlType(ctrlType0); % next control that can occur afte the previous time step control (if it changes)

rsw0 = model.setupRegionSwitchFlags(state, ctrlType0);
rsw00 = model.setupRegionSwitchFlags(state0, ctrlType0);
rsw0 = model.setupRegionSwitchFlags(state, ctrlType0);

switch currentCtrlType

case nextCtrlType0

% We are in the situation where the control has switched: The current control is the next one after the
% previous control type. We want to determine if we switch back.

if (rsw0.beforeSwitchRegion) && (~rsw00.beforeSwitchRegion && ~rsw00.afterSwitchRegion)

% We switch back because we are before the beginning of the switching region but we do not switch back if we were already in the switching region for state0
ctrlType = ctrlType0;

else

% We keep the control type as it is
ctrlType = nextCtrlType0;

end

case ctrlType0

% The control has not changed from previous and we want to determine if we should change it.
% The control has not changed from previous time step and we want to determine if we should change it.

if rsw0.afterSwitchRegion

Expand All @@ -188,6 +161,13 @@

end

case nextCtrlType0

% We do not switch back to avoid oscillation. We are anyway within the given tolerance for the
% control so that we keep the control as it is.

ctrlType = nextCtrlType0;

otherwise

error('control type not recognized');
Expand All @@ -196,6 +176,7 @@

end

state.ctrlType = ctrlType;
state = model.updateValueFromControl(state);

end
Expand Down Expand Up @@ -278,12 +259,13 @@

arefulfilled = true;

rsw = model.setupRegionSwitchFlags(state, state.ctrlType);

if strcmp(ctrlType, ctrlType0) && rsw.afterSwitchRegion

rsw = model.setupRegionSwitchFlags(state, state.ctrlType);
rswN = model.setupRegionSwitchFlags(state, nextCtrlType);

if (strcmp(ctrlType, ctrlType0) && rsw.afterSwitchRegion) || (strcmp(ctrlType, nextCtrlType) && ~rswN.beforeSwitchRegion)

arefulfilled = false;

end

end
Expand All @@ -303,38 +285,40 @@

case 'CC_discharge1'

before = (E - Emin)/Emin > tols.(ctrlType);
after = (E - Emin)/Emin < -tols.(ctrlType);
before = E > Emin*(1 + tols.(ctrlType));
after = E < Emin*(1 - tols.(ctrlType));

case 'CC_discharge2'

if isfield(state, 'dEdt')
dEdt = state.dEdt;
before = (abs(dEdt) - dEdtMin)/dEdtMin > tols.(ctrlType);
after = (abs(dEdt) - dEdtMin)/dEdtMin < -tols.(ctrlType);
before = abs(dEdt) > dEdtMin*(1 + tols.(ctrlType));
after = abs(dEdt) < dEdtMin*(1 - tols.(ctrlType));
else
before = false;
after = false;
end

case 'CC_charge1'

before = (E - Emax)/Emax < -tols.(ctrlType);
after = (E - Emax)/Emax > tols.(ctrlType);
before = E < Emax*(1 - tols.(ctrlType));
after = E > Emax*(1 + tols.(ctrlType));

case 'CV_charge2'

if isfield(state, 'dIdt')
dIdt = state.dIdt;
before = (abs(dIdt) - dIdtMin)/dIdtMin > tols.(ctrlType);
after = (abs(dIdt) - dIdtMin)/dIdtMin < -tols.(ctrlType);
before = abs(dIdt) > dIdtMin*(1 + tols.(ctrlType));
after = abs(dIdt) < dIdtMin*(1 - tols.(ctrlType));
else
before = false;
after = false;
end

otherwise

error('control type not recognized');

end

rsf = struct('beforeSwitchRegion', before, ...
Expand Down
2 changes: 0 additions & 2 deletions Control/CcCvControlModelInputParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
jsonstruct = setDefaultJsonStructField(jsonstruct, {'switchTolerances', 'CC_discharge2'}, 0.9);
jsonstruct = setDefaultJsonStructField(jsonstruct, {'switchTolerances', 'CC_charge1'}, 1e-2);
jsonstruct = setDefaultJsonStructField(jsonstruct, {'switchTolerances', 'CV_charge2'}, 0.9);



inputparams = inputparams@ControlModelInputParams(jsonstruct);
inputparams.controlPolicy = 'CCCV';
Expand Down

0 comments on commit f5fb9cf

Please sign in to comment.