# Temperature average by codedFunctionObject

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

March 6, 2023, 13:31
Temperature average by codedFunctionObject
#1
New Member

george
Join Date: Feb 2021
Posts: 14
Rep Power: 5
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
 logbuoyantPimpleFoam.txt (21.0 KB, 0 views)

 March 6, 2023, 20:51 #2 New Member   george Join Date: Feb 2021 Posts: 14 Rep Power: 5 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("U"); const volScalarField& T = mesh().lookupObject("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.

March 9, 2023, 15:31
#3
New Member

george
Join Date: Feb 2021
Posts: 14
Rep Power: 5
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] #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)
at ??:?
[82:291860] *** Process received signal ***
[82:291860] Signal: Floating point exception (8)
[82:291860] Signal code:  (-6)
[82:291861] [ 0] [82:291860] [ 0] /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
 logbuoyantBoussinesqSimpleFoam.txt (56.8 KB, 1 views)

 August 22, 2023, 08:25 #4 New Member   Elena Join Date: Aug 2023 Posts: 3 Rep Power: 2 Please did you resolved your problem, I have the same issue Last edited by ElenaJL00; August 27, 2023 at 17:41.

August 22, 2023, 09:20
#5
Senior Member

Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,708
Rep Power: 40
Quote:
 Originally Posted by ElenaJL00 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.

 Tags coded, coded function object, temperature average value