CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Pre-Processing

Temperature average by codedFunctionObject

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 2 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 6, 2023, 13:31
Post Temperature average by codedFunctionObject
  #1
New Member
 
george
Join Date: Feb 2021
Posts: 14
Rep Power: 5
scfd is on a distinguished road
Hi,

I'm trying to get average value of temperature with additional statements, like if the Velocity magnitude is zero take that cell to account.

I've just 2 conditions.
  1. First one is the positional, check the cell z-position if it is between P1 and P2.
  2. Second one is the velocity magnitude.
If the cell satisfies the conditions, add the temperature value of that cell by Tm. Divide Tm by counter after the for loop.
Code:
	
temperatureAverage
{
    type coded;
    libs (utilityFunctionObjects);
    writeControl writeTime;
    name temp;
	
    codeWrite
    #{
	scalar P1 = 1.0;
	scalar P2 = 2.0;
	scalar Tm = 0;
	scalar TposAve = 0;
	scalar counter = 0;
        const scalarField& zPos= mesh().C().component(2);
        const volVectorField& U = mesh().lookupObject<volVectorField>("U");
	const volScalarField& T = mesh().lookupObject<volScalarField>("T");
	forAll(zPos,i)
	{
		if (zPos[i] >= P1 && zPos[i] <= P2)
		{
			if (U[i] > 0)
			{
				Tm += T[i];
				counter += 1;
			}
		}
	}
	TposAve = Tm/counter;
#};
}
ERROR:
Code:
/aveTest/system/controlDict.functions.temperatureAverage: In member function ‘virtual bool Foam::tempFunctionObject::write()’:
/aveTest/system/controlDict.functions.temperatureAverage:96:14: error: no match for ‘operator>’ (operand types are ‘const Foam::Vector<double>’ and ‘int’)
I attached the log file for further reading.

regards,
scfd
Attached Files
File Type: txt logbuoyantPimpleFoam.txt (21.0 KB, 0 views)
scfd is offline   Reply With Quote

Old   March 6, 2023, 20:51
Post
  #2
New Member
 
george
Join Date: Feb 2021
Posts: 14
Rep Power: 5
scfd is on a distinguished road
Edit:

This one is works but it can't make a log file in postProcessing folder.

Code:
temperatureAverage
{
type coded;
libs (utilityFunctionObjects);
writeControl writeTime;
name temp;

codeWrite
#{
	scalar P1 = 1.0;
	scalar P2 = 2.0;
	scalar Tm = 0;
	scalar TposAve = 0;
	scalar counter = 0;
	const scalarField& zPos= mesh().C().component(2);
	const volVectorField& U = mesh().lookupObject<volVectorField>("U");
	const volScalarField& T = mesh().lookupObject<volScalarField>("T");
	forAll(zPos,i)
	{
		if (zPos[i] >= P1 && zPos[i] <= P2)
		{
			if (sqrt(sqr(U[0][i])+sqr(U[1][i])+sqr(U[2][i])) > 0) //mag(U)[i]
			{
				Tm += T[i];
				counter += 1;
				//Info<< "Tm = " << Tm << endl;
				//Info<< "Counter = " << counter << endl;
			}
		}
	}
	TposAve = Tm/counter;
	Info<< "AVERAGE TEMPERATURE = " << TposAve << endl;
	//temp.write();
#};
}
regards,
scfd

Last edited by scfd; March 7, 2023 at 10:43.
scfd is offline   Reply With Quote

Old   March 9, 2023, 15:31
Post
  #3
New Member
 
george
Join Date: Feb 2021
Posts: 14
Rep Power: 5
scfd is on a distinguished road
Hi again,

Code runs correctly on buoyantPimpleFoam and returns the expected values, however it gives error on buoyantBoussinesSimpleFoam. There are only two difference on them. First case is transient and using boussinesq as EoS, the second one is steady-state and using directly boussinesq solver. I attached the log file.

What should I do to make it work in boussinesq solver as well?

ERROR:
Code:
    functionObjects::mag mag(U) writing field: mag(U)
