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

Initializing a template variable

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 9, 2012, 03:29
Default Initializing a template variable
  #1
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Germany
Posts: 1,377
Rep Power: 29
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.
akidess is offline   Reply With Quote

Old   January 9, 2012, 14:35
Default
  #2
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
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, 02:38
Default
  #3
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Germany
Posts: 1,377
Rep Power: 29
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.
akidess 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
emag beta feature: charge density charlotte CFX 4 March 22, 2011 09:14
Initializing reference variable of type objectRegistry deji OpenFOAM 8 August 6, 2010 13:50
error in COMSOL:'ERROR:6164 Duplicate Variable' bhushas COMSOL 1 May 30, 2008 04:35
Env variable not set gruber2 OpenFOAM Installation 5 December 30, 2005 04:27
Replace periodic by inlet-outlet pair lego CFX 3 November 5, 2002 20:09


All times are GMT -4. The time now is 21:51.