Skip to content

Commit

Permalink
Fixed bug in SweepMaterial creation (#3879)
Browse files Browse the repository at this point in the history
* Fixed bug in SweepMaterial creation

* Improved add_material_sweep unit test

* beautify
  • Loading branch information
Alberto-DM authored Nov 16, 2023
1 parent 67d7ec1 commit e44a1cd
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 54 deletions.
1 change: 1 addition & 0 deletions _unittest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions _unittest/test_01_Design.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import tempfile

from _unittest.conftest import config
from _unittest.conftest import desktop_version
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 25 additions & 2 deletions _unittest/test_03_Materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 1 addition & 3 deletions _unittest/test_09_VariableManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions _unittest_solvers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pyaedt/generic/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pyaedt/icepak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
74 changes: 28 additions & 46 deletions pyaedt/modules/MaterialLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = {}
Expand All @@ -221,6 +207,7 @@ def _get_surface_materials(self):
self,
ds,
self._app.project_properties["AnsoftProject"]["Definitions"]["SurfaceMaterials"][ds],
material_update=False,
)
except:
pass
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()]
Expand All @@ -404,22 +392,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
-------
Expand All @@ -441,25 +422,26 @@ def add_material_sweep(self, swargs, matname):
>>> hfss.materials.add_material_sweep(["MyMaterial", "MyMaterial2"], "Sweep_copper")
"""
matsweep = []
for args in swargs:
matobj = self.checkifmaterialexists(args)
for mat in materials_list:
matobj = self.checkifmaterialexists(mat)
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()
Expand Down Expand Up @@ -526,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:
Expand All @@ -543,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
Expand Down Expand Up @@ -580,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

Expand Down Expand Up @@ -811,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
Expand Down Expand Up @@ -864,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)

Expand Down

0 comments on commit e44a1cd

Please sign in to comment.