Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a76a258
Make jump field in sol output a logical array
Schlevidon May 17, 2026
c90668f
Set datahandle model number directly when retrieving RHS for model
Schlevidon May 17, 2026
32c7484
WIP: Add prototype for new sensitivity solver
Schlevidon May 17, 2026
6742c67
Fix incorrect comment in sensitivity update function
Schlevidon May 26, 2026
938cd95
Simplify VDE RHS interface
Schlevidon May 26, 2026
582ee8e
Add WIP class diagram for sensitivities
Schlevidon Jun 1, 2026
2abc4b0
WIP: Cleanup new sensitivity implementation
Schlevidon Jun 9, 2026
ff438a3
Return sensitivity at timepoints via piecewise sol evaluation
Schlevidon Jun 10, 2026
d2dc5bb
WIP: Implement new sensitivity into old interface and fix bugs
Schlevidon Jun 10, 2026
f022612
Refactor finite difference computation with class
Schlevidon Jun 14, 2026
478fb9d
Fix inverted logic for submodel export config flag in sens updates
Schlevidon Jun 14, 2026
d11beb2
Add direction argument for sensitivity in main interface for VDE method
Schlevidon Jun 15, 2026
592cd8d
Fix bug in finite differences when all directions zero
Schlevidon Jun 24, 2026
6390a53
Test effect of FD step for directional sensitivities
Schlevidon Jun 24, 2026
e2deba9
Enforce output compatibility with legacy sensitivity format
Schlevidon Jun 24, 2026
7574b35
Fix formatting in generateSensitivityFunction
Schlevidon Jun 24, 2026
354fec3
Allow passing multiple time points to finite differences
Schlevidon Jun 24, 2026
f961ee6
Add END method for directional sensitivities
Schlevidon Jun 24, 2026
434a151
Simplify finite difference code
Schlevidon Jun 26, 2026
4f63b93
Provide dense output for sensitivity END solver
Schlevidon Jun 26, 2026
10da32f
Add END piecewise solver for directional sensitivities
Schlevidon Jun 28, 2026
83810db
Remove old temporary sensitivity test code
Schlevidon Jun 28, 2026
a5cfc49
Move old sensitivity code to separate directory
Schlevidon Jun 28, 2026
5506fc8
Remove old class diagram
Schlevidon Aug 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/TestStateJumps.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function testBounceball(testCase)
sensTimes = [t0 sensTimes tEnd];
fdStep = generateFDstep(numel(x0), numel(p), 'hy', 1e-6, 'hp', 1e-6);
sensFun = generateSensitivityFunction(datahandle, sol, 'method', 'VDE', 'FDstep', fdStep, ...
'CalcGy', true, 'CalcGp', true, 'Gmatrices_intermediate', true);
'CalcGy', true, 'CalcGp', true, 'Gmatrices_intermediate', false);
sens = sensFun(sensTimes);
Gy = {sens.Gy};
Gp = {sens.Gp};
Expand Down Expand Up @@ -257,7 +257,7 @@ function testSensitivitiesSimpleVDE(testCase)
rtol2 = 1e-5;

sensFun = generateSensitivityFunction(datahandle, sol, 'method', 'VDE', ...
'CalcGy', true, 'CalcGp', true, 'Gmatrices_intermediate', true);
'CalcGy', true, 'CalcGp', true, 'Gmatrices_intermediate', false);

t1 = sol.switches(1);
t1Minus = leftLimit(t1);
Expand Down Expand Up @@ -293,7 +293,7 @@ function testSensitivitiesSimpleEND_piecewise(testCase)
rtol2 = 1e-5;

sensFun = generateSensitivityFunction(datahandle, sol, 'method', 'END_piecewise', ...
'CalcGy', true, 'CalcGp', true, 'Gmatrices_intermediate', true);
'CalcGy', true, 'CalcGp', true, 'Gmatrices_intermediate', false);

t1 = sol.switches(1);
t1Minus = leftLimit(t1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
tspan = [0 20];
initialvalues = [1;0];
parameters = 5.437;
sol = solveODE(datahandle, tspan, initialvalues, parameters);
sol = solveODE(datahandle, tspan, initialvalues, parameters);

%% Precalculations for sensitivities
dim_y = size(sol.y, 1);
Expand All @@ -16,8 +16,10 @@

%% Sensitivities VDE
integrator_options = odeset( 'AbsTol', 1e-14,'RelTol', 1e-12);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'integrator_options', integrator_options, 'method', 'VDE', 'Gmatrices_intermediate', true);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, ...
'integrator_options', integrator_options, 'method', 'VDE', 'Gmatrices_intermediate', true, 'legacy', true);
sensitivities_VDE = sensitivities_function_VDE(20);

