comparison +scheme/Hypsyst2d.m @ 296:a6ae1b104391 feature/hypsyst

Renamed class.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 26 Sep 2016 14:21:37 +0200
parents +scheme/hypsyst2d.m@da0131655035
children cd30b22cee56
comparison
equal deleted inserted replaced
295:da0131655035 296:a6ae1b104391
1 classdef Hypsyst2d < scheme.Scheme
2 properties
3 m % Number of points in each direction, possibly a vector
4 n %size of system
5 h % Grid spacing
6 x,y % Grid
7 X,Y % Values of x and y for each grid point
8 order % Order accuracy for the approximation
9
10 D % non-stabalized scheme operator
11 A, B, E
12
13 H % Discrete norm
14 % Norms in the x and y directions
15 Hxi,Hyi % Kroneckerd norms. 1'*Hx*v corresponds to integration in the x dir.
16 I_x,I_y, I_N
17 e_w, e_e, e_s, e_n
18 params %parameters for the coeficient matrices
19 matrices
20 end
21
22
23 methods
24 function obj = Hypsyst2d(m, lim, order, A, B, E, params)
25 default_arg('E', [])
26 xlim = lim{1};
27 ylim = lim{2};
28
29 if length(m) == 1
30 m = [m m];
31 end
32
33 m_x = m(1);
34 m_y = m(2);
35 obj.params = params;
36
37 obj.matrices = matrices;
38
39 ops_x = sbp.D2Standard(m_x,xlim,order);
40 ops_y = sbp.D2Standard(m_y,ylim,order);
41
42 obj.x = ops_x.x;
43 obj.y = ops_y.x;
44
45 obj.X = kr(obj.x,ones(m_y,1));
46 obj.Y = kr(ones(m_x,1),obj.y);
47
48 obj.A = obj.evaluateCoefficientMatrix(matrices.A, obj.X, obj.Y);
49 obj.B = obj.evaluateCoefficientMatrix(matrices.B, obj.X, obj.Y);
50 obj.E = obj.evaluateCoefficientMatrix(matrices.E, obj.X, obj.Y);
51
52 obj.n = length(matrices.A(obj.params,0,0));
53
54 I_n = eye(obj.n);I_x = speye(m_x);
55 obj.I_x = I_x;
56 I_y = speye(m_y);
57 obj.I_y = I_y;
58
59
60 D1_x = kr(I_n, ops_x.D1, I_y);
61 obj.Hxi = kr(I_n, ops_x.HI, I_y);
62 D1_y = kr(I_n, I_x, ops_y.D1));
63 obj.Hyi = kr(I_n, I_x, ops_y.HI));
64
65 obj.e_w = kr(I_n, ops_x.e_l, I_y);
66 obj.e_e = kr(I_n, ops_x.e_r, I_y);
67 obj.e_s = kr(I_n, I_x, ops_y.e_l);
68 obj.e_n = kr(I_n, I_x, ops_y.e_r);
69
70 obj.m=m;
71 obj.h=[ops_x.h ops_y.h];
72 obj.order=order;
73
74 obj.D=-obj.A*D1_x-obj.B*D1_y-obj.E;
75
76 end
77
78 % Closure functions return the opertors applied to the own doamin to close the boundary
79 % Penalty functions return the opertors to force the solution. In the case of an interface it returns the operator applied to the other doamin.
80 % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'.
81 % type is a string specifying the type of boundary condition if there are several.
82 % data is a function returning the data that should be applied at the boundary.
83 function [closure, penalty] = boundary_condition(obj,boundary,type,L)
84 default_arg('type','char');
85 switch type
86 case{'c','char'}
87 [closure,penalty]=boundary_condition_char(obj,boundary);
88 case{'general'}
89 [closure,penalty]=boundary_condition_general(obj,boundary,L);
90 otherwise
91 error('No such boundary condition')
92 end
93 end
94
95 function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary)
96 error('An interface function does not exist yet');
97 end
98
99 function N = size(obj)
100 N = obj.m;
101 end
102
103 function [ret] = evaluateCoefficientMatrix(obj, mat, X, Y)
104 params=obj.params;
105
106 if isa(mat,'function_handle')
107 [rows,cols]=size(mat(params,0,0));
108 matVec=mat(params,X',Y');
109 matVec=sparse(matVec);
110 side=max(length(X),length(Y));
111 else
112 matVec=mat;
113 [rows,cols]=size(matVec);
114 side=max(length(X),length(Y));
115 cols=cols/side;
116 end
117 ret=kron(ones(rows,cols),speye(side));
118
119 for ii=1:rows
120 for jj=1:cols
121 ret((ii-1)*side+1:ii*side,(jj-1)*side+1:jj*side)=diag(matVec(ii,(jj-1)*side+1:jj*side));
122 end
123 end
124 end
125
126
127 function [closure, penalty]=boundary_condition_char(obj,boundary)
128 params=obj.params;
129 x=obj.x; y=obj.y;
130 side=max(length(x),length(y));
131
132 switch boundary
133 case {'w','W','west'}
134 e_=obj.e_w;
135 mat=obj.matrices.A;
136 boundPos='l';
137 Hi=obj.Hxi;
138 [V,Vi,D,signVec]=obj.matrixDiag(mat,x(1),y);
139 case {'e','E','east'}
140 e_=obj.e_e;
141 mat=obj.matrices.A;
142 boundPos='r';
143 Hi=obj.Hxi;
144 [V,Vi,D,signVec]=obj.matrixDiag(mat,x(end),y);
145 case {'s','S','south'}
146 e_=obj.e_s;
147 mat=obj.matrices.B;
148 boundPos='l';
149 Hi=obj.Hxi;
150 [V,Vi,D,signVec]=obj.matrixDiag(mat,x,y(1));
151 case {'n','N','north'}
152 e_=obj.e_n;
153 mat=obj.matrices.B;
154 boundPos='r';
155 Hi=obj.Hxi;
156 [V,Vi,D,signVec]=obj.matrixDiag(mat,x,y(end));
157 end
158
159 pos=signVec(1); zeroval=signVec(2); neg=signVec(3);
160
161 switch boundPos
162 case {'l'}
163 tau=sparse(obj.n*side,pos*side);
164 Vi_plus=Vi(1:pos*side,:);
165 tau(1:pos*side,:)=-abs(D(1:pos*side,1:pos*side));
166 closure=Hi*e_*V*tau*Vi_plus*e_';
167 penalty=-Hi*e_*V*tau*Vi_plus;
168 case {'r'}
169 tau=sparse(obj.n*side,neg*side);
170 tau((pos+zeroval)*side+1:obj.n*side,:)=-abs(D((pos+zeroval)*side+1:obj.n*side,(pos+zeroval)*side+1:obj.n*side));
171 Vi_minus=Vi((pos+zeroval)*side+1:obj.n*side,:);
172 closure=Hi*e_*V*tau*Vi_minus*e_';
173 penalty=-Hi*e_*V*tau*Vi_minus;
174 end
175 end
176
177
178 function [closure,penalty]=boundary_condition_general(obj,boundary,L)
179 params=obj.params;
180 x=obj.x; y=obj.y;
181 side=max(length(x),length(y));
182
183 switch boundary
184 case {'w','W','west'}
185 e_=obj.e_w;
186 mat=obj.matrices.A;
187 boundPos='l';
188 Hi=obj.Hxi;
189 [V,Vi,D,signVec]=obj.matrixDiag(mat,x(1),y);
190 L=obj.evaluateCoefficientMatrix(L,x(1),y);
191 case {'e','E','east'}
192 e_=obj.e_e;
193 mat=obj.matrices.A;
194 boundPos='r';
195 Hi=obj.Hxi;
196 [V,Vi,D,signVec]=obj.matrixDiag(mat,x(end),y);
197 L=obj.evaluateCoefficientMatrix(L,x(end),y);
198 case {'s','S','south'}
199 e_=obj.e_s;
200 mat=obj.matrices.B;
201 boundPos='l';
202 Hi=obj.Hxi;
203 [V,Vi,D,signVec]=obj.matrixDiag(mat,x,y(1));
204 L=obj.evaluateCoefficientMatrix(L,x,y(1));
205 case {'n','N','north'}
206 e_=obj.e_n;
207 mat=obj.matrices.B;
208 boundPos='r';
209 Hi=obj.Hxi;
210 [V,Vi,D,signVec]=obj.matrixDiag(mat,x,y(end));
211 L=obj.evaluateCoefficientMatrix(L,x,y(end));
212 end
213
214 pos=signVec(1); zeroval=signVec(2); neg=signVec(3);
215
216 switch boundPos
217 case {'l'}
218 tau=sparse(obj.n*side,pos*side);
219 Vi_plus=Vi(1:pos*side,:);
220 Vi_minus=Vi(pos*side+1:obj.n*side,:);
221 V_plus=V(:,1:pos*side);
222 V_minus=V(:,(pos+zeroval)*side+1:obj.n*side);
223
224 tau(1:pos*side,:)=-abs(D(1:pos*side,1:pos*side));
225 R=-inv(L*V_plus)*(L*V_minus);
226 closure=Hi*e_*V*tau*(Vi_plus-R*Vi_minus)*e_';
227 penalty=-Hi*e_*V*tau*inv(L*V_plus)*L;
228 case {'r'}
229 tau=sparse(obj.n*side,neg*side);
230 tau((pos+zeroval)*side+1:obj.n*side,:)=-abs(D((pos+zeroval)*side+1:obj.n*side,(pos+zeroval)*side+1:obj.n*side));
231 Vi_plus=Vi(1:pos*side,:);
232 Vi_minus=Vi((pos+zeroval)*side+1:obj.n*side,:);
233
234 V_plus=V(:,1:pos*side);
235 V_minus=V(:,(pos+zeroval)*side+1:obj.n*side);
236 R=-inv(L*V_minus)*(L*V_plus);
237 closure=Hi*e_*V*tau*(Vi_minus-R*Vi_plus)*e_';
238 penalty=-Hi*e_*V*tau*inv(L*V_minus)*L;
239 end
240 end
241
242
243 function [V,Vi, D,signVec]=matrixDiag(obj,mat,x,y)
244 params=obj.params;
245 syms xs ys;
246 [V, D]=eig(mat(params,xs,ys));
247 xs=1;ys=1;
248 DD=eval(diag(D));
249
250 poseig=find(DD>0);
251 zeroeig=find(DD==0);
252 negeig=find(DD<0);
253 syms xs ys
254 DD=diag(D);
255
256 D=diag([DD(poseig);DD(zeroeig); DD(negeig)]);
257 V=[V(:,poseig) V(:,zeroeig) V(:,negeig)];
258 xs=x; ys=y;
259
260 side=max(length(x),length(y));
261 Dret=zeros(obj.n,side*obj.n);
262 Vret=zeros(obj.n,side*obj.n);
263 for ii=1:obj.n
264 for jj=1:obj.n
265 Dret(jj,(ii-1)*side+1:side*ii)=eval(D(jj,ii));
266 Vret(jj,(ii-1)*side+1:side*ii)=eval(V(jj,ii));
267 end
268 end
269
270 D=sparse(Dret);
271 V=sparse(normc(Vret));
272 V=obj.evaluateCoefficientMatrix(V,x,y);
273 D=obj.evaluateCoefficientMatrix(D,x,y);
274 Vi=inv(V);
275 signVec=[length(poseig),length(zeroeig),length(negeig)];
276 end
277
278 end
279 end