File Exchange

## A Simple Finite Volume Solver for Matlab

version 2.1.0.0 (2.05 MB) by Ehsan

### Ehsan (view profile)

A simple yet general purpose FVM solver for transient convection diffusion PDE

Updated 17 Jun 2020

A simple Finite volume tool
This code is the result of the efforts of a chemical/petroleum engineer to develop a simple tool to solve the general form of convection-diffusion equation:
α∂ϕ/∂t+∇.(uϕ)+∇.(−D∇ϕ)+βϕ=γ
on simple uniform/nonuniform mesh over 1D, 1D axisymmetric (radial), 2D, 2D axisymmetric (cylindrical), and 3D domains.
The code accepts Dirichlet, Neumann, and Robin boundary conditions (which can be achieved by changing a, b, and c in the following equation) on a whole or part of a boundary:
a∇ϕ.n+bϕ=c.
It also accepts periodic boundary conditions.
The main purpose of this code is to serve as a handy tool for those who try to play with mathematical models, solve the model numerically in 1D, compare it to analytical solutions, and extend their numerical code to 2D and 3D with the minimum number of modifications in the 1D code.
The discretizaion schemes include
* central difference
* upwind scheme for convective terms
* TVD schemes for convective terms with many flux limiters
To get started, go to the Test folder and run the test scripts.
A few calculus functions (divergence, gradient, etc) and averaging techniques (arithmetic average, harmonic average, etc) are available, which can be helpful specially for solving nonlinear or coupled equations or implementing explicit schemes.
I have used the code to solve coupled nonlinear systems of PDE. You can find some of them in the Examples/advanced folder.

There are a few functions in the 'PhysicalProperties' folder for the calculation of the physical properties of fluids. Some of them are not mine, which is specified inside the file.

I'll try to update the documents regularly, in the github repository. Please give me your feedback/questions by writing a comment in my weblog: <http://fvt.simulkade.com/>
Special thanks: I vastly benefited from the ideas behind Fipy <http://www.ctcms.nist.gov/fipy/>, a python-based finite volume solver.

To start the solver, download and extract the zip archive, open and run 'FVToolStartUp' function.
To see the code in action, copy and paste the following in your Matlab command window:

clc; clear;
L = 50; % domain length
Nx = 20; % number of cells
m = createMesh3D(Nx,Nx,Nx, L,L,L);
BC = createBC(m); % all Neumann boundary condition structure
BC.left.a(:) = 0; BC.left.b(:)=1; BC.left.c(:)=1; % Dirichlet for the left boundary
BC.right.a(:) = 0; BC.right.b(:)=1; BC.right.c(:)=0; % Dirichlet for the right boundary
D_val = 1; % value of the diffusion coefficient
D = createCellVariable(m, D_val); % assign the diffusion coefficient to the cells
D_face = harmonicMean(D); % calculate harmonic average of the diffusion coef on the cell faces
Mdiff = diffusionTerm(D_face); % matrix of coefficients for the diffusion term
[Mbc, RHSbc] = boundaryCondition(BC); % matix of coefficients and RHS vector for the BC
M = Mdiff + Mbc; % matrix of cefficients for the PDE
c = solvePDE(m,M, RHSbc); % send M and RHS to the solver
visualizeCells(c); % visualize the results

You can find some animated results of this code in my youtube channel:

### Cite As

Eftekhari, A.A. et al. (2015). FVTool: a finite volume toolbox for Matlab. Zenodo. http://doi.org/10.5281/zenodo.32745

Ehsan

### Ehsan (view profile)

@Ali, unfortunately, FVTool does not support triangular mesh.

Ali Hammouche

### Ali Hammouche (view profile)

