|
[Sponsors] |
November 8, 2021, 11:45 |
Running python file/function within OpenFOAM
|
#1 |
New Member
Adrian del Ser
Join Date: Oct 2021
Posts: 15
Rep Power: 5 |
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! |
|
November 8, 2021, 12:13 |
|
#2 |
Senior Member
Join Date: Apr 2020
Location: UK
Posts: 736
Rep Power: 14 |
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.
|
|
November 8, 2021, 12:19 |
|
#3 | |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40 |
Quote:
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. |
||
November 8, 2021, 12:41 |
|
#4 | |
New Member
Adrian del Ser
Join Date: Oct 2021
Posts: 15
Rep Power: 5 |
Quote:
Perhaps this is a well known issue for which solutions exist that I am unaware of! |
||
November 8, 2021, 12:44 |
|
#5 |
Senior Member
Join Date: Apr 2020
Location: UK
Posts: 736
Rep Power: 14 |
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!
|
|
November 8, 2021, 16:01 |
|
#6 | |
New Member
Adrian del Ser
Join Date: Oct 2021
Posts: 15
Rep Power: 5 |
Quote:
|
||
Tags |
c++, cython, python, python support |
|
|
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 |