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

Initializing a template variable

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

Reply
 
LinkBack Thread Tools Display Modes
Old   January 9, 2012, 04:29
Default Initializing a template variable
  #1
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 919
Rep Power: 17
akidess will become famous soon enough
I'm extending the foamCalc tool to calculate a volume integral of a given field. The code in the foamCalcFunctions library is templated to deal with the various field types (scalar, vector, ...). My problem is that when I define a templated variable to store the result of the computations, I'm not sure how I can initialize the variable nicely to be zero (again scalar(0), vector(0,0,0), ...). My current solution is just to misuse a value from the field and subtract it again, but of course that's not very elegant. Can anyone help me with a nicer solution?

Code:
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;

    if (header.headerClassName() == fieldType::typeName)
    {
        Info<< "    Reading " << header.name() << endl;
        fieldType field(header, mesh);

        //Initialization
        dimensioned<Type> volInt ("volInt", field[0]);
        volInt -= field[0];
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
*Help define the OpenFOAM stackexchange Q&A site: http://area51.stackexchange.com/prop...oam-technology
akidess is offline   Reply With Quote

Old   January 9, 2012, 15:35
Default
  #2
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 3,912
Rep Power: 40
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by akidess View Post
I'm extending the foamCalc tool to calculate a volume integral of a given field. The code in the foamCalcFunctions library is templated to deal with the various field types (scalar, vector, ...). My problem is that when I define a templated variable to store the result of the computations, I'm not sure how I can initialize the variable nicely to be zero (again scalar(0), vector(0,0,0), ...). My current solution is just to misuse a value from the field and subtract it again, but of course that's not very elegant. Can anyone help me with a nicer solution?

Code:
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;

    if (header.headerClassName() == fieldType::typeName)
    {
        Info<< "    Reading " << header.name() << endl;
        fieldType field(header, mesh);

        //Initialization
        dimensioned<Type> volInt ("volInt", field[0]);
        volInt -= field[0];
pTraits<Type>::zero
gschaider is offline   Reply With Quote

Old   January 10, 2012, 03:38
Default
  #3
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 919
Rep Power: 17
akidess will become famous soon enough
Thanks a lot! If anyone wants to use the code as well, I pushed it to http://code.google.com/p/foamcalcex/
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
*Help define the OpenFOAM stackexchange Q&A site: http://area51.stackexchange.com/prop...oam-technology
akidess is offline   Reply With Quote

Reply

Thread Tools
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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
emag beta feature: charge density charlotte CFX 4 March 22, 2011 10:14
Initializing reference variable of type objectRegistry deji OpenFOAM 8 August 6, 2010 13:50
error in COMSOL:'ERROR:6164 Duplicate Variable' bhushas Main CFD Forum 1 May 30, 2008 04:35
Env variable not set gruber2 OpenFOAM Installation 5 December 30, 2005 05:27
Replace periodic by inlet-outlet pair lego CFX 3 November 5, 2002 21:09


All times are GMT -4. The time now is 01:50.