CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Pre-Processing

read csv file into openfoam

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree8Likes
  • 1 Post By crubio.abujas
  • 1 Post By crubio.abujas
  • 1 Post By crubio.abujas
  • 1 Post By carolee
  • 1 Post By crubio.abujas
  • 1 Post By crubio.abujas
  • 2 Post By carolee

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 29, 2021, 08:58
Default read csv file into openfoam
  #1
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
Hi everybody,
I added a constant force as source term in momentum equation,

dimensionedVector F ("F", dimensionSet (0,1, -2,0,0,0,0), vector (0,0,-2.5));
tmp<fvVectorMatrix> tUEqn
(
fvm::div(phi, U)
+ MRF.DDt(U)
+ turbulence->divDevReff(U)
- F
- fvm::laplacian(mu, U)
==
fvOptions(U)
);

but I want that openfoam read the value of this force (0,0,-2.5) from an external file like csv file not from my solver, because I will add a variable force so I have to try first with a simple case, any help to this problem will be apreciated
carolee is offline   Reply With Quote

Old   May 30, 2021, 14:21
Default Try Function1.h
  #2
Senior Member
 
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 118
Rep Power: 6
crubio.abujas is on a distinguished road
Hello Simona,

You can check "function1.h" library. With that you are not limited to csvFiles, but also other option such as polynomial or constant values. All is defined in runTime so you don't need to recompile it each time you need to make a change.

If it sounds interesting try adding this to your code.

In the solver source:
Code:
#include "Function1.h"

int main(int argc, char* argv[])
{
     ...
     
     // Read interpolation (only done once typically in createFields.h)
     dictionary FDict;
     IOobject FDictIO
     (
        "F", // name of the file
        mesh.time().constant(), // path to where the file is
        mesh, // reference to the mesh needed by the constructor
        IOobject::MUST_READ // indicate that reading this dictionary is compulsory
     );
     FDict = IOdictionary(FDictIO);
    
    // Create a function to interpolate F, the function is defined in the case settings
    autoPtr<Function1<vector>> function_ 
    (
        Function1<vector>::New("function", FDict)
    );

    ...

    //When you want to use it. (Update the matrix)
    dimensionedVector F 
   (
          "F",
          dimensionSet (0,1, -2,0,0,0,0),
          function_->value(time)
   );
   ...

}
Then, in the case definition create a constant/F file with the following information:
Code:
/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  7
|   \\  /    A nd           | Web:      www.OpenFOAM.org                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       dictionary;
    location    "constant";
    object      F;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

function csvFile;

functionCoeffs
{
    nHeaderLine         1;
    refColumn           0;
    componentColumns    (1 2 3);
    separator           ",";
    mergeSeparators     no;
    file                "constant/data.csv";
    outOfBounds         clamp;
    interpolationScheme linear;
};

// ************************************************************************* //
In this file you can set the path to your data, and configure the interpolation method, limits and so on.

Be aware that the code may change with the version of Openfoam you're using, so check it out. I've used Openfoam7 for the tests.

Good luck!
carolee likes this.
crubio.abujas is offline   Reply With Quote

Old   June 1, 2021, 15:10
Default
  #3
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
thank you carlos for your reply, I will try it
carolee is offline   Reply With Quote

Old   June 1, 2021, 19:20
Default
  #4
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
I try to compile the solver, I obtain these errors, how can I make a new dictionary named F ?

./Function1.H:4:6: error: ‘dictionary’ does not name a type
dictionary FDict;
^
./Function1.H:5:6: error: ‘IOobject’ does not name a type
IOobject FDictIO
^
./Function1.H:12:6: error: ‘FDict’ does not name a type
FDict = IOdictionary(FDictIO);
^
./Function1.H:15:5: error: ‘autoPtr’ does not name a type
autoPtr<Function1<vector>> function_
^
./Function1.H:20:5: error: expected unqualified-id before ‘...’ token
...
carolee is offline   Reply With Quote

Old   June 2, 2021, 02:50
Default
  #5
