Skip to content

Commit 169f947

Browse files
Comments and print statements updated in terzaghi1D
1 parent c0f16f1 commit 169f947

2 files changed

Lines changed: 65 additions & 94 deletions

File tree

examples/matlab/terzaghi1D.m

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
m = 50; % Number of cells
1313

1414
% Spatial discretization
15-
a = 0;
16-
b = l;
17-
dx = (b - a)/m;
18-
xgrid = [a a+dx/2 : dx : b-dx/2 b]; % Staggered grid
15+
a = 0; % Left boundary of the domain [m]
16+
b = l; % Right boundary of the domain [m]
17+
dx = (b - a)/m; % Cell width (uniform grid spacing) [m]
18+
xgrid = [a a+dx/2 : dx : b-dx/2 b]; % Staggered grid with ghost nodes at boundaries
19+
% xgrid has m+2 points:
20+
% - ghost cell at a (Dirichlet BC)
21+
% - m internal nodes
22+
% - ghost cell at b (Neumann BC)
1923

2024
% Times to evaluate (in hours)
21-
times_hr = [1, 10, 40, 70];
22-
times_sec = times_hr * 3600;
25+
times_hr = [1, 10, 40, 70]; % Time snapshots in hours for comparison
26+
times_sec = times_hr * 3600; % Convert time points to seconds for simulation
2327

2428
% Fluid properties
2529
K = 1e-12; % Permeability [m^2]
@@ -32,41 +36,78 @@
3236
G = grad(k, m, dx);
3337
L(1,:) = 0; L(end,:) = 0;
3438

35-
p_numerical = zeros(length(xgrid), length(times_sec));
39+
p_numerical = zeros(length(xgrid), length(times_sec)); % Pressure field [Pa]
40+
q_numerical = zeros(m+1, length(times_sec)); % Darcy flux [m/s] (size = m+1)
41+
42+
% Loop over each specified final time
3643
for i = 1:length(times_sec)
3744
t_final = times_sec(i);
3845
dt = 0.03;
3946
nsteps = round(t_final / dt);
4047

41-
% Uniform Initial Condition
48+
% Uniform Initial Condition : p(x,0) = P0
4249
p = P0 * ones(size(xgrid))';
4350

51+
% Time integration using Forward Euler
4452
for step = 1:nsteps
4553
p = p + dt * cf * (L * p);
46-
p(1) = 0; % Dirichlet BC
47-
p(end) = p(end-1); % Neumann BC
54+
p(1) = 0; % Dirichlet BC at x = 0
55+
p(end) = p(end-1); % Neumann BC at x = L
4856
end
4957

5058
p_numerical(:,i) = p;
59+
60+
% Compute Darcy flux (numerical)
61+
dpdx = G * p;
62+
q = - (K / mu) * (dpdx - rho * g);
63+
q_numerical(:,i) = q(1:m+1);
64+
65+
% Print numerical results
66+
fprintf('\nNumerical results at t = %.2f hr:\n', times_hr(i));
67+
fprintf('| x (m) | Numerical p [Pa] | Darcy Flux [m/s] |\n');
68+
fprintf('|---------------|------------------|------------------|\n');
69+
for j = 1:m+1
70+
fprintf('| %13.6f | %16.6e | %16.6e |\n', xgrid(j), p_numerical(j,i), q_numerical(j,i));
71+
end
5172
end
5273

5374
%% Analytical Solution
54-
N_max = 100;
55-
p_analytical = zeros(length(xgrid), length(times_sec));
75+
N_max = 100; % Number of Fourier series terms
76+
p_analytical = zeros(length(xgrid), length(times_sec)); % Pressure field [Pa]
77+
q_analytical = zeros(m+1, length(times_sec)); % Darcy flux [m/s] (size = m+1)
78+
79+
% Loop over all time snapshots
5680
for i = 1:length(times_sec)
5781
t = times_sec(i);
5882
p = zeros(size(xgrid));
83+
84+
% Compute analytical solution using truncated Fourier sine series
5985
for N = 0:N_max
60-
n = 2*N + 1;
86+
n = 2*N + 1; % Odd terms only (satisfies boundary conditions)
6187
term = (1/n) * sin(n*pi*xgrid/(2*l)) .* exp(-(n^2)*(pi^2)*cf*t/(4*l^2));
6288
p = p + term;
6389
end
64-
p_analytical(:,i) = (4/pi) * P0 * p;
90+
p = (4/pi) * P0 * p;
91+
p_analytical(:,i) = p;
92+
93+
% Compute Darcy flux (analytical)
94+
dpdx = gradient(p, dx); % basic gradient
95+
q = - (K / mu) * (dpdx - rho * g);
96+
q_analytical(:,i) = q(1:m+1);
97+
98+
% Print analytical results
99+
fprintf('\nAnalytical results at t = %.2f hr:\n', times_hr(i));
100+
fprintf('| x (m) | Analytical p [Pa] | Darcy Flux [m/s] |\n');
101+
fprintf('|---------------|-------------------|------------------|\n');
102+
for j = 1:m+1
103+
fprintf('| %13.6f | %18.6e | %16.6e |\n', xgrid(j), p_analytical(j,i), q_analytical(j,i));
104+
end
65105
end
66106

67107
%% Combined Plot
68108
figure('Name','Terzaghi one-dimensional consolidation');
69109
set(gcf,'Color','white');
110+
70111
% Top subplot: MOLE numerical
71112
subplot(2,1,1);
72113
hold on;
@@ -94,7 +135,15 @@
94135

95136
%% Print relative L2 errors
96137
fprintf('\nRelative L2 Errors (Numerical vs Analytical):\n');
138+
fprintf('| Time [hr] | Pressure Error | Darcy Flux Error |\n');
139+
fprintf('|------------------|--------------------|---------------------|\n');
97140
for i = 1:length(times_hr)
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);
141+
% L2 error for pressure
142+
rel_err_p = norm(p_numerical(:,i) - p_analytical(:,i)) / norm(p_analytical(:,i));
143+
144+
% L2 error for flux
145+
rel_err_q = norm(q_numerical(:,i) - q_analytical(:,i)) / norm(q_analytical(:,i));
146+
147+
% Print in table format
148+
fprintf('| %16.2f | %18.6e | %19.6e |\n', times_hr(i), rel_err_p, rel_err_q);
100149
end

examples/matlab/terzaghi1D_benchmark.m

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

0 commit comments

Comments
 (0)