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/)
-   -   Update all cells in volVectorField from my BC (https://www.cfd-online.com/Forums/openfoam-programming-development/198792-update-all-cells-volvectorfield-my-bc.html)

Rojj February 18, 2018 05:20

Update all cells in volVectorField from my BC
 
Hi,

I would like to update all cell values from my boundary condition. This is what I have done so far

Code:

const volVectorField& U = db().lookupObject<volVectorField>("U");
Info << "U(164): " << U.internalField()[164].x() << nl << endl;

This works, so I can access cell values. Then I try to extend the above to all cells:

Code:

forAll(U.internalField(), cellID)
{
  U.internalField()[cellID].x() = 1.2 * U.internalField()[cellID].x();
  U.internalField()[cellID].y() = 1.2 * U.internalField()[cellID].y();
  U.internalField()[cellID].z() = 1.2 * U.internalField()[cellID].z();
}

but the compiler complains with

Code:

error: assignment of read-only location
I believe that I have to use a specific mutator rather than simple assignment, but I am struggling to find it in the of source code.

Any advice?

Rojj February 20, 2018 13:00

I have also tried

Code:

U.internalField()[cellID].x().value() = ...
but I still get an error.

Any hint?

metalfox February 21, 2018 04:50

You are trying to modify a const reference to the U field. Remove the const keyword.

akabat March 6, 2018 11:08

Hello Ruggiero,
could you solve the issue? In my case with version 17.12 I have to const cast the field (I know it is not the best solution, but at least it works) and modify the fields with using the primitiveFieldRef

const volVectorField& U = db().lookupObject<volVectorField>("U");
velVectorField& Umod = const_cast<volVectorField&>(U);
Umod.primitiveFieldRef()[cellID] = ...

Regards
Alex


All times are GMT -4. The time now is 13:43.