Mercurial > repos > public > sbplib
comparison diracDiscr.m @ 1128:3a9262c045d0 feature/laplace_curvilinear_test
Copy diracDiscr.m from feature/poroelastic
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Tue, 21 May 2019 17:59:30 -0700 |
parents | |
children | b29892853daf 52d774e69b1f |
comparison
equal
deleted
inserted
replaced
1127:0aed89043ad6 | 1128:3a9262c045d0 |
---|---|
1 | |
2 function d = diracDiscr(x_s, x, m_order, s_order, H) | |
3 % n-dimensional delta function | |
4 % x_s: source point coordinate vector, e.g. [x, y] or [x, y, z]. | |
5 % x: cell array of grid point column vectors for each dimension. | |
6 % m_order: Number of moment conditions | |
7 % s_order: Number of smoothness conditions | |
8 % H: cell array of 1D norm matrices | |
9 | |
10 dim = length(x_s); | |
11 d_1D = cell(dim,1); | |
12 | |
13 % If 1D, non-cell input is accepted | |
14 if dim == 1 && ~iscell(x) | |
15 d = diracDiscr1D(x_s, x, m_order, s_order, H); | |
16 | |
17 else | |
18 for i = 1:dim | |
19 d_1D{i} = diracDiscr1D(x_s(i), x{i}, m_order, s_order, H{i}); | |
20 end | |
21 | |
22 d = d_1D{dim}; | |
23 for i = dim-1: -1: 1 | |
24 % Perform outer product, transpose, and then turn into column vector | |
25 d = (d_1D{i}*d')'; | |
26 d = d(:); | |
27 end | |
28 end | |
29 | |
30 end | |
31 | |
32 | |
33 % Helper function for 1D delta functions | |
34 function ret = diracDiscr1D(x_0in , x , m_order, s_order, H) | |
35 | |
36 m = length(x); | |
37 | |
38 % Return zeros if x0 is outside grid | |
39 if(x_0in < x(1) || x_0in > x(end) ) | |
40 | |
41 ret = zeros(size(x)); | |
42 | |
43 else | |
44 | |
45 fnorm = diag(H); | |
46 eta = abs(x-x_0in); | |
47 tot = m_order+s_order; | |
48 S = []; | |
49 M = []; | |
50 | |
51 % Get interior grid spacing | |
52 middle = floor(m/2); | |
53 h = x(middle+1) - x(middle); | |
54 | |
55 poss = find(tot*h/2 >= eta); | |
56 | |
57 % Ensure that poss is not too long | |
58 if length(poss) == (tot + 2) | |
59 poss = poss(2:end-1); | |
60 elseif length(poss) == (tot + 1) | |
61 poss = poss(1:end-1); | |
62 end | |
63 | |
64 % Use first tot grid points | |
65 if length(poss)<tot && x_0in < x(1) + ceil(tot/2)*h; | |
66 index=1:tot; | |
67 pol=(x(1:tot)-x(1))/(x(tot)-x(1)); | |
68 x_0=(x_0in-x(1))/(x(tot)-x(1)); | |
69 norm=fnorm(1:tot)/h; | |
70 | |
71 % Use last tot grid points | |
72 elseif length(poss)<tot && x_0in > x(end) - ceil(tot/2)*h; | |
73 index = length(x)-tot+1:length(x); | |
74 pol = (x(end-tot+1:end)-x(end-tot+1))/(x(end)-x(end-tot+1)); | |
75 norm = fnorm(end-tot+1:end)/h; | |
76 x_0 = (x_0in-x(end-tot+1))/(x(end)-x(end-tot+1)); | |
77 | |
78 % Interior, compensate for round-off errors. | |
79 elseif length(poss) < tot | |
80 if poss(end)<m | |
81 poss = [poss; poss(end)+1]; | |
82 else | |
83 poss = [poss(1)-1; poss]; | |
84 end | |
85 pol = (x(poss)-x(poss(1)))/(x(poss(end))-x(poss(1))); | |
86 x_0 = (x_0in-x(poss(1)))/(x(poss(end))-x(poss(1))); | |
87 norm = fnorm(poss)/h; | |
88 index = poss; | |
89 | |
90 % Interior | |
91 else | |
92 pol = (x(poss)-x(poss(1)))/(x(poss(end))-x(poss(1))); | |
93 x_0 = (x_0in-x(poss(1)))/(x(poss(end))-x(poss(1))); | |
94 norm = fnorm(poss)/h; | |
95 index = poss; | |
96 end | |
97 | |
98 h_pol = pol(2)-pol(1); | |
99 b = zeros(m_order+s_order,1); | |
100 | |
101 for i = 1:m_order | |
102 b(i,1) = x_0^(i-1); | |
103 end | |
104 | |
105 for i = 1:(m_order+s_order) | |
106 for j = 1:m_order | |
107 M(j,i) = pol(i)^(j-1)*h_pol*norm(i); | |
108 end | |
109 end | |
110 | |
111 for i = 1:(m_order+s_order) | |
112 for j = 1:s_order | |
113 S(j,i) = (-1)^(i-1)*pol(i)^(j-1); | |
114 end | |
115 end | |
116 | |
117 A = [M;S]; | |
118 | |
119 d = A\b; | |
120 ret = x*0; | |
121 ret(index) = d/h*h_pol; | |
122 end | |
123 | |
124 end | |
125 | |
126 | |
127 | |
128 | |
129 | |
130 |