
[Sponsors] 
March 17, 2016, 12:34 
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: 2 
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:
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. 

March 18, 2016, 16:57 

#2 
Member
Thomas Boucheres
Join Date: May 2013
Posts: 38
Rep Power: 5 
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)? 

March 19, 2016, 06:00 

#3 
Senior Member
Hassan Kassem
Join Date: May 2010
Location: UK
Posts: 231
Rep Power: 10 
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. 

March 19, 2016, 11:56 

#4 
New Member
Carlos Baptista
Join Date: Feb 2016
Location: Rotterdam, The Netherlands
Posts: 14
Rep Power: 2 
Hello guys! Thank you for your replies
@thomasArk47 in my specific case I have a very small Ogrid around an airfoil. Because the numerical boundary is very close the airfoil I need to impose a nonuniform 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 dropin 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. 

March 20, 2016, 10:28 

#5 
Member
Thomas Boucheres
Join Date: May 2013
Posts: 38
Rep Power: 5 
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. 

March 22, 2016, 07:50 

#6 
Senior Member
Hassan Kassem
Join Date: May 2010
Location: UK
Posts: 231
Rep Power: 10 
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 

March 22, 2016, 08:31 

#7  
Senior Member
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 1,163
Rep Power: 20 
Quote:
__________________
*On twitter @akidTwit *Spend as much time formulating your questions as you expect people to spend on their answer. *Join the OpenFOAM stackexchange Q&A site: http://area51.stackexchange.com/prop...oHPxcPqde7HtA2 

March 23, 2016, 05:50 

#8 
New Member
Carlos Baptista
Join Date: Feb 2016
Location: Rotterdam, The Netherlands
Posts: 14
Rep Power: 2 
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. 

Tags 
c++ solver class, embedding openfoam, interfacing openfoam 
Thread Tools  
Display Modes  


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  0  September 7, 2015 13:26 
OpenFOAM Training, London, Chicago, Munich, SepOct 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 13:55 
Building a custom solver on OpenFOAM 2.0  wschosta  OpenFOAM Programming & Development  1  July 8, 2011 15:07 