annotate diracPrimDiscr1D.m @ 1138:afd06a84b69c feature/laplace_curvilinear_test

Bugfixes in VirtaMin.interfaceStandard()
author Martin Almquist <malmquist@stanford.edu>
date Mon, 10 Jun 2019 14:39:14 +0200
parents 99fd66ffe714
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1130
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
1 % Generates discretized derivative of delta function in 1D
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
2 function ret = diracPrimDiscr1D(x_0in, x, m_order, s_order, H)
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
3
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
4 % diracPrim satisfies one more moment condition than dirac
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
5 m_order = m_order + 1;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
6
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
7 m = length(x);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
8
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
9 % Return zeros if x0 is outside grid
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
10 if(x_0in < x(1) || x_0in > x(end) )
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
11
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
12 ret = zeros(size(x));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
13
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
14 else
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
15
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
16 fnorm = diag(H);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
17 eta = abs(x-x_0in);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
18 tot = m_order+s_order;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
19 S = [];
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
20 M = [];
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
21
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
22 % Get interior grid spacing
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
23 middle = floor(m/2);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
24 h = x(middle+1) - x(middle);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
25
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
26 poss = find(tot*h/2 >= eta);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
27
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
28 % Ensure that poss is not too long
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
29 if length(poss) == (tot + 2)
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
30 poss = poss(2:end-1);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
31 elseif length(poss) == (tot + 1)
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
32 poss = poss(1:end-1);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
33 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
34
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
35 % Use first tot grid points
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
36 if length(poss)<tot && x_0in < x(1) + ceil(tot/2)*h;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
37 index=1:tot;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
38 pol=(x(1:tot)-x(1))/(x(tot)-x(1));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
39 x_0=(x_0in-x(1))/(x(tot)-x(1));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
40 norm=fnorm(1:tot)/h;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
41
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
42 % Use last tot grid points
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
43 elseif length(poss)<tot && x_0in > x(end) - ceil(tot/2)*h;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
44 index = length(x)-tot+1:length(x);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
45 pol = (x(end-tot+1:end)-x(end-tot+1))/(x(end)-x(end-tot+1));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
46 norm = fnorm(end-tot+1:end)/h;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
47 x_0 = (x_0in-x(end-tot+1))/(x(end)-x(end-tot+1));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
48
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
49 % Interior, compensate for round-off errors.
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
50 elseif length(poss) < tot
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
51 if poss(end)<m
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
52 poss = [poss; poss(end)+1];
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
53 else
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
54 poss = [poss(1)-1; poss];
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
55 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
56 pol = (x(poss)-x(poss(1)))/(x(poss(end))-x(poss(1)));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
57 x_0 = (x_0in-x(poss(1)))/(x(poss(end))-x(poss(1)));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
58 norm = fnorm(poss)/h;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
59 index = poss;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
60
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
61 % Interior
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
62 else
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
63 pol = (x(poss)-x(poss(1)))/(x(poss(end))-x(poss(1)));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
64 x_0 = (x_0in-x(poss(1)))/(x(poss(end))-x(poss(1)));
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
65 norm = fnorm(poss)/h;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
66 index = poss;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
67 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
68
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
69 h_pol = pol(2)-pol(1);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
70 b = zeros(m_order+s_order,1);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
71
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
72 b(1) = 0;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
73 for i = 2:m_order
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
74 b(i) = -(i-1)*x_0^(i-2);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
75 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
76
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
77 for i = 1:(m_order+s_order)
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
78 for j = 1:m_order
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
79 M(j,i) = pol(i)^(j-1)*h_pol*norm(i);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
80 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
81 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
82
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
83 for i = 1:(m_order+s_order)
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
84 for j = 1:s_order
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
85 S(j,i) = (-1)^(i-1)*pol(i)^(j-1);
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
86 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
87 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
88
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
89 A = [M;S];
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
90
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
91 d = A\b;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
92 ret = x*0;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
93 ret(index) = d*(h_pol/h)^2;
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
94 end
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
95
99fd66ffe714 Add derivative of delta functions and corresponding tests, tested for 1D.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
96 end