Mercurial > repos > public > sbplib
annotate +scheme/Schrodinger.m @ 501:b7280c6c6b9a feature/quantumTriangles
Found some small bugs when making slides for pressentation
author | Ylva Rydin <ylva.rydin@telia.com> |
---|---|
date | Tue, 25 Apr 2017 09:52:32 +0200 |
parents | b91d23271481 |
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 | |
428
a39fe3bcbd95
Changed so that the shrodinger scheme works with the grids
Ylva Rydin <ylva.rydin@telia.com>
parents:
426
diff
changeset
|
7 grid |
a39fe3bcbd95
Changed so that the shrodinger scheme works with the grids
Ylva Rydin <ylva.rydin@telia.com>
parents:
426
diff
changeset
|
8 |
0 | 9 D % non-stabalized scheme operator |
10 H % Discrete norm | |
11 M % Derivative norm | |
12 alpha | |
13 | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
14 D2 |
0 | 15 Hi |
16 e_l | |
17 e_r | |
18 d1_l | |
19 d1_r | |
20 gamm | |
21 end | |
22 | |
23 methods | |
24 % Solving SE in the form u_t = i*u_xx -i*V; | |
495
b91d23271481
Added new penalty parameter to the interface in shrodinger curve
Ylva Rydin <ylva.rydin@telia.com>
parents:
428
diff
changeset
|
25 function obj = Schrodinger(g,order,V) |
0 | 26 default_arg('V',0); |
495
b91d23271481
Added new penalty parameter to the interface in shrodinger curve
Ylva Rydin <ylva.rydin@telia.com>
parents:
428
diff
changeset
|
27 obj.grid = g; |
b91d23271481
Added new penalty parameter to the interface in shrodinger curve
Ylva Rydin <ylva.rydin@telia.com>
parents:
428
diff
changeset
|
28 m = N(obj.grid); |
b91d23271481
Added new penalty parameter to the interface in shrodinger curve
Ylva Rydin <ylva.rydin@telia.com>
parents:
428
diff
changeset
|
29 obj.x = points(obj.grid); |
b91d23271481
Added new penalty parameter to the interface in shrodinger curve
Ylva Rydin <ylva.rydin@telia.com>
parents:
428
diff
changeset
|
30 ops = sbp.D2Standard(m,{obj.x(1) obj.x(end)},order); |
426
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
31 |
495
b91d23271481
Added new penalty parameter to the interface in shrodinger curve
Ylva Rydin <ylva.rydin@telia.com>
parents:
428
diff
changeset
|
32 |
b91d23271481
Added new penalty parameter to the interface in shrodinger curve
Ylva Rydin <ylva.rydin@telia.com>
parents:
428
diff
changeset
|
33 obj.h = ops.h; |
426
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
34 obj.D2 = ops.D2; |
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
35 obj.H = ops.H; |
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
36 obj.Hi = ops.HI; |
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
37 obj.M = ops.M; |
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
38 obj.e_l = ops.e_l; |
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
39 obj.e_r = ops.e_r; |
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
40 obj.d1_l = ops.d1_l; |
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
41 obj.d1_r = ops.d1_r; |
0 | 42 |
43 | |
44 if isa(V,'function_handle') | |
426
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
45 V_vec = V(obj.x); |
0 | 46 else |
426
29944ea7674b
Updated the Shrodinger scheme to the new operator syntax
Ylva Rydin <ylva.rydin@telia.com>
parents:
67
diff
changeset
|
47 V_vec = obj.x*0 + V; |
0 | 48 end |
49 | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
50 V_mat = spdiags(V_vec,0,m,m); |
0 | 51 |
67
446d67a49cd8
Fixed some errors in scheme.Schrodinger.m
Jonatan Werpers <jonatan@werpers.com>
parents:
65
diff
changeset
|
52 obj.D = 1i * obj.D2 - 1i * V_mat; |
0 | 53 |
54 obj.m = m; | |
55 obj.order = order; | |
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 | |
70 [e,d,s] = obj.get_boundary_ops(boundary); | |
71 | |
72 switch type | |
73 % Dirichlet boundary condition | |
74 case {'D','d','dirichlet'} | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
75 tau = s * 1i*d; |
0 | 76 closure = obj.Hi*tau*e'; |
77 | |
78 switch class(data) | |
79 case 'double' | |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
80 penalty = -obj.Hi*tau*data; |
0 | 81 case 'function_handle' |
65
33f0654a2413
Fixed mistakes in Schrodinger scheme. BC are now working.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
82 penalty = @(t)-obj.Hi*tau*data(t); |
0 | 83 otherwise |
84 error('Wierd data argument!') | |
85 end | |
86 | |
87 % Unknown, boundary condition | |
88 otherwise | |
89 error('No such boundary condition: type = %s',type); | |
90 end | |
91 end | |
92 | |
93 function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary) | |
94 % u denotes the solution in the own domain | |
95 % v denotes the solution in the neighbour domain | |
96 [e_u,d_u,s_u] = obj.get_boundary_ops(boundary); | |
97 [e_v,d_v,s_v] = neighbour_scheme.get_boundary_ops(neighbour_boundary); | |
98 | |
67
446d67a49cd8
Fixed some errors in scheme.Schrodinger.m
Jonatan Werpers <jonatan@werpers.com>
parents:
65
diff
changeset
|
99 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
|
100 b = a'; |
0 | 101 |
102 tau = b*d_u; | |
67
446d67a49cd8
Fixed some errors in scheme.Schrodinger.m
Jonatan Werpers <jonatan@werpers.com>
parents:
65
diff
changeset
|
103 sig = -a*e_u; |
0 | 104 |
105 closure = obj.Hi * (tau*e_u' + sig*d_u'); | |
106 penalty = obj.Hi * (-tau*e_v' - sig*d_v'); | |
107 end | |
108 | |
109 % Ruturns the boundary ops and sign for the boundary specified by the string boundary. | |
110 % The right boundary is considered the positive boundary | |
111 function [e,d,s] = get_boundary_ops(obj,boundary) | |
112 switch boundary | |
113 case 'l' | |
114 e = obj.e_l; | |
115 d = obj.d1_l; | |
116 s = -1; | |
117 case 'r' | |
118 e = obj.e_r; | |
119 d = obj.d1_r; | |
120 s = 1; | |
121 otherwise | |
122 error('No such boundary: boundary = %s',boundary); | |
123 end | |
124 end | |
125 | |
126 function N = size(obj) | |
127 N = obj.m; | |
128 end | |
129 | |
130 end | |
131 | |
132 methods(Static) | |
133 % Calculates the matrcis need for the inteface coupling between boundary bound_u of scheme schm_u | |
134 % and bound_v of scheme schm_v. | |
135 % [uu, uv, vv, vu] = inteface_couplong(A,'r',B,'l') | |
136 function [uu, uv, vv, vu] = interface_coupling(schm_u,bound_u,schm_v,bound_v) | |
137 [uu,uv] = schm_u.interface(bound_u,schm_v,bound_v); | |
138 [vv,vu] = schm_v.interface(bound_v,schm_u,bound_u); | |
139 end | |
140 end | |
141 end |