# New problem in version 4.0! The boundary field become read only!

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

July 4, 2016, 22:03
New problem in version 4.0! The boundary field become read only!
#1
New Member

Xiaoqiu HE
Join Date: Mar 2016
Location: Wuhan, China
Posts: 29
Rep Power: 10
I used to adopt following way in order to impose a complex fixedValue type BC:
1. Implement a program to manipulate the value of boundary field directly and write the field into file. The code is as follow:
Code:
```    const fvBoundaryMesh & thisBoundary = mesh.boundary(); //here I get the boundary of the mesh
forAll( thisBoundary, fvPatchID ) // go through all the patchs of the boundary
{
const fvPatch & thisPatch = thisBoundary[ fvPatchID ];
Info << "\n Imposing the boundary condition on the fvPatch of " <<
thisPatch.name() << endl;

const vectorField & position = thisPatch.Cf(); // get the cell face centers' positions
forAll( thisPatch, elmtID ) // imposing values on the centers of all the cell faces of this patch
{
const scalar & x = position[elmtID].component(0);
const scalar & y = position[elmtID].component(1);

//you can replace your function on the right side of the =
pS.boundaryField()[fvPatchID][elmtID] = -1 * Foam::exp(-1 * x) * Foam::cos( x );
pC.boundaryField()[fvPatchID][elmtID] = Foam::exp(-1 * x) * Foam::sin( x );

vS.boundaryField()[fvPatchID][elmtID].component(0) = -1 * Foam::exp(-1 * x) * Foam::cos( x );
vS.boundaryField()[fvPatchID][elmtID].component(1) = 2 * Foam::cos( x ) * Foam::sin( y ) ;

vC.boundaryField()[fvPatchID][elmtID].component(0) = Foam::exp(-1 * x) * Foam::sin( x );
vC.boundaryField()[fvPatchID][elmtID].component(1) = Foam::sin( x ) * Foam::cos( y );

}

}```
2. Then I open the file, change the BC type from "calculated" to "fixedValue"

3. After that, I can run my solver

But!!!
Now in new 4.0 version OpenFOAM, the boundary field is read only! When I compile the BC implement program, the compiler indicates that
Quote:
For information the detailed error is as follow:
Code:
```error: assignment of read-only location ‘(&(&(& u2.Foam::GeometricField<Type, PatchField, GeoMesh>::boundaryField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>())->Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>::Boundary::<anonymous>.Foam::FieldField<Foam::fvPatchField, Foam::Vector<double> >::<anonymous>.Foam::PtrList<Foam::fvPatchField<Foam::Vector<double> > >::<anonymous>.Foam::UPtrList<T>::operator[]<Foam::fvPatchField<Foam::Vector<double> > >(fvPatchID))->Foam::fvPatchField<Foam::Vector<double> >::<anonymous>.Foam::Field<Foam::Vector<double> >::<anonymous>.Foam::List<Foam::Vector<double> >::<anonymous>.Foam::UList<T>::operator[]<Foam::Vector<double> >(elmtID))->Foam::Vector<double>::<anonymous>.Foam::VectorSpace<Form, Cmpt, Ncmpts>::component<Foam::Vector<double>, double, 3u>(1)’
vC.boundaryField()[fvPatchID][elmtID].component(1) = 0.1*x*y```
What can I do for that? Can I have the full control over every single elements of fields in new 4.0 OF?
Thanks!

 July 5, 2016, 06:50 #2 Senior Member   Hassan Kassem Join Date: May 2010 Location: Germany Posts: 242 Rep Power: 18 I think it is related to this commit [LINK] kittychunk, elmo555, Democritus and 4 others like this. __________________ @HIKassem | HassanKassem.me

July 6, 2016, 02:10
#3
New Member

Quinn Reynolds
Join Date: Jun 2014
Posts: 7
Rep Power: 12
Quote:
 Originally Posted by hk318i I think it is related to this commit [LINK]
This solved the problem for me.

The summary version is that you need to replace instances of "boundaryField()" and "internalField()" with "boundaryFieldRef()" and "internalFieldRef()" if you want to modify them in your code. The non-"Ref" calls are const now.

Update

Seems like internalField and internalFieldRef have been replaced by direct calls to the field variable. Eg: U.internalField() becomes U() for a const reference to the internal field, or U.ref() for non-const access. See associated bug report.

Last edited by kittychunk; July 7, 2016 at 01:54. Reason: Additional info from bug report

