CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   Vector field from scalars (https://www.cfd-online.com/Forums/openfoam/76002-vector-field-scalars.html)

Vadims.geza May 11, 2010 10:39

Vector field from scalars
 
Hello all!

I need to create vector field from three scalars. It can not be done by using vector operations in this case. I have got all scalar components VsX, VsY and VsZ.

volVectorField Vs
IOobject
(
"Vs",
runTime.timeName(),
mesh,
IOobject::NO_READ
),
mesh,
dimenstionedVector("variableName", dimensionSet(0,0,0,0,0,0,0), Foam::vector(VsX,VsY,VsZ))

it says
error: no matching function for call to 'Foam::Vector<double>::Vector(Foam::volScalarField &, Foam::volScalarField&, Foam::volScalarField&)'

So, please give me some advice how to do this, if it is possible!
Many thanks in advance!

novyno May 11, 2010 13:51

Hi,

you can read the vectorField initialized with dummy values (i.e zero value):

volVectorField Vs
(
IOobject
(
"Vs",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

do the same with the scalarField(s):

volScalarField VsX
(
IOobject
(
"VsX",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

etc...

and finanlly:

forAll( Vs.internalField(), i)
{
Vs.internalField()[i].component(vector::X) = Vsx.internalField()[i];
Vs.internalField()[i].component(vector::Y) = Vsy.internalField()[i];
Vs.internalField()[i].component(vector::Z) = Vsz.internalField()[i];
}
forAll( Vs.boundaryField(), boundaryI)
{
forAll( Vs.boundaryField()[boundaryI], i)
{
Vs.boundaryField()[boundaryI][i].component(vector::X) = Vsx.boundaryField()[boundaryI][i];
Vs.boundaryField()[boundaryI][i].component(vector::Y) = Vsy.boundaryField()[boundaryI][i];
Vs.boundaryField()[boundaryI][i].component(vector::Z) = Vsz.boundaryField()[boundaryI][i];
}
}

Of course if you already have the fields you don't have to read them! :-)

Sugajen May 21, 2017 17:14

Error
 
I did as you had suggested, but it throws an error saying "error: assignment of read-only location (&(& Ulu.Foam::GeometricField<Type, PatchField, GeoMesh>::internalField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>())->Foam::DimensionedField<Foam::Vector<double>, Foam::volMesh>::<anonymous>.Foam::Field<Foam::Vect or<double> >::<anonymous>.Foam::List<Foam::Vector<double> >::<anonymous>.Foam::UList<T>::operator[]<Foam::Vector<double> >(i))->Foam::Vector<double>::<anonymous>.Foam::VectorSpa ce<Form, Cmpt, Ncmpts>::component<Foam::Vector<double>, double, 3u>(0)
Ulu.internalField()[i].component(vector::X) = 0;"

also,

" error: assignment of read-only location (&(& Ulu.Foam::GeometricField<Type, PatchField, GeoMesh>::internalField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>())->Foam::DimensionedField<Foam::Vector<double>, Foam::volMesh>::<anonymous>.Foam::Field<Foam::Vect or<double> >::<anonymous>.Foam::List<Foam::Vector<double> >::<anonymous>.Foam::UList<T>::operator[]<Foam::Vector<double> >(i))->Foam::Vector<double>::<anonymous>.Foam::VectorSpa ce<Form, Cmpt, Ncmpts>::component<Foam::Vector<double>, double, 3u>(2)
Ulu.internalField()[i].component(vector::Z) = Jw.internalField()[i];
^
"

alexeym May 22, 2017 04:24

Hi all,

@Sugajen

In your initial attempt you tried to create vector from scalarFields. it is not easy.

Since you need volVectorField with default (calculated) boundary conditions, why not use something like:

Code:

const vector i(1, 0, 0);
const vector j(0, 1, 0);
const vector k(0, 0, 1);

volVectorField Vs("Vs", i*VsX + j*VsY + k*VsZ);

It seems that you even do not need AUTO_WRITE capability for the field (otherwise, you can use constructor from IOobject and volVectorField).

novyno's answer is correct for versions below 3.0.x. In 3 and 4 internalField is read-only, yet there is ref, which returns non-const reference.

Sugajen May 22, 2017 18:16

Thank you Alexey. It works!


All times are GMT -4. The time now is 03:42.