CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Best practices in CFD (code structure) (https://www.cfd-online.com/Forums/main/140438-best-practices-cfd-code-structure.html)

Joachim August 14, 2014 14:29

Best practices in CFD (code structure)
 
Hey everyone!

I would like to improve the organization of my CFD code (in Fortran 90), and I would gladly appreciate any suggestions/comments you might have!

To summarize, here is the current structure:

1. import grid / construct metrics, volumes, etc
2. initialize solution
----------------time loop---------------------
3. compute residuals
- compute roe fluxes (including turbulent terms)
- compute transport fluxes (both laminar and turbulent at same time)
- compute source terms if any
4. update solution (laminar and turbulent at same time)
5. impose new boundary conditions in ghost cells and update dependent variables
---------------------------------------------

I have constantly stored in memory the state variables, as well as dependent variables (pressure, temperature, speed of sound, viscosity, etc + turbulent quantities such as eddy viscosity, blending functions, etc). I read that in CFL3D, they only stored the primitive variables, but in FUN3D, they were basically storing everything. Which approach would you recommend?

Some people told me it might be better to separate laminar and turbulent subroutines, and put the latter in a big module. That would make things a bit more clear. However, if I consider the Roe fluxes computation for example, I will need uL and uR in for my turbulent fluxes too. It means that I either have to store them and waste memory or recompute them and waste CPU time...

I guess the new structure would look something like that:

1. import grid / construct metrics, volumes, etc
2. initialize solution
----------------time loop---------------------
3. compute NS residuals
4. add turbulent residuals
5. update solution (laminar and turbulent at same time, since my code is tighly coupled)
6. impose NS boundary conditions
7. impose turbulent boundary conditions
----------------------------------------------

I was also suggested to use structures for my grid, solution, etc. Any comment about that?

What is, in your opinion, the best way to structure a code? I know there is not a single right answer to that question, but I would gladly hear your suggestions!

Thank you very much!

Joachim

RameshK August 16, 2014 22:31

Hi

I have written my code for 2D and 3D compressible solvers and my code structure looks similar to yours, except that I apply boundary conditions first and then go about computing others.

Regarding storage of variables, different numerical schemes have different ways of computing, so it does not make sense to store all the values, hence the basic metrics of the grid (if it is stationary ) need to be stored and the primitive and conserved variables should be stored rest need to be calculated. This is how I do....

Regards

Joachim August 17, 2014 11:04

Thank you Rameshk!

Actually, my code is working just fine right now, but other people will probably use it after I graduate. I am afraid the current organization will make it difficult for someone to implement a new turbulence model for example. They will basically have to go through each and every subroutine and add some extra-stuffs here and there...

This is mainly why I was thinking about moving from a tightly coupled solver to a loosely coupled one. Then I would put all my kw-SST subroutines in one module, same for SA, and it would become relatively easy to add/modify turbulent stuffs in the solver.

However, it is kind of a pain sometimess...
The best example I have is the computation of the Roe fluxes. For the laminar parts, you need to compute the Roe matrices, which takes quite a while. For the turbulent part, I can reuse the same eigenvectors, eigenvalues, etc and just change a little something. Basically, I am not sure whether I should store all these terms (at all faces!) as global variables and reuse them, or just recompute them (waste of CPU time!!)

Your opinion about that??

Thank you!

Joachim

Joachim August 18, 2014 15:47

Actually, it seems that people just neglect the dissipation terms for the turbulent convective fluxes...that makes it simple. :D

However, what about suing structure in my code? I was suggested to put all the flow variables in a solution structure, and the grid parameters in a grid structure.
I tried that, but now my code is kind of messy, since I have grid%x(i,j) , or solu%u(i,j) everywhere...

Should I really use structures? What is the advantage of doing so?

Thank you so much!

Joachim


All times are GMT -4. The time now is 14:24.