1+ % Nonlinear diffusion equation: a tutorial
2+ % see http://fvt.simulkade.com/posts/2015-04-06-solving-nonlinear-pdes-with-fvm.html
3+ L= 1.0 ; % domain length
4+ Nx= 100 ; % number of cells
5+ m= createMesh1D(Nx , L ); % create a 1D mesh
6+ D0= 1.0 ; % diffusion coefficient constant
7+ % Define the diffusion coefficientand its derivative
8+ D= @(phi )(D0 *(1.0 + phi .^ 2 ));
9+ dD= @(phi )(2.0 * D0 * phi );
10+ % create boundary condition
11+ BC = createBC(m );
12+ BC .left .a(: )=0.0 ;
13+ BC .left .b(: )=1.0 ;
14+ BC .left .c(: )=5.0 ;
15+ BC .right .a(: )=0.0 ;
16+ BC .right .b(: )=1.0 ;
17+ BC .right .c(: )=0.0 ;
18+ [Mbc , RHSbc ]= boundaryCondition(m ,BC );
19+ % define the initial condition
20+ phi0= 0.0 ; % initial value
21+ phi.Old= createCellVariable(m , phi0 , BC ); % create initial cells
22+ alfa= createCellVariable(m , 1.0 );
23+ phi.value= phi .Old ;
24+ % define the time steps
25+ dt= 0.001 * L * L / D0 ; % a proper time step for diffusion process
26+ for i= 1 : 10
27+ err= 100 ;
28+ [Mt , RHSt ] = transientTerm(m ,alfa ,dt , phi );
29+ while err > 1e- 10
30+ % calculate the diffusion coefficient
31+ Dface= harmonicMean(m ,D(phi .value ));
32+ % calculate the face value of phi_0
33+ phi_face= arithmeticMean(m ,phi .value );
34+ % calculate the velocity for convection term
35+ u= funceval(dD , phi_face ).*gradientTerm(m ,phi .value );
36+ % diffusion term
37+ Mdif= diffusionTerm(m ,Dface );
38+ % convection term
39+ Mconv= convectionTerm(m ,u );
40+ % divergence term on the RHS
41+ RHSlin= divergenceTerm(m ,u .* phi_face );
42+ % matrix of coefficients
43+ M= Mt - Mdif - Mconv + Mbc ;
44+ % RHS vector
45+ RHS= RHSbc + RHSt - RHSlin ;
46+ % call the linear solver
47+ phi_new= solvePDE(m , M , RHS );
48+ % calculate the error
49+ err= max(abs(phi_new(: )-phi .value(: )));
50+ % assign the new phi to the old phi
51+ phi.value= phi_new ;
52+ end
53+ phi.Old= phi .value ;
54+ visualizeCells(m ,phi .value ); drawnow ;
55+ end
0 commit comments