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/)
-   -   Is it possible to exchange some vectors/ variables within C++ files of OpenFoam ? (https://www.cfd-online.com/Forums/openfoam-programming-development/157210-possible-exchange-some-vectors-variables-within-c-files-openfoam.html)

Asghari_M July 22, 2015 16:35

Is it possible to exchange some vectors/ variables within C++ files of OpenFoam ?
 
Hi all,
Since OpenFoam is an open-source code , it sounds like to be possible exchanging variables within its essential C++ files.
However, in some cases, it seems to be impossible. How can we explain this paradox?
For example , suppose there is a data/txt file as input as follows:
---
2
2.1
2.5
3.27

Now , I'm going to define a vector variable (A) corresponding with this txt/data file in an initial file as follows:

A(1)=2 (first line of data file)
A(2)=2.1 (second line of data file)
A(3)=2.5 (third line of data file)
A(4)=3.27 (forth line of data file)
Then , this vector is used in the inlet BC at blockMesh dict as follows:
Code:

inlet
{
Read A
type codedFixedValue;
value $internalField;
redirectType ramp;
code
#{
I= int(db().time().value()/A(1))
(*this)==vector(A(I)*db().time().value()+A(I+1),0,0);
#};
}

Is this operation (defining & exchanging variable (A) within routines/C++ dict files) feasible in this open-source code?
How can I define a vector based on txt/data file?
No wonder if the most of open-source codes have this capability.
Note that computational cost is a critical matter in this case.

Thanks a lot for any suggestion & attention.
M.Asghari

hk318i July 22, 2015 19:04

Here is an example which could be a start point for your case,
Assume A = (2 2.1 2.5 3.27)
So create a file in 0 directory and name it Avalue and add A as follows;
0/Avalue file
Code:

A (2 2.1 2.5 3.27);
#inputMode merge

And assuming that you want to read A[1] to use it for U BC as X component for fixedValue BC, so edit U as follows;
A/U file (note this from cavity tutorial)
Code:

#include "Avalue"
dimensions      [0 1 -1 0 0 0 0];

internalField  uniform (0 0 0);

boundaryField
{
    movingWall
    {
        type            fixedValue;
        value          #codeStream
        {
      codeInclude
      #{
        #include "scalarField.H"
        #include "vector.H"
      #};

            codeOptions
            #{
              // optional you may need it later which adds flags to  EXE_INC in Make/options
               
            #};
            codeLibs
            #{
              // optional you may need it later which adds flags to  LIB_LIBS in Make/options

            #};
          code
            #{

              scalarField A(dict.lookup("A", true));
              os  << "uniform " << vector(A[1],0,0);
              #};
     
        };
    }

    fixedWalls
    {
        type            fixedValue;
        value          uniform (0 0 0);
    }

    frontAndBack
    {
        type            empty;
    }
}

It is the most basic case of using codeStream. For more information about it check the user guide and check this thread which shows some of codeStream functionality http://www.cfd-online.com/Forums/ope...tml#post542310

Asghari_M July 24, 2015 04:35

Quote:

Originally Posted by hk318i (Post 556598)
Here is an example which could be a start point for your case,
Assume A = (2 2.1 2.5 3.27)
So create a file in 0 directory and name it Avalue and add A as follows;
0/Avalue file
Code:

A (2 2.1 2.5 3.27);
#inputMode merge

And assuming that you want to read A[1] to use it for U BC as X component for fixedValue BC, so edit U as follows;
A/U file (I think your purpose was 0/U. Is my view correct?)(note this from cavity tutorial)
Code:

#include "Avalue"
dimensions      [0 1 -1 0 0 0 0];

internalField  uniform (0 0 0);

boundaryField
{
    movingWall
    {
        type            fixedValue;
        value          #codeStream
        {
      codeInclude
      #{
        #include "scalarField.H"
        #include "vector.H"
      #};

            codeOptions
            #{
              // optional you may need it later which adds flags to  EXE_INC in Make/options
               
            #};
            codeLibs
            #{
              // optional you may need it later which adds flags to  LIB_LIBS in Make/options

            #};
          code
            #{

          scalarField A(dict.lookup("A", true));
              os  << "uniform " << vector(A[1],0,0);

              #};
     
        };
    }

    fixedWalls
    {
        type            fixedValue;
        value          uniform (0 0 0);
    }

    frontAndBack
    {
        type            empty;
    }
}


Hello Hassan.
Can I set A vector in its own 0/U code as follows?
Code:

A (2 2.1 2.5 3.27);
dimensions      [0 1 -1 0 0 0 0];

internalField  uniform (0 0 0);

boundaryField
{
    movingWall
    {
        type            fixedValue;
        value          #codeStream
        {
      codeInclude
      #{
        #include "scalarField.H"
        #include "vector.H"
      #};

            codeOptions
            #{
              // optional you may need it later which adds flags to  EXE_INC in Make/options
               
            #};
            codeLibs
            #{
              // optional you may need it later which adds flags to  LIB_LIBS in Make/options

            #};
          code
            #{

              scalarField A(dict.lookup("A", true));
              os  << "uniform " << vector(A[1],0,0);
              #};
     
        };
    }

    fixedWalls
    {
        type            fixedValue;
        value          uniform (0 0 0);
    }

    frontAndBack
    {
        type            empty;
    }
}

Really, what's difference between writing of vector A in U file and writing of it in an individual Avalue code?

Also, assume I use I and A(I) phrases since I need them for inlet velocity BC linear interpolation. Can I use following code (inserted in U file) for their definition (assume the relations are correct . I only worry about syntax in OF & C++):
Code:

          code
                        #{
                            scalarField A(dict.lookup("A", true));
                            I= real( int(db().time().value()/A(1)));
                            os  << "uniform " <<vector(A(I)*db().time().value()+A(I+1),0,0);
             
          #};

Are syntaxes of red expressions correct?
What's dict.lookup meaning in this phrase?
Thanks for your kind attention.

hk318i July 24, 2015 19:07

  • It does matter to define A inside the 0/U file. I put it in separate file just as a more general example.
  • The code in red isn't correct, codeStream has the same syntax as OF library and c++. scalarField is of type List<double> which has access operator [ ] not ( ). Therefore any element could be accessed as A[i] where i must be of type label or int, see my previous post as example for accessing A[1] which is equal to 2.1
  • dict is a reference to the dictionary where the codeStream is defined. dict is declared automatically and the Ostream os as well, see the user guide link in my previous post.

Asghari_M July 25, 2015 16:42

Quote:

Originally Posted by hk318i (Post 556901)
  • The code in red isn't correct, codeStream has the same syntax as OF library and c++. scalarField is of type List<double> which has access operator [ ] not ( ). Therefore any element could be accessed as A[i] where i must be of type label or int, see my previous post as example for accessing A[1] which is equal to 2.1

Thanks for your kind attentions & helpful responses.
Only two question remain.
1-Is it feasible to use A[I] and db().time().value(), synchronously at code
as follows?
Code:

os  << "uniform " <<vector(A[I]*db().time().value()+A[I+1],0,0);
2-Is it possible to access time variables such as db().time().value() within the code with fixedvalue type instead of codedFixedValue without using the following code?
Code:

#include Time.h
Thanks very much .

hk318i July 25, 2015 17:15

I didn't try before but if time is available using codedFixedValue, you can use it instead of fixed value. I believe the same syntax for reading A will work.

Asghari_M July 26, 2015 13:06

As last question, Is it important to know what's the type of Avalue file?
There two types for typical output or info/dict files as far as I know as follows:
1- Plain text document (text/plain) like thing which used in probes.
2- C source code (text/x-csrc) such as 0/U file.
Is Avalue file type first or second?

Thanks for your all helpful answers.

hk318i July 26, 2015 13:14

I am not sure if I understand your question right but A is defined in dictionary 0/U as dictionary entry

Name Value;

Asghari_M July 29, 2015 02:51

Frankly you're the only one ever to all my questions without hesitation replied.

Thank you for your kindness.

As for the type of file, by right clicking on the file and then clicking on properies , file type is specified.
Due to ur discription, Avalue file type should be c source code or same second option.
Also, I'd glad if you participate in my next discussions in this forum and post-processing one that I will post them.
M.Asghari

hk318i July 29, 2015 03:43

You are welcome.
Don't worry about the file time because Linux isn't like windows needs file extension to recognise the file type. All OF case files are just text file and the type which you can see down in gedit is just for highlighting.

Asghari_M August 2, 2015 01:39

I concluded that it isn't a critical matter what is to be the type of Avalue file.
It can be either Plain text document (text/plain) or C source code type. Isn't it?

Best Regards.


All times are GMT -4. The time now is 23:11.