1 function W=w_assembly(A,X,u0,lambda,params)
4 % A penalty coefficients matrix, size 3x3, s.p.d.
5 % X cell array with 3d arrays x y z coordinates of mesh vertices
6 % u0 cell array with 3d arrays initial wind vector at centers in x y z directions
7 % lambda scalar field on node; if empty, do not compute K and F
9 % K stiffness matrix, sparse
11 % W gradient of lambda
13 % do to: now computing all three K,F,W, add a switch to compute only one
16 fprintf('w_assembly:')
18 n = size(X{1}); % mesh size
21 W = {zeros(n-1),zeros(n-1),zeros(n-1)};
28 %disp('Constructing W')
29 %disp('lambda array is')
31 %disp('u0 from matlab is')
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=1+j1+2*(j2+2*j3); % local index of the node in element (i1, i2. i3)
41 % kloc1=sub2ind([2,2,2],j1+1,j2+1,j3+1); if kloc1~=kloc, error('kloc'),end
42 k1 = i1+j1; k2 = i2+j2; k3 = i3+j3; % position of the node in the global grid
43 kglo(kloc)=k1+n(1)*((k2-1)+n(2)*(k3-1)); % global index
44 % kglo1=sub2ind(n,i1+j1,i2+j2,i3+j3); if kglo1 ~= kglo(kloc), error('kglo'), end
46 Xloc(i,kloc)=X{i}(i1+j1,i2+j2,i3+j3); % node coordinate i
54 u0loc=[u0{1}(i1,i2,i3),u0{2}(i1,i2,i3),u0{3}(i1,i2,i3)]';
60 [~,~,Jg]=hexa(A,Xloc,u0loc); % create local matrix and rhs
63 % instead, accumulate contributions to the global matrix
65 grad = lambda(kglo)'*Jg; % grad lambda
66 %disp('Local lambda is')
68 %disp(' Grad prior to multiplication by A_inv is')
71 %disp('Grad after to multiplication by A_inv is')
74 W{i}(i1,i2,i3)=grad(i);