Skip to content
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

FEAT: MaxwellMatrix class #4855

Merged
merged 8 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 21 additions & 2 deletions _unittest/test_28_Maxwell3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ 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
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 +536,24 @@ 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 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 +572,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
123 changes: 123 additions & 0 deletions pyaedt/modules/Boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,33 @@
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 given a parent matrix.

Returns
-------
dict
Dictionary of reduced matrices where key is the name of the parent matrix
and values is 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 @@

@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

Check warning on line 1562 in pyaedt/modules/Boundary.py

View check run for this annotation

Codecov / codecov/patch

pyaedt/modules/Boundary.py#L1561-L1562

Added lines #L1561 - L1562 were not covered by tests
if not matrix_name:
matrix_name = generate_unique_name("ReducedMatrix", n=3)
if not join_name:
Expand Down Expand Up @@ -1592,6 +1622,99 @@
)


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.

Parameters
----------

Returns
-------
"""
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
It can either 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 be included in 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("Source does not exist.")
return False

Check warning on line 1685 in pyaedt/modules/Boundary.py

View check run for this annotation

Codecov / codecov/patch

pyaedt/modules/Boundary.py#L1684-L1685

Added lines #L1684 - L1685 were not covered by tests
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("Join type not valid.")
return False

Check warning on line 1690 in pyaedt/modules/Boundary.py

View check run for this annotation

Codecov / codecov/patch

pyaedt/modules/Boundary.py#L1689-L1690

Added lines #L1689 - L1690 were not covered by tests
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("Invalid source name.")
return False

Check warning on line 1713 in pyaedt/modules/Boundary.py

View check run for this annotation

Codecov / codecov/patch

pyaedt/modules/Boundary.py#L1712-L1713

Added lines #L1712 - L1713 were not covered by tests
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.

Expand Down
Loading