comparison +multiblock/+domain/Line.m @ 1061:c7b619cf5e34

Add multiblock domain Line
author Martin Almquist <malmquist@stanford.edu>
date Thu, 07 Feb 2019 18:42:50 -0800
parents
children 532b58a9e849
comparison
equal deleted inserted replaced
1054:77676c26056d 1061:c7b619cf5e34
1 classdef Line < multiblock.Definition
2 properties
3
4 xlims
5 blockNames % Cell array of block labels
6 nBlocks
7 connections % Cell array specifying connections between blocks
8 boundaryGroups % Structure of boundaryGroups
9
10 end
11
12
13 methods
14 % Creates a divided line
15 % x is a vector of boundary and interface positions.
16 % blockNames: cell array of labels. The id is default.
17 function obj = Line(x,blockNames)
18 default_arg('blockNames',[]);
19
20 N = length(x)-1; % number of blocks in the x direction.
21
22 if ~issorted(x)
23 error('The elements of x seem to be in the wrong order');
24 end
25
26 % Dimensions of blocks and number of points
27 blockTi = cell(N,1);
28 xlims = cell(N,1);
29 for i = 1:N
30 xlims{i} = {x(i), x(i+1)};
31 end
32
33 % Interface couplings
34 conn = cell(N,N);
35 for i = 1:N
36 conn{i,i+1} = {'r','l'};
37 end
38
39 % Block names (id number as default)
40 if isempty(blockNames)
41 obj.blockNames = cell(1, N);
42 for i = 1:N
43 obj.blockNames{i} = sprintf('%d', i);
44 end
45 else
46 assert(length(blockNames) == N);
47 obj.blockNames = blockNames;
48 end
49 nBlocks = N;
50
51 % Boundary groups
52 boundaryGroups = struct();
53 L = { {1, 'l'} };
54 R = { {N, 'r'} };
55 boundaryGroups.L = multiblock.BoundaryGroup(L);
56 boundaryGroups.R = multiblock.BoundaryGroup(R);
57 boundaryGroups.all = multiblock.BoundaryGroup([L,R]);
58
59 obj.connections = conn;
60 obj.nBlocks = nBlocks;
61 obj.boundaryGroups = boundaryGroups;
62 obj.xlims = xlims;
63
64 end
65
66
67 % Returns a multiblock.Grid given some parameters
68 % ms: cell array of m values
69 % For same m in every block, just input one scalar.
70 function g = getGrid(obj, ms, varargin)
71
72 default_arg('ms',21)
73
74 % Extend ms if input is a single scalar
75 if (numel(ms) == 1) && ~iscell(ms)
76 m = ms;
77 ms = cell(1,obj.nBlocks);
78 for i = 1:obj.nBlocks
79 ms{i} = m;
80 end
81 end
82
83 grids = cell(1, obj.nBlocks);
84 for i = 1:obj.nBlocks
85 grids{i} = grid.equidistant(ms{i}, obj.xlims{i}, obj.ylims{i});
86 end
87
88 g = multiblock.Grid(grids, obj.connections, obj.boundaryGroups);
89 end
90
91 % Returns a multiblock.Grid given some parameters
92 % ms: cell array of m values
93 % For same m in every block, just input one scalar.
94 function g = getStaggeredGrid(obj, ms, varargin)
95
96 default_arg('ms',21)
97
98 % Extend ms if input is a single scalar
99 if (numel(ms) == 1) && ~iscell(ms)
100 m = ms;
101 ms = cell(1,obj.nBlocks);
102 for i = 1:obj.nBlocks
103 ms{i} = m;
104 end
105 end
106
107 grids = cell(1, obj.nBlocks);
108 for i = 1:obj.nBlocks
109 [g_primal, g_dual] = grid.primalDual1D(ms{i}, obj.xlims{i});
110 grids{i} = grid.Staggered1d(g_primal, g_dual);
111 end
112
113 g = multiblock.Grid(grids, obj.connections, obj.boundaryGroups);
114 end
115
116 % label is the type of label used for plotting,
117 % default is block name, 'id' show the index for each block.
118 function show(obj, label)
119 default_arg('label', 'name')
120
121 m = 10;
122 figure
123 for i = 1:obj.nBlocks
124 x = linspace(obj.xlims{i}{1}, obj.xlims{i}{2}, m);
125 y = 0*x + 0.05* ( (-1)^i + 1 ) ;
126 plot(x,y,'+');
127 hold on
128 end
129 hold off
130
131 switch label
132 case 'name'
133 labels = obj.blockNames;
134 case 'id'
135 labels = {};
136 for i = 1:obj.nBlocks
137 labels{i} = num2str(i);
138 end
139 otherwise
140 axis equal
141 return
142 end
143
144 legend(labels)
145 axis equal
146 end
147
148 % Returns the grid size of each block in a cell array
149 % The input parameters are determined by the subclass
150 function ms = getGridSizes(obj, varargin)
151 end
152 end
153 end