Senior Member
 
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 118
Rep Power: 6
crubio.abujas is on a distinguished road
Hi Simona,

from the errors I assume that you have written a Function1.H file, but this file shall already exists in your implementation of Openfoam so you don't have to create it.

For example look at this simple test, this should be equivalent to your solver.
Code:
#include "fvCFD.H"
#include "Function1.H"

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

    dictionary FDict;
    IOobject FDictIO
    (
        "F", 
        mesh.time().constant(), 
        mesh, 
        IOobject::MUST_READ 
    );
    FDict = IOdictionary(FDictIO);
    autoPtr<Function1<vector>> function_ 
    (
        Function1<vector>::New("function", FDict)
    );

    dimensionedVector F
    (
        "F",
        dimensionSet(0,1,-2,0,0,0,0),
        function_->value(0)
    );

    Info << "Read function_ for velocityProfile" << nl
         << "type:" << FDict.lookup("function") << nl
         << "Value of t=0 -> " << function_->value(0) << nl
         << "Value of t=1 -> " << function_->value(1) << nl
         << "Value of t=2 -> " << function_->value(2) << nl
         << endl;

    Info<< "End\n" << endl;
    return 0;
}
This example is just a merge between the previous code and some bare minimum to launch the solver and print the first instances of the vector.

If the errors persists, please share the code or a simplified version of it to better understand where the problem is. Also it may be important to know what is the openfoam version you're working with.
carolee likes this.
crubio.abujas is offline   Reply With Quote

Old   June 2, 2021, 05:17
Default
  #6
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
thank you carlos, the solver compile now
carolee is offline   Reply With Quote

Old   June 2, 2021, 06:17
Default
  #7
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
I made a csv file named data which contains the value of my force like that
x y z
F 0 0 2,5
I create a constant/F file as you describe to me, I obtain these errors


Starting time loop

Time = 1

--> FOAM FATAL IO ERROR:
keyword fileName is undefined in dictionary "/home/simona/OpenFOAM/simona-4.1/run/laminarPipe/constant/F.functionCoeffs"

file: /home/simona/OpenFOAM/simona-4.1/run/laminarPipe/constant/F.functionCoeffs from line 22 to line 29.

From function const Foam::entry& Foam::dictionary::lookupEntry(const Foam::word&, bool, bool) const
in file db/dictionary/dictionary.C at line 441.
carolee is offline   Reply With Quote

Old   June 2, 2021, 08:27
Default
  #8
Senior Member
 
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 118
Rep Power: 6
crubio.abujas is on a distinguished road
Hi Simona,

Some details depend on the version you're using. It seems that in your version (I assume you're using Openfoam 4.1 from the user path) the keyword for the csv file is fileName insted of file. Just change it in the definition file.

Also, in your csv definition, take into account that you need to provide some index (time for example) to work around. Ensure that the separator and decimal symbols match. In your csv definition seem that you're using spaces as separator and "," as decimal separator. Decimal separator can be quite anoying, but I recommend you to use "." instead.

With all, your csv file shall look like this.
Code:
# time x y z
0 0 0 2.5
1 0 0 2
And your F file like this.
Code:
function csvFile;

functionCoeffs
{
    nHeaderLine         1;
    refColumn           0;
    componentColumns    (1 2 3);
    separator           " ";
    mergeSeparators     no;
    fileName                "constant/data.csv";
    outOfBounds         clamp;
    interpolationScheme linear;
};

// ************************************************************************* //
I have not check the OF4.1 version, but there may be other keywords that are different. Try to read the exception notes to understand what may be missing.
Code:
--> FOAM FATAL IO ERROR:
keyword fileName is undefined in dictionary "/home/simona/OpenFOAM/simona-4.1/run/laminarPipe/constant/F.functionCoeffs"
carolee likes this.
crubio.abujas is offline   Reply With Quote

Old   June 3, 2021, 09:14
Default
  #9
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
It works, thank you carlos
crubio.abujas likes this.
carolee is offline   Reply With Quote

