Skip to content

Commit

Permalink
FEAT: MaxwellMatrix class (#4855)
Browse files Browse the repository at this point in the history
Co-authored-by: Kathy Pippert <[email protected]>
  • Loading branch information
gmalinve and PipKat authored Jul 3, 2024
1 parent c4cde69 commit 8a30b49
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 3 deletions.
31 changes: 29 additions & 2 deletions _unittest/test_28_Maxwell3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,14 @@ def test_32a_matrix(self, add_app):
m3d.assign_current(rectangle4.faces[0], amplitude=1, name="Cur4")

L = m3d.assign_matrix(assignment=["Cur1", "Cur2", "Cur3"], matrix_name="Matrix1")
assert not L.reduced_matrices
m3d.solution_type = SOLUTIONS.Maxwell3d.Magnetostatic
out = L.join_series(sources=["Cur1", "Cur2"], matrix_name="ReducedMatrix3")
assert not out[0]
assert not out[1]
m3d.solution_type = SOLUTIONS.Maxwell3d.EddyCurrent
out = L.join_series(sources=["Cur1", "Cur2"], matrix_name="ReducedMatrix1")
assert L.reduced_matrices
assert isinstance(out[0], str)
assert isinstance(out[1], str)
out = L.join_parallel(["Cur1", "Cur3"], matrix_name="ReducedMatrix2")
Expand All @@ -534,7 +541,27 @@ def test_32a_matrix(self, add_app):
out = L.join_parallel(["Cur5"])
assert not out[0]

def test_32b_export_rl_matrix(self):
def test_32b_reduced_matrix(self):
self.aedtapp.set_active_design("Matrix2")
parent_matrix = [m for m in self.aedtapp.boundaries if m.type == "Matrix"][0]
assert parent_matrix.reduced_matrices
reduced_matrix_1 = parent_matrix.reduced_matrices[0]
assert reduced_matrix_1.name == "ReducedMatrix1"
assert reduced_matrix_1.parent_matrix == parent_matrix.name
source_name = list(reduced_matrix_1.sources.keys())[0]
assert reduced_matrix_1.update(old_source=source_name, source_type="series", new_source="new_series")
assert list(reduced_matrix_1.sources.keys())[0] == "new_series"
assert reduced_matrix_1.sources["new_series"] == "Cur1, Cur2"
assert reduced_matrix_1.update(old_source="new_series", source_type="series", new_excitations="Cur2, Cur3")
assert list(reduced_matrix_1.sources.keys())[0] == "new_series"
assert reduced_matrix_1.sources["new_series"] == "Cur2, Cur3"
assert not reduced_matrix_1.update(old_source="invalid", source_type="series", new_excitations="Cur2, Cur3")
assert not reduced_matrix_1.update(old_source="new_series", source_type="invalid", new_excitations="Cur2, Cur3")
assert not reduced_matrix_1.delete(source="invalid")
assert reduced_matrix_1.delete(source="new_series")
assert len(parent_matrix.reduced_matrices) == 1

def test_32c_export_rl_matrix(self):
self.aedtapp.set_active_design("Matrix2")
L = self.aedtapp.assign_matrix(assignment=["Cur1", "Cur2", "Cur3"], matrix_name="matrix_export_test")
L.join_series(["Cur1", "Cur2"], matrix_name="reduced_matrix_export_test")
Expand All @@ -553,7 +580,7 @@ def test_32b_export_rl_matrix(self):
assert self.aedtapp.export_rl_matrix("matrix_export_test", export_path_2, False, 10, 3, True)
assert os.path.exists(export_path_2)

def test_32c_post_processing(self):
def test_32d_post_processing(self):
expressions = self.aedtapp.post.available_report_quantities(
report_category="EddyCurrent", display_type="Data Table", context={"Matrix1": "ReducedMatrix1"}
)
Expand Down
118 changes: 117 additions & 1 deletion pyaedt/modules/Boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,33 @@ def __init__(self, app, name, props=None, boundarytype=None):
self.type = boundarytype
self._boundary_name = self.name
self.auto_update = True
self.__reduced_matrices = None
self.matrix_assignment = None

@property
def reduced_matrices(self):
"""List of reduced matrix groups for the parent matrix.
Returns
-------
dict
Dictionary of reduced matrices where the key is the name of the parent matrix
and the values are in a list of reduced matrix groups.
"""
if self._app.solution_type == "EddyCurrent":
self.__reduced_matrices = {}
cc = self._app.odesign.GetChildObject("Parameters")
parents = cc.GetChildNames()
if self.name in parents:
parent_object = self._app.odesign.GetChildObject("Parameters").GetChildObject(self.name)
parent_type = parent_object.GetPropValue("Type")
if parent_type == "Matrix":
self.matrix_assignment = parent_object.GetPropValue("Selection").split(",")
child_names = parent_object.GetChildNames()
self.__reduced_matrices = []
for r in child_names:
self.__reduced_matrices.append(MaxwellMatrix(self._app, self.name, r))
return self.__reduced_matrices

@property
def object_properties(self):
Expand Down Expand Up @@ -1530,6 +1557,9 @@ def update(self):

@pyaedt_function_handler()
def _create_matrix_reduction(self, red_type, sources, matrix_name=None, join_name=None):
if not self._app.solution_type == "EddyCurrent":
self._app.logger.error("Matrix reduction is possible only in Eddy current solvers.")
return False, False
if not matrix_name:
matrix_name = generate_unique_name("ReducedMatrix", n=3)
if not join_name:
Expand Down Expand Up @@ -1592,8 +1622,94 @@ def join_parallel(self, sources, matrix_name=None, join_name=None):
)


