|
[Sponsors] |
codeFixedValue - define and use function in code block? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
March 15, 2022, 01:48 |
codeFixedValue - define and use function in code block?
|
#1 |
New Member
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4 |
I am using "codeFixedValue" in "0/U" to implement a time-varying boundary condition (parabolic velocity profile at an inlet with time-varying max velocity). Writing out the code as a series of C++ statements works fine. However, if I consolidate some of the code into a function that I define and use in the code block, I get an error that includes the following: "error: a function-definition is not allowed here before '{' token". The '{' token is the opening curly brace for the function code.
Is it possible to define and use a function in the "codeFixedValue" code block? Or is there another way to achieve the same purpose by defining the function in a separate file and including it somehow? |
|
March 15, 2022, 13:44 |
Solution
|
#2 |
New Member
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4 |
I found a way to resolve my issue based on Ben Voight's answer at Simulating nested functions in C++. A full solution is shown below for 2D inlet parabolic fluid velocity with pulsed fluid flow in time. The key point is defining a "struct" (see "struct local" below) with a member function, "u_now", that is called further down in the code (see line with "u = local::u_now(...)").
Code:
inlet { type codedFixedValue; value uniform (0 0 0); name inletParabolicVelocityTimeVarying; code #{ const fvPatch &boundaryPatch = patch(); // generic const vectorField &Cf = boundaryPatch.Cf(); // generic vectorField &field = *this; // generic const scalar t = this->db().time().value(); const scalar chan_width = 100e-6; // definition of the channel width const scalar Umax = 0.001; // definition of the maximum velocity struct local { static scalar u_now(scalar t, scalar t0, scalar delta_t_start, scalar delta_t_on, scalar delta_t_stop, scalar Umax) { const scalar t1 = t0 + delta_t_start; const scalar t2 = t1 + delta_t_on; const scalar t3 = t2 + delta_t_stop; scalar u; if (t < t0) u = 0; else if (t < t1) u = Umax * (t - t0) / (t1 - t0); else if (t < t2) u = Umax; else if (t < t3) u = Umax * (1.0 - (t - t2) / (t3 - t2)); else u = 0; return u; } }; // Ramp velocity up from 0, hold, then ramp back down to 0 const scalar t0 = 0.01; const scalar delta_t_start = 0.01; const scalar delta_t_on = 0.01; const scalar delta_t_stop = 0.01; const scalar delta_t_between_pumps = 0.020; const scalar delta_t = delta_t_start + delta_t_on + delta_t_stop + delta_t_between_pumps; scalar u; scalar t0_now; int N = floor((t - t0) / delta_t); if ((t - t0) < 0.0) t0_now = t0; else { t0_now = t0 + N * delta_t; } u = local::u_now(t, t0_now, delta_t_start, delta_t_on, delta_t_stop, Umax); forAll(Cf, faceI) // loop over all the patch faces { const scalar y = Cf[faceI].y(); // y coordinate of the faces i const scalar normalized_y = y / chan_width; // compute radius from center patch field[faceI] = vector(4 * u * normalized_y * (1 - normalized_y), 0, 0); // define velocity value on the face i } #}; } Last edited by GregNordin; March 15, 2022 at 13:45. Reason: Clarity |
|
May 17, 2022, 07:55 |
|
#3 |
New Member
mill
Join Date: Feb 2021
Posts: 9
Rep Power: 5 |
Hi,
Did you solve this problem, I also want to learn how to use codeFixedValue |
|
May 17, 2022, 11:25 |
|
#4 |
New Member
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4 |
Yes, my solution is posted above (dated March 15, 2022). I had to use a struct instead of a function, and then OpenFoam handled it fine.
|
|
May 18, 2022, 04:23 |
|
#5 |
New Member
mill
Join Date: Feb 2021
Posts: 9
Rep Power: 5 |
||
November 9, 2023, 18:34 |
|
#6 |
New Member
Prom
Join Date: Aug 2023
Posts: 8
Rep Power: 3 |
Hi Greg, did you ever have issues where an error arose that your function was an undeclared identifier? Like this error:
Code:
/Users/promisea/OpenFOAM/ibukunOluwa/testCases/blockTest/testNSTX/0/T.boundaryField.top:83:45: error: use of undeclared identifier 'readXPositionArray' std::vector<double> xPosArray = readXPositionArray(filePath); ^ 1 error generated. make: *** [Make/darwin64ClangDPInt32Opt/fixedValueFvPatchFieldTemplate.o] Error 1 |
|
November 9, 2023, 18:43 |
|
#7 |
New Member
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4 |
@gbope7, sorry, I don't recall seeing an error like that.
|
|
|
|