Skip to content

Commit

Permalink
REFACTOR: Intrinsics management (#4921)
Browse files Browse the repository at this point in the history
Co-authored-by: maxcapodi78 <Shark78>
Co-authored-by: Giulia Malinverno <giulia.malinverno@ansys.com>
Co-authored-by: gmalinve <103059376+gmalinve@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 22, 2024
1 parent 1a7837a commit eb9f171
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 110 deletions.
21 changes: 19 additions & 2 deletions _unittest_solvers/test_00_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,32 @@ def test_02_hfss_export_results(self, hfss_app):
matrix_type="Y",
)
assert len(exported_files) > 0

fld_file1 = os.path.join(self.local_scratch.path, "test_fld_hfss1.fld")
assert hfss_app.post.export_field_file(quantity="Mag_E", output_file=fld_file1, assignment="Box1",
intrinsics="1GHz", phase="5deg")
assert os.path.exists(fld_file1)
fld_file2 = os.path.join(self.local_scratch.path, "test_fld_hfss2.fld")
assert hfss_app.post.export_field_file(quantity="Mag_E", output_file=fld_file2, assignment="Box1",
intrinsics="1GHz")
intrinsics={"frequency":"1GHz"})
assert os.path.exists(fld_file2)
fld_file2 = os.path.join(self.local_scratch.path, "test_fld_hfss3.fld")
assert hfss_app.post.export_field_file(quantity="Mag_E", output_file=fld_file2, assignment="Box1",
intrinsics={"frequency":"1GHz", "phase":"30deg"})
assert os.path.exists(fld_file2)
fld_file2 = os.path.join(self.local_scratch.path, "test_fld_hfss4.fld")
assert hfss_app.post.export_field_file(quantity="Mag_E", output_file=fld_file2, assignment="Box1",
intrinsics={"frequency": "1GHz"}, phase="30deg")
assert os.path.exists(fld_file2)
fld_file2 = os.path.join(self.local_scratch.path, "test_fld_hfss5.fld")
assert hfss_app.post.export_field_file(quantity="Mag_E", output_file=fld_file2, assignment="Box1",
)
assert os.path.exists(fld_file2)
fld_file2 = os.path.join(self.local_scratch.path, "test_fld_hfss6.fld")
with pytest.raises(AttributeError):
hfss_app.post.export_field_file(quantity="Mag_E", output_file=fld_file2, assignment="Box1",
intrinsics=[])
assert not os.path.exists(fld_file2)


def test_03a_icepak_analyze_and_export_summary(self):
self.icepak_app.solution_type = self.icepak_app.SOLUTIONS.Icepak.SteadyFlowOnly
Expand Down
45 changes: 45 additions & 0 deletions pyaedt/application/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,51 @@ def excitation_objects(self):

return self._excitation_objects

@pyaedt_function_handler()
def _check_intrinsics(self, input_data, input_phase=None, setup=None, return_list=False):
intrinsics = {}
if input_data is None:
if setup is None:
try:
setup = self.existing_analysis_sweeps[0].split(":")[0].strip()
except Exception:
setup = None
else:
setup = setup.split(":")[0].strip()
for set_obj in self.setups:
if set_obj.name == setup:
intrinsics = set_obj.default_intrinsics
break

elif isinstance(input_data, str):
if "Freq" in self.design_solutions.intrinsics:
intrinsics["Freq"] = input_data
if "Phase" in self.design_solutions.intrinsics:
intrinsics["Phase"] = input_phase if input_phase else "0deg"
elif "Time" in self.design_solutions.intrinsics:
intrinsics["Time"] = input_data
elif isinstance(input_data, dict):
for k, v in input_data.items():
if k in ["Freq", "freq", "frequency", "Frequency"]:
intrinsics["Freq"] = v
elif k in ["Phase", "phase"]:
intrinsics["Phase"] = v
elif k in ["Time", "time"]:
intrinsics["Time"] = v
if input_phase:
intrinsics["Phase"] = input_phase
if "Phase" in self.design_solutions.intrinsics and "Phase" not in intrinsics:
intrinsics["Phase"] = "0deg"
else:
raise AttributeError("Intrinsics has to be a string or list.")
if return_list:
intrinsics_list = []
for k, v in intrinsics.items():
intrinsics_list.append("{}:=".format(k))
intrinsics_list.append(v)
return intrinsics_list
return intrinsics

