comparison +multiblock/joinGrids.m @ 1199:dfb1b84b63f4 feature/joingrids

Add multiblock.joinGrids, which joins several multiblock grids into one.
author Martin Almquist <malmquist@stanford.edu>
date Sat, 17 Aug 2019 16:20:07 -0700
parents
children
comparison
equal deleted inserted replaced
1116:33c378e508d2 1199:dfb1b84b63f4
1 % Connects several multiblock grids into one grid
2 % gs -- Cell array of multiblock grids
3 % gConnections -- Upper-triangular cell matrix
4 % gConnections{i,j} specifies all connections between grid i and grid j
5 % Example:
6 % gConnections{i,j} = { {{1,'e'},{2,'w'}}, {{5,'s'},{2,'n'}},... };
7 % names -- (Optional) cell array of strings, used for boundary groups in new grid
8 % default: names = {'g1', 'g2', ..., 'gN'};
9 % Boundary groups from grid i are contained in g.boundaryGroups.(names{i}), etc.
10 function g = joinGrids(gs, gConnections, names)
11
12 nGrids = numel(gs);
13
14 % Default names are {'g1', 'g2', ... 'gN'}.
15 defaultNames = cell(nGrids, 1);
16 for i = 1:nGrids
17 defaultNames{i} = sprintf('g%d',i);
18 end
19 default_arg('names', defaultNames);
20
21 nBlocks = 0;
22 for i = 1:nGrids
23 nBlocks = nBlocks + gs{i}.nBlocks();
24 end
25
26 % Create vector of cumulative sum of number of blocks per grid
27 startIndex = zeros(1, nGrids);
28 for i = 2:nGrids
29 startIndex(i) = startIndex(i-1) + gs{i-1}.nBlocks();
30 end
31
32 % Create cell array of all grids
33 grids = cell(nBlocks, 1);
34 for i = 1:nGrids
35 for j = 1:gs{i}.nBlocks();
36 grids{startIndex(i)+j} = gs{i}.grids{j};
37 end
38 end
39
40 % Create cell matrix of connections
41 connections = cell(nBlocks, nBlocks);
42
43 % Connections within grids
44 for i = 1:nGrids
45 for j = 1:gs{i}.nBlocks()
46 for k = 1:gs{i}.nBlocks()
47 connections{startIndex(i)+j,startIndex(i)+k} = gs{i}.connections{j,k};
48 end
49 end
50 end
51
52 % Connections between grids
53 for i = 1:nGrids
54 for j = 1:nGrids
55 for k = 1:numel(gConnections{i,j})
56 b1 = gConnections{i,j}{k}{1};
57 id1 = b1{1};
58 str1 = b1{2};
59
60 b2 = gConnections{i,j}{k}{2};
61 id2 = b2{1};
62 str2 = b2{2};
63
64 connections{startIndex(i)+id1, startIndex(j)+id2} = {str1, str2};
65 end
66 end
67 end
68
69 % Boundary groups
70 boundaryGroups = struct;
71 for i = 1:nGrids
72 bgs = gs{i}.boundaryGroups;
73 bgNames = fieldnames(bgs);
74 for j = 1:numel(bgNames)
75 bg = bgs.(bgNames{j});
76
77 % Shift block id:s in boundary groups
78 for k = 1:length(bg)
79 bg{k}{1} = bg{k}{1} + startIndex(i);
80 end
81
82 bgs.(bgNames{j}) = bg;
83 end
84 boundaryGroups.(names{i}) = bgs;
85 end
86
87 % Create grid object
88 g = multiblock.Grid(grids, connections, boundaryGroups);
89
90 end