CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   C question (https://www.cfd-online.com/Forums/openfoam-solving/58877-c-question.html)

kar February 19, 2008 11:43

Hello, I'm not sure what's
 
Hello,

I'm not sure what's the meaning of that part after ":":

template<class>
fvMatrix<type>::fvMatrix(const fvMatrix<type>& fvm)
:
refCount(),
lduMatrix(fvm),
psi_(fvm.psi_),
dimensions_(fvm.dimensions_),
source_(fvm.source_),
internalCoeffs_(fvm.internalCoeffs_),
boundaryCoeffs_(fvm.boundaryCoeffs_),
faceFluxCorrectionPtr_(NULL)
{
...
}

Could you explain or give some example?

K.

deepsterblue February 19, 2008 17:06

This has been answered before:
 
This has been answered before:

http://www.cfd-online.com/OpenFOAM_D...tml?1202118865

gschaider February 19, 2008 17:21

The super-classes and member-v
 
The super-classes and member-variables get initialized

That is the short explanation. Books have been written with longer explanations (on the basics of C++) and I would recommend rereading one of these before even looking at the fvMatrix-class

Sorry. But these are the two best explanations I can think of that take less than one page

marziolettich February 20, 2008 03:31

I had the same problem in unde
 
I had the same problem in understanding this "initializer list": it's a way to initialize the private member of a class (at least, that's what I understood!). I found an exhaustive answer on a C++ Ubuntu forum: http://ubuntuforums.org/archive/index.php/t-645008.html

Anyway, in some books this "initializer list" is not even mentioned (Schildt's for instance is quite a popular book on C++, but I wasn't able to find it).

jaswi February 20, 2008 04:15

Hi Karlis Please just read
 
Hi Karlis

Please just read through a nice book on C++ and you will have no problem with OpenFOAM. I would recommend you the following books:

1) C++ How to Program
Harvey M. Deitel and Paul J. Deitel
2) Object-Oriented Programming in C++ by Josuttis.

3) Professional C++ by Kleper, Wrox professional
series.

Start with the first one and then move on to the next two books. 1st and 3rd are available as compiled HTML books all over the web.

Just Google them.

OpenFOAM is worth the time you will invest in understanding C++. These books will teach you how to read the code and OpenFOAM will teach you how one can put to real use, all you have read in these books.

Wish you a successful FOAMing ahead.

Jaswi

tspon February 20, 2008 04:33

Hi guys, sorry for disturbi
 
Hi guys,

sorry for disturbing you, but I'm looking for some C++ experts who could help me with this little problem, nothing complicated really.
I only want to extract the U field values at the outlet of my pipe in order to initialize my U_boundary inlet with the appropriate velocity values from the outlet (kind of manual cyclic).
Even after an extensiv search in the documentation as well as in this forum I do not know which command to use. Something like

field[outlet]

does not help.

@ Bernhard: I successfully tried your setParabolicInlet.C but that does not help with turbulent flows or not fully developed flows, I guess.

Thanks for your reply in advance. Think its not too difficult for you guys.

thomas

jaswi February 20, 2008 04:49

Hi Thomas Try const lab
 
Hi Thomas

Try

const label patchID = mesh.boundaryMesh().findPatchID("outlet")
volVectorField = U.boundaryField()[patchID]

Hope that helps

Regards
Jaswi

tspon February 20, 2008 05:16

Hi Jaswi, thanks for your q
 
Hi Jaswi,

thanks for your quick response. I tried a similar one

vectorField& outlet = U.boundaryField()[patchID]

but the problem is that I only get the entries of the outlet_boundary.
What I want is extracting from the U_field of lets say 1000 time steps (simple foam) the entries of the cells forming the outlet of my pipe. That is take for example 50 cells out of 2000. Take these entries and put them in the /0/U boundary_inlet for the next run. So my problem is, how do I tell OpenFoam to take only the ones of the outlet.
BTW I could manually do that with the help of paraview but I'll be a mess with more than 50 cells.

jaswi February 20, 2008 05:33

Hi Thomas As per my underst
 
Hi Thomas

As per my understanding level, in that case you need to use sets.

Define a cellSet comprising of the desired cells and then you can access all the data fields associated to this set.

Look into /applications/utilities/mesh/manipulation/

There are examples of faceSetDict, cellSetDict, faceSetDict.

Anybody please correct me if I am wrong. Also please checkout a recent post by Dragos.

