I am new to Mathematica and I have a problem specifying Neumann boundary conditions in diffusion equation. The best result I managed to get is this.
n0 = 4*10^15; d = 0.005; diff = 17.647; d = 0.005;
Manipulate[
Module[{soln =
NDSolve[{ D[n[x, t], x, x] == 1/diff D[n[x, t], t] +
NeumannValue[1.25*10^18, x == -d] -
NeumannValue[-5.2*10^17, x == d],
DirichletCondition[n[x, t] == n0, t == 0]},
n, {x, -d, d}, {t, 0, 10^-5}]},
Plot[n[x, t0] /. soln, {x, -d, d},
PlotRange -> {{-d, d}, {0, 5 10^15}}]], {t0, 0, 10^-5,
Appearance -> "Labeled"}]
It is actually not so bad, but from the plot you can clearly see that Neumann conditions are not satisfied, as the divergence of n[x,t] at -d and d becomes time-dependent, though it should be constant. I have an analytical solution of this equation obtained by Fourier method and it works fine, but I need a numerical solution as well. My problem seems to originate from lack of understanding how NeumannValue works, but the way it is explained in manuals makes me even more confused. Dirichlet condition is far more straightforward, but with NeumannValue I don't really understand why and in what form do I need to sum it with my equation. I guess in my case I need to multiply NeumannValue on something (presumably, time-dependent and having a dimention of inverse length), but I can't figure it out on what exactly. I tried to search this problem on Stack Exchange, but it didn't help me understand what to do to get a result. I am sorry for my non-native English and probably a silly question.
Eited #1
As Alex Trounev noticed, the boundary and initial conditions are inconsistent, in my case as at t==0 the direction of current changes istanteneously, leaving aside how it can be physically (though experiments show that it is a good approximation for this problem). It is actually not important for analytical solution, but seems to have an impact on numerical computation. I tried to correct it artificially (no idea if it is a correct way) by introducing a piecewise-linear initial condition with a small parameter delta so that the derivatives at boundaries match the boundary conditions, and at the same time in the main region concentration is n0. It did not change a lot.
n0 = 4*10^15; d = 0.005; dif = 17.647; d = 0.005; delta = 10^-4;
Manipulate[
Module[{soln = NDSolve[{D[n[x, t], x, x] == 1/dif D[n[x, t], t] +
NeumannValue[1.25*10^18, x == -d] -
NeumannValue[-5.2*10^17, x == d],
DirichletCondition[
n[x, t] ==
1.25*10^18 (x + d - delta)*UnitStep[delta - x - d] + n0 -
5.2*10^17 (-d + x + delta)*UnitStep[-d + x + delta],
t == 0]}, n, {x, -d, d}, {t, 0, 10^-5}]},
Plot[n[x, t0] /. soln, {x, -d, d},
PlotRange -> {{-d, d}, {0, 5 10^15}}]], {t0, 0, 10^-5,
Appearance -> "Labeled"}, {delta, 2 10^-4, 0,
Appearance -> "Labeled"}]
Next I tried another variant using derivatives directly instead of NeumannValue, but using the same corrected initial condition. The result is much closer to what I expect, as the boundary conditions are now visually satisfied, but there are still several problems: first, the initial condition is somehow smoothened, so at t0==0 I have a smooth function instead of piecewise-linear, an I get a warning that "boundary and initial conditions are inconsistent" anyway. Second, the solution depends on delta quite strongly, which is certainly not what I need to have. That's how it looks like.
n0 = 4*10^15; d = 0.005; dif = 17.647; d = 0.005; delta = 10^-4;
Manipulate[
Module[{soln =
NDSolve[{dif Derivative[2, 0][n][x, t] ==
Derivative[0, 1][n][x, t],
Derivative[1, 0][n][-d, t] == 1.25*10^18,
Derivative[1, 0][n][d, t] == -5.2*10^17,
n[x, 0] ==
1.25*10^18 (x + d - delta)*UnitStep[delta - x - d] + n0 -
5.2*10^17 (-d + x + delta)*UnitStep[-d + x + delta]},
n, {x, -d, d}, {t, 0, 10^-5}]},
Plot[n[x, t0] /. soln, {x, -d, d},
PlotRange -> {{-d, d}, {0, 5 10^15}}]], {t0, 0, 10^-5,
Appearance -> "Labeled"}, {delta, 2 10^-4, 0,
Appearance -> "Labeled"}]