CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

OpenFoam programming

Register Blogs Community New Posts Updated Threads Search

Like Tree29Likes
  • 2 Post By prapanj
  • 2 Post By ngj
  • 25 Post By henrik

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 23, 2009, 00:54
Default OpenFoam programming
  #1
Senior Member
 
Prapanch Nair
Join Date: Mar 2009
Location: Bangalore, India
Posts: 105
Rep Power: 17
prapanj is on a distinguished road
Hi,

I am a OpenFoam user. I have some programming knowledge in fortran. I have done pretty well in scientific programming, like writing euler equation solver for 2D unstructured grid etc. But I don't have any experience in oops, c or c++. Since openFoam is a collaborative effort, I really want to participate in its development. But I am stuck. Like it claims of its object oriented approach, I don't find it that easy to program in openFoam. For example, I thought I could write a poisson eqn solver based on LaplacianFoam. But I feel it is difficult to understand why certain header files are called. I believe that I will need an understand of how the whole code works, inorder to add or modify. I went through the programmers Guide. But I did not find that explaining what I mentioned above. I don't understand the reason why tutorial cases are explained in programmers guide.
I want other programmers' advice on this. What sequence of actions should I perform so that I can get comfortable in contributing to openFoam? What would be like a first exercise, first thing to know etc. I am willing to write tutorials on what I discover too. I know this question may sound trivial. But that is because I am not in a job that demands me to learn openFoam programming. I am doing it on my own interest.

Thank you
Prapanj.
Luttappy and ordinary like this.
prapanj is offline   Reply With Quote

Old   June 24, 2009, 02:40
Default
  #2
Senior Member
 
Steve Hansel
Join Date: Jun 2009
Location: Colorado, USA
Posts: 112
Rep Power: 17
hansel is on a distinguished road
I'd think the first step would be to get a book on C++ and go through some of the basics like the syntax, classes, member variables, methods, etc.

I actually learned OO programming by learning basic Java. Java is a pure OO language and everything is OO. C++ is C with OO kind of tacked on. The general syntax with the {} for loops, whiles, dos, etc is the same in C, C++ and Java.

I guess for you I'd suggest going straight to C++ but if you want more OO background you can also take a look at Java.

Steve
hansel is offline   Reply With Quote

Old   June 25, 2009, 11:53
Default
  #3
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,903
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Hi

I have followed a course in C++, and I found that the book covered many of the questions one has when reading through the source code:

Andrew Koenig and Barbara E. Moo
Accelerated C++
Pratical Porgramming by Example

The only drawback is that they base many of there exercises on string manipulation, which has few things to do with OF, however I have never seen programming books which uses engineering examples as exercises/examples.

Good luck

Niels
alia and Luttappy like this.
ngj is offline   Reply With Quote

Old   July 11, 2009, 07:05
Default
  #4
Senior Member
 
Henrik Rusche
Join Date: Mar 2009
Location: Wernigerode, Sachsen-Anhalt, Germany
Posts: 281
Rep Power: 18
henrik is on a distinguished road
Hi Prapanj,

Quote:
But I don't have any experience in oops, c or c++.
Keep in mind that OpenFOAM is using generic programming as much as object orientated programming:

Quote:
I believe that I will need an understand of how the whole code works, inorder to add or modify.
Believe me - you don't! It would take a couple of years and your boss will fire you ;-)

Think of OpenFOAM C++ as a language to solve continuum mechanics problems. Understand it's language, then you can dig deeper.

So here is my reciepe:
  1. read the guides
  2. Run the tutorials described in the guide
  3. Read an introduction to C++ to C-programmers. For example, Chapter 11 "An overview of C++" and Chapter 18 "Templates" of "The complete C++ Reference" (H. Schildt)
  4. Spend a day writing some micky mouse examples from scratch
  5. Run other tutorials (that are not described in the guide) but are close to what you try to do
  6. Analyse the code of the closest tutorial line by line
  7. Modify the tutorials and associated codes step-by-step
  8. Search the forum when running into trouble
