| 1 |
function [mesh1, I1] = reg_mesh_refine2_mid(mesh) |
| 2 |
%FIX THE COMMENTS LATER |
| 3 |
% [mesh1,I1]=reg_mesh_refine2(mesh) |
| 4 |
% |
| 5 |
% Compute a regular refinement of a mesh given by p, e, t. |
| 6 |
% Each triangle of the original mesh is refined into four triangles |
| 7 |
% by dividing each edge into two. |
| 8 |
% |
| 9 |
% Input |
| 10 |
% mesh structure with the fields mesh.p, mesh.e, mesh.t |
| 11 |
% If size(mesh.t, 2) == 3 |
| 12 |
% mesh.p(i, 1:2) x-, y-coordinates of the i-th vertex |
| 13 |
% |
| 14 |
% mesh.e(i, 1:2) indices of vertices of boundary edge i. |
| 15 |
% mesh.e(i, 3) boundary marker of edge i |
| 16 |
% |
| 17 |
% mesh.t(i, 1:3) indices of vertices in triangle i. |
| 18 |
% If size(mesh.t, 2) == 6 |
| 19 |
% mesh.p(i, 1:2) x-, y-coordinates of the i-th vertex |
| 20 |
% |
| 21 |
% mesh.e(i, 1:2) indices of vertices of boundary edge i. |
| 22 |
% mesh.e(i, 3) indices of the mid-point of boundary edge i. |
| 23 |
% mesh.e(i, 4) boundary marker of edge i |
| 24 |
% |
| 25 |
% mesh.t(i, 1:3) indices of vertices in triangle i. |
| 26 |
% mesh.t(i, 4:6) indices of mid-points in triangle i. |
| 27 |
% |
| 28 |
% Output |
| 29 |
% mesh1 structure with the fields mesh1.p, mesh1.e, mesh1.t |
| 30 |
% If size(mesh.t, 2) == 3 |
| 31 |
% mesh1.p(i, 1:2) x-, y-coordinates of the i-th vertex in |
| 32 |
% the refined mesh. |
| 33 |
% If np = size(mesh.p, 1), then mesh1.p(1:np, :) = mesh.p. |
| 34 |
% |
| 35 |
% mesh1.e(i, 1:2) indices of vertices of boundary edge i of |
| 36 |
% the refined mesh |
| 37 |
% mesh1.e(i, 3) boundary marker of edge i of the refined mesh. |
| 38 |
% If ne = size(mesh.e, 1), then mesh1.e(1:ne, :) = mesh.e. |
| 39 |
% |
| 40 |
% mesh1.t(i, 1:3) indices of vertices in triangle i of the |
| 41 |
% refined mesh. |
| 42 |
% If nt = size(mesh.t, 1), then mesh1.t(1:nt, :) = mesh.t. |
| 43 |
% If size(mesh.t, 2) == 6 |
| 44 |
% mesh1.p(i, 1:2) x-, y-coordinates of the i-th vertex in |
| 45 |
% the refined mesh. |
| 46 |
% If np = size(mesh.p, 1), then mesh1.p(1:np, :) = mesh.p. |
| 47 |
% |
| 48 |
% mesh1.e(i, 1:2) indices of vertices of boundary edge i of |
| 49 |
% the refined mesh |
| 50 |
% mesh1.e(i, 3) indices of mid-point of boundary edge i of |
| 51 |
% the refined mesh |
| 52 |
% mesh1.e(i, 4) boundary marker of edge i of the refined |
| 53 |
% mesh. |
| 54 |
% |
| 55 |
% mesh1.t(i, 1:3) indices of vertices in triangle i of the |
| 56 |
% refined mesh. |
| 57 |
% mesh1.t(i, 4:6) indices of mid-points in triangle i of the |
| 58 |
% refined mesh. |
| 59 |
% |
| 60 |
% I1 Interpolation matrix of size np1 x np, where np1 is the number |
| 61 |
% of vertices in the refined mesh and np is the number of |
| 62 |
% vertices in the original mesh. |
| 63 |
% If u is the vector of function values of a piecewise linear function |
| 64 |
% on the original mesh (u(i) is the function value at node |
| 65 |
% mesh.p(i,:)), then u1 = I1*u is the vector of function values of the |
| 66 |
% piecewise linear function on the refined mesh (u1(i) is the function |
| 67 |
% value at node mesh1.p(i,:)). |
| 68 |
% |
| 69 |
% reg_mesh_refine2_mid is based on a modification of refinemesh.m in the Matlab PDEToolbox. |
| 70 |
% |
| 71 |
% AUTHOR: Patricia A. Howard |
| 72 |
% Department of Computational and Applied Mathematics |
| 73 |
% Rice University |
| 74 |
% March 3, 2006 |
| 75 |
% |
| 76 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 77 |
% |
| 78 |
|
| 79 |
np = size(mesh.p, 1); |
| 80 |
nt = 0; |
| 81 |
ne = 0; |
| 82 |
|
| 83 |
% Cannot use matrix indices that exceeds the size of a signed int |
| 84 |
[comp, maxsize] = computer; |
| 85 |
indexproblem = np^2 > maxsize; |
| 86 |
|
| 87 |
% Make a connectivity matrix, with edges to be refined. |
| 88 |
% -1 means no point is yet allocated |
| 89 |
it = []; |
| 90 |
ip1 = []; |
| 91 |
ip2 = []; |
| 92 |
ip3 = []; |
| 93 |
ip4 = []; |
| 94 |
ip5 = []; |
| 95 |
ip6 = []; |
| 96 |
if size(mesh.t, 2) == 3 |
| 97 |
ne = size(mesh.e, 1); |
| 98 |
nt = size(mesh.t, 1); |
| 99 |
it =(1:nt); |
| 100 |
ip1 = mesh.t(it, 1); |
| 101 |
ip2 = mesh.t(it, 2); |
| 102 |
ip3 = mesh.t(it, 3); |
| 103 |
elseif size(mesh.t, 2) == 6 |
| 104 |
ip1 = mesh.t(:, 1); |
| 105 |
ip2 = mesh.t(:, 2); |
| 106 |
ip3 = mesh.t(:, 3); |
| 107 |
ip4 = mesh.t(:, 4); |
| 108 |
ip5 = mesh.t(:, 5); |
| 109 |
ip6 = mesh.t(:, 6); |
| 110 |
% Construct new triangles with existing mid-point information |
| 111 |
mesh1.t = [ip1 ip6 ip5; |
| 112 |
ip2 ip4 ip6; |
| 113 |
ip4 ip5 ip6; |
| 114 |
ip3 ip5 ip4]; |
| 115 |
nt = size(mesh1.t, 1); |
| 116 |
it =(1:nt); |
| 117 |
ip1 = mesh1.t(it, 1); |
| 118 |
ip2 = mesh1.t(it, 2); |
| 119 |
ip3 = mesh1.t(it, 3); |
| 120 |
ne = 2 * size(mesh.e, 1); |
| 121 |
% Construct new edges with existing mid-point information |
| 122 |
mesh1.e = [mesh.e(:, 1) mesh.e(:, 3) (-1 * ones(length(mesh.e(:, 1)), 1)) mesh.e(:, 4); |
| 123 |
mesh.e(:, 3) mesh.e(:, 2) (-1 * ones(length(mesh.e(:, 2)), 1)) mesh.e(:, 4)]; |
| 124 |
end |
| 125 |
|
| 126 |
A = sparse(ip1, ip2, -1, np, np); |
| 127 |
A = A + sparse(ip2, ip3, -1, np, np); |
| 128 |
A = A + sparse(ip3, ip1, -1, np, np); |
| 129 |
A = -((A + A.') < 0); |
| 130 |
|
| 131 |
% Generate points on (interior and boundary) edges |
| 132 |
% Determine (interior and boundary) edges |
| 133 |
[i1, i2] = find(A == -1 & A.' == -1); |
| 134 |
i = find(i2 > i1); |
| 135 |
i1 = i1(i); |
| 136 |
i2 = i2(i); |
| 137 |
% Edges (interior and boundary) have vertices i1 i2. |
| 138 |
% Compute midpoints |
| 139 |
mesh1.p = [mesh.p; ((mesh.p(i1,1:2) + mesh.p(i2,1:2))/2)]; |
| 140 |
|
| 141 |
% Fill in the new points. |
| 142 |
% The midpoint of edge with vertices i1 and i2 gets index ip |
| 143 |
ip = ((np + 1):(np + length(i)))'; |
| 144 |
if ~indexproblem |
| 145 |
A(i1 + np * (i2 - 1)) = ip; |
| 146 |
A(i2 + np * (i1 - 1)) = ip; |
| 147 |
else |
| 148 |
A=l_assign(A, [i1 i2], [i2 i1], [ip ip]); |
| 149 |
end |
| 150 |
|
| 151 |
% Construct interpolation matrix |
| 152 |
I1 = []; |
| 153 |
if size(mesh.t, 2) == 3 |
| 154 |
irowI1 = [(1:np)']; |
| 155 |
icolI1 = [(1:np)']; |
| 156 |
sI1 = [ones(np, 1)]; |
| 157 |
|
| 158 |
irowI1 = [irowI1; ip; ip]; |
| 159 |
icolI1 = [icolI1; i1; i2]; |
| 160 |
sI1 = [sI1; 0.5 * ones(2 * length(i), 1)]; |
| 161 |
|
| 162 |
I1 = sparse(irowI1, icolI1, sI1); |
| 163 |
elseif size(mesh.t, 2) == 6 |
| 164 |
% Constructs an interpolation matrix for linear elements |
| 165 |
% WITHOUT mid-point (as is required in source-inv) |
| 166 |
nv = max(max(mesh.t(:, 1:3))); |
| 167 |
row_col_val = [(1:nv)' (1:nv)' ones(nv, 1)]; |
| 168 |
|
| 169 |
row_col_val = [row_col_val; |
| 170 |
ip6 mesh.t(:, 1) 0.5 * ones(length(ip6), 1); |
| 171 |
ip6 mesh.t(:, 2) 0.5 * ones(length(ip6), 1); |
| 172 |
ip4 mesh.t(:, 2) 0.5 * ones(length(ip4), 1); |
| 173 |
ip4 mesh.t(:, 3) 0.5 * ones(length(ip4), 1); |
| 174 |
ip5 mesh.t(:, 3) 0.5 * ones(length(ip5), 1); |
| 175 |
ip6 mesh.t(:, 1) 0.5 * ones(length(ip5), 1)]; |
| 176 |
|
| 177 |
row_col_val = unique(row_col_val, 'rows'); |
| 178 |
|
| 179 |
I1 = sparse(row_col_val(:, 1), row_col_val(:, 2), row_col_val(:, 3)); |
| 180 |
end |
| 181 |
|
| 182 |
% Form the new triangles (if size(mesh.t, 2) = 3) or |
| 183 |
% new mid-points (if size(mesh.t, 2) = 6) |
| 184 |
if ~indexproblem |
| 185 |
mp1 = full(A(ip2 + np * (ip3 - 1))); % mp1 is index of midpoint of edge opposite of vectex 1 |
| 186 |
mp2 = full(A(ip3 + np * (ip1 - 1))); % mp2 is index of midpoint of edge opposite of vectex 2 |
| 187 |
mp3 = full(A(ip1 + np * (ip2 - 1))); % mp3 is index of midpoint of edge opposite of vectex 3 |
| 188 |
else |
| 189 |
mp1 = l_extract(A, ip2, ip3); |
| 190 |
mp2 = l_extract(A, ip3, ip1); |
| 191 |
mp3 = l_extract(A, ip1, ip2); |
| 192 |
end |
| 193 |
|
| 194 |
if size(mesh.t, 2) == 3 |
| 195 |
mesh1.t = zeros(4 * nt, 3); |
| 196 |
i = (1:nt); |
| 197 |
nt1 = 0; |
| 198 |
mesh1.t((nt1 + 1):(nt1 + length(i)), :)=[mesh.t(it(i), 1) mp3(i) mp2(i)]; |
| 199 |
nt1 = nt1 + length(i); |
| 200 |
mesh1.t((nt1 + 1):(nt1 + length(i)), :) = [mesh.t(it(i), 2) mp1(i) mp3(i)]; |
| 201 |
nt1 = nt1 + length(i); |
| 202 |
mesh1.t((nt1 + 1):(nt1 + length(i)), :) = [mesh.t(it(i), 3) mp2(i) mp1(i)]; |
| 203 |
nt1 = nt1 + length(i); |
| 204 |
mesh1.t((nt1 + 1):(nt1 + length(i)), :) = [mp1(i) mp2(i) mp3(i)]; |
| 205 |
nt1 = nt1 + length(i); |
| 206 |
elseif size(mesh.t, 2) == 6 |
| 207 |
i = (1:nt); |
| 208 |
mesh1.t(it, 4) = mp1(i); |
| 209 |
mesh1.t(it, 5) = mp2(i); |
| 210 |
mesh1.t(it, 6) = mp3(i); |
| 211 |
end |
| 212 |
|
| 213 |
|
| 214 |
% Form new edges (if size(mesh.t, 2) = 3) or add mid-points (if |
| 215 |
% size(mesh.t, 2) = 6) |
| 216 |
ie = (1:ne); |
| 217 |
if size(mesh.t, 2) == 3 |
| 218 |
if ~indexproblem |
| 219 |
mp1 = full(A(mesh.e(ie, 1) + np * (mesh.e(ie, 2) - 1))); %mp1 is index of midpoint of edge ie |
| 220 |
else |
| 221 |
mp1 = l_extract(A, mesh.e(ie, 1), mesh.e(ie, 2)); |
| 222 |
end |
| 223 |
|
| 224 |
% Create new edges |
| 225 |
mesh1.e = [[mesh.e(ie,1) mp1 mesh.e(ie,3)]; ... |
| 226 |
[mp1 mesh.e(ie,2) mesh.e(ie,3)]]; |
| 227 |
elseif size(mesh.t, 2) == 6 |
| 228 |
if ~indexproblem |
| 229 |
mp1 = full(A(mesh1.e(ie, 1) + np * (mesh1.e(ie, 2) - 1))); %mp1 is index of midpoint of edge ie |
| 230 |
else |
| 231 |
mp1 = l_extract(A, mesh1.e(ie, 1), mesh1.e(ie, 2)); |
| 232 |
end |
| 233 |
|
| 234 |
mesh1.e(:, 3) = mp1; |
| 235 |
end |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 240 |
|
| 241 |
function k=l_extract(A,i,j) |
| 242 |
|
| 243 |
if numel(i)~=numel(j) |
| 244 |
error('PDE:refinemesh:ijNumel', 'i and j must have the same number of elements.') |
| 245 |
end |
| 246 |
|
| 247 |
k=zeros(size(i)); |
| 248 |
|
| 249 |
for l=1:numel(i) |
| 250 |
k(l)=A(i(l),j(l)); |
| 251 |
end |
| 252 |
|
| 253 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 254 |
|
| 255 |
function A=l_assign(A,i,j,k) |
| 256 |
|
| 257 |
if numel(i)~=numel(j) || numel(i)~=numel(k) |
| 258 |
error('PDE:refinemesh:ijkNumel', 'i, j, and k must have the same number of elements.') |
| 259 |
end |
| 260 |
|
| 261 |
for l=1:numel(i) |
| 262 |
A(i(l),j(l))=k(l); |
| 263 |
end |