CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   How to rewrite a standard OpenFOAM solver as a C++ class (https://www.cfd-online.com/Forums/openfoam-programming-development/168218-how-rewrite-standard-openfoam-solver-c-class.html)

cfbaptista March 17, 2016 11:34

How to rewrite a standard OpenFOAM solver as a C++ class
 
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.

thomasArk47 March 18, 2016 15:57

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)?

hk318i March 19, 2016 05:00

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.

cfbaptista March 19, 2016 10:56

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.

thomasArk47 March 20, 2016 09:28

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.

hk318i March 22, 2016 06:50

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

akidess March 22, 2016 07:31

Quote:

Originally Posted by cfbaptista (Post 590577)
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?

cfbaptista March 23, 2016 04:50

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.


All times are GMT -4. The time now is 19:20.