CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   How to modify discrete scheme (http://www.cfd-online.com/Forums/openfoam/79741-how-modify-discrete-scheme.html)

crammer008 September 2, 2010 05:21

How to modify discrete scheme
 
Dear all,

If I developed a new scheme of Laplacian, may I know how to add the c++ code into openFoam so that I can call it like "fvm::Laplacian()"?

Many thanks!

Crammer008

crammer008 September 2, 2010 22:33

Supplement of the above problem
 
Dear all,

I'd like to give a simple example to demonstrate my problem.
Assume the new scheme is quite simple.
Based on two nodes (i and j), the scheme is
dF/dx = ( F(i) - F(j) ) / ( x(i) - x(j) )

May I know how to append such a simple sheme to openFoam so that I can call this scheme by "fvm::Laplacian()"?

Many thanks and looking forward to your guidance:)

Crammer008

akidess September 3, 2010 03:50

I suggest finding the fvm::lapl operator, copying the files, and making the modifications according to your new scheme and changing the name to Laplacian. Not sure if that's all there's to it but that would be a way to start.

crammer008 September 7, 2010 23:02

How to fine the source file?
 
Dear Akidess,

Thanks a lot for your guidance:) Would you please tell me how to find the file related to the detailed description of fvm::lapl? It seems that there are a lot of files including fvm::lapl but all of them are not related to the algorithm level. May I know what kind of file is related to the bottom code?

Many thanks!
Crammer008

akidess September 8, 2010 03:03

I'm guessing it's this: http://foam.sourceforge.net/docs/cpp...cianScheme.php

crammer008 September 8, 2010 22:14

Dear Akidess,

Thanks a lot for your guidance:)
It seems that there are tons of materials. I will read them carefully. Hope to get your further guidance.

Best regards,
Crammer008

crammer008 October 22, 2010 03:52

Is is possible to see code of algorithm?
 
Dear OpenFoam expert,

As aforesaid, I am trying to find the bottom code related to algorithm. I read almost all the *.H and *.c files related to Laplacian or divScheme from OpenFoam source. However, I found the original point of the Laplacian or divergence operator is limited to member function. For example, disScheme is realized by two functions mesh_ and tinterpScheme_ (look at the code below). If you dig further, you'll never find how these two functions are realized.

May I know whether OpenFoam does not provide the bottom code related to the algorithm realization? For example, OpenFoam just tells you how to use a member function add(a,b) but it never tells you that the add is realized by a+b. Is it the current status of OpenFoam? If so, think OpenFoam is just a blackbox for me.

Looking forward to your guidance

Thanks a lot
Luo Chao

--------------------------------------
00108 //- Construct from mesh
00109 divScheme(const fvMesh& mesh)
00110 :
00111 mesh_(mesh),
00112 tinterpScheme_(new linear<Type>(mesh))
00113 {}
-----------------------------------------------------

crammer008 October 24, 2010 23:24

Dear OpenFoam expert,

Is there anybody who can help answer the above problem?

Many thanks
crammer008

marupio October 25, 2010 11:44

Quote:

Originally Posted by crammer008 (Post 280285)
Dear OpenFoam expert,

As aforesaid, I am trying to find the bottom code related to algorithm. I read almost all the *.H and *.c files related to Laplacian or divScheme from OpenFoam source. However, I found the original point of the Laplacian or divergence operator is limited to member function. For example, disScheme is realized by two functions mesh_ and tinterpScheme_ (look at the code below). If you dig further, you'll never find how these two functions are realized.

May I know whether OpenFoam does not provide the bottom code related to the algorithm realization? For example, OpenFoam just tells you how to use a member function add(a,b) but it never tells you that the add is realized by a+b. Is it the current status of OpenFoam? If so, think OpenFoam is just a blackbox for me.

Looking forward to your guidance

Thanks a lot
Luo Chao