Search the forum for his posts and there you will find a recent post where he has shown how to extract cell sets and the fields related to them.

Hope that helps

Regards
Jaswi

tspon February 20, 2008 06:34

Hi Jaswi, Thanks for your h
 
Hi Jaswi,

Thanks for your help once again. It works now. I could manage your first hint. It was my fault. I forgot to put in the time steps and only tried for the /U/0. Therefore the outlet had no entries.

Sorry for that. BTW the faceSet hint could be of great importance for me. I'll have a look.

Regards
Thomas

kar February 28, 2008 15:39

Hi, this is me again http://w
 
Hi,
this is me again http://www.cfd-online.com/OpenFOAM_D...part/happy.gif

One more C++ question - I try to make my own boundary condition that involves gradient, but don't understand what is done when something like this:

"vectorField::operator=(n_*maxValue_*(1.0 - sqr(coord)));"

is called in parabolic U BC by Harvoje:

void parabolicVelocityFvPatchVectorField::updateCoeffs( )
{
// Get range and orientation
boundBox bb(patch().patch().localPoints(), false);

vector ctr = 0.5*(bb.max() + bb.min());

const vectorField& c = patch().Cf();

// Calculate local 1-D coordinate for the parabolic profile
scalarField coord = ((c - ctr) & y_)/((bb.max() - bb.min()) & y_);

vectorField::operator=(n_*maxValue_*(1.0 - sqr(coord)));
}

I managed to make similar BC based on fixedValue, but not yet on fixedGradient, which is necessary indeed. That requires better understanding of code. Please explain about
"vectorField::operator=(n_*maxValue_*(1.0 - sqr(coord)));"!

Kārlis

deepsterblue February 28, 2008 16:10

Simple. vectorField::opera
 
Simple.

vectorField::operator= calls the base-class implementation of the '=' operator (from which FvPatchVectorField is derived).

deepsterblue February 29, 2008 10:38

I'm not sure if I've understoo
 
I'm not sure if I've understood this right, but here goes - Do you want to access the '=' operator of the base class using the derived class? If that's the case, here's the modified code:

#include <iostream>
using namespace std;

class base
{
private:
int x;

public:
base(const int& a = 2)
: x(a) {}
const int gx() {return x;}
void se(int& i) {x=i;}
void operator=(int k);
};

class deriv : public base
{
private:
int z;

public:
deriv(const int &tz, const int & x): z(tz), base(x) { }
const int & mul(){ int rez = z*base::gx(); int & p = rez; return p; }

//void operator=(int k) {z = k;}
};

void base::operator=(int k)
{ x = k; }

int main(int argc, char ** args)
{
deriv d(6, 7);
d.base::operator=(10);

cout << d.mul() << "\n";

return 0;
}

It gives me the answer you want. Hope this helps.

kar February 29, 2008 12:19

Thanks for trying Sandeep! Yo
 
Thanks for trying Sandeep!
You just added ".base::operator=(10)", right? Somehow my compiler says:

operators2.cpp: In function 'int main(int, char**)':
operators2.cpp:5: error: 'class base' is inaccessible
operators2.cpp:35: error: within this context
operators2.cpp:35: error: 'base' is an inaccessible base of 'deriv'

GNU C++ version 4.2.1 (i686-pc-linux-gnu)
compiled by GNU C version 4.2.1.

Comments???

deepsterblue February 29, 2008 12:30

Not just that. The derived cla
 
Not just that. The derived class is defined as:

class deriv : public base

This allows deriv to access the public members of its base class.

kar February 29, 2008 12:45

Yeeeeeeesss, I got 60! Thanks
 
Yeeeeeeesss, I got 60!
Thanks for prompt response! I had to be very careful or write your code in new file to diff...

kar March 3, 2008 12:00

Hi, one more thing: in code
 
Hi,

