Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/4866.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add get and create methods and **kwarg support for create
Comment thread
Gobot1234 marked this conversation as resolved.
10 changes: 6 additions & 4 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ def to_python_keys(cls, value):
query_names = []
_child_aliases = {}

def _create_child_object(self, cname: str):
def _create_child_object(self, cname: str, **kwargs: Any):
ret = self._objects.get(cname)
if not ret:
cls = self.__class__.child_object_type
Expand All @@ -1399,6 +1399,7 @@ def _create_child_object(self, cname: str):
"rename",
types.MethodType(lambda obj, name: _rename(self, name, cname), ret),
)
ret.set_state(kwargs)
return ret

def _update_objects(self):
Expand Down Expand Up @@ -1451,7 +1452,7 @@ def get_object_names(self):
obj_names_list = obj_names if isinstance(obj_names, list) else list(obj_names)
return obj_names_list

def __getitem__(self, name: str) -> ChildTypeT:
def __getitem__(self, name: str, **kwargs: Any) -> ChildTypeT:
Comment thread
Gobot1234 marked this conversation as resolved.
Comment thread
Gobot1234 marked this conversation as resolved.
if name not in self.get_object_names():
if self.flproxy.has_wildcard(name):
child_cls = self.__class__.child_object_type
Expand All @@ -1473,7 +1474,7 @@ def __getitem__(self, name: str) -> ChildTypeT:

obj = self._objects.get(name)
if not obj:
obj = self._create_child_object(name)
obj = self._create_child_object(name, **kwargs)
return obj

def get(self, name: str) -> ChildTypeT:
Expand Down Expand Up @@ -1843,7 +1844,8 @@ def execute_command(self, *args, **kwds):
and isinstance(self._parent, NamedObject)
and ret in self._parent
):
Comment thread
Gobot1234 marked this conversation as resolved.
return self._parent[ret]
nameless_kwargs = {k: v for k, v in kwds.items() if k != "name"}
return self._parent.__getitem__(ret, **nameless_kwargs)
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
return_t = getattr(self, "return_type", None)
if return_t:
base_t = _baseTypes.get(return_t)
Expand Down
73 changes: 53 additions & 20 deletions src/ansys/fluent/core/solver/settings_builtin_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

"""Base classes for builtin setting classes."""

from typing import Protocol, runtime_checkable
from typing import Any, Protocol, runtime_checkable

from typing_extensions import Self

