CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

How to update fvSchemes at run time

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

Like Tree1Likes
  • 1 Post By Jerryfan

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 27, 2016, 21:33
Default How to update fvSchemes at run time
  #1
Member
 
Jack
Join Date: Dec 2011
Posts: 94
Rep Power: 14
ripperjack is on a distinguished road
Hi guys,

I would like to update the div(phi,U) scheme within one iteration at runtime. I want to calculate the div(phi,U) term using both 1st and 2nd order schemes. I did the following and I managed to change the div scheme from the default "bounded Gauss linear" to "bounded Gauss upwind".

Code:
volVectorField div( fvc::div(phi,U) );
    
Info<<"Initial Div: "<< div[100] <<endl;

const objectRegistry& db = mesh.thisDb();

IOdictionary& fvSchemeDict = const_cast<IOdictionary&>(db.lookupObject<IOdictionary>("fvSchemes") );
     
dictionary& tmp ( fvSchemeDict.subDict("divSchemes")) ;
     
tmp.set("div(phi,U)","bounded Gauss upwind");

volVectorField div1( fvc::div(phi,U) );
    
Info<<"Modified Div: "<< div1[100] <<endl;
However, the div and div1 values calculated above are identical which tells me that although the fvScheme is changed, the fvc::div operator does not detect the change.. I guess I need to do something to tell fvc::div to use the modified div scheme. Any idea? Tons of thanks in advance!

Best regards,
Jack
ripperjack is offline   Reply With Quote

Old   July 27, 2016, 21:36
Default
  #2
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18
anishtain4 is on a distinguished road
why are you casting IOdictionary when you have it on both sides?
anishtain4 is offline   Reply With Quote

Old   July 27, 2016, 21:45
Default
  #3
Member
 
Jack
Join Date: Dec 2011
Posts: 94
Rep Power: 14
ripperjack is on a distinguished road
Quote:
Originally Posted by anishtain4 View Post
why are you casting IOdictionary when you have it on both sides?
Hi Mahdi,

Thanks very much for your reply!

Do u mean this line?

Code:
IOdictionary& fvSchemeDict = const_cast<IOdictionary&>( db.lookupObject<IOdictionary>("fvSchemes") );
I did the con_cast since I would like to change the IOdictionary. I am not sure if it is necessary but I did output the new fvSchemeDict

Code:
Info<<"Modified fvScheme "<<fvSchemeDict<<endl;

And the div(phi,U) scheme is indeed changed to "bounded Gauss upwind". It will be great if you can help me to check if I change the fvScheme in a right way. Thanks very much in advance!

Best regards,
Jack
ripperjack is offline   Reply With Quote

Old   July 27, 2016, 21:52
Default
  #4
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18
anishtain4 is on a distinguished road
Yeah I realized that you wanted the constness to be casted away after I took a closer look. Not sure how div works though. Gotta take a look at the source code.
anishtain4 is offline   Reply With Quote

Old   July 28, 2016, 00:21
Default
  #5
Member
 
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 12
Jerryfan is on a distinguished road
Quote:
And the div(phi,U) scheme is indeed changed to "bounded Gauss upwind". It will be great if you can help me to check if I change the fvScheme in a right way.

Hi ripperjack,


I come up with another way to walk around. Instead of trying to reset the values of the fvSchemes dictionary, try to add another entry in the fvSchemes file. For example, add a new entry as div(phi,U1) bounded Gauss upwind grad(U1) in the divSchemes sub-dictionary. As for why it doesn't work just by resetting is because, in the evaluation of the div operation, it reads the scheme from the divSchemes_ rather than fvSchemes directly. I tracked the source code, it seems that it's pretty hard to reset this divSchemes_. If someone can think of a way to reset this divSchemes_ dictionary, please add.

Quote:
volVectorField div( fvc::div(phi,U) );

Info<<"Initial Div: "<< div[100] <<endl;

const volVectorField U1("U1", U);
volVectorField div1( fvc::div(phi,U1) );
Info<<"Modified Div: "<< div1[100] <<endl;
This should work fine.
Jerryfan is offline   Reply With Quote

Old   July 28, 2016, 00:52
Default
  #6
Member
 
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 12
Jerryfan is on a distinguished road
Quote:
Hi ripperjack,


I come up with another way to walk around. Instead of trying to reset the values of the fvSchemes dictionary, try to add another entry in the fvSchemes file. For example, add a new entry as div(phi,U1) bounded Gauss upwind grad(U1) in the divSchemes sub-dictionary. As for why it doesn't work just by resetting is because, in the evaluation of the div operation, it reads the scheme from the divSchemes_ rather than fvSchemes directly. I tracked the source code, it seems that it's pretty hard to reset this divSchemes_. If someone can think of a way to reset this divSchemes_ dictionary, please add.

Quote:
volVectorField div( fvc::div(phi,U) );

Info<<"Initial Div: "<< div[100] <<endl;

