ansys / pyfluent

Pythonic interface to Ansys Fluent
https://fluent.docs.pyansys.com
MIT License
276 stars 41 forks source link

Don't support hook udf function #2327

Closed sjhsbhqf closed 6 months ago

sjhsbhqf commented 10 months ago

πŸ” Before submitting the issue

🐞 Description of the bug

error message when execute solver.tui.define.user_defined.function_hooks.execute_at_end('"execute_at_end::libudf"')

invalid command ["execute_at_end"]

πŸ“ Steps to reproduce

I want to implement the functionality of the TUI command below

TUI command /define/user_defined/function_hooks/execute_at_end "execute_at_end::libudf"

According to the official documentation, the correct Python command is

solver.tui.define.user_defined.function_hooks.execute_at_end('"execute_at_end::libudf"')

But I got an error message

invalid command ["execute_at_end"]

πŸ’» Which operating system are you using?

Windows

πŸ“€ Which ANSYS version are you using?

ANSYS 2021R1

🐍 Which Python version are you using?

3.9

πŸ“¦ Installed packages

accessible-pygments==0.0.4
alabaster==0.7.13
ansys-api-fluent==0.3.19
ansys-api-platform-instancemanagement==1.0.0
ansys-fluent-core==0.18.2
ansys-fluent-parametric==0.9.0
ansys-fluent-visualization==0.8.0
ansys-platform-instancemanagement==1.1.2
ansys-sphinx-theme==0.12.5
asttokens==2.4.1
attrs==23.1.0
Babel==2.14.0
beautifulsoup4==4.12.2
bleach==6.1.0
certifi==2023.11.17
charset-normalizer==3.3.2
colorama==0.4.6
comm==0.2.0
contourpy==1.2.0
cycler==0.12.1
decorator==5.1.1
defusedxml==0.7.1
docker==7.0.0
docutils==0.20.1
exceptiongroup==1.2.0
executing==2.0.1
fastjsonschema==2.19.0
fonttools==4.46.0
grpcio==1.60.0
grpcio-health-checking==1.48.2
h5py==3.10.0
idna==3.6
imagesize==1.4.1
importlib-metadata==7.0.0
importlib-resources==6.1.1
ipython==8.18.1
ipywidgets==8.1.1
jedi==0.19.1
Jinja2==3.1.2
jsonschema==4.20.0
jsonschema-specifications==2023.11.2
jupyter-sphinx==0.4.0
jupyter_client==8.6.0
jupyter_core==5.5.0
jupyterlab-widgets==3.0.9
jupyterlab_pygments==0.3.0
kiwisolver==1.4.5
lxml==4.9.3
MarkupSafe==2.1.3
matplotlib==3.8.2
matplotlib-inline==0.1.6
mistune==3.0.2
nbclient==0.9.0
nbconvert==7.12.0
nbformat==5.9.2
numpy==1.26.2
packaging==23.2
pandas==2.1.4
pandocfilters==1.5.0
parso==0.8.3
Pillow==10.1.0
platformdirs==4.1.0
pooch==1.8.0
prompt-toolkit==3.0.43
protobuf==3.20.3
psutil==5.9.7
pure-eval==0.2.2
pydata-sphinx-theme==0.14.4
Pygments==2.17.2
pyparsing==3.1.1
PySide6==6.6.1
PySide6-Addons==6.6.1
PySide6-Essentials==6.6.1
python-dateutil==2.8.2
pytz==2023.3.post1
pyvista==0.43.1
pyvistaqt==0.11.0
pywin32==306
PyYAML==6.0.1
pyzmq==25.1.2
QtPy==2.4.1
referencing==0.32.0
requests==2.31.0
rpds-py==0.15.2
scooby==0.9.2
shiboken6==6.6.1
six==1.16.0
snowballstemmer==2.2.0
soupsieve==2.5
Sphinx==7.2.6
sphinx-autodoc-typehints==1.25.2
sphinx-copybutton==0.5.2
sphinx-gallery==0.15.0
sphinx-notfound-page==1.0.0
sphinxcontrib-applehelp==1.0.7
sphinxcontrib-devhelp==1.0.5
sphinxcontrib-htmlhelp==2.0.4
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.6
sphinxcontrib-serializinghtml==1.1.9
sphinxemoji==0.2.0
stack-data==0.6.3
tinycss2==1.2.1
tornado==6.4
traitlets==5.14.0
typing_extensions==4.9.0
tzdata==2023.3
urllib3==2.1.0
vtk==9.3.20230807rc0
wcwidth==0.2.12
webencodings==0.5.1
widgetsnbextension==4.0.9
zipp==3.17.0
sjhsbhqf commented 10 months ago