--------------------------------------
00108 //- Construct from mesh
00109 divScheme(const fvMesh& mesh)
00110 :
00111 mesh_(mesh),
00112 tinterpScheme_(new linear<Type>(mesh))
00113 {}
-----------------------------------------------------

Hi &quot;crammer&quot;, the code you are quoting is from the generic &quot;divScheme&quot;, which only provides the interface for the actual divScheme. I think what may be causing you trouble is OpenFOAM's runTimeSelection.

See an article on it here: http://openfoamwiki.net/index.php/Op...tion_mechanism

When it comes to things like schemes, boundary conditions, solvers - anything that varies depending on what the user puts into the case dictionary files - you can't tell by looking at the code what is actually going to be constructed.

Luckily for you, there is only one divScheme and one LaplacianScheme... at least in my version. Look for the subdirectories under src/finiteVolume/finiteVolume/laplacianSchemes and divSchemes. laplacianSchemes has &quot;gaussLaplacianScheme&quot; and divSchemes has &quot;gaussDivScheme&quot;. The source for the algorithms you want are in there.

As for tinterpScheme_, this is also runTimeSelectable, but it seems to be hard-coded to &quot;linear&quot;. mesh_ is a reference to the mesh, which comes from the parameter mesh.
Sometimes you will find other functions that do not appear to be defined in the source code. For instance try to find the definition of exp(scalar)... exp is a standard C++ function, but there is no scalar in standard C++. Many of the primitive functions are defined in macros... so reading the full definition of something like this requires some macro reconstruction. For exp(scalar), it is defined in Scalar.H:

transFunc(exp)

And the definition of this macro is in doubleScalar.H (or floatScalar.H):
Code:

#define transFunc(func)            \ inline Scalar func(const Scalar s) \ {                                    \    return ::func(s);              \ }
The deeper you go into the source code, the foggier it gets. But really, you shouldn't ever need to go that deep.

To write your own custom laplacianScheme, here's a rough list of what you need to do:

1- Copy the gaussLaplacianScheme folder to a new name customLaplacianScheme
2- Rename all the files in the folder to &quot;customLaplacianScheme&quot;... one file needs to end in 's'. Delete the .dep file.
3- Open each file and do a text search and replace for &quot;gaussLaplacianScheme&quot; with &quot;customLaplacianScheme&quot;
4- In gaussLaplacianScheme.H you'll find a line that says
Code:

    TypeName(&quot;Gauss&quot;)
This is the dictionary name it looks for in fvSchemes. Change it to whatever you want - say &quot;custom&quot;.
5- Edit the custom scheme to whatever you want.
6- Edit the file src/finiteVolume/Make/files
7- Search for &quot;gaussLaplacianScheme&quot;
8- Add a new line for your custom scheme. Remember the &quot;s&quot; on the end.
9- Compile the finiteVolume library (go to src/finiteVolume, and type wmake libso)


Good luck!

crammer008 October 28, 2010 01:47

Is OpenFoam a completely open source?
 
Dear Marupio,

Thanks a lot for your great guidance:) Under your statement, I begin to realize that OpenFoam may not be a completely open source. I have the following statement about your reply. Hope to get your further guidance:)

Many thanks
Crammer008

Quote:

Originally Posted by marupio (Post 280677)
Hi &quot;crammer&quot;, the code you are quoting is from the generic &quot;divScheme&quot;, which only provides the interface for the actual divScheme. I think what may be causing you trouble is OpenFOAM's runTimeSelection.

See an article on it here: http://openfoamwiki.net/index.php/Op...tion_mechanism

When it comes to things like schemes, boundary conditions, solvers - anything that varies depending on what the user puts into the case dictionary files - you can't tell by looking at the code what is actually going to be constructed.
a). I guess it means that OpenFoam just publish the high-level code but hide the actual code of data operation.

The deeper you go into the source code, the foggier it gets. But really, you shouldn't ever need to go that deep.
b). As a common user who just wants to use OpenFoam as a tool, yes, there is no need to explore the source code so deep. However, as a researcher who wants to improve algorithm and compare it with the existing code, he hopes the data and relevant numerical operations are transparent to him. I think this is the original aim of open source.

