|
[Sponsors] |
![]() |
![]() |
#1 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
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 |
|
![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 12 ![]() |
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) ); ... } 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; }; // ************************************************************************* // 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! |
|
![]() |
![]() |
![]() |
![]() |
#3 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
thank you carlos for your reply, I will try it
|
|
![]() |
![]() |
![]() |
![]() |
#4 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
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 ... |
|
![]() |
![]() |
![]() |
![]() |
#5 |
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 12 ![]() |
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; } 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. |
|
![]() |
![]() |
![]() |
![]() |
#6 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
thank you carlos, the solver compile now
|
|
![]() |
![]() |
![]() |
![]() |
#7 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
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. |
|
![]() |
![]() |
![]() |
![]() |
#8 |
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 12 ![]() |
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 Code:
function csvFile; functionCoeffs { nHeaderLine 1; refColumn 0; componentColumns (1 2 3); separator " "; mergeSeparators no; fileName "constant/data.csv"; outOfBounds clamp; interpolationScheme linear; }; // ************************************************************************* // Code:
--> FOAM FATAL IO ERROR: keyword fileName is undefined in dictionary "/home/simona/OpenFOAM/simona-4.1/run/laminarPipe/constant/F.functionCoeffs" |
|
![]() |
![]() |
![]() |
![]() |
#9 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
It works, thank you carlos
![]() |
|
![]() |
![]() |
![]() |
![]() |
#10 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
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 |
|
![]() |
![]() |
![]() |
![]() |
#11 |
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 12 ![]() |
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) ); |
|
![]() |
![]() |
![]() |
![]() |
#12 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
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(); |
|
![]() |
![]() |
![]() |
![]() |
#13 | ||
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 12 ![]() |
Quote:
Quote:
With the following snippet you shoud get the current time. Code:
scalar indexTime = runTime.time().timeOutputValue(); |
|||
![]() |
![]() |
![]() |
![]() |
#14 |
New Member
simona
Join Date: Apr 2021
Posts: 21
Rep Power: 6 ![]() |
it works perfectly carlos thank you very much
![]() |
|
![]() |
![]() |
![]() |
![]() |
#15 | |
Member
James
Join Date: Jan 2014
Posts: 38
Rep Power: 13 ![]() |
Quote:
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, |
||
![]() |
![]() |
![]() |
![]() |
#16 |
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 12 ![]() |
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. |
|
![]() |
![]() |
![]() |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
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 18:13 |
polynomial BC | srv537 | OpenFOAM Pre-Processing | 4 | December 3, 2016 09:07 |
[OpenFOAM.org] Error creating ParaView-4.1.0 OpenFOAM 2.3.0 | tlcoons | OpenFOAM Installation | 13 | April 20, 2016 17:34 |
centOS 5.6 : paraFoam not working | yossi | OpenFOAM Installation | 2 | October 9, 2013 01:41 |
DxFoam reader update | hjasak | OpenFOAM Post-Processing | 69 | April 24, 2008 01:24 |