CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Any efficient way to set a field to ZERO or ONE without looping all cells?

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 2, 2022, 01:25
Talking Any efficient way to set a field to ZERO or ONE without looping all cells?
  #1
Senior Member
 
David Long
Join Date: May 2012
Location: Germany
Posts: 104
Rep Power: 13
keepfit is on a distinguished road
As the title says, without looping over all CFD cells, is there any way to set a scalar/vector field to a constant value?

For instance, if we want to reset some special Scalar/Vector volField to Zero at the beginning of every PISO loop, the straightforward method is:

Code:
for (int cell_id=0; cell_id<Number_AllCells; cell_id++)
 {
    Scalar_volField [cell_id] = 0.0;

    Vector_volField [cell_id].x() = 0.0;
    Vector_volField [cell_id].y() = 0.0;
    Vector_volField [cell_id].z() = 0.0;
 }
In my case, the number of CFD cells is about 300k~500k, the above method seems not very efficient. I checked the online code guide, did not find something like to assign zeroField() or oneField to existing volumetric fields:

Code:
Scalar_volField = zeroField();  // oneField();
Vector_volField = zeroField();  // oneField();
Any idea? I believe there must be some functions in OpenFoam similar to typical c++ vector: myVec = std::vector(myVec.size(), 0.0), which zeros the "myVector".
keepfit is offline   Reply With Quote

Old   February 2, 2022, 12:54
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,685
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by keepfit View Post
Any idea? I believe there must be some functions in OpenFoam similar to typical c++ vector: myVec = std::vector(myVec.size(), 0.0), which zeros the "myVector".

My first idea would be to look at the GeometricField header and see which assignment operators you find. Hint: around Lines 700 onwards you should find something.
olesen is offline   Reply With Quote

Old   February 2, 2022, 23:53
Question
  #3
Senior Member
 
David Long
Join Date: May 2012
Location: Germany
Posts: 104
Rep Power: 13
keepfit is on a distinguished road
Quote:
Originally Posted by olesen View Post
My first idea would be to look at the GeometricField header and see which assignment operators you find. Hint: around Lines 700 onwards you should find something.
Thanks for the reply. Could you please elaborate how to use these operators to assign constant fields? For static fvMesh, I think we could create some fields that does not change over time (e.g. constVolField), and we can always assign it to existing fields:

Code:
myVolField = constVolField;
Nevertheless, sometimes I need to do the interface refinement between different phases, in this case, how can we assign some `const` fields with dynamic mesh?


Code:
 // Member Operators  // line 724 in GeometricField.H 

        //- Return a const-reference to the dimensioned internal field
        //  Useful in the formulation of source-terms for FV equations
        inline const Internal& operator()() const;

        void operator=(const GeometricField<Type, PatchField, GeoMesh>&);
        void operator=(const tmp<GeometricField<Type, PatchField, GeoMesh>>&);
        void operator=(const dimensioned<Type>&);

        void operator==(const tmp<GeometricField<Type, PatchField, GeoMesh>>&);
        void operator==(const dimensioned<Type>&);

        void operator+=(const GeometricField<Type, PatchField, GeoMesh>&);
        void operator+=(const tmp<GeometricField<Type, PatchField, GeoMesh>>&);

        void operator-=(const GeometricField<Type, PatchField, GeoMesh>&);
        void operator-=(const tmp<GeometricField<Type, PatchField, GeoMesh>>&);

        void operator*=(const GeometricField<scalar, PatchField, GeoMesh>&);
        void operator*=(const tmp<GeometricField<scalar,PatchField,GeoMesh>>&);

        void operator/=(const GeometricField<scalar, PatchField, GeoMesh>&);
        void operator/=(const tmp<GeometricField<scalar,PatchField,GeoMesh>>&);

        void operator+=(const dimensioned<Type>&);
        void operator-=(const dimensioned<Type>&);

        void operator*=(const dimensioned<scalar>&);
        void operator/=(const dimensioned<scalar>&);
keepfit is offline   Reply With Quote

Old   February 3, 2022, 12:11
Default
  #4
Senior Member
 
Josh Williams
Join Date: Feb 2021
Location: Scotland
Posts: 112
Rep Power: 5
joshwilliams is on a distinguished road
Quote:
Originally Posted by keepfit View Post
As the title says, without looping over all CFD cells, is there any way to set a scalar/vector field to a constant value?
Have you tried multiplying the field by 0? Then if you want to set it to one you can add one? I have done this a few times as a lazy way of initialising fields in post-processing tools.
joshwilliams is offline   Reply With Quote

Old   February 3, 2022, 19:37
Default
  #5
Senior Member
 
David Long
Join Date: May 2012
Location: Germany
Posts: 104
Rep Power: 13
keepfit is on a distinguished road
Quote:
Originally Posted by joshwilliams View Post
Have you tried multiplying the field by 0? Then if you want to set it to one you can add one? I have done this a few times as a lazy way of initialising fields in post-processing tools.
Good idea, I will give it a try on dynamic mesh.
keepfit is offline   Reply With Quote

Reply

Tags
fields, zerofield()


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Foam::error::PrintStack almir OpenFOAM Running, Solving & CFD 91 December 21, 2022 04:50
[surface handling] Decomposing Faces after Extrude2DMesh with autoPatch JEBland OpenFOAM Meshing & Mesh Conversion 5 December 6, 2021 07:28
overPimpleDyMFoam rotating airfoil startup problems jantheron OpenFOAM Running, Solving & CFD 1 May 20, 2020 04:55
[blockMesh] Create internal faces as patch in blockMesh m.delta68 OpenFOAM Meshing & Mesh Conversion 14 July 12, 2018 14:43
Ansys Licence Serve on Ubuntu 16.04 LTS david.pasquale ANSYS 2 January 20, 2017 11:52


All times are GMT -4. The time now is 19:53.