Skip to content

Commit c0f16f1

Browse files
updated terzaghi1D
1 parent 5c0bc38 commit c0f16f1

2 files changed

Lines changed: 61 additions & 104 deletions

File tree

examples/matlab/compare_terzaghi_solutions.m

Lines changed: 0 additions & 47 deletions
This file was deleted.

examples/matlab/terzaghi1D.m

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,100 @@
1-
% Numerical solution of Terzaghi's 1D consolidation equation using MOLE
1+
% Terzaghi 1D: using MOLE operators
22
clc;
33
close all;
44

5-
addpath('../../src/matlab'); % Update this path to your MOLE operators
5+
addpath('../../src/matlab'); % MOLE operator path
66

7-
% Parameters
7+
%% Parameters
88
P0 = 10e6; % Face load in Pascals
9-
cf = 1e-4; % Consolidation (diffusion) constant
10-
L = 25; % Domain length in meters
11-
k = 2; % Order of accuracy
9+
cf = 1e-4; % Diffusion constant
10+
l = 25; % Domain length in meters
11+
k = 2; % MOLE operator order
1212
m = 50; % Number of cells
13-
a = 0; b = L;
13+
14+
% Spatial discretization
15+
a = 0;
16+
b = l;
1417
dx = (b - a)/m;
1518
xgrid = [a a+dx/2 : dx : b-dx/2 b]; % Staggered grid
1619

1720
% Times to evaluate (in hours)
1821
times_hr = [1, 10, 40, 70];
1922
times_sec = times_hr * 3600;
2023

21-
% Fluid and medium properties
22-
K = 1e-12; % Permeability [m^2]
23-
mu = 1e-3; % Dynamic viscosity [Pa·s]
24-
rho = 1000; % Fluid density [kg/m^3]
25-
g = 9.81; % Gravity [m/s^2]
26-
27-
% MOLE operators
28-
L = lap(k, m, dx); % Laplacian
29-
G = grad(k, m, dx); % Gradient
24+
% Fluid properties
25+
K = 1e-12; % Permeability [m^2]
26+
mu = 1e-3; % Dynamic viscosity [Pa·s]
27+
rho = 1000; % Fluid density [kg/m^3]
28+
g = 9.81; % Gravity [m/s^2]
3029

31-
% Enforce zero rows for boundary (to avoid Laplacian affecting boundary values)
32-
L(1,:) = 0;
33-
L(end,:) = 0;
30+
%% Numerical (MOLE) Solution
31+
L = lap(k, m, dx);
32+
G = grad(k, m, dx);
33+
L(1,:) = 0; L(end,:) = 0;
3434

35-
% Store results
36-
p_all = zeros(length(xgrid), length(times_sec));
37-
q_all = zeros(length(xgrid), length(times_sec));
38-
39-
% Plot pressure
40-
figure(1); hold on;
35+
p_numerical = zeros(length(xgrid), length(times_sec));
4136
for i = 1:length(times_sec)
4237
t_final = times_sec(i);
4338
dt = 0.03;
4439
nsteps = round(t_final / dt);
45-
46-
%Uniform Initial Condition
40+
41+
% Uniform Initial Condition
4742
p = P0 * ones(size(xgrid))';
4843

49-
% Time stepping loop
5044
for step = 1:nsteps
5145
p = p + dt * cf * (L * p);
52-
% Boundary Conditions
53-
p(1) = 0; % Dirichlet at x = 0
54-
p(end) = p(end-1); % Neumann at x = L (zero gradient)
46+
p(1) = 0; % Dirichlet BC
47+
p(end) = p(end-1); % Neumann BC
5548
end
56-
57-
% Store final pressure
58-
p_all(:,i) = p;
5949

60-
% Compute gradient and Darcy flux
61-
dpdx = G * p;
62-
q = - (K / mu) * (dpdx - rho * g);
63-
q_all(1:m+1,i) = q(1:m+1); % Exclude ghost point
50+
p_numerical(:,i) = p;
51+
end
6452

65-
% Plot pressure profile
66-
semilogy(xgrid, p/1e6, '-o', 'DisplayName', ['t = ' num2str(times_hr(i)) ' hr']);
53+
%% Analytical Solution
54+
N_max = 100;
55+
p_analytical = zeros(length(xgrid), length(times_sec));
56+
for i = 1:length(times_sec)
57+
t = times_sec(i);
58+
p = zeros(size(xgrid));
59+
for N = 0:N_max
60+
n = 2*N + 1;
61+
term = (1/n) * sin(n*pi*xgrid/(2*l)) .* exp(-(n^2)*(pi^2)*cf*t/(4*l^2));
62+
p = p + term;
63+
end
64+
p_analytical(:,i) = (4/pi) * P0 * p;
6765
end
68-
title('Pressure p(x,t) at various times');
69-
xlabel('x (m)');
66+
67+
%% Combined Plot
68+
figure('Name','Terzaghi one-dimensional consolidation');
69+
set(gcf,'Color','white');
70+
% Top subplot: MOLE numerical
71+
subplot(2,1,1);
72+
hold on;
73+
for i = 1:length(times_sec)
74+
semilogy(xgrid, p_numerical(:,i)/1e6, '-o', 'DisplayName', [num2str(times_hr(i)) ' hr']);
75+
end
76+
title('MOLE Numerical Solution');
7077
ylabel('Excess Pore Pressure p(x,t) [MPa]');
78+
axis([0 l 1e-3 10]);
7179
legend('Location', 'southeast');
7280
grid on;
7381

74-
% Plot Darcy flux
75-
figure(2); hold on;
76-
x_flux = linspace(a + dx/2, b - dx/2, m+1);
82+
% Bottom subplot: Analytical
83+
subplot(2,1,2);
84+
hold on;
7785
for i = 1:length(times_sec)
78-
q = q_all(1:m+1,i);
79-
semilogy(x_flux, q, '-s', 'DisplayName', ['t = ' num2str(times_hr(i)) ' hr']);
86+
semilogy(xgrid, p_analytical(:,i)/1e6, '--s', 'DisplayName', [num2str(times_hr(i)) ' hr']);
8087
end
81-
title('Darcy Flux q(x,t) at various times');
88+
title('Analytical Benchmark Solution');
8289
xlabel('x (m)');
83-
ylabel('q(x,t) [m/s]');
90+
ylabel('Excess Pore Pressure p(x,t) [MPa]');
91+
axis([0 l 1e-3 10]);
8492
legend('Location', 'southeast');
8593
grid on;
8694

87-
% Print final values
95+
%% Print relative L2 errors
96+
fprintf('\nRelative L2 Errors (Numerical vs Analytical):\n');
8897
for i = 1:length(times_hr)
89-
fprintf('\nFinal pressure p(x,t = %.2f hr):\n', times_hr(i));
90-
disp(p_all(:,i)');
91-
fprintf('Final Darcy flux q(x,t = %.2f hr):\n', times_hr(i));
92-
disp(q_all(1:m+1,i)');
98+
rel_err = norm(p_numerical(:,i) - p_analytical(:,i)) / norm(p_analytical(:,i));
99+
fprintf('t = %.2f hr: %.6e\n', times_hr(i), rel_err);
93100
end
94-
95-
% Save pressure for comparison
96-
p_numerical = p_all;

0 commit comments

Comments
 (0)