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

Running python file/function within OpenFOAM

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 8, 2021, 11:45
Question Running python file/function within OpenFOAM
  #1
New Member
 
Adrian del Ser
Join Date: Oct 2021
Posts: 15
Rep Power: 5
adrian.delser is on a distinguished road
Hello everyone,


Let's say I wish to call a simple python function (defined in its own separate .py file) from a custom foam solver. The function will take an input (for example the velocity field: volVectorField U), and return a modified version of it. Let's say for simplicity we just want to take the velocity values defined for each cell centre, flip the sign of each component of velocity (just as a test) and return this to C++ as a modified volVectorField.



The simplified question is: how can I use a variable currently defined in the solver in C++ as an input of a python function and then use the output of that python function back inside C++.


I have seen generic implementations using " #include Python.h ", but none of these use variables present in the C++ code as inputs for the python code.


Thanks!
adrian.delser is offline   Reply With Quote

Old   November 8, 2021, 12:13
Default
  #2
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 736
Rep Power: 14
Tobermory will become famous soon enough
Have you thought about writing to a scratch file, instead of passing the data as parameters? Not as elegant, but easy to do in both directions.
Tobermory is offline   Reply With Quote

Old   November 8, 2021, 12:19
Default
  #3
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by adrian.delser View Post
Hello everyone,


Let's say I wish to call a simple python function (defined in its own separate .py file) from a custom foam solver. The function will take an input (for example the velocity field: volVectorField U), and return a modified version of it. Let's say for simplicity we just want to take the velocity values defined for each cell centre, flip the sign of each component of velocity (just as a test) and return this to C++ as a modified volVectorField.



The simplified question is: how can I use a variable currently defined in the solver in C++ as an input of a python function and then use the output of that python function back inside C++.


I have seen generic implementations using " #include Python.h ", but none of these use variables present in the C++ code as inputs for the python code.

Thanks!

I haven't yet gotten around to doing these wrappings, but as a slight brute-force method you could follow what is being done here:


https://github.com/argonne-lcf/Pytho...thonComm.H#L74


but I really believe that it should be possible to simply define a wrapped numpy array pointing to the volField:rimitiveField::data() address, with the appropriate stride according to pTraits nComponents - however I haven't yet found the time to test.


If that works out properly (please ping me on develop.openfoam.com if you get something workable), it should be possible to build a bidirectional wrapper, but you will still need a correctBoundaryConditions after doing any modifications behind the scenes. Could probably make it have access to any field registered on the object registry.
olesen is offline   Reply With Quote

Old   November 8, 2021, 12:41
Default
  #4
New Member
 
Adrian del Ser
Join Date: Oct 2021
Posts: 15
Rep Power: 5
adrian.delser is on a distinguished road
Quote:
Originally Posted by Tobermory View Post
Have you thought about writing to a scratch file, instead of passing the data as parameters? Not as elegant, but easy to do in both directions.
I have thought of that, but I'm not entirely sure to tell python to work on that file every time it gets updated by openfoam. Basically, I'm testing a deep learning poisson solver (implemented in python) to replace the classical cfd methods of correcting the velocity field as a way to save time. This means that for every temporal iteration, openfoam would have to write the unocorrected (non divergence free) velocity field to a scratch file and I would have to somehow tell python to correct that field as soon as openfoam updates the file and then tell openfoam to do the same when python is done correcting the field. I'm not quite sure how to tell either program to read the file only when it has been changed by the other.



Perhaps this is a well known issue for which solutions exist that I am unaware of!
adrian.delser is offline   Reply With Quote

Old   November 8, 2021, 12:44
Default
  #5
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 736
Rep Power: 14
Tobermory will become famous soon enough
Aah - with that much data being transferred so regularly, then a scratch file is not very efficient/clever. Look at Mark's response above - that's the pro way to do it!
Tobermory is offline   Reply With Quote

Old   November 8, 2021, 16:01
Default
  #6
New Member
 
Adrian del Ser
Join Date: Oct 2021
Posts: 15
Rep Power: 5
adrian.delser is on a distinguished road
Quote:
Originally Posted by olesen View Post
I haven't yet gotten around to doing these wrappings, but as a slight brute-force method you could follow what is being done here:


https://github.com/argonne-lcf/Pytho...thonComm.H#L74


but I really believe that it should be possible to simply define a wrapped numpy array pointing to the volField:rimitiveField::data() address, with the appropriate stride according to pTraits nComponents - however I haven't yet found the time to test.


If that works out properly (please ping me on develop.openfoam.com if you get something workable), it should be possible to build a bidirectional wrapper, but you will still need a correctBoundaryConditions after doing any modifications behind the scenes. Could probably make it have access to any field registered on the object registry.
Thanks! This is all a little new to me still but I’ll spend some time figuring out what you’ve suggested and I’ll get back if I make progress (or with questions if not)
adrian.delser is offline   Reply With Quote

Reply

Tags
c++, cython, python, python support


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
How to contribute to the community of OpenFOAM users and to the OpenFOAM technology wyldckat OpenFOAM 17 November 10, 2017 16:54
OpenFOAM Training, London, Chicago, Munich, Houston 2016-2017 cfd.direct OpenFOAM Announcements from Other Sources 0 September 14, 2016 04:19
[OpenFOAM.org] A Mac OS X of23x Development Environment Using Docker rt08 OpenFOAM Installation 1 February 28, 2016 20:00
Python for openfoam Divyaprakash OpenFOAM Programming & Development 1 August 22, 2014 10:05
Statically Compiling OpenFOAM Issues herzfeldd OpenFOAM Installation 21 January 6, 2009 10:38


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