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/)
-   -   error: "namespace definition is not allowed here" (https://www.cfd-online.com/Forums/openfoam-programming-development/108469-error-namespace-definition-not-allowed-here.html)

meinenn October 24, 2012 10:55

error: "namespace definition is not allowed here"
 
Hej
I'm trying to adapt the twoPhaseEulerFoam solver in a way, that one can use the LaunderGibsonRSTM turbulence model instead of the kEpsilon model. I replaced the corresponding line in the main code (in myTwoPhaseEulerFoam.C) an defined all the necessary links in the make/options file. So far, everything is clear and ok.
Now, I'm trying to compile the new solver (with wmake) an I get always the same errors: for 6 H-files, the error "namespace definition is not allowed here" occurs. These are all unchanged files at the original place, so a bug in these files can be excluded, I assume.
Online, I found some similar problem descriptions, but without solutions. And I really have no idea in which direction I have to look for the solution...
Does somebody know this problem? I'd be really glad about an answer :-)
Thanks in advance!
Nik

PS: I'm working with OpenFoam 1.7.1

gwierink October 24, 2012 17:16

Hi Nik,

The reason is that you include a file with a namespace declaration where you shouldn't. Just test it by copying the original twoPhaseEulerFoam and putting
Code:

namespace Foam
{

at the top of kEpsilon.H and of course a closing curly bracket "}" at the bottom and try to compile. So, the problem is probably that you're including a class (e.g. LaunderGibsonRSTM.H) with a namespace declaration.

meinenn October 25, 2012 04:47

Thanks a lot for your answer. I also had the idea to test the original twoPhaseEulerFoam and I surprisingly remarked that there I get the same error message. Although, it is possible to run a case with my new solver, I just do not know if everything works fine or not, the results I get are generally quite reasonable, but a "rest-uncertainty" is still there. I think I have to discuss that tomorrow with my thesis-supervisor...
If I get more insight in that problem, I'll post that solution here...

KHoy July 6, 2015 11:36

namespace definition not allowed here
 
Hi everyone,

I, too, have this error when I am trying to compile meltFoam after changing the include file from readSIMPLEControls.H to simpleControl.H. In this thread it is suggested that the namespace is included where it shouldn't be.
However the simpleControl.H has the following lines:
...
#include "solutionControl.H"

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
Class simpleControl Declaration
\*---------------------------------------------------------------------------*/
...
and the solutionControl.H has
...
#include "fvMesh.H"

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
Class solutionControl Declaration
\*---------------------------------------------------------------------------*/
...

I don't know why this compiler error appears, since both the namespace declarations are "as is" in the lnInclude directory.
How is it that both namespace Foam declarations are included to the meltFoam.c code. Isn't this a conflict to start out with?

Thanks for helping me out here.:confused:

K

anuja August 4, 2015 07:01

Hi,
I am also getting this error of namespace definition..

mahaanu@HDHW3153 /opt/OpenFOAM/OpenFOAM-2.3.x/applications/solvers/trial
$ wmake
Making dependency files...
SOURCE=trial.C ; x86_64-w64-mingw32-g++ -m64 -Dcygwin64 -DWM_DP -DMSWIN -DLITTLE_ENDIAN -Wall -Wextra -Wno-un used-parameter -Wnon-virtual-dtor -O3 -DNDEBUG -DNoRepository -ftemplate-depth-100 -ItwoPhaseMixtureThermo -I /opt/OpenFOAM/OpenFOAM-2.3.x/src/transportModels/compressible/lnInclude -I/opt/OpenFOAM/OpenFOAM-2.3.x/src/the rmophysicalModels/basic/lnInclude -I/opt/OpenFOAM/OpenFOAM-2.3.x/src/transportModels/twoPhaseMixture/lnInclude -I/opt/OpenFOAM/OpenFOAM-2.3.x/src/transportModels/interfaceProperties/lnInclude -I/opt/OpenFOAM/OpenFOAM-2.3 .x/src/turbulenceModels/compressible/turbulenceModel -I/opt/OpenFOAM/OpenFOAM-2.3.x/src/finiteVolume/lnInclude -IlnInclude -I. -I/opt/OpenFOAM/OpenFOAM-2.3.x/src/OpenFOAM/lnInclude -I/opt/OpenFOAM/OpenFOAM-2.3.x/src/OSsp ecific/MSWindows/lnInclude -c $SOURCE -o Make/cygwin64mingw-w64DPOpt/trial.o
In file included from /opt/OpenFOAM/OpenFOAM-2.3.x/src/transportModels/compressible/lnInclude/PorousZones.H:41 :0,
from trial.C:56:
/opt/OpenFOAM/OpenFOAM-2.3.x/src/OpenFOAM/lnInclude/IOPtrList.H: In function ‘int main(int, char**)’:
/opt/OpenFOAM/OpenFOAM-2.3.x/src/OpenFOAM/lnInclude/IOPtrList.H:43:1: error: ‘namespace’ definition is not all owed here
namespace Foam
^
In file included from /opt/OpenFOAM/OpenFOAM-2.3.x/src/OpenFOAM/lnInclude/IOPtrList.H:104:0,
from /opt/OpenFOAM/OpenFOAM-2.3.x/src/transportModels/compressible/lnInclude/PorousZones.H:41 ,
from trial.C:56:
/opt/OpenFOAM/OpenFOAM-2.3.x/src/OpenFOAM/lnInclude/IOPtrList.C:30:1: error: a template declaration cannot app ear at block scope
template<class T>
^
/opt/OpenFOAM/OpenFOAM-2.3.x/src/OpenFOAM/lnInclude/IOPtrList.C:51:1: error: expected ‘;’ before ‘template’
template<class T>
^
trial.C:121:1: error: expected ‘}’ at end of input
}
^
trial.dep:578: die Regel für Ziel „Make/cygwin64mingw-w64DPOpt/trial.o“ scheiterte
make: *** [Make/cygwin64mingw-w64DPOpt/trial.o] Fehler 1