one more thing: in code above one has to call d.base::operator=(10); to access operator in base class.
Let T be volScalarField. T.boundaryField()[patchID][faceID]; works, despite there is no operator[] in GeometricBoundaryField, nor FieldField, just in class PtrList (to access [patchID]! There are numerous other similar examples, so my question now is: when members of base or base-base-..-base class can be accessible directly?

Kārlis

deepsterblue March 3, 2008 17:42

Yes, PtrList gives a Field, wh
 
Yes, PtrList[patchID] gives a Field, which is in turn derived from List, and so on. The base has an operator[] implementation, which is used.

Without looking at the details, I'm guessing that something like this is being done:

#include <iostream>
using namespace std;

class base
{
private:
int x;

public:
base(const int& a = 2)
: x(a) {}
const int gx() {return x;}
void se(int& i) {x=i;}
void operator=(int k);
};

void base::operator=(int k)
{ x = k; }

class deriv : public base
{
private:
int z;

public:
deriv(const int &tz, const int & x): z(tz), base(x) { }
const int & mul(){ int rez = z*base::gx(); int & p = rez; return p; }

void operator=(int k) { this->base::operator=(k);}
};

int main(int argc, char ** args)
{
deriv d(6, 7);
d=10;

cout << d.mul() << "\n";

return 0;
}

As you can see, I've just used 'd=10', which gives me the same answer you want.

kar March 4, 2008 03:46

Sandeep, follows, that it's
 
Sandeep,

follows, that it's sufficient to prove there is no direct member function operator[] for GeometricBoundaryField,to show something other is done in OpenFOAM code. I'm quite sure there is no operator[] in GeometricBoundaryField class. (reviewed class declaration + searched through ALL OF code for " GeometricBoundaryField" and "::operator\[\]" in one line. No results!)

So, my question remains - why T.boundaryField()[patchID] returns element of PtrList?

Kārlis

dmoroian March 4, 2008 03:57

Hi Kārlis, If you look at th
 
Hi Kārlis,
If you look at the inheritance diagram GeometricBoundaryField, you'll see that it inherits the PtrList (among other classes). Consequently (see List of all members), one of the inherited operators is operator[](const label); and guess what it returns...?

Dragos

kar March 4, 2008 04:59

Dragos, I did followed thr
 
Dragos,

I did followed through code, but did you read previous posts - the thing is: when I try to declare my own base and derived class with operator= in base class, operator= isn't accessible for object of class deriv if there is no forwarding in deriv class's declaration. Can't see such forwarding in class GeometricBoundaryField (and others) - can you help?

K.

dmoroian March 4, 2008 08:59

Indeed you're right, and it wa
 
Indeed you're right, and it was my mistake. I don't see why in your code it is possible to call int i = 10; d.se(i); but not int i = 10; d.operator=(10); nor int i = 10; d = i;
...promise to look into it!

Dragos

dmoroian March 5, 2008 07:47

Ok, I think I have an answer!
 
Ok, I think I have an answer!
Among other free on-line c++ book, there is also Thinking in C++ by Bruce Eckel. It specifies, and I quote:
Quote:

Except for the assignment operator, operators are automatically inherited into a derived class.
Which means that, you just picked an unfortunate example, trying to overload operator=. If instead of operator=, you would have chosen any other operator, like operator[], then everything would go smoothly.

I've just tried it, and it works!

I hope this is useful,
Dragos

kar March 5, 2008 07:59

Thank you! I'll take a look at
 
Thank you! I'll take a look at that book http://www.cfd-online.com/OpenFOAM_D...part/happy.gif

kar May 19, 2008 13:27

Hmm, may I ask about virtual f
 
Hmm, may I ask about virtual functions and inheritance?

#include <iostream>

using namespace std;

class base1
{
public:
bool use(bool y) {return x(y);}
virtual bool x(bool y) = 0;
};

class base2
{
public:
bool x(bool y) {return !y;}
};

class deriv:
public base1, public base2
{
// whatever
};

int main( int c, char ** a)
{
deriv d;
if (!d.use(1)) cout << "yes";
return 1;
}

But it is "no", because compiler output is:

virtinh.cpp: In function 'int main(int, char**)':
virtinh.cpp:26: error: cannot declare variable 'd' to be of abstract type 'deriv'
virtinh.cpp:20: note: because the following virtual functions are pure within 'deriv':
virtinh.cpp:9: note: virtual bool base1::x(bool)

So is it impossible to "merge" an abstract class with definitions of virtual functions, that reside in another base class similarly to example below?

su_junwei May 19, 2008 19:51

may be the pure virtual functi
 
may be the pure virtual function "virtual bool x(bool y) = 0; " in class "base1" should be defined definitely.
Su Junwei

kar May 20, 2008 06:05

That is a whole point of my qu
 
That is a whole point of my question, that virtual bool x(bool y) = 0; and I want it to be called from use(..) through deriv d!


All times are GMT -4. The time now is 06:16.