Mercurial > repos > public > sbplib
annotate +scheme/Schrodinger.m @ 1301:8978521b0f06 default
Fix incorrect package name.
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Wed, 08 Jul 2020 19:11:04 +0200 |
parents | 0c504a21432d |
children |
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) |
1042
8d73fcdb07a5
Add asserts to boundary identifier inputs
Jonatan Werpers <jonatan@werpers.com>
parents:
999
diff
changeset
|
117 assertIsMember(boundary, {'l', 'r'}) |
999
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
118 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
119 if ~iscell(op) |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
120 op = {op}; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
121 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
122 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
123 for i = 1:numel(op) |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
124 switch op{i} |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
125 case 'e' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
126 switch boundary |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
127 case 'l' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
128 e = obj.e_l; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
129 case 'r' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
130 e = obj.e_r; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
131 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
132 varargout{i} = e; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
133 |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
134 case 'd' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
135 switch boundary |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
136 case 'l' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
137 d = obj.d1_l; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
138 case 'r' |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
139 d = obj.d1_r; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
140 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
141 varargout{i} = d; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
142 end |
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 end |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
145 |
1049
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
146 % Returns square boundary quadrature matrix, of dimension |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
147 % corresponding to the number of boundary points |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
148 % |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
149 % boundary -- string |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
150 % Note: for 1d diffOps, the boundary quadrature is the scalar 1. |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
151 function H_b = getBoundaryQuadrature(obj, boundary) |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
152 assertIsMember(boundary, {'l', 'r'}) |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
153 |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
154 H_b = 1; |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
155 end |
0c504a21432d
Add getBoundaryQuadrature to all 1d diffOps
Martin Almquist <malmquist@stanford.edu>
parents:
1044
diff
changeset
|
156 |
999
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
157 % 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
|
158 % boundary -- string |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
159 function s = getBoundarySign(obj, boundary) |
1042
8d73fcdb07a5
Add asserts to boundary identifier inputs
Jonatan Werpers <jonatan@werpers.com>
parents:
999
diff
changeset
|
160 assertIsMember(boundary, {'l', 'r'}) |
8d73fcdb07a5
Add asserts to boundary identifier inputs
Jonatan Werpers <jonatan@werpers.com>
parents:
999
diff
changeset
|
161 |
0 | 162 switch boundary |
999
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
163 case {'r'} |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
164 s = 1; |
337c4d1dcef5
Fix overlooked Schrodinger1d scheme.
Martin Almquist <malmquist@stanford.edu>
parents:
946
diff
changeset
|
165 case {'l'} |
0 | 166 s = -1; |
167 end | |
168 end | |
169 | |
170 function N = size(obj) | |
171 N = obj.m; | |
172 end | |
173 | |
174 end | |
1043
c12b84fe9b00
Remove static method `interface_coupling` that shouldn't have existed in the first place
Jonatan Werpers <jonatan@werpers.com>
parents:
946
diff
changeset
|
175 end |