@Ehsan Hello I want to solve a Poisson equation by FVM (-div(e*grad(A))=curl(M).M has tow components (Mx My).
By using triangulare mesh i dont khow how to dicretize term source by Fvm with tri mesh ?? I used (pdemesh,)

@Ehsan
Many thanks for sharing this! I believe this code has a lot of potential specially for solving reacting flow like chemical reactors.

Ehsan

### Ehsan (view profile)

I have solved several problems with nonlinear source term for my work (reactive transport in porous media) that I have not shared yet. You can find one example here: https://github.com/behzaadh/FVTool/tree/master/Examples/External/InjectionHClCoreFloodProblem
This is a relatively advanced case although I don't think the source term is linearized. I'll add a simple example later.

Is there any example to demonstrate such solution?

Sometimes this is not quite easy! The reason I asked for f(phi) instead of phi to let solver can solve nonlinear source term.
for example in chemical reaction, most of the time you have very nonlinear terms.

Ehsan

### Ehsan (view profile)

@Mohammad Rahmani: you can do that easily by writing the Taylor expansion of your f(\phi) function :-)

Good job. if instead of \beta*\phi you can use f(\phi) then your code can be used for reaction-diffusion-convection cases!

Keep going on!
Good luck

mali Mir

Tamour Zubair

### Tamour Zubair (view profile)

@All
anyone please help me i am quite new in matlab. In very start, i am facing the following error in 1D meshing. Thanks in advance
Error:
Undefined function or variable 'MeshStructure'.

Error in createMesh1D (line 42)
MS=MeshStructure(1, [Nx,1], CellSize, CellLocation,
FaceLocation, [1], [1]);

Fernando Fernandes

### Fernando Fernandes (view profile)

Hello! I'm really new in the simulation world. Your scripts are fantastic! I need to simulate a salt rock flow towards a petroleum well, How could I use your scripts for this?? I don't know to open the files, I think I need open the file "meshgeneration" together to the main file. Could u send a step by step???

Thank U

Amitava Biswas

Ehsan

### Ehsan (view profile)

@Vahid,

Currently, internal boundaries are not possible. The Jacobian matrix is made manually in FVTool. It is not too hard to make them. I have one example here for a nonlinear diffusion coefficient: http://fvt.simulkade.com/posts/2015-04-06-solving-nonlinear-pdes-with-fvm.html

If you open issues on github page, I see them and react faster: https://github.com/simulkade/FVTool/issues

vahid moss

### vahid moss (view profile)

Hi. Is there any chance to apply your code for a geometry with internal boundaries. For instance, assume a large scale rectangular geometry and take a small rectangular out. See also, https://www.mathworks.com/help/matlab/ref/polyshape.regions.html . Another example can be found in : Fig. 4.3 (the page 89) of the book " The Finite Volume Method in Computational Fluid Dynamics An Advanced Introduction with OpenFOAM and Matlab".

vahid moss

### vahid moss (view profile)

Hi, Alireza, I am new with FVM but I have found you code wonderful. Here is my first question. I assume, a set of algebraic equation in FVM should be solved similar to FDM. In FDM, a nonlinear source term, e.g. nonlinear source in Poisson's equation, will produce nonlinear algebraic equations which must be solved, e.g. by Newton method and building a Jacobian matrix. Does your code include a function or class where the jacobian matrix being built? If yes, where it is located in the package?

Ehsan

@Kannapiran:

Kannapiran Arasu

### Kannapiran Arasu (view profile)

How to use or install this ? Thannks.

MITHRIL

### MITHRIL (view profile)

Hi,
Could you tell me how to define a non-uniform velocity field (say a Couette flow) in a 2D model, please?
Thank you.

Ann Ann

marzieh

Avaneesh Narla

### Avaneesh Narla (view profile)

I tried running the diffusiontutorial_spherical code, but I commented out line 20, and uncommented line 19 (switching BCs from left to right), and the computation no longer performs as expected. As I am just making a symmetric change in the BCs, is there a reason it doesn't work?

Thanks!

Ehsan

### Ehsan (view profile)

I mostly used "An Introduction to Computational Fluid Dynamics: The Finite Volume Method Approach" by Versteeg and Malalasekera, except for the boundary conditions that are my own design.

dinesh aravinth

### dinesh aravinth (view profile)

Hi sir,
Can you specify which book you followed for writing this code?

Du Wang

catalina medina