const volVectorField U1("U1", U);
volVectorField div1( fvc::div(phi,U1) );
Info<<"Modified Div: "<< div1[100] <<endl;
This should work fine.
A follow-up of my previous reply, we can simply add a new member function to the fvSchemes class. Or define a new class that is inherited from fvSchemes by adding this new member function.

dictionary& Foam::fvSchemes:: divSchemeDic ()
{
return divSchemes_;
}

or

dictionary& Foam::myfvSchemes:: divSchemeDic () //myfvScheme is derived from fvSchemes
{
return divSchemes_;
}


Then recompile fvSchemes class.
And in the implementation, we then try to reset divSchemes_ dictionary.

Quote:
volVectorField div( fvc::div(phi,U) );
Info<<"Initial Div: "<< div[100] <<endl;
const objectRegistry& db = mesh.thisDb();
fvSchemes& fvSchemeDict = const_cast<fvSchemes&>(db.lookupObject<fvSchemes>( "fvSchemes") );
fvSchemeDict.divSchemeDic().set("div(phi,U)","boun ded Gauss upwind");
volVectorField div1( fvc::div(phi,U) );
Info<<"Modified Div: "<< div1[100] <<endl;
Jerryfan is offline   Reply With Quote

Old   July 28, 2016, 09:23
Default
  #7
Member
 
Jack
Join Date: Dec 2011
Posts: 94
Rep Power: 14
ripperjack is on a distinguished road
Quote:
Originally Posted by Jerryfan View Post
A follow-up of my previous reply, we can simply add a new member function to the fvSchemes class. Or define a new class that is inherited from fvSchemes by adding this new member function.

dictionary& Foam::fvSchemes:: divSchemeDic ()
{
return divSchemes_;
}

or

dictionary& Foam::myfvSchemes:: divSchemeDic () //myfvScheme is derived from fvSchemes
{
return divSchemes_;
}


Then recompile fvSchemes class.
And in the implementation, we then try to reset divSchemes_ dictionary.
Hi Jerry,

Thank you so much for your reply! Your last post is exactly what I need! I will try and let you know how it goes.

Thanks,
Jack
ripperjack is offline   Reply With Quote

Old   July 28, 2016, 13:00
Default
  #8
Member
 
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 12
Jerryfan is on a distinguished road
Hi jack,



I come up with a even simpler way to work around. We can rename the name of U variable to "U1". And read the scheme for U1 like what I suggested defining another variable before and adding an extra entry for U1 in the fvSchemes file. Therefore, in this case, it's the same variable, but without causing extra memory usage. The only thing that is changed is the name.


Quote:
volVectorField div( fvc::div(phi,U) );
Info<<"Initial Div: "<< div[100] <<endl;
U.rename("U1");
volVectorField div1( fvc::div(phi,U) );
Info<<"Modified Div: "<< div1[100] <<endl;

Best regards,

Jerry
LogiDF likes this.
Jerryfan is offline   Reply With Quote

Old   July 28, 2016, 13:08
Default
  #9
Member
 
Jack
Join Date: Dec 2011
Posts: 94
Rep Power: 14
ripperjack is on a distinguished road
Quote:
Originally Posted by Jerryfan View Post
Hi jack,



I come up with a even simpler way to work around. We can rename the name of U variable to "U1". And read the scheme for U1 like what I suggested defining another variable before and adding an extra entry for U1 in the fvSchemes file. Therefore, in this case, it's the same variable, but without causing extra memory usage. The only thing that is changed is the name.





Best regards,

Jerry
Wow, that works like a charm! Thank you so much Jerry!

Cheers,
Jack
ripperjack is offline   Reply With Quote

Old   July 28, 2016, 13:21
Default
  #10
Member
 
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 12
Jerryfan is on a distinguished road
Hi Jack,



I thought about it for quite long. That's the beauty of O.F. There are always ways to work around. But that's also the downside, sometimes. ^-^. Good luck to you.


Best regards,
jerry
Jerryfan is offline   Reply With Quote

Old   July 28, 2016, 13:34
Default
  #11
Member
 
Jack
Join Date: Dec 2011
Posts: 94
Rep Power: 14
ripperjack is on a distinguished road
Quote:
Originally Posted by Jerryfan View Post
Hi Jack,



I thought about it for quite long. That's the beauty of O.F. There are always ways to work around. But that's also the downside, sometimes. ^-^. Good luck to you.


Best regards,
jerry
I totally agree with you. And this is the beauty of this forum as well! There are always nice people like u helping around. I have been stuck for a few days and now it finally works. Thanks again for your help!
ripperjack is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Floating point exception error lpz_michele OpenFOAM Running, Solving & CFD 53 October 19, 2015 02:50
Star cd es-ice solver error ernarasimman STAR-CD 2 September 12, 2014 00:01
How to write k and epsilon before the abnormal end xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 15:33
plot over time fferroni OpenFOAM Post-Processing 7 June 8, 2012 07:56
Upgraded from Karmic Koala 9.10 to Lucid Lynx10.04.3 bookie56 OpenFOAM Installation 8 August 13, 2011 04:03


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