0001 function [fwd_mdl]= ...
0002 ng_mk_fwd_model( ng_vol_filename, centres, ...
0003 name, stim_pattern, z_contact, postprocmesh)
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 if nargin==1 && strcmp(ng_vol_filename,'UNIT_TEST'); do_unit_test; return; end
0024
0025 if isempty(name);
0026 name = ['MDL from', ng_vol_filename];
0027 end
0028
0029 if nargin<5
0030 z_contact=0.01;
0031 end
0032
0033
0034 [srf,vtx,fc,bc,simp,edg,mat_ind] = ng_read_mesh(ng_vol_filename);
0035 if isempty(vtx);
0036 error('EIDORS: ng_mk_fwd_model: Netgen meshing failed. Stopping');
0037 end
0038 if nargin>=6
0039 N_elec = size(centres,1);
0040 [srf,vtx,fc,bc,simp,edg,mat_ind] = feval(postprocmesh,...
0041 srf,vtx,fc,bc,simp,edg,mat_ind, N_elec);
0042 end
0043 fwd_mdl= construct_fwd_model(srf,vtx,simp,bc, name, ...
0044 stim_pattern, centres, z_contact,fc);
0045
0046 [fwd_mdl.mat_idx] = mk_mat_indices(mat_ind);
0047
0048 if ~isfield(fwd_mdl,'normalize_measurements')
0049 fwd_mdl.normalize_measurements = 0;
0050 end
0051
0052
0053 function fwd_mdl= construct_fwd_model(srf,vtx,simp,bc, name, ...
0054 stim_pattern, centres, z_contact,fc)
0055
0056
0057
0058 [~,idx] = unique(sort(srf,2),'rows','stable');
0059 if length(idx)~=size(srf,1)
0060 error('Duplicate elements on boundary. Netgen completed, but EIDORS can''t interpret the result. Try modifying the netgen parameters.')
0061 end
0062
0063
0064
0065 mdl.nodes = vtx;
0066 mdl.elems = simp;
0067 mdl.boundary = srf;
0068 mdl.boundary_numbers=fc;
0069 mdl.gnd_node= find_centre_node(vtx);
0070 mdl.np_fwd_solve.perm_sym = '{n}';
0071 mdl.name = name;
0072
0073
0074 if ~isempty(stim_pattern)
0075 mdl.stimulation= stim_pattern;
0076 end
0077
0078 nelec= size(centres,1);
0079 if nelec>0
0080
0081 [elec,sels,electrodes] = ng_tank_find_elec(srf,vtx,bc,centres);
0082 if size(elec,1) ~= nelec
0083 error('Failed to find all the electrodes')
0084 end
0085
0086
0087 z_contact= z_contact.*ones(nelec,1);
0088 for i=1:nelec
0089 electrodes(i).z_contact= z_contact(i);
0090 end
0091
0092 mdl.electrode = electrodes;
0093 end
0094
0095 mdl.solve= 'eidors_default';
0096 mdl.jacobian= 'eidors_default';
0097 mdl.system_mat= 'eidors_default';
0098
0099 fwd_mdl= eidors_obj('fwd_model', mdl);
0100
0101
0102
0103
0104 function [mat_idx] = mk_mat_indices( mat_ind);
0105
0106
0107
0108 if isempty(mat_ind)
0109 mat_idx = [];
0110 return
0111 end
0112 mat_indices = unique( mat_ind );
0113 for i= 1:length(mat_indices);
0114 mat_idx{i}= find(mat_ind == mat_indices(i));
0115 end
0116
0117 function gnd_node= find_centre_node(vtx);
0118
0119 d = sum( vtx.^2, 2);
0120 [jnk,gnd_node] = min(d);
0121 gnd_node= gnd_node(1);
0122
0123 function do_unit_test
0124
0125 fmdl = ng_mk_cyl_models(1,[],[]);
0126 unit_test_cmp('No elecs',num_elecs(fmdl),0);
0127 fmdl = ng_mk_cyl_models(1,[0,0.5;10,0.2],[0.1]);
0128 unit_test_cmp('Two elecs',num_elecs(fmdl),2);