|
[Sponsors] |
November 7, 2020, 04:48 |
Access to protected Ubar_ in fvOption (Urgent Help)
|
#1 |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Hi all,
I have a question about fvOption. I am using meanVelocityForce as the type of momentum source in my fvOption file. However, by looking the source file of this momentum type at OpenFOAM-5.x/src/fvOptions/sources/derived/meanVelocityForce, something seems hard to me. https://github.com/OpenFOAM/OpenFOAM...ityForce.C#L89 We have a Ubar_ vector as a protected value which is read from fvOption file. Due to its protected type, it cannot be used at the outside of its class. So can anybody help me to just print its value (Info) at each timestep of my solver. I think a solution is defining a public function at the class to print its value, and use this function at each time step in our solver coding (fvOptions.functionforUbar). But it has lots of c++ details. Last edited by mostanad; November 7, 2020 at 11:46. |
|
November 7, 2020, 05:08 |
|
#2 |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
Dear mostanad,
As you said, the Ubar_ defined at class "meanVelocityForce" is read from "fvOptions" file. And it's just a user-defined fixed vector value which is not changed during the calculation. So if you want to use it in your solver, i think an easy way is to (1) define the same "Ubar" in your solver's dict file and read it in your code or (2) read 'fvOptions' in your solver's code to get the user-defined "Ubar" value. Timo |
|
November 7, 2020, 05:23 |
|
#3 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
Thank you Dear Timo for your quick reply. The problem cannot be solved by these two methods. Yes, the Ubar_ is a user defined vector and by #include "createFvOptions.H" in my solver, the solver reads just once the fvOption file and gives the Ubar value to this vector. So it cannot be changed in different timestep. However, if I can define a function in meanVelocityForce.C (like constrain or correct functions), I report this value in every timestep or as my final goal, I can change its value(because I want to change Ubar_ based on some results in each timestep). So do you have any solution for that? Thanks, Mohammad |
||
November 7, 2020, 05:36 |
|
#4 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
I have found something similar to my problem, but with no details!!! So still help me! How to access protected variable? |
||
November 7, 2020, 05:45 |
|
#5 |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
Dear Mohammad,
Okay, I get your point. I think it's possible to change the Ubar_ value on the fly. The idea is to pass a parameter to meanVelocityForce::correct(volVectorField& U) or meanVelocityForce::constrain. Say, (1)change the following function: Code:
void Foam::fv::meanVelocityForce::correct(volVectorField& U) { ... } Code:
void Foam::fv::meanVelocityForce::correct(volVectorField& U, vector& Ubar_New) { ... Ubar_ = Ubar_New; ... } Code:
void Foam::fv::meanVelocityForce::constrain ( fvMatrix<vector>& eqn, const label ) { ... } Code:
void Foam::fv::meanVelocityForce::constrain ( fvMatrix<vector>& eqn, const label, vector& Ubar_new ) { ... Uba_ = Ubar_New; } This *correct* and *constrain* function is called in the UEqn.H. You can check pimpleFoam solver for more information and choose one by your own situation. Timo |
|
November 7, 2020, 06:25 |
|
#6 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
Last edited by mostanad; November 7, 2020 at 07:36. |
||
November 7, 2020, 06:37 |
|
#7 |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
Dear Mohammad,
Yes, this solution will introduce some inconvenience. It's much better to define a separate function to just set your desired Ubar value. I'll have a look at it when I finish my dinner. By the way, both solution need to define virtual functions in the *fvOptions* so that you can access it in your solver. Timo |
|
November 7, 2020, 07:55 |
|
#8 | |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
Quote:
1. create virtual functions in fvOption.[CH]: fvOptions/fvOptions/fvOption.H Code:
virtual void setUbar(vector& UbarTarget); Code:
void Foam::fv::option::setUbar(vector& UbarTarget) { // do nothing } fvOptions/fvOptions/fvOpitonList.H Code:
void setUbar(vector& UbarTarget); Code:
void Foam::fv::optionList::setUbar(vector& UbarTarget) { forAll(*this, i) { this->operator[](i).setUbar(UbarTarget); } } meanVelocityForce.H Code:
//- Set target ubar virtual void setUbar(vector& UbarTarget); Code:
void Foam::fv::meanVelocityForce::setUbar ( vector& UbarTarget ) { //Info<< "UbarTarget:"<<UbarTarget<<" "<<Ubar_<<endl; // Set Ubar by UbarTarget Ubar_ = UbarTarget; } Code:
vector UbarTarget(5.0,0.0,0.0); fvOptions.setUbar(UbarTarget); Timo |
||
November 7, 2020, 07:59 |
|
#9 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
Code:
fvOptions.Uvalue(); Code:
virtual void Uvalue(); Code:
void Foam::fv::meanVelocityForce::Uvalue()//insert by Mo { Info << " Ubar " << Ubar_ << endl;//insert by Mo } Code:
virtual void Uvalue(); Code:
void Foam::fv::option::Uvalue(){} Code:
void Uvalue()//insert by Mo { forAll(*this, i) { option& source = this->operator[](i); source.Uvalue(); } } Thank you, Mohammad |
||
November 7, 2020, 08:02 |
|
#10 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
|
||
November 7, 2020, 08:10 |
|
#11 |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
Haha, yes, we did the same thing at the same time.
I think you should pass a parameter in your Uvalue() function and modify Ubar_ in it, such that the Ubar_ will be changed every time you call fvOptions.Uvalue. Here is my test result using the code in my last post: Code:
Time = 0.200001 smoothSolver: Solving for Ux, Initial residual = 0.00115195, Final residual = 2.02236e-07, No Iterations 2 smoothSolver: Solving for Uy, Initial residual = 0.00936929, Final residual = 1.63209e-06, No Iterations 2 smoothSolver: Solving for Uz, Initial residual = 0.00943482, Final residual = 1.54585e-06, No Iterations 2 Pressure gradient source: uncorrected Ubar = 36, pressure gradient = 2284.67 UbarTarget:(5 0 0) (36 0 0) GAMG: Solving for p, Initial residual = 0.0710281, Final residual = 0.0024497, No Iterations 2 time step continuity errors : sum local = 4.06092e-08, global = -3.4513e-18, cumulative = -3.4513e-18 Pressure gradient source: uncorrected Ubar = 36, pressure gradient = -5.98508e+07 GAMG: Solving for p, Initial residual = 0.448843, Final residual = 8.42978e-07, No Iterations 28 time step continuity errors : sum local = 1.94414e-11, global = 6.0499e-18, cumulative = 2.5986e-18 Pressure gradient source: uncorrected Ubar = 35.6024, pressure gradient = -5.90832e+07 ExecutionTime = 0.78 s ClockTime = 1 s fieldAverage fieldAverage1: Reading/initialising field UMean Reading/initialising field UPrime2Mean fieldAverage fieldAverage1 output: Calculating averages Courant Number mean: 0.0414973 max: 0.0803248 Time = 0.200002 smoothSolver: Solving for Ux, Initial residual = 0.919327, Final residual = 7.03513e-06, No Iterations 5 smoothSolver: Solving for Uy, Initial residual = 0.0113043, Final residual = 7.20097e-06, No Iterations 3 smoothSolver: Solving for Uz, Initial residual = 0.0112912, Final residual = 6.50256e-06, No Iterations 3 Pressure gradient source: uncorrected Ubar = -53.3497, pressure gradient = 5.40978e+07 UbarTarget:(5 0 0) (5 0 0) GAMG: Solving for p, Initial residual = 0.97442, Final residual = 0.051851, No Iterations 3 time step continuity errors : sum local = 2.22175e-05, global = -9.29353e-16, cumulative = -9.26754e-16 Pressure gradient source: uncorrected Ubar = -52.7138, pressure gradient = 5.28642e+07 GAMG: Solving for p, Initial residual = 0.0645526, Final residual = 9.08584e-07, No Iterations 16 time step continuity errors : sum local = 7.7011e-10, global = 4.40136e-15, cumulative = 3.47461e-15 Pressure gradient source: uncorrected Ubar = -52.6635, pressure gradient = 5.27667e+07 ExecutionTime = 1.24 s ClockTime = 2 s UbarTarget5 0 0) (36 0 0) Second step, Ubar is changed to 5 which is modified by setUbar(UbarTarget): UbarTarget5 0 0) (5 0 0) Timo |
|
November 7, 2020, 08:56 |
|
#12 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
Dear Time, I followed all the steps you posted, but it is getting the following error in solver compilation: Code:
periodiccfdemSolverIB.C:(.text.startup+0x28a4): undefined reference to `Foam::fv::optionList::setUbar(Foam::Vector<double>&)' collect2: error: ld returned 1 exit status Last edited by mostanad; November 7, 2020 at 23:52. |
||
November 7, 2020, 09:34 |
|
#13 | |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
Quote:
Well, this is weird. I don't have OpenFOAM-5.x compiled on my computer, so I can't reproduce this error. I tested it under OpenFOAM-2.3./2.3x and it's okay. So I recommend you to check the code again because i don't know what's the difference between 2.3.x and 5.x. See the attachment of my case for more info. Timo |
||
November 7, 2020, 09:41 |
|
#14 |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
I think this is because of this part:
Code:
this->operator[](i).setUbar(UbarTarget); Code:
option& source = this->operator[](i);; source.setUbar(UbarTarget); Thank you again. |
|
November 7, 2020, 09:52 |
|
#15 | |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
Quote:
Timo |
||
November 7, 2020, 11:44 |
|
#16 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
Thanks Mohammad |
||
November 7, 2020, 11:53 |
|
#17 |
New Member
Timo ZHANG
Join Date: Aug 2016
Posts: 27
Rep Power: 10 |
I'm not in my office so I can't sent you the code right now.
But it's very simple. My test solver is just the original pimpleFoam solver of OpenFOAM-2.3.1.I only add that two lines in the end of UEqn.H file. Timo |
|
November 7, 2020, 23:53 |
|
#18 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Quote:
|
||
November 9, 2020, 03:59 |
|
#19 |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 281
Rep Power: 12 |
Finally, the modification suggested by Timo worked for me. I just recompiled all the src library again.
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Is there a way to access the gradient limiter in Fluent ? | CFDYourself | FLUENT | 1 | February 16, 2016 06:49 |
How can I access to a volTensorField elements? urgent!!! | adambarfi | OpenFOAM Running, Solving & CFD | 6 | May 14, 2013 17:28 |
Urgent: Attempt to access un-allocated densityLe | devesh.baghel | STAR-CCM+ | 0 | July 19, 2011 03:44 |
Urgent! Access violation of UDF using VOF | Rucy | FLUENT | 0 | January 9, 2006 05:01 |
Error message!! plz help!! Urgent | SAM | FLUENT | 2 | February 1, 2005 13:11 |