CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   weighted average of a volScalarField (https://www.cfd-online.com/Forums/openfoam-programming-development/240036-weighted-average-volscalarfield.html)

Niazi December 8, 2021 05:09

weighted average of a volScalarField
 
Hi Foamers,

I am trying to read the pressure field from the mesh and then take a weighted average of pressure and using that I want to find the velocity on the inlet patch. to read the Pressure field I am using the following,

const volScalarField& pIn=db().lookupObject<volScalarField>("p");

then to take the weightedaverage
const volScalarField pAverage = pIn.weightedAverage(patch().boundaryMesh().mesh(). V());

and for velocity calculation

vectorField :: operator=(n_*pow(p1-pAverage)/rho,0.5));

but as I am new to OpenFOAM I don't understand how to properly implement that. I get the following error,


conversion from ‘Foam::dimensioned<double>’ to non-scalar type ‘const volScalarField’ {aka ‘const Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>’} requested
175 | const volScalarField pAverage = pIn.weightedAverage(patch().boundaryMesh().mesh(). V());

Can anyone please help me out here, I will be greatly thankful.

Niazi December 10, 2021 03:21

Thanks to Alexeym, I was able to figure out the problem.
Quote:

Originally Posted by alexeym
You call value method of the result of weightedAverage, i.e. pIn.weightedAverage(internalField().mesh().V()).va lue().

As I said pIn.mesh().C() is an array of cell centres, which are vectors, so if you call component method, you get component of a vector. If you want a value of a field, you use v() method.

This starts being personal consulting, which is not the aim of this forum. So, this is my last answer in private messages. Add you question in a forum post, so other people can benefit from answers.

Quote:

Originally Posted by Niazi
How I am supposed to call value(), like this?
scalar pAverage = pIn.value().weightedAverage(internalField().mesh() .V())
this gives me an error,

"
error: ‘const volScalarField’ {aka ‘const class Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>’} has no member named ‘value’
180 | scalar pAverage = pIn.value().weightedAverage(internalField().mesh() .V());

"
for cell value, I want to read the pressure (any volScalarField for generic) value at a specific cell say 500th, as pressure is a scalar field, why am I required to give a compoenent?


Quote:

Originally Posted by alexeym
Well, it says in almost plain English: result of weighted average has dimensions and cannot be assigned to simple scalar value. So either you make pAverage dimensionedScalar or call value() method of weighted average result. The second way would be easier, since you plan to use pAverage in non-dimensional way later.

pIn.mesh().C() is an array of cell centres, so with pIn.mesh().C()[500].component(0) you extract X component of centre vector of 500-th cell. What value you would like to read from a cell?

Quote:

Originally Posted by Niazi
Thank you so much for your response, Alexeym. I try to implement that but I am still confused, basically what I want is to read the volScalarField from the mesh, for that purpose I used the following,

const volScalarField& pIn=db().lookupObject<volScalarField>("p");

then to calculate the weighted average, I used the following

scalar pAverage = pIn.weightedAverage(internalField().mesh().V());

I get this error
"
error: cannot convert ‘Foam::dimensioned<double>’ to ‘Foam::scalar’ {aka ‘double’} in initialization
179 | scalar pAverage = pIn.weightedAverage(internalField().mesh().V());
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| Foam::dimensioned<double>

"
To be honest, I don't know what syntax I must use to find the weighted average, can you please write a generic syntax? Another thing I stuck at is to read the value from a certain cell, now as I am reading the volScalarField I am using the following,

const scalar pi=pIn.mesh().C()[500].component(0);

but I am confused, because as it is a scalar field why we define the component, otherwise it fails and gives the following error,

"error: conversion from ‘const Foam::Vector<double>’ to non-scalar type ‘const scalarField’ {aka ‘const Foam::Field<double>’} requested
181 | const scalarField pi=pIn.mesh().C()[i];"


I will be very thankful for your help. Thanks once again.

Quote:

Originally Posted by alexeym
Hi,

Weighted average is a scalar value (single number) and you try to assign this weigher average to a field.

Quote:

Originally Posted by Niazi
Hi Alexeym,

I hope you are in good health. I need your help in finding the weighted average of a volScalarField in writing a boundary condition. Here is my thread,

I am trying to read the pressure field from the mesh and then take a weighted average of pressure and using that I want to find the velocity on the inlet patch. to read the Pressure field I am using the following,

const volScalarField& pIn=db().lookupObject<volScalarField>("p");

then to take the weightedaverage
const volScalarField pAverage = pIn.weightedAverage(patch().boundaryMesh().mesh(). V());

and for velocity calculation

vectorField :: operator=(n_*pow(p1-pAverage)/rho,0.5));

but as I am new to OpenFOAM I don't understand how to properly implement that. I get the following error,


conversion from ‘Foam::dimensioned<double>’ to non-scalar type ‘const volScalarField’ {aka ‘const Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>’} requested
175 | const volScalarField pAverage = pIn.weightedAverage(patch().boundaryMesh().mesh(). V());

I will be very grateful for your help. Thanks for your time and consideration.

Regards,
Niazi








All times are GMT -4. The time now is 00:28.