Changeset 22502


Ignore:
Timestamp:
03/06/18 14:46:56 (7 years ago)
Author:
Mathieu Morlighem
Message:

CHG: sped up squaremesh (needed for BedMachine Ant.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/mesh/squaremesh.m

    r17558 r22502  
    1 function md=squaremesh(md,Lx,Ly,nx,ny)
     1function md=squaremesh(md,Lx,Ly,nx,ny,computeconnectivity)
    22%SQUAREMESH - create a structured square mesh
    33%
     
    1010%      [md]=squaremesh(md,Lx,Ly,nx,ny)
    1111
     12%process options
     13if nargin == 5,
     14        computeconnectivity = 1;
     15end
     16
    1217%get number of elements and number of nodes
    1318nel=(nx-1)*(ny-1)*2;
    1419nods=nx*ny;
    1520
    16 %initialization
    17 index=zeros(nel,3);
    18 x=zeros(nx*ny,1);
    19 y=zeros(nx*ny,1);
     21%Old method
     22if 0,
     23        %initialization
     24        index=zeros(nel,3);
     25        x=zeros(nx*ny,1);
     26        y=zeros(nx*ny,1);
    2027
    21 %create coordinates
    22 for n=1:nx,
    23         for m=1:ny,
    24                 x((n-1)*ny+m)=(n-1.);
    25                 y((n-1)*ny+m)=(m-1.);
     28        %create coordinates
     29        for n=1:nx,
     30                for m=1:ny,
     31                        x((n-1)*ny+m)=(n-1.);
     32                        y((n-1)*ny+m)=(m-1.);
     33                end
    2634        end
     35
     36        %create index
     37        for n=1:(nx-1)
     38                for m=1:(ny-1),
     39                        A=(n-1)*ny+m;
     40                        B=A+1;
     41                        C=n*ny+m;
     42                        D=C+1;
     43                        index((n-1)*(ny-1)*2+2*(m-1)+1,:)=[A C B];
     44                        index((n-1)*(ny-1)*2+2*m,:)=[B C D];
     45                end
     46        end
     47
     48        %Scale  x and y
     49        x=x/max(x)*Lx;
     50        y=y/max(y)*Ly;
     51else
     52        %New method (faster!)
     53        x = repmat(linspace(0,Lx,nx),[ny 1]);
     54        x = reshape(x,[nx*ny 1]);
     55        y = repmat(linspace(0,Ly,ny)',[1 nx]);
     56        y = reshape(y,[nx*ny 1]);
     57
     58        %do first column of elements first
     59        nels1 = 2*(ny-1);
     60        index = ones(nels1,3);
     61        %First column
     62        index(2:2:nels1,1) = 2:ny;
     63        index(3:2:nels1,1) = 2:ny-1;
     64        %2d column
     65        index(1:2:nels1,2) = ny+1:2*ny-1;
     66        index(2:2:nels1,2) = ny+1:2*ny-1;
     67        %3rd column
     68        index(1:2:nels1,3) = 2:ny;
     69        index(2:2:nels1,3) = ny+2:2*ny;
     70
     71        %Now copy column and offset with ny, nx times
     72        index = repmat(index,[nx-1 1]);
     73        offset = repmat([0:ny:(nx-2)*ny],[nels1 1]);
     74        offset = reshape(offset,[(nx-1)*nels1,1]);
     75        offset = repmat(offset,[1,3]);
     76        index = index + offset;
    2777end
    2878
    29 %create index
    30 for n=1:(nx-1)
    31         for m=1:(ny-1),
    32                 A=(n-1)*ny+m;
    33                 B=A+1;
    34                 C=n*ny+m;
    35                 D=C+1;
    36                 index((n-1)*(ny-1)*2+2*(m-1)+1,:)=[A C B];
    37                 index((n-1)*(ny-1)*2+2*m,:)=[B C D];
    38         end
    39 end
    40 
    41 %Scale  x and y
    42 x=x/max(x)*Lx;
    43 y=y/max(y)*Ly;
    4479
    4580%create segments
     
    67102
    68103%Now, build the connectivity tables for this mesh.
    69 md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices);
    70 md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity);
     104if computeconnectivity,
     105        md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices);
     106        md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity);
     107end
Note: See TracChangeset for help on using the changeset viewer.