Implementing Crank Nicholson in spherical setting
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Matthew Hunt
el 16 de Mzo. de 2022
I am trying to solve the spherical diffusion equation using the Crank-Nicholson method. I think that I understand the method, I have included a derivation of the method in the file FD_diffusion.pdf and I have included my files for the code, spherical_fd.m is the main code used to solve the system and test_wrap.m is simply a wrapper function.
When you run the code to see what it does it seems to work relatively well, there isn't anything which seems to be wrong. However there seems to be two major issues which are apparent:
- The solution changes dramatically depending on the fineness of the discretisation taken.
- The conservation property doesn't seem to be obeyed
I have gone over my derivations time and time again, and I've looked at my code and there doesn't seem to by anything wrong but there obviously is but for the life of me, I can't figure it out.
7 comentarios
J. Alex Lee
el 25 de Mzo. de 2022
on my computer, running your algorithm spherical_fd takes ~0.85 seconds, whereas a pdepe implementation of your problem takes ~1.3 seconds...is your real application significantly different in scale that this difference is important?
Respuesta aceptada
Más respuestas (2)
J. Alex Lee
el 29 de Mzo. de 2022
I believe this should be the right formulation for CN (taking a few liberties interpreting your problem).
If you want speed, probably better to use sparse matrices and do the linear solve every iteration rather than pre-solve a full matrix and doing only the forward multiplication in time loop.
However, keep in mind that CN has a fixed accuracy in time whereas pdepe seems to use ode15s to advance so that it can have a higher order accuracy (and also will also adapt time step size), so probably compared to CN on the basis of same accuracy, it will probably be a lot faster.
D = 1e-3;
q0 = 0.15;
tc = 4;
xmesh = linspace(0,1,800).';
tspan = linspace(0,20,50);
N = numel(xmesh);
r = xmesh;
dr = xmesh(2)-xmesh(1);
dt = tspan(2)-tspan(1);
% helper
g = D*dt/2/dr/dr;
% vectorized sparse construction of matrices
tic
offdiags = ...
+ sparse(2:(N-1),1:(N-2),+(dr./r(2:(end-1))-1)*g , N,N) ...
+ sparse(2:(N-1),3:(N ),-(dr./r(2:(end-1))+1)*g , N,N);
ML = sparse(2:(N-1),2:(N-1), ones(N-2,1) + 2*g , N,N) + offdiags;
MR = sparse(2:(N-1),2:(N-1), ones(N-2,1) - 2*g , N,N) - offdiags;
% boundary conditions
ML(1, 1:3 ) = [-3/2 , 2 , -1/2]/dr;
ML(N,(N-2):N) = [ 1/2 , -2 , 3/2]/dr;
u = zeros(N,1);
figure; hold on
for k = 1:(numel(tspan)-1)
up = u;
d = MR*up;
d(N) = qfun(tspan(k+1),q0,tc)/D;
u = ML\d;
plot(r,u)
end
%% transient flux function
function q = qfun(t,q0,tc)
q = q0;
if t >= tc
q = 0;
end
end
2 comentarios
J. Alex Lee
el 27 de Mzo. de 2022
Editada: J. Alex Lee
el 27 de Mzo. de 2022
For one thing, your boundary conditions (as developed on your pdf) don't look right.
At r=0, you can just directly use Eq 8 LHS as your equation. In the scheme
A*u_(i+1) = B*u_(i) + c
this means your first row in B will be zeroed out, and the first row in A will just be your FD coefficients. You have j=-1 and j=+1, but that is confusing since there is no -1th node...you can either use 0 and 1 (1st and 2nd columns), or use a 3 point forward FD, with coefficients -3/2, 2, -1/2.
3 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!