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 ## Use this function before plotting commands and after every call to |
|
41 ## print since print() resets output to stdout (unfortunately, gnpulot |
|
42 ## can't pop output as it can the terminal type). |
|
43 function bury_output () |
6257
|
44 f = figure (1); |
|
45 set (f, "visible", "off"); |
6003
|
46 endfunction |
|
47 |
6040
|
48 function gplotimages (nm, typ) |
6003
|
49 bury_output (); |
6040
|
50 A = sparse ([2,6,1,3,2,4,3,5,4,6,1,5], |
|
51 [1,1,2,2,3,3,4,4,5,5,6,6], 1, 6, 6); |
6003
|
52 xy = [0,4,8,6,4,2;5,0,5,7,5,7]'; |
6040
|
53 gplot (A, xy) |
|
54 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6003
|
55 bury_output (); |
|
56 endfunction |
|
57 |
|
58 function txtimages(nm,n,typ) |
|
59 a = 10*speye(n) + sparse(1:n,ceil([1:n]/2),1,n,n) + ... |
|
60 sparse(ceil([1:n]/2),1:n,1,n,n); |
|
61 if (strcmp (nm, "gplot") || strcmp (nm, "grid")) |
|
62 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
|
63 fputs (fid, "+---------------------------------+\n"); |
|
64 fputs (fid, "| Image unavailable in text mode. |\n"); |
|
65 fputs (fid, "+---------------------------------+\n"); |
|
66 fclose (fid); |
|
67 elseif (strcmp (nm, "spmatrix")) |
|
68 printsparse(a,strcat("spmatrix.",typ)); |
|
69 else |
|
70 if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_COLAMD")) && |
|
71 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_CHOLMOD"))) |
|
72 if (strcmp (nm, "spchol")) |
|
73 r1 = chol(a); |
|
74 printsparse(r1,strcat("spchol.",typ)); |
|
75 elseif (strcmp (nm, "spcholperm")) |
|
76 [r2,p2,q2]=chol(a); |
|
77 printsparse(r2,strcat("spcholperm.",typ)); |
|
78 endif |
|
79 ## printf("Text NNZ: Matrix %d, Chol %d, PermChol %d\n",nnz(a),nnz(r1),nnz(r2)); |
|
80 endif |
|
81 endif |
|
82 endfunction |
|
83 |
|
84 function otherimages(nm,n,typ) |
|
85 bury_output (); |
|
86 a = 10*speye(n) + sparse(1:n,ceil([1:n]/2),1,n,n) + ... |
|
87 sparse(ceil([1:n]/2),1:n,1,n,n); |
|
88 if (strcmp (nm, "spmatrix")) |
|
89 spy(a); |
|
90 axis("ij") |
|
91 print(strcat("spmatrix.",typ),strcat("-d",typ)) |
|
92 bury_output (); |
|
93 else |
|
94 if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_COLAMD")) && |
|
95 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_CHOLMOD"))) |
|
96 if (strcmp (nm, "spchol")) |
|
97 r1 = chol(a); |
|
98 spy(r1); |
|
99 axis("ij") |
|
100 print(strcat("spchol.",typ),strcat("-d",typ)) |
|
101 bury_output (); |
|
102 elseif (strcmp (nm, "spcholperm")) |
|
103 [r2,p2,q2]=chol(a); |
|
104 spy(r2); |
|
105 axis("ij") |
|
106 print(strcat("spcholperm.",typ),strcat("-d",typ)) |
|
107 bury_output (); |
|
108 endif |
|
109 ## printf("Image NNZ: Matrix %d, Chol %d, PermChol %d\n",nnz(a),nnz(r1),nnz(r2)); |
|
110 endif |
|
111 endif |
|
112 endfunction |
|
113 |
|
114 function printsparse(a,nm) |
|
115 fid = fopen (nm,"wt"); |
|
116 for i = 1:size(a,1) |
|
117 if (rem(i,5) == 0) |
|
118 fprintf (fid," %2d - ", i); |
|
119 else |
|
120 fprintf (fid," | "); |
|
121 endif |
|
122 for j = 1:size(a,2) |
|
123 if (a(i,j) == 0) |
|
124 fprintf(fid," ") |
|
125 else |
|
126 fprintf(fid," *") |
|
127 endif |
|
128 endfor |
|
129 fprintf(fid,"\n") |
|
130 endfor |
|
131 fprintf(fid," |-"); |
|
132 for j=1:size(a,2) |
|
133 if (rem(j,5)==0) |
|
134 fprintf(fid,"-|"); |
|
135 else |
|
136 fprintf(fid,"--"); |
|
137 endif |
|
138 endfor |
|
139 fprintf(fid,"\n") |
|
140 fprintf(fid," "); |
|
141 for j=1:size(a,2) |
|
142 if (rem(j,5)==0) |
|
143 fprintf(fid,"%2d",j); |
|
144 else |
|
145 fprintf(fid," "); |
|
146 endif |
|
147 endfor |
|
148 fclose(fid); |
|
149 endfunction |
|
150 |
|
151 function femimages (nm,typ) |
|
152 bury_output (); |
|
153 if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_COLAMD")) && |
|
154 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_CHOLMOD")) && |
|
155 !isempty(findstr(octave_config_info ("DEFS"),"HAVE_UMFPACK"))) |
|
156 ## build a rectangle |
|
157 node_y = [1;1.2;1.5;1.8;2]*ones(1,11); |
|
158 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]; |
|
159 nodes = [node_x(:), node_y(:)]; |
|
160 |
|
161 [h,w] = size(node_x); |
|
162 elems = []; |
|
163 for idx = 1:w-1 |
|
164 widx = (idx-1)*h; |
|
165 elems = [elems; widx+[(1:h-1);(2:h);h+(1:h-1)]']; |
|
166 elems = [elems; widx+[(2:h);h+(2:h);h+(1:h-1)]']; |
|
167 endfor |
|
168 |
|
169 E = size(elems,1); #No. of elements |
|
170 N = size(nodes,1); #No. of elements |
|
171 D = size(elems,2); #dimentions+1 |
|
172 |
|
173 ## Plot FEM Geometry |
|
174 elemx = elems(:,[1,2,3,1])'; |
|
175 xelems = reshape( nodes(elemx, 1), 4, E); |
|
176 yelems = reshape( nodes(elemx, 2), 4, E); |
|
177 |
|
178 ## Set element conductivity |
|
179 conductivity = [1*ones(1,16),2*ones(1,48),1*ones(1,16)]; |
|
180 |
|
181 ## Dirichlet boundary conditions |
|
182 D_nodes = [1:5, 51:55]; |
|
183 D_value = [10*ones(1,5), 20*ones(1,5)]; |
|
184 |
|
185 ## Neumann boundary conditions |
|
186 ## Note that N_value must be normalized by the boundary |
|
187 ## length and element conductivity |
|
188 N_nodes = []; |
|
189 N_value = []; |
|
190 |
|
191 ## Calculate connectivity matrix |
|
192 C = sparse((1:D*E), reshape(elems',D*E,1),1, D*E, N); |
|
193 |
|
194 ## Calculate stiffness matrix |
|
195 Siidx = floor([0:D*E-1]'/D)*D*ones(1,D) + ones(D*E,1)*(1:D) ; |
|
196 Sjidx = [1:D*E]'*ones(1,D); |
|
197 Sdata = zeros(D*E,D); |
|
198 dfact = prod(2:(D-1)); |
|
199 for j = 1:E |
|
200 a = inv([ ones(D,1), nodes( elems(j,:), : ) ]); |
|
201 const = conductivity(j)*2/dfact/abs(det(a)); |
|
202 Sdata(D*(j-1)+(1:D),:)= const * a(2:D,:)'*a(2:D,:); |
|
203 endfor |
|
204 |
|
205 ## Element-wise system matrix |
|
206 SE = sparse(Siidx,Sjidx,Sdata); |
|
207 ## Global system matrix |
|
208 S = C'* SE *C; |
|
209 |
|
210 ## Set Dirichlet boundary |
|
211 V = zeros(N,1); |
|
212 V(D_nodes) = D_value; |
|
213 idx = 1:N; |
|
214 idx(D_nodes) = []; |
|
215 |
|
216 ## Set Neumann boundary |
|
217 Q = zeros(N,1); |
|
218 Q(N_nodes) = N_value; # FIXME |
|
219 |
|
220 V(idx) = S(idx,idx)\( Q(idx) - S(idx,D_nodes)*V(D_nodes) ); |
|
221 |
|
222 velems = reshape( V(elemx), 4, E); |
|
223 |
|
224 sz = size(xelems,2); |
6178
|
225 |
|
226 plot3 (xelems, yelems, velems); |
6257
|
227 view (10, 10); |
6178
|
228 print(strcat(nm,".",typ),strcat("-d",typ)) |
|
229 bury_output (); |
6003
|
230 endif |
|
231 endfunction |
6040
|
232 |
|
233 ## There is no sparse matrix implementation available because of missing |
|
234 ## libraries, plot sombreros instead. Also plot a nice title that we are |
|
235 ## sorry about that. |
|
236 function sombreroimage (nm, typ) |
|
237 if (strcmp (typ, "txt")) |
|
238 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
|
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 |