UDF and Python script

Prabin Pradhananga_22497
Prabin Pradhananga_22497 Altair Community Member
edited July 2022 in Community Q&A

Hi guys,

I have been working with acuSolve user-defined function (UDF) and am now interested to call a python script from inside a UDF. This is necessary to me because I am working with some code that would otherwise be very difficult to be written in C. I was wondering if I would be able to call a python script from inside the UDF because UDF is written in C. The flow sequence of the program would be:-

1. Call UDF from acuSolve

2. Call python from UDF and pass arguments

3. Perform calculations inside python script

4. Return arguments to UDF

 

Thanks in advance.

 

Answers

  • acupro
    acupro
    Altair Employee
    edited July 2022

    I'll add some comments from internal discussions on the subject:

    #####

    Calling python from C , if the end user has the capability, is something they can do. AcuMakeDll may not include the right links etc., but a user ( again if they know how to do it, link python.lib etc ), can do it on their own.

    The installation does include python24, but nothing is stopping the user from including a newer version of python (AcuSolve executable is NEVER linked against python ), like the one in HyperWorks ( 3.8.10 ), they can use that too. In that case, at run time, python38.dll should be in the path.

    #####

    Calling Python in C is straightforward, users need some makefile knowledge the tricky part though is safely passing and receiving pointers.

    If I have python script and willing to call it from AcuSolve UDF, I think converting the python script to C will be an easier and more efficient option.

    #####

    Bottom line - it's not really something built-in, but you may be able to do it.

  • Prabin Pradhananga_22497
    Prabin Pradhananga_22497 Altair Community Member
    edited July 2022

    I'll add some comments from internal discussions on the subject:

    #####

    Calling python from C , if the end user has the capability, is something they can do. AcuMakeDll may not include the right links etc., but a user ( again if they know how to do it, link python.lib etc ), can do it on their own.

    The installation does include python24, but nothing is stopping the user from including a newer version of python (AcuSolve executable is NEVER linked against python ), like the one in HyperWorks ( 3.8.10 ), they can use that too. In that case, at run time, python38.dll should be in the path.

    #####

    Calling Python in C is straightforward, users need some makefile knowledge the tricky part though is safely passing and receiving pointers.

    If I have python script and willing to call it from AcuSolve UDF, I think converting the python script to C will be an easier and more efficient option.

    #####

    Bottom line - it's not really something built-in, but you may be able to do it.

    Currently, I am running a linux machine. Outside of acuSolve, I tried to run python script from a C file. It worked well as desired. I included all necessary header files including Python.h, edited .bashrc file to provide necessary path to the library and used required flags during gcc compilation. 

    However, when I tried to run the same code from inside the UDF, it reports an error. acuSolve declines to even recognize the UDF function prototype. The log file reports the following error.

    image

  • acupro
    acupro
    Altair Employee
    edited July 2022

    Currently, I am running a linux machine. Outside of acuSolve, I tried to run python script from a C file. It worked well as desired. I included all necessary header files including Python.h, edited .bashrc file to provide necessary path to the library and used required flags during gcc compilation. 

    However, when I tried to run the same code from inside the UDF, it reports an error. acuSolve declines to even recognize the UDF function prototype. The log file reports the following error.

    image

    What exact commands did you use to compile/generate the libusr.so library?  Did you use acuMakeLib or gcc to compile?

  • Prabin Pradhananga_22497
    Prabin Pradhananga_22497 Altair Community Member
    edited July 2022

    What exact commands did you use to compile/generate the libusr.so library?  Did you use acuMakeLib or gcc to compile?

    There's an update--I am able to call python script from UDF. I ran simple python scripts like printing 'Hello World'. I checked the log file where I could see 'Hello World' lines. I didn't use acuMakeLib to compile because it did not allow me to add python flags. Again, I was able to compile the code without any error--I successfully obtained libusr.so file. However, during the execution of acuRun, I encountered error.

    The commands I used are:-

    1. gcc -fPIC -DACUSIM_LINUX64 -c -I/home/.../altair/altair/hwcfdsolvers/acusolve/linux64/include $(python3-config --cflags) -O  file1.c

    (where ... is user name for current profile)

    2. gcc -o libusr.so -shared -fPIC file1.o -lm $(python3-config --embed --ldflags)

    3. acuRun -libs libusr.so

    image

  • acupro
    acupro
    Altair Employee
    edited July 2022

    There's an update--I am able to call python script from UDF. I ran simple python scripts like printing 'Hello World'. I checked the log file where I could see 'Hello World' lines. I didn't use acuMakeLib to compile because it did not allow me to add python flags. Again, I was able to compile the code without any error--I successfully obtained libusr.so file. However, during the execution of acuRun, I encountered error.

    The commands I used are:-

    1. gcc -fPIC -DACUSIM_LINUX64 -c -I/home/.../altair/altair/hwcfdsolvers/acusolve/linux64/include $(python3-config --cflags) -O  file1.c

    (where ... is user name for current profile)

    2. gcc -o libusr.so -shared -fPIC file1.o -lm $(python3-config --embed --ldflags)

    3. acuRun -libs libusr.so

    image

    Probably better to convert it to C, main reason is passing variables from Python to C.  Having hello world working is easy but running python and actually using the output in C will not be easy.

  • Prabin Pradhananga_22497
    Prabin Pradhananga_22497 Altair Community Member
    edited July 2022

    Probably better to convert it to C, main reason is passing variables from Python to C.  Having hello world working is easy but running python and actually using the output in C will not be easy.

    As I mentioned earlier, I am able to call python from C, pass argument to python from C, perform calculation and return result from python to C. Calling python from C outside of acuSolve is doable, but I am wondering why acuSolve is not allowing me to do that. 

    Thanks for your help.

  • acupro
    acupro
    Altair Employee
    edited July 2022

    As I mentioned earlier, I am able to call python from C, pass argument to python from C, perform calculation and return result from python to C. Calling python from C outside of acuSolve is doable, but I am wondering why acuSolve is not allowing me to do that. 

    Thanks for your help.

    Are you able to attach the complete AcuSolve .inp and .Log files - along with the C and Python codes?

  • Prabin Pradhananga_22497
    Prabin Pradhananga_22497 Altair Community Member
    edited July 2022

    Are you able to attach the complete AcuSolve .inp and .Log files - along with the C and Python codes?

    No, I am not able to do that. But, I was able to run the entire C-Python code outside of acuSolve environment.