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/)
-   -   Interpolation using interpolationLookUpTable or interpolationTable classes (https://www.cfd-online.com/Forums/openfoam-programming-development/68830-interpolation-using-interpolationlookuptable-interpolationtable-classes.html)

scotth2o October 2, 2009 16:56

Interpolation using interpolationLookUpTable or interpolationTable classes
 
I’d like to perform an interpolation on a volScalarField using a lookup table of values, interpolating linearly between defined points. For example if I have points (x & y, as shown in the table below and volScalarField X, I want to make a volScalarField Y corresponding to column y.

x | y
------
0 | 0
1 | 2

X=[0.2 0.4
0.5 0.6]

i.e. Y would equal

Y=[0.4 0.8
1.0 1.2

Based on the table .

My first question is what class would be the best for this type of interpolation (Note that in the end it will not be a linear as shown above).



After reading through the doxygen guide it looks like there are two classes to start with, "interpolationLookUpTable" and "interpolationTable". I'm not sure of the distinction between the two. I found a previous post regarding interpolationTable so I started there and I wrote a short program to figure out how to use it .


Code:

#include "fvCFD.H"
#include "interpolationTable.H"
#include "List.H"


int main(int argc, char *argv[])
{

    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"

    volScalarField X
    (
        IOobject
        (
            "X",
            runTime.timeName(),
            mesh,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh
    );
                        //  x|y
                        //------
    Tuple2<scalar,scalar> a(0,0);
    Tuple2<scalar,scalar> b(1,2);
    List< Tuple2<scalar,scalar> > c
    (
        a,
        b
    );

    scalar d=0.5;


    Foam::interpolationTable<List<Tuple2<scalar,scalar> > > JUNK(c, {0,1,0,0}, "");
    volScalarField Y=JUNK(X);


    Info << Y << endl;
    return 0;
}

When I compile I get the following errors



crap3.C: In function âint main(int, char**)â:
crap3.C:40: error: expected primary-expression before â{â token
crap3.C:41: error: no match for call to â(Foam::interpolationTable<Foam::List<Foam::Tuple2 <double, double> > >) (Foam::volScalarField&)â


I think the error in line 40 has something to do with the boundsHandling argument.

The constructor for this class is:
Code:

interpolationTable
          (
              const List<Tuple2<scalar, Type> >& values,
              const boundsHandling bounds,
              const fileName& fName
          );

At this point I'd like to know if this is the most efficient way to solve the problem. If so can someone help with the syntax of the example above?

Thanks

linch May 19, 2011 10:04

Hello Scott,

did you find a solution for implementing lookup tabels and the difference between mentioned classes?

Regards,
Ilya

scotth2o May 19, 2011 11:52

It's been so long I can't remember specifically what I was asking but I do remember the solution. I just went ahead and wrote a function to do look ups for scalar fields, see below.

Code:

volScalarField SHlibInterp::Interp1(List<scalar> X, List<scalar> Y,volScalarField& Xi,volScalarField& Yi)
    {
        scalar m;
        scalar b;
        int FOUND;
        int jj;
        forAll(Xi.internalField(),ii)
        {
            FOUND=0; jj=0;
            while((FOUND==0) && (jj<X.size()))
            {
                if(Xi.internalField()[ii]<X[0])
                {
                    Yi.internalField()[ii]=0;
                    FOUND=1;
                }
                else if(Xi.internalField()[ii]>X[X.size()])
                {
                    Yi.internalField()[ii]=Y[jj];
                    FOUND=1;
                }
                else if(Xi.internalField()[ii]>=X[jj] && Xi.internalField()[ii]<X[jj+1])
                {
                    m=(Y[jj+1]-Y[jj])/(X[jj+1]-X[jj]);
                    b=Y[jj]-m*X[jj];
                    Yi.internalField()[ii]=m*Xi.internalField()[ii]+b;
                    FOUND=1;
                };
                jj=jj+1;
            };
        };
        return(Yi);
    };


linch May 20, 2011 07:06

Thank you!

atoof August 31, 2012 08:51

Reading Scalars from dictionaries
 
Quote:

Originally Posted by scotth2o (Post 308408)

Code:

volScalarField SHlibInterp::Interp1(List<scalar> X, List<scalar> Y,volScalarField& Xi,volScalarField& Yi)

Hello Scott,

I have a similar problem. It seems that your function is suitable for interpolating between scalars. So I want to use your function. But I should read X, Y and Xi from dictionaries. Do you have any idea to do this?

Sincerely yours,

Hossein

atoof September 3, 2012 07:23

Hello,

It is solved.


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