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