Debian Codegraph @

packages/intrepid/matlab/intrelab/mesh/reg_mesh_refine2.m

raw
1 function [mesh1,I1]=reg_mesh_refine2(mesh)
2 %
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 % mesh.p(i,1:2) x-, y-coordinates of the i-th vertex
12 %
13 % mesh.e(i,1:2) indices of vertices of boundary edge i.
14 % mesh.e(i,3) boundary marker of edge i
15 %
16 % mesh.t(i,1:3) indices of vertices in triangle i.
17 %
18 % Output
19 % mesh1 structure with the fields mesh1.p, mesh1.e, mesh1.t
20 % mesh1.p(i,1:2) x-, y-coordinates of the i-th vertex in
21 % the refined mesh.
22 % If np = size(mesh.p,1), then mesh1.p(1:np,:) = mesh.p.
23 %
24 % mesh1.e(i,1:2) indices of vertices of boundary edge i of
25 % the refined mesh
26 % mesh1.e(i,3) boundary marker of edge i of the refined mesh.
27 % If ne = size(mesh.e,1), then mesh1.e(1:ne,:) = mesh.e.
28 %
29 % mesh1.t(i,1:3) indices of vertices in triangle i of the
30 % refined mesh.
31 % If nt = size(mesh.t,1), then mesh1.t(1:nt,:) = mesh.t.
32 %
33 % I1 Interpolation matrix of size np1 x np, where np1 is the number
34 % of nodes in the refined mesh and np is the number of nodes
35 % in the original mesh.
36 % If u is the vector of function values of a piecewise linear function
37 % on the original mesh (u(i) is the function value at node
38 % mesh.p(i,:)), then u1 = I1*u is the vector of function values of the
39 % piecewise linear function on the refined mesh (u1(i) is the function
40 % value at node mesh1.p(i,:)).
41 %
42 % reg_mesh_refine2 is based on a modification of refinemesh.m in the Matlab PDEToolbox.
43 %
44 % AUTHOR: Matthias Heinkenschloss and Denis Ridzal
45 % Department of Computational and Applied Mathematics
46 % Rice University
47 % December 1, 2005
48 %
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %
51
52
53 np=size(mesh.p,1);
54 ne=size(mesh.e,1);
55 nt=size(mesh.t,1);
56
57
58
59 % Cannot use matrix indices that exceeds the size of a signed int
60 [comp,maxsize]=computer;
61 indexproblem=np^2>maxsize;
62
63
64 % Make a connectivity matrix, with edges to be refined.
65 % -1 means no point is yet allocated
66 it =(1:nt);
67 ip1=mesh.t(it,1);
68 ip2=mesh.t(it,2);
69 ip3=mesh.t(it,3);
70
71 A=sparse(ip1,ip2,-1,np,np);
72 A=A+sparse(ip2,ip3,-1,np,np);
73 A=A+sparse(ip3,ip1,-1,np,np);
74 A=-((A+A.')<0);
75
76
77 % Generate points on (interior and boundary) edges
78 % Determine (interior and boundary) edges
79 [i1,i2]=find(A==-1 & A.'==-1);
80 i=find(i2>i1);
81 i1=i1(i);
82 i2=i2(i);
83 % Edges (interior and boundary) have vertices i1 i2.
84 % Compute midpoints
85 mesh1.p=[mesh.p; ((mesh.p(i1,1:2)+mesh.p(i2,1:2))/2)];
86
87 % Fill in the new points.
88 % The midpoint of edge with vertices i1 and i2 gets index ip
89 ip=((np+1):(np+length(i)))';
90 if ~indexproblem
91 A(i1+np*(i2-1))=ip;
92 A(i2+np*(i1-1))=ip;
93 else
94 A=l_assign(A,[i1 i2],[i2 i1],[ip ip]);
95 end
96
97
98 % interpolation matrix
99 irowI1 = [(1:np)'];
100 icolI1 = [(1:np)'];
101 sI1 = [ones(np,1)];
102
103 irowI1 = [irowI1; ip; ip];
104 icolI1 = [icolI1; i1; i2];
105 sI1 = [sI1; 0.5*ones(2*length(i),1)];
106
107 I1 = sparse(irowI1,icolI1,sI1);
108
109
110 % Form the new triangles
111 ip1=mesh.t(it,1);
112 ip2=mesh.t(it,2);
113 ip3=mesh.t(it,3);
114 if ~indexproblem
115 mp1=full(A(ip2+np*(ip3-1))); %mp1 is index of midpoint of edge opposite of vectex 1
116 mp2=full(A(ip3+np*(ip1-1))); %mp2 is index of midpoint of edge opposite of vectex 2
117 mp3=full(A(ip1+np*(ip2-1))); %mp3 is index of midpoint of edge opposite of vectex 3
118 else
119 mp1=l_extract(A,ip2,ip3);
120 mp2=l_extract(A,ip3,ip1);
121 mp3=l_extract(A,ip1,ip2);
122 end
123
124
125 mesh1.t =zeros(4*nt,3);
126 i = (1:nt);
127 nt1=0;
128 mesh1.t((nt1+1):(nt1+length(i)),:)=[mesh.t(it(i),1) mp3(i) mp2(i)];
129 nt1=nt1+length(i);
130 mesh1.t((nt1+1):(nt1+length(i)),:)=[mesh.t(it(i),2) mp1(i) mp3(i)];
131 nt1=nt1+length(i);
132 mesh1.t((nt1+1):(nt1+length(i)),:)=[mesh.t(it(i),3) mp2(i) mp1(i)];
133 nt1=nt1+length(i);
134 mesh1.t((nt1+1):(nt1+length(i)),:)=[mp1(i) mp2(i) mp3(i)];
135 nt1=nt1+length(i);
136
137
138 % form new edges
139 ie = (1:ne);
140 if ~indexproblem
141 mp1=full(A(mesh.e(ie,1)+np*(mesh.e(ie,2)-1))); %mp1 is index of midpoint of edge ie
142 else
143 mp1=l_extract(A,mesh.e(ie,1),mesh.e(ie,2));
144 end
145
146 % Create new edges
147 mesh1.e=[[mesh.e(ie,1) mp1 mesh.e(ie,3)]; ...
148 [mp1 mesh.e(ie,2) mesh.e(ie,3)]];
149
150
151
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
154 function k=l_extract(A,i,j)
155
156 if numel(i)~=numel(j)
157 error('PDE:refinemesh:ijNumel', 'i and j must have the same number of elements.')
158 end
159
160 k=zeros(size(i));
161
162 for l=1:numel(i)
163 k(l)=A(i(l),j(l));
164 end
165
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167
168 function A=l_assign(A,i,j,k)
169
170 if numel(i)~=numel(j) || numel(i)~=numel(k)
171 error('PDE:refinemesh:ijkNumel', 'i, j, and k must have the same number of elements.')
172 end
173
174 for l=1:numel(i)
175 A(i(l),j(l))=k(l);
176 end