From 5baf6fbac721d935bc7d8368c07234455378ac9e Mon Sep 17 00:00:00 2001 From: adimaria Date: Tue, 14 Nov 2023 12:39:35 +0100 Subject: [PATCH 1/6] Fixed bug in SweepMaterial creation --- pyaedt/modules/MaterialLib.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/pyaedt/modules/MaterialLib.py b/pyaedt/modules/MaterialLib.py index dcbfecbc873..cf317c38c8c 100644 --- a/pyaedt/modules/MaterialLib.py +++ b/pyaedt/modules/MaterialLib.py @@ -404,22 +404,15 @@ def _create_mat_project_vars(self, matlist): return matprop @pyaedt_function_handler() - def add_material_sweep(self, swargs, matname): + def add_material_sweep(self, materials_list, material_name): """Create a sweep material made of an array of materials. - If a material needs to have a dataset (thermal modifier), then a - dataset is created. Material properties are loaded from the XML file - database ``amat.xml``. - Parameters ---------- - swargs : list + materials_list : list List of materials to merge into a single sweep material. - matname : str + material_name : str Name of the sweep material. - enableTM : bool, optional - Unavailable currently. Whether to enable the thermal modifier. - The default is ``True``. Returns ------- @@ -441,25 +434,26 @@ def add_material_sweep(self, swargs, matname): >>> hfss.materials.add_material_sweep(["MyMaterial", "MyMaterial2"], "Sweep_copper") """ matsweep = [] - for args in swargs: + for args in materials_list: matobj = self.checkifmaterialexists(args) if matobj: matsweep.append(matobj) mat_dict = self._create_mat_project_vars(matsweep) - newmat = Material(self, matname) - index = "$ID" + matname + newmat = Material(self, material_name, material_update=False) + index = "$ID" + material_name newmat.is_sweep_material = True self._app[index] = 0 for el in mat_dict: if el in list(mat_dict.keys()): - self._app["$" + matname + el] = mat_dict[el] - newmat.__dict__["_" + el].value = "$" + matname + el + "[" + index + "]" - newmat._update_props(el, "$" + matname + el + "[" + index + "]", False) + array_var_name = "$" + material_name + "_" + el + self._app[array_var_name] = mat_dict[el] + newmat.__dict__["_" + el].value = array_var_name + "[" + index + "]" + newmat._update_props(el, array_var_name + "[" + index + "]", False) newmat.update() - self.material_keys[matname.lower()] = newmat + self.material_keys[material_name.lower()] = newmat return index @pyaedt_function_handler() From 3c229edc1478cd1d03fa1004db92eb7b1ea1350f Mon Sep 17 00:00:00 2001 From: adimaria Date: Tue, 14 Nov 2023 13:01:53 +0100 Subject: [PATCH 2/6] Improved add_material_sweep unit test --- _unittest/test_03_Materials.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/_unittest/test_03_Materials.py b/_unittest/test_03_Materials.py index 53b23a0bbf1..c6f47d6dd6d 100644 --- a/_unittest/test_03_Materials.py +++ b/_unittest/test_03_Materials.py @@ -232,8 +232,31 @@ def test_09_non_linear_materials(self, add_app): assert app.materials.material_keys["mymat2"].is_used def test_10_add_material_sweep(self): - assert self.aedtapp.materials.add_material_sweep(["copper", "aluminum"], "sweep_copper") - assert "sweep_copper" in list(self.aedtapp.materials.material_keys.keys()) + material_name = "sweep_material" + assert self.aedtapp.materials.add_material_sweep(["copper", "aluminum", "FR4_epoxy"], material_name) + assert material_name in list(self.aedtapp.materials.material_keys.keys()) + properties_to_check = [ + "permittivity", + "permeability", + "conductivity", + "dielectric_loss_tangent", + "thermal_conductivity", + "mass_density", + "specific_heat", + "thermal_expansion_coefficient", + "youngs_modulus", + "poissons_ratio", + ] + # check if the variables are correctly created + assert "$ID" + material_name in self.aedtapp.variable_manager.variable_names + for prop in properties_to_check: + var_name = "$" + material_name + "_" + prop + assert var_name in self.aedtapp.variable_manager.variable_names + # check if the material properties are correct + for prop in properties_to_check: + var_name = "$" + material_name + "_" + prop + mat_prop = getattr(self.aedtapp.materials[material_name], prop).value + assert mat_prop == var_name + "[$ID" + material_name + "]" def test_11_material_case(self): assert self.aedtapp.materials["Aluminum"] == self.aedtapp.materials["aluminum"] From 9366fbb0140c41265986a663d20b354ae1571d0c Mon Sep 17 00:00:00 2001 From: adimaria Date: Tue, 14 Nov 2023 13:03:52 +0100 Subject: [PATCH 3/6] beautify --- pyaedt/modules/MaterialLib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyaedt/modules/MaterialLib.py b/pyaedt/modules/MaterialLib.py index cf317c38c8c..47a705dd825 100644 --- a/pyaedt/modules/MaterialLib.py +++ b/pyaedt/modules/MaterialLib.py @@ -434,8 +434,8 @@ def add_material_sweep(self, materials_list, material_name): >>> hfss.materials.add_material_sweep(["MyMaterial", "MyMaterial2"], "Sweep_copper") """ matsweep = [] - for args in materials_list: - matobj = self.checkifmaterialexists(args) + for mat in materials_list: + matobj = self.checkifmaterialexists(mat) if matobj: matsweep.append(matobj) From 2789c6051aaca3030b0c2f1d6778d21b7a9a6835 Mon Sep 17 00:00:00 2001 From: adimaria Date: Tue, 14 Nov 2023 15:52:04 +0100 Subject: [PATCH 4/6] beautify --- _unittest/test_09_VariableManager.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/_unittest/test_09_VariableManager.py b/_unittest/test_09_VariableManager.py index 1fd01c51075..3444dc3e731 100644 --- a/_unittest/test_09_VariableManager.py +++ b/_unittest/test_09_VariableManager.py @@ -461,14 +461,12 @@ def test_16_maxwell_circuit_variables(self): assert mc["var3"] == "10deg" assert mc["var4"] == "10rad" - def test_17_project_sweep_variable(self): + def test_17_project_variable_operation(self): self.aedtapp["$my_proj_test"] = "1mm" self.aedtapp["$my_proj_test2"] = 2 self.aedtapp["$my_proj_test3"] = "$my_proj_test*$my_proj_test2" assert self.aedtapp.variable_manager["$my_proj_test3"].units == "mm" assert self.aedtapp.variable_manager["$my_proj_test3"].numeric_value == 2.0 - self.aedtapp.materials.add_material_sweep(["copper", "aluminum"], "sweep_alu") - assert "$sweep_alupermittivity" in self.aedtapp.variable_manager.dependent_variables def test_18_test_optimization_properties(self): var = "v1" From f0561d8045791dafc94cd5a0a4637c3055aedf73 Mon Sep 17 00:00:00 2001 From: maxcapodi78 Date: Wed, 15 Nov 2023 10:23:16 +0100 Subject: [PATCH 5/6] improve efficiency in update_material calls --- pyaedt/generic/configurations.py | 4 +-- pyaedt/icepak.py | 4 ++- pyaedt/modules/MaterialLib.py | 44 ++++++++++++-------------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/pyaedt/generic/configurations.py b/pyaedt/generic/configurations.py index fb32675cab7..91403b5f4c3 100644 --- a/pyaedt/generic/configurations.py +++ b/pyaedt/generic/configurations.py @@ -1060,8 +1060,8 @@ def import_config(self, config_file, *args): self._app.logger.warning("Material %s already exists. Renaming to %s", el, newname) else: newname = el - newmat = Material(self._app, el, val) - if newmat.update(): + newmat = Material(self._app, el, val, material_update=True) + if newmat: self._app.materials.material_keys[newname] = newmat else: # pragma: no cover self.results.import_materials = False diff --git a/pyaedt/icepak.py b/pyaedt/icepak.py index e6b678d6254..c20bee56ea5 100644 --- a/pyaedt/icepak.py +++ b/pyaedt/icepak.py @@ -2953,7 +2953,7 @@ def assign_surface_material(self, obj, mat): if oo: from pyaedt.modules.Material import SurfaceMaterial - sm = SurfaceMaterial(self.materials, mat) + sm = SurfaceMaterial(self.materials, mat, material_update=False) sm.coordinate_system = oo.GetPropEvaluatedValue("Coordinate System Type") props = oo.GetPropNames() if "Surface Emissivity" in props: @@ -2966,6 +2966,8 @@ def assign_surface_material(self, obj, mat): sm.surface_diffuse_absorptance = oo.GetPropEvaluatedValue("Solar Diffuse Absorptance") if "Solar Normal Absorptance" in props: sm.surface_incident_absorptance = oo.GetPropEvaluatedValue("Solar Normal Absorptance") + sm.update() + sm._material_update = True self.materials.surface_material_keys[mat.lower()] = sm return True diff --git a/pyaedt/modules/MaterialLib.py b/pyaedt/modules/MaterialLib.py index 47a705dd825..5aa55150cd5 100644 --- a/pyaedt/modules/MaterialLib.py +++ b/pyaedt/modules/MaterialLib.py @@ -49,7 +49,6 @@ def __init__(self, app): self._desktop = self._app.odesktop self._oproject = self._app.oproject self.logger = self._app.logger - # self.material_keys = self._get_materials() self.material_keys = {} self._surface_material_keys = {} self._load_from_project() @@ -199,19 +198,6 @@ def _get_aedt_case_name(self, material_name): return self._mat_names_aedt[self.mat_names_aedt_lower.index(material_name.lower())] return False - @pyaedt_function_handler() - def _get_materials(self): - """Get materials.""" - mats = {} - try: - for ds in self._app.project_properties["AnsoftProject"]["Definitions"]["Materials"]: - mats[ds.lower()] = Material( - self, ds, self._app.project_properties["AnsoftProject"]["Definitions"]["Materials"][ds] - ) - except: - pass - return mats - @pyaedt_function_handler() def _get_surface_materials(self): mats = {} @@ -221,6 +207,7 @@ def _get_surface_materials(self): self, ds, self._app.project_properties["AnsoftProject"]["Definitions"]["SurfaceMaterials"][ds], + material_update=False, ) except: pass @@ -330,8 +317,8 @@ def add_material(self, materialname, props=None): elif self._get_aedt_case_name(materialname): return self._aedmattolibrary(self._get_aedt_case_name(materialname)) else: - material = Material(self, materialname, props) - if material.update(): + material = Material(self, materialname, props, material_update=True) + if material: self.logger.info("Material has been added. Edit it to update in Desktop.") self.material_keys[materialname.lower()] = material self._mats.append(materialname) @@ -375,10 +362,11 @@ def add_surface_material(self, material_name, emissivity=None): self.logger.warning("Warning. The material is already in the database. Change the name or edit it.") return self.surface_material_keys[material_name.lower()] else: - material = SurfaceMaterial(self._app, material_name) + material = SurfaceMaterial(self._app, material_name, material_update=False) if emissivity: material.emissivity = emissivity - material.update() + material.update() + material._material_update = True self.logger.info("Material has been added. Edit it to update in Desktop.") self.surface_material_keys[material_name.lower()] = material return self.surface_material_keys[material_name.lower()] @@ -520,8 +508,7 @@ def duplicate_material(self, material_name, new_name=None, props=None): if not new_name: new_name = material_name + "_clone" - new_material = Material(self, new_name, material._props) - new_material.update() + new_material = Material(self, new_name, material._props, material_update=False) # Parameterize material properties if these were passed. if props: @@ -537,8 +524,8 @@ def duplicate_material(self, material_name, new_name=None, props=None): setattr(new_material, p, var_name) except TypeError: print("p = {}".format(p)) - - # new_material.update() # Assign parameter to material property. + new_material.update() + new_material._material_update = True self._mats.append(new_name) self.material_keys[new_name.lower()] = new_material return new_material @@ -574,8 +561,9 @@ def duplicate_surface_material(self, material, new_name): if not material.lower() in list(self.surface_material_keys.keys()): self.logger.error("Material {} is not present".format(material)) return False - newmat = SurfaceMaterial(self, new_name.lower(), self.surface_material_keys[material.lower()]._props) - newmat.update() + newmat = SurfaceMaterial( + self, new_name.lower(), self.surface_material_keys[material.lower()]._props, material_update=True + ) self.surface_material_keys[new_name.lower()] = newmat return newmat @@ -805,8 +793,8 @@ def import_materials_from_file(self, full_json_path): self.logger.warning("Material %s already exists. Renaming to %s", el, newname) else: newname = el - newmat = Material(self, newname, val) - newmat.update() + newmat = Material(self, newname, val, material_update=True) + # newmat.update() self.material_keys[newname] = newmat materials_added.append(newmat) return materials_added @@ -858,8 +846,8 @@ def import_materials_from_excel(self, material_file): and not (isinstance(val[keys.index(prop)], float) and math.isnan(val[keys.index(prop)])) ): props[prop] = float(val[keys.index(prop)]) - new_material = Material(self, newname, props) - new_material.update() + new_material = Material(self, newname, props, material_update=True) + # new_material.update() self.material_keys[newname] = new_material materials_added.append(new_material) From 0e3085de9980b1b9d05ef24b244b6dff02314a71 Mon Sep 17 00:00:00 2001 From: maxcapodi78 Date: Wed, 15 Nov 2023 11:50:24 +0100 Subject: [PATCH 6/6] reset temp dir --- _unittest/conftest.py | 1 + _unittest/test_01_Design.py | 2 ++ _unittest_solvers/conftest.py | 1 + 3 files changed, 4 insertions(+) diff --git a/_unittest/conftest.py b/_unittest/conftest.py index 2e481dcfa91..a7202f811fb 100644 --- a/_unittest/conftest.py +++ b/_unittest/conftest.py @@ -138,6 +138,7 @@ def desktop(): for key in keys: del _desktop_sessions[key] d = Desktop(desktop_version, NONGRAPHICAL, new_thread) + d.odesktop.SetTempDirectory(tempfile.gettempdir()) d.disable_autosave() d.odesktop.SetDesktopConfiguration("All") d.odesktop.SetSchematicEnvironment(0) diff --git a/_unittest/test_01_Design.py b/_unittest/test_01_Design.py index 00e37c48092..b2ee32b2b48 100644 --- a/_unittest/test_01_Design.py +++ b/_unittest/test_01_Design.py @@ -1,4 +1,5 @@ import os +import tempfile from _unittest.conftest import config from _unittest.conftest import desktop_version @@ -90,6 +91,7 @@ def test_06_libs(self): def test_06a_set_temp_dir(self): assert os.path.exists(self.aedtapp.set_temporary_directory(os.path.join(self.local_scratch.path, "temp_dir"))) assert self.aedtapp.set_temporary_directory(os.path.join(self.local_scratch.path, "temp_dir")) + self.aedtapp.set_temporary_directory(tempfile.gettempdir()) def test_08_objects(self): print(self.aedtapp.oboundary) diff --git a/_unittest_solvers/conftest.py b/_unittest_solvers/conftest.py index 62566a67495..88608481ed0 100644 --- a/_unittest_solvers/conftest.py +++ b/_unittest_solvers/conftest.py @@ -131,6 +131,7 @@ def local_scratch(init_scratch): @pytest.fixture(scope="module", autouse=True) def desktop(): d = Desktop(desktop_version, NONGRAPHICAL, new_thread) + d.odesktop.SetTempDirectory(tempfile.gettempdir()) d.disable_autosave() yield d