Mercurial > repos > public > sbplib
annotate +scheme/Utux2D.m @ 610:b7b3c11fab4d feature/utux2D
Add interpolation to scheme
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Sat, 14 Oct 2017 22:38:42 -0700 |
parents | 0f9d20dbb7ce |
children | 2d85f17a8aec |
rev | line source |
---|---|
591 | 1 classdef Utux2D < scheme.Scheme |
2 properties | |
3 m % Number of points in each direction, possibly a vector | |
4 h % Grid spacing | |
5 grid % Grid | |
6 order % Order accuracy for the approximation | |
7 v0 % Initial data | |
8 | |
9 a % Wave speed a = [a1, a2]; | |
10 | |
11 H % Discrete norm | |
595 | 12 H_x, H_y % Norms in the x and y directions |
13 Hi, Hx, Hy, Hxi, Hyi % Kroneckered norms | |
591 | 14 |
15 % Derivatives | |
16 Dx, Dy | |
17 | |
18 % Boundary operators | |
19 e_w, e_e, e_s, e_n | |
20 | |
21 D % Total discrete operator | |
605
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
22 |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
23 % String, type of interface coupling |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
24 % Default: 'upwind' |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
25 % Other: 'centered' |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
26 coupling_type |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
27 |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
28 % String, type of interpolation operators |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
29 % Default: 'AWW' (Almquist Wang Werpers) |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
30 % Other: 'MC' (Mattsson Carpenter) |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
31 interpolation_type |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
32 |
591 | 33 |
34 end | |
35 | |
36 | |
37 methods | |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
38 function obj = Utux2D(g ,order, opSet, a, coupling_type, interpolation_type) |
605
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
39 |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
40 default_arg('interpolation_type','AWW'); |
605
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
41 default_arg('coupling_type','upwind'); |
591 | 42 default_arg('a',1/sqrt(2)*[1, 1]); |
43 default_arg('opSet',@sbp.D2Standard); | |
44 assert(isa(g, 'grid.Cartesian')) | |
45 | |
46 m = g.size(); | |
47 m_x = m(1); | |
48 m_y = m(2); | |
49 m_tot = g.N(); | |
50 | |
595 | 51 xlim = {g.x{1}(1), g.x{1}(end)}; |
52 ylim = {g.x{2}(1), g.x{2}(end)}; | |
591 | 53 obj.grid = g; |
54 | |
55 % Operator sets | |
56 ops_x = opSet(m_x, xlim, order); | |
57 ops_y = opSet(m_y, ylim, order); | |
58 Ix = speye(m_x); | |
59 Iy = speye(m_y); | |
60 | |
61 % Norms | |
62 Hx = ops_x.H; | |
63 Hy = ops_y.H; | |
64 Hxi = ops_x.HI; | |
65 Hyi = ops_y.HI; | |
595 | 66 |
67 obj.H_x = Hx; | |
68 obj.H_y = Hy; | |
591 | 69 obj.H = kron(Hx,Hy); |
70 obj.Hi = kron(Hxi,Hyi); | |
71 obj.Hx = kron(Hx,Iy); | |
72 obj.Hy = kron(Ix,Hy); | |
73 obj.Hxi = kron(Hxi,Iy); | |
74 obj.Hyi = kron(Ix,Hyi); | |
75 | |
76 % Derivatives | |
77 Dx = ops_x.D1; | |
78 Dy = ops_y.D1; | |
79 obj.Dx = kron(Dx,Iy); | |
80 obj.Dy = kron(Ix,Dy); | |
81 | |
82 % Boundary operators | |
83 obj.e_w = kr(ops_x.e_l, Iy); | |
84 obj.e_e = kr(ops_x.e_r, Iy); | |
85 obj.e_s = kr(Ix, ops_y.e_l); | |
86 obj.e_n = kr(Ix, ops_y.e_r); | |
87 | |
88 obj.m = m; | |
89 obj.h = [ops_x.h ops_y.h]; | |
90 obj.order = order; | |
595 | 91 obj.a = a; |
605
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
92 obj.coupling_type = coupling_type; |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
93 obj.interpolation_type = interpolation_type; |
591 | 94 obj.D = -(a(1)*obj.Dx + a(2)*obj.Dy); |
95 | |
96 end | |
97 % Closure functions return the opertors applied to the own domain to close the boundary | |
98 % Penalty functions return the opertors to force the solution. In the case of an interface it returns the operator applied to the other doamin. | |
99 % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. | |
100 % type is a string specifying the type of boundary condition if there are several. | |
101 % data is a function returning the data that should be applied at the boundary. | |
102 % neighbour_scheme is an instance of Scheme that should be interfaced to. | |
103 % neighbour_boundary is a string specifying which boundary to interface to. | |
104 function [closure, penalty] = boundary_condition(obj,boundary,type) | |
105 default_arg('type','dirichlet'); | |
106 | |
107 sigma = -1; % Scalar penalty parameter | |
108 switch boundary | |
109 case {'w','W','west','West'} | |
595 | 110 tau = sigma*obj.a(1)*obj.e_w*obj.H_y; |
591 | 111 closure = obj.Hi*tau*obj.e_w'; |
112 | |
113 case {'s','S','south','South'} | |
595 | 114 tau = sigma*obj.a(2)*obj.e_s*obj.H_x; |
591 | 115 closure = obj.Hi*tau*obj.e_s'; |
116 end | |
117 penalty = -obj.Hi*tau; | |
118 | |
119 end | |
120 | |
121 function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary) | |
122 | |
123 % Get neighbour boundary operator | |
124 switch neighbour_boundary | |
125 case {'e','E','east','East'} | |
126 e_neighbour = neighbour_scheme.e_e; | |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
127 m_neighbour = neighbour_scheme.m(2); |
591 | 128 case {'w','W','west','West'} |
129 e_neighbour = neighbour_scheme.e_w; | |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
130 m_neighbour = neighbour_scheme.m(2); |
591 | 131 case {'n','N','north','North'} |
132 e_neighbour = neighbour_scheme.e_n; | |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
133 m_neighbour = neighbour_scheme.m(1); |
591 | 134 case {'s','S','south','South'} |
135 e_neighbour = neighbour_scheme.e_s; | |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
136 m_neighbour = neighbour_scheme.m(1); |
591 | 137 end |
138 | |
605
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
139 switch obj.coupling_type |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
140 |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
141 % Upwind coupling (energy dissipation) |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
142 case 'upwind' |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
143 sigma_ds = -1; %"Downstream" penalty |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
144 sigma_us = 0; %"Upstream" penalty |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
145 |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
146 % Energy-preserving coupling (no energy dissipation) |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
147 case 'centered' |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
148 sigma_ds = -1/2; %"Downstream" penalty |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
149 sigma_us = 1/2; %"Upstream" penalty |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
150 |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
151 otherwise |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
152 error(['Interface coupling type ' coupling_type ' is not available.']) |
0f9d20dbb7ce
Add centered interface coupling in addition to upwind
Martin Almquist <malmquist@stanford.edu>
parents:
595
diff
changeset
|
153 end |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
154 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
155 % Check grid ratio for interpolation |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
156 switch boundary |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
157 case {'w','W','west','West','e','E','east','East'} |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
158 m = obj.m(2); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
159 case {'s','S','south','South','n','N','north','North'} |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
160 m = obj.m(1); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
161 end |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
162 grid_ratio = m/m_neighbour; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
163 if grid_ratio ~= 1 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
164 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
165 [ms, index] = sort([m, m_neighbour]); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
166 orders = [obj.order, neighbour_scheme.order]; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
167 orders = orders(index); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
168 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
169 switch obj.interpolation_type |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
170 case 'MC' |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
171 interpOpSet = sbp.InterpMC(ms(1),ms(2),orders(1),orders(2)); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
172 if grid_ratio < 1 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
173 I_neighbour2local_us = interpOpSet.IF2C; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
174 I_neighbour2local_ds = interpOpSet.IF2C; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
175 elseif grid_ratio > 1 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
176 I_neighbour2local_us = interpOpSet.IC2F; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
177 I_neighbour2local_ds = interpOpSet.IC2F; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
178 end |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
179 case 'AWW' |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
180 %String 'C2F' indicates that ICF2 is more accurate. |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
181 interpOpSetF2C = sbp.InterpAWW(ms(1),ms(2),orders(1),orders(2),'F2C'); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
182 interpOpSetC2F = sbp.InterpAWW(ms(1),ms(2),orders(1),orders(2),'C2F'); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
183 if grid_ratio < 1 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
184 % Local is coarser than neighbour |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
185 I_neighbour2local_us = interpOpSetC2F.IF2C; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
186 I_neighbour2local_ds = interpOpSetF2C.IF2C; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
187 elseif grid_ratio > 1 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
188 % Local is finer than neighbour |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
189 I_neighbour2local_us = interpOpSetF2C.IC2F; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
190 I_neighbour2local_ds = interpOpSetC2F.IC2F; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
191 end |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
192 otherwise |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
193 error(['Interpolation type ' obj.interpolation_type ... |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
194 ' is not available.' ]); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
195 end |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
196 |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
197 else |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
198 % No interpolation required |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
199 I_neighbour2local_us = speye(m,m); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
200 I_neighbour2local_ds = speye(m,m); |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
201 end |
591 | 202 |
203 switch boundary | |
204 case {'w','W','west','West'} | |
595 | 205 tau = sigma_ds*obj.a(1)*obj.e_w*obj.H_y; |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
206 closure = obj.Hi*tau*obj.e_w'; |
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
207 penalty = -obj.Hi*tau*I_neighbour2local_ds*e_neighbour'; |
591 | 208 case {'e','E','east','East'} |
595 | 209 tau = sigma_us*obj.a(1)*obj.e_e*obj.H_y; |
591 | 210 closure = obj.Hi*tau*obj.e_e'; |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
211 penalty = -obj.Hi*tau*I_neighbour2local_us*e_neighbour'; |
591 | 212 case {'s','S','south','South'} |
595 | 213 tau = sigma_ds*obj.a(2)*obj.e_s*obj.H_x; |
591 | 214 closure = obj.Hi*tau*obj.e_s'; |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
215 penalty = -obj.Hi*tau*I_neighbour2local_ds*e_neighbour'; |
591 | 216 case {'n','N','north','North'} |
595 | 217 tau = sigma_us*obj.a(2)*obj.e_n*obj.H_x; |
591 | 218 closure = obj.Hi*tau*obj.e_n'; |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
219 penalty = -obj.Hi*tau*I_neighbour2local_us*e_neighbour'; |
591 | 220 end |
610
b7b3c11fab4d
Add interpolation to scheme
Martin Almquist <malmquist@stanford.edu>
parents:
605
diff
changeset
|
221 |
591 | 222 |
223 end | |
224 | |
225 function N = size(obj) | |
226 N = obj.m; | |
227 end | |
228 | |
229 end | |
230 | |
231 methods(Static) | |
232 % Calculates the matrices needed for the inteface coupling between boundary bound_u of scheme schm_u | |
233 % and bound_v of scheme schm_v. | |
234 % [uu, uv, vv, vu] = inteface_coupling(A,'r',B,'l') | |
235 function [uu, uv, vv, vu] = interface_coupling(schm_u,bound_u,schm_v,bound_v) | |
236 [uu,uv] = schm_u.interface(bound_u,schm_v,bound_v); | |
237 [vv,vu] = schm_v.interface(bound_v,schm_u,bound_u); | |
238 end | |
239 end | |
240 end |