annotate +sbp/+implementations/d2_variable_2.m @ 1198:2924b3a9b921 feature/d2_compatible

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.
author Martin Almquist <malmquist@stanford.edu>
date Fri, 16 Aug 2019 14:30:28 -0700
parents ded4156e53e2
children b758d1cf4c8e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
362
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
1 function [H, HI, D1, D2, e_l, e_r, d1_l, d1_r] = d2_variable_2(m,h)
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
2
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
3 BP = 1;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
4 if(m<2*BP)
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
5 error(['Operator requires at least ' num2str(2*BP) ' grid points']);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
6 end
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
7
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
8 % Norm
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
9 Hv = ones(m,1);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
10 Hv(1) = 1/2;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
11 Hv(m:m) = 1/2;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
12 Hv = h*Hv;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
13 H = spdiag(Hv, 0);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
14 HI = spdiag(1./Hv, 0);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
15
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
16
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
17 % Boundary operators
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
18 e_l = sparse(m,1);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
19 e_l(1) = 1;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
20 e_r = rot90(e_l, 2);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
21
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
22 d1_l = sparse(m,1);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
23 d1_l(1:3) = 1/h*[-3/2 2 -1/2];
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
24 d1_r = -rot90(d1_l, 2);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
25
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
26 % D1 operator
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
27 diags = -1:1;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
28 stencil = [-1/2 0 1/2];
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
29 D1 = stripeMatrix(stencil, diags, m);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
30
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
31 D1(1,1)=-1;D1(1,2)=1;D1(m,m-1)=-1;D1(m,m)=1;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
32 D1(m,m-1)=-1;D1(m,m)=1;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
33 D1=D1/h;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
34 %Q=H*D1 + 1/2*(e_1*e_1') - 1/2*(e_m*e_m');
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
35
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
36
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
37 M=sparse(m,m);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
38
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
39 scheme_width = 3;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
40 scheme_radius = (scheme_width-1)/2;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
41 r = (1+scheme_radius):(m-scheme_radius);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
42
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
43 function D2 = D2_fun(c)
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
44
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
45 Mm1 = -c(r-1)/2 - c(r)/2;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
46 M0 = c(r-1)/2 + c(r) + c(r+1)/2;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
47 Mp1 = -c(r)/2 - c(r+1)/2;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
48
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
49 M(r,:) = spdiags([Mm1 M0 Mp1],0:2*scheme_radius,length(r),m);
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
50
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
51
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
52 M(1:2,1:2)=[c(1)/2 + c(2)/2 -c(1)/2 - c(2)/2; -c(1)/2 - c(2)/2 c(1)/2 + c(2) + c(3)/2;];
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
53 M(m-1:m,m-1:m)=[c(m-2)/2 + c(m-1) + c(m)/2 -c(m-1)/2 - c(m)/2; -c(m-1)/2 - c(m)/2 c(m-1)/2 + c(m)/2;];
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
54 M=M/h;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
55
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
56 D2=HI*(-M-c(1)*e_l*d1_l'+c(m)*e_r*d1_r');
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
57 end
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
58 D2 = @D2_fun;
ded4156e53e2 Added 2nd order accurate 2nd derivative with variable coefficents in a separate implementation file, used by the class D2Variable.
Martin Almquist <martin.almquist@it.uu.se>
parents:
diff changeset
59 end