When I edit the relevant source code TUI_ 231.py to the following:

class user_defined(TUIMenu):
            """
            Enters the user-defined functions and scalars menu.
            """
            def __init__(self, service, version, mode, path):
                self._service = service
                self._version = version
                self._mode = mode
                self._path = path
                self.function_hooks = self.__class__.function_hooks(service, version, mode, path + ["function_hooks"])
                self.real_gas_models = self.__class__.real_gas_models(service, version, mode, path + ["real_gas_models"])
                super().__init__(service, version, mode, path)

            def auto_compile_compiled_udfs(self, *args, **kwargs):
                """
                For this Fluent session, specify whether to allow auto-compilation of compiled UDF when a case file (or settings file) is read.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/auto_compile_compiled_udfs").execute(*args, **kwargs)
            def compiled_functions(self, *args, **kwargs):
                """
                Opens user-defined function library.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/compiled_functions").execute(*args, **kwargs)
            def use_built_in_compiler(self, *args, **kwargs):
                """
                Enables/disables the use of a built-in compiler (Clang) when the define/user-defined/compiled-functions text command is used. This text command / compiler is available for Windows only, and is provided as part of the ANSYS Fluent installation. It is recommended that you enable this text command when the compiler you installed on your machine is an older version that is no longer supported. Note that the built-in compiler is used automatically if Fluent determines that you have not installed Microsoft Visual Studio or Clang on your computer, whether this text command is enabled or not.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/use_built_in_compiler").execute(*args, **kwargs)
            def interpreted_functions(self, *args, **kwargs):
                """
                Loads interpreted user-defined functions.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/interpreted_functions").execute(*args, **kwargs)
            def execute_on_demand(self, *args, **kwargs):
                """
                Executes UDFs on demand.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/execute_on_demand").execute(*args, **kwargs)
            def user_defined_memory(self, *args, **kwargs):
                """
                Allocates user-defined memory.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/user_defined_memory").execute(*args, **kwargs)
            def user_defined_node_memory(self, *args, **kwargs):
                """
                Allocate user-defined node memory.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/user_defined_node_memory").execute(*args, **kwargs)
            def use_contributed_cpp(self, *args, **kwargs):
                """
                Enable/disable use of cpp from the Fluent.Inc/contrib directory.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/use_contributed_cpp").execute(*args, **kwargs)
            def fan_model(self, *args, **kwargs):
                """
                Configures user-defined fan model.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/fan_model").execute(*args, **kwargs)
            def one_D_coupling(self, *args, **kwargs):
                """
                Load 1D library.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/one_D_coupling").execute(*args, **kwargs)
            def user_defined_scalars(self, *args, **kwargs):
                """
                Defines user-defined scalars.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/user_defined_scalars").execute(*args, **kwargs)
            def enable_udf_on_gpu(self, *args, **kwargs):
                """
                Compile UDFs with OpenCL support.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/enable_udf_on_gpu").execute(*args, **kwargs)
            def compile_customized_addon_module(self, *args, **kwargs):
                """
                Compile customized addon module?.
                """
                return PyMenu(self._service, self._version, self._mode, "/define/user_defined/compile_customized_addon_module").execute(*args, **kwargs)
            class function_hooks(TUIMenu):
                """
                Hooks up user-defined functions.
                """
                def __init__(self, service, version, mode, path):
                    self._service = service
                    self._version = version
                    self._mode = mode
                    self._path = path
                    super().__init__(service, version, mode, path)
                def adjust(self, *args, **kwargs):
                    """
                    Hook the adjust macro.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/function_hooks/adjust").execute(*args, **kwargs)
                def execute_at_end(self, *args, **kwargs):
                    """
                    Hook the execute_at_end macro.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/function_hooks/execute_at_end").execute(*args, **kwargs)
                def execute_at_exit(self, *args, **kwargs):
                    """
                    Hook the execute_at_exit macro.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/function_hooks/execute_at_exit").execute(*args, **kwargs)
            class real_gas_models(TUIMenu):
                """
                Enters the real-gas menu to enable/configure real gas model.
                """
                def __init__(self, service, version, mode, path):
                    self._service = service
                    self._version = version
                    self._mode = mode
                    self._path = path
                    super().__init__(service, version, mode, path)
                def nist_real_gas_model(self, *args, **kwargs):
                    """
                    Loads the NIST real-gas library.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/real_gas_models/nist_real_gas_model").execute(*args, **kwargs)
                def nist_multispecies_real_gas_model(self, *args, **kwargs):
                    """
                    Loads the NIST real-gas library.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/real_gas_models/nist_multispecies_real_gas_model").execute(*args, **kwargs)
                def set_state(self, *args, **kwargs):
                    """
                    Selects the state for NIST real gas model.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/real_gas_models/set_state").execute(*args, **kwargs)
                def nist_settings(self, *args, **kwargs):
                    """
                    Specifies the name and the location for the REFPROP library and fluid files.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/real_gas_models/nist_settings").execute(*args, **kwargs)
                def user_defined_real_gas_model(self, *args, **kwargs):
                    """
                    Loads the user-defined real-gas library.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/real_gas_models/user_defined_real_gas_model").execute(*args, **kwargs)
                def user_defined_multispecies_real_gas_model(self, *args, **kwargs):
                    """
                    Loads a user-defined multispecies real-gas library.
                    """
                    return PyMenu(self._service, self._version, self._mode, "/define/user_defined/real_gas_models/user_defined_multispecies_real_gas_model").execute(*args, **kwargs)

