Mercurial > repos > public > sbplib
comparison +scheme/Laplace1dVariable.m @ 1323:3f4e011193f0 feature/laplace1d_variable
First implementation of Laplace1dVariable. Passes symmetry and definiteness tests.
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Sun, 11 Apr 2021 21:10:23 +0200 |
parents | |
children | 56439c1a49b4 |
comparison
equal
deleted
inserted
replaced
1301:8978521b0f06 | 1323:3f4e011193f0 |
---|---|
1 classdef Laplace1dVariable < scheme.Scheme | |
2 properties | |
3 grid | |
4 order % Order accuracy for the approximation | |
5 | |
6 D % scheme operator | |
7 H % Discrete norm | |
8 | |
9 % Variable coefficients a(b u_x)_x | |
10 a | |
11 b | |
12 | |
13 Hi | |
14 e_l | |
15 e_r | |
16 d_l | |
17 d_r | |
18 gamm | |
19 end | |
20 | |
21 methods | |
22 function obj = Laplace1dVariable(g, order, a, b) | |
23 default_arg('a', @(x) 0*x + 1); | |
24 default_arg('b', @(x) 0*x + 1); | |
25 | |
26 assertType(g, 'grid.Cartesian'); | |
27 | |
28 ops = sbp.D2Variable(g.size(), g.lim{1}, order); | |
29 | |
30 obj.H = sparse(ops.H); | |
31 obj.Hi = sparse(ops.HI); | |
32 obj.e_l = sparse(ops.e_l); | |
33 obj.e_r = sparse(ops.e_r); | |
34 obj.d_l = -sparse(ops.d1_l); | |
35 obj.d_r = sparse(ops.d1_r); | |
36 | |
37 | |
38 obj.grid = g; | |
39 obj.order = order; | |
40 | |
41 a = grid.evalOn(g, a); | |
42 b = grid.evalOn(g, b); | |
43 | |
44 A = spdiag(a); | |
45 B = spdiag(b); | |
46 | |
47 obj.a = A; | |
48 obj.b = B; | |
49 | |
50 obj.D = A*ops.D2(b); | |
51 | |
52 obj.gamm = g.h*ops.borrowing.M.d1; | |
53 end | |
54 | |
55 | |
56 % Closure functions return the opertors applied to the own doamin to close the boundary | |
57 % Penalty functions return the opertors to force the solution. In the case of an interface it returns the operator applied to the other doamin. | |
58 % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. | |
59 % type is a string specifying the type of boundary condition if there are several. | |
60 % data is a function returning the data that should be applied at the boundary. | |
61 % neighbour_scheme is an instance of Scheme that should be interfaced to. | |
62 % neighbour_boundary is a string specifying which boundary to interface to. | |
63 function [closure, penalty] = boundary_condition(obj,boundary,type,data) | |
64 default_arg('type','neumann'); | |
65 default_arg('data',0); | |
66 | |
67 e = obj.getBoundaryOperator('e', boundary); | |
68 d = obj.getBoundaryOperator('d', boundary); | |
69 s = obj.getBoundarySign(boundary); | |
70 | |
71 b = e'*obj.b*e; | |
72 | |
73 switch type | |
74 % Dirichlet boundary condition | |
75 case {'D','d','dirichlet'} | |
76 tuning = 1.1; | |
77 tau1 = -tuning/obj.gamm; | |
78 tau2 = 1; | |
79 | |
80 tau = tau1*e + tau2*d; | |
81 | |
82 closure = obj.a*obj.Hi*tau*b*e'; | |
83 penalty = obj.a*obj.Hi*tau*b; | |
84 | |
85 % Neumann boundary condition | |
86 case {'N','n','neumann'} | |
87 tau = -e; | |
88 | |
89 closure = obj.a*obj.Hi*tau*b*d'; | |
90 penalty = -obj.a*obj.Hi*tau*b; | |
91 | |
92 % Unknown, boundary condition | |
93 otherwise | |
94 error('No such boundary condition: type = %s',type); | |
95 end | |
96 end | |
97 | |
98 function [closure, penalty] = interface(obj, boundary, neighbour_scheme, neighbour_boundary, type) | |
99 % u denotes the solution in the own domain | |
100 % v denotes the solution in the neighbour domain | |
101 e_u = obj.getBoundaryOperator('e', boundary); | |
102 d_u = obj.getBoundaryOperator('d', boundary); | |
103 s_u = obj.getBoundarySign(boundary); | |
104 | |
105 e_v = neighbour_scheme.getBoundaryOperator('e', neighbour_boundary); | |
106 d_v = neighbour_scheme.getBoundaryOperator('d', neighbour_boundary); | |
107 s_v = neighbour_scheme.getBoundarySign(neighbour_boundary); | |
108 | |
109 b_u = e_u'*obj.b*e_u; | |
110 b_v = e_v'*neighbour_scheme.b*e_v; | |
111 | |
112 gamm_u = obj.gamm; | |
113 gamm_v = neighbour_scheme.gamm; | |
114 | |
115 tuning = 1.1; | |
116 | |
117 closure = zeros(size(obj.D)); | |
118 penalty = zeros(length(obj.D), length(b_v)); | |
119 | |
120 % Continuity of bu_x | |
121 closure = closure - 1/2*e_u*b_u*d_u'; | |
122 penalty = penalty - 1/2*e_u*b_v*d_v'; | |
123 | |
124 % Continuity of u (symmetrizing term) | |
125 closure = closure + 1/2*d_u*b_u*e_u'; | |
126 penalty = penalty - 1/2*d_u*b_u*e_v'; | |
127 | |
128 % Continuity of u (symmetric term) | |
129 tau = 1/4*(b_u/gamm_u + b_v/gamm_v)*tuning; | |
130 closure = closure - e_u*tau*e_u'; | |
131 penalty = penalty + e_u*tau*e_v'; | |
132 | |
133 % Multiply by Hi and a. | |
134 closure = obj.Hi*obj.a*closure; | |
135 penalty = obj.Hi*obj.a*penalty; | |
136 | |
137 end | |
138 | |
139 % Returns the boundary operator op for the boundary specified by the string boundary. | |
140 % op -- string | |
141 % boundary -- string | |
142 function o = getBoundaryOperator(obj, op, boundary) | |
143 assertIsMember(op, {'e', 'd'}) | |
144 assertIsMember(boundary, {'l', 'r'}) | |
145 | |
146 o = obj.([op, '_', boundary]); | |
147 end | |
148 | |
149 % Returns square boundary quadrature matrix, of dimension | |
150 % corresponding to the number of boundary points | |
151 % | |
152 % boundary -- string | |
153 % Note: for 1d diffOps, the boundary quadrature is the scalar 1. | |
154 function H_b = getBoundaryQuadrature(obj, boundary) | |
155 assertIsMember(boundary, {'l', 'r'}) | |
156 | |
157 H_b = 1; | |
158 end | |
159 | |
160 % Returns the boundary sign. The right boundary is considered the positive boundary | |
161 % boundary -- string | |
162 function s = getBoundarySign(obj, boundary) | |
163 assertIsMember(boundary, {'l', 'r'}) | |
164 | |
165 switch boundary | |
166 case {'r'} | |
167 s = 1; | |
168 case {'l'} | |
169 s = -1; | |
170 end | |
171 end | |
172 | |
173 function N = size(obj) | |
174 N = obj.grid.size(); | |
175 end | |
176 | |
177 end | |
178 end |