annotate +sbp/D2VariableCompatible.m @ 1217:a3d9567d9004 feature/d2_compatible

Improve efficiency of construction of D2(b) by precomputing as much as possible
author Martin Almquist <malmquist@stanford.edu>
date Wed, 13 Nov 2019 14:54:22 -0800
parents 2924b3a9b921
children 60c875c18de3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1198
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
1 classdef D2VariableCompatible < sbp.OpSet
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
2 properties
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
3 D1 % SBP operator approximating first derivative
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
4 H % Norm matrix
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
5 HI % H^-1
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
6 Q % Skew-symmetric matrix
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
7 e_l % Left boundary operator
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
8 e_r % Right boundary operator
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
9 D2 % SBP operator for second derivative
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
10 M % Norm matrix, second derivative
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
11 d1_l % Left boundary first derivative
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
12 d1_r % Right boundary first derivative
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
13 m % Number of grid points.
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
14 h % Step size
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
15 x % grid
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
16 borrowing % Struct with borrowing limits for different norm matrices
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
17 end
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
18
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
19 methods
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
20 function obj = D2VariableCompatible(m,lim,order)
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
21
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
22 x_l = lim{1};
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
23 x_r = lim{2};
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
24 L = x_r-x_l;
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
25 obj.h = L/(m-1);
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
26 obj.x = linspace(x_l,x_r,m)';
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
27
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
28 switch order
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
29
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
30 case 6
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
31
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
32 [obj.H, obj.HI, obj.D1, D2, ...
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
33 ~, obj.e_l, obj.e_r, ~, ~, ~, ~, ~,...
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
34 d1_l, d1_r] = ...
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
35 sbp.implementations.d4_variable_6(m, obj.h);
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
36
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
37 case 4
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
38 [obj.H, obj.HI, obj.D1, D2, obj.e_l,...
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
39 obj.e_r, d1_l, d1_r] = ...
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
40 sbp.implementations.d2_variable_4(m,obj.h);
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
41 case 2
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
42 [obj.H, obj.HI, obj.D1, D2, obj.e_l,...
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
43 obj.e_r, d1_l, d1_r] = ...
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
44 sbp.implementations.d2_variable_2(m,obj.h);
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
45
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
46 otherwise
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
47 error('Invalid operator order %d.',order);
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
48 end
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
49 obj.borrowing.H11 = obj.H(1,1)/obj.h; % First element in H/h,
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
50 obj.borrowing.M.d1 = obj.H(1,1)/obj.h; % First element in H/h is borrowing also for M
1217
a3d9567d9004 Improve efficiency of construction of D2(b) by precomputing as much as possible
Martin Almquist <malmquist@stanford.edu>
parents: 1198
diff changeset
51 obj.borrowing.R.delta_D = inf;
1198
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
52 obj.m = m;
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
53 obj.M = [];
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
54
1217
a3d9567d9004 Improve efficiency of construction of D2(b) by precomputing as much as possible
Martin Almquist <malmquist@stanford.edu>
parents: 1198
diff changeset
55
1198
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
56 D1 = obj.D1;
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
57 e_r = obj.e_r;
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
58 e_l = obj.e_l;
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
59
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
60 % D2 = Hinv * (-M + br*er*d1r^T - bl*el*d1l^T);
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
61 % Replace d1' by e'*D1 in D2.
1217
a3d9567d9004 Improve efficiency of construction of D2(b) by precomputing as much as possible
Martin Almquist <malmquist@stanford.edu>
parents: 1198
diff changeset
62 correction_l = obj.HI*(e_l*d1_l' - e_l*e_l'*D1);
a3d9567d9004 Improve efficiency of construction of D2(b) by precomputing as much as possible
Martin Almquist <malmquist@stanford.edu>
parents: 1198
diff changeset
63 correction_r = - obj.HI*(e_r*d1_r' - e_r*e_r'*D1);
a3d9567d9004 Improve efficiency of construction of D2(b) by precomputing as much as possible
Martin Almquist <malmquist@stanford.edu>
parents: 1198
diff changeset
64
a3d9567d9004 Improve efficiency of construction of D2(b) by precomputing as much as possible
Martin Almquist <malmquist@stanford.edu>
parents: 1198
diff changeset
65 D2_compatible = @(b) D2(b) + b(1)*correction_l + b(m)*correction_r;
1198
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
66
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
67 obj.D2 = D2_compatible;
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
68 obj.d1_l = (e_l'*D1)';
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
69 obj.d1_r = (e_r'*D1)';
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
70
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
71 end
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
72 function str = string(obj)
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
73 str = [class(obj) '_' num2str(obj.order)];
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
74 end
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
75 end
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
76
1217
a3d9567d9004 Improve efficiency of construction of D2(b) by precomputing as much as possible
Martin Almquist <malmquist@stanford.edu>
parents: 1198
diff changeset
77
1198
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
78 end
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
79
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
80
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
81
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
82
2924b3a9b921 Add OpSet for fully compatible D2Variable, created from regular D2Variable by replacing d1 by first row of D1. Formal reduction by one order of accuracy at the boundary point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
83