comparison +multiblock/DiffOp.m @ 427:a613960a157b feature/quantumTriangles

merged with feature/beams
author Ylva Rydin <ylva.rydin@telia.com>
date Thu, 26 Jan 2017 15:59:25 +0100
parents 30ff8879162e
children 225765e345c4 324c927d8b1d
comparison
equal deleted inserted replaced
426:29944ea7674b 427:a613960a157b
1 classdef DiffOp < scheme.Scheme
2 properties
3 grid
4 order
5 diffOps
6 D
7 H
8
9 blockmatrixDiv
10 end
11
12 methods
13 function obj = DiffOp(doHand, grid, order, doParam)
14 % doHand -- may either be a function handle or a cell array of
15 % function handles for each grid. The function handle(s)
16 % should be on the form do = doHand(grid, order, ...)
17 % Additional parameters for each doHand may be provided in
18 % the doParam input.
19 % grid -- a multiblock grid
20 % order -- integer specifying the order of accuracy
21 % doParam -- may either be a cell array or a cell array of cell arrays
22 % for each block. If it is a cell array with length equal
23 % to the number of blocks then each element is sent to the
24 % corresponding function handle as extra parameters:
25 % doHand(..., doParam{i}{:}) Otherwise doParam is sent as
26 % extra parameters to all doHand: doHand(..., doParam{:})
27 default_arg('doParam', [])
28
29 [getHand, getParam] = parseInput(doHand, grid, doParam);
30
31 nBlocks = grid.nBlocks();
32
33 obj.order = order;
34
35 % Create the diffOps for each block
36 obj.diffOps = cell(1, nBlocks);
37 for i = 1:nBlocks
38 h = getHand(i);
39 p = getParam(i);
40 if ~iscell(p)
41 p = {p};
42 end
43 obj.diffOps{i} = h(grid.grids{i}, order, p{:});
44 end
45
46
47 % Build the norm matrix
48 H = cell(nBlocks, nBlocks);
49 for i = 1:nBlocks
50 H{i,i} = obj.diffOps{i}.H;
51 end
52 obj.H = blockmatrix.toMatrix(H);
53
54
55 % Build the differentiation matrix
56 obj.blockmatrixDiv = {grid.Ns, grid.Ns};
57 D = blockmatrix.zero(obj.blockmatrixDiv);
58 for i = 1:nBlocks
59 D{i,i} = obj.diffOps{i}.D;
60 end
61
62 for i = 1:nBlocks
63 for j = 1:nBlocks
64 intf = grid.connections{i,j};
65 if isempty(intf)
66 continue
67 end
68
69
70 [ii, ij] = obj.diffOps{i}.interface(intf{1}, obj.diffOps{j}, intf{2});
71 D{i,i} = D{i,i} + ii;
72 D{i,j} = D{i,j} + ij;
73
74 [jj, ji] = obj.diffOps{j}.interface(intf{2}, obj.diffOps{i}, intf{1});
75 D{j,j} = D{j,j} + jj;
76 D{j,i} = D{j,i} + ji;
77 end
78 end
79 obj.D = blockmatrix.toMatrix(D);
80
81
82 function [getHand, getParam] = parseInput(doHand, grid, doParam)
83 if ~isa(grid, 'multiblock.Grid')
84 error('multiblock:DiffOp:DiffOp:InvalidGrid', 'Requires a multiblock grid.');
85 end
86
87 if iscell(doHand) && length(doHand) == grid.nBlocks()
88 getHand = @(i)doHand{i};
89 elseif isa(doHand, 'function_handle')
90 getHand = @(i)doHand;
91 else
92 error('multiblock:DiffOp:DiffOp:InvalidGridDoHand', 'doHand must be a function handle or a cell array of length grid.nBlocks');
93 end
94
95 if isempty(doParam)
96 getParam = @(i){};
97 return
98 end
99
100 if ~iscell(doParam)
101 getParam = @(i)doParam;
102 return
103 end
104
105 % doParam is a non-empty cell-array
106
107 if length(doParam) == grid.nBlocks() && all(cellfun(@iscell, doParam))
108 % doParam is a cell-array of cell-arrays
109 getParam = @(i)doParam{i};
110 return
111 end
112
113 getParam = @(i)doParam;
114 end
115 end
116
117 function ops = splitOp(obj, op)
118 % Splits a matrix operator into a cell-matrix of matrix operators for
119 % each grid.
120 ops = sparse2cell(op, obj.NNN);
121 end
122
123 function op = getBoundaryOperator(obj, op, boundary)
124 if iscell(boundary)
125 localOpName = [op '_' boundary{2}];
126 blockId = boundary{1};
127 localOp = obj.diffOps{blockId}.(localOpName);
128
129 div = {obj.blockmatrixDiv{1}, size(localOp,2)};
130 blockOp = blockmatrix.zero(div);
131 blockOp{blockId,1} = localOp;
132 op = blockmatrix.toMatrix(blockOp);
133 return
134 else
135 % Boundary är en sträng med en boundary group i.
136 end
137 end
138
139 % Creates the closure and penalty matrix for a given boundary condition,
140 % boundary -- the name of the boundary on the form {id,name} where
141 % id is the number of a block and name is the name of a
142 % boundary of that block example: {1,'s'} or {3,'w'}
143 function [closure, penalty] = boundary_condition(obj, boundary, type)
144 I = boundary{1};
145 name = boundary{2};
146
147 % Get the closure and penaly matrices
148 [blockClosure, blockPenalty] = obj.diffOps{I}.boundary_condition(name, type);
149
150 % Expand to matrix for full domain.
151 div = obj.blockmatrixDiv;
152 if ~iscell(blockClosure)
153 temp = blockmatrix.zero(div);
154 temp{I,I} = blockClosure;
155 closure = blockmatrix.toMatrix(temp);
156 else
157 for i = 1:length(blockClosure)
158 temp = blockmatrix.zero(div);
159 temp{I,I} = blockClosure{i};
160 closure{i} = blockmatrix.toMatrix(temp);
161 end
162 end
163
164 div{2} = size(blockPenalty, 2); % Penalty is a column vector
165 if ~iscell(blockPenalty)
166 p = blockmatrix.zero(div);
167 p{I} = blockPenalty;
168 penalty = blockmatrix.toMatrix(p);
169 else
170 for i = 1:length(blockPenalty)
171 p = blockmatrix.zero(div);
172 p{I} = blockPenalty{i};
173 penalty{i} = blockmatrix.toMatrix(p);
174 end
175 end
176 end
177
178 function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary)
179
180 end
181
182 % Size returns the number of degrees of freedom
183 function N = size(obj)
184 N = 0;
185 for i = 1:length(obj.diffOps)
186 N = N + obj.diffOps{i}.size();
187 end
188 end
189 end
190 end