remove_unused_nodes

PURPOSE ^

REMOVE_UNUSED_NODES: identify and remove unused nodes in model

SYNOPSIS ^

function fmdl = remove_unused_nodes( fmdl );

DESCRIPTION ^

 REMOVE_UNUSED_NODES: identify and remove unused nodes in model
 Usage: fmdl = remove_unused_nodes( fmdl );

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function fmdl = remove_unused_nodes( fmdl );
0002 % REMOVE_UNUSED_NODES: identify and remove unused nodes in model
0003 % Usage: fmdl = remove_unused_nodes( fmdl );
0004 
0005 % (C) 2019 Andy Adler. License: GPL v2 or v3. $Id: remove_unused_nodes.m 6101 2021-03-30 18:52:30Z aadler $
0006 
0007    if ischar(fmdl) && strcmp(fmdl,'UNIT_TEST'); do_unit_test; return; end
0008 
0009    if num_elems(fmdl)==0; return; end; % don't operate on pathalogical models
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 %  fmdl.boundary = find_boundary(fmdl);
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 %% New gnd node if missing
0042       fmdl = assign_new_gnd_node( fmdl );
0043    end
0044 
0045 %  fmdl.elems = reshape(nidx(fmdl.elems),[],size(fmdl.elems,2));
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);

Generated on Fri 30-Dec-2022 19:44:54 by m2html © 2005