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

Adding functionality to classes

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

Reply
 
LinkBack Thread Tools Display Modes
Old   February 27, 2008, 14:29
Default I understand how to add public
  #1
New Member
 
Sergio Ferraris
Join Date: Mar 2009
Posts: 17
Rep Power: 8
sergio is on a distinguished road
I understand how to add public members functions to classes already implemented in OP. But, I need to add the declaration in H. and I can not add my own code to the original source code but to do totally independent. It is possible to add member functions outside the original class declaration ?

Thanks
sergio is offline   Reply With Quote

Old   February 27, 2008, 18:58
Default Hi Sergio! Yep. C++ has a m
  #2
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 3,912
Rep Power: 40
gschaider will become famous soon enoughgschaider will become famous soon enough
Hi Sergio!

Yep. C++ has a mechanism for that: it's called inheritance.

You create a new class that is derived from the original class and add/modify all the functionality you require.

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   February 28, 2008, 05:05
Default Hi, Thanks. Yes, I know that.
  #3
New Member
 
Sergio Ferraris
Join Date: Mar 2009
Posts: 17
Rep Power: 8
sergio is on a distinguished road
Hi, Thanks.
Yes, I know that.
What I want to do is to modify an already implemented class (i.e. LES Model) to add an extra virtual member function and define it on one of the derived class. But, all these not into the original definition and implementation of LES model. H or LES Model.c, but on an independent file structure. This is useful for geting use made code without touching any bit of the original.
sergio is offline   Reply With Quote

Old   February 28, 2008, 06:13
Default Hi Sergio! Sorry for sugges
  #4
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 3,912
Rep Power: 40
gschaider will become famous soon enoughgschaider will become famous soon enough
Hi Sergio!

Sorry for suggesting you didn't know basic C++.

Still: what you are suggesting would suggest the very idea of data encapsulation (apart from technical problems) because your new methods could access for instance private variables of the original class. Don't get me wrong: I don't have a problem with subversion. C++ has ;)

My suggestion would be: make your own copy of the OF-installation (call it for instance: OpenFOAM-1.4.1-sergio). Do the modifications you're intending there and try them. Once they're working you can wonder about the technicalities of making them compatible with a vanilla OF-installation.

The other way would be generating a subclass of the original turbulence model that would contain a pointer to a turbulence model which it would dynamically create at construction time (from a parameter). All calls to methods of that class would be delegated to the pointer except the ones you're modifying (plus the methods you're adding). The problem is that I don't know the turbulence classes that well to tell whether this is feasable (there might be necessary constructors missing from the original class .... etc). So stick with the first approach and worry about the technicalities later.

Bernhard

PS: this is about adding switchable wall-functions, is it?
__________________
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   February 28, 2008, 06:42
Default Hi Bernghard, Thanks. You ar
  #5
New Member
 
Sergio Ferraris
Join Date: Mar 2009
Posts: 17
Rep Power: 8
sergio is on a distinguished road
Hi Bernghard,
Thanks.
You are right. I dont know c++, I just struggle with it and beat it now and then!
OK.

I know that the variations works because I tried them in the original files before.

It is simply to pass an scalar field to solve the transport equation for k in a slightly different way as OP does. I need to change a bit the solution for k (turbulent intensity).

The classes are: LES Models.H (abstract) <----- SGS Eddy Vis(k) <---KEq.H (this solve k)

I need to add a member to KEq.H, I can do it in other file (Keq_sergio.c) but I need to modify KEq.H for the interface. The same applies to LES Models.H, where I have to add the virtual function. This is the point I dont want to change LES Models.H and Keq.H in the OF original folder but just to "add" functionallity.

The seconf way you mentioned is when C++ beat me.
What is it a sub-class, is it a derived class or a class inside a class?

I am in charged of the research at Kingston University, London, and I am setting the way for the PhD students to work well with OP, in a way to develope the models 100% independenly of the original source code (this means without touching it at all). This will help to compatibility in the future for our group.

Thanks
very much
Sergio
sergio is offline   Reply With Quote

Old   February 28, 2008, 08:15
Default Dear Sergio, What i underst
  #6
New Member
 
David Palko
Join Date: Mar 2009
Location: Stockholm
Posts: 17
Rep Power: 8
dpalko is on a distinguished road
Dear Sergio,