[0] [1] ##0  0  Foam::error::printStack(Foam::Ostream&)Foam::error::printStack(Foam::Ostream&) at  at ??:?
??:?
[0] #1  Foam::sigFpe::sigHandler(int)[1] #1  Foam::sigFpe::sigHandler(int) at ??:?
[1] #2  ? at ??:?
[0] #2  ? in /lib64/libpthread.so.0
[1] #3  Foam::tempAveCodeFunctionObject::write() in /lib64/libpthread.so.0
[0] #3  Foam::tempAveCodeFunctionObject::write() at system/controlDict.functions.temperatureAverage:82
 at system/controlDict.functions.temperatureAverage:82
[0] #4  Foam::functionObjects::timeControl::write()[1] #4  Foam::functionObjects::timeControl::write() at ??:?
 at ??:?
[0] #5  Foam::functionObjectList::execute()[1] #5  Foam::functionObjectList::execute() at ??:?
[1] #6  Foam::Time::run() const at ??:?
[0] #6  Foam::Time::run() const at ??:?
[1] #7  Foam::Time::loop() at ??:?
[0] #7  Foam::Time::loop() at ??:?
 at ??:?
[1] #8  Foam::simpleControl::loop()[0] #8  Foam::simpleControl::loop() at ??:?
[1] #9   at ??:?
[0] #9  ?? at ??:?
[1] #10  __libc_start_main at ??:?
[0] #10  __libc_start_main in /lib64/libc.so.6
[1] #11   in /lib64/libc.so.6
[0] #11  ?? at ??:?
[82:291861] *** Process received signal ***
[82:291861] Signal: Floating point exception (8)
[82:291861] Signal code:  (-6)
[82:291861] Failing at address: 0x199000047415
 at ??:?
[82:291860] *** Process received signal ***
[82:291860] Signal: Floating point exception (8)
[82:291860] Signal code:  (-6)
[82:291860] Failing at address: 0x199000047414
[82:291861] [ 0] [82:291860] [ 0] /lib64/libpthread.so.0(+0xf370)[0x2b084f966370]
[82:291861] [ 1] /lib64/libpthread.so.0(raise+0x2b)[0x2b084f96623b]
[82:291861] [ 2] /lib64/libpthread.so.0(+0xf370)[0x2b297d50d370]
[82:291860] [ 1] /lib64/libpthread.so.0(raise+0x2b)[0x2b297d50d23b]
[82:291860] [ 2] /lib64/libpthread.so.0(+0xf370)[0x2b297d50d370]
/lib64/libpthread.so.0(+0xf370)[0x2b084f966370]
[82:291861] [ 3] [82:291860] [ 3] /run/dynamicCode/platforms/linux64GccDPInt32Opt/lib/libtempAveCode_5eeffd6dc79cd41527594d65478d16bbfc6b994f.so(_ZN4Foam25tempAveCodeFunctionObject5writeEv+0x1f0)[0x2b29a89216c0]
[82:291860] [ 4] /run/dynamicCode/platforms/linux64GccDPInt32Opt/lib/libtempAveCode_5eeffd6dc79cd41527594d65478d16bbfc6b994f.so(_ZN4Foam25tempAveCodeFunctionObject5writeEv+0x1f0)[0x2b087aec96c0]
[82:291861] [ 4] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZN4Foam15functionObjects11timeControl5writeEv+0x5e)[0x2b297c2887ee]
[82:291860] [ 5] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZN4Foam15functionObjects11timeControl5writeEv+0x5e)[0x2b084e6e17ee]
[82:291861] [ 5] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZN4Foam18functionObjectList7executeEv+0x2df)[0x2b297c27bcaf]
[82:291860] [ 6] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZN4Foam18functionObjectList7executeEv+0x2df)[0x2b084e6d4caf]
[82:291861] [ 6] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZNK4Foam4Time3runEv+0xdd)[0x2b297c29266d]
[82:291860] [ 7] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZNK4Foam4Time3runEv+0xdd)[0x2b084e6eb66d]
[82:291861] [ 7] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZN4Foam4Time4loopEv+0x13)[0x2b297c2928d3]
[82:291860] [ 8] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZN4Foam4Time4loopEv+0x13)[0x2b084e6eb8d3]
[82:291861] [ 8] /PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZN4Foam13simpleControl4loopEv+0x85)[0x2b29789f0565]
[82:291860] [ 9] buoyantBoussinesqSimpleFoam[0x435e0b]
[82:291860] [10] /lib64/libc.so.6(__libc_start_main+0xf5)[0x2b297d73bb35]
/PATH/openfoam/of_2112/v2112/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZN4Foam13simpleControl4loopEv+0x85)[0x2b084ae49565]
[82:291861] [ 9] buoyantBoussinesqSimpleFoam[0x435e0b]
[82:291861] [10] /lib64/libc.so.6(__libc_start_main+0xf5)[0x2b084fb94b35]
[82:291861] [11] [82:291860] [11] buoyantBoussinesqSimpleFoam[0x4387c1]
[82:291861] *** End of error message ***
buoyantBoussinesqSimpleFoam[0x4387c1]
[82:291860] *** End of error message ***
regards,
scfd
Attached Files
File Type: txt logbuoyantBoussinesqSimpleFoam.txt (56.8 KB, 1 views)
scfd is offline   Reply With Quote