### catalina medina (view profile)

Hi!

I'm trying to use the FVM toolbox you created for MATLAB to calculate the temperature of a fluid being injected in an oil well that is closed on the bottom. The fluid goes down trough the space between the production tube and the casing of the well and then it goes up through the production tube. I need to calculate the heat transfer between the rocks and the fluid going down and also between the fluid going downwards and the fluid going upwards. There's a paper that explains the problem very well and it also has the needed equations. It's called "geothermal energy production utilizing abandoned oil and gas wells" by Xianbiao Bu. Do you think you'r code could do the job? I'm having trouble using it for this purpose. Any help will be very welcome!

Best regards,
Catalina.

Ali Akbar Eftekhari

### Ali Akbar Eftekhari (view profile)

Hi Erdum,

The nonuniform mesh is created using the coordinates of the faces of the cells. You should use the cell centers coordinates:

x=[0 0.12 0.13 0.2 0.4 0.41 0.56 0.8 1]; %non-uniform grid
m = createMesh1D(x);
BC = createBC(m); % all Neumann boundary condition structure
BC.left.a(:) = 0; BC.left.b(:)=1; BC.left.c(:)=1; % Dirichlet for the left boundary
BC.right.a(:) = 0; BC.right.b(:)=1; BC.right.c(:)=0; % right boundary
X = m.cellcenters.x;
D_val = sin(X)+2; % value of the diffusion coefficient
D = createCellVariable(m, D_val); % assign the diffusion coefficient to the cells
D_face = harmonicMean(D); % calculate harmonic average of the diffusion coef on the cell faces
Mdiff = diffusionTerm(D_face); % matrix of coefficients for the diffusion term
[Mbc, RHSbc] = boundaryCondition(BC); % matix of coefficients and RHS vector for the BC
M = Mdiff + Mbc; % matrix of cefficients for the PDE
c = solvePDE(m,M, RHSbc); % send M and RHS to the solver
visualizeCells(c); % visualize the results


Erdem Uguz

### Erdem Uguz (view profile)

hello,
I am trying to use the toolbox for non-uniform grid with nonconstant diffusion coefficient. I can do the non-uniform grid (i.e. x).
I replaced D_val with sin(x)+2 and I get D values are 0 therefore result as NAN. How can I do this? I will extend this to 2D and 3D of course.
x=[0 0.12 0.13 0.2 0.4 0.41 0.56 0.8 1]; %non-uniform grid
>> m = createMesh1D(x);
>> BC = createBC(m); % all Neumann boundary condition structure
BC.left.a(:) = 0; BC.left.b(:)=1; BC.left.c(:)=1; % Dirichlet for the left boundary
BC.right.a(:) = 0; BC.right.b(:)=1; BC.right.c(:)=0; % right boundary
D_val = sin(x)+2; % value of the diffusion coefficient
D = createCellVariable(m, D_val); % assign the diffusion coefficient to the cells
D_face = harmonicMean(D); % calculate harmonic average of the diffusion coef on the cell faces
Mdiff = diffusionTerm(D_face); % matrix of coefficients for the diffusion term
[Mbc, RHSbc] = boundaryCondition(BC); % matix of coefficients and RHS vector for the BC
M = Mdiff + Mbc; % matrix of cefficients for the PDE
c = solvePDE(m,M, RHSbc); % send M and RHS to the solver
visualizeCells(c); % visualize the results

Md Shujan Ali

Sfalahati

### Sfalahati (view profile)

