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

How to rewrite a standard OpenFOAM solver as a C++ class

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 17, 2016, 11:34
Default How to rewrite a standard OpenFOAM solver as a C++ class
  #1
New Member
 
Carlos Baptista
Join Date: Feb 2016
Location: Rotterdam, The Netherlands
Posts: 14
Rep Power: 10
cfbaptista is on a distinguished road
Standard OpenFOAM solvers are complete applications. This implies they do everything: read configuration files, initialize case and (for each time step) solve equations and write out data. This makes it difficult to embed an OpenFOAM solver into a 3rd party solver in a flexible manner.

I would like to know how to rewrite a standard OpenFOAM solver as a C++ class. By defining variables that need to remain in RAM as properties of the class and by defining actions (e.g. set boundary conditions, perform single time step, write out data) as methods of the class, I can instruct OpenFOAM to do what I want whenever I want.

My final objective is to rewrite pisoFoam as a C++ solver class for external flow cases (e.g. airfoil simulations). This class should contain methods to do at least the following:
  1. Set Dirichlet boundary conditions for the velocity field on the numerical boundary
  2. Perform a single time step

Can someone help me identify which variables I need to define as properties, or point me to documentation/tutorials dealing with similar things?

ps: Doing it the other way around (e.g. embedding the 3rd party solver inside OpenFOAM) is, unfortunately, not an option.
cfbaptista is offline   Reply With Quote

Old   March 18, 2016, 15:57
Default
  #2
Member
 
Thomas Boucheres
Join Date: May 2013
Posts: 41
Rep Power: 12
thomasArk47 is on a distinguished road
Hello,

at first glance, embedding a solver in a class is not a big concern by itself but what could be much more time consuming is to adapt the data structure of OpenFoam (fields and mesh roughly speaking) to the external solver. Maybe not necessary nevertheless. It depends on the precise coupling you want to do. What is it? Boundary conditions coupling only? (i.e different meshes for the two solvers) Otherwise? If just boundary conditions, is your Dirichlet value uniform on space (I believe it is not the case)?
thomasArk47 is offline   Reply With Quote

Old   March 19, 2016, 05:00
Default
  #3
Senior Member
 
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17
hk318i is on a distinguished road
Hello,
The idea seems possible despite the fact that the OpenFOAM is not designed to support that by default.
I have two suggestions first have a look at externalCoupled BC [LINK]. Maybe it is not a very efficient way but it could be a good starting point.
My second suggestion is to create hybrid application (solver) which basically combines pisoFoam and your 3dparty solver within one main function. In this case you will need few functions or classes to convert the OpenFOAM data types to/from 3dparty solver.
hk318i is offline   Reply With Quote

Old   March 19, 2016, 10:56
Default
  #4
New Member
 
Carlos Baptista
Join Date: Feb 2016
Location: Rotterdam, The Netherlands
Posts: 14
Rep Power: 10
cfbaptista is on a distinguished road
Hello guys! Thank you for your replies

@thomasArk47 in my specific case I have a very small O-grid around an airfoil. Because the numerical boundary is very close the airfoil I need to impose a non-uniform Dirichlet boundary condition for the velocity field on the numerical boundary at each time step. I use the second solver (the one into which I want to embed the OpenFOAM solver) to compute the values I need to impose. So, basically, I want to set up OpenFOAM such that it solves for a single time step as function of an input boundary condition.

@hk318i the framework for the hybrid solver already exists, works and has been validated. We are currently replacing one of the solvers with OpenFOAM. So we are really attempting to achieve a drop-in replacement. For this to work we need to set up OpenFOAM such that it solves for a single time step as function of an input boundary condition.
cfbaptista is offline   Reply With Quote

Old   March 20, 2016, 09:28
Default
  #5
Member
 
Thomas Boucheres
Join Date: May 2013
Posts: 41
Rep Power: 12
thomasArk47 is on a distinguished road
Hello cfbatista,

had you have progress in your problem?

If not, I think we can help you but it would be necessary you share your framework for which you wish to change a solver for OF.
thomasArk47 is offline   Reply With Quote

Old   March 22, 2016, 06:50
Default
  #6
Senior Member
 
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17
hk318i is on a distinguished road
I would start with base class or even abstract class which has the common functions between solver such as reading mesh, createFields (U,P) and few virtual functions for the thermophysical models, .. etc. Then for each new solver, it should be inherited for the base class. The most important function for your case, I believe, is BC update which be done using dynamic cast and (==) operator.
It is just a very rough design idea, you should tailor the base class to work as interface with the 3rdParty code.

I think the main challenge is to design a general and abstract framework which could be used with many solvers as possible.
Bw,
Hassan
hk318i is offline   Reply With Quote

Old   March 22, 2016, 07:31
Default
  #7
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Germany
Posts: 1,377
Rep Power: 29
akidess will become famous soon enough
Quote:
Originally Posted by cfbaptista View Post
I use the second solver (the one into which I want to embed the OpenFOAM solver) to compute the values I need to impose. So, basically, I want to set up OpenFOAM such that it solves for a single time step as function of an input boundary condition.

@hk318i the framework for the hybrid solver already exists, works and has been validated. We are currently replacing one of the solvers with OpenFOAM. So we are really attempting to achieve a drop-in replacement. For this to work we need to set up OpenFOAM such that it solves for a single time step as function of an input boundary condition.
A thought without going through the entire thread - have you looked into the externalCoupled boundary condition?
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
akidess is offline   Reply With Quote

Old   March 23, 2016, 04:50
Default
  #8
New Member
 
Carlos Baptista
Join Date: Feb 2016
Location: Rotterdam, The Netherlands
Posts: 14
Rep Power: 10
cfbaptista is on a distinguished road
Thanks guys for all the help!

@thomasArk47, I have compiled a function based on pisoFoam which performs only a single time step into a dynamic library. Currently, I am figuring out how to import this function into a Python using PyBoost (the hybrid framework is written in Python). After that I will focus on how to modify BC in OpenFOAM. After I have a feel of how things are done in OpenFOAM I start rewriting the code into a solver class.

@hk318i, as a first step I would like to build just one dedicated solver. After I have a hang on how the OpenFOAM API works I can look into making my work more general and applicable to a range of solvers.

@akidess, I still haven't looked into externalCoupled yet, but will do shortly.
cfbaptista is offline   Reply With Quote

Reply

Tags
c++ solver class, embedding openfoam, interfacing openfoam


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
Which would be the most suitable standard solver in ejector problem? jexposito OpenFOAM Running, Solving & CFD 1 October 25, 2019 10:58
OpenFOAM Training, London, Chicago, Munich, Sep-Oct 2015 cfd.direct OpenFOAM Announcements from Other Sources 2 August 31, 2015 13:36
Can't get data from OpenFoam to external solver using externalCoupled perry OpenFOAM Running, Solving & CFD 4 May 26, 2014 08:09
Possible bug in OpenFoam Interpolation class MMC15 OpenFOAM Bugs 2 March 23, 2014 12:55
Building a custom solver on OpenFOAM 2.0 wschosta OpenFOAM Programming & Development 1 July 8, 2011 15:07


All times are GMT -4. The time now is 07:09.