0001 function fmdl = remove_unused_nodes( fmdl );
0002
0003
0004
0005
0006
0007 if ischar(fmdl) && strcmp(fmdl,'UNIT_TEST'); do_unit_test; return; end
0008
0009 if num_elems(fmdl)==0; return; end;
0010
0011 usednodes = unique(fmdl.elems(:));
0012 if max(usednodes) > num_nodes(fmdl)
0013 error('remove_unused_nodes: more nodes are used than exist');
0014 end
0015 fmdl0 = fmdl;
0016 nidx = zeros(num_nodes(fmdl),1);
0017 nidx(usednodes) = 1:length(usednodes);
0018 fmdl.nodes(nidx==0,:) = [];
0019 fmdl.elems = remap(nidx, fmdl.elems);
0020
0021 for i=1:length(fmdl.electrode)
0022 fmdl.electrode(i).nodes = remap(nidx, fmdl.electrode(i).nodes);
0023 removed = fmdl.electrode(i).nodes == 0;
0024 fmdl.electrode(i).nodes( removed ) = [];
0025 elec_empty = isempty(fmdl.electrode(i).nodes);
0026 if isfield(fmdl.electrode(i),'faces')
0027 fmdl.electrode(i).faces = remap(nidx, fmdl.electrode(i).faces);
0028 removed = any(fmdl.electrode(i).faces== 0,2);
0029 fmdl.electrode(i).faces(removed,:) = [];
0030 elec_empty= elec_empty && isempty(fmdl.electrode(i).faces);
0031 end
0032 if elec_empty
0033 eidors_msg('Zeros in electrode #%d nodes and faces. Often this means parts of the model are disconnected.',i,1);
0034 keyboard
0035 end
0036 end
0037
0038 fmdl.boundary = remap(nidx, fmdl.boundary);
0039 fmdl.boundary(any(fmdl.boundary==0,2),:) = [];
0040 fmdl.gnd_node = nidx(fmdl.gnd_node);
0041 if fmdl.gnd_node == 0
0042 fmdl = assign_new_gnd_node( fmdl );
0043 end
0044
0045
0046 function mat = remap(nidx,mat)
0047 mat = reshape(nidx(mat),size(mat));
0048
0049 function fmdl = assign_new_gnd_node( fmdl );
0050 eidors_msg('FEM_ELECTRODE: Lost ground node => replacing',1);
0051 d2 = sum((fmdl.nodes - repmat(mean(fmdl.nodes, 1), size(fmdl.nodes, 1), 1)).^2,2);
0052 [~,fmdl.gnd_node] = min(d2);
0053
0054 function do_unit_test
0055 fmdl = getfield(mk_common_model('a2c2',4),'fwd_model');
0056 fmdl.elems(1:4,:) = [];
0057 fmdl = remove_unused_nodes(fmdl);
0058
0059 unit_test_cmp('nodes',size(fmdl.nodes),[40,2]);
0060 unit_test_cmp('elems',size(fmdl.elems),[60,3]);
0061 unit_test_cmp('ground',fmdl.gnd_node,3);