It's very strange that I still get errors when running the code:

RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_6064\1045546776.py in <module>
----> 1 solver.tui.define.user_defined.function_hooks.execute_at_end('"execute_at_end::libudf"')

D:\Anaconda3\lib\site-packages\ansys\fluent\core\services\datamodel_tui.py in __getattribute__(self, name)
    303                 # some runtime submenus are generated as methods during codegen
    304                 path = self._path + [name]
--> 305                 if PyMenu(
    306                     self._service, self._version, self._mode, path
    307                 ).get_child_names():

D:\Anaconda3\lib\site-packages\ansys\fluent\core\services\datamodel_tui.py in get_child_names(self, include_unavailable)
    169         if include_unavailable:
    170             request.args["include_unavailable"] = 1
--> 171         response = self._service.get_attribute_value(request)
    172         return _convert_gvalue_to_value(response.value)
    173 

D:\Anaconda3\lib\site-packages\ansys\fluent\core\services\error_handler.py in func(*args, **kwargs)
     13             return f(*args, **kwargs)
     14         except grpc.RpcError as ex:
---> 15             raise RuntimeError(ex.details()) from None
     16 
     17     return func

RuntimeError: menu not found
prmukherj commented 9 months ago

The tui command is: /define/user-defined/function-hooks/execute-at-end "execute-at-end::libudf"

The PyConsole way that it works is: solver.tui.define.user_defined.function_hooks("execute-at-end", '"execute-at-end::libudf"')

@mkundu1, these are my findings so far. Please have a look into it and suggest if the behavior can be updated. Thank you.