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

newbie question - simple pTraits

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

Like Tree4Likes
  • 1 Post By wyldckat
  • 3 Post By ngj

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 23, 2011, 20:46
Default newbie question - simple pTraits
  #1
ans
New Member
 
Alan N. Sayre
Join Date: May 2009
Posts: 3
Rep Power: 16
ans is on a distinguished road
I want to compile the pTraits test routine using just command line e.g.

g++ pTraitsTest.C

I get:

gcc version 4.3.4 20090804 (release) 1 (GCC)

$ g++ pTraitsTest.C
pTraits.H: In instantiation of `Foam:Traits<double>':
pTraitsTest.C:42: instantiated from here
pTraits.H:52: error: base type `double' fails to be a struct or class type
pTraitsTest.C: In function `int main()':
pTraitsTest.C:42: error: `typeName' is not a member of `Foam:Traits<double>'
ansayre@ansayre2m4 /cygdrive/d/dev/OpenFOAM/OpenFOAM-1.7.1/gcc/pTraits

I defined WM_DP. What else do I need to do?
ans is offline   Reply With Quote

Old   April 24, 2011, 11:19
Default
  #2
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,974
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Greetings Alan,

Newbie? You're using Cygwin!

You need OpenFOAM to be already built under your development environment. If that has already been achieved, simply use this command:
Code:
wmake
It will place the binary in the folder outputted by the following command:
Code:
echo $FOAM_USER_APPBIN
It's not advisable to try and build these OpenFOAM things manually, since they have tons of dependencies!

Best regards,
Bruno
__________________

Last edited by wyldckat; April 25, 2011 at 18:48. Reason: typo: "You're" not "your"...
wyldckat is offline   Reply With Quote

Old   February 5, 2014, 12:00
Default
  #3
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
Dear Bruno,

About the pTraits, can we view them as a special scalar or vector? Because now I have met two situations they are used:

1, used in the operations:

Code:
vector omg = 2*ranGen.vector01() - pTraits<vector>::one
2, used in the initialization of some objects

Code:
loc_(pTraits<vector>::zero)
I feel that they are equal to [1 1 1] and [0 0 0]. Right?
openfoammaofnepo is offline   Reply With Quote

Old   February 5, 2014, 16:20
Default
  #4
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,974
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Hi openfoammaofnepo,

Quote:
Originally Posted by openfoammaofnepo View Post
About the pTraits, can we view them as a special scalar or vector? Because now I have met two situations they are used:
Honestly, I've got no idea what "pTraits" is. I'll have to investigate about it.
I'll try to have a look into it this coming weekend.

Best regards,
Bruno
__________________
wyldckat is offline   Reply With Quote

Old   February 5, 2014, 17:53
Default
  #5
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
Thank you in advance!
openfoammaofnepo is offline   Reply With Quote

Old   February 9, 2014, 16:38
Default
  #6
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,974
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Hi openfoammaofnepo,

I did a quick search and this is what I've figured out:
  1. pTraits is located in "$FOAM_SRC/OpenFOAM/primitives/pTraits/pTraits.H"
  2. It has this comment/description:
    Code:
    All primitives need a specialised version of this class. The specialised version will normally also require a conversion method.
  3. Template specialization is explained here: http://www.cplusplus.com/doc/tutoria...specialization
  4. In essence, this allows dynamic casting for whatever necessary.
  5. If you have a look into the file "$FOAM_SRC/OpenFOAM/primitives/VectorSpace/products.H", you'll see that this further extends functionality to the pTraits template class, by adding the ability to be able to handle products between 2 other items of the same pTraits template class.
Therefore, the pTraits template class is essential to how OpenFOAM handles mathematical data.


As for the question you asked:
Code:
pTraits<vector>::one
pTraits<vector>::zero
This accesses the global definitions defined at "$FOAM_SRC/OpenFOAM/primitives/Vector/vector/vector.C".

... and I'm a bit lost in the code. I'm not 100% certain, but it either it's for accessing the static definitions of one and zero for vector, or to have access to the mathematical operators wrapped up in pTraits.

Best regards,
Bruno
shang likes this.
__________________
wyldckat is offline   Reply With Quote

Old   February 9, 2014, 17:00
Default
  #7
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
Thank you very much for your so detailed explanantion. I think I have more understanding after I read your reply for me.

Thank you again!
openfoammaofnepo is offline   Reply With Quote

Old   February 9, 2014, 17:48
Default
  #8
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,900
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Good evening,

I will probably not be able to tell the full story, but let me share what I know:

As Bruno says, it is merely a matter of accessing ::zero, :ne, the size (e.g. number of elements) of a given type (etc).

pTraits was (allegedly) introduced in order to handle this smoothly for scalars, because they were previously derived directly from the std:: namespace, i.e. if-sentences were needed to distinguish between scalars and everything else. This caused loss of computational efficiency.

Therefore, pTraits is merely a wrapper class, which unifies access to certain properties for all primitive types.

It is used in the very core of everything, e.g. whenever you compute a gradient of a given field, the following function is called in the grad schemes:

Code:
virtual tmp
        <
            GeometricField
            <typename outerProduct<vector, Type>::type, fvPatchField, volMesh>
        > grad
        (
            const GeometricField<Type, fvPatchField, volMesh>&
        ) const = 0;
