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

The effect of write precision on CFD solution with codeStream

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 13, 2022, 13:01
Default The effect of write precision on CFD solution with codeStream
  #1
New Member
 
Join Date: Feb 2022
Posts: 25
Rep Power: 4
theBananaTrick is on a distinguished road
Hi,

Following my previous post, Custom boundaries and explicit operators, I met another "funny" thing. I am new to OF and did not find this information anywhere so I decided to share it. I am using OF2106.

If one is using codeStream/setExprField, and I guess coded boundaries conditions. The writePrecision parameter in controlDict can have some significance!

I have the following codestream code for defining the internalField in p:
Code:
dimensions      [1 -1 -2 0 0 0 0];

internalField nonuniform #codeStream
{
    codeInclude
    #{
        #include "fvCFD.H"
    #};

    codeOptions
    #{
        -I$(LIB_SRC)/finiteVolume/lnInclude \
        -I$(LIB_SRC)/meshTools/lnInclude
    #};

    codeLibs
    #{
        -lmeshTools \
        -lfiniteVolume
    #};

    code
    #{
        const IOdictionary& d = static_cast<const IOdictionary&>(dict);
        const fvMesh& mesh = refCast<const fvMesh>(d.db());
        scalarField myInteriorField(mesh.nCells(), 0.0);
        const volVectorField& C = mesh.C();

        forAll(myInteriorField, cellI)
        {
            const scalar x = C[cellI].x();
            const scalar y = C[cellI].y();
            const scalar tmp0 = (5.0/4.0)*M_PI; 
            myInteriorField[cellI] = 1000.0*Foam::sin(tmp0*y) + 1000.0*Foam::cos(tmp0*x) + 100000.0; 
        }

        os.writeEntry("", myInteriorField);
    #};
};
Inside the code I am computing the difference between a dummy field and the pressure field. The dummy field has the same value as the pressureField.
Code:
const volVectorField& C = mesh.C(); 

volScalarField dummyField
(
    IOobject
    (
        "dummyField",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    mesh,
    dimensionedScalar("dummyScalar", dimless, 0.0)
);

forAll(dummyField,  cellI)          
    {                               
        const scalar x = C[cellI].x(); 
        const scalar y = C[cellI].y(); 
                             
        const scalar tmp0 = (5.0/4.0)*M_PI; 
        const scalar solution = 1000.0*Foam::sin(tmp0*y) + 1000.0*Foam::cos(tmp0*x) + 100000.0; 
                                                                 
        dummyField[cellI] = mag(solution - p[cellI]); 
    }
And finally computing the L2 error norm as:

Code:
const scalarField& V = mesh.V();  

Info << "Error is "    << Foam::sqrt( gSum(dummyField*dummyField*V)/gSum(V) )    << endl;
One would expect for the norm to be zero.. but the value differs depending on the value of writePrecision... The default value is 6, so if I proceed I will get a L2 value of 0.22. I only get a value of 0 with 17 decimal cases. I was under the impression that writePrecision would control the amount of decimal cases for output information of the field. But apparently it also has an effect on the input given.
theBananaTrick is offline   Reply With Quote

Old   February 16, 2022, 13:10
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
You need to adjust your expectations and/or find another way to handle the problem. The codeStream bits work by outputting to StringStream and then reparsing that to get tokens to populate the dictionary contents. These are the tokens that are actually used to populate the field.



Yes, you are correct to think that this sounds ugly!
olesen is offline   Reply With Quote

Old   April 7, 2022, 02:11
Default
  #3
Senior Member
 
TWB
Join Date: Mar 2009
Posts: 401
Rep Power: 19
quarkz is on a distinguished road
Hi,

I just found this out too after a week plus of debugging. Besides increasing precision for writePrecision and maybe timePrecision, there may also be a need to increase the read/write precision for reading/writing files:

Code:
while(MyReadFile.good() && (getline(MyReadFile, line)))
                        {
                                istringstream iss(line);

                                iss >> std::fixed >> std::setprecision(8) >> time_saved >> old_incoming_vel;

                        }

                        MyReadFile.close();
This is because default c++ precision is also 6. Hope this helps.
quarkz 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing CFD solution data in CFD POST Aveek Pal ANSYS 2 February 24, 2020 03:04
Effect Sloshing in a tank of water with CFD RobinReiban FLUENT 0 April 2, 2019 15:01
Steady state CFD in a solid - effect of Cp and density? Abhya Main CFD Forum 0 February 15, 2018 08:52
how to model polymer solution in CFD koraltp Main CFD Forum 0 November 16, 2017 01:23
confusion on how to write udf for density of a mixture solution in multiphase problem Hirakjyoti Fluent UDF and Scheme Programming 1 September 11, 2017 01:21


All times are GMT -4. The time now is 08:14.