1 function K=nd_assembly(A,X,lambda,params);
3 % A penalty coefficients matrix, size 3x3, s.p.d.
4 % X cell array with 3d arrays x y z coordinates of mesh vertices
6 % K stiffness matrix, stored as size (n1,n2,n3,3,3,3)
8 fprintf('sparse_assembly:')
10 d = size(X,2); % dimensions
11 n = size(X{1}); % mesh size in nodes
12 nn = prod(n); % total nodes
16 K = zeros(n(1),n(2),n(3),3,3,3);
18 % preallocate matrices
19 tstart=tic; % start timer
22 m = n-1; % grid size in elements
23 disp('accumulating global stiffness matrix entries')
26 for i1=1:m(1) % loop over elements
27 % now in element (i1,i2,i3)
28 % build the matrix of node coordinates to pass to hexa
29 for j3=0:1 % loop over corners of the element
32 jloc=1+j1+2*(j2+2*j3); % local index of the node in element (i1, i2. i3)
34 Xloc(i,jloc)=X{i}(i1+j1,i2+j2,i3+j3); % node coordinate i
39 [Kloc,~,~]=hexa(A,Xloc,zeros(3,1)); % compute the local matrix
40 % loop over local matrix entries j,k
44 jloc=1+j1+2*(j2+2*j3); % local index j
45 for k3=0:1 % loop over corners of the element
48 kloc=1+k1+2*(k2+2*k3);
49 % add entry of the local matrix; need
50 % to offset by 2 because all arrays
51 % dimensions in matlab start at 1
52 K(i1+j1,i2+j2,i3+j3,2+k1-j1,2+k2-j2,2+k3-j3)=...
53 K(i1+j1,i2+j2,i3+j3,2+k1-j1,2+k2-j2,2+k3-j3)+...