Mercurial > repos > public > sbplib
annotate +scheme/Schrodinger.m @ 999:337c4d1dcef5 feature/getBoundaryOp
Fix overlooked Schrodinger1d scheme.
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Sat, 12 Jan 2019 13:44:08 -0800 |
parents | 706d1c2b4199 |
children | 8d73fcdb07a5 |
rev | line source |
---|---|
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
1 classdef Schrodinger < scheme.Scheme |
0 | 2 properties |
3 m % Number of points in each direction, possibly a vector | |
4 h % Grid spacing | |
5 x % Grid | |
6 order % Order accuracy for the approximation | |
7 | |
8 D % non-stabalized scheme operator | |
9 H % Discrete norm | |
10 M % Derivative norm | |
11 alpha | |
12 | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
13 D2 |
0 | 14 Hi |
15 e_l | |
16 e_r | |
17 d1_l | |
18 d1_r | |
19 gamm | |
20 end | |
21 | |
22 methods | |
23 % Solving SE in the form u_t = i*u_xx -i*V; | |
24 function obj = Schrodinger(m,xlim,order,V) | |
25 default_arg('V',0); | |
26 | |
27 [x, h] = util.get_grid(xlim{:},m); | |
28 | |
29 ops = sbp.Ordinary(m,h,order); | |
30 | |
31 obj.D2 = sparse(ops.derivatives.D2); | |
32 obj.H = sparse(ops.norms.H); | |
33 obj.Hi = sparse(ops.norms.HI); | |
34 obj.M = sparse(ops.norms.M); | |
35 obj.e_l = sparse(ops.boundary.e_1); | |
36 obj.e_r = sparse(ops.boundary.e_m); | |
37 obj.d1_l = sparse(ops.boundary.S_1); | |
38 obj.d1_r = sparse(ops.boundary.S_m); | |
39 | |
40 | |
41 if isa(V,'function_handle') | |
42 V_vec = V(x); | |
43 else | |
44 V_vec = x*0 + V; | |
45 end | |
46 | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
47 V_mat = spdiags(V_vec,0,m,m); |
0 | 48 |
67
446d67a49cd8
Fixed some errors in scheme.Schrodinger.m
Jonatan Werpers <jonatan@werpers.com>
parents:
65
diff
changeset
|
49 obj.D = 1i * obj.D2 - 1i * V_mat; |
0 | 50 |
51 obj.m = m; | |
52 obj.h = h; | |
53 obj.order = order; | |
54 | |
55 obj.x = x; | |
56 end | |
57 | |
58 | |
59 % Closure functions return the opertors applied to the own doamin to close the boundary | |
60 % Penalty functions return the opertors to force the solution. In the case of an interface it returns the operator applied to the other doamin. | |
61 % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. | |
62 % type is a string specifying the type of boundary condition if there are several. | |
63 % data is a function returning the data that should be applied at the boundary. | |
64 % neighbour_scheme is an instance of Scheme that should be interfaced to. | |
65 % neighbour_boundary is a string specifying which boundary to interface to. | |
66 function [closure, penalty] = boundary_condition(obj,boundary,type,data) | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
67 default_arg('type','dirichlet'); |
0 | 68 default_arg('data',0); |
69 | |
999
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
70 [e, d] = obj.getBoundaryOperator({'e', 'd'}, boundary); |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
71 s = obj.getBoundarySign(boundary); |
0 | 72 |
73 switch type | |
74 % Dirichlet boundary condition | |
75 case {'D','d','dirichlet'} | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
76 tau = s * 1i*d; |
0 | 77 closure = obj.Hi*tau*e'; |
78 | |
79 switch class(data) | |
80 case 'double' | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
81 penalty = -obj.Hi*tau*data; |
0 | 82 case 'function_handle' |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
83 penalty = @(t)-obj.Hi*tau*data(t); |
0 | 84 otherwise |
85 error('Wierd data argument!') | |
86 end | |
87 | |
88 % Unknown, boundary condition | |
89 otherwise | |
90 error('No such boundary condition: type = %s',type); | |
91 end | |
92 end | |
93 | |
946
706d1c2b4199
Raname opts to type in a bunch of interface methods
Jonatan Werpers <jonatan@werpers.com>
parents:
910
diff
changeset
|
94 function [closure, penalty] = interface(obj, boundary, neighbour_scheme, neighbour_boundary, type) |
0 | 95 % u denotes the solution in the own domain |
96 % v denotes the solution in the neighbour domain | |
999
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
97 [e_u, d_u] = obj.getBoundaryOperator({'e', 'd'}, boundary); |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
98 s_u = obj.getBoundarySign(boundary); |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
99 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
100 [e_v, d_v] = neighbour_scheme.getBoundaryOperator({'e', 'd'}, neighbour_boundary); |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
101 s_v = neighbour_scheme.getBoundarySign(neighbour_boundary); |
0 | 102 |
67
446d67a49cd8
Fixed some errors in scheme.Schrodinger.m
Jonatan Werpers <jonatan@werpers.com>
parents:
65
diff
changeset
|
103 a = -s_u* 1/2 * 1i ; |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
104 b = a'; |
0 | 105 |
106 tau = b*d_u; | |
67
446d67a49cd8
Fixed some errors in scheme.Schrodinger.m
Jonatan Werpers <jonatan@werpers.com>
parents:
65
diff
changeset
|
107 sig = -a*e_u; |
0 | 108 |
109 closure = obj.Hi * (tau*e_u' + sig*d_u'); | |
110 penalty = obj.Hi * (-tau*e_v' - sig*d_v'); | |
111 end | |
112 | |
999
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
113 % Returns the boundary operator op for the boundary specified by the string boundary. |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
114 % op -- string or a cell array of strings |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
115 % boundary -- string |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
116 function varargout = getBoundaryOperator(obj, op, boundary) |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
117 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
118 if ~iscell(op) |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
119 op = {op}; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
120 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
121 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
122 for i = 1:numel(op) |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
123 switch op{i} |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
124 case 'e' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
125 switch boundary |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
126 case 'l' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
127 e = obj.e_l; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
128 case 'r' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
129 e = obj.e_r; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
130 otherwise |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
131 error('No such boundary: boundary = %s',boundary); |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
132 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
133 varargout{i} = e; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
134 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
135 case 'd' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
136 switch boundary |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
137 case 'l' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
138 d = obj.d1_l; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
139 case 'r' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
140 d = obj.d1_r; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
141 otherwise |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
142 error('No such boundary: boundary = %s',boundary); |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
143 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
144 varargout{i} = d; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
145 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
146 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
147 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
148 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
149 % Returns the boundary sign. The right boundary is considered the positive boundary |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
150 % boundary -- string |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
151 function s = getBoundarySign(obj, boundary) |
0 | 152 switch boundary |
999
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
153 case {'r'} |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
154 s = 1; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
155 case {'l'} |
0 | 156 s = -1; |
157 otherwise | |
158 error('No such boundary: boundary = %s',boundary); | |
159 end | |
160 end | |
161 | |
162 function N = size(obj) | |
163 N = obj.m; | |
164 end | |
165 | |
166 end | |
167 | |
168 methods(Static) | |
169 % Calculates the matrcis need for the inteface coupling between boundary bound_u of scheme schm_u | |
170 % and bound_v of scheme schm_v. | |
171 % [uu, uv, vv, vu] = inteface_couplong(A,'r',B,'l') | |
172 function [uu, uv, vv, vu] = interface_coupling(schm_u,bound_u,schm_v,bound_v) | |
173 [uu,uv] = schm_u.interface(bound_u,schm_v,bound_v); | |
174 [vv,vu] = schm_v.interface(bound_v,schm_u,bound_u); | |
175 end | |
176 end | |
177 end |