@pyaedt_function_handler()
def get_traces_for_plot(
self,
Expand Down
36 changes: 24 additions & 12 deletions pyaedt/modules/AdvancedPostProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,17 @@ def plot_field(
``"CutPlane"``, ``"Surface"``, and ``"Volume"``.
setup : str, optional
Setup and sweep name on which create the field plot. Default is None for nominal setup usage.
intrinsics : dict, optional.
Intrinsic dictionary that is needed for the export.
The default is ``None`` which try to retrieve intrinsics from setup.
intrinsics : dict, str, optional
Intrinsic variables required to compute the field before the export.
These are typically: frequency, time and phase.
It can be provided either as a dictionary or as a string.
If it is a dictionary, keys depend on the solution type and can be expressed as:
- ``"Freq"`` or ``"Frequency"``
- ``"Time"``
- ``"Phase"``
in lower or camel case.
If it is a string, it can either be ``"Freq"`` or ``"Time"`` depending on the solution type.
The default is ``None`` in which case the intrinsics value is automatically computed based on the setup.
mesh_on_fields : bool, optional
Whether to create and plot the mesh over the fields. The
default is ``False``.
Expand Down Expand Up @@ -542,16 +550,13 @@ def plot_field(
:class:`pyaedt.generic.plot.ModelPlotter`
Model Object.
"""
intrinsics = self._app._check_intrinsics(intrinsics, setup=setup)
if filter_objects is None:
filter_objects = []
if os.getenv("PYAEDT_DOC_GENERATION", "False").lower() in ("true", "1", "t"): # pragma: no cover
show = False
if not setup:
setup = self._app.existing_analysis_sweeps[0]
if not intrinsics:
for i in self._app.setups:
if i.name == setup.split(" : ")[0]:
intrinsics = i.default_intrinsics

# file_to_add = []
if plot_type == "Surface":
Expand Down Expand Up @@ -630,9 +635,17 @@ def plot_animated_field(
``"CutPlane"``, ``"Surface"``, and ``"Volume"``.
setup : str, optional
Setup and sweep name on which create the field plot. Default is None for nominal setup usage.
intrinsics : dict, optional.
Intrinsic dictionary that is needed for the export.
The default is ``None`` which try to retrieve intrinsics from setup.
intrinsics : dict, str, optional
Intrinsic variables required to compute the field before the export.
These are typically: frequency, time and phase.
It can be provided either as a dictionary or as a string.
If it is a dictionary, keys depend on the solution type and can be expressed as:
- ``"Freq"`` or ``"Frequency"``
- ``"Time"``
- ``"Phase"``
in lower or camel case.
If it is a string, it can either be ``"Freq"`` or ``"Time"`` depending on the solution type.
The default is ``None`` in which case the intrinsics value is automatically computed based on the setup.
variation_variable : str, optional
Variable to vary. The default is ``"Phi"``.
variations : list, optional
Expand Down Expand Up @@ -675,12 +688,11 @@ def plot_animated_field(
:class:`pyaedt.generic.plot.ModelPlotter`
Model Object.
"""
intrinsics = self._app._check_intrinsics(intrinsics, setup=setup)
if variations is None:
variations = ["0deg"]
if os.getenv("PYAEDT_DOC_GENERATION", "False").lower() in ("true", "1", "t"): # pragma: no cover
show = False
if intrinsics is None:
intrinsics = {}
if not export_path:
export_path = self._app.working_directory
if not filter_objects:
Expand Down
Loading

0 comments on commit eb9f171

Please sign in to comment.