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

Convert templated class into vector

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 14, 2012, 04:50
Default Convert templated class into vector
  #1
Member
 
Join Date: Oct 2011
Posts: 37
Rep Power: 14
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: https://olesenm.github.io/
Posts: 1,689
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
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.
randolph likes this.
olesen is offline   Reply With Quote

Reply

Tags
convert, template, type, vector


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
how to set periodic boundary conditions Ganesh FLUENT 15 November 18, 2020 06:09
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
Errors running allwmake in OpenFOAM141dev with WM_COMPILE_OPTION%3ddebug unoder OpenFOAM Installation 11 January 30, 2008 20:30


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