%% Analytical solution
yA_1 = @(t) (1/300)*t.^3 + 1;
yA_2 = 0;
Expand Down Expand Up @@ -89,4 +91,3 @@
sensitivities_VDE.Up{1} - U1_p;
sensitivities_VDE.Up{2} - U2_p;
sensitivities_VDE.Gp - Gp_tf_t0;

Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,5 @@
yFull{idxModel} = @(t) [y{1, idxModel}(t); y{2, idxModel}(t)];
end

funcSolPiecewise = @(t) evalPiecewise(t, yFull, length(y0), tSwitch);
end

function y = evalPiecewise(t, solPiece, dimy, sw)
% Make sure eval points and switching times are sorted in ascending order.
[t, tSortIdx] = sort(t);
sw = sort(sw);
y = zeros(dimy, length(t));

done = false;
idxModelStart = 1;
for idxSw=1:length(sw)
% Since eval points and switching times are sorted, we look for the first eval point that lies after the next switch.
idxModelEnd = (idxModelStart - 1) + find(t(idxModelStart:end) >= sw(idxSw), 1);
% If no such point exists, then we stay in the same model until the end.
if isempty(idxModelEnd)
y(:, idxModelStart:end) = solPiece{idxSw}(t(idxModelStart:end));
done = true;
break
end
% Otherwise, evaluate points before switch in the current model and update model for next iteration.
y(:, idxModelStart:idxModelEnd-1) = solPiece{idxSw}(t(idxModelStart:idxModelEnd-1));
idxModelStart = idxModelEnd;
end

% If we actually reach the last model or stay in first model, then we have to evaluate the remaining points in that model.
if ~done
y(:, idxModelStart:end) = solPiece{end}(t(idxModelStart:end));
end

% Reverse the sorting
y(:, tSortIdx) = y(:, 1:end);
funcSolPiecewise = @(t) evalPiecewiseFunc(t, yFull, length(y0), tSwitch);
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
%% Generation of sensitivity functions
tic;
integrator_options = odeset( 'AbsTol', 1e-14,'RelTol', 1e-12);
sensitivities_function_ENDfull = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_full', 'calcGy', true,'calcGp',true, 'Gmatrices_intermediate', true, 'save_intermediates', true);
sensitivities_function_ENDpiece = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_piecewise', 'calcGy', true,'calcGp',true, 'Gmatrices_intermediate', true, 'save_intermediates', false);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'integrator_options', integrator_options, 'method', 'VDE', 'calcGy', true,'calcGp',false, 'Gmatrices_intermediate', true, 'save_intermediates', false);
sensitivities_function_ENDfull = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_full', 'calcGy', true,'calcGp',true, 'Gmatrices_intermediate', false, 'save_intermediates', true);
sensitivities_function_ENDpiece = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_piecewise', 'calcGy', true,'calcGp',true, 'Gmatrices_intermediate', false, 'save_intermediates', false);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'integrator_options', integrator_options, 'method', 'VDE', 'calcGy', true,'calcGp',false, 'Gmatrices_intermediate', false, 'save_intermediates', false);
toc;

%% Sensitivty calculations
Expand All @@ -35,8 +35,8 @@
toc;

%% Plot sensitivities initial values
sensitivities_function_ENDpiecewise = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_piecewise', 'Gy', true,'Gp',false, 'Gmatrices_intermediate', true, 'save_intermediates', true);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'VDE', 'calcGy', true,'calcGp',false, 'Gmatrices_intermediate', true, 'save_intermediates', true);
sensitivities_function_ENDpiecewise = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_piecewise', 'Gy', true,'Gp',false, 'Gmatrices_intermediate', false, 'save_intermediates', true);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'VDE', 'calcGy', true,'calcGp',false, 'Gmatrices_intermediate', false, 'save_intermediates', true);

t_plot = 0:0.01:20;
%sensitivities_END_plot = sensitivities_function_ENDpiecewise(t_plot);
Expand Down Expand Up @@ -97,8 +97,8 @@

%% Plot sensitivities parameters

%sensitivities_function_ENDpiecewise = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_piecewise', 'Gy', false,'Gp',true, 'Gmatrices_intermediate', true, 'save_intermediates', true);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'VDE', 'calcGy', false,'calcGp',true, 'Gmatrices_intermediate', true, 'save_intermediates', true);
%sensitivities_function_ENDpiecewise = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'END_piecewise', 'Gy', false,'Gp',true, 'Gmatrices_intermediate', false, 'save_intermediates', true);
sensitivities_function_VDE = generateSensitivityFunction(datahandle, sol, 'FDstep', FDstep, 'method', 'VDE', 'calcGy', false,'calcGp',true, 'Gmatrices_intermediate', false, 'save_intermediates', true);

t_plot = 0:0.01:20;
%sensitivities_END_plot = sensitivities_function_ENDpiecewise(t_plot);
Expand Down
Binary file modified toolbox/examples/liveExamples/CanonicalSensitivities.mlx
Binary file not shown.
Loading