linear_reorder

PURPOSE ^

function [fwd_model] = linear_reorder(fwd_model,ccw)

SYNOPSIS ^

function [fwd_model] = linear_reorder(fwd_model,ccw)

DESCRIPTION ^

function [fwd_model] = linear_reorder(fwd_model,ccw)
Function to reorder local nodes (counter)clockwise per element
Input:  - fwd_model structure
        - ccw = -1 (default) - counter clockwise OR 1 - clockwise   
Output: - fwd_model structure (only .elems changes)
NOTE:Function only for linear triangles, since in this case, identity:
         No. of nodes/element = No. spatial dimensions + 1

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [fwd_model] = linear_reorder(fwd_model,ccw)
0002 %function [fwd_model] = linear_reorder(fwd_model,ccw)
0003 %Function to reorder local nodes (counter)clockwise per element
0004 %Input:  - fwd_model structure
0005 %        - ccw = -1 (default) - counter clockwise OR 1 - clockwise
0006 %Output: - fwd_model structure (only .elems changes)
0007 %NOTE:Function only for linear triangles, since in this case, identity:
0008 %         No. of nodes/element = No. spatial dimensions + 1
0009 
0010 % (C) 2011 Michael Crabb. License: GPL version 2 or version 3
0011 % $Id: linear_reorder.html 2819 2011-09-07 16:43:11Z aadler $
0012 
0013 if isstr(fwd_model) && strcmp(fwd_model,'UNIT_TEST'); do_unit_test; return; end
0014 
0015 if (nargin==1) 
0016     ccw=-1; %Default specify counter-clockwise nodes
0017 end
0018 
0019 nodecoords = fwd_model.nodes; %Cache coorindates of nodes [nnodesxnodedim]
0020 elementnodes = fwd_model.elems; %Cache matrix of elements [eletotalxelenode]
0021 
0022 eletotal = size(elementnodes,1); %No. of elements
0023 elenode = size(elementnodes,2); %No. of nodes per element
0024 
0025 for e=1:eletotal; %Loop over all elements
0026     %Row vector of global nodes [1xelenode]
0027     enodes = elementnodes(e,:); 
0028     %Matrix of nodal positions [elenodexdim] (Linear dimension==elenode-1)
0029     nd = nodecoords(enodes,:); 
0030     
0031     %Calculate area of triangle/volume defined by the elements nodes
0032     %In 2D this is area and in 3D this is volume
0033     area= det([ones(elenode,1),nd]);
0034     areasign=sign(area); 
0035     
0036     %If sign is (pos) neg swap two nodes (last two will suffice..)
0037     if(areasign == ccw) %Swap last two entries of enodes
0038         temp=enodes(elenode-1);
0039         enodes(elenode-1)=enodes(elenode);
0040         enodes(elenode) = temp;
0041         %elementnodes(e,:)=enodes; %Put back into elementnodes matrix
0042     end
0043     elementnodes(e,:)=enodes; %Put enodes back into elementnodes matrix
0044 end
0045 fwd_model.elems=elementnodes; %Reassign fwd_model.elems
0046 
0047 %End LinearReorder function
0048 
0049 function do_unit_test
0050    imdl = mk_common_model('n3r2',32); fmdl = imdl.fwd_model;
0051    fm1 = linear_reorder(fmdl);
0052    vol1= test_linear_reorder( fm1 );
0053    ok = all(vol1>0);
0054    fprintf('test1 3d: OK=%d\n',ok);
0055 
0056    fm1 = linear_reorder(fmdl, 1);
0057    vol1= test_linear_reorder( fm1 );
0058    ok = all(vol1<0);
0059    fprintf('test2 3d: OK=%d\n',ok);
0060 
0061    imdl = mk_common_model('a2c2',8); fmdl = imdl.fwd_model;
0062    fm1 = linear_reorder(fmdl);
0063    vol1= test_linear_reorder( fm1 );
0064    ok = all(vol1>0);
0065    fprintf('test1 2d: OK=%d\n',ok);
0066 
0067    fm1 = linear_reorder(fmdl, 1);
0068    vol1= test_linear_reorder( fm1 );
0069    ok = all(vol1<0);
0070    fprintf('test2 2d: OK=%d\n',ok);
0071 
0072    
0073 
0074 function vol = test_linear_reorder(fwd_model)
0075 
0076 dim=size(fwd_model.nodes,2); elee=size(fwd_model.elems,1);
0077 
0078 for e=1:elee
0079     b=fwd_model.elems(e,:);  [v]=fwd_model.nodes(b,:);
0080         for i=1:dim
0081             vv1(i,:)=v(i+1,:)-v(1,:);
0082         end
0083     vol(e)=det([vv1]);
0084 end
0085

Generated on Tue 09-Aug-2011 11:38:31 by m2html © 2005