1 function [K,F,W]=sparse_assembly(A,X,u0,lambda)
3 % A penalty coefficients matrix, size 3x3, s.p.d.
4 % X cell array with 3d arrays x y z coordinates of mesh vertices
5 % u0 cell array with 3d arrays initial wind vector at centers in x y z directions
6 % lambda scalar field on node
8 % K stiffness matrix, sparse
10 % W gradient of lambda
12 % do to: now computing all three K,F,W, add a switch to compute only one
15 fprintf('sparse_assembly:')
17 d = size(X,2); % dimensions
18 n = size(X{1}); % mesh size
19 nn = prod(n); % total nodes
20 nz = nn*27; % estimated nonzeros
23 K = sparse([],[],[],nn,nn,nz);
25 W = {zeros(n-1),zeros(n-1),zeros(n-1)};
35 for i1=1:m(1) % loop over elements
36 % now in element (i1,i2,i3)
37 for j3=0:1 % loop over corners of the element
40 kloc=sub2ind([2,2,2],j1+1,j2+1,j3+1); % local index
41 % kloc=1+j1+2*(j2+2*j3);
42 kglo(kloc)=sub2ind(n,i1+j1,i2+j2,i3+j3); % global index
44 Xloc(i,kloc)=X{i}(i1+j1,i2+j2,i3+j3); % node coordinate i
49 u0loc=[u0{1}(i1,i2,i3),u0{2}(i1,i2,i3),u0{3}(i1,i2,i3)]';
50 [Kloc,Floc,Jg]=hexa(A,Xloc,u0loc); % create local matrix and rhs
51 K(kglo,kglo)=K(kglo,kglo)+Kloc; % assemble to global matrix
52 F(kglo)=F(kglo)+Floc; % assemble to global rhs
53 grad = lambda(kglo)'*Jg; % grad lambda
56 W{i}(i1,i2,i3)=grad(i);
59 done = 100*((i3-1)*m(2)+i2)/(m(3)*m(2));
62 fprintf(' %g%% ',done)
73 fprintf('stiffness matrix size %g nonzeros %g density %g%%\n',nn,nz,100*nz/nn^2)
75 if length(F)>nn, size(F),error('size has grown by an index out of bounds'),end
76 check_symmetry(K,'K',eps)
77 K = (K+K')/2; % symmetrize