Iterate as required!

Some pointers where people have contributed modified codes which you may what to analyse:

http://openfoamwiki.net/index.php/Main_CodeSnipplets
http://openfoamwiki.net/index.php/Main_ContribSolvers
http://openfoamwiki.net/index.php/Main_ContribUtilities
http://openfoamwiki.net/index.php/Main_ContribExamples

http://www.tfd.chalmers.se/~hani/kurser/OS_CFD_2007/

Henrik
linnemann, Amir, tinhtt and 22 others like this.
henrik is offline   Reply With Quote

Old   July 11, 2009, 13:22
Default
  #5
Senior Member
 
Prapanch Nair
Join Date: Mar 2009
Location: Bangalore, India
Posts: 105
Rep Power: 17
prapanj is on a distinguished road
Hi Henrik,

That was really helpful. This reply of yours is going to make my life much easier. Thank you.

Prapanj.
prapanj is offline   Reply With Quote

Old   July 14, 2009, 13:36
Default
  #6
Senior Member
 
Sandy Lee
Join Date: Mar 2009
Posts: 213
Rep Power: 18
sandy is on a distinguished road
Hi Henrik,

I do bileve you that, after finish to read your documents a couple of years later, my boss will hire me!

Yes, I agree to you that we do need not to dig very deep knowledges about C++, if we could ever use Fortran smoothly. But, we really don't need to learn more major branches of OpenFOAM?

Giving your a example of mine. I don't know the reason that if I add the code sentences as follows

volScalarField Sp
(
IOobject
(
"Sp",
runTime.timeName(),
mesh
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
vDotvAlphal - vDotcAlphal
);


into gammaEqn.H of interPhaseChangeFoam solver, I could not get the output file about volScalarField Sp ; however, if above same code sentences of volScalarField Sp was inserted into createFields.H, it can be output smoothly, although maybe it is the old data of last iterative step?

So, as your viewpoints, in order to get OpenFOAM, which part knowledges I should further pay more efforts to study, C++ or major branches of OpenFOAM?

Our police cats!
sandy is offline   Reply With Quote

Old   July 14, 2009, 15:09
Default
  #7
Senior Member
 
Henrik Rusche
Join Date: Mar 2009
Location: Wernigerode, Sachsen-Anhalt, Germany
Posts: 281
Rep Power: 18
henrik is on a distinguished road
Sandy,

I remember that made this mistake, too ;-)

In C++, all variables that are instantiated within a pair of curly brackets "{}" are deleted with the "}" as explained in more detail in the reference given above (Chapter 11).

That's why there is no Sp when the fields are written with runTime.write(); when you instantiate the variable in gammaEqn.H.

Henrik
henrik is offline   Reply With Quote

Old   July 14, 2009, 19:56
Default
  #8
Senior Member
 
Sandy Lee
Join Date: Mar 2009
Posts: 213
Rep Power: 18
sandy is on a distinguished road
Yes, sir. this is a important C++ rule.

However, sometimes I am still blind. in which {} and when a variable can be added or removed with the runtime.write()? And how to transfer those closely connected varialbes between different subroutines? They actually show explicit or implicit branches of OpenFOAM in my eyes, which are really complicated and difficult to clearly learn but not only C++ rule.
sandy is offline   Reply With Quote

Old   September 24, 2009, 01:37
Default
  #9
Senior Member
 
Prapanch Nair
Join Date: Mar 2009
Location: Bangalore, India
Posts: 105
Rep Power: 17
prapanj is on a distinguished road
Thanks so far for the wonderful pointers. I have one more stupid question - When I begin to write a code, how do I know what header files to #include ? For example if I am trying to add a new turbulence model, and I am basing that on say std K epsilon. I then pick up the source of k epsilon and begin to modify it. Now suppose my turbulence model needs some regression values etc. So I will need some extra operations on the data. Then how will I find what header file to use? and what to remove ?
(The above was just a hypothetical example)
Thanks,
Prapanj
prapanj is offline   Reply With Quote