Old   June 18, 2021, 19:09
Default
  #10
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
hello carlos, I hope you are fine, I have a problem regarding the csv file,I added a constant force and it works perfectly but when I try to add a variable force which increases over time, openFoam only reads the first value in the CSV file (0 0 0), it ignores other values
how can I fix this problem please?
time x y z
5 0 0 0
100 0 0 0.5
200 0 0 2
300 0 0 3.5
400 0 0 4
500 0 0 5.5
600 0 0 6
700 0 0 6.7
carolee is offline   Reply With Quote

Old   June 19, 2021, 12:02
Default
  #11
Senior Member
 
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 118
Rep Power: 6
crubio.abujas is on a distinguished road
Hi Simona,

What is the index your using for the interpolation? Does it corresponds with the runTime iteration?

The code should look like this:

Code:
scalar indexTime = runTime.time().timeName();
dimensionedVector F 
(
     "F",
     dimensionSet (0,1, -2,0,0,0,0),
     function_->value(indexTime)
);
Here I read the current timestep in a scalar named indexTime and used it as index of the function.
carolee likes this.
crubio.abujas is offline   Reply With Quote

Old   June 19, 2021, 13:24
Default
  #12
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
F was declared as below:
dimensionedVector F
(
"F",
dimensionSet(0,1,-2,0,0,0,0),
function_->value(0)
);

when I put the new code, I have this error:

UEqn.H: In function ‘int main(int, char**)’:
UEqn.H:11:47: error: cannot convert ‘Foam::word’ to ‘Foam::scalar {aka double}’ in initialization
scalar indexTime = runTime.time().timeName();
carolee is offline   Reply With Quote

Old   June 19, 2021, 14:48
Default
  #13
Senior Member
 
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 118
Rep Power: 6
crubio.abujas is on a distinguished road
Quote:
Originally Posted by carolee View Post
F was declared as below:
dimensionedVector F
(
"F",
dimensionSet(0,1,-2,0,0,0,0),
function_->value(0)
);
That explains why you only have the value for instant 0, you were evaluating the function in the same instant.

Quote:
Originally Posted by carolee View Post
when I put the new code, I have this error:

UEqn.H: In function ‘int main(int, char**)’:
UEqn.H:11:47: error: cannot convert ‘Foam::word’ to ‘Foam::scalar {aka double}’ in initialization
scalar indexTime = runTime.time().timeName();
Ok, I didn't check. the method timeName() returns a word and there is not any conversion to a scalar. You need to call other method.
With the following snippet you shoud get the current time.
Code:
scalar indexTime = runTime.time().timeOutputValue();
carolee likes this.
crubio.abujas is offline   Reply With Quote

Old   June 19, 2021, 15:24
Default
  #14
New Member
 
simona
Join Date: Apr 2021
Posts: 20
Rep Power: 2
carolee is on a distinguished road
it works perfectly carlos thank you very much
Madeinspace and crubio.abujas like this.
carolee is offline   Reply With Quote

Old   September 27, 2021, 05:28
Default
  #15
Member
 
James
Join Date: Jan 2014
Posts: 37
Rep Power: 9
Madeinspace is on a distinguished road
Quote:
Originally Posted by crubio.abujas View Post
Hi Simona,

Some details depend on the version you're using. It seems that in your version (I assume you're using Openfoam 4.1 from the user path) the keyword for the csv file is fileName insted of file. Just change it in the definition file.

Also, in your csv definition, take into account that you need to provide some index (time for example) to work around. Ensure that the separator and decimal symbols match. In your csv definition seem that you're using spaces as separator and "," as decimal separator. Decimal separator can be quite anoying, but I recommend you to use "." instead.

With all, your csv file shall look like this.
Code:
# time x y z
0 0 0 2.5
1 0 0 2
And your F file like this.
Code:
function csvFile;

