Skip to content

Commit e745a26

Browse files
authored
Mimetic Mixed Boundary Conditions Op - MATLAB/Octave
1 parent 3e2c57f commit e745a26

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

mole_MATLAB/mixedBC.m

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function BC = mixedBC(k, m, dx, left, coeffs_left, right, coeffs_right)
2+
% Constructs a 1D mimetic mixed boundary conditions operator
3+
%
4+
% Parameters:
5+
% k : Order of accuracy
6+
% m : Number of cells
7+
% dx : Step size
8+
% left : Type of boundary condition at the left boundary ('Dirichlet', 'Neumann', 'Robin')
9+
% coeffs_left : Coefficients for the left boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
10+
% right : Type of boundary condition at the right boundary ('Dirichlet', 'Neumann', 'Robin')
11+
% coeffs_right : Coefficients for the right boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
12+
13+
A = sparse(m+2, m+2);
14+
B = sparse(m+2, m+1);
15+
16+
switch left
17+
case 'Dirichlet'
18+
A(1, 1) = coeffs_left;
19+
case 'Neumann'
20+
B(1, 1) = -coeffs_left;
21+
case 'Robin'
22+
A(1, 1) = coeffs_left(1);
23+
B(1, 1) = -coeffs_left(2);
24+
otherwise
25+
error('Unknown boundary condition type for left boundary');
26+
end
27+
28+
switch right
29+
case 'Dirichlet'
30+
A(end, end) = coeffs_right;
31+
case 'Neumann'
32+
B(end, end) = coeffs_right;
33+
case 'Robin'
34+
A(end, end) = coeffs_right(1);
35+
B(end, end) = coeffs_right(2);
36+
otherwise
37+
error('Unknown boundary condition type for right boundary');
38+
end
39+
40+
G = grad(k, m, dx);
41+
42+
BC = A + B*G;
43+
end

mole_MATLAB/mixedBC2D.m

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function BC = mixedBC2D(k, m, dx, n, dy, left, coeffs_left, right, coeffs_right, bottom, coeffs_bottom, top, coeffs_top)
2+
% Constructs a 2D mimetic mixed boundary conditions operator
3+
%
4+
% Parameters:
5+
% k : Order of accuracy
6+
% m : Number of cells in x-direction
7+
% dx : Step size in x-direction
8+
% n : Number of cells in y-direction
9+
% dy : Step size in y-direction
10+
% left : Type of boundary condition at the left boundary ('Dirichlet', 'Neumann', 'Robin')
11+
% coeffs_left : Coefficients for the left boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
12+
% right : Type of boundary condition at the right boundary ('Dirichlet', 'Neumann', 'Robin')
13+
% coeffs_right : Coefficients for the right boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
14+
% bottom : Type of boundary condition at the bottom boundary ('Dirichlet', 'Neumann', 'Robin')
15+
% coeffs_bottom : Coefficients for the bottom boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
16+
% top : Type of boundary condition at the top boundary ('Dirichlet', 'Neumann', 'Robin')
17+
% coeffs_top : Coefficients for the top boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
18+
19+
% 1-D boundary operators
20+
Bm = mixedBC(k, m, dx, left, coeffs_left, right, coeffs_right);
21+
Bn = mixedBC(k, n, dy, top, coeffs_top, bottom, coeffs_bottom);
22+
23+
Im = speye(m+2);
24+
25+
In = speye(n+2);
26+
In(1, 1) = 0;
27+
In(end, end) = 0;
28+
29+
BC1 = kron(In, Bm);
30+
BC2 = kron(Bn, Im);
31+
32+
BC = BC1 + BC2;
33+
end

mole_MATLAB/mixedBC3D.m

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function BC = mixedBC3D(k, m, dx, n, dy, o, dz, left, coeffs_left, right, coeffs_right, bottom, coeffs_bottom, top, coeffs_top, front, coeffs_front, back, coeffs_back)
2+
% Constructs a 3D mimetic mixed boundary conditions operator
3+
%
4+
% Parameters:
5+
% k : Order of accuracy
6+
% m : Number of cells in x-direction
7+
% dx : Step size in x-direction
8+
% n : Number of cells in y-direction
9+
% dy : Step size in y-direction
10+
% o : Number of cells in z-direction
11+
% dz : Step size in z-direction
12+
% left : Type of boundary condition at the left boundary ('Dirichlet', 'Neumann', 'Robin')
13+
% coeffs_left : Coefficients for the left boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
14+
% right : Type of boundary condition at the right boundary ('Dirichlet', 'Neumann', 'Robin')
15+
% coeffs_right : Coefficients for the right boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
16+
% bottom : Type of boundary condition at the bottom boundary ('Dirichlet', 'Neumann', 'Robin')
17+
% coeffs_bottom : Coefficients for the bottom boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
18+
% top : Type of boundary condition at the top boundary ('Dirichlet', 'Neumann', 'Robin')
19+
% coeffs_top : Coefficients for the top boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
20+
% front : Type of boundary condition at the front boundary ('Dirichlet', 'Neumann', 'Robin')
21+
% coeffs_front : Coefficients for the front boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
22+
% back : Type of boundary condition at the back boundary ('Dirichlet', 'Neumann', 'Robin')
23+
% coeffs_back : Coefficients for the back boundary condition (a, b for Robin, otherwise coeff. for Dirichlet or Neumann)
24+
25+
% 1-D boundary operators
26+
Bx = mixedBC(k, m, dx, left, coeffs_left, right, coeffs_right);
27+
By = mixedBC(k, n, dy, bottom, coeffs_bottom, top, coeffs_top);
28+
Bz = mixedBC(k, o, dz, front, coeffs_front, back, coeffs_back);
29+
30+
Im = speye(m+2);
31+
In = speye(n+2);
32+
Io = speye(o+2);
33+
34+
In(1, 1) = 0;
35+
In(end, end) = 0;
36+
Io(1, 1) = 0;
37+
Io(end, end) = 0;
38+
39+
BC1 = kron(kron(Io, In), Bx);
40+
BC2 = kron(kron(Io, By), Im);
41+
BC3 = kron(kron(Bz, In), Im);
42+
43+
BC = BC1 + BC2 + BC3;
44+
end

0 commit comments

Comments
 (0)