CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Running, Solving & CFD

codeFixedValue - define and use function in code block?

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By GregNordin

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 15, 2022, 00:48
Default codeFixedValue - define and use function in code block?
  #1
New Member
 
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4
GregNordin is on a distinguished road
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?
GregNordin is offline   Reply With Quote

Old   March 15, 2022, 12:44
Default Solution
  #2
New Member
 
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4
GregNordin is on a distinguished road
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 12:45. Reason: Clarity
GregNordin is offline   Reply With Quote

Old   May 17, 2022, 06:55
Default
  #3
New Member
 
mill
Join Date: Feb 2021
Posts: 9
Rep Power: 5
mill is on a distinguished road
Hi,
Did you solve this problem, I also want to learn how to use codeFixedValue
mill is offline   Reply With Quote

Old   May 17, 2022, 10:25
Default
  #4
New Member
 
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4
GregNordin is on a distinguished road
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.
mill likes this.
GregNordin is offline   Reply With Quote

Old   May 18, 2022, 03:23
Default
  #5
New Member
 
mill
Join Date: Feb 2021
Posts: 9
Rep Power: 5
mill is on a distinguished road
Quote:
Originally Posted by GregNordin View Post
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.
Thank you!
mill is offline   Reply With Quote

Old   November 9, 2023, 17:34
Default
  #6
New Member
 
Prom
Join Date: Aug 2023
Posts: 7
Rep Power: 2
gbope7 is on a distinguished road
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
gbope7 is offline   Reply With Quote

Old   November 9, 2023, 17:43
Default
  #7
New Member
 
Greg Nordin
Join Date: Feb 2022
Posts: 9
Rep Power: 4
GregNordin is on a distinguished road
@gbope7, sorry, I don't recall seeing an error like that.
GregNordin is offline   Reply With Quote

Reply


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



All times are GMT -4. The time now is 06:24.