Initializing a template variable

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

 January 9, 2012, 04:29 Initializing a template variable #1 Senior Member     Anton Kidess Join Date: May 2009 Location: Germany Posts: 1,265 Rep Power: 23 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 fieldType; if (header.headerClassName() == fieldType::typeName) { Info<< " Reading " << header.name() << endl; fieldType field(header, mesh); //Initialization dimensioned 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.

January 9, 2012, 15:35
#2
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,020
Rep Power: 43
Quote:
 Originally Posted by akidess 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 fieldType; if (header.headerClassName() == fieldType::typeName) { Info<< " Reading " << header.name() << endl; fieldType field(header, mesh); //Initialization dimensioned volInt ("volInt", field[0]); volInt -= field[0];```
pTraits<Type>::zero

 January 10, 2012, 03:38 #3 Senior Member     Anton Kidess Join Date: May 2009 Location: Germany Posts: 1,265 Rep Power: 23 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.

 Thread Tools Display Modes Linear Mode

 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 OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post charlotte CFX 4 March 22, 2011 10:14 deji OpenFOAM 8 August 6, 2010 13:50 bhushas Main CFD Forum 1 May 30, 2008 04:35 gruber2 OpenFOAM Installation 5 December 30, 2005 05:27 lego CFX 3 November 5, 2002 21:09

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