Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6219cff
deleted unused input
MichaelStrik Oct 1, 2024
3be4f5a
Merge branch 'public' of github.com:andreassommer/ifdiff into public
MichaelStrik Nov 1, 2024
d95700f
Merge branch 'public' of github.com:andreassommer/ifdiff into public
MichaelStrik Dec 5, 2024
9dafff8
rebase: resolve conflict
MichaelStrik Dec 8, 2024
4753486
rebase: resolve conflict 2
MichaelStrik Dec 8, 2024
02c858e
rebase: resolve conflict 3
MichaelStrik Dec 8, 2024
79f622c
rebase: resolve conflict 4
MichaelStrik Dec 8, 2024
2ba490d
feat: pass condition value extended to mtree_replaceJumpifByCtrlif
MichaelStrik Dec 9, 2024
147e927
feat: pass condition value; fixed generation of switching functions
MichaelStrik Dec 16, 2024
de120ec
feat: pass condition value; additional test
MichaelStrik Dec 16, 2024
97129f3
feat: pass condition; fixed error where sign can't be preprocessed in…
MichaelStrik Jan 13, 2025
432b8f6
new snapshot: jumpif-feat
MichaelStrik Jan 14, 2025
4678fc1
Refactor: mtree_checkForComparisonOperator only gets the first op
Schlevidon Feb 3, 2025
80b961c
Refactor: Remove redundant comp in mtree_createAndAdd_NewNode
Schlevidon Feb 3, 2025
34addb5
Refactor: Simplify logic in mtree_addSwitchEvaluation
Schlevidon Feb 3, 2025
3111ac3
Refactor: Always add parens in
Schlevidon Feb 3, 2025
bd29db8
Refactor: Simplify swapping children in mtree_switchLeftRightChildren
Schlevidon Feb 3, 2025
7e6c0b6
Refactor: Simplify preprocess_setUpCtrlif_switchThenAndElsePart
Schlevidon Feb 3, 2025
4c3e791
Refactor: Fix some small typos in comments
Schlevidon Feb 4, 2025
5aad2c3
Merge pull request #26 from andreassommer/pass-condition_value-review
MichaelStrik Feb 24, 2025
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
45 changes: 45 additions & 0 deletions Examples/sign/sign_inconsistent_2d_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
integrator = @ode45;
t0 = 0;
tf = 1;
% tf = 0.5+30*eps;
timeinterval = [t0,tf];
initstates = [1, 1];
p = 0;

fprintf('Preprocessing...\n ');
odeoptions = odeset( 'AbsTol', 1e-14, 'MaxStep', 2);
filename = 'sign_inconsistent_rhs_2d';
tic
datahandle = prepareDatahandleForIntegration(filename, ...
'solver', func2str(integrator), 'options', odeoptions);
toc

fprintf('Integration with ifdiff/%s...\n ', func2str(integrator));
tic
sol_rhs_test = solveODE(datahandle, timeinterval, initstates, p);
% sol_ode45 = ode45(@(t,x) sign_inconsistent_rhs_2d(t,x,p), timeinterval, initstates);
toc



% Read out number of switches during integration
n_switches = max(size(datahandle.getData().SWP_detection.switchingpoints));
fprintf('Total number of switches during integration: %d \n', n_switches);

sw_points = cell2mat(datahandle.getData().SWP_detection.switchingpoints);
sw_points_y = deval(sol_rhs_test, sw_points);

% Visualize solution
fig = figure(1);
clf(fig, 'reset');
scale = 4*eps;
ax1 = subplot(1,3,1); plot(sol_rhs_test.x, sol_rhs_test.y(1,:));
ax2 = subplot(1,3,2); plot(sol_rhs_test.x, sol_rhs_test.y(1,:), sw_points, sw_points_y, 'o');
ax3 = subplot(1,3,3); plot(sol_rhs_test.x, sol_rhs_test.y(2,:));

grid([ax1, ax2, ax3], 'on');
legend(ax2, 'x(t)','Switches','Location','West');
axis(ax2, [0.5, 0.5+eps, -scale, scale]);
xlim([ax1, ax3], [0, 1]);

sgtitle('Inconsistently switching sign-equation');
12 changes: 12 additions & 0 deletions Examples/sign/sign_inconsistent_rhs_2d.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function dx = sign_inconsistent_rhs_2d(t,x,p)

dx = zeros(2,1);
sign_value = sign(x(2)-p(1));
dx(1) = 4 + 2*sign_value;
dx(2) = 2 - 4*sign_value;