Old   February 5, 2010, 03:23
Default help
  #10
Member
 
mohsen kh
Join Date: Nov 2009
Posts: 41
Rep Power: 16
mohsenkh599 is an unknown quantity at this point
Hi Foamers
I am an amateur and I want to simulate viscoelastic fluids flow, But I am completely confused with OpenFOAM. And a lot of questions arise for me working with this software. Would you possibly answer some of my questions?
1.For implementing a new model (like LPPT) is it sufficient to write a new application in (OpenFOAM/applications) or it is necessary to modify OpenFOAM/src and/or lib and/or etc.
2. Could you possibly send me one or more new application file which you have made it by yourself (a viscoelastic model for instance). Or a detailed help about implementing new models in OpenFOAM?
I do need your help. Please help. That’s for my master thesis.
Best wishes
Mohsen – m.kh.599@gmail.com
mohsenkh599 is offline   Reply With Quote

Old   March 18, 2010, 08:23
Default
  #11
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21
tomislav_maric is on a distinguished road
Quote:
Originally Posted by mohsenkh599 View Post
Hi Foamers
1.For implementing a new model (like LPPT) is it sufficient to write a new application in (OpenFOAM/applications) or it is necessary to modify OpenFOAM/src and/or lib and/or etc.
I have no clue about viscoelastic solvers (other than they probably just change the forces on the r.h.s of the momentum equation and that you add models for the tangential stresses that depict viscoelastic behaviour of the continuum), but still, here's a few pointers:

If you just want to write some model you don't need an application, you need a library. The difference is in the fact that the library defines stuff that you need and doesn't execute anything. The application in this sense is a solver, a preprocessing or postprocessing app, anyway, something that has the

Code:
int main( blah )
inside. Can you please tell me what do you want, physically? Have you checked out the viscoelastic Foam solver? Inside the "viscoelasticFluidFoam.C" you will find:

Code:
#include "viscoelasticModel.H"
you should use "locate viscoelasticModel.H" and edit it with an editor. As you see, it's a class declaration. This means that the viscoelasticFluidFoam solver has it's models defined in a library that's compiled and used in binary form together with the solver. This is done so that you can add models and remove them with only having to compile as less code as possible over and over.

My final answer to this question would be: no, you may write your own library (the way depends on what you are doing), compile it to $FOAM_USER_LIBBIN and then just include it in your solver that's compiled to $FOAM_USER_APPBIN, there is no need to change the libOpenFOAM.so. When your work is finished, you will contribute it to the server, and then it will be where it should be, among the models somewhere in the source tree.

Quote:
Originally Posted by mohsenkh599 View Post
2. Could you possibly send me one or more new application file which you have made it by yourself (a viscoelastic model for instance). Or a detailed help about implementing new models in OpenFOAM?
I do need your help. Please help. That’s for my master thesis.
Best wishes
Mohsen – m.kh.599@gmail.com
First of all, good luck with the thesis. The library: you should be able to find tutorials, and notes that describe this online. Tell me what you want and what does this model do exactly, maybe I can give you some pointers.

A piece of advice: contact the guy who wrote this viscoelastic solver (also for his Master thesis work) maybe he will be willing to help you, at least with some general pointers.
tomislav_maric is offline   Reply With Quote

Reply

Tags
guide, learning, object oriented, programming


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming in OpenFOAM vinu OpenFOAM 2 July 11, 2009 11:16
OpenFOAM Training in Europe and USA hjasak OpenFOAM 0 August 8, 2008 06:33
Adventure of fisrst openfoam installation on Ubuntu 710 jussi OpenFOAM Installation 0 April 24, 2008 15:25
OpenFOAM Debian packaging current status problems and TODOs oseen OpenFOAM Installation 9 August 26, 2007 14:50
OpenFOAM Training and Workshop Zagreb 2628Jan2006 hjasak OpenFOAM 1 February 2, 2006 22:07


All times are GMT -4. The time now is 09:20.