|
[Sponsors] |
June 30, 2016, 14:49 |
Coding issue / OpenFOAM
|
#1 |
Senior Member
SinaJ
Join Date: Nov 2009
Posts: 136
Rep Power: 16 |
Hi,
I'm trying to compile a solver. I have several functions that those functions want to access some mesh data, and I would like to keep those functions in separate C files, and having access in main C file via header files. Please see "testme.C" below! I get errors, cuase I can't access to 'vector', 'label', 'mesh', and etc. How can I fix that? Example (I know the purpose of the function doesn't make sense ... but it's just an example!) Code:
// testme.C file double TestMe(std::vector <double> & myvec) { vector myPoint(1.0, 1.0, 1.0); label myCell = mesh.findCell(myPoint); double u_here = U[myCell].component(0); return u_here; } Code:
// testme.h file double TestMe(std::vector <double> & myvec); Code:
// mySolver.C file ... int main(int argc, char *argv[]) { ... std::vector <double> myvec; double x_com = TestMe (myvec); .... .... } |
|
June 30, 2016, 17:43 |
|
#2 |
Senior Member
SinaJ
Join Date: Nov 2009
Posts: 136
Rep Power: 16 |
Any suggestions would be really helpful
Last edited by sina_mech; July 1, 2016 at 13:59. |
|
July 4, 2016, 08:25 |
|
#3 |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
Your code has some major issues, I will try to highlight the main problems;
Code:
//fun.H #ifndef fun_H #define fun_H #include "scalar.H" #include "vector.H" namespace Foam { vector TestMe(const vector& myvec); } #endif Code:
//fun.C #include "fun.H" Foam::vector Foam::TestMe(const vector& myvec) { Info<< "Hello from TestMe" << endl; Info<< "vector: " << myvec << endl; Info<< "vectorXcmpt: " << myvec.component(0) << endl; Info<< "vectorYcmpt: " << myvec.component(1) << endl; Info<< "vectorZcmpt: " << myvec.component(2) << endl; return myvec; } Code:
//exApp.C #include "fun.H" using namespace Foam; int main(int argc, char *argv[]) { vector A(1,2,3); vector B(4,5,6); vector a = TestMe(A); vector b = TestMe(B); Info<< "Hello from the main()" << endl; Info<< "vector a: " << a << endl; Info<< "vector b: " << b << endl; return 0; } Code:
//Make/files fun.C exApp.C EXE = $(FOAM_USER_APPBIN)/exApp Code:
//Make/files EXE_INC = \ -I$(LIB_SRC)/finiteVolume/lnInclude \ -I$(LIB_SRC)/meshTools/lnInclude EXE_LIBS = \ -lfiniteVolume \ -lmeshTools Best wishes, Hassan |
|
July 6, 2016, 11:52 |
|
#4 | |
Senior Member
SinaJ
Join Date: Nov 2009
Posts: 136
Rep Power: 16 |
Quote:
Thank you for your reply I know i would be better to stick to openFoam's protocols, but in a case, I tried this out, and seems to be working: Code:
//myOwnCFile.C #include "fvMesh.H" #include "fvCFD.H" void MyFunction(std::vector<double> & test, fvMesh const& mesh, vectorField & U) ... |
||
July 6, 2016, 13:10 |
|
#5 |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
It is up to you to use std C++ or not. If you are passing a value by refrence and not planning to change the variable within the function or class, use ``const`` identifier.
Code:
void MyFunction(const std::vector<double>& test, const fvMesh& mesh, const vectorField& U) { ... .... } |
|
September 14, 2022, 22:44 |
|
#6 |
Member
|
Dear Hassan,
I want to create some functions in the solver. I have tested the code you proposed. They are correct perfectly. I just want to replace the fun.H file by Code:
template<class CloudType> inline const Foam::tmp<Foam::volVectorField> Foam::KinematicCloud<CloudType>::UPar() const { tmp<volVectorField> tUPar ( volVectorField::New ( this->name() + ":UPar", mesh_, dimensionedVector(dimVelocity, vector(0,0,0)), extrapolatedCalculatedFvPatchVectorField::typeName ) ); volVectorField& UPar = tUPar.ref(); forAllConstIter(typename KinematicCloud<CloudType>, *this, iter) { const parcelType& p = iter(); const label celli = p.cell(); UPar[celli] += (p.U())/(p.nParticle()); } UPar.primitiveFieldRef() /= 1;//mesh_.V(); UPar.correctBoundaryConditions(); return tUPar; } template<class CloudType> inline const Foam::tmp<Foam::volScalarField> Foam::KinematicCloud<CloudType>::SumPar() const { tmp<volScalarField> tSumPar ( volScalarField::New ( this->name() + ":SumPar", mesh_, dimensionedScalar(dimless, 0), extrapolatedCalculatedFvPatchScalarField::typeName ) ); volScalarField& SumPar = tSumPar.ref(); forAllConstIter(typename KinematicCloud<CloudType>, *this, iter) { const parcelType& p = iter(); const label celli = p.cell(); SumPar[celli] += p.nParticle(); } SumPar.primitiveFieldRef() /= 1;//mesh_.V(); SumPar.correctBoundaryConditions(); return tSumPar; } such as Code:
//fun.H #ifndef fun_H #define fun_H template<class CloudType> inline const Foam::tmp<Foam::volVectorField> Foam::KinematicCloud<CloudType>::UPar() const { tmp<volVectorField> tUPar ( volVectorField::New ( this->name() + ":UPar", mesh_, dimensionedVector(dimVelocity, vector(0,0,0)), extrapolatedCalculatedFvPatchVectorField::typeName ) ); volVectorField& UPar = tUPar.ref(); forAllConstIter(typename KinematicCloud<CloudType>, *this, iter) { const parcelType& p = iter(); const label celli = p.cell(); UPar[celli] += (p.U())/(p.nParticle()); } UPar.primitiveFieldRef() /= 1;//mesh_.V(); UPar.correctBoundaryConditions(); return tUPar; } template<class CloudType> inline const Foam::tmp<Foam::volScalarField> Foam::KinematicCloud<CloudType>::SumPar() const { tmp<volScalarField> tSumPar ( volScalarField::New ( this->name() + ":SumPar", mesh_, dimensionedScalar(dimless, 0), extrapolatedCalculatedFvPatchScalarField::typeName ) ); volScalarField& SumPar = tSumPar.ref(); forAllConstIter(typename KinematicCloud<CloudType>, *this, iter) { const parcelType& p = iter(); const label celli = p.cell(); SumPar[celli] += p.nParticle(); } SumPar.primitiveFieldRef() /= 1;//mesh_.V(); SumPar.correctBoundaryConditions(); return tSumPar; } #endif Code:
fun.H:16:41: error: no ‘const Foam::tmp<Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh> > Foam::KinematicCloud<CloudType>::UPar() const’ member function declared in class ‘Foam::KinematicCloud<CloudType>’ Foam::KinematicCloud<CloudType>::UPar() const ^ fun.H:48:43: error: no ‘const Foam::tmp<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> > Foam::KinematicCloud<CloudType>::SumPar() const’ member function declared in class ‘Foam::KinematicCloud<CloudType>’ Foam::KinematicCloud<CloudType>::SumPar() const Best regards, Chengan |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[OpenFOAM.org] A Mac OS X of23x Development Environment Using Docker | rt08 | OpenFOAM Installation | 1 | February 28, 2016 19:00 |
OpenFoam parallel running log issue | clktp | OpenFOAM Running, Solving & CFD | 2 | January 19, 2016 09:12 |
OpenFOAM Training, London, Chicago, Munich, Sep-Oct 2015 | cfd.direct | OpenFOAM Announcements from Other Sources | 2 | August 31, 2015 13:36 |
CyclicAMI Issue In OpenFOAM 2.2.0 | prasant | OpenFOAM Running, Solving & CFD | 17 | March 16, 2013 02:00 |
Issue installation OpenFOAM - libopen-rte.so.0 | Voyage_gui | OpenFOAM | 1 | August 12, 2011 03:46 |