Here, the important part is "outerProduct", which is to be found in the file:

Code:
./primitives/VectorSpace/products.H
and the implementation reads:

Code:
template<class arg1, class arg2>
class outerProduct
{
public:

    typedef typename typeOfRank
    <
        typename pTraits<arg1>::cmptType,
        int(pTraits<arg1>::rank) + int(pTraits<arg2>::rank)
    >::type type;
};
This is a nice example, where the pTraits is handy. The gradient operator "simply" raises the rank by one, which is achieved by this, because one of the Type-arguments in the template of the gradient operator is of type vector (rank = 1).

Kind regards,

Niels
wyldckat, shang and sencer like this.
__________________
Please note that I do not use the Friend-feature, so do not be offended, if I do not accept a request.
ngj is offline   Reply With Quote

Old   August 17, 2015, 16:09
Default
  #9
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
Dear ngj,

Thank you so much. Today I met the pTraits in the turbulentInlet.C again. So I had a look at thread again. The codes are as follows:

Code:
        scalar rmsCorr = sqrt(12*(2*alpha_ - sqr(alpha_)))/alpha_;

        patchField =
            (1 - alpha_)*patchField
          + alpha_*
            (
                referenceField_
              + rmsCorr*cmptMultiply
                (
                    randomField - 0.5*pTraits<Type>::one,
                    fluctuationScale_
                )*mag(referenceField_)
            );
So here when patchField is vector (actually it is velocity becase it is a boundary type for velocity), is "pTraits<Type>::one" the vector [1 1 1]?

Thank you so much. OFFO

Quote:
Originally Posted by ngj View Post
Good evening,

I will probably not be able to tell the full story, but let me share what I know:

As Bruno says, it is merely a matter of accessing ::zero, ::one, the size (e.g. number of elements) of a given type (etc).

pTraits was (allegedly) introduced in order to handle this smoothly for scalars, because they were previously derived directly from the std:: namespace, i.e. if-sentences were needed to distinguish between scalars and everything else. This caused loss of computational efficiency.

Therefore, pTraits is merely a wrapper class, which unifies access to certain properties for all primitive types.

It is used in the very core of everything, e.g. whenever you compute a gradient of a given field, the following function is called in the grad schemes:

Code:
virtual tmp
        <
            GeometricField
            <typename outerProduct<vector, Type>::type, fvPatchField, volMesh>
        > grad
        (
            const GeometricField<Type, fvPatchField, volMesh>&
        ) const = 0;
Here, the important part is "outerProduct", which is to be found in the file:

Code:
./primitives/VectorSpace/products.H
and the implementation reads:

Code:
template<class arg1, class arg2>
class outerProduct
{
public:

    typedef typename typeOfRank
    <
        typename pTraits<arg1>::cmptType,
        int(pTraits<arg1>::rank) + int(pTraits<arg2>::rank)
    >::type type;
};
This is a nice example, where the pTraits is handy. The gradient operator "simply" raises the rank by one, which is achieved by this, because one of the Type-arguments in the template of the gradient operator is of type vector (rank = 1).

Kind regards,

Niels

Last edited by wyldckat; August 18, 2015 at 13:03. Reason: Added [CODE][/CODE] markers
openfoammaofnepo is offline   Reply With Quote

Old   August 18, 2015, 13:05
Default
  #10
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,974
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Quote:
Originally Posted by openfoammaofnepo View Post
is "pTraits<Type>::one" the vector [1 1 1]?
Quick answer: If the template implementation defined for "Type" is "vector", then yes, it should be the vector "(1 1 1)".

Last edited by wyldckat; August 18, 2015 at 14:56. Reason: typo
wyldckat is offline   Reply With Quote

Old   August 18, 2015, 13:26
Default
  #11
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
Thank you!!

Quote:
Originally Posted by wyldckat View Post
Quick answer: If the template implementation defined for "Type" is "vector", the yes, it should be the vector "(1 1 1)".
openfoammaofnepo is offline   Reply With Quote

Old   December 15, 2016, 10:23
Default
  #12
Member
 
Emad Tandis
Join Date: Sep 2010
Posts: 77
Rep Power: 15
EmadTandis is on a distinguished road
Hi openfoammaofnepo
can you explain about below code:

template<class arg1, class arg2>
class outerProduct
{
public:
typedef typename typeOfRank
<
typename pTraits<arg1>::cmptType,
int(pTraits<arg1>::rank) + int(pTraits<arg2>::rank)
>::type type;
};
EmadTandis is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Question Regarding Symmetry Planes Atella CFX 3 April 11, 2010 07:44
Simple Question Regarding Symmetry Planes Atella Main CFD Forum 0 April 9, 2010 11:58
Question of Anil Date's SIMPLE method universez Main CFD Forum 0 November 18, 2009 21:12
1D thermal stress problem simple barrodbeam newsboost OpenFOAM Running, Solving & CFD 3 January 2, 2009 16:12
A Simple Question Thomas P. Abraham Main CFD Forum 3 September 6, 1999 19:19


All times are GMT -4. The time now is 19:14.