class MaxwellMatrix(object):
def __init__(self, app, parent_name, reduced_name):
self._app = app
self.parent_matrix = parent_name
self.name = reduced_name
self.__sources = None

@property
def sources(self):
"""List of matrix sources."""
if self._app.solution_type == "EddyCurrent":
sources = (
self._app.odesign.GetChildObject("Parameters")
.GetChildObject(self.parent_matrix)
.GetChildObject(self.name)
.GetChildNames()
)
self.__sources = {}
for s in sources:
excitations = (
self._app.odesign.GetChildObject("Parameters")
.GetChildObject(self.parent_matrix)
.GetChildObject(self.name)
.GetChildObject(s)
.GetPropValue("Source")
)
self.__sources[s] = excitations
return self.__sources

@pyaedt_function_handler()
def update(self, old_source, source_type, new_source=None, new_excitations=None):
"""Update the reduced matrix.
Parameters
----------
old_source : str
Original name of the source to update.
source_type : str
Source type, which can be ``Series`` or ``Parallel``.
new_source : str, optional
New name of the source to update.
The default value is the old source name.
new_excitations : str, optional
List of excitations to include in the matrix reduction.
The default values are excitations included in the source to update.
Returns
-------
bool
``True`` when successful, ``False`` when failed.
"""
if old_source not in self.sources.keys():
self._app.logger.error("Source does not exist.")
return False
else:
new_excitations = self.sources[old_source] if not new_excitations else new_excitations
if source_type.lower() not in ["series", "parallel"]:
self._app.logger.error("Join type not valid.")
return False
if not new_source:
new_source = old_source
args = ["NAME:" + new_source, "Type:=", "Join in " + source_type, "Sources:=", new_excitations]
self._app.o_maxwell_parameters.EditReduceOp(self.parent_matrix, self.name, old_source, args)
return True

@pyaedt_function_handler()
def delete(self, source):
"""Delete a specified source in a reduced matrix.
Parameters
----------
source : string
Name of the source to delete.
Returns
-------
bool
``True`` when successful, ``False`` when failed.
"""
if source not in self.sources.keys():
self._app.logger.error("Invalid source name.")
return False
self._app.o_maxwell_parameters.DeleteReduceOp(self.parent_matrix, self.name, source)
return True


class FieldSetup(BoundaryCommon, object):
"""Manages Far Field and Near Field Component data and execution.
"""Manages far field and near field component data and execution.
Examples
--------
Expand Down

0 comments on commit 8a30b49

Please sign in to comment.