functionCoeffs
{
    nHeaderLine         1;
    refColumn           0;
    componentColumns    (1 2 3);
    separator           " ";
    mergeSeparators     no;
   fileName                "constant/data.csv";
    outOfBounds         clamp;
    interpolationScheme linear;
};

// ************************************************************************* //
I have not check the OF4.1 version, but there may be other keywords that are different. Try to read the exception notes to understand what may be missing.
Code:
--> FOAM FATAL IO ERROR:
keyword fileName is undefined in dictionary "/home/simona/OpenFOAM/simona-4.1/run/laminarPipe/constant/F.functionCoeffs"


Thank you Simona and Carlos for this interesting thread. I am trying to do something similar and would really appereciate your input.

If I understand from Simona's post, your constant force is same in every cell of the computational domain, right? Mine is slighlty different.

I have a volVectorFields (Force). The value of the force is different in each cell. Earlier I only had one (averaged) field for the whole simulation time, so I calculated this field and stored it in 0/ directory and read it during the declaration and used it as a source term in my UEqn. This worked fine.


But now, I would like to apply this force such that it is not averaged but it changes with time. So say, now I have the same field but 10 of them. For now, I have named them CSV1, CSV2, ... CSV10.

Now I would like to apply the force such that it changes every 1 sec when simulating from 0 to 9 sec. Would it be possible to read different file every 1 second?

In that sense, each second, I should read different file as shown below.

# time x y z
0 CSV1
1 CSV2
2 CSV3
.
.
.
9 CSV10



Would this be possible with this approach.

Thanks. Would appereaciate your input !

BR,
Madeinspace is offline   Reply With Quote

Old   September 28, 2021, 04:28
Default
  #16
Senior Member
 
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 118
Rep Power: 6
crubio.abujas is on a distinguished road
HiJames,


I think it is more complicated than that.What Carolee has done is a 1D interpolation, as the field is uniform in the entire domain.You can use built in functions to do this direcly with the Function1 library. What you are suggesting is a 4D interpolation (on a 3D space and 1 in time) which is as you can expect more complex than what has been done here. As far as I know there is not a direct way to do this with already implemented code, so it may be needed to assembly some code to do this.

This problem reminds me of the timeVaryingMappedFixedValue boundary condition, where a set of points is provided and in separate files a set of values for those points. The format is not csv but is quite simple to understand and manage. It can even use linear interpolation on time, so the change on the source is not abrupt. But this code is only applicable to boundaries, so you will need to create your own code to perform this operation on the entire domain.

Another approach to explore is to check the interpolatorCell used on the mappings. I think you may try to create an artificial vectorField with your source field and use it to construct an instance of interpolatorCell to do the interpolation of the values on the cells. I think it may be interesting to store the interpolation weights as it can be quite expensive to calculate them multiple times, but this may imply more changes on the code.

The simplest approach is to get rid of the interpolation entirely and use the exact points for the source formulation. In this approach you have to check how to read the points and generate the vector sources on whatever you are using to obtain the vector values (I guess you are using some kind of external model on MATLAB or python). Then you have to create a set of files in the same fashion of the U field, where a nonuniform value is used in the fieldValue. Be aware that the order of the values should correspond with the cell order stored in the mesh. Then just add some code to check and load the corresponding file for each time.

There are probably better solutions using some implemented code, but right now is all I can think about. Whenever I have more time I may play around with some of these approaches, but I cannot tell you for sure when.

Let me know any advancements on this problem in case I can help you further.
crubio.abujas is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[foam-extend.org] Problems installing foam-extend-4.0 on openSUSE 42.2 and Ubuntu 16.04 ordinary OpenFOAM Installation 19 September 3, 2019 19:13
polynomial BC srv537 OpenFOAM Pre-Processing 4 December 3, 2016 10:07
[OpenFOAM.org] Error creating ParaView-4.1.0 OpenFOAM 2.3.0 tlcoons OpenFOAM Installation 13 April 20, 2016 18:34
centOS 5.6 : paraFoam not working yossi OpenFOAM Installation 2 October 9, 2013 02:41
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 02:24


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