July 7, 2016, 01:18
#4
New Member

Xiaoqiu HE
Join Date: Mar 2016
Location: Wuhan, China
Posts: 29
Rep Power: 10
Quote:
 Originally Posted by hk318i I think it is related to this commit [LINK]
Thank you very much!

July 7, 2016, 01:20
#5
New Member

Xiaoqiu HE
Join Date: Mar 2016
Location: Wuhan, China
Posts: 29
Rep Power: 10
Quote:
 Originally Posted by kittychunk This solved the problem for me. The summary version is that you need to replace instances of "boundaryField()" and "internalField()" with "boundaryFieldRef()" and "internalFieldRef()" if you want to modify them in your code. The non-"Ref" calls are const now.
Thank you very much! My problem is solved~!

August 17, 2016, 08:53
#6
Member

Zhiheng Wang
Join Date: Mar 2016
Posts: 72
Rep Power: 10
Quote:
 Originally Posted by Democritus Thank you very much! My problem is solved~!
Hi my createField.H has defination of T as

const volScalarField& T = thermo.T();
I want to impliment my code on boundaryCondition correction as

forAll(T.boundaryField()[patchID],i)
{

T.boundaryField)([patchID][i] = T.boundaryField()[patchID][i]-DT;
------------
-------------------------- (some code for spoces) ........
T.write();

}

I am getting following error

error: assignment of read-only location ‘(&(&(& T)->Foam::GeometricField<Type, PatchField, GeoMesh>::boundaryField<double,.....

How to resolve this condition I want to correct the temperature T with some differet function based on spices flux. please help

 May 23, 2017, 19:12 boundaryFieldRef does not affect the actual field #7 Member   Sugajen Join Date: Jan 2012 Location: Tempe, USA Posts: 52 Rep Power: 14 Hi all, I had a similar problem of read-only and added Ref to the boundaryField. But the values that I assign are not reflected on the output. Where am I going wrong ? Code: ``` label upMem = mesh.boundaryMesh().findPatchID("upperMembrane"); forAll( U.boundaryFieldRef()[upMem], i) { U.boundaryFieldRef()[upMem][i].component(vector::X) = 0; U.boundaryFieldRef()[upMem][i].component(vector::Y) = 0; U.boundaryFieldRef()[upMem][i].component(vector::Z) = Jw.boundaryFieldRef()[upMem][i]; }```

 June 5, 2017, 06:27 #8 Member   Zhiheng Wang Join Date: Mar 2016 Posts: 72 Rep Power: 10 Store script in some file "patch.H" and include file as #include "patch.H" in pEqn.H before U.correctBoundary() This will reflect effect of code in boundary Sent from my Lenovo K50a40 using CFD Online Forum mobile app

 June 5, 2017, 21:10 #9 Member   Sugajen Join Date: Jan 2012 Location: Tempe, USA Posts: 52 Rep Power: 14 Thank you Zhiheng! It worked after I changed the boundary conditions to (0.0, 0.0, 0.0) instead of noSlip. Looks like noSlip overwrites whatever we do!

 June 6, 2017, 04:43 #10 Member   Zhiheng Wang Join Date: Mar 2016 Posts: 72 Rep Power: 10 Yes you need to write type fixedValue ; Uniform (0.0 0.0 0.0); Sent from my Lenovo K50a40 using CFD Online Forum mobile app Sugajen likes this.

April 11, 2020, 09:58
#11
Member

alexander thierfelder
Join Date: Dec 2019
Posts: 71
Rep Power: 6
Quote:
 Originally Posted by kittychunk This solved the problem for me. The summary version is that you need to replace instances of "boundaryField()" and "internalField()" with "boundaryFieldRef()" and "internalFieldRef()" if you want to modify them in your code. The non-"Ref" calls are const now. Update Seems like internalField and internalFieldRef have been replaced by direct calls to the field variable. Eg: U.internalField() becomes U() for a const reference to the internal field, or U.ref() for non-const access. See associated bug report.
I now searched a whole day for this. For me it is nearly impossible to get solutions for those problems by myself. Does anybody has an advice how to find those problems that occure by changes in the "openfoam language"? I mean I read many codes where
Code:
`.boundaryField()`
worked fine, and I got nearly crazy when it did not work for me. What is the best workflow to find the root of the problem in openfoam? I mean, I would never have assumed that they changed the functionality of ".boundaryField()" in the src code.

 Tags boundary condition, boundary field