To write your own custom laplacianScheme, here's a rough list of what you need to do:

1- Copy the gaussLaplacianScheme folder to a new name customLaplacianScheme
2- Rename all the files in the folder to &quot;customLaplacianScheme&quot;... one file needs to end in 's'. Delete the .dep file.
3- Open each file and do a text search and replace for &quot;gaussLaplacianScheme&quot; with &quot;customLaplacianScheme&quot;
4- In gaussLaplacianScheme.H you'll find a line that says
Code:

    TypeName(&quot;Gauss&quot;)
This is the dictionary name it looks for in fvSchemes. Change it to whatever you want - say &quot;custom&quot;.
5- Edit the custom scheme to whatever you want.
6- Edit the file src/finiteVolume/Make/files
7- Search for &quot;gaussLaplacianScheme&quot;
8- Add a new line for your custom scheme. Remember the &quot;s&quot; on the end.
9- Compile the finiteVolume library (go to src/finiteVolume, and type wmake libso)

c). Dear Marupio:
Thanks a lot for your guidance on how to modify the existed Scheme. This modification is just related to high-level code. In the file gaussLaplacianScheme.H, I can not find the source data to be manufactured. I can not even find any variable which is to be returned in the function. Since the input data, manufactured data and returned data are concealed, is there anything we can do to develop and improve the existing code?



Good luck!


marupio October 28, 2010 12:25

Quote:

Originally Posted by crammer008 (Post 281117)
a). I guess it means that OpenFoam just publish the high-level code but hide the actual code of data operation.

No, all of the source code is available. When I said you can't tell what will be loaded, that is because the specific derived class to be constructed depends on what you put in the files in your case directory. For instance, you can't tell what specific function fvm::ddt() will be calling until you look at your fvSchemes in system. If you specified:
Code:

ddtSchemes
{
    default    Euler;
}

Then fvm::ddt() will call EulerDdtScheme<Type>::fvmDdt(). specified:
Code:

ddtSchemes
{
    default    backward;
}

Then fvm::ddt() will call backwardDdtScheme<Type>::fvmDdt().

Quote:

Originally Posted by crammer008 (Post 281117)
b). As a common user who just wants to use OpenFoam as a tool, yes, there is no need to explore the source code so deep. However, as a researcher who wants to improve algorithm and compare it with the existing code, he hopes the data and relevant numerical operations are transparent to him. I think this is the original aim of open source.

The macros are a useful short hand. Without them the code would get quite bloated. If you must understand what they do, you can reassemble them. It's often unnecessary. It is fairly obvious that scalar += scalar exists, and what it does... no need to look for it.

Quote:

Originally Posted by crammer008 (Post 281117)
c). Dear Marupio:
Thanks a lot for your guidance on how to modify the existed Scheme. This modification is just related to high-level code. In the file gaussLaplacianScheme.H, I can not find the source data to be manufactured. I can not even find any variable which is to be returned in the function. Since the input data, manufactured data and returned data are concealed, is there anything we can do to develop and improve the existing code?

gaussLaplacian? Let's see... the code is located in gaussLaplacianScheme.C. fvmLaplacianUncorrected performs the Laplacian itself, the guts of which is:
Code:

    fvm.upper() = deltaCoeffs.internalField()*gammaMagSf.internalField();
    fvm.negSumDiag();

Remember, the fvm.lower() is not mentioned because it is symmetric, and therefore equal to fvm.upper(). The function gammaSnGradCorr is (I'm guessing) the non-orthogonality correction... and I'm guessing the remaining functions are various dispatches for these first two.

When dealing with implicit calculations, the schemes are all about filling in the coefficients of the discretization matrix. See this article here:

http://openfoamwiki.net/index.php/Op...es_in_OpenFOAM

Good luck!


All times are GMT -4. The time now is 11:12.