Hi guys,
I need to write a code for CFD to solve the difference heat equation and conduct 6 cases simulations.
Equation: (

Thanh Hung Vo

### Thanh Hung Vo (view profile)

Dear Sir. Could Sir help me explain how can i use these code for enhanced oil recovery. Thank you so much

Yongjia Zhang

### Yongjia Zhang (view profile)

Marco dos Santos Bernardes

### Marco dos Santos Bernardes (view profile)

Ali Akbar Eftekhari

### Ali Akbar Eftekhari (view profile)

Hi all,
I'm adding a few examples to the github page to answer your questions regarding the Poisson equation and the source terms.
In short, you can have a derivative boundary condition by changing the BC lines in the above code to:
BC.left.a(:) = 1; BC.left.b(:)=0; BC.left.c(:)=1; % Neumann for the left boundary
BC.right.a(:) = 1; BC.right.b(:)=0; BC.right.c(:)=0; % Neumann for the right boundary

A source term can be added by calling the function constantSourceTerm(q) where q is a cell variable. Don't forget to divide the source term by the cell volume.

Yue SUN

### Yue SUN (view profile)

Just want to say thx

Chu-Yao Chou

Rodward Hewlin

### Rodward Hewlin (view profile)

Hi I would like to know how to add a source term to the diffusion equation using your solver.

hi, how I can solve 3d poison equation with derivative boundary conditions in pdetool in rectangular channel domain?

Ali Akbar Eftekhari

Hi Shijie,

Ali

Shijie liu

### Shijie liu (view profile)

Hi Ali,
I don't know why I can't download the attached .zip file. And I have tried many times. Could you send the files to me? My email is shi.jieliu@163.com. Thanks a lot!

Saif Manji

Yuri Feldman

### Yuri Feldman (view profile)

Hi Ali,
Can you please explain the following corrections in your upwind scheme for convection term:
% Also correct for the boundary cells (not the ghost cells)
% Left boundary:
APx(1,:) = APx(1,:)-uw_max(1,:)/(2*DXp(1)); AW(1,:) = AW(1,:)/2;
% Right boundary:
AE(end,:) = AE(end,:)/2; APx(end,:) = APx(end,:) + ue_min(end,:)/(2*DXp(end));
% Bottom boundary:
APy(:,1) = APy(:,1)-vs_max(:,1)/(2*DYp(1)); AS(:,1) = AS(:,1)/2;
% Top boundary:
AN(:,end) = AN(:,end)/2; APy(:,end) = APy(:,end) + vn_min(:,end)/(2*DYp(end));

I will also appreciate if you could refer me to the relevant literature.
Thank you,
Yuri

Ali Akbar Eftekhari

### Ali Akbar Eftekhari (view profile)

Hi Everyone,

Ali Akbar Eftekhari

### Ali Akbar Eftekhari (view profile)

Sorry Alireza for this late reply. I have a new mathworks account so I don't receive notifications anymore. Yes, you can define a heterogeneous field. For instance, in the example above, replace the mesh creation and D definition lines with:
m=createMesh2D(Nx, Nx, L, L);
D=createCellVariable(m, rand(Nx, Nx));
You can replace the rand(Nx, Nx) with any matrix of Nx x Nx size.

Antonio

super

AG

### AG (view profile)

Thanks for the magnificent work. Does your code support heterogeneous material properties as well? I am trying to solve a 2D transient heat equation on a domain that has different conductivities and heat capacities and I was hoping your framework could be of help.

Many thanks!

Ali Akbar Eftekhari

### Ali Akbar Eftekhari (view profile)

Hi Hongwei, I'm glad you find the code useful. Please let me know if you like to add your application to the example folder. You are always welcome to send a pull request on github.

Hongwei Guo

### Hongwei Guo (view profile)

Thanks a lot !!! Very professional and general code !!! I will try to apply this in electron transport problems !!!

Hongwei Guo

Guoxi HE

William Schill

Peng Cao

Nikhil Kumar

Martin

### Martin (view profile)

 28 Apr 2020 2.1.0.0 Citation information and some clean up 10 Aug 2015 2.0.0.0 showdemo function is not available. I will update it later. 24 Dec 2014 1.4.0.0 added support for 2D radial (r, theta) and 3D cylindrical (r, thetta, z) 20 May 2014 1.3.0.0 updated descriptions 18 May 2014 1.2.0.0 update my weblog address 16 May 2014 1.1.0.0 add youtube channel link to descriptions
##### MATLAB Release Compatibility
Created with R2014a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux
##### Acknowledgements

Inspired by: IAPWS_IF97