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/)
-   -   Accessing a vector in a volVectorField by cell label (https://www.cfd-online.com/Forums/openfoam-programming-development/86035-accessing-vector-volvectorfield-cell-label.html)

andrewryan March 12, 2011 16:50

Accessing a vector in a volVectorField by cell label
 
Given a volVectorField how can I access the vector corresponding to a given cell label? Simple question, but I couldn't figure it out so far. :confused: For example I want to access the value of U at a boundary as defined in 0/U how can I do that?

Also as far as I can see the underlying data structure of volVectorField is a linked list, is that correct? I don't understand how the mapping of the cell labels to the physical data such as U and V works.

thx for any help!

ngj March 12, 2011 17:43

Good evening

Let me try answering you questions, as I understand them. Accessing an internal value is done as:

Code:

label celli(100);
vector uCell = U[celli];

This is the easy part. Knowing the cell label you can directly get the data, as it is always internal.

To access the values on a boundary, you need to get a reference to the boundary field. This is done in the following way:

Code:

label patchID = mesh.boundaryMesh().whichPatchID("myBoundaryName");
vectorField & Uw = U.boundaryField()[patchID];

In the above, Uw is a list of velocities which has the same length as the number of faces on that specific boundary. To access the velocity component you can do it in two ways. The first only works on cell level:

Code:

scalar u = U[celli].x();
scalar v = U[celli].y();
scalar w = U[celli].z();

and the other method allows you to access the entire field

Code:

scalarField u = U.component(0);
scalarField v = U.component(1);
scalarField w = U.component(2);

The latter method does only return the velocity components of the internal values, thus if you need the boundary values merely replace U with Uw in the last expression.

Best regards

Niels

andrewryan March 13, 2011 05:53

thx a lot for you quick answer :)

ICL January 15, 2012 06:48

Accessing vector components
 
Hi guys,

I am wondering if you can help me on the following problem:

The velocity vector components can be accessed using U.componet(0), U.componet(1), U.componet(2). I intend to add a constant to velocity Y component during the iteration. Initially I tried:

U.component(1) += constant;

The outcome shows that (Obviously) U has not been changed. Is it possible to define constant as a vector and add it to the velocity vector? how should it be written?

ali.m.1 April 18, 2016 12:45

Hi All

Thanks for the tips. Is there any way to call the cell normal component? My cells lie in the x-y-z plane at the moment, but I want my code to work with any angled cells too. I want to extract the velocity component through a boundary face normal.
Any help is appreciated.

regards

Ali

ali.m.1 April 19, 2016 09:32

I solved my problem. I used the dot product on the velocity vector and the direction vector.
Quote:

vectorField normalSlaveVector = mesh.Sf().boundaryField()[slave];
Where slave is the patch name. Hope this helps anyone else.

PavlosAlx April 21, 2016 07:51

Quote:

Originally Posted by ICL (Post 339361)
Hi guys,

I am wondering if you can help me on the following problem:

The velocity vector components can be accessed using U.componet(0), U.componet(1), U.componet(2). I intend to add a constant to velocity Y component during the iteration. Initially I tried:

U.component(1) += constant;

The outcome shows that (Obviously) U has not been changed. Is it possible to define constant as a vector and add it to the velocity vector? how should it be written?

You can simply do

scalar constValue = ... ;

vector const(constValue, constValue, constValue);

U[cellLabel] += const;

Elham June 8, 2018 05:48

Quote:

Originally Posted by PavlosAlx (Post 596090)
You can simply do

scalar constValue = ... ;

vector const(constValue, constValue, constValue);

U[cellLabel] += const;


Hello everyone,

My question is vice versa. How can I create volVectorField from a vector?
I have tried the follwong:

Code:

// 
    //volVectorField gradAlpha(fvc::grad(alpha1));
    volVectorField gradAlpha(pos(alpha1)*fvc::grad(alpha1));

    const dimensionedScalar deltaN = 1e-8/pow(average(mixture.U().mesh().V()), 1.0/3.0);

    // Face unit interface normal
    volVectorField nHatfv(gradAlpha/(mag(gradAlpha) + deltaN));

    volVectorField Vr(mDotU / rho * nHatfv) ;

label celli;
const cellList& Cells = mesh.cells(); //list of all cell IDs
const faceList& faces = mesh.faces(); //list of all face IDs
for(celli = 0; celli < mesh.nCells(); celli++)
{
    if(alpha1[celli] > 0 && alpha1[celli] < 1)
    {   
        vector um[celli]=U[celli]+Vr[celli];
    }
    else
    {
        vector um[celli]=U[celli];
    }
}

  volVectorField U1
    (
        IOobject
        (
            "U1",
            runTime.timeName(),
            mesh,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh,
        dimensionedScalar ("U1",dimVelocity,1)
    );


    volVectorField Um = um * U1 ;

But the following error comes up:

Code:

UEqn.H:49:5: error: no matching function for call to ‘Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>::GeometricField(Foam::IOobject, Foam::dynamicFvMesh&, Foam::dimensionedScalar)’
Cheers,

Elham


All times are GMT -4. The time now is 04:30.