CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Initializing a template variable (http://www.cfd-online.com/Forums/openfoam-programming-development/95960-initializing-template-variable.html)

akidess January 9, 2012 04:29

Initializing a template variable
 
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];


gschaider January 9, 2012 15:35

Quote:

Originally Posted by akidess (Post 338409)
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

akidess January 10, 2012 03:38

Thanks a lot! :) If anyone wants to use the code as well, I pushed it to http://code.google.com/p/foamcalcex/


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