Skip to content

Props getitem and setitem #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions _unittest/test_02_3D_modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,17 @@ def test_27_create_region(self):

def test_28A_create_face_list(self):
fl = self.aedtapp.modeler.get_object_faces("Second_airbox")
assert self.aedtapp.modeler.create_face_list(fl, "my_face_list")
assert not self.aedtapp.modeler.create_face_list(fl, "my_face_list")
fl2 = self.aedtapp.modeler.create_face_list(fl, "my_face_list")
assert fl2
assert self.aedtapp.modeler.create_face_list(fl, "my_face_list") == fl2
assert self.aedtapp.modeler.create_face_list(fl)
assert self.aedtapp.modeler.create_face_list([str(fl[0])])
assert not self.aedtapp.modeler.create_face_list(["outer2"])

def test_28B_create_object_list(self):
assert self.aedtapp.modeler.create_object_list(["Second_airbox"], "my_object_list")
assert not self.aedtapp.modeler.create_object_list(["Second_airbox"], "my_object_list")
fl1 = self.aedtapp.modeler.create_object_list(["Second_airbox"], "my_object_list")
assert fl1
assert self.aedtapp.modeler.create_object_list(["Second_airbox"], "my_object_list") == fl1
assert self.aedtapp.modeler.create_object_list(["Core", "outer"])
self.aedtapp.modeler.user_lists[4].props["List"] = ["outer", "Core", "inner"]
self.aedtapp.modeler.user_lists[4].auto_update = False
Expand Down
2 changes: 2 additions & 0 deletions _unittest/test_05_Mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def test_assign_surface_mesh_manual(self):
surface = self.aedtapp.mesh.assign_surface_mesh_manual(o.id, 1e-6, aspect_ratio=3, meshop_name="Surface_Manual")
assert "Surface_Manual" in [i.name for i in self.aedtapp.mesh.meshoperations]
assert surface.props["SurfDev"] == 1e-6
surface["SurfDev"] = 1e-5
assert surface.props["SurfDev"] == 1e-5
assert surface.props["AspectRatioChoice"] == 2

