Mercurial > repos > public > sbplib
comparison +multiblock/Grid.m @ 886:8894e9c49e40 feature/timesteppers
Merge with default for latest changes
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Thu, 15 Nov 2018 16:36:21 -0800 |
parents | a55d3c1e1f83 |
children | 67228a10dfad |
comparison
equal
deleted
inserted
replaced
816:b5e5b195da1e | 886:8894e9c49e40 |
---|---|
1 classdef Grid < grid.Grid | |
2 properties | |
3 grids | |
4 connections | |
5 boundaryGroups | |
6 | |
7 nPoints | |
8 end | |
9 | |
10 % General multiblock grid | |
11 methods | |
12 % grids -- cell array of N grids | |
13 % connections -- NxN upper triangular cell matrix. connections{i,j} | |
14 % specifies the connection between block i and j. If | |
15 % it's empty there is no connection otherwise it's a 2 | |
16 % -cell-vector with strings naming the boundaries to be | |
17 % connected. (inverted coupling?) | |
18 % boundaryGroups -- A struct of BoundaryGroups. The field names of the | |
19 % struct are the names of each boundary group. | |
20 % The boundary groups can be used to collect block | |
21 % boundaries into physical boundaries to simplify | |
22 % getting boundary operators and setting boundary conditions | |
23 function obj = Grid(grids, connections, boundaryGroups) | |
24 default_arg('boundaryGroups', struct()); | |
25 assertType(grids, 'cell') | |
26 obj.grids = grids; | |
27 obj.connections = connections; | |
28 | |
29 obj.nPoints = 0; | |
30 for i = 1:length(grids) | |
31 obj.nPoints = obj.nPoints + grids{i}.N(); | |
32 end | |
33 | |
34 obj.boundaryGroups = boundaryGroups; | |
35 end | |
36 | |
37 function n = size(obj) | |
38 n = length(obj.grids); | |
39 end | |
40 | |
41 % N returns the number of points in the grid | |
42 function o = N(obj) | |
43 o = obj.nPoints; | |
44 end | |
45 | |
46 % Ns returns the number of points in each sub grid as a vector | |
47 function o = Ns(obj) | |
48 ns = zeros(1,obj.nBlocks); | |
49 for i = 1:obj.nBlocks | |
50 ns(i) = obj.grids{i}.N(); | |
51 end | |
52 o = ns; | |
53 end | |
54 | |
55 function n = nBlocks(obj) | |
56 n = length(obj.grids); | |
57 end | |
58 | |
59 % d returns the spatial dimension of the grid | |
60 function o = D(obj) | |
61 o = obj.grids{1}.D(); | |
62 end | |
63 | |
64 % points returns a n x d matrix containing the coordinates for all points. | |
65 function X = points(obj) | |
66 X = sparse(0,obj.D()); | |
67 for i = 1:length(obj.grids) | |
68 X = [X; obj.grids{i}.points]; | |
69 end | |
70 end | |
71 | |
72 % Split a grid function on obj to a cell array of grid function on each block | |
73 function gfs = splitFunc(obj, gf) | |
74 nComponents = length(gf)/obj.nPoints; | |
75 nBlocks = length(obj.grids); | |
76 | |
77 % Collect number of points in each block | |
78 N = zeros(1,nBlocks); | |
79 for i = 1:nBlocks | |
80 N(i) = obj.grids{i}.N()*nComponents; | |
81 end | |
82 | |
83 gfs = blockmatrix.fromMatrix(gf, {N,1}); | |
84 end | |
85 | |
86 % TODO: Split op? | |
87 % Should the method to split an operator be moved here instead of being in multiblock.DiffOp? | |
88 | |
89 % Converts a gridfunction to a set of plot matrices | |
90 % Takes a grid function and and a structured grid. | |
91 function F = funcToPlotMatrices(obj, gf) | |
92 % TODO: This method should problably not be here. | |
93 % The funcToPlotMatrix uses .size poperty of the grids | |
94 % Which doesn't always exist for all types of grids. | |
95 % It's only valid for structured grids | |
96 gfs = obj.splitFunc(gf); | |
97 | |
98 F = cell(1, obj.nBlocks()); | |
99 | |
100 for i = 1:obj.nBlocks() | |
101 F{i} = grid.funcToPlotMatrix(obj.grids{i}, gfs{i}); | |
102 end | |
103 end | |
104 | |
105 | |
106 % Restricts the grid function gf on obj to the subgrid g. | |
107 function gf = restrictFunc(obj, gf, g) | |
108 gfs = obj.splitFunc(gf); | |
109 | |
110 for i = 1:length(obj.grids) | |
111 gfs{i} = obj.grids{i}.restrictFunc(gfs{i}, g.grids{i}); | |
112 end | |
113 | |
114 gf = cell2mat(gfs); | |
115 end | |
116 | |
117 % Projects the grid function gf on obj to the grid g. | |
118 function o = projectFunc(obj, gf, g) | |
119 error('not implemented') | |
120 | |
121 p = g.points(); | |
122 o = zeros(length(p),1); | |
123 for i = 1:length(p) | |
124 I = whatGrid(p(i)); | |
125 o(i) = obj.grids{I}.projectFunc(gf, p(i)); | |
126 end | |
127 | |
128 | |
129 function I = whatGrid(p) | |
130 % Find what grid a point lies on | |
131 end | |
132 | |
133 end | |
134 | |
135 % Find all non interface boundaries of all blocks. | |
136 % Return their grid.boundaryIdentifiers in a cell array. | |
137 function bs = getBoundaryNames(obj) | |
138 bs = {}; | |
139 for i = 1:obj.nBlocks() | |
140 candidates = obj.grids{i}.getBoundaryNames(); | |
141 for j = 1:obj.nBlocks() | |
142 if ~isempty(obj.connections{i,j}) | |
143 candidates = setdiff(candidates, obj.connections{i,j}{1}); | |
144 end | |
145 | |
146 if ~isempty(obj.connections{j,i}) | |
147 candidates = setdiff(candidates, obj.connections{j,i}{2}); | |
148 end | |
149 end | |
150 | |
151 for k = 1:length(candidates) | |
152 bs{end+1} = {i, candidates{k}}; | |
153 end | |
154 end | |
155 end | |
156 | |
157 % Return coordinates for the given boundary/boundaryGroup | |
158 function b = getBoundary(obj, boundary) | |
159 switch class(boundary) | |
160 case 'cell' | |
161 I = boundary{1}; | |
162 name = boundary{2}; | |
163 b = obj.grids{I}.getBoundary(name); | |
164 case 'multiblock.BoundaryGroup' | |
165 b = sparse(0,obj.D()); | |
166 for i = 1:length(boundary) | |
167 b = [b; obj.getBoundary(boundary{i})]; | |
168 end | |
169 otherwise | |
170 error('Unknown boundary indentifier') | |
171 end | |
172 end | |
173 end | |
174 end |