What i understood from this conversation is that the priority for you is to keep the original source code of OF untouched. In this case, i think, the only solution is the second suggestion by Bernhard. Create a derived class from the original turbulence model and define your new method here. Then, later, when you refer to this method (function) in your solver code, refer to the derivedClass.method() or derivedClass->method(). For the unmodified methods, refer to parentClass.method() or parentClass->method(). This is not as elegant as refering to the virtual method of parent class, but for me it is the only way how to avoid the implementation of virtual method to the existing parent class.

Anyone, please correct me if i'm wrong.

David
dpalko is offline   Reply With Quote

Old   February 28, 2008, 09:27
Default I was thinking of a delegation
  #7
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 3,912
Rep Power: 40
gschaider will become famous soon enoughgschaider will become famous soon enough
I was thinking of a delegation. Not exaktly like the examples in http://en.wikipedia.org/wiki/Delegation_(programming)

This is just a sketch:

class SergioTurbClass : public OrigTurbClass {
OrigTurbClass *orig;
public:
virtual void sergiosMethod()=0;
void delegatedVirtualMethod() {orig->delegatedVirtualMethod(); }
void modifiedVirtualMethod() {
this->sergiosMethod(); orig->modifiedVirtualMethod();
}
};

The problem with this approach (still havn't looked at the turbulence hierarchy) is that there is some data in the base class and with that approach it would be duplicated (once by inheritance, once by the object orig refers to). Keeping that in sync might be painful

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   February 29, 2008, 04:50
Default David and Bernhard, thanks fo
  #8
New Member
 
Sergio Ferraris
Join Date: Mar 2009
Posts: 17
Rep Power: 8
sergio is on a distinguished road
David and Bernhard,
thanks for your help. I will try both approaches and tell you later. Although, I though that it was going to be easier.I believed that the object oriented concept permited to leave untouch and and extra functionality externally.
I think that David's idea is problably simpler tov understand and implement and as I am looking for some rule to follow it looks neater. Even when you need to modify the main code and check that it does all what the original method did.

I will be in contact short.

Thanks very much
Sergio
sergio is offline   Reply With Quote

Old   March 5, 2008, 08:02
Default Another alternative. Create a
  #9
Senior Member
 
Eugene de Villiers
Join Date: Mar 2009
Posts: 725
Rep Power: 12
eugene is on a distinguished road
Another alternative. Create a new user library called say sergioLib. Add the modified turbulence model to this library under a new name - e.g. newkModel. Pull the new library into your applications via the controlDict functionality - libs ("libsergioLib.so").
eugene is offline   Reply With Quote

Old   March 13, 2008, 06:18
Default Hi, Following the discussion
  #10
New Member
 
Sergio Ferraris
Join Date: Mar 2009
Posts: 17
Rep Power: 8
sergio is on a distinguished road
Hi,
Following the discussion about extending a class functionality I derived a class from the one I wanted to extend the functions and re-implemented the same virtual class but with extra parameters. In this way, I don't change the interface of the virtual base class. What I am expecting is that when I call the member function from the base it should realised that it is the modified version (the one in the derived class) and not the original. For this I created an extra member function to set the value of the extra parameter which will be used by the modified member.

Do you think it might work?

Other problem I had is an unexpected error in the constructor of the derived class.

This is the constractor with a base class oneEqEddy from where I derived oneEqEddyKU.
I followed the same sintaxis as in other occasions but it does not compile so far.


oneEqEddyKU::oneEqEddyKU
(
const volScalarField& rho,
const volVectorField& U,
const surfaceScalarField& phi,
const basicThermo& thermoPhysicalModel
)
:
LESmodel(typeName, rho, U, phi, thermoPhysicalModel),
oneEqEddy(rho, U, phi, thermoPhysicalModel),

ck_(LESmodelProperties().lookup("ck"))
{}
sergio is offline   Reply With Quote

Reply

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
Finite Area Classes and Surface related Utilities philippose OpenFOAM 10 December 9, 2011 12:42
Example of use of the solidParticlesolidParticleCloud classes alberto OpenFOAM Running, Solving & CFD 27 July 9, 2009 03:00
Understanding tmp%2360T classes nadine OpenFOAM Running, Solving & CFD 14 January 22, 2009 10:25
Turbo functionality is not enabled! nancy FLUENT 0 October 9, 2003 15:44
Parallel Computing Classes at San Diego Supercomputer Center Jan. 20-22 Amitava Majumdar Main CFD Forum 0 January 5, 1999 13:00


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