Negative concentrations solving nonlinear coupled PDEs with pdepe
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I'm trying to solve the following system of equations (these are partial derivatives of course):
d(n1)/dt = D1*f*d^2(n1)/dx^2 - v*(1-f)*d(n1)/dx + alpha + beta*n2 - gamma*n1*(n1+n2)
d(n2)/dt = D2*d^2(n2)/dx^2 - beta*n2 + gamma*n1*(n1+n2)
v = v1 - v2*(1+delta*n1)exp(-epsilon*n2)
The boundary conditions are zero-flux at the boundaries and initially the concentration of both species is 0. The idea is that n1 gets produced at a (slow) constant rate, leading to the production of n2, and their spatial distributions are affected by their concentrations through the advection term v. v1, v2, D1, D2, f, alpha, beta, gamma, delta, and epsilon are all positive constants. I'm encountering an issue where for some values of the parameters, the solver hangs considerably and will produce negative concentrations, while for others it seems to produce more-or-less physical-looking solutions. I understand that the system may not be stable for all parameter values, but my concern is that I'm encoding the problem incorrectly and therefore all of the results I'm seeing are suspect. In particular the solver seems to have trouble with the nonlinear term, gamma*n1*(n1+n2) and when v1 =/= v2. I've tried different x-meshes than the one below (note that I've been using L = 1000 or 10000) and it doesn't seem to help the issue; the negative concentrations appear in n1 and usually near x = 0.
Below is how I've encoded the function (being new to pdepe, I followed the template laid out in the documentation for a coupled set of PDEs). Since this is a new arena for me I'd appreciate any insight as to where the problem may lie. In particular I'd like to be sure that my encoding in MATLAB is solving what I intend to solve, with the boundary conditions specified properly.
-Justin
function [n2, n2, v_eff] = TransportPDE(alpha, beta, gamma, delta, epsilon, L, T)
% This is a function that solves the following system of coupled PDES:
%
% Dn1/Dt = f*diff1*D^2n1/Dx^2+v*(1-f)*Dn1/Dx + alpha +
% beta*n2 - gamma*n1*(n1+n2)
% Dn2/Dt = diff2*D^2n2/Dx^2 - beta*n2 +
% gamma*n1*(n1+n2)
frac = 0.92;
v1 = 0.7;
v2 = 0.7;
diff1 = 11;
diff2 = 0.01*diff_mon;
m = 0;
x = [linspace(0,0.1*L,0.16*L), linspace(0.11*L,0.89*L,0.08*L), linspace(0.9*L,L,0.16*L)];
t = [linspace(0,99,200), linspace(100,T,200)];
sol = mypdepe(m,@pdex4pde,@pdex4ic,@pdex4bc,x,t);
n1 = sol(:,:,1);
n2 = sol(:,:,2);
v_eff = v1*(ones(400,L/2.5)+delta*n1).*exp(-epsilon*n2)-v2;
% --------------------------------------------------------------
function [c,f,s] = pdex4pde(x,t,u,DuDx)
% This creates a function handle to pass to the PDE solver for the coupled
% set of equations stated above.
v = v2 - v1*(1+delta*u(1))*exp(-epsilon*u(2));
c = [1; 1];
f1 = frac*diff1*DuDx(1) + v*(1-frac);
f2 = diff2*DuDx(2);
f = [f1; f2];
s1 = alpha + beta*u(2) - gamma*u(1)*(u(1)+u(2));
s2 = gamma*u(1)*(u(1)+u(2)) - beta*u(2);
s = [s1; s2];
end
% --------------------------------------------------------------
function u0 = pdex4ic(x);
% This creates a function handle to pass to the PDE solver for the initial
% conditions.
u0 = [0; 0];
end
% --------------------------------------------------------------
function [pl,ql,pr,qr] = pdex4bc(xl,ul,xr,ur,t)
% This creates a function handle to pass to the PDE solver for the boundary
% conditions. The following conditions specify that the flux of
% both species is 0 at either end.
v1 = v2 - v1*(1+delta*ul(1))*exp(-epsilon*ul(2));
vr = v2 - v1*(1+delta*ur(1))*exp(-epsilon*ur(2));
pl = [-vl*(1-frac)*ul(1); 0];
ql = [-diff1*frac; -diff2];
pr = [-vr*(1-frac)*ur(1); 0];
qr = [-diff1*frac; -diff2];
end
% --------------------------------------------------------------
end
3 Comments
Torsten
on 29 Jun 2018
Could you write out the boundary conditions you want to set at both Ends ?
Justin Torok
on 29 Jun 2018
I want both species to be trapped and reflecting off the boundaries, so at both x = 0 and x = L:
-diff1*frac*d(n1)/dx - v*(1-frac)*n1 = 0
-diff2*d(n2)/dx = 0
As stated above, v = v2-v1*(1+delta*n1)*exp(epsilon*n2). frac is bounded between 0 and 1 (here 0.92) and determines how much of n1 is undergoing diffusion vs. advection, which are separate processes in this system.
Bill Greene
on 9 Jul 2018
Can you provide some values for alpha, beta, gamma, delta, epsilon, L, T that cause the problems you describe?
Accepted Answer
Torsten
on 29 Jun 2018
Then
f1 = frac*diff1*DuDx(1) + v*(1-frac)*u(1);
instead of
f1 = frac*diff1*DuDx(1) + v*(1-frac);
in pdex4pde
and
pl = [0;0];
ql = [1;1];
pr = [0;0];
qr = [1;1];
instead of
pl = [-vl*(1-frac)*ul(1); 0];
ql = [-diff1*frac; -diff2];
pr = [-vr*(1-frac)*ur(1); 0];
qr = [-diff1*frac; -diff2];
in pdex4bc.
Best wishes
Torsten.
23 Comments
Justin Torok
on 29 Jun 2018
Edited: Justin Torok
on 29 Jun 2018
Thank you for your help - your answer makes sense to me, although another question is now raised that I hadn't considered before and is potentially problematic both in the initial code and your proposal. Since v is dependent on n1 and n2, does this not add extra terms to the first equation because of the way the equation needs to be encoded in pdepe? What I mean is, the advection term that I would like to model is:
(1-frac)*(v2-v1*(1+delta*n1)*exp(-epsilon*n2)*d(n1)/dx
If I encode v within f in pdex4pde as it is above, then is the solver evaluating instead:
d[(1-frac)*(v2-v1*(1+delta*n1)*exp(-epsilon*n2))*n1]/dx
which has additional components? If so, should I subtract out the resultant extra components within s? More specifically, when I write this out, I get:
d[(1-frac)*v*n1]/dx = (1-frac)*v*d(n1)/dx + (1-frac)*n1*d(v)/dx
with
dv/dx = -v1*exp(-epsilon*n2)*[delta*d(n1)/dx + (1 + delta*n1)*(-epsilon*d(n2)/dx)]
So should s then include these terms, negated?
Thanks again, Justin
Torsten
on 2 Jul 2018
Put the advection term in the source term "s" and modify the settings for pl, pr, ql, qr correspondingly.
Best wishes
Torsten.
Justin Torok
on 5 Jul 2018
Torsten,
I had thought everything was fine, but I'm still encountering issues - now that mass is not conserved - and it's still tied to the v term. I recoded the function and boundary conditions as we discussed, namely:
function [c,f,s] = pdex4pde(x,t,u,DuDx)
% This creates a function handle to pass to the PDE solver for the coupled
% set of equations stated above.
v = v2 - v1*(1+delta*u(1))*exp(-epsilon*u(2));
c = [1; 1];
f1 = frac*diff1*DuDx(1);
f2 = diff2*DuDx(2);
f = [f1; f2];
s1 = alpha + beta*u(2) - gamma*u(1)*(u(1)+u(2)) + v*(1-frac)*DuDx(1);
s2 = gamma*u(1)*(u(1)+u(2)) - beta*u(2);
s = [s1; s2];
end
function [pl,ql,pr,qr] = pdex4bc(xl,ul,xr,ur,t)
% This creates a function handle to pass to the PDE solver for the boundary
% conditions.
vl = v2 - v1*(1+delta*ul(1))*exp(-epsilon*ul(2));
vr = v2 - v1*(1+delta*ur(1))*exp(-epsilon*ur(2));
pl = [vl*(1-frac)*ul(1); 0];
ql = [1; 1];
pr = [vr*(1-frac)*ur(1); 0];
qr = [1; 1];
end
When v1 =/= v2, the end amount of mass is not the same for different parameter sets, keeping alpha the same and setting delta and epsilon to 0 for simplicity. In this case the sum of the masses should be the same for zero-flux boundary conditions I've tried to encode, which I do observe for v1 = v2 (i.e. v = 0). I really thought I understood the function calls here after our discussion but clearly something is still wrong.
Torsten
on 6 Jul 2018
Please add equations and boundary conditions you want to set in a mathematical notation.
Best wishes
Torsten.
Justin Torok
on 6 Jul 2018
The equations and boundary conditions are the same as in the original post, namely:
d(n1)/dt = D1*f*d^2(n1)/dx^2 - v*(1-f)*d(n1)/dx + alpha + beta*n2 - gamma*n1*(n1+n2)
d(n2)/dt = D2*d^2(n2)/dx^2 - beta*n2 + gamma*n1*(n1+n2)
with v = v1 - v2*(1+delta*n1)exp(-epsilon*n2)
Boundary conditions (same at both ends):
D1*frac*d(n1)/dx + v*(1-frac)*n1 = 0
D2*d(n2)/dx = 0
The symmetry in the equations and the no-flux boundary conditions should lead to a conservation law of sorts: d(n1 + n2)/dt ~ alpha, so the total amount of mass in the system at the end of any simulation (keeping simulation time constant) should be the same. I notice this when v1, v2, delta, and epsilon are all set to 0 independent of the other parameters, as I would expect. When v is nonzero, I see significant fluctuations in total mass, which tells me there is something wrong with either the way I've set up the boundary conditions or the way I've encoded them - I want all mass to reflect off the boundaries as though I'm dealing with a closed tube.
Thanks, Justin
Torsten
on 6 Jul 2018
If mass shall be conserved, the first boundary condition should read
D1*frac*d(n1)/dx - v*(1-frac)*n1 = 0
This should work for v2 = 0.
For v2 not equal to 0, mass will not be conserved because
D1*f*d^2(n1)/dx^2 + D2*d^2(n2)/dx^2 - v*(1-f)*d(n1)/dx
will not equal
d/dx ( D1*f*d(n1)/dx + D2*d(n2)/dx - v*(1-f)*n1 )
The following code reflects your equations (except for the modification of the first boundary condition):
function [c,f,s] = pdex4pde(x,t,u,DuDx)
% This creates a function handle to pass to the PDE solver for the coupled
% set of equations stated above.
v = v1 - v2*(1+delta*u(1))*exp(-epsilon*u(2));
c = [1; 1];
f1 = frac*diff1*DuDx(1);
f2 = diff2*DuDx(2);
f = [f1; f2];
s1 = alpha + beta*u(2) - gamma*u(1)*(u(1)+u(2)) - v*(1-frac)*DuDx(1);
s2 = gamma*u(1)*(u(1)+u(2)) - beta*u(2);
s = [s1; s2];
end
function [pl,ql,pr,qr] = pdex4bc(xl,ul,xr,ur,t)
% This creates a function handle to pass to the PDE solver for the boundary
% conditions.
vl = v1 - v2*(1+delta*ul(1))*exp(-epsilon*ul(2));
vr = v1 - v2*(1+delta*ur(1))*exp(-epsilon*ur(2));
pl = [-vl*(1-frac)*ul(1); 0];
ql = [1; 1];
pr = [-vr*(1-frac)*ur(1); 0];
qr = [1; 1];
end
Justin Torok
on 6 Jul 2018
Edited: Justin Torok
on 6 Jul 2018
First, I do not observe mass conservation for delta and epsilon = 0 where this derivative problem would not be an issue. In that case v = v2 - v1 = constant for all values of n1 and n2. This is precisely what is bothering me. I expected that if there was a problem with the way I'm encoding the boundary conditions, it would be reflected by the n1 and n2 dependence in v - instead if I have a term where d(n1)/dx is nonzero, even if the coefficient is constant, there is suddenly no mass conservation.
Second, If I'm understanding you correctly, the way I'm encoding the equations and boundary conditions will lead to a lack of mass conservation when delta and epsilon are not equal to zero. This is not the situation I want. In a previous comment I highlighted the issue of other terms in d(v*(1-frac)*n1)/dx and suggested that I encode that in f but subtract out those extra terms in s. You suggested that I instead encode this expression in s and leave f with just diffusion terms. I'm not sure which is right but neither conserves mass by my observations.
Torsten
on 6 Jul 2018
You observed the changes I made to your code ?
v = v1 - v2*(1+delta*u(1))*exp(-epsilon*u(2));
s1 = alpha + beta*u(2) - gamma*u(1)*(u(1)+u(2)) - v*(1-frac)*DuDx(1);
instead of
v = v2 - v1*(1+delta*u(1))*exp(-epsilon*u(2));
s1 = alpha + beta*u(2) - gamma*u(1)*(u(1)+u(2)) + v*(1-frac)*DuDx(1);
in pdex4pde and
vl = v1 - v2*(1+delta*ul(1))*exp(-epsilon*ul(2));
vr = v1 - v2*(1+delta*ur(1))*exp(-epsilon*ur(2));
pl = [-vl*(1-frac)*ul(1); 0];
pr = [-vr*(1-frac)*ur(1); 0];
instead of
vl = v2 - v1*(1+delta*ul(1))*exp(-epsilon*ul(2));
vr = v2 - v1*(1+delta*ur(1))*exp(-epsilon*ur(2));
pl = [vl*(1-frac)*ul(1); 0];
pr = [vr*(1-frac)*ur(1); 0];
in pdex4bc ?
And still no mass conservation when you set v2 = 0 ?
Justin Torok
on 6 Jul 2018
Edited: Justin Torok
on 6 Jul 2018
Unfortunately, the answer is still no after 1) changing the order of v1 and v2, 2) negating the term in s (although this isn't what I want to encode exactly), and 3) negating the first term of pl and pr. While this is concerning, ultimately v2, epsilon, and delta are not zero; my larger concern is that this encoding will not conserve mass when v2 is nonzero. Mass should be conserved for this system when v2, delta and epsilon are not 0 - i.e. I want this to hold:
d/dx ( D1*f*d(n1)/dx + D2*d(n2)/dx - v*(1-f)*n1 ) = 0
What is the best way for me to encode this?
Edit: I applied a much tighter x-mesh, and under those conditions, mass conservation seems to improve. It's not exact but it's < 1% variance for the parameters I tried, which is much improved. The previous point about mass conservation needing to hold when v2 is nonzero still stands, though.
Torsten
on 6 Jul 2018
Mass conservation in your sense leads to the general condition
(D1*f*d(n1)/dx + D2*d(n2)/dx - v*(1-f)*n1) @x=L -
(D1*f*d(n1)/dx + D2*d(n2)/dx - v*(1-f)*n1) @x=0 +
integral_{x=0}^{x=L} dv/dx*n1*(1-f) dx = 0
You see that if v does not depend on x, your boundary conditions from above should conserve mass.
If v depends on x, the last volume integral must be taken into consideration in the definition of the boundary conditions. Since in "pdepe", you don't have access to the complete solution in "pdex4bc", this will become an ambitious task. Maybe "OutputFcn" can be of help here to compute the value of the integral in each time step.
Best wishes
Torsten.
Justin Torok
on 6 Jul 2018
Edited: Justin Torok
on 6 Jul 2018
I think I understand the issue. Could you explain a bit more what you mean by using "OutputFcn"? I'm reading through the documentation but it's not apparent to me how I would use this to better implement the boundary conditions so that mass is conserved at each time step without modifying pdepe itself. I'm also inferring that doing this will significantly increase the computation time.
If pdepe is ill-equipped to handle this particular problem, is there a different implementation in the PDE toolbox that would better suit my purposes, or is this going to be an issue independent of which solver I choose?
Torsten
on 9 Jul 2018
Could you explain a bit more what you mean by using "OutputFcn"?
After each successful time step, you could calculate the above integral in OutputFcn, put the value in a global variable and use it in pdex4bc in the next time step.
If pdepe is ill-equipped to handle this particular problem, is there a different implementation in the PDE toolbox that would better suit my purposes, or is this going to be an issue independent of which solver I choose?
All the PDE solvers I know of work pointwise as "pdepe" because they want to ensure that the resulting system of ODEs that is solved in the background has a banded Jacobian. So the issue will remain independent of the solver.
The only "clean" solution is to discretize your equations in space on your own and solve the resulting system of ordinary differential equations using ODE15s. This way, you would have full control over the complete solution in each time step, but it requires some experience on the discretization of partial differential equations.
Best wishes
Torsten.
Justin Torok
on 10 Jul 2018
Thanks for all your help. A quick follow-up: Regarding using 'OutputFcn', I would require the values of n1 and n2 as well as their partial derivatives d(n1)/dx and d(n2)dx at each time step to estimate the integral. The built-in function pdeval seems to handle this normally using pdepe solutions as an input, but I think it would require pdepe to have completed running - is there a way to dynamically access estimates for the derivatives of n1 and n2 so that I can modify the boundary conditions at each time step as you suggest?
Maybe you find how the authors discretized first-order derivatives from the article
Skeel, R. D. and M. Berzins, "A Method for the Spatial Discretization of Parabolic Equations in One Space Variable," SIAM Journal on Scientific and Statistical Computing, Vol. 11, 1990, pp.1–32.
For a first shot, I'd just use
du/dx @x=xi = (u(x(i+1))-u(x(i-1)))/(x(i+1)-x(i-1))
I don't know about the physics of your problem, but is it not quite arbirtrary to fix the boundary condition such that change of mass in the domain is equal to alpha*t as you plan to incorporate ?
Best wishes
Torsten.
Justin Torok
on 11 Jul 2018
Thanks for the reference - I was going to try some type of finite-difference as you suggest.
Regarding the last point, let me try to explain that aspect of the system a bit more clearly. In this system, the first species is generated at a constant rate (alpha), which in turn generates the second. The system is closed at both ends, so only way mass can enter the system is through the generation of n1 (note that of course total mass of the physical system upon which the model is based is not changing and n1 isn't appearing out of thin air, but the substrate for n1 - let's call it n0 - is much greater in concentration so as to be more or less constant). As it stands right now, it seems that mass is getting generated depending on other parameters because of the way the boundary conditions are being handled - that's the aspect that's pathological. The problem isn't that mass isn't being conserved, per se - because indeed mass isn't conserved in this system - but that it's getting added in unpredictable ways not endogenous to the problem I'd like to solve.
You say that the system is closed at both ends. Neverthess, n1 flows from the left to the right end. Shouldn't it be the consequence that n1 accumulates at the right end point ?
Justin Torok
on 12 Jul 2018
Not necessarily, because as the concentration of n2 builds up, v shifts from positive to negative and the advection direction flips. Certainly early in the simulation the tendency will be from left to right, but later on it should reverse. The left-to-right bias is something I generally observe very early in the simulations as you point out, although n1 tends to reach a more-or-less uniform (in x) steady state relatively quickly while n2 continues to accumulate with an x-distribution dependent on the parameters.
There is a barrier preventing either n1 and n2 from moving left of x=0 and right of x=L, so they shouldn't leave the system once they bump into either end. Moreover, they may accumulate at one end or the other depending on the sign and magnitude of v (as well as the magnitude of the other parameters, such as beta and gamma, that control the rate of interconversion between n1 and n2). The main feature is that the driving force acting on n1 is dependent on its concentration and the concentration of n2 (i.e. through a changing v) and so the distributions of n1 and n2 in x are dependent on the parameter values. However, n1 is only added to the system through alpha and the interconversion between n1 and n2 is balanced, so the total mass over the entire length should be proportional to alpha*t.
Torsten
on 13 Jul 2018
Your whole problem would be much easier if your first equation read
d(n1)/dt = d/dx (D1*f*d(n1)/dx - v*(1-f)*n1) + alpha + beta*n2 - gamma*n1*(n1+n2)
which - by the way - is the usual form of a convection-diffusion equation if velocity depends on the space variable x:
https://en.wikipedia.org/wiki/Convection%E2%80%93diffusion_equation
Are you sure this is not correct in your case ?
Best wishes
Torsten.
Justin Torok
on 13 Jul 2018
Edited: Justin Torok
on 13 Jul 2018
That actually may be right - I might have overlooked that when writing my equations down. In that case, would I instead put v in f1 and recode the boundary conditions as it is in your original answer? I think the stability issues I originally cited were still there when I tried that, but I can go back and try that again.
Torsten
on 13 Jul 2018
Yes, only the changes mentionned in the "Accepted Answer" had to be incorporated into the original code.
Justin Torok
on 10 Sep 2018
Torsten,
I had a quick followup question - much belated. I'm still working with this system using the encoding you suggested in the accepted answer, and the negative concentration issue I originally had isn't there. Unfortunately, the mass conservation issue I alluded to earlier persists - that is, for different values of the parameters delta and epsilon, the total sum of mass in the system changes at the end of the simulation. Is that a consequence of the fact that v depends on x, as you pointed out in a previous post, or was that argument based on the way I had originally encoded the equations? If it is because v depends on x, then I can try correcting for it using OutputFcn as you suggested, but if this system should be mass-conserving, do you have any idea what could be causing the discrepancy?
Thanks, Justin
Torsten
on 11 Sep 2018
If you use the version in the accepted answer, it should come out that
integral_{x=0}^{x=L} (n1(t=T)+n2(t=T)) dx =
integral_{x=0}^{x=L} (n1(t=0)+n2(t=0)) dx + alpha*L*T
That means that the mass in the system should increase by alpha*L*T from t=0 up to time t=T.
Best wishes
Torsten.
Justin Torok
on 11 Sep 2018
Right, that's what I had thought. I realized what I was comparing was wrong (sum(n1+n2) over the whole length) because I was using a nonuniform x-mesh. When I use a uniform x-mesh, the mass conservation you mentioned and I had originally expected holds. Thanks again for your help!
More Answers (0)
Categories
Find more on Eigenvalue Problems in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)