7018
|
1 ## Copyright (C) 2006, 2007 David Bateman |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 |
6126
|
19 function sparseimages (nm, typ) |
6040
|
20 if (! isempty (findstr (octave_config_info ("DEFS"), "HAVE_COLAMD")) |
|
21 && ! isempty (findstr (octave_config_info ("DEFS"), "HAVE_CHOLMOD")) |
|
22 && ! isempty (findstr (octave_config_info ("DEFS"), "HAVE_UMFPACK"))) |
6126
|
23 if (strcmp(typ,"txt")) |
|
24 txtimages (nm, 15, typ); |
6003
|
25 else |
6126
|
26 if (strcmp (nm, "gplot")) |
|
27 gplotimages ("gplot", typ); |
|
28 elseif (strcmp (nm, "grid")) |
|
29 femimages ("grid", typ); |
|
30 else |
|
31 otherimages (nm, 200, typ); |
|
32 endif |
6003
|
33 endif |
6040
|
34 else ## There is no sparse matrix implementation available because |
|
35 ## of missing libraries, plot sombreros instead |
|
36 sombreroimage (nm, typ); |
|
37 endif |
6003
|
38 endfunction |
|
39 |
|
40 function bury_output () |
6257
|
41 f = figure (1); |
|
42 set (f, "visible", "off"); |
6003
|
43 endfunction |
|
44 |
6040
|
45 function gplotimages (nm, typ) |
6003
|
46 bury_output (); |
6040
|
47 A = sparse ([2,6,1,3,2,4,3,5,4,6,1,5], |
|
48 [1,1,2,2,3,3,4,4,5,5,6,6], 1, 6, 6); |
6003
|
49 xy = [0,4,8,6,4,2;5,0,5,7,5,7]'; |
6040
|
50 gplot (A, xy) |
|
51 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6003
|
52 bury_output (); |
|
53 endfunction |
|
54 |
|
55 function txtimages(nm,n,typ) |
|
56 a = 10*speye(n) + sparse(1:n,ceil([1:n]/2),1,n,n) + ... |
|
57 sparse(ceil([1:n]/2),1:n,1,n,n); |
|
58 if (strcmp (nm, "gplot") || strcmp (nm, "grid")) |
|
59 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
7256
|
60 fputs (fid, "\n"); |
6003
|
61 fputs (fid, "+---------------------------------+\n"); |
|
62 fputs (fid, "| Image unavailable in text mode. |\n"); |
|
63 fputs (fid, "+---------------------------------+\n"); |
|
64 fclose (fid); |
|
65 elseif (strcmp (nm, "spmatrix")) |
|
66 printsparse(a,strcat("spmatrix.",typ)); |
|
67 else |
|
68 if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_COLAMD")) && |
|
69 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_CHOLMOD"))) |
|
70 if (strcmp (nm, "spchol")) |
|
71 r1 = chol(a); |
|
72 printsparse(r1,strcat("spchol.",typ)); |
|
73 elseif (strcmp (nm, "spcholperm")) |
|
74 [r2,p2,q2]=chol(a); |
|
75 printsparse(r2,strcat("spcholperm.",typ)); |
|
76 endif |
|
77 ## printf("Text NNZ: Matrix %d, Chol %d, PermChol %d\n",nnz(a),nnz(r1),nnz(r2)); |
|
78 endif |
|
79 endif |
|
80 endfunction |
|
81 |
|
82 function otherimages(nm,n,typ) |
|
83 bury_output (); |
|
84 a = 10*speye(n) + sparse(1:n,ceil([1:n]/2),1,n,n) + ... |
|
85 sparse(ceil([1:n]/2),1:n,1,n,n); |
|
86 if (strcmp (nm, "spmatrix")) |
|
87 spy(a); |
|
88 axis("ij") |
|
89 print(strcat("spmatrix.",typ),strcat("-d",typ)) |
|
90 bury_output (); |
|
91 else |
|
92 if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_COLAMD")) && |
|
93 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_CHOLMOD"))) |
|
94 if (strcmp (nm, "spchol")) |
|
95 r1 = chol(a); |
|
96 spy(r1); |
|
97 axis("ij") |
|
98 print(strcat("spchol.",typ),strcat("-d",typ)) |
|
99 bury_output (); |
|
100 elseif (strcmp (nm, "spcholperm")) |
|
101 [r2,p2,q2]=chol(a); |
|
102 spy(r2); |
|
103 axis("ij") |
|
104 print(strcat("spcholperm.",typ),strcat("-d",typ)) |
|
105 bury_output (); |
|
106 endif |
|
107 ## printf("Image NNZ: Matrix %d, Chol %d, PermChol %d\n",nnz(a),nnz(r1),nnz(r2)); |
|
108 endif |
|
109 endif |
|
110 endfunction |
|
111 |
|
112 function printsparse(a,nm) |
|
113 fid = fopen (nm,"wt"); |
7256
|
114 fputs (fid, "\n"); |
6003
|
115 for i = 1:size(a,1) |
|
116 if (rem(i,5) == 0) |
|
117 fprintf (fid," %2d - ", i); |
|
118 else |
|
119 fprintf (fid," | "); |
|
120 endif |
|
121 for j = 1:size(a,2) |
|
122 if (a(i,j) == 0) |
|
123 fprintf(fid," ") |
|
124 else |
|
125 fprintf(fid," *") |
|
126 endif |
|
127 endfor |
|
128 fprintf(fid,"\n") |
|
129 endfor |
|
130 fprintf(fid," |-"); |
|
131 for j=1:size(a,2) |
|
132 if (rem(j,5)==0) |
|
133 fprintf(fid,"-|"); |
|
134 else |
|
135 fprintf(fid,"--"); |
|
136 endif |
|
137 endfor |
|
138 fprintf(fid,"\n") |
|
139 fprintf(fid," "); |
|
140 for j=1:size(a,2) |
|
141 if (rem(j,5)==0) |
|
142 fprintf(fid,"%2d",j); |
|
143 else |
|
144 fprintf(fid," "); |
|
145 endif |
|
146 endfor |
|
147 fclose(fid); |
|
148 endfunction |
|
149 |
|
150 function femimages (nm,typ) |
|
151 bury_output (); |
|
152 if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_COLAMD")) && |
|
153 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_CHOLMOD")) && |
|
154 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_UMFPACK"))) |
|
155 ## build a rectangle |
|
156 node_y = [1;1.2;1.5;1.8;2]*ones(1,11); |
|
157 node_x = ones(5,1)*[1,1.05,1.1,1.2,1.3,1.5,1.7,1.8,1.9,1.95,2]; |
|
158 nodes = [node_x(:), node_y(:)]; |
|
159 |
|
160 [h,w] = size(node_x); |
|
161 elems = []; |
|
162 for idx = 1:w-1 |
|
163 widx = (idx-1)*h; |
|
164 elems = [elems; widx+[(1:h-1);(2:h);h+(1:h-1)]']; |
|
165 elems = [elems; widx+[(2:h);h+(2:h);h+(1:h-1)]']; |
|
166 endfor |
|
167 |
|
168 E = size(elems,1); #No. of elements |
|
169 N = size(nodes,1); #No. of elements |
|
170 D = size(elems,2); #dimentions+1 |
|
171 |
|
172 ## Plot FEM Geometry |
|
173 elemx = elems(:,[1,2,3,1])'; |
|
174 xelems = reshape( nodes(elemx, 1), 4, E); |
|
175 yelems = reshape( nodes(elemx, 2), 4, E); |
|
176 |
|
177 ## Set element conductivity |
|
178 conductivity = [1*ones(1,16),2*ones(1,48),1*ones(1,16)]; |
|
179 |
|
180 ## Dirichlet boundary conditions |
|
181 D_nodes = [1:5, 51:55]; |
|
182 D_value = [10*ones(1,5), 20*ones(1,5)]; |
|
183 |
|
184 ## Neumann boundary conditions |
|
185 ## Note that N_value must be normalized by the boundary |
|
186 ## length and element conductivity |
|
187 N_nodes = []; |
|
188 N_value = []; |
|
189 |
|
190 ## Calculate connectivity matrix |
|
191 C = sparse((1:D*E), reshape(elems',D*E,1),1, D*E, N); |
|
192 |
|
193 ## Calculate stiffness matrix |
|
194 Siidx = floor([0:D*E-1]'/D)*D*ones(1,D) + ones(D*E,1)*(1:D) ; |
|
195 Sjidx = [1:D*E]'*ones(1,D); |
|
196 Sdata = zeros(D*E,D); |
|
197 dfact = prod(2:(D-1)); |
|
198 for j = 1:E |
|
199 a = inv([ ones(D,1), nodes( elems(j,:), : ) ]); |
|
200 const = conductivity(j)*2/dfact/abs(det(a)); |
|
201 Sdata(D*(j-1)+(1:D),:)= const * a(2:D,:)'*a(2:D,:); |
|
202 endfor |
|
203 |
|
204 ## Element-wise system matrix |
|
205 SE = sparse(Siidx,Sjidx,Sdata); |
|
206 ## Global system matrix |
|
207 S = C'* SE *C; |
|
208 |
|
209 ## Set Dirichlet boundary |
|
210 V = zeros(N,1); |
|
211 V(D_nodes) = D_value; |
|
212 idx = 1:N; |
|
213 idx(D_nodes) = []; |
|
214 |
|
215 ## Set Neumann boundary |
|
216 Q = zeros(N,1); |
|
217 Q(N_nodes) = N_value; # FIXME |
|
218 |
|
219 V(idx) = S(idx,idx)\( Q(idx) - S(idx,D_nodes)*V(D_nodes) ); |
|
220 |
|
221 velems = reshape( V(elemx), 4, E); |
|
222 |
|
223 sz = size(xelems,2); |
6178
|
224 |
|
225 plot3 (xelems, yelems, velems); |
6257
|
226 view (10, 10); |
6178
|
227 print(strcat(nm,".",typ),strcat("-d",typ)) |
|
228 bury_output (); |
6003
|
229 endif |
|
230 endfunction |
6040
|
231 |
|
232 ## There is no sparse matrix implementation available because of missing |
|
233 ## libraries, plot sombreros instead. Also plot a nice title that we are |
|
234 ## sorry about that. |
|
235 function sombreroimage (nm, typ) |
|
236 if (strcmp (typ, "txt")) |
|
237 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
7256
|
238 fputs (fid, "\n"); |
6040
|
239 fputs (fid, "+---------------------------------------+\n"); |
|
240 fputs (fid, "| Image unavailable because of a |\n"); |
|
241 fputs (fid, "| missing sparse matrix implementation. |\n"); |
|
242 fputs (fid, "+---------------------------------------+\n"); |
|
243 fclose (fid); |
|
244 return; |
|
245 else ## if (!strcmp (typ, "txt")) |
|
246 |
|
247 bury_output (); |
|
248 |
|
249 x = y = linspace (-8, 8, 41)'; |
|
250 [xx, yy] = meshgrid (x, y); |
|
251 r = sqrt (xx .^ 2 + yy .^ 2) + eps; |
|
252 z = sin (r) ./ r; |
|
253 unwind_protect |
6257
|
254 mesh (x, y, z); |
|
255 title ("Sorry, graphics not available because octave was\\ncompiled without the sparse matrix implementation."); |
6040
|
256 unwind_protect_cleanup |
|
257 print (strcat (nm, ".", typ), strcat ("-d", typ)); |
|
258 bury_output (); |
|
259 end_unwind_protect |
|
260 endif |
|
261 endfunction |