1 function res = Dmul_v(X,trans,v)
2 % multiplies the divergence matrix (or its transpose) and a vector
3 % the operation is res = op(D) * v, where op(D) = D or D^T
6 % trans indicates whether the matrix is transposed or not:
7 % trans = 'T' or trans = 't': op(D) = D^T
8 % trans = 'N' or trans = 'n': op(D) = D
10 % v vector to multiply
14 if ~exist('trans','var')
17 if (trans ~= 'T' && trans ~= 't' && trans ~= 'N' && trans ~= 'n')
18 error('trans must be N, T, n, or t')
25 x = X{1}; y = X{2}; z = X{3};
28 if (trans == 'T' || trans == 't')
29 res = zeros((nx+1)*ny*nz+nx*(ny+1)*nz+nx*ny*(nz+1),1);
31 res = zeros(nx*ny*nz,1);
34 if (trans == 't' || trans == 'T')
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% D^T * v %%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 res(ny*(nx+1)*(k-1)+(nx+1)*(j-1)+1) = ...
46 -v(nx * (helper_x - 1) + 1);
48 res(ny*(nx+1)*(k-1)+(nx+1)*(j-1)+nx+1) = ...
51 res(ny*(nx+1)*(k-1)+(nx+1)*(j-1)+i) = ...
52 v(nx * (helper_x-1) + i-1)-v(nx * (helper_x-1)+i);
55 helper_x = helper_x + 1;
58 add_factor = (nx+1)*ny*nz;
63 res(add_factor + (ny+1)*nx*(k-1)+nx*(j-1)+i) = ...
66 res(add_factor + (ny+1) * nx * k - nx + i) = ...
68 helper_y = helper_y + 1;
70 res(add_factor + (ny + 1) * nx * (k-1) + nx * (j - 1) + i) = ...
71 v(helper_y)-v(helper_y + nx);
72 helper_y = helper_y + 1;
77 add_factor = add_factor + (ny+1)*nx*nz;
82 res(add_factor + (ny+1)*nx*(k-1)+nx*(j-1)+i) = ...
83 -v(helper_z - 1 + (j - 1) * nx + i);
85 res(add_factor + nx * ny * nz + (j-1)*nx + i) = ...
87 helper_z = helper_z + 1;
89 res(add_factor + ny * nx * (k-1) + nx * (j-1) + i) = ...
90 v(helper_z)-v(helper_z + nx*ny);
91 helper_z = helper_z + 1;
97 else % trans = 'N' || trans = 'n'
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% D * v %%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 res((i-1) + (j-1)*nx + (k-1)*nx*(ny)) = ...
105 res((i-1) + (j-1)*nx + (k-1)*nx*(ny)) ...
106 - v((i-1) + (j-1)*(nx+1) + (k-1)*(nx+1)*(ny));
109 res(i + (j-1)*nx + (k-1)*nx*(ny)) = ...
110 res(i + (j-1)*nx + (k-1)*nx*(ny)) ...
111 + v(i + 1 + (j-1)*(nx+1) + (k-1)*(nx+1)*(ny));
116 add_factor = (nx+1)*ny*nz;
121 res(i + (j-2)*nx + (k-1)*nx*ny) = ...
122 res(i + (j-2)*nx + (k-1)*nx*ny) ...
123 - v(add_factor + i + (j-2)*nx + (k-1)*nx*(ny+1));
126 res(i + (j-1)*nx + (k-1)*nx*ny) = ...
127 res(i + (j-1)*nx + (k-1)*nx*ny) ...
128 + v(add_factor + i + j*nx + (k-1)*nx*(ny+1));
133 add_factor = add_factor + nx*(ny+1)*nz;
138 res(i + (j-1)*nx + (k-2)*nx*ny) = ...
139 res(i + (j-1)*nx + (k-2)*nx*ny) ...
140 - v(add_factor + i + (j-1)*nx + (k-2)*nx*ny);
143 res(i + (j-1)*nx + (k-1)*nx*ny) = ...
144 res(i + (j-1)*nx + (k-1)*nx*ny) ...
145 + v(add_factor + i + (j-1)*nx + (k)*nx*ny);