0001 function [fmdl, c2f_idx] = remove_elems(fmdl, idx, elec_opt)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 if ischar(fmdl) && strcmp(fmdl,'UNIT_TEST'); do_unit_test; return; end
0027
0028 if nargin < 3, elec_opt = ''; end
0029
0030 remove = false([num_elems(fmdl), 1]);
0031 remove(idx) = true;
0032 c2f_idx = [];
0033
0034 if ~any(remove), return, end
0035 if all(remove)
0036 error('Cannot remove all elements')
0037 end
0038
0039 fmdl.elems(remove,:) = [];
0040
0041 if isfield(fmdl, 'coarse2fine')
0042 fmdl.coarse2fine(remove,:) = [];
0043 end
0044
0045
0046
0047 c2f_idx = find(~remove);
0048 elem_map = zeros(size(remove));
0049 elem_map(~remove) = 1:nnz(~remove);
0050
0051 if isfield(fmdl, 'mat_idx')
0052 mat_removed = false(size(fmdl.mat_idx));
0053 for i = 1:numel(fmdl.mat_idx)
0054 fmdl.mat_idx{i} = elem_map(fmdl.mat_idx{i});
0055 fmdl.mat_idx{i}(fmdl.mat_idx{i} == 0) = [];
0056 if isempty(fmdl.mat_idx{i})
0057 mat_removed(i) = true;
0058 end
0059 end
0060 fmdl.mat_idx(mat_removed) = [];
0061 if isfield(fmdl, 'mat_names')
0062 fmdl.mat_names(mat_removed) = [];
0063 end
0064 end
0065
0066
0067 fmdl = remove_unused_nodes(fmdl, elec_opt);
0068
0069
0070 function do_unit_test
0071 f0= mk_common_model('a2c2',8);
0072 f0= f0.fwd_model;
0073 fr= remove_elems(f0, [7,13,14,21,22,32]);
0074 unit_test_cmp('2D- 1a',fr.nodes(11,:), f0.nodes(12,:));
0075 unit_test_cmp('2D- 1b',fr.elems(20,:),[13,14, 6]);
0076
0077 fr= remove_elems(f0, [38,50,51],'remove');
0078 unit_test_cmp('2D- 2e',num_elecs(fr),7);
0079 unit_test_cmp('2D- 2b',fr.nodes(27,:), f0.nodes(27,:));
0080 unit_test_cmp('2D- 2a',fr.nodes(28,:), f0.nodes(29,:));
0081 unit_test_cmp('2D- 2c',f0.elems(40,:),[20,19,33]);
0082 unit_test_cmp('2D- 2d',fr.elems(40,:),[31,19,18]);
0083
0084 fr= remove_elems(f0, [38,50,51],'keep');
0085 unit_test_cmp('2D- 2e',num_elecs(fr),8);
0086 unit_test_cmp('2D- 2b',fr.nodes(27,:), f0.nodes(27,:));
0087 unit_test_cmp('2D- 2a',fr.nodes(28,:), f0.nodes(29,:));
0088 unit_test_cmp('2D- 2c',f0.elems(40,:),[20,19,33]);
0089 unit_test_cmp('2D- 2d',fr.elems(40,:),[31,19,18]);
0090