CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

function within solver

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By Lieven

Reply
 
LinkBack Thread Tools Display Modes
Old   November 30, 2012, 10:42
Default function within solver
  #1
Senior Member
 
Join Date: Nov 2010
Posts: 113
Rep Power: 6
lindstroem is on a distinguished road
Hi Foamers,

I was wondering about a defintion of a little function within a solver.
If I, for example, want to calculate the square of x in a function within a solver it would look like this:
Code:
        scalar xsquare
        (
                const scalar& x
        ) const
        {
                return pow(x,2);
        }
But my problem is, that I don't know where to place it. Using it in a BC I would put it in the header, but I cannot put it in createFields.H of my solver (in my case interFoam).

Any advises?
Thanks in advance!
lindstroem is offline   Reply With Quote

Old   November 30, 2012, 10:54
Default
  #2
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 12
Bernhard is on a distinguished road
Did you try putting them in front of "int main()"? If I remember correctly, functions are always defined outside the main program. createFields.H is included in the main program.
Bernhard is offline   Reply With Quote

Old   November 30, 2012, 11:00
Default
  #3
Senior Member
 
Join Date: Nov 2010
Posts: 113
Rep Power: 6
lindstroem is on a distinguished road
Good hint, but then I need to set the namespace correctly, right?

Code:
interFoam.C:57:4: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
interFoam.C:57:4: error: non-member function 'const scalar xsquare(const scalar&)' cannot have cv-qualifier
You know how to handle that?
thanks!
lindstroem is offline   Reply With Quote

Old   November 30, 2012, 11:14
Default
  #4
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 12
Bernhard is on a distinguished road
The only other hint I can give you, is to put it below other includes like fvCFD.H, but probably you tried that.
Bernhard is offline   Reply With Quote

Old   November 30, 2012, 11:17
Default
  #5
Senior Member
 
Join Date: Nov 2010
Posts: 113
Rep Power: 6
lindstroem is on a distinguished road
Yes, what I did was
Code:
#include "fvCFD.H"
#include "MULES.H"
#include "subCycle.H"
#include "interfaceProperties.H"
#include "twoPhaseMixture.H"
#include "turbulenceModel.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

scalar xsquare
    (
        const scalar& x
    ) const
  {
      return pow(x,2);
     }

int main(int argc, char *argv[])
{
    #include "setRootCase.H"
    ...
But thanks anyway, I'll keep on digging for the namespace stuff, I am pretty sure thats the reason!

Greetings
lindstroem is offline   Reply With Quote

Old   November 30, 2012, 11:21
Default
  #6
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 12
Bernhard is on a distinguished road
In that case you can try Foam::scalar instead of just scalar, but I am absolutely not sure about that. Good luck!
Bernhard is offline   Reply With Quote

Old   November 30, 2012, 11:23
Default
  #7
Senior Member
 
Join Date: Nov 2010
Posts: 113
Rep Power: 6
lindstroem is on a distinguished road
Yeah, tried several of these combinations, but unsuccessfully. I got to understand that, otherwise it's just trial and error..
lindstroem is offline   Reply With Quote

Old   November 30, 2012, 12:01
Default
  #8
Senior Member
 
Lieven
Join Date: Dec 2011
Location: Mol, Belgium
Posts: 295
Rep Power: 13
Lieven will become famous soon enough
Hey all,

This is how I would do it. Add
Code:
scalar xsquare(const scalar& x);
right underneath the #include statements and before int main (...

This way you can simply add your function at the bottom of the file so after the } of int main. But you should remove the 'const' after the (const scalar& x). So the function becomes:

Code:
scalar xsquare(const scalar& x)
 {
     return pow(x,2);
 }
I'm not a C++ expert, but this will make it work...
lindstroem likes this.
Lieven is offline   Reply With Quote

Old   November 30, 2012, 12:20
Default
  #9
Senior Member
 
Join Date: Nov 2010
Posts: 113
Rep Power: 6
lindstroem is on a distinguished road
Thank you! Just works

Greetings
lindstroem is offline   Reply With Quote

Reply

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
LiencubiclowRemodel nzy102 OpenFOAM Bugs 14 January 10, 2012 09:53
Problems in compiling paraview in Suse 10.3 platform chiven OpenFOAM Installation 3 December 1, 2009 08:21
Installation 1.5 as a user of server (New bird) chiven OpenFOAM Installation 15 April 26, 2009 22:33
Control function in Elliptic solver Hoang Anh Duong Main CFD Forum 4 July 9, 2007 03:32
DecomposePar links against liblamso0 with OpenMPI jens_klostermann OpenFOAM Bugs 11 June 28, 2007 17:51


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