CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   function within solver (https://www.cfd-online.com/Forums/openfoam-programming-development/109935-function-within-solver.html)

lindstroem November 30, 2012 09:42

function within solver
 
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!

Bernhard November 30, 2012 09:54

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.

lindstroem November 30, 2012 10:00

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!

Bernhard November 30, 2012 10:14

The only other hint I can give you, is to put it below other includes like fvCFD.H, but probably you tried that.

lindstroem November 30, 2012 10:17

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

Bernhard November 30, 2012 10:21

In that case you can try Foam::scalar instead of just scalar, but I am absolutely not sure about that. Good luck!

lindstroem November 30, 2012 10:23

Yeah, tried several of these combinations, but unsuccessfully. I got to understand that, otherwise it's just trial and error..

Lieven November 30, 2012 11:01

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 November 30, 2012 11:20

Thank you! Just works :)

Greetings


All times are GMT -4. The time now is 18:30.