% dx(1) = 4 + 2*sign(x(2)-p(1));
% dx(2) = 2 - 4*sign(x(2)-p(1));

end
3 changes: 2 additions & 1 deletion createDatahandle.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
data.caseCtrlif = config.caseCtrlif.default;

datahandle = makeClosure(data);
end

end
4 changes: 3 additions & 1 deletion ctrlif.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function y = ctrlif(condition, truepart, falsepart, ctrlif_index, function_index, datahandle)
function y = ctrlif(num, truepart, falsepart, ctrlif_index, function_index, datahandle)
% function for:
%
% - supervising signature during integration (.active_SWP_detection = 1)
Expand All @@ -11,6 +11,8 @@

config = makeConfig();
data = datahandle.getData();

condition = (num >= 0);

switch data.caseCtrlif
case config.caseCtrlif.default
Expand Down
4 changes: 4 additions & 0 deletions makeConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

% Names related to the ctrlif function.
config.ctrlif.functionName = 'ctrlif';
config.ctrlif.switchEvalName = 'switchEval';
config.ctrlif.outputName = 'condition_value';

% Make setting the mode a ctrlif operates in more readable.
Expand Down Expand Up @@ -103,6 +104,9 @@
config.mtree_rIndex_function.Suffix_equals = '_Equals';
config.mtree_rIndex_function.Suffix_out = '_Out';
config.mtree_rIndex_function.Suffix_fname = '_Fname';

% Error types
config.errors.infeasible_if_condition = 'IFDIFF:INFEASIBLE_IF_CONDITION';
% ==================================================================================

config.jump.specifyingFunction = 'ifdiff_jumpif';
Expand Down
49 changes: 49 additions & 0 deletions mtree_addSwitchEvaluation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function [mtreeobj, operatorKind] = mtree_addSwitchEvaluation(mtreeobj, from, condition)
% [mtreeobj, operatorKind] = mtree_addSwitchEvaluation(mtreeobj, from, condition)
%
% Takes the condition at rIndex ´condition´, copies it to ´from´ and
% converts it into a switching function evaluation.
% For example, a >= b is converted into a - b.
%
%
% INPUT:
% ´mtreeobj´ mtree
% ´from´ rIndex at which to add the switch evaluation.
% ´condition´ rIndex of switch condition.
%
%
% OUTPUT:
% ´mtreeobj´ Edited mtree.
% ´operatorKind´ The comparison operator the switch condition
% contained (<,>,<=,>=).


% Determine the operator involved in the switch condition
[operatorKind, rIndexOperator] = mtree_getFirstComparisonOperator(mtreeobj, condition);

if ~ismember(operatorKind, [mtreeobj.K.GE, mtreeobj.K.GT, mtreeobj.K.LT, mtreeobj.K.LE])
% operator is none of the allowed types
error( ...
config.errors.infeasible_if_condition, ...
'"If" statement does not include on of the comparisons <, >, <=, >=.');
end

cIndex = mtree_cIndex();
mtreeobj = mtree_connectNodes(mtreeobj, from, condition, cIndex.indexRightchild);

% Transform comparison into normal form (i.e. <expr> >= 0)
% '<=': a<=b <=> b-a>=0
% '>': a>b <=> not b-a>=0
% '<': a<b <=> not a-b>=0
% '>=': a>=b <=> a-b>=0

% Switch a and b first if necessary.
if operatorKind == mtreeobj.K.LE || operatorKind == mtreeobj.K.GT
mtreeobj = mtree_switchLeftRightChildren(mtreeobj, rIndexOperator);
end

% Replace comparison operator by minus.
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, rIndexOperator);

% preprocess_setUpCtrlif(...) takes care of the negation if necessary.
end
50 changes: 0 additions & 50 deletions mtree_checkForComparisonOperator.m

This file was deleted.

25 changes: 11 additions & 14 deletions mtree_createAndAdd_NewNode.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [mtreeobj, rIndex_new_row] = mtree_createAndAdd_NewNode(mtreeobj, varargin)
function [mtreeobj, rIndex_new_row] = mtree_createAndAdd_NewNode(mtreeobj, from, from_type, kind, varargin)
% create and add new nodes to a mtree.
%
% INPUT:
Expand Down Expand Up @@ -54,13 +54,6 @@
% cIndex -> column index; refers to a property type
cIndex = mtree_cIndex();

