Mercurial > repos > public > sbplib
comparison +scheme/Schrodinger.m @ 0:48b6fb693025
Initial commit.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 17 Sep 2015 10:12:50 +0200 |
parents | |
children | 33f0654a2413 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:48b6fb693025 |
---|---|
1 classdef Schrodinger < noname.Scheme | |
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 | |
13 Hi | |
14 e_l | |
15 e_r | |
16 d1_l | |
17 d1_r | |
18 gamm | |
19 end | |
20 | |
21 methods | |
22 % Solving SE in the form u_t = i*u_xx -i*V; | |
23 function obj = Schrodinger(m,xlim,order,V) | |
24 default_arg('V',0); | |
25 | |
26 [x, h] = util.get_grid(xlim{:},m); | |
27 | |
28 ops = sbp.Ordinary(m,h,order); | |
29 | |
30 obj.D2 = sparse(ops.derivatives.D2); | |
31 obj.H = sparse(ops.norms.H); | |
32 obj.Hi = sparse(ops.norms.HI); | |
33 obj.M = sparse(ops.norms.M); | |
34 obj.e_l = sparse(ops.boundary.e_1); | |
35 obj.e_r = sparse(ops.boundary.e_m); | |
36 obj.d1_l = sparse(ops.boundary.S_1); | |
37 obj.d1_r = sparse(ops.boundary.S_m); | |
38 | |
39 | |
40 if isa(V,'function_handle') | |
41 V_vec = V(x); | |
42 else | |
43 V_vec = x*0 + V; | |
44 end | |
45 | |
46 V_mat = spdiags(V,0,m,m); | |
47 | |
48 | |
49 D = 1i * obj.D2 - 1i * V; | |
50 | |
51 obj.m = m; | |
52 obj.h = h; | |
53 obj.order = order; | |
54 | |
55 obj.D = alpha*obj.D2; | |
56 obj.x = x; | |
57 end | |
58 | |
59 | |
60 % Closure functions return the opertors applied to the own doamin to close the boundary | |
61 % Penalty functions return the opertors to force the solution. In the case of an interface it returns the operator applied to the other doamin. | |
62 % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. | |
63 % type is a string specifying the type of boundary condition if there are several. | |
64 % data is a function returning the data that should be applied at the boundary. | |
65 % neighbour_scheme is an instance of Scheme that should be interfaced to. | |
66 % neighbour_boundary is a string specifying which boundary to interface to. | |
67 function [closure, penalty] = boundary_condition(obj,boundary,type,data) | |
68 default_arg('type','neumann'); | |
69 default_arg('data',0); | |
70 | |
71 [e,d,s] = obj.get_boundary_ops(boundary); | |
72 | |
73 switch type | |
74 % Dirichlet boundary condition | |
75 case {'D','d','dirichlet'} | |
76 tau = -1i* s * d; | |
77 | |
78 closure = obj.Hi*tau*e'; | |
79 | |
80 pp = obj.Hi*p; | |
81 switch class(data) | |
82 case 'double' | |
83 penalty = pp*data; | |
84 case 'function_handle' | |
85 penalty = @(t)pp*data(t); | |
86 otherwise | |
87 error('Wierd data argument!') | |
88 end | |
89 | |
90 % Unknown, boundary condition | |
91 otherwise | |
92 error('No such boundary condition: type = %s',type); | |
93 end | |
94 end | |
95 | |
96 function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary) | |
97 % u denotes the solution in the own domain | |
98 % v denotes the solution in the neighbour domain | |
99 [e_u,d_u,s_u] = obj.get_boundary_ops(boundary); | |
100 [e_v,d_v,s_v] = neighbour_scheme.get_boundary_ops(neighbour_boundary); | |
101 | |
102 a = s/2 * 1i ; | |
103 b = - a'; | |
104 | |
105 tau = b*d_u; | |
106 sig = a*e_u; | |
107 | |
108 closure = obj.Hi * (tau*e_u' + sig*d_u'); | |
109 penalty = obj.Hi * (-tau*e_v' - sig*d_v'); | |
110 end | |
111 | |
112 % Ruturns the boundary ops and sign for the boundary specified by the string boundary. | |
113 % The right boundary is considered the positive boundary | |
114 function [e,d,s] = get_boundary_ops(obj,boundary) | |
115 switch boundary | |
116 case 'l' | |
117 e = obj.e_l; | |
118 d = obj.d1_l; | |
119 s = -1; | |
120 case 'r' | |
121 e = obj.e_r; | |
122 d = obj.d1_r; | |
123 s = 1; | |
124 otherwise | |
125 error('No such boundary: boundary = %s',boundary); | |
126 end | |
127 end | |
128 | |
129 function N = size(obj) | |
130 N = obj.m; | |
131 end | |
132 | |
133 end | |
134 | |
135 methods(Static) | |
136 % Calculates the matrcis need for the inteface coupling between boundary bound_u of scheme schm_u | |
137 % and bound_v of scheme schm_v. | |
138 % [uu, uv, vv, vu] = inteface_couplong(A,'r',B,'l') | |
139 function [uu, uv, vv, vu] = interface_coupling(schm_u,bound_u,schm_v,bound_v) | |
140 [uu,uv] = schm_u.interface(bound_u,schm_v,bound_v); | |
141 [vv,vu] = schm_v.interface(bound_v,schm_u,bound_u); | |
142 end | |
143 end | |
144 end |