comparison +multiblock/InterfaceOptions.m @ 907:c0652621bd69 feature/utux2D

Add new class multiblock.InterfaceOptions whose instances are passed to multiblock.DiffOp.
author Martin Almquist <malmquist@stanford.edu>
date Fri, 23 Nov 2018 20:12:54 -0800
parents
children ab1759166a8c
comparison
equal deleted inserted replaced
906:0499239496cf 907:c0652621bd69
1 % An object of class InterfaceOptions can be passed as argument to multiblock.Diffop
2 % to specify details of the interface couplings.
3 %
4 % An InterfaceOptions object is essentially a cell array of options,
5 % equipped with methods that make it easier to change options.
6 classdef InterfaceOptions < handle
7 properties
8 % optsCell -- nBlocks x nBlocks cell array (same size as grid.connections)
9 % Must have the same sparsity pattern as grid.connections
10 optsCell
11 end
12
13 methods
14
15 % grid -- mutliblock.grid
16 % intialOpts -- cell array of interface options
17 function obj = InterfaceOptions(grid, initialOpts)
18
19 default_arg('initialOpts', []);
20
21 % If no initialOpts are given, create empty options cell.
22 % The cell matrix is non-empty where connections is non-empty.
23 if isempty(initialOpts)
24 opts = grid.connections;
25 for i = 1:numel(grid.connections)
26 if ~isempty(grid.connections{i})
27 opts{i} = cell(1, 2);
28 opts{i}{1} = struct;
29 opts{i}{2} = struct;
30 end
31 end
32
33 % If no grid is given, assume that initialOpts is correct and use it.
34 elseif isempty(grid)
35 opts = initialOpts;
36
37 % Check that grid.connections and initialOpts match, and then use initialOpts.
38 else
39 assert(numel(grid.connections) == numel(initialOpts),...
40 'InterfaceOptions: grid.connections and initialOpts do not match');
41 opts = initialOpts;
42 end
43 obj.optsCell = opts;
44 end
45
46 % Returns the cell matrix that contains the options
47 function opts = getOptions(obj)
48 opts = obj.optsCell;
49 end
50
51
52 % Sets the option optStr to val, for the coupling berween blocks i and j
53 % If i and j are omitted, all couplings get optStr = val.
54 %
55 % optStr -- string
56 % val -- anything
57 % i,j -- integers
58 function setOption(obj, optStr, val, i ,j)
59 default_arg('i',[]);
60 default_arg('j',[]);
61
62 opts = obj.optsCell;
63
64 if isempty(i) && ~isempty(j)
65 error('If i is empty, j must also be empty.');
66
67 elseif isempty(j) && ~isempty(i)
68 error('If j is empty, i must also be empty.');
69
70 % If i and j are empty, set the option for all interfaces
71 elseif isempty(i) && isempty(j)
72 for k = 1:numel(opts)
73 if ~isempty(opts{k})
74 opts{k}{1} = setfield(opts{k}{1}, optStr, val);
75 opts{k}{2} = setfield(opts{k}{2}, optStr, val);
76 end
77 end
78
79 % Both i and j are nonempty, set property only for that interface
80 else
81 opts{i,j}{1} = setfield(opts{i,j}{1}, optStr, val);
82 opts{i,j}{2} = setfield(opts{i,j}{2}, optStr, val);
83 end
84
85 obj.optsCell = opts;
86 end
87
88
89 % Constructs the union of two InterfaceOptions
90 function union(obj, obj2)
91 localOpts = obj.optsCell;
92 remoteOpts = obj2.optsCell;
93
94 assert( numel(localOpts) == numel(remoteOpts), ...
95 'multiblock.InterfaceOptions: The two InterfaceOptions do not have the same dimension.');
96
97 for i = 1:numel(localOpts)
98 if ~isempty(remoteOpts{i})
99 if isempty(localOpts{i})
100 error('multiblock.InterfaceOptions: The two InterfaceOptions must have the same interface connections');
101 else
102 for j = 1:2
103 remoteStruct = remoteOpts{i}{j};
104 localStruct = localOpts{i}{j};
105 localFields = fieldnames(localStruct);
106
107 % Assert the we don't have any identical field names, which would lead to overwriting
108 for k = 1:numel(localFields)
109 if isfield(remoteStruct, localFields{k})
110 error('multiblock.InterfaceOptions: Cannot perform union of InterfaceOptions with common options');
111 end
112 end
113
114 % Take fields from remote and deal to local
115 remoteFields = fieldnames(remoteStruct);
116 for k = 1:numel(remoteFields)
117 name = remoteFields{k};
118 val = getfield(remoteStruct, name);
119 localStruct = setfield(localStruct, name, val);
120 end
121
122 localOpts{i}{j} = localStruct;
123 end
124 end
125 end
126 end
127
128 obj.optsCell = localOpts;
129 end
130
131
132 end
133 end