cylinder_zx = self.aedtapp.modeler.create_cylinder(
Expand Down
8 changes: 7 additions & 1 deletion _unittest/test_11_Setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def test_01_create_hfss_setup(self):
setup1 = self.aedtapp.create_setup("My_HFSS_Setup", self.aedtapp.SETUPS.HFSSDrivenDefault)
assert setup1.name == "My_HFSS_Setup"
assert "SaveRadFieldsOnly" in setup1.props
setup1.props["SaveRadFieldsOnly"] = True
assert "SaveRadFieldsOnly" in setup1.available_properties
setup1["SaveRadFieldsonly"] = True
assert setup1.props["SaveRadFieldsOnly"] == setup1["SaveRadFieldsonly"]
assert setup1.enable_adaptive_setup_multifrequency([1, 2, 3])
assert setup1.props["MultipleAdaptiveFreqsSetup"]["1GHz"][0] == 0.02
assert setup1.enable_adaptive_setup_broadband(1, 2.5, 10, 0.01)
Expand Down Expand Up @@ -51,6 +53,10 @@ def test_02_create_circuit_setup(self):
setup1 = circuit.create_setup("circuit", self.aedtapp.SETUPS.NexximLNA)
assert setup1.name == "circuit"
setup1.props["SweepDefinition"]["Data"] = "LINC 0GHz 4GHz 501"
setup1["SaveRadFieldsonly"] = True
setup1["SweepDefinition/Data"] = "LINC 0GHz 4GHz 301"
assert setup1.props["SweepDefinition"]["Data"] == "LINC 0GHz 4GHz 301"
assert "SweepDefinition" in setup1.available_properties
setup1.update()
setup1.disable()
setup1.enable()
Expand Down
2 changes: 2 additions & 0 deletions _unittest/test_27_Maxwell2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def test_05_create_vector_potential(self):
bounds = self.aedtapp.assign_vector_potential(edge_object.id, 3)
assert bounds
assert bounds.props["Value"] == "3"
bounds["Value"] = "2"
assert bounds.props["Value"] == "2"
line = self.aedtapp.modeler.create_polyline([[0, 0, 0], [1, 0, 1]], name="myline")
bound2 = self.aedtapp.assign_vector_potential(line.id, 2)
assert bound2
Expand Down
2 changes: 1 addition & 1 deletion _unittest/test_41_3dlayout_modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_14_create_setup(self):
setup_name = "RFBoardSetup"
setup = self.aedtapp.create_setup(setupname=setup_name)
assert setup.name == self.aedtapp.existing_analysis_setups[0]
assert setup.setup_type == "HFSS"
assert setup.solver_type == "HFSS"

def test_15_edit_setup(self):
setup_name = "RFBoardSetup2"
Expand Down
12 changes: 9 additions & 3 deletions examples/00-EDB/03_5G_antenna_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,16 @@ def points(self):
###############################################################################
# Create Setup and Sweeps
# ~~~~~~~~~~~~~~~~~~~~~~~
#
# New getters and setter facilitate the settings on nested property dictionary.
# Previously the following command had to be used
# `setup.props["AdaptiveSettings"]["SingleFrequencyDataList"]["AdaptiveFrequencyData"]["AdaptiveFrequency"] = "20GHz"`
# `setup.props["AdaptiveSettings"]["SingleFrequencyDataList"]["AdaptiveFrequencyData"]["MaxPasses"] = 4`
# Now there are simpler approaches like showed below.

setup = h3d.create_setup()
setup.props["AdaptiveSettings"]["SingleFrequencyDataList"]["AdaptiveFrequencyData"]["AdaptiveFrequency"] = "20GHz"
setup.props["AdaptiveSettings"]["SingleFrequencyDataList"]["AdaptiveFrequencyData"]["MaxPasses"] = 4

setup["AdaptiveFrequency"] = "20GHz"
setup["AdaptiveSettings/SingleFrequencyDataList/AdaptiveFrequencyData/MaxPasses"] = 4
h3d.create_linear_count_sweep(
setupname=setup.name,
unit="GHz",
Expand Down
10 changes: 6 additions & 4 deletions examples/01-Modeling-Setup/Configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@
# Setup
# ~~~~~
# Create Setup
# with new getters and setters, the properties can be setup from the setup object
# and they don't to perfectly match the prop sintax.

setup1 = ipk.create_setup()
setup1.props["Flow Regime"] = "Turbulent"
setup1.props["Convergence Criteria - Max Iterations"] = 5
setup1.props["Linear Solver Type - Pressure"] = "flex"
setup1.props["Linear Solver Type - Temperature"] = "flex"
setup1["FlowRegime"] = "Turbulent"
setup1["Max Iterations"] = 5
setup1["Solver Type Pressure"] = "flex"
setup1["Solver Type Temperature"] = "flex"
ipk.save_project(r"C:\temp\Graphic_card.aedt")

###############################################################################
Expand Down
2 changes: 1 addition & 1 deletion examples/01-Modeling-Setup/HFSS_CoordinateSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
# The `cs1` object exposes properties and methods to manipulate the coordinate system.
# The origin can be changed.

cs1.props["OriginX"] = 10
cs1["OriginX"] = 10
cs1.props["OriginY"] = 10
cs1.props["OriginZ"] = 10

Expand Down
2 changes: 1 addition & 1 deletion examples/02-HFSS/Flex_CPWG.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def create_bending(radius, extension=0):
# ~~~~~~~~~~~~~~~
# This part creates setup and sweep.
setup = hfss.create_setup("setup1")
setup.props["Frequency"] = "2GHz"
setup["Frequency"] = "2GHz"
setup.props["MaximumPasses"] = 10
setup.props["MinimumConvergedPasses"] = 2
hfss.create_linear_count_sweep(
Expand Down
2 changes: 1 addition & 1 deletion examples/02-HFSS/HFSS_Choke.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@

setup = hfss.create_setup("MySetup")
setup.props["Frequency"] = "50MHz"
setup.props["MaximumPasses"] = 10
setup["MaximumPasses"] = 10
hfss.create_linear_count_sweep(
setupname=setup.name,
unit="MHz",
Expand Down
3 changes: 2 additions & 1 deletion examples/02-HFSS/Machine_learning_applied_to_Patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@
current_setup.props["MaximumPasses"] = 30
current_setup.props["MinimumConvergedPasses"] = 2
current_setup.props["MaxDeltaS"] = 0.05
current_setup.props["SaveAnyFields"] = False
current_setup.update()
current_setup["SaveAnyFields"] = False

freq_start = freq * 0.75
freq_stop = freq * 1.25
sweep_name = "Sweep_of_" + setup_name
Expand Down
4 changes: 2 additions & 2 deletions examples/02-HFSS/SBR_Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
setup1.props["ComputeFarFields"] = True
setup1.props["RayDensityPerWavelength"] = 2
setup1.props["MaxNumberOfBounces"] = 3
setup1.props["Sweeps"]["Sweep"]["RangeType"] = "SinglePoints"
setup1.props["Sweeps"]["Sweep"]["RangeStart"] = "10GHz"
setup1["RangeType"] = "SinglePoints"
setup1["RangeStart"] = "10GHz"
target.analyze_nominal()

###############################################################################
Expand Down
6 changes: 3 additions & 3 deletions examples/02-Maxwell/Maxwell3DTeam3.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@
# Add a linear parametric sweep for the two coil positions
sweepname = "CoilSweep"
param = M3D.parametrics.add("Coil_Position", -20, 0, 20, "LinearStep", parametricname=sweepname)
param.props["ProdOptiSetupDataV2"]["SaveFields"] = True
param.props["ProdOptiSetupDataV2"]["CopyMesh"] = False
param.props["ProdOptiSetupDataV2"]["SolveWithCopiedMeshOnly"] = True
param["SaveFields"] = True
param["CopyMesh"] = False
param["SolveWithCopiedMeshOnly"] = True

# Solve the model, we solve the parametric sweep directly so results of all variations are available.
M3D.analyze_setup(sweepname)
Expand Down
2 changes: 1 addition & 1 deletion pyaedt/application/Analysis3DLayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def create_setup(self, setupname="MySetupAuto", setuptype=None, props={}):
if props:
for el in props:
setup.props[el] = props[el]
setup.update()
setup.update()
self.analysis_setup = name
self.setups.append(setup)
return setup
Expand Down
2 changes: 1 addition & 1 deletion pyaedt/application/AnalysisNexxim.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def create_setup(self, setupname="MySetupAuto", setuptype=None, props={}):
if props:
for el in props:
setup.props._setitem_without_update(el, props[el])
setup.update()
setup.update()
self.analysis_setup = name
self.setups.append(setup)
return setup
Expand Down
2 changes: 1 addition & 1 deletion pyaedt/application/AnalysisTwinBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def create_setup(self, setupname="MySetupAuto", setuptype=None, props={}):
if props:
for el in props:
setup.props[el] = props[el]
setup.update()
setup.update()
self.analysis_setup = name
self.setups.append(setup)
return setup
109 changes: 109 additions & 0 deletions pyaedt/generic/general_methods.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import codecs
import csv
import datetime
import difflib
import fnmatch
import inspect
import itertools
Expand Down Expand Up @@ -796,6 +797,114 @@ def grpc_active_sessions(version=None, student_version=False, non_graphical=Fals
return sessions


class PropsManager(object):
def __getitem__(self, item):
"""Get the `self.props` key value.

Parameters
----------
item : str
Key to search
"""
item_split = item.split("/")
props = self.props
found_el = []
matching_percentage = 1
while matching_percentage >= 0.4:
for item_value in item_split:
found_el = difflib.get_close_matches(item_value, list(props.keys()), 1, 0.8)
if found_el:
props = props[found_el[0]]
if found_el:
return props
else:
matching_percentage -= 0.02
self._app.logger.warning("Key %s not found.Check one of available keys in self.available_properties", item)
return None

def __setitem__(self, key, value):
"""Set the `self.props` key value.

Parameters
----------
key : str
Key to apply.
value : int or float or bool or str or dict
Value to apply
"""
item_split = key.split("/")
found_el = []
props = self.props
matching_percentage = 1
key_path = []
while matching_percentage >= 0.4:
for item_value in item_split:
found_el = self._recursive_search(props, item_value, matching_percentage)
if found_el:
props = found_el[1][found_el[2]]
key_path.append(found_el[2])
if found_el:
if matching_percentage < 1:
self._app.logger.info(
"Key %s matched internal key '%s' with confidence of %s.",
key,
"/".join(key_path),
round(matching_percentage * 100),
)
matching_percentage = 0

else:
matching_percentage -= 0.02
if found_el:
found_el[1][found_el[2]] = value
self.update()
else:
props[key] = value
self.update()
self._app.logger.warning("Key %s not found. Trying to applying new key ", key)

@pyaedt_function_handler()
def _recursive_search(self, dict_in, key="", matching_percentage=0.8):
f = difflib.get_close_matches(key, list(dict_in.keys()), 1, matching_percentage)
if f:
return True, dict_in, f[0]
else:
for v in list(dict_in.values()):
if isinstance(v, (dict, OrderedDict)):
out_val = self._recursive_search(v, key, matching_percentage)
if out_val:
return out_val
return False

@pyaedt_function_handler()
def _recursive_list(self, dict_in, prefix=""):
available_list = []
for k, v in dict_in.items():
if prefix:
name = prefix + "/" + k
else:
name = k
available_list.append(name)
if isinstance(v, (dict, OrderedDict)):
available_list.extend(self._recursive_list(v, name))
return available_list

@property
def available_properties(self):
"""Available properties.

Returns
-------
list
"""
return self._recursive_list(self.props)

@pyaedt_function_handler()
def update(self):
"""Update method."""
pass


class Settings(object):
"""Class that manages all PyAEDT Environment Variables and global settings."""

Expand Down
4 changes: 2 additions & 2 deletions pyaedt/icepak.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def assign_grille(
"""
boundary_name = generate_unique_name("Grille")

self.modeler.create_face_list(air_faces, "boundary_faces")
self.modeler.create_face_list(air_faces, "boundary_faces" + boundary_name)
props = {}
air_faces = self.modeler.convert_to_selections(air_faces, True)

Expand Down Expand Up @@ -289,7 +289,7 @@ def assign_openings(self, air_faces):
pyaedt info: Opening Assigned
"""
boundary_name = generate_unique_name("Opening")
self.modeler.create_face_list(air_faces, "boundary_faces")
self.modeler.create_face_list(air_faces, "boundary_faces" + boundary_name)
props = {}
air_faces = self.modeler.convert_to_selections(air_faces, True)

Expand Down
Loading