0001 function mapping = mk_coarse_fine_mapping( f_mdl, c_mdl );
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 c_mdl = assign_defaults( c_mdl, f_mdl );
0028 c_obj = cache_obj(c_mdl, f_mdl);
0029
0030 f_mdl= offset_and_project( f_mdl, c_mdl);
0031 mapping = eidors_obj('get-cache', c_obj, 'coarse_fine_mapping');
0032 if ~isempty(mapping)
0033 eidors_msg('mk_coarse_fine_mapping: using cached value', 3);
0034 else
0035
0036 z_depth = c_mdl.mk_coarse_fine_mapping.z_depth;
0037
0038 f_elems = all_contained_elems( f_mdl, c_mdl, z_depth);
0039 mapping = contained_elems_i( f_mdl, c_mdl, f_elems, z_depth);
0040
0041 if isfield(c_mdl,'coarse2fine')
0042 mapping = mapping*c_mdl.coarse2fine;
0043 end
0044
0045 eidors_obj('set-cache', c_obj, 'coarse_fine_mapping', mapping);
0046 eidors_msg('mk_coarse_fine_mapping: setting cached value', 3);
0047 end
0048
0049
0050 function c_obj = cache_obj(c_mdl, f_mdl)
0051 c_obj = {c_mdl.nodes, c_mdl.elems, c_mdl.mk_coarse_fine_mapping, ...
0052 f_mdl.nodes, f_mdl.elems};
0053
0054
0055 function c_elems = all_contained_elems( fm, cm, z_depth)
0056 [nf,ef]= size(fm.elems);
0057 [nc,ec]= size(cm.elems);
0058 fm_pts = zeros(nf*ef,3);
0059
0060
0061
0062
0063 s_fac= .9;
0064 for dim= 1:ef-1
0065
0066 fm_pt= reshape(fm.nodes(fm.elems,dim),nf,ef);
0067 fm_ctr= mean(fm_pt,2)*ones(1,ef);
0068 fm_pt = s_fac*(fm_pt-fm_ctr) + fm_ctr;
0069 fm_pts(:,dim) = fm_pt(:);
0070 end
0071
0072 tsn= search_fm_pts_in_cm(cm, fm_pts, z_depth);
0073 tsn= reshape( tsn, [], ef);
0074
0075
0076
0077 c_elems= all(diff(tsn,1,2)==0,2) .* tsn(:,1);
0078 c_elems(all(tsn==-1,2))= -1;
0079
0080
0081
0082
0083
0084 function tsn= search_fm_pts_in_cm(cm, fm_pts, z_depth);
0085 dc= size(cm.elems,2)-1;
0086 df= size(fm_pts,2);
0087
0088 tsn= -ones(size(fm_pts,1),1);
0089 not_oor= (tsn==-1);
0090
0091 if dc==2
0092
0093 if df==3
0094
0095 not_oor= not_oor & any( abs(fm_pts(:,3) ) <= z_depth , 2);
0096 end
0097 dims=1:2;
0098
0099 elseif dc==3
0100
0101 dims=1:3;
0102
0103 else
0104 error('coarse model must be 2 or 3D');
0105 end
0106
0107 tsn(not_oor)= tsearchn(cm.nodes(:,dims), cm.elems, fm_pts(not_oor,dims));
0108 tsn(isnan(tsn))= -1;
0109
0110
0111
0112
0113
0114
0115 function c_elems = contained_elems_i( fm, cm, idx, z_depth)
0116 [nc,dc]= size(cm.elems);
0117 [nf,df]= size(fm.elems);
0118
0119 fidx= find(idx==0);
0120 l_fidx= length(fidx);
0121
0122 c_e_i= []; c_e_j=[]; c_e_v=[];
0123
0124
0125
0126 n_interp = 7-df;
0127 interp_mdl.nodes= fm.nodes;
0128 interp_mdl.elems= fm.elems(fidx,:);
0129
0130 fm_pts = interp_mesh( interp_mdl, n_interp);
0131 l_interp = size(fm_pts,3);
0132
0133 fm_pts = permute( fm_pts, [3,1,2]);
0134 fm_pts = reshape(fm_pts, [], df-1);
0135
0136 tsn= search_fm_pts_in_cm(cm, fm_pts, z_depth);
0137
0138 tsn_idx= ones(l_interp,1)*fidx(:)';
0139 tsn_idx= tsn_idx(:);
0140
0141 outside_idx= tsn==-1;
0142 tsn(outside_idx) = [];
0143 tsn_idx(outside_idx) = [];
0144
0145 in_idx= find(idx<=0);
0146 ridx= 1:nf; ridx(in_idx)=[];
0147 idx(in_idx)=[];
0148
0149
0150
0151 c_elems = sparse(ridx,idx,1,nf,nc) + ...
0152 sparse(tsn_idx,tsn,1,nf,nc)/l_interp;
0153
0154
0155
0156 function xyz = interpxyz( xyzmin, xyzmax, n_interp);
0157 xyzdelta= xyzmax - xyzmin;
0158 xyz_interp = 1 + floor(n_interp * xyzdelta / max(xyzdelta) );
0159 xspace = linspace(xyzmin(1), xyzmax(1), xyz_interp(1) );
0160 yspace = linspace(xyzmin(2), xyzmax(2), xyz_interp(2) );
0161 zspace = linspace(xyzmin(3), xyzmax(3), xyz_interp(3) );
0162 [xx3,yy3,zz3] = ndgrid( xspace, yspace, zspace );
0163 xyz= [xx3(:), yy3(:), zz3(:)];
0164
0165
0166 function f_mdl= offset_and_project( f_mdl, c_mdl);
0167 [fn,fd]= size(f_mdl.nodes);
0168 T= c_mdl.mk_coarse_fine_mapping.f2c_offset;
0169 M= c_mdl.mk_coarse_fine_mapping.f2c_project;
0170
0171 f_mdl.nodes= ( f_mdl.nodes - ones(fn,1)*T )*M;
0172
0173 function c_mdl = assign_defaults( c_mdl, f_mdl );
0174 [fn,fd]= size(f_mdl.nodes);
0175 try c_mdl.mk_coarse_fine_mapping.f2c_offset;
0176 catch c_mdl.mk_coarse_fine_mapping.f2c_offset= zeros(1,fd);
0177 end
0178 try c_mdl.mk_coarse_fine_mapping.f2c_project;
0179 catch c_mdl.mk_coarse_fine_mapping.f2c_project= speye(fd);
0180 end
0181 try c_mdl.mk_coarse_fine_mapping.z_depth;
0182 catch c_mdl.mk_coarse_fine_mapping.z_depth= inf;
0183 end