Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
df68d26
Add get and create methods to objects
Gobot1234 Jan 27, 2026
61d4c66
Update src/ansys/fluent/core/solver/settings_builtin_bases.py
Gobot1234 Jan 28, 2026
f274df3
add a test case
Gobot1234 Jan 28, 2026
4e5c6be
check get on non creatable objects
Gobot1234 Jan 28, 2026
269c59e
chore: adding changelog file 4866.added.md [dependabot-skip]
pyansys-ci-bot Jan 28, 2026
c40e9ed
chore: adding changelog file 4866.added.md [dependabot-skip]
pyansys-ci-bot Jan 28, 2026
91882b2
Update src/ansys/fluent/core/solver/flobject.py
Gobot1234 Jan 28, 2026
9430310
fix tests + warning and fmt
Gobot1234 Jan 28, 2026
c38d797
generate the types for this
Gobot1234 Jan 28, 2026
9d6dacc
slightly fix up generation
Gobot1234 Jan 28, 2026
73cafbe
add all
Gobot1234 Jan 28, 2026
d42a24a
add some more tests
Gobot1234 Jan 28, 2026
779bb60
Apply suggestions from code review
Gobot1234 Jan 28, 2026
51664a4
Apply suggestions from code review
Gobot1234 Jan 29, 2026
c3c5aee
Merge branch 'main' into jhilton/create-and-find
Gobot1234 Feb 3, 2026
20ce7f9
add more tests and fix the data
Gobot1234 Feb 3, 2026
ae11446
Merge branch 'jhilton/create-and-find' of https://github.com/ansys/py…
Gobot1234 Feb 3, 2026
fedf799
touch of work still doesnt work
Gobot1234 Feb 13, 2026
fb48b06
add all as an alias for allowed_values
Gobot1234 Mar 16, 2026
a81582c
Merge branch 'main' into jhilton/create-and-find
Gobot1234 Apr 24, 2026
dae1864
Apply suggestions from code review
Gobot1234 May 5, 2026
644de8b
update docs
May 12, 2026
a173cd8
make stuff actually work
May 12, 2026
4be2437
remove unsupported tests
May 12, 2026
83f560d
chore: adding changelog file 4866.added.md [dependabot-skip]
pyansys-ci-bot May 12, 2026
61e1717
add some comments
Gobot1234 May 13, 2026
f22c11d
Update src/ansys/fluent/core/codegen/builtin_settingsgen.py
Gobot1234 May 13, 2026
2a8df61
Potential fix for pull request finding
Gobot1234 May 13, 2026
e48d985
remove warning as it's too noisy
May 14, 2026
4bf9cdf
more service fixes
May 14, 2026
f9724f6
chore: adding changelog file 4866.added.md [dependabot-skip]
pyansys-ci-bot May 14, 2026
a29e52c
add missing reciprocals
May 14, 2026
95558fe
remove kwarg setting source
May 14, 2026
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.
Outdated
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
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)
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
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)
Comment thread
Gobot1234 marked this conversation as resolved.
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]
child_attributes = {k: v for k, v in kwds.items() if k != "name"}
return self._parent.__getitem__(ret, **child_attributes)
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,
Comment on lines +27 to 31
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.
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated

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

@Gobot1234 Gobot1234 May 12, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@seanpearsonuk please can I get some feedback on this, I'm not really sure why you'd choose to support late initialisation so I've remove it as it massively complicated the implementation.

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


@pytest.mark.codegen_required
@pytest.mark.fluent_version(">=23.2")
def test_builtin_singleton_setting_assign_session(
Expand Down
Loading