udf也能用Python

具体步骤见《fluent加载第三方(C++,Fortran)动态链接库》

我们对导入的动态链接库进行改动

打开VS2013

 

udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python

完成了上述过程以后,还需要配置Python

首先需要安装Python,我安装的是anaconda2(可到清华大学开源软件镜像站下载,链接:https:// mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 

udf也能用Python

udf也能用Python

udf也能用Python

udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python
udf也能用Python

源代码

#include / *输入输出流头文件*/

#include / *Python头文件*/

#include  / *字符串头文件*/

#include

 

using namespace std;

 

#ifdef __cplusplus

   #define EXPORT extern "C" __declspec(dllexport) / *如果是c++则以c函数的调用约定导出函数*/

#else

   #define EXPORT __declspec(dllexport)

#endif

 

EXPORT void PythonPrint()

{

   Py_Initialize();  / *初始化Python*/

   PyObject* osModule = PyImport_ImportModule("os"); / *加载Pythonos模块*/

   PyObject* listDirFunc = PyObject_GetAttrString(osModule, "listdir");   / *加载pythonos模块的listdir函数*/

   PyObject* fileList = PyObject_CallFunction(listDirFunc, "s", "."); / *调用os中的listdir函数,向其中传入算例目录*/

   int listLen = static_cast(PyObject_Size(fileList));  / *获取返回列表的大小*/

   PyObject *listItem = NULL / *用来存放返回Python列表的每一个元素*/

   string strItem; / *用来存放将Python字符串转换为c++字符串*/

   for (int i = 0; i < listLen; i++)

   {

      listItem = PyList_GetItem(fileList, i); / *获取返回列表的元素*/

      strItem = PyString_AsString(listItem);  / *将返回的元素转换为c++类型的字符串*/

      cout << setiosflags(ios::right);

      cout << setw(40) << strItem;   / *输出文件和目录*/

      if ((i + 1) % 4 == 0)

         cout << endl;

   }

   cout << endl;

   Py_Finalize(); / *结束python解释器,释放资源*/

}

———————————————————————————————————————————————————————————————————————————

UDF源代码如下:

#include "udf.h"

 

/ *链接我们刚才动态链接库的函数*/

extern void PythonPrint();

 

/ ****************************************************

*我们在每次迭代完成以后,

*用动态链接库中的函数输出算例目录下的所有文件和文件夹

****************************************************/

DEFINE_ADJUST(my_adjust,d)

{

   PythonPrint(); / *调用动态链接库中的函数*/

}

udf也能用Python

我们打开Fluent 

udf也能用Python

导入网格,设置好各种求解参数

加载编译好的UDF 

udf也能用Python
udf也能用Python

求解运行算例 

udf也能用Python

posted @ 2017-06-12 20:19  硫酸亚铜  阅读(2084)  评论(1编辑  收藏  举报