% assign input
from = varargin{1};
from_type = varargin{2};
kind = varargin{3};



% check whether to add variable/integer or other
if length(kind) == 1
% check input
Expand All @@ -81,18 +74,22 @@
% kindOfNode; ID if variable, INT for integer
% sizOfNode: length of the character string
% stringtableindex: position of the string in mtreeobj.C (array of all variables)
newNode = mtree_createMtreeNode('kindOfNode', kindOfNode, ...
'sizeOfNode', length(str), ...
'stringTableIndex', index_new_node);
newNode = mtree_createMtreeNode('kindOfNode', kindOfNode, ...
'sizeOfNode', length(str), ...
'stringTableIndex', index_new_node);
end

% new row index
rIndex_new_row = length(mtreeobj.IX) + 1;

% check if there is any input for to/to_type.
if nargin >= 5
to = varargin{4};
to_type = varargin{5};
if ~isempty(varargin)
if length(varargin) == 1
error('Got argument ''to'', but missing argument ''to_type''.');
end

to = varargin{1};
to_type = varargin{2};

if to ~= 0 % caution Remark: extend compability with multiple values in to

Expand Down
28 changes: 28 additions & 0 deletions mtree_ctrlifNormFormTrafo_substituteComparison.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, idxRel)
% Changes type of node at idxRel to MINUS and adds parantheses before the right child of idxRel.
%
% Intended for converting 'a>=b' into 'a-(b)'
% (or any of <,>,<= instead of >=).
%
%
% INPUT:
% ´mtreeobj´ mtree
% ´idxRel´ index of relation
%
% OUTPUT:
% ´mtreeobj´ Edited mtree.


cIndex = mtree_cIndex();

% Replace comparison with MINUS
mtreeobj.T(idxRel, cIndex.kindOfNode) = mtreeobj.K.MINUS;

% Add parantheses around MINUS term
[mtreeobj, ~] = mtree_createAndAdd_NewNode(mtreeobj, ...
idxRel, ... % from
cIndex.indexRightchild, ... % from_type
mtreeobj.K.PARENS, ... % new node
mtreeobj.T(idxRel, cIndex.indexRightchild), ... % to
cIndex.indexLeftchild); % to_type
end
5 changes: 2 additions & 3 deletions mtree_findBeginOfLine.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function output = mtree_findBeginOfLine(mtreeobj, start_node, node_index_to_find, varargin)
% Function to either find a expr node are any other beginning of an line
% for example if, while etc.
function output = mtree_findBeginOfLine(mtreeobj, start_node, node_index_to_find)
% Function to either find an expr node or any other beginning of a line, e.g. if, while etc.

cIndex = mtree_cIndex();

Expand Down
38 changes: 38 additions & 0 deletions mtree_getFirstComparisonOperator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function [kind, rIndex] = mtree_getFirstComparisonOperator(mtreeobj, index)
% Searches the subtree of ´mtreeobj´ defined by ´index´ for the first node which
% represents one of the following comparison operators: "<", ">", "<=" or ">=".
%
% INPUT:
% ´mtreeobj´ mtree
% ´index´ Node index that defines the subtree to be searched.
%
%
% OUTPUT:
% ´kind´ Kind of operator that was found.
% ´rIndex´ Node index of the operator that was found in the mtree.

% If nothing is found, return 0 and an empty array
kind = 0;
rIndex = [];


cIndex = mtree_cIndex();
rIndexNode = find(mtree_getAllLeftRightChildren(mtreeobj, index));
kindOfNode = mtreeobj.T(rIndexNode, cIndex.kindOfNode);

operators = [
mtreeobj.K.GE, ...
mtreeobj.K.GT, ...
mtreeobj.K.LT, ...
mtreeobj.K.LE];

% For each child node (column): check if type matches one of the comparison operators
nodeOperators = transpose(kindOfNode == operators);

% Find first child node which contains a comparison operator
[idxOperator, idxNode] = find(nodeOperators, 1);
if ~isempty(idxOperator)
kind = operators(idxOperator);
rIndex = rIndexNode(idxNode);
end
end
5 changes: 3 additions & 2 deletions mtree_replaceAbsByCtrlif.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
ctrlif_index, ...
abs_argument_character_string, ...
abs_argument_character_string, ...
['-', abs_argument_character_string]);
['-', abs_argument_character_string], ...
0);
ctrlif_index = ctrlif_index + 1;
end
end
end
Loading