File Exchange

image thumbnail

A Simple Finite Volume Solver for Matlab

version 2.0.0.0 (1.79 MB) by Ehsan
A simple yet general purpose FVM solver for transient convection diffusion PDE

103 Downloads

Updated 14 Apr 2018

GitHub view license on GitHub

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:
https://www.youtube.com/user/processsimulation/videos

Cite As

Ehsan (2019). A Simple Finite Volume Solver for Matlab (https://www.github.com/simulkade/FVTool), GitHub. Retrieved .

Comments and Ratings (47)

@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]);

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

Ehsan

@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

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

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:
Download the file, extract it, and run FVToolStartUP

How to use or install this ? Thannks.

MITHRIL

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

SAHi adel

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

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.

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

Du Wang

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.

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

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

Impressive and helpful

Sfalahati

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

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

Hi all,
Please ask your questions by opening an issue in the github repository (https://github.com/simulkade/FVTool) orby writing a comment in my blog (http://fvt.simulkade.com). I don't have access to this page anymore, since my previous email at TU Delft is not active.
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

Just want to say thx

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?

Hi Shijie,

I don't visit this page regularly. You can always download the latest version from github:
https://github.com/simulkade/FVTool

Ali

Shijie liu

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

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

Hi Everyone,

Please write your questions or comments preferably in the Github page of the code. My mathworks account is changed and I don't receive notifications anymore.
https://github.com/simulkade/FVTool

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

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!

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

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

Hongwei Guo

Guoxi HE

Peng Cao

Martin

Updates

2.0.0.0

showdemo function is not available. I will update it later.

1.4.0.0

added support for 2D radial (r, theta) and 3D cylindrical (r, thetta, z)

1.3.0.0

updated descriptions

1.2.0.0

update my weblog address

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

Boundary

Calculus

Classes/@BoundaryCondition

Classes/@CellVariable

Classes/@CellVector

Classes/@FaceVariable

Classes/@MeshStructure

Discretization

Examples/Advanced

Examples/External/PhaseChangeEnthalpyMethod

Examples/External/PhaseChangeEnthalpyMethod/Functions

Examples/External/SteadyLidDrivenCavityProblem

Examples/External/SteadyLidDrivenCavityProblem/Functions

Examples/External/SteadyLidDrivenCavityProblem/Testcases

Examples/Tutorial

FieldGeology

MeshGeneration

PhysicalProperties

Physics

Solvers

Tests

Utilities

Visualization