Solution of Fick's Second Law of Diffusion Equation
Mostra commenti meno recenti
I require the code to solve the equation ∂C/∂t=D.[(∂^2 C)/(∂x^2 )+(∂^2 C)/(∂x^2 )+(∂^2 C)/(∂x^2 )]
I have value for the diffusion coefficient D, I also have value for the rate of variation of concentration ∂C/∂t, all I need is the distribution of concentration C along x, y and z axes respectively.
8 Commenti
Torsten
il 9 Gen 2023
So my guess is you mean
dC/dt = D*(d^2C/dx^2 + d^2C/dy^2 + d^2C/dz^2)
instead of
dC/dt = D*(d^2C/dx^2 + d^2C/dx^2 + d^2C/dx^2)
?
Use the PDE toolbox.
Soumyadeep
il 9 Gen 2023
Soumyadeep
il 9 Gen 2023
Soumyadeep
il 9 Gen 2023
Torsten
il 9 Gen 2023
There is no internal source, only inward diffusion of a gas takes place.
So the boundary condition is C = C_gas on all six faces of the cuboid and initial concentration is C = 0 within the cuboid ?
Soumyadeep
il 9 Gen 2023
Risposta accettata
Più risposte (1)
J. Alex Lee
il 10 Gen 2023
Modificato: J. Alex Lee
il 10 Gen 2023
I agree with the general idea in Torsten's answer using FD and method of lines. Specifically what ode solver to use on the discretized system I think depends. Granted explicit Euler is not generally practical for reasons of accuracy in addition to stability in general, but this answer is just to close the loop against Torsten's categorical objection.
With this simple problem you can find stable sets of discretization for specific values of diffusion coefficient and box dimensions, and you can then decide if those discretizations are acceptable for you w.r.t. practicality of number of steps you need to take and accuracy.
Explicit Euler should be stable as long as the condition is met:
Here is an example in 1D to illustrate: vanilla diffusion on unit domain with both boundaries fixed to unity, and initial condition of zero everywhere else.
In 1D with space indexed by subscript i and time indexed by superscript, to keep notation simple, for the generic internal node i, in a way to emphasize that the finite difference coefficients can be pulled outside, explicit Euler is, after solving for the "next" time step:

Let's put our units in terms of mm and hr.
For discretization of the 1D domain 100mm long (the first dimension of the original problem) into N nodes
N = 50;
x = linspace(0,100,N)'; % mm
So
is
dx2 = (x(2)-x(1))^2
The longest time I'm interested in is
tEnd = 96; % hours
Then as long as I choose the pair of D and
such that the above condition is met, we should be good to go. Physically, we can expect the concentration to approach some significant fraction of the steady state by 96 hours if the diffusion coefficient is
D = 30; % mm^2/hr
and to satisfy stability the min timestep we need is
dt = dx2/D * 0.5
and the number of time steps required to get to it is
nSteps = round(tEnd/dt)
This is trivial, but of course the actual numbers depend on the actual value of D, and the desired spatial resolution of the box, and some tolerance on accuracy.
So to solve the problem, construct the finite difference coefficient matrix
F = spdiags(-2*ones(N,1),0,N,N) ...
+ spdiags(ones(N,1),+1,N,N) ...
+ spdiags(ones(N,1),-1,N,N);
The simple fixed (Dirichlet) BCs just mean zero out the coefficients for the end nodes
F(1,1:2) = 0;
F(N,end-1:end) = 0;
Initial condition:
c = zeros(N,1);
c(1) = 1;
c(N) = 1;
execute and plot 10 intermediate steps
figure(1); hold on;
A = F*dt*D/dx2;
for k = 1:nSteps
c = c + A*c;
if mod(k,round(nSteps/10))==0
plot(x,c,'.-')
end
end
Is this accurate enough? Well, you'd have to check against better integrators, and decide on the trade-off in runtimes.
6 Commenti
Soumyadeep
il 10 Gen 2023
That's what I meant by "stiff":
N = 50;
F = spdiags(-2*ones(N,1),0,N,N) ...
+ spdiags(ones(N,1),+1,N,N) ...
+ spdiags(ones(N,1),-1,N,N);
S = eig(F);
Smax = max(abs(S))
Smin = min(abs(S))
Smax/Smin
Soumyadeep
il 11 Gen 2023
J. Alex Lee
il 11 Gen 2023
@Torsten, Ok I see where you're coming from now. I take your point that "methods of line are stiff" in the sense that the system of equations resulting from the space discretization is stiff, as measured by ratio of eigenvalues in the resulting matrix F (or probably more to the point A). I will assert that it isn't about "conduction or diffusion" problems though.
With my background, I tend to think more of stiffness resulting from the physics, e.g., reaction diffusion problems where reaction rate and diffusion rate scales are disparate. You can also introduce stiffness in forcing terms like boundary conditions and source terms. Stiffness in these senses seem to me less straightforward than that introduced by discretization in this simple case, and present challenges beyond what we are talking about here, which is essentially about tradeoff between accuracy, time step size, and spatial resolution.
To the point of this discussion, every choice in numerical methods represents a trade-off, so I think it is important to lay out first "what the investigator cares about". What's missing, and I'm trying to supply, is a question about runtime. Unquestionably, more sophisticated integrators can be much more efficient with time step size given a fixed spatial discretization and diffusion coefficient for some target accuracy. What's unclear to me is how the trade-off plays out with run time. To illustrate (and disclaimer, I have no idea how the numbers actually come out, and surely it depends on a lot of details): you may be able to take 10 times larger time steps, but if each time step takes 10 times longer to calculate, what have you gained?
But I guess my point is look how trivial it is to set up an explicit Euler, is it good enough for your needs?
Soumyadeep
il 11 Gen 2023
I will assert that it isn't about "conduction or diffusion" problems though.
Each system of ODEs that contains a discretization of 2nd order derivatives is stiff. The reference examples are diffusion and heat conduction problems.
Concerning the method to use in the above case:
The fastest solution will be to supply the Jacobian for ODE15S directly in sparse form as you did it for the 1d-case, tell the solver that it is constant over the complete time span and take advantage of the adaptive time stepping of the integrator.
But of course one will first have to read a little about how to supply a constant Jacobian in sparse form for ODE15S in the options structure.
Categorie
Scopri di più su Heat Transfer in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
