CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   how to call 'p' as pressure into my code from OpenFOAM? (

atmcfd February 16, 2013 08:45

how to call 'p' as pressure into my code from OpenFOAM?
Hello all,

I am written a new wall function in OpenFOAM and I am trying to compile it. I am getting the error saying


error: p was not declared in this scope
where 'p' is supposed to be the pressure. I want to call the variable for "pressure" into my code, but I do not know how I do it.

I saw the channelFoam and pisoFoam codes where the variable 'p' was used as pressure in the code. I want to do just the same in my code.

Any help will be appreciated. Thank you!


ngj February 16, 2013 09:05


Inside the wall functions you do not have direct access to the pressure, since it is not part of the construction. You do, however, have access to the database structure, so you can obtain a constant reference to the pressure in the following way:


const volScalarField & p = this->db().lookupObject<volScalarField>("p");
Kind regards,


atmcfd February 18, 2013 01:42


Thank you very much for your reply - It works now. I have just one more question. Forgive me if it is very basic; its just that OpenFOAM's level of C++ takes some time to get used to, and I am fairly new to this game;)

I have included 'p' into my code as you said, but I tried taking a gradient of 'p' and it gave me compile errors. I tried
grad(p) and also Foam::fvc::grad(p)
Neither of which work. All I want to do is \frac{\partial p}{\partial x}
Is there an elegant way to do this? I want only the streamwise (x) pressure gradient, instead of the gradient in all directions.

Finally, omitting the gradient term, my code compiles saying

'' is up to date.

But when I run a test case with my new wall function included in the 0/nuSgs file, it aborts


Unknown patchField type NewWallFunction for patch type wall

Valid patchField types are :


I have followed the nuSgsUSpaldingWallFunction file and made sure my file naming conventions are consistent, just like this one.

Can you please give me some ideas as to what I am doing wrong here? How do I integrate my New wall function into OpenFOAM like the existing ones??

Thanks for your time and help.



ngj February 18, 2013 05:18


Good. I will try to answer your questions below:

1. Please provide me/us with some compilation errors, as the error could be due to a multiple number of things.

2. It does not seem as you have written the correct lines in your Make/files script, since it tells you that it outputs the library to a default name, namely Please post the Make/files file here.

3. Your simulation does not recognise the new library, because the solver has not been linked to it during compilation. You can do a runTime-linking adding the following line to the controlDict:


libs ( "<myLibraryName>" );
where you in this specific case should write inside the "".

Kind regards,


atmcfd February 18, 2013 05:45

Hi Niels,

This is the code I inserted (NewWallFunctionFvPatchScalarField.C:138:48 in the error message)


    const scalarField GradP(Foam::fvc::grad(pr));
where 'pr' is the pressure, and I want to store an array GradP having the
\frac{\partial p}{\partial x} of the pressure in the whole field.

Here is the error msg I get - Its a dropbox link to a text file.

And this is my Make/file



EXE = $(FOAM_USER_LIBBIN)/NewWallFunction

Please let me know if this information is sufficient for you. Else I can show you the actual code and the Make/options if you need it to find the cause.
Basically, I just used the nuSgsUSpaldingWallFunction code as a template, and edited it to the new function, so as to be consistent.

Thank you very much for your help again.


ngj February 18, 2013 05:54


With respect to the pressure, then the gradient of a volScalarField is a vectorField, so the following should compile:


volVectorField gradP = Foam::fvc::grad( pr )();
Then you will need to extract the x-component of the gradient field subsequently.

Secondly, as you are compiling a library and not a solver/utility, the line in your Make/files should read the following:


LIB = $(FOAM_USER_LIBBIN)/libNewWallFunction

atmcfd February 18, 2013 06:20


Originally Posted by ngj (Post 408410)

Secondly, as you are compiling a library and not a solver/utility, the line in your Make/files should read the following:


LIB = $(FOAM_USER_LIBBIN)/libNewWallFunction

Oh! That was a really silly mistake.:eek: Should have noticed that :mad:

Meanwhile, I have been trying my code with the fix you suggested for grad(p). I am having some additional problems, but I will try my best and figure it out before I post it here for you.

Thank you very much for your help so far !!

ngj February 18, 2013 06:28

OK, good luck - I will keep my eyes on the thread :)

/ Niels

ndr January 13, 2014 12:04


did you manage to get the streamwise pressure gradient to work properly?
I'm dealing with a similar problem at the moment, and I tried the solution as destribed above with


volVectorField gradient = Foam::fvc::grad(pressure)();
This compiles without any errors and also runs fine when using only one CPU for the simulation.
However, in parallel the simulation crashes as soon as the volVectorField for the gradient definition is included in the code. It even does not matter if I really use the gradient later on, just the definition itself seems to be critical. It seems to me this is some kind of MPI and/or memory allocation problem...

Did you experience the same issue when testing your wall function and do you have an idea how to solve this?



canopus March 25, 2016 18:13

Where to add this in case one wants to write out the pressure gradient during runtime?

Is something like probe for p/U possible for the pressure gradient by adding locations in controldict?


All times are GMT -4. The time now is 01:46.