%% One-dimensional manifold continuation using the atlas_kd atlas algorithm

% Check automatic step size refinement
prob = coco_prob();
prob = coco_add_func(prob, 'ellipse', @ellipse, [], ...
  'zero', 'u0', [1+1/30; 0]);
prob = coco_add_pars(prob, 'pars', [1; 2], {'x', 'y'});
prob = coco_set(prob, 'cont', 'atlas', @atlas_kd.create);
prob = coco_set(prob, 'cont', 'NPR', 1, 'PtMX', 400);
prob = coco_set(prob, 'cont', 'almax', 10, 'h_max', 1, 'h_min', 1e-5);
coco(prob, 'run1', [], 1, {'x' 'y'});
atlas = coco_bd_read('run1', 'atlas');
figure(1)
clf
plot_atlas(atlas, 2)
axis tight
axis equal

% With computational domain boundary
coco(prob, 'run2', [], 1, {'x' 'y'}, {[1 1.5], [0 0.8]});
atlas = coco_bd_read('run2', 'atlas');
figure(2)
clf
plot_atlas(atlas, 2)
axis tight
axis equal
% Use this command to check the step size used to compute each solution
% point: coco_bd_col(bd, 'StepSize')

%% Further tests of conditional flows: theta

% theta == 0 : tangent predictor (explicit Euler)
probb = coco_set(prob, 'cont', 'theta', 0);
coco(probb, 'th1', [], 1, {'x' 'y'});

% theta = 0.5 : linearly implicit mid-point rule
probb = coco_set(prob, 'cont', 'theta', 0.5);
coco(probb, 'th2', [], 1, {'x' 'y'});

% theta = 1 : linearly implicit Euler
probb = coco_set(prob, 'cont', 'theta', 1);
coco(probb, 'th3', [], 1, {'x' 'y'});

% view log files of previous runs
coco_view_log('th1', 'type')
coco_view_log('th2', 'type')
coco_view_log('th3', 'type')
