1 function K2=ndt_convert(K,n1,n2,n3,st)
3 % K2=ndt_convert(K,n1,n2,n3,st)
4 % convert ndt matrix from one storage format to another
5 % st = storage type of output, 27 or 14 or 0=sparse
6 % n1 n2 n3 = dimensions of output, if K is sparse
7 if issparse(K) % storage type of input:
12 error('input must satisfy n1*n2*n3 == n')
15 error('Sparse input matrix must be symmetric')
18 t1=ndt_storage_table(27); % need to loop over all output entries
21 [n1,n2,n3,st1]=size(K);
22 t1=ndt_storage_table(st1);
24 switch st % set storage type of output: 0 or 14 or 27
32 st,error('allowable values of st: 27 or nons, 14 or symm, 0 or sparse')
35 t2=ndt_storage_table(st2);
36 K2=zeros(n1,n2,n3,st2);
39 mnz=n*27; % max nonzeros
44 % K2=sparse([],[],[],n,n,mnz);
46 g=@(i1,i2,i3)i1+n1*(i2-1+n2*(i3-1)); % flattened index in 1:n by 1:n matrix
53 % global index of neighbor node k
57 % relative index of the row where the entry (i,k)
58 % is stored, 0 0 0 = this row
59 % in fortran we won't have the 2+ because
60 % the array t will be indexed -1:1
61 % row m where the entry (i,k) is stored in K
62 m3 = i3+t1(3,2+j1,2+j2,2+j3);
63 m2 = i2+t1(2,2+j1,2+j2,2+j3);
64 m1 = i1+t1(1,2+j1,2+j2,2+j3);
65 % index of the matrix entry (i,k) in K(m,:)
66 jx = t1(4,2+j1,2+j2,2+j3);
67 if ~(m1<1 || m1>n1 || m2<1 || m2>n2 || m3<1 || m3>n3 || ...
68 k1<1 || k1>n1 || k2<1 || k2>n2 || k3<1 || k3>n3 )
71 v = K(m1,m2,m3,jx); % value of the entry in ndt
76 Kg(ig,jg)=0; % mark location used
78 if st2 % ndt storage scheme
79 % entry (i,k) is stored in K2(l,p)
80 l3=i3+t2(3,2+j1,2+j2,2+j3); % row + offset
81 l2=i2+t2(2,2+j1,2+j2,2+j3); % row + offset
82 l1=i1+t2(1,2+j1,2+j2,2+j3); % row + offset
83 if ~(l1<1 || l1>n1 || l2<1 || l2>n2 || l3<1 || l3>n3)
84 px = t2(4,2+j1,2+j2,2+j3); % index in the row
105 error('K is not in the 3D grid form, nonzeros left')
109 K2 = sparse(ii(1:nn),jj(1:nn),vv(1:nn),n,n);