CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   including parameter file in codedFixedValue (https://www.cfd-online.com/Forums/openfoam-solving/212162-including-parameter-file-codedfixedvalue.html)

Loekatoni November 27, 2018 09:07

including parameter file in codedFixedValue
 
Hi all,

Intro
I have a boundary condition in which i give a profile to the inlet of my system via codedFixedValue. Now I want to be able to scale the whol system, so I want to put as many parameters in a separate file, in which I can change to desired values. This file is called parameters. So I want to include the file in the 0/U file. For a boundary which doens't include codedFixedValue it worked by including in the start of the 0/U file like:


______________________

/* start of openfoam file */

#include "../../parameters"

dimensions [0 1 -1 0 0 0 0];



// rest of file
______________________


I did this with the BC flowRateInletVelocity.


Now the above line didn't work for codedFixedValue. As far as I tried some random things, it looks like it needs to be included in the specific codedFixedValue BC if I want to use it in the code:
________________________________________________
boundaryField
{

inlet
{
type codedFixedValue;
value uniform (0 0 0);
name velocityProfile;

code
#{
#include "/home/path/to/parameters"
scalar A = ((($p0-$pL)/$L + $rho*$g)*pow($R, 2))/(2*$mu);
scalar U_TB = $U_TB;
scalar R = $R;
fixedValueFvPatchVectorField inlet(*this);

forAll(this->patch().Cf(),i)
{
inlet[i] = vector(0, 0, A*(Foam::pow(this->patch().Cf()[i].x()/R, 2)-1)-U_TB);
}
operator == (inlet);
#};
}
}
________________________________________________



the #include "../../parameters" didn't work here, it needed to be changed to #include "/home/path/to/parameters", otherwise it gave the error: no such file or directory. No it recognizes the file, but gives a couple of errors.

Errors
Now it is able to include it, but I get two kinds of errors;
1. /home/path/to/parameters:line:element: error: 'parameter' was not declared in this scope


This refers to a parameter I have defined, for example L:
L #calc "$tau*$U_TB"; where tau and U_TB are defined before this line comes along.



2. /home/path/to/parameters:line:element: error: stray '#' in program.


This refers to a calculation that needs to be done to obtain a parameter, which as example can also be the line for L from above.


Questions
- How can I solve these errors?
- Is there a way I can do this include with ../../parameters instead of /home/path/to/parameters?


Sincerely,
Loek

Loekatoni November 27, 2018 10:13

Partial solution to the problem
 
So I have succeeded in getting the codedFixedValue to work with including the parameter file. Here is how I did it:


Partial solution
I made a parameters2 file. In this file my parameters are defined differently:


______________________________________
// Variable paremeters
double R = 0.0005; // m, channel radius
double tau = 5; // s, residence time
double p0 = 1.001; // bar, inlet pressure
double pL = 1; // bar, outlet pressure


// Fixed parameters
double g = 9.81; // m/s2, gravitational constant
double rho = 870; // kg/m3, density fluid
double mu = 0.00056; // Pa*s, viscosity fluid


// Calculated parameters
double U_TB = 0.351*sqrt(g*2*R);

double L = tau*U_TB;
______________________________________


Now with this file included while in

boundaryFields
{
inlet
{
blabla
code
#{
#include "/home/path/to/parameters2"

formulation of equation

#}


New problem
While this works for codedFixedValue, for initialising the velocity of the wall of my channel I also need a U_TB. This one needs to be initiated differently. This parameters file needs to be included at the start of the 0/U file as follows:
#include "../../parameters"
as it is two directories down the line.

And has the form:
______________________________________

// Variable paremeters
R 0.0005; // m, channel radius
tau 5; // s, residence time
p0 5e5; // Pa, inlet pressure
pL 1e5; // Pa, outlet pressure


// Fixed parameters
g 9.81; // m/s2, gravitational constant
rho 870; // kg/m3, density fluid
mu 0.00056; // Pa*s, viscosity fluid


// Calculated parameters
U_TB #calc "0.351*sqrt($g*2*$R)";

L #calc "$tau*$U_TB";
______________________________________

A side note: the files parameters and parameters2 are in the same directory.


New questions
Can someone explain to me what the different forms of defining parameters are?
And why does the first type work for the codedFixedValue and not for the flowRateInletVelocity?
And why does the second type works vice versa?


Sincerely,
Loek

ykanani June 9, 2020 16:31

Thanks for the workaround.

The first way of defining parameter is recognizable to OpenFOAM IODictionaries and that is why it works fine for flowRateInletVelocity or within any other dictionary.

This seems to be not recognized within the "code" section of the codedFixedValue, and hence, in your workaround, you define the parameters using C++ syntax, and include it within the code itself, and hence it works fine.


This is still puzzling for me as I had used this feature (the first way you tried) earlier withing the blockmeshdict in a codestream block and it worked fine. you can also see other people using it too:
https://www.cfd-online.com/Forums/op...kmeshdict.html

But your solution fixes my problem, so thanks!

Quote:

Originally Posted by Loekatoni (Post 717046)
So I have succeeded in getting the codedFixedValue to work with including the parameter file. Here is how I did it:


Partial solution
I made a parameters2 file. In this file my parameters are defined differently:


______________________________________
// Variable paremeters
double R = 0.0005; // m, channel radius
double tau = 5; // s, residence time
double p0 = 1.001; // bar, inlet pressure
double pL = 1; // bar, outlet pressure


// Fixed parameters
double g = 9.81; // m/s2, gravitational constant
double rho = 870; // kg/m3, density fluid
double mu = 0.00056; // Pa*s, viscosity fluid


// Calculated parameters
double U_TB = 0.351*sqrt(g*2*R);

double L = tau*U_TB;
______________________________________


Now with this file included while in

boundaryFields
{
inlet
{
blabla
code
#{
#include "/home/path/to/parameters2"

formulation of equation

#}


New problem
While this works for codedFixedValue, for initialising the velocity of the wall of my channel I also need a U_TB. This one needs to be initiated differently. This parameters file needs to be included at the start of the 0/U file as follows:
#include "../../parameters"
as it is two directories down the line.

And has the form:
______________________________________

// Variable paremeters
R 0.0005; // m, channel radius
tau 5; // s, residence time
p0 5e5; // Pa, inlet pressure
pL 1e5; // Pa, outlet pressure


// Fixed parameters
g 9.81; // m/s2, gravitational constant
rho 870; // kg/m3, density fluid
mu 0.00056; // Pa*s, viscosity fluid


// Calculated parameters
U_TB #calc "0.351*sqrt($g*2*$R)";

L #calc "$tau*$U_TB";
______________________________________

A side note: the files parameters and parameters2 are in the same directory.


New questions
Can someone explain to me what the different forms of defining parameters are?
And why does the first type work for the codedFixedValue and not for the flowRateInletVelocity?
And why does the second type works vice versa?


Sincerely,
Loek


dMonty August 19, 2022 12:01

Thanks for the info. I've had good luck using this approach with internal fields and boundary conditions, but have ran into a problem when I try to decompose the boundary conditions for parallel applications. The problem comes from how decomposePar writes the code for each processor. I've been putting the #include "$FOAM_CASES/inputParams" line above the code since the "/home/to/path/inputParams" always leads to an error.

Do you know where the /home/to/path is pointing? Where are you storing your input file?

Thanks!

gbope7 November 9, 2023 16:56

Hi Loek, were your parameters files written in OpenFOAM/C++ syntax? I have a text file (r132406.txt) that contains position data that I would like to be parsed by a function that I defined in a local struct in the codedFixedValue BC.


I was able able to include the text file using your method, but I got an error as OpenFOAM tried parsing through it automatically. Is there a way to include any text file without running into this issue?



Quote:

Invoking wmake libso /Users/promisea/OpenFOAM/ibukunOluwa/testCases/blockTest/testNSTX/dynamicCode/surfaceBC
wmake libso /Users/promisea/OpenFOAM/ibukunOluwa/testCases/blockTest/testNSTX/dynamicCode/surfaceBC
ln: ./lnInclude
dep: fixedValueFvPatchFieldTemplate.C
wmkdepend: parse error while scanning '/Users/promisea/OpenFOAM/ibukunOluwa/testCases/blockTest/testNSTX/r132406.txt' ... perhaps missing a final newline
Ctoo: fixedValueFvPatchFieldTemplate.C
In file included from /Users/promisea/OpenFOAM/ibukunOluwa/testCases/blockTest/testNSTX/0/T.boundaryField.top:35:
/Users/promisea/OpenFOAM/ibukunOluwa/testCases/blockTest/testNSTX/r132406.txt:1:1: error: expected unqualified-id
0.647001
^
1 error generated.
make: *** [Make/darwin64ClangDPInt32Opt/fixedValueFvPatchFieldTemplate.o] Error 1


--> FOAM FATAL IO ERROR: (openfoam-2306)
Failed wmake "dynamicCode/surfaceBC/platforms/darwin64ClangDPInt32Opt/lib/libsurfaceBC_303d73216935d55234d3e56b4a68e30ac2c21 169.dylib"


file: 0/T.boundaryField.top at line 28 to 43.

From void Foam::codedBase::createLibrary(Foam::dynamicCode &, const Foam::dynamicCodeContext &) const
in file db/dynamicLibrary/codedBase/codedBase.C at line 245.

FOAM exiting


All times are GMT -4. The time now is 15:21.