CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Pre-Processing (
-   -   INTERPOLATEXY format entry for the Xold and Yold (

anne June 23, 2006 06:54

Hello, I have read several

I have read several interpolateXY (the engineValve example, ...) but I could'nt find
out hown to use interpolateXY, i.e, what is the format input read by interpolate.
I struggled in trying to find out some
information in graph applications but unusefully as my C++ knowledge is almost "null".

What is the correct format declaration for
the entry values in interpolateXY?

Thanks if you can help.


hjasak June 23, 2006 14:02

The function signature is:
The function signature is:

Field<type> interpolateXY
const scalarField& xNew,
const scalarField& xOld,
const Field<type>& yOld


Type interpolateXY
const scalar x,
const scalarField& xOld,
const Field<type>& yOld

Therefore, for new x value you can give it a scalar or a scalar field, for old y you give it a field of whatever you are interpolating (scalar, vector, tensor) and you get back either a single value (second function) or a whole field (first function) of the same thing. You should be able to read this from the function signature...



anne June 26, 2006 03:16

Thank You Hrv, Actually wha
Thank You Hrv,

Actually what is not clear for me is
the format for the interpolate function.
Should I write the input data in a similar way
to a volvelocity field read by
OpenFoam but specifying that is is a Field or Scalar interpolateXY and thus write by columns:
xnew, xold, yold

Must they have the same number of points or
not? (i.e xnew and xold the same number of data?).

There are perhaps evident questions nut I
really struggle with c++ format.

Thank you for your help,

hjasak June 26, 2006 06:40

Hello Anne, Let's think abo
Hello Anne,

Let's think about it: the idea of interpolation is that you give me an x-y graph and based on this I read off some values from the graph and return them. We have 2 functions: one which reads off values one at a time and another which will read lots of values together (see function signatures above).

So, the "old graph" is given by xold and yold, where xold is the abscissa and yold is the data (ordinate). Now, the function takes xnew (that would be the points you are interested in) and returns the data read off the graph for the new points.

Therefore, xnew can be any size you like and you will get the data (call it ynew - the return value of the function) with the same number of entries as xnew. Additionally, the type of ynew is the same as the type of yold, so if you have a graph of scalars you get scalars, for a graph of vectors you get the vectors etc.



anne June 26, 2006 06:56

Hello Hrv, what you told me
Hello Hrv,

what you told me is clear.
The problem I have is that I don't know how to
make read the graph file.

In engineValve, the declaration is as follows:

const graph& liftProfile

and thus a call to interpolate.

However, I don't find out where the graph file has to be put (in the 0 time directory ?), and also how to declare the name graph file.
It is a very basic problem (I know) but I have been trying to find an example but I didn't.
So the main problem I have is how to declare correctly my graph file so that the data be read.

I feel sorry to bother you with so basical sttuf, but I always work with the very simple
f77 and c++ is
a hard stuff for me. But I recognize, that
c++ is a very powerfull programmation languange
and I am quite impressed by the code structure.

Thanks again


hjasak June 26, 2006 07:31

Aha. It is really up to you w
Aha. It is really up to you where you put the file. To skip the "complex things", here's how I read valve lift in the engineValve (adjusted for your needs). The constructor (a function that makes the graph) we will use is defined in the library (OpenFOAM-1.3/src/OpenFOAM/graph/graph.H):

//- Construct from Istream given title and labels
const string& title,
const string& xName,
const string& yName,
Istream& is

This says: give me 3 strings (name of graph, label of x-axis and label of y-axis) and a stream of x-y data and I will make a graph for you.

The stream can be created in many ways. Probably the easiest one is from a file - I will give you a very simple example and you can adjust it to your needs:

IFstream if("myFile");

This creates a stream called if by opening a file called myFile in the CURRENT DIRECTORY. If you want to open (say) a file myFile in the constant directory of the case, you would do:

IFstream if(runTime.path()/runTime.constant()/"myFile");

Here, I am assuming that runTime is the name of your Time (database) class, as it is in the top-level code.

The format of myFile would be straight x-y with some brackets around it:

0 0.001112360000000003
2 0.001325159999999999
4 0.001549999999999994
6 0.001784680000000005
8 0.002027220000000005
10 0.002275840000000002

We can now create the graph:

graph myGraph


You can now do the interpolation like this: say, we are reading out a single value for myX:

scalar myY = interpolateXY

Still clear?

You should, of course, adjust any of the above to your needs and use more sensible names :-)



anne June 26, 2006 07:57

Dear Hrv, That is exactly t
Dear Hrv,

That is exactly the information I wanted from you.

So I think that I will now be able to do my interpolation ...

I thank you a lot,


anne June 27, 2006 04:32

Hello Hrv, Thanks to you I
Hello Hrv,

Thanks to you I have now my interpolation

However I still have a question more ...:

I use the interpolation to get an inlet profile
from experimental data and I do the following:

Info<< "Interpolation injector inlet" <<endl;

label inletPatchID=mesh.boundaryMesh().findPatchID("inje ctor_inl");
vectorField& UIN= U.boundaryField()[inletPatchID];
volVectorField centres = U.mesh().C();

vectorField& centresbc = centres.boundaryField()[inletPatchID];
scalarField ybc =centresbc.component(vector::Y);
scalarField zbc =centresbc.component(vector::X);

scalarField rbc =pow(ybc,2)+pow(zbc,2);

# include "createGraphIN.H"
scalarField rbc2=sqrt(rbc);

scalarField vzexp=interpolateXY


Apparently I have, when I visualize the velocity contours with paraFoam a correct inlet profile.

I would like you just to confirm that I really applied the interpolated profile by doing so.
I ask you this because I read quite a lot on the forum about this king of stuff and in one
of the discussion the creation of a new
PrimitivePatch or PatchtoPatchinterpolation was apparently necessary (or something like that)???

Thanks you


hjasak June 28, 2006 12:59

A few minor things: label
A few minor things:

label inletPatchID=mesh.boundaryMesh().findPatchID("inje ctor_inl");

After you do this, you should check that the label is not -1: if you mis-spelled the name, the patch will not be found and the function returns -1. If you carro on, the code will probably core dump, so it's nicer to check.

volVectorField centres = U.mesh().C();

This should be:

const volVectorField& centres = U.mesh().C();

Currently, you are making a copy, which is not necessary. Also:

const vectorField& centresbc = centres.boundaryField()[inletPatchID];

The rest is OK - you may consider using the patch normal instead of vector(0,0,1); to make it more general, but it does not matter.

Have a look at the file to see if the field is correctly set up.

Have fun,


anne June 29, 2006 03:25

Dear Hrv, Thanks a lot for
Dear Hrv,

Thanks a lot for your suggestions.

I will add the checking label.

You write that I can use the patch normal instead of vector(0,0,1) ... Well, I don't know the variable name of the patch normal and how to declare it , but I think I can find it in the forum or Dioxygen ...

Thanks again


hjasak June 29, 2006 06:49

Try: U.mesh().boundary().nf


The function is defined in fvPatch.H in the finiteVolume library:

//- Return face normals
virtual const vectorField& nf() const;


francois July 3, 2006 04:36

Hello, I just read these po

I just read these posts with great interest and still have some minor questions. I apologize if they are so "easy" but I don't find an answer in the message board:

1. When using interpolateXY must we always use a graph object to read the data file from which we want to make the interpolation ? Is there a way to use IFstream, read the data in this file and put them in 2 scalarField (one for the x coordinate and the other for the y coordinate) ?

2.Perhaps the second question is linked to the first one. I have tried the procedure described in this thread and I'm able to test the interpolation for one scalar. Now I have created a scalarField an want to make the interpolation on this scalar field, but I'm not able to write the new x scalar and y scalar field in a file using OFstream. Yes I know it's realy basic ... I have tried this:

/ Reading data file
IFstream fichierProfil(runTime.constant()/"profil.dat");
Info << "Reading file profil.dat " << endl;

graph myGraph

// Interpolation

scalarField xNew;
scalarField yNew = interpolateXY

// Writing interpolated data to a new file

OFstream fichierProfilInterpole (runTime.constant()/"profilNew.dat");
Info << "Writing interpolated data" << endl;
fichierProfilInterpole << xNew << yNew << endl;

And I the new file profilNew.dat I only have :


Any idea ?
Thank's a lot


aurelia July 3, 2006 04:51

Hello you need to change some
you need to change some file to use the append option of OFstream: see the last post of Hrvoje in


aurelia July 3, 2006 04:54

sorry it was not the good link
sorry it was not the good link
you will find the new OFstream file if you search for "OFstream app"

francois July 3, 2006 05:13

Thank's aurelia but I don' wan
Thank's aurelia but I don' want to make an append to an old file but only write some xy data to a new file.

I'm sorry but I send the wrong code ...
Well always think twice before posting !!!

So the new scalarField is defined by:

scalarField xNew=myGraph.x()+0.001 ;

And in the the new file I have:
6(0.001 2.001 4.001 6.001 8.001 10.001)6(0.00111247 0.00132527 0.00155012 0.0017848 0.00202734 0.00227584)

The interpolation works but the format of the file is strange for me. What about the 6 before the x and y values ?

hjasak July 3, 2006 05:28

Hi Francois, The 6 before o
Hi Francois,

The 6 before open bracket tells you that the length of the list is 6 (that's all). You have written out 2 scalar fields so I do not see any problem with the write out. If you want to have x-y pairs in the file, you should make a graph first and then write it out.


francois July 3, 2006 07:54

Thank's a lot Hrv ! You're so
Thank's a lot Hrv ! You're so helpfull

It's what I'm trying to do but I have problems with the format of the graph:

fileName fichierProfilInterpole (runTime.constant()/"profilNew.dat");
Info << "Ecriture dans le fichier" << endl;

graph myNewGraph

I tried raw, ascii but always have a message like:

raw was not declared in this scope

I'm looking in OpenFOAM/OpenFOAM-1.3/src/OpenFOAM/graph/ for the available format but can't find them ...

Anyway thanx for your help

hjasak July 3, 2006 11:56

Try "raw" in quotes and tell m
Try "raw" in quotes and tell me what happened.


francois July 3, 2006 17:10

Thank's Hrv it works I thi
Thank's Hrv it works

I think that all is in:

void graph::write(Ostream& os, const word& format)

I forget that the format was of type word, so it explains the quotes no ?

I will try to find a way to remove the .xy extention of the filename.

Thank's it realy helps me

francois July 3, 2006 17:22

I just find the tips use:
I just find the tips


OFstream fichierProfilInterpole (runTime.constant()/"profilNew.dat");

instead of:

fileName fichierProfilInterpole (runTime.constant()/"profilNew.dat");

The last command will add the .xy extension to the fileName when using "raw" format ...

I think it's in graph.C:

void graph::write(Ostream& os, const word& format) const
writer::New(format)().write(*this, os);

void graph::write(const fileName& fName, const word& format) const
autoPtr<writer> graphWriter(writer::New(format));

OFstream graphFile(fName + '.' + graphWriter().ext());

All times are GMT -4. The time now is 06:35.