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

Convert templated class into vector

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

Reply
 
LinkBack Thread Tools Display Modes
Old   June 14, 2012, 04:50
Default Convert templated class into vector
  #1
Member
 
Join Date: Oct 2011
Posts: 36
Rep Power: 5
Peter Müller is on a distinguished road
Hello together

First sorry for creating a new thread but I could not find what I was looking for.

I'm creating a new boundary and the boundary should be build up as templated version so to make it usable for any kind of quantity. The problem now is that I would like to do a coordinate transform to the velocity.
Therefore I need to grab the internal field like

Field<Type> iField = this->internalField();

In a next step I wanted to create a vectorField if "Type" is vector

if(pTraits<Type>::rank ==1)
{
Field<vector> myField = iField;
}

As you may probably already notice this want work since iField is templated so it can't be given to a vector field since the compiler doesn't know that my if-statement only allows the operation if Type==vector.

Has anybody an idea how to circumvent this problem?

Many thanks
Peter
Peter Müller is offline   Reply With Quote

Old   June 15, 2012, 02:45
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: http://olesenm.github.io/
Posts: 777
Rep Power: 18
olesen will become famous soon enough
Quote:
Originally Posted by Peter Müller View Post
Hello together

First sorry for creating a new thread but I could not find what I was looking for.

I'm creating a new boundary and the boundary should be build up as templated version so to make it usable for any kind of quantity. The problem now is that I would like to do a coordinate transform to the velocity.
Therefore I need to grab the internal field like

Field<Type> iField = this->internalField();

In a next step I wanted to create a vectorField if "Type" is vector

if(pTraits<Type>::rank ==1)
{
Field<vector> myField = iField;
}

As you may probably already notice this want work since iField is templated so it can't be given to a vector field since the compiler doesn't know that my if-statement only allows the operation if Type==vector.

Has anybody an idea how to circumvent this problem?

Many thanks
Peter


You are correct. Since things are templated, you would normally not like that. What you need is a template specialization for the type vector that implements what you want. If you haven't worked with this before, it may take you a few attempts to get it going. The important points:
  • The template specialization itself must be defined within a namespace.
  • The template specialization must be seen before the generic template form.
A trivial example can be see in the OPENFOAM UList<T> implementation. Take a look at the [] operator (the const implementation).
From the UList.H:
Code:
 
inline const T& operator[](const label) const;
Within UListI.H, you will find the specialization and the generic form:

Code:
 
namespace Foam
{
    // Template specialization for bool
    template<>
    inline const bool& Foam::UList<bool>::operator[](const label i) const
    {
    ...
    }
} // end of namespace Foam
 
// const element access
template<class T>
inline const T& Foam::UList<T>::operator[](const label i) const
{
...
}

In your particular case, you can either specialize the entire method for various vector, or factor it out as a private method. In either case, you can view the specialization as being a compile-time version of an 'if' or a 'switch'.

I hope this helps you get going.
olesen is offline   Reply With Quote

Reply

Tags
convert, template, type, vector

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
how to set periodic boundary conditions Ganesh FLUENT 13 January 22, 2014 05:11
cast vector to Type jordi.muela OpenFOAM Programming & Development 0 May 31, 2012 03:42
UDF for wall slipping HFLUENT Fluent UDF and Scheme Programming 0 April 27, 2011 12:03
How to convert DRM protected music and movies to MP4/AVI/MOV/MP3/WMV/AAC... urutyerid6 ANSYS 0 April 22, 2010 04:52
Errors running allwmake in OpenFOAM141dev with WM_COMPILE_OPTION%3ddebug unoder OpenFOAM Installation 11 January 30, 2008 21:30


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