from ansys.fluent.core.solver.flobject import (
Comment on lines +27 to 30
InactiveObjectError,
Expand Down Expand Up @@ -82,19 +84,32 @@ def _get_settings_obj(settings_root, builtin_settings_obj):
return obj


def _initialize_settings(instance, defaults: dict, settings_source=None, **kwargs):
active_session = _get_active_session()
instance.__dict__.update(defaults | kwargs)
if settings_source is not None:
instance.settings_source = settings_source
elif active_session:
instance.settings_source = active_session


class _SingletonSetting:
class _SettingsObjectMixin:
def __init__(self, defaults: dict, settings_source: SettingsBase | Solver | None = None, **kwargs: Any):
active_session = _get_active_session()
self.__dict__.update(defaults | kwargs)
if settings_source is not None:
self.settings_source = settings_source
elif active_session:
self.settings_source = active_session

@classmethod
def get(cls, settings_source: SettingsBase | Solver | None = None, /, *, name: str) -> Self:
"""Get and return the singleton instance of this object in Fluent.
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring says 'singleton instance' but this method can be used on non-singleton classes like _CreatableNamedObjectSetting. The description should be more generic, such as 'Get and return an instance of this object in Fluent' to accurately reflect its usage across different setting types.

Suggested change
"""Get and return the singleton instance of this object in Fluent.
"""Get and return an instance of this object in Fluent.

Copilot uses AI. Check for mistakes.

Parameters
----------
settings_source
Something with a ``settings`` attribute. If omitted the active session is assumed from the :func:`using` context manager.
name
Name of the object to get, if applicable, can be a wildcard pattern.
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
"""
return cls(settings_source=settings_source, name=name)

class _SingletonSetting(_SettingsObjectMixin):
# Covers groups, named-object containers and commands.
def __init__(self, settings_source: SettingsBase | Solver | None = None, **kwargs):
_initialize_settings(self, {"settings_source": None}, settings_source, **kwargs)
super().__init__({"settings_source": None}, settings_source, **kwargs)

def __setattr__(self, name, value):
if name == "settings_source":
Expand All @@ -107,13 +122,12 @@ def __setattr__(self, name, value):
super().__setattr__(name, value)


class _NonCreatableNamedObjectSetting:

class _NonCreatableNamedObjectSetting(_SettingsObjectMixin):
def __init__(
self, name: str, settings_source: SettingsBase | Solver | None = None, **kwargs
):
_initialize_settings(
self, {"settings_source": None, "name": name}, settings_source, **kwargs
)
super().__init__({"settings_source": None, "name": name}, settings_source, **kwargs)

def __setattr__(self, name, value):
if name == "settings_source":
Expand All @@ -126,8 +140,7 @@ def __setattr__(self, name, value):
else:
super().__setattr__(name, value)


class _CreatableNamedObjectSetting:
class _CreatableNamedObjectSetting(_SettingsObjectMixin):
def __init__(
self,
settings_source: SettingsBase | Solver | None = None,
Expand All @@ -137,8 +150,7 @@ def __init__(
):
if name and new_instance_name:
raise ValueError("Cannot specify both name and new_instance_name.")
_initialize_settings(
self,
super().__init__(
{
"settings_source": None,
"name": name,
Expand All @@ -148,6 +160,27 @@ def __init__(
**kwargs,
)

@classmethod
def create(cls, settings_source: SettingsBase | Solver | None = None, /, name: str | None = None, **kwargs: Any) -> Self:
"""Create and return an instance of this object in Fluent.

Parameters
----------
settings_source
Something with a ``settings`` attribute. If omitted the active session is assumed from the :func:`using` context manager.
name
Name of the new object to create. If omitted, a default name will be assigned by Fluent.
**kwargs
Additional attributes to set on the created object. This only works for direct value assignments, not for nested objects.
Comment thread
Gobot1234 marked this conversation as resolved.
"""
self = cls(
settings_source=settings_source,
new_instance_name=name,
)
self.set_state(kwargs)

return self

def __setattr__(self, name, value):
if name == "settings_source":
settings_root = _get_settings_root(value)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_builtin_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pathlib import Path
import tempfile

from ansys.fluent.core.session_solver import Solver
import pytest

import ansys.fluent.core as pyfluent
Expand Down Expand Up @@ -98,6 +99,12 @@ def test_builtin_settings(mixing_elbow_case_data_session):
)
with pytest.raises(TypeError):
BoundaryCondition(settings_source=solver, new_instance_name="bc-1")

assert (
BoundaryCondition.get(settings_source=solver, name="cold-inlet")
== solver.setup.boundary_conditions["cold-inlet"]
)

assert (
VelocityInlets(settings_source=solver)
== solver.setup.boundary_conditions.velocity_inlet
Expand Down Expand Up @@ -368,6 +375,29 @@ def test_builtin_settings(mixing_elbow_case_data_session):
)


@pytest.mark.codegen_required
def test_builtin_settings_methods(mixing_elbow_case_data_session: Solver):
solver = mixing_elbow_case_data_session
assert (
ReportFile.create(solver, name="report-file-1")
== ReportFile.get(solver, name="report-file-1")
)

file = ReportFile.create(solver, name="report-file-2", file_name="foo.out")
assert (
file
== ReportFile.get(solver, name="report-file-2")
)
assert file.file_name == "foo.out"

file_2 = ReportFile.create(solver)
assert file_2.name() # it should have a default anonymous name assigned by Fluent
Comment thread
Gobot1234 marked this conversation as resolved.

assert BoundaryCondition.get(
solver, name="cold-inlet"
)
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated


Comment thread
Gobot1234 marked this conversation as resolved.
@pytest.mark.codegen_required
@pytest.mark.fluent_version(">=23.2")
def test_builtin_singleton_setting_assign_session(
Expand Down
Loading