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

use of pointers in openfoam

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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 27, 2020, 04:48
Default use of pointers in openfoam
  #1
New Member
 
kenan
Join Date: Sep 2012
Posts: 9
Rep Power: 10
kcengiz is on a distinguished road
Hi all,
Because I am new to C++ programming and the OpenFOAM templates and tools, I have a rather simple question. I need to collect extra data for postprocessing (via fieldAverage) in every time step.



Code:
        //Calculate gradU and distribute to scalar Fields
        gradU = fvc::grad(U);

         gradux = gradU.component(tensor::XX);
         graduy = gradU.component(tensor::XY);
         graduz = gradU.component(tensor::XZ);
 
         gradvx = gradU.component(tensor::YX);
         gradvy = gradU.component(tensor::YY);
         gradvz = gradU.component(tensor::YZ);
 
         gradwx = gradU.component(tensor::ZX);
         gradwy = gradU.component(tensor::ZY);
         gradwz = gradU.component(tensor::ZZ);
This works fine, but is also stupid because there is a copying operation at each time step (as well as waste of memory). gradux etc are declared as scalar fields,


Code:
     volScalarField gradux
      (
         IOobject
         (
             "gradux",
             runTime.timeName(),
             mesh,
             IOobject::NO_READ,
             IOobject::NO_WRITE
         ),
         mesh, dimensionedScalar("gradux", dimensionSet(0,0,-1,0,0,0,0), 0.0)
      );
etc., because fieldAverage function object cannot work with tensors. Is there an easy way to use pointers instead?


Thanks for the help!
kcengiz is offline   Reply With Quote

Old   October 29, 2020, 17:30
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: http://olesenm.github.io/
Posts: 1,144
Rep Power: 28
olesen will become famous soon enougholesen will become famous soon enough
Quote:
Originally Posted by kcengiz View Post
Code:
        //Calculate gradU and distribute to scalar Fields
        gradU = fvc::grad(U);

         gradux = gradU.component(tensor::XX);
         graduy = gradU.component(tensor::XY);
         graduz = gradU.component(tensor::XZ);
 
         gradvx = gradU.component(tensor::YX);
         gradvy = gradU.component(tensor::YY);
         gradvz = gradU.component(tensor::YZ);
 
         gradwx = gradU.component(tensor::ZX);
         gradwy = gradU.component(tensor::ZY);
         gradwz = gradU.component(tensor::ZZ);
This works fine, but is also stupid because there is a copying operation at each time step (as well as waste of memory). gradux etc are declared as scalar fields,
This probably reflects my first impressions as well, however you have to dig deeper into what is really happening. Without saying "read the docs" in a bad sense, you really do need to read the docs to see how wasteful /non-wasteful it is (needs a bit of digging). The component() method returns a 'tmp', which masks a useful reference or allocated pointer duality. In the case of component(), a pointer to a field is allocated. However, in the next step the assignment of this tmp field to a field results in a transfer of its content to the field and deletion of the pointer. The addressed range of the field is transferred without copying.

Later, when you want to use any of these fields as a volField, they should be wrapped with a std::move() to get move semantics on construction. Of course you need to do that yourself, the compiler and class definitions won't do it for you.
olesen is offline   Reply With Quote

Reply

Tags
field average, pointers, postprocessing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
OpenFOAM Training, London, Chicago, Munich, Sep-Oct 2015 cfd.direct OpenFOAM Announcements from Other Sources 2 August 31, 2015 14:36
OpenFOAM Foundation Releases OpenFOAM v2.3.0 opencfd OpenFOAM Announcements from OpenFOAM Foundation 3 December 23, 2014 04:43
Suggestion for a new sub-forum at OpenFOAM's Forum wyldckat Site Help, Feedback & Discussions 20 October 28, 2014 10:04
Frequently Asked Questions about Installing OpenFOAM wyldckat OpenFOAM Installation 0 January 1, 2014 20:21
Cross-compiling OpenFOAM 1.7.0 on Linux for Windows 32 and 64bits with Mingw-w64 wyldckat OpenFOAM Announcements from Other Sources 3 September 8, 2010 07:25


All times are GMT -4. The time now is 14:40.