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

C scope resolution problem

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 3, 2008, 13:46
Default I'm trying to write a code tha
  #1
Senior Member
 
Maka Mohu
Join Date: Mar 2009
Posts: 305
Rep Power: 18
maka is on a distinguished road
I'm trying to write a code that process both scalarFields and vectorFields and I came to the point where I need something like the following. But offcourse it does not work because the scope of X is limited by the if statement scope. I tried lots of ideas including #pragama but I can not find a solution. Does any body have an idea how to accomplish this? Thanks.

if (Xheader.headerClassName() == "volScalarField")
{
volScalarField X(Xheader, mesh);
} else if (Xheader.headerClassName() == "volVectorField")
{
volVectorField X(Xheader, mesh);
} else {

}

// use X

/Maka.
maka is offline   Reply With Quote

Old   March 3, 2008, 15:40
Default Hi Maka! You could only do
  #2
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Hi Maka!

You could only do this via a pointer to the "next" common untemplated parent-class of these two classes which would be regIOobject (and I don't think that the functionality of this class is sufficient for what you propably want to do)

"You need a thief to catch a thief" or like the C++-programmer says: you need a templated function to catch template instances. (Static polymorphism)
How to implement this principle depends on what you want to do. Have a look at the sources of the sample-utility or decomposePar. If I remember it correctly they do similar things. Maybe somebody else can point you to a simpler example than these in the OF-sources

Bernhard
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   March 4, 2008, 09:33
Default to make what I want to do more
  #3
Senior Member
 
Maka Mohu
Join Date: Mar 2009
Posts: 305
Rep Power: 18
maka is on a distinguished road
to make what I want to do more clear instead of "// use X" the following code should be in place:

const vectorField& centres = mesh.C();

forAll(centres, celli)
{
// use X
}

That is the reason why I want to avoid constructing the volScalarField or volVectorField inside the loop since it loops over all cells in the domain while the field, to be efficient, should be constructed once outside that loop.

Best regards,
Maka
maka is offline   Reply With Quote

Old   March 4, 2008, 10:07
Default I might have missed soemthing,
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
I might have missed soemthing, but it looks like you simply need a template function with the looping over the cell centres instead it.
olesen is offline   Reply With Quote

Old   March 4, 2008, 11:00
Default I'm currently reading about fu
  #5
Senior Member
 
Maka Mohu
Join Date: Mar 2009
Posts: 305
Rep Power: 18
maka is on a distinguished road
I'm currently reading about function templates and trying to see if it solves the problem. Many thanks for your help.

in more details see the following. I could solve the problem if in C++ we could store variable types. something like

#include<typeinfo>

type_info x = int;

if (condition)
x=scalar;
else
x=vector

But it does not work. I tried using typedef but you can not redefine typedef. I can not try #def and #undef since it will have the scope problem again and it will not work since the type is determined at run time.

What I want to do in full detail:

if (Xheader.headerClassName() == "volScalarField")
{
volScalarField X(Xheader, mesh);
} else if (Xheader.headerClassName() == "volVectorField")
{
volVectorField X(Xheader, mesh);
} else {

}

forAll(centres, celli)
{
// some code


if (Xheader.headerClassName() == "volScalarField")
{
// do something
} else if (Xheader.headerClassName() == "volVectorField")
{
// do another thing
} else {

}

}
maka is offline   Reply With Quote

Old   March 5, 2008, 06:38
Default I'm currently reading about fu
  #6
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
I'm currently reading about function templates and trying to see if it solves the problem.
Keep reading - it should solve your problem.
(Forget about trying to use typedefs for the same thing).
olesen is offline   Reply With Quote

Reply


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
Scope of CFD in aerospace engineering Apoorva L Main CFD Forum 2 April 6, 2011 15:25
OF14 compilation error bistreamb is already declared in this scope rafal OpenFOAM Installation 5 August 26, 2008 12:23
scope of turbulent transition Prapanj Main CFD Forum 0 August 20, 2007 14:39
cfd-scope,future and changes shiva Main CFD Forum 11 August 6, 2005 11:30
cfd market scope narasimma varman Main CFD Forum 0 November 5, 2002 04:37


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