Home > eidors > meshing > netgen > ng_mk_fwd_model.m

ng_mk_fwd_model

PURPOSE ^

NG_MK_FWD_MODEL: create a fwd_model object from a netgen vol file

SYNOPSIS ^

function [fwd_mdl, mat_idx_reordered]=ng_mk_fwd_model( ng_vol_filename, centres,name, stim_pattern, z_contact, postprocmesh)

DESCRIPTION ^

 NG_MK_FWD_MODEL: create a fwd_model object from a netgen vol file
 [fwd_mdl, mat_idx_reordered]= ...
      ng_mk_fwd_model( ng_vol_filename, centres, ...
                       name, stim_pattern, z_contact)

  ng_vol_filename:   filename output from netgen
  name:              name for object (if [] use ng_vol_filename)
  centres:           matrix of N x [x,y,z] electrode centres
                     centres can also be a Nx1 cell matrix of
                     functions which are 1 inside the electrode and 0 outside
  stim_pattern:      a stimulation pattern structure
                     empty ([]) if stim_pattern is not available
  z_contact:         vector or scalar electrode contact impedance

  fwd_mdl:           eidors format fwd_model
  mat_idx_reordered: cell array of material indices from eidors
                     reordered so that the material with
                     the most elements is placed first in
                     the list.  This is supposed to be the
                     main region but sometimes breaks when
                     there is a small region that is highly
                     refined.
                     -- mat_idx_reordered is DEPRECIATED (2013-09-18),
                        the cell array is now stored inside
                        fwd_mdl as fwd_mdl.mat_idx_reordered and the
                        original list is stored as fwd_mdl.mat_idx
 (C) 2006 Andy Adler. (C) 2013 Alistair Boyle. License: GPL version 2 or version 3
 $Id: ng_mk_fwd_model.m 5221 2016-04-11 04:29:34Z aadler $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [fwd_mdl, mat_idx_reordered]= ...
0002              ng_mk_fwd_model( ng_vol_filename, centres, ...
0003                               name, stim_pattern, z_contact, postprocmesh)
0004 % NG_MK_FWD_MODEL: create a fwd_model object from a netgen vol file
0005 % [fwd_mdl, mat_idx_reordered]= ...
0006 %      ng_mk_fwd_model( ng_vol_filename, centres, ...
0007 %                       name, stim_pattern, z_contact)
0008 %
0009 %  ng_vol_filename:   filename output from netgen
0010 %  name:              name for object (if [] use ng_vol_filename)
0011 %  centres:           matrix of N x [x,y,z] electrode centres
0012 %                     centres can also be a Nx1 cell matrix of
0013 %                     functions which are 1 inside the electrode and 0 outside
0014 %  stim_pattern:      a stimulation pattern structure
0015 %                     empty ([]) if stim_pattern is not available
0016 %  z_contact:         vector or scalar electrode contact impedance
0017 %
0018 %  fwd_mdl:           eidors format fwd_model
0019 %  mat_idx_reordered: cell array of material indices from eidors
0020 %                     reordered so that the material with
0021 %                     the most elements is placed first in
0022 %                     the list.  This is supposed to be the
0023 %                     main region but sometimes breaks when
0024 %                     there is a small region that is highly
0025 %                     refined.
0026 %                     -- mat_idx_reordered is DEPRECIATED (2013-09-18),
0027 %                        the cell array is now stored inside
0028 %                        fwd_mdl as fwd_mdl.mat_idx_reordered and the
0029 %                        original list is stored as fwd_mdl.mat_idx
0030 % (C) 2006 Andy Adler. (C) 2013 Alistair Boyle. License: GPL version 2 or version 3
0031 % $Id: ng_mk_fwd_model.m 5221 2016-04-11 04:29:34Z aadler $
0032 
0033 if isempty(name); 
0034    name = ['MDL from', ng_vol_filename];
0035 end
0036 
0037 if nargin<5
0038    z_contact=0.01; % singular if z_contact=0
0039 end
0040 
0041 % Model Geometry
0042 [srf,vtx,fc,bc,simp,edg,mat_ind] = ng_read_mesh(ng_vol_filename);
0043 if isempty(vtx); 
0044    error('EIDORS: ng_mk_fwd_model: Netgen meshing failed. Stopping');
0045 end
0046 if nargin>=6
0047     N_elec = max(size(centres));
0048     [srf,vtx,fc,bc,simp,edg,mat_ind] = feval(postprocmesh,...
0049         srf,vtx,fc,bc,simp,edg,mat_ind, N_elec);
0050 end
0051 fwd_mdl= construct_fwd_model(srf,vtx,simp,bc, name, ...
0052                              stim_pattern, centres, z_contact,fc);
0053 
0054 [fwd_mdl.mat_idx, fwd_mdl.mat_idx_reordered] = mk_mat_indices(mat_ind);
0055 
0056 mat_idx_reordered = fwd_mdl.mat_idx_reordered;
0057 
0058 if ~isfield(fwd_mdl,'normalize_measurements')
0059    fwd_mdl.normalize_measurements = 0;
0060 end
0061 
0062 % build fwd_model structure
0063 function fwd_mdl= construct_fwd_model(srf,vtx,simp,bc, name, ...
0064                        stim_pattern, centres, z_contact,fc)
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 % Model Stimulation
0074 if ~isempty(stim_pattern)
0075    mdl.stimulation= stim_pattern;
0076 end
0077 
0078 nelec= size(centres,1);
0079 if nelec>0
0080    % Electrodes
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    % set the z_contact
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 % Output mat_idx cell array of indices into each material
0102 % typei. Array order is the order of the specified material
0103 % (netgen 'tlo' statements in the .geo file).
0104 % For mat_idx_reordered, the largest material (possibly the
0105 % background if it has the most elements) is placed first.
0106 function [mat_idx, mat_idx_reordered] = mk_mat_indices( mat_ind);
0107   % find length of mat_indices
0108   % test example: mat_ind=[10 12 14 14 12 12 14 12];
0109 
0110   if isempty(mat_ind)
0111      mat_idx = [];
0112      mat_idx_reordered = [];
0113      return
0114   end
0115   mat_indices = unique( mat_ind );
0116   for i= 1:length(mat_indices);
0117      mat_idx{i}= find(mat_ind == mat_indices(i));
0118   end
0119 % from here to end-of-function ... DEPRECIATED
0120 % (old code --- hack that sometimes breaks)
0121   % put the largest material first
0122   for i= 1:length(mat_indices);
0123      mat_idx_l(i) = length( mat_idx{i} );
0124   end
0125   [jnk, max_l] = max(mat_idx_l);
0126   new_idx = 1:length(mat_indices); new_idx(max_l) = [];
0127   ver= eidors_obj('interpreter_version');
0128   if ~ver.isoctave && ver.ver < 7
0129 % STUPID MATLAB WORK AROUND
0130      mat_idx_reordered = {};
0131      ii=1;
0132      for i=[max_l, new_idx];
0133          mat_idx_reordered{ii} = mat_idx{i};
0134          ii=ii+1;
0135      end
0136   else
0137      mat_idx_reordered = cell(length(mat_idx),1);
0138      [mat_idx_reordered{:}] = mat_idx{[max_l, new_idx]};
0139   end
0140 
0141 function gnd_node=    find_centre_node(vtx);
0142   %distance from zero
0143   d = sum( vtx.^2, 2);
0144   [jnk,gnd_node] = min(d);
0145   gnd_node= gnd_node(1);

Generated on Fri 01-Jun-2018 15:59:55 by m2html © 2005