Old   August 22, 2023, 08:25
Smile
  #4
New Member
 
Elena
Join Date: Aug 2023
Posts: 3
Rep Power: 2
ElenaJL00 is on a distinguished road
Please did you resolved your problem, I have the same issue

Last edited by ElenaJL00; August 27, 2023 at 17:41.
ElenaJL00 is offline   Reply With Quote

Old   August 22, 2023, 09:20
Default
  #5
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,708
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by ElenaJL00 View Post
Please did you resolved your problem, I have the same issue
Don't be surprised to see a segfault or floating point exception with the original code, especially when running in parallel. You need to use the overall values when calculating any average and also need to protect against division by zero. If you do not perform the reduction, the answer will not only be incorrect but there is most certainly going to be one or more processor domains with zero cells that match your selection criteria and they will provoke a divide by zero.
Here is a slightly updated version of the code. Note that we avoid extracting the z-component up front. That is adds extra memory overhead - much better to get the z component on a per-cell basis.
Code:
temperatureAverage 
{ 
    type coded; 
    libs (utilityFunctionObjects); 
    writeControl writeTime; 
    name temp; 

    codeWrite 
    #{ 
        const scalar P1 = 1.0; 
        const scalar P2 = 2.0; 

        const auto& cc = mesh().C(); 
        const auto& U = mesh().lookupObject<volVectorField>("U"); 
        const auto& T = mesh().lookupObject<volScalarField>("T"); 
         const label nCells = cc.size();

         scalar TposAvg = 0; 
         label count = 0;


        for (label celli = 0; celli < nCells; ++celli) 
        { 
            if 
            ( 
                (cc[celli].z() >= P1 && cc[celli].z() <= P2) 
              && mag(U[celli]) > 0 
            ) 
            { 
                TposAvg += T[celli]; 
                ++count; 
            } 
         }


        reduce(TposAvg, sumOp<scalar>()); 
        reduce(count, sumOp<label>()); 
   // Or:
        // sumReduce(TposAvg, count);
         
        if (count) 
        { 
            TposAvg = TposAvg/count; 
            Info<< "AVERAGE TEMPERATURE = " << TposAvg << endl; 
        } 
        else 
        { 
            Info<< "AVERAGE TEMPERATURE = " << none << endl; 
        } 
    #}; 
}

Probably makes more sense to have this type of code as 'execute()' instead of 'write()' since it is just reporting back values and not writing to disk.
scfd and ElenaJL00 like this.
olesen is offline   Reply With Quote

Reply

Tags
coded, coded function object, temperature average value

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
[openSmoke] libOpenSMOKE Tobi OpenFOAM Community Contributions 562 January 25, 2023 09:21
Converging Diverging Nozzle with dbnsTurbFoam Saleh Abuhanieh OpenFOAM Running, Solving & CFD 4 December 13, 2019 10:26
Area weighted average temperature vin95 Main CFD Forum 2 August 8, 2018 01:46
How can i export the average Temperature for many time steps from Fluent? benmel2006 FLUENT 1 April 5, 2017 13:32
average interior temperature less than the operating temperature Naher FLUENT 4 March 31, 2011 23:14


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