Could you please tell me what I can do?
Thanks in advance!

alexeym August 4, 2015 16:36

Hi,

There are .H files that are supposed to be included from within the solver main function (like createTime.H, createMesh.H, UEqn.H, pEqn.H etc) and there are .H files that are supposed to be included outside main function.

Unfortunately they have the same extension (for example in controversial Google C++ style guide extensions of header files that a part of class code and that are meant just for textual inclusion have different extensions).

So I guess, you have to provide more details about your code and what you are trying to achieve. Right now answer is "you are trying to include file inside your main function that is not supposed to be included there".

user_of_cfx December 3, 2015 06:29

update:I moved the inclusion of the header file outside of the main, just below # inlcude pimpleControl.H, and that seems to sort out this problem. Several more to go, but it's progress.

Hi, I am running against this issue as well.

Brief overview of what I'm trying to do:

- modify sonicLiquidFoam.C and createFields.H so I can run non Newtonian fluid simulation
- build compressible transport model library, by modifying the files structured as in this directory
- compile and run compressible non Newtonian simulations

So I have now build and successfully compiled my compressibleTransportModels library in the user directory, but I am getting the error in namespace definition when trying to compile the solver.

My new solver looks like this (I am highlighting the changes I have made from the original):

Code:

#include "fvCFD.H"
#include "pimpleControl.H"

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

int main(int argc, char *argv[])
{
    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"

    pimpleControl pimple(mesh);

    #include "readThermodynamicProperties.H"
    #include "compressibleSinglePhaseTransportModel.H" //"readTransportProperties.H"
    #include "createFields.H"
    #include "initContinuityErrs.H"

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

    Info<< "\nStarting time loop\n" << endl;

    while (runTime.loop())
    {
        Info<< "Time = " << runTime.timeName() << nl << endl;

        #include "readTimeControls.H"
        #include "compressibleCourantNo.H"

        fluid.correct();

        solve(fvm::ddt(rho) + fvc::div(phi));

        // --- Pressure-velocity PIMPLE corrector loop
        while (pimple.loop())
        {
            fvVectorMatrix UEqn
            (
                fvm::ddt(rho, U)
              + fvm::div(phi, U)
              - fvm::laplacian(fluid.mu(), U)
            );

            solve(UEqn == -fvc::grad(p));

            // --- Pressure corrector loop
            while (pimple.correct())
            {
                volScalarField rAU("rAU", 1.0/UEqn.A());
                surfaceScalarField rhorAUf
                (
                    "rhorAUf",
                    fvc::interpolate(rho*rAU)
                );

                U = rAU*UEqn.H();

                surfaceScalarField phid
                (
                    "phid",
                    psi
                  *(
                      (fvc::interpolate(U) & mesh.Sf())
                    + rhorAUf*fvc::ddtCorr(rho, U, phi)/fvc::interpolate(rho)
                    )
                );

                phi = (rhoO/psi)*phid;

                fvScalarMatrix pEqn
                (
                    fvm::ddt(psi, p)
                  + fvc::div(phi)
                  + fvm::div(phid, p)
                  - fvm::laplacian(rhorAUf, p)
                );

                pEqn.solve();

                phi += pEqn.flux();

                solve(fvm::ddt(rho) + fvc::div(phi));
                #include "compressibleContinuityErrs.H"

                U -= rAU*fvc::grad(p);
                U.correctBoundaryConditions();
            }
        }

        rho = rhoO + psi*p;

        runTime.write();

        Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
            << "  ClockTime = " << runTime.elapsedClockTime() << " s"
            << nl << endl;
    }

    Info<< "End\n" << endl;

    return 0;
}


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

So the main change is that I am including a new header for compressibleSinglePhaseTransportModel.H, whose job is to calculate the dynamic viscosity and return it to the solver, similarly to how nonNewtonianIcoFoam works.

When compiling this code, I receive an error that the namespace definition is not allowed in this syntax (inside the compressibleTransportModel.H):

Code:

#ifndef compressibleTransportModel_H
#define compressibleTransportModel_H

#include "primitiveFieldsFwd.H"
#include "volFieldsFwd.H"
#include "surfaceFieldsFwd.H"

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
                Class compressibleTransportModel Declaration
\*---------------------------------------------------------------------------*/

class compressibleTransportModel
{
    // Private Member Functions

        //- Disallow copy construct
        compressibleTransportModel(const compressibleTransportModel&);

        //- Disallow default bitwise assignment
        void operator=(const compressibleTransportModel&);


public:

    //- Runtime type information
    TypeName("compressibleTransportModel");


    // Constructors

        //- Construct from components
        compressibleTransportModel
        ();


    //- Destructor
    virtual ~compressibleTransportModel();


    // Member Functions

        //- Return the dynamic laminar viscosity
        virtual tmp<volScalarField> mu() const = 0;

        //- Return the dynamic laminar viscosity for patch
        virtual tmp<scalarField> mu(const label patchi) const = 0;

        //- Return the laminar viscosity
        // virtual tmp<volScalarField> nu() const = 0;

        //- Return the laminar viscosity for patch
        // virtual tmp<scalarField> nu(const label patchi) const = 0;
};


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

} // End namespace Foam

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

#endif

and a host of errors underneath that, but this is the first one I get.

I am confused because as mentioned by people above:
1. the part of the code that crashes is left as is i.e. I did not modify this file
2. the way the header is included in exactly the same as in the case of nonNewtonianIcoFoam, the only standard solver to include a transport model and which I used as my model for the modification.

Any help will be greatly appreciated.

Cheers,

Christa

alexeym December 3, 2015 08:58

Hi,

Let me repeat:

1. There are headers just for textual inclusion (like UEqn.H, pEqn.H, readPISOControls.H etc) that SHOULD be included inside main function. There is no class definitions inside, just a sequence of statements that for convenience are put in a separate file.

2. There are header with class definitions (like compressibleSinglePhaseTransportModel.H, incompressibleTurbulenceModel.H etc). Inside these files APIs are defined, they should be included outside main function (just in the beginning of the file).

So returning to your colourful code.

1. Red include statement should be outside main function.
2. Red fluid object should be created in createFields.H file (or elsewhere before while loop but inside main function).

user_of_cfx December 3, 2015 18:26

Quote:

Originally Posted by alexeym (Post 576067)
So returning to your colourful code.

1. Red include statement should be outside main function.
2. Red fluid object should be created in createFields.H file (or elsewhere before while loop but inside main function).

Thanks Alexey. I have fixed #1 and I had already created fluid in createFields.H, that was not a problem. Had to do some more syntax debugging (see: c++ novice) and now my new solver runs. Waiting to see if it does what I am asking it to, and if it does I will be very happy.

Sorry for making you repeat things. I read your message many times and only now do I actually understand what you're saying about the different header files. Coding is a bit of a struggle for me.

Many thanks again,

Christa


All times are GMT -4. The time now is 17:54.