1
function [G, p, pinv, Gnew] = meshnd (arg1,n,k)
2
%MESHND creation and nested dissection of a regular 2D or 3D mesh.
3
% [p G pinv Gnew] = meshnd (m,n) constructs a m-by-n 2D mesh G, and then finds
4
% a permuted mesh Gnew where Gnew = pinv(G) and G = p(Gnew). meshnd(m,n,k)
5
% creates an m-by-n-by-k 3D mesh.
7
% [p G pinv Gnew] = meshnd (G) does not construct G, but uses the mesh G as
8
% given on input instead.
11
% [G p pinv Gnew] = meshnd (4,5) ;
25
% With no inputs, a few example meshes are generated and plotted.
27
% See also nested, numgrid.
29
% Copyright 2007, Timothy A. Davis, Univ. of Florida
31
% get the inputs and create the mesh if not provided on input
34
% run a simple example
39
% the mesh is provided on input
45
% create the m-by-n-by-k mesh in "natural" (row-major) order. This is how
46
% a typical 2D mesh is ordered. A column-major order would be better, since
47
% in that case G(:) would equal 1:(m*n) ... but let's stick with tradition.
50
G = reshape (1:(m*n*k), n, m, k)' ;
54
% create the m-by-n-by-k mesh in column-major order. The first m-by-n-by-1
55
% slice is in column-major order, followed by all the other slices 2 to k.
57
G = reshape (1:(m*n*k), m, n, k) ;
61
error ('Usage: [G p pinv Gnew] = meshnd(G), meshnd(m,n) or meshnd(m,n,k)') ;
66
p = nd2 (G)' ; % order the mesh
70
pinv (p) = 1:(m*n*k) ; % find the inverse permutation
74
Gnew = pinv (G) ; % find the permuted mesh
77
%-------------------------------------------------------------------------------
80
%ND2 p = nd2 (G) permutes a 2D or 3D mesh G.
81
% Compare with nestdiss which uses p as a scalar offset and returns a modified
82
% mesh G that corresponds to Gnew in meshnd. Here, the scalar offset p in
83
% nestdiss is not needed. Instead, p is a permutation, and the modified mesh
84
% Gnew is not returned.
88
if (max ([m n k]) <= 2)
90
% G is small; do not cut it
95
% cut G along the middle slice, cutting k in half
98
p = [(nd2 (G (:,:,1:s-1))) ; (nd2 (G (:,:,s+1:k))) ; middle(:)] ;
100
elseif n >= max (m,k)
102
% cut G along the middle column, cutting n in half
105
p = [(nd2 (G (:,1:s-1,:))) ; (nd2 (G (:,s+1:n,:))) ; middle(:)] ;
109
% cut G along the middle row, cutting m in half
112
p = [(nd2 (G (1:s-1,:,:))) ; (nd2 (G (s+1:m,:,:))) ; middle(:)] ;