0001 function memb_frac = elem_select( fmdl, select_fcn )
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
0027
0028
0029 if ischar(fmdl) && strcmp(fmdl,'UNIT_TEST'); do_unit_test; return; end
0030
0031
0032
0033 dims = size(fmdl.nodes,2);
0034 interp_no = 6 - dims;
0035 try
0036 interp_no = fmdl.elem_select.interp_no;
0037 end
0038
0039 pts = interp_mesh( fmdl, interp_no );
0040 x = squeeze(pts(:,1,:));
0041 y = squeeze(pts(:,2,:));
0042 if dims ==2;
0043 z = 0*x;
0044 else
0045 z = squeeze(pts(:,3,:));
0046 end
0047 if ischar(select_fcn)
0048
0049 select_fcn = inline(select_fcn, 'x','y','z');
0050 memb_frac = mean( feval(select_fcn,x,y,z), 2);
0051 elseif ~iscell(select_fcn)
0052
0053 memb_frac = mean( feval(select_fcn,x,y,z), 2);
0054 else
0055
0056 memb_val = ones(size(x));
0057 for i = 1:numel(select_fcn)
0058 memb_val = memb_val .* feval(select_fcn{i},x,y,z);
0059 end
0060 memb_frac = mean(memb_val,2);
0061 end
0062
0063
0064 function do_unit_test;
0065 imdl = mk_common_model('a2c2',8);
0066 select_fcn = '(x-0.2).^2+(y-0.5).^2<0.2^2';
0067 memb_frac = elem_select( imdl.fwd_model, select_fcn);
0068 unit_test_cmp('a2c2 (string)',find(memb_frac), [5, 10,18,26,27]');
0069
0070 imdl = mk_common_model('a2c2',8);
0071 select_fcn = inline('(x-0.2).^2+(y-0.5).^2<0.2^2','x','y','z');
0072 memb_frac = elem_select( imdl.fwd_model, select_fcn);
0073 unit_test_cmp('a2c2',find(memb_frac), [5, 10,18,26,27]');
0074
0075
0076 select_fcn2= inline('y<0.4','x','y','z');
0077 memb_frac = elem_select( imdl.fwd_model, {select_fcn,select_fcn2});
0078 unit_test_cmp('a2c2 (2fcns)',find(memb_frac), [5, 10,18,27]');
0079
0080 imdl = mk_common_model('n3r2',[16,2]);
0081 select_fcn = inline('(x-0.2).^2+(y-0.5).^2 + (z-1).^2<0.1^2','x','y','z');
0082 memb_frac = elem_select( imdl.fwd_model, select_fcn);
0083 unit_test_cmp('n3r2',find(memb_frac), [156 159 162 168 431 434 437 503]');