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

Issue with mesh().C()

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By Tobermory
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 16, 2021, 08:55
Default Issue with mesh().C()
  #1
New Member
 
Claudio Contarini
Join Date: Apr 2021
Posts: 2
Rep Power: 0
ClaudioC is on a distinguished road
Hello Foamers!
I'm currently trying to implement a few lines of code to be inserted in fvOptions to generate a non uniform heat source. The geometry i'm working with is a tube along z direction that is running the chtMultiRegionFoam solver on 4 parallel processors.
I'm currently stuck due to the implementation of the following lines of code:



Quote:
energySource
{
type scalarCodedSource;
active true;
name generation;
selectionMode cellSet;
cellSet tube;
fields (e);

scalarCodedSourceCoeffs
{
selectionMode cellSet;
cellSet tube;
fields (e);
name generation;


codeInclude
#{
#include <IFstream.H>
#include <stdio.h>
#include <iostream>
#};

codeCorrect
#{
Pout<< "**codeCorrect**" << endl;
#};

codeAddSup
#{
const Time& time = mesh().time();
const scalarField& V = mesh_.V();

scalarField& heSource = eqn.source();
// Retrieve the x component of the cell centres
const scalarField& cellx = mesh().C().component(0);
// Retrieve the y component of the cell centres
const scalarField& celly = mesh().C().component(1);

const scalarField& cell10 = mesh().C().component(10);
const volVectorField& cell = mesh().C();

cout<<"The Value of 'cellx' is "<<cellx[10000];
cout<<"The Value of 'cell10' is "<<cell10 [10000];
cout<<"The Value of 'celly' is "<<celly [10000];
cout<<" cell 0 is "<< cell[10000][0];
cout<<" cell 1 is "<< cell[10000][1];
cout<<" cell 2 is "<< cell[10000][2];
cout<<" cell 3 is "<< cell[10000][3];
cout<<" cell 10 is "<< cell[10000][10];

}#
codeContrain
#{
Pout<< "**codeConstrain**" << endl;
#};
codeSetValue
#{
Pout<< "**codeSetValue**" << endl;
#};

I would expect cellx and celly to have different values, but any selected point posses the same coordinates in x and y. In addition, I would expect mesh().C() to possess only 3 components (the x,y and z coordinates of the cell centers) but I'm obtaining the same results for any component selected (in the results below the component(10) is shown)



Quote:
The Value of 'cellx' is -0.0259017The Value of 'cell10' is -0.0259017The Value of 'celly' is -0.0259017 cell 0 is 0.0501623 cell 1 is -0.0258483 cell 2 is 0.807492 cell 3 is 0.0528021 cell 10 is -0.0259017[3] **codeSetValue**
The Value of 'cellx' is 0.038028The Value of 'cell10' is 0.038028The Value of 'celly' is 0.038028 cell 0 is 0.0410684 cell 1 is 0.0380552 cell 2 is 0.054488 cell 3 is 0.0380504 cell 10 is 0.038028[0] **codeSetValue**
The Value of 'cellx' is 0.00781293The Value of 'cell10' is 0.00781293The Value of 'celly' is 0.00781293 cell 0 is 0.0583116 cell 1 is 0.00139837 cell 2 is 0.306497 cell 3 is 0.0554213 cell 10 is 0.00781293[1] **codeSetValue**
The Value of 'cellx' is 0.00773194The Value of 'cell10' is 0.00773194The Value of 'celly' is 0.00773194 cell 0 is -0.0558103 cell 1 is 0.00779338 cell 2 is 0.558712 cell 3 is -0.0582097 cell 10 is 0.00773194[2] **codeSetValue**

Any ideas on what I could do to solve this?
ClaudioC is offline   Reply With Quote

Old   April 17, 2021, 07:26
Default
  #2
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 668
Rep Power: 14
Tobermory will become famous soon enough
Instead of the components method, just use the index, eg:

Code:
    func1
    {
        libs            ( "libutilityFunctionObjects.so" );
        type            coded;
        name            test;
        writeControl    timeStep;
        writeInterval   1;
        codeWrite       #{
            float  value(-0.4);
            int intValue(value);
            bool boolValue(value);
            bool boolIntValue(intValue);
            Info << "Cell[100] C  = " << mesh().C()[100]
                 << "  Cell[100] Cx = " << mesh().C()[100][0]
                 << "  Cell[100] Cy = " << mesh().C()[100][1] << endl;
        #};
    }
works nicely:
Quote:
Cell[100] C = (0.125 -4.625 -4.875) Cell[100] Cx = 0.125 Cell[100] Cy = -4.625
Tobermory is offline   Reply With Quote

Old   April 17, 2021, 07:30
Default
  #3
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 668
Rep Power: 14
Tobermory will become famous soon enough
... as does:

Code:
    func1
    {
        libs            ( "libutilityFunctionObjects.so" );
        type            coded;
        name            test;
        writeControl    timeStep;
        writeInterval   1;
        codeWrite       #{
            float  value(-0.4);
            int intValue(value);
            bool boolValue(value);
            bool boolIntValue(intValue);
            Info << "Cell[100] C  = " << mesh().C()[100]
                 << ",  Cell[100] Cx = " << mesh().C()[100][0]
                 << ",  Cell[100] Cy = " << mesh().C()[100][1] << endl;
            Info << "Cell[100] C  = " << mesh().C()[100]
                 << ",  Cell[100] Cx = " << mesh().C()[100].component(0)
                 << ",  Cell[100] Cy = " << mesh().C()[100].component(1) << endl;
        #};
    }
Quote:
Cell[100] C = (0.125 -4.625 -4.875), Cell[100] Cx = 0.125, Cell[100] Cy = -4.625
Cell[100] C = (0.125 -4.625 -4.875), Cell[100] Cx = 0.125, Cell[100] Cy = -4.625
i.e. check your syntax. Good luck!
olesen likes this.
Tobermory is offline   Reply With Quote

Old   April 18, 2021, 03:57
Default
  #4
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 ClaudioC View Post
Hello Foamers!
I'm currently trying to implement a few lines of code to be inserted in fvOptions to generate a non uniform heat source. The geometry i'm working with is a tube along z direction that is running the chtMultiRegionFoam solver on 4 parallel processors.
I'm currently stuck due to the implementation of the following lines of code:






I would expect cellx and celly to have different values, but any selected point posses the same coordinates in x and y. In addition, I would expect mesh().C() to possess only 3 components (the x,y and z coordinates of the cell centers) but I'm obtaining the same results for any component selected (in the results below the component(10) is shown)






Any ideas on what I could do to solve this?
Your syntax is so completely wrong in so many places. Look at what Tobermory suggested. With your [] syntax for obtaining component 10?? of the vector. There are only three components, so you are slicing into some other memory location.
olesen is offline   Reply With Quote

Old   April 18, 2021, 04:14
Default
  #5
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 Tobermory View Post
... as does:

Code:
    func1
    {
        libs            ( "libutilityFunctionObjects.so" );
        type            coded;
        name            test;
        writeControl    timeStep;
        writeInterval   1;
        codeWrite       #{
            float  value(-0.4);
            int intValue(value);
            bool boolValue(value);
            bool boolIntValue(intValue);
            Info << "Cell[100] C  = " << mesh().C()[100]
                 << ",  Cell[100] Cx = " << mesh().C()[100][0]
                 << ",  Cell[100] Cy = " << mesh().C()[100][1] << endl;
            Info << "Cell[100] C  = " << mesh().C()[100]
                 << ",  Cell[100] Cx = " << mesh().C()[100].component(0)
                 << ",  Cell[100] Cy = " << mesh().C()[100].component(1) << endl;
        #};
    }

i.e. check your syntax. Good luck!
Good solution. For additional clarity I would always use the vector accessors x(), y(), z() - which also happen to be shorter to type.
People should also be aware that the components method on the volume field will return a tmp scalarField. So binding to a const-reference is dubious. And unless you need these as an entire field, also wasteful of memory and slower than directly accessing the components of individual elements.
Slightly reworked:

Code:
    func1
    {
        name test;
        type coded;
        libs ( "libutilityFunctionObjects.so" );

        codeWrite
       #{
            const auto& cc = mesh().C();

            Info << "Cell[100] C  = " << cc[100]
                 << ",  Cell[100] Cx = " << cc[100].x()
                 << ",  Cell[100] Cy = " << cc[100].y() << nl;
        #};
    }
Tobermory likes this.
olesen is offline   Reply With Quote

Old   April 18, 2021, 12:17
Default
  #6
New Member
 
Claudio Contarini
Join Date: Apr 2021
Posts: 2
Rep Power: 0
ClaudioC is on a distinguished road
Thank you very much Tobermory. I was actually thinking to employ the components as entire fields, as Olesen suggested, but i managed to rewrite my code so that i can employ a single
Quote:
mesh().C()[i].component(0)
in a for cycle and obtain the result i needed.

Thank you all guys
ClaudioC is offline   Reply With Quote

Reply


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
Issue on zeroGradient BC for system rotation magnushaese OpenFOAM Running, Solving & CFD 2 January 28, 2022 13:43
rhoCentralFoam inviscid airfoil issue (Foam::error::printStack(Foam::Ostream&) at ??) kmkb21 OpenFOAM Running, Solving & CFD 1 March 1, 2018 01:07
Prism Layer - Core Mesh Boundary Issue InfernoxCJC STAR-CCM+ 7 November 21, 2017 15:46
foamToTecplot360 issue with multiregion solvers manuc OpenFOAM Post-Processing 2 November 21, 2016 13:51
Pressure boundary condition issue Vijay FLUENT 0 April 6, 2012 13:35


All times are GMT -4. The time now is 18:17.