From ea7ce53d1936cf427d1b7211f0da6206995c4195 Mon Sep 17 00:00:00 2001 From: Giulia Malinverno Date: Tue, 24 Sep 2024 15:20:31 +0200 Subject: [PATCH 1/3] merge examples in one --- .../magnetic/magneto_motive_contour.py | 138 ------------------ .../magnetic/magneto_motive_line.py | 115 ++++++++++++--- 2 files changed, 91 insertions(+), 162 deletions(-) delete mode 100644 examples/low_frequency/magnetic/magneto_motive_contour.py diff --git a/examples/low_frequency/magnetic/magneto_motive_contour.py b/examples/low_frequency/magnetic/magneto_motive_contour.py deleted file mode 100644 index d46ae8155..000000000 --- a/examples/low_frequency/magnetic/magneto_motive_contour.py +++ /dev/null @@ -1,138 +0,0 @@ -# # Magnetomotive force along several lines -# -# This example shows how to use PyAEDT to calculate -# the magnetomotive force along several lines. -# It shows how to leverage the PyAEDT advanced fields calculator -# to insert a custom formula, which in this case is the integral -# of the H field along a line. -# -# Keywords: **Maxwell 2D**, **magnetomotive force**. - -# ## Perform imports and define constants -# -# Perform required imports. - -import os -import tempfile -import time - -import ansys.aedt.core - -# Define constants. - -AEDT_VERSION = "2024.2" -NUM_CORES = 4 -NG_MODE = False # Open AEDT UI when it is launched. - -# ## Create temporary directory -# -# Create a temporary directory where downloaded data or -# dumped data can be stored. -# If you'd like to retrieve the project data for subsequent use, -# the temporary folder name is given by ``temp_folder.name``. - -temp_folder = tempfile.TemporaryDirectory(suffix=".ansys") - -# ## Import project -# -# Download the files required to run this example to the temporary working folder. - -project_path = ansys.aedt.core.downloads.download_file( - source="maxwell_magnetic_force", - name="Maxwell_Magnetic_Force.aedt", - destination=temp_folder.name, -) - -# ## Initialize and launch Maxwell 2D -# -# Initialize and launch Maxwell 2D, providing the version and the path of the project. - -m2d = ansys.aedt.core.Maxwell2d( - version=AEDT_VERSION, - non_graphical=NG_MODE, - project=project_path, - design="Maxwell2DDesign1", -) - -# ## Create a polyline -# -# Create a polyline, specifying its points. - -poly = m2d.modeler.create_polyline(points=[[10, -10, 0], [10, 10, 0]], name="polyline") - -# Duplicate the polyline along a vector. - -polys = [poly.name] -polys.extend(poly.duplicate_along_line(vector=[-0.5, 0, 0], clones=10)) - -# ## Plot model - -model = m2d.plot(show=False) -model.plot(os.path.join(temp_folder.name, "Image.jpg")) - -# ## Analyze setup -# -# Analyze the setup, specifying the setup name. - -m2d.analyze_setup(name=m2d.setups[0].name, cores=NUM_CORES, use_auto_settings=False) - -# ## Compute magnetomotive force along each line -# -# Create and add a new formula to add in the PyAEDT advanced fields calculator. -# Create the fields report object and get field data. -# Create a data table report for the H field along each line and export it to a .csv file. - -for p in polys: - quantity = "H_field_{}".format(p) - my_expression = { - "name": quantity, - "description": "Magnetomotive force along a line", - "design_type": ["Maxwell 2D", "Maxwell 3D"], - "fields_type": ["Fields"], - "primary_sweep": "distance", - "assignment": p, - "assignment_type": ["Line"], - "operations": [ - "Fundamental_Quantity('H')", - "Operation('Tangent')", - "Operation('Dot')", - "EnterLine('assignment')", - "Operation('LineValue')", - "Operation('Integrate')", - ], - "report": ["Data Table"], - } - m2d.post.fields_calculator.add_expression(my_expression, p) - report = m2d.post.reports_by_category.fields( - expressions=quantity, setup=m2d.nominal_sweep, polyline=p - ) - data = report.get_solution_data() - h = data.data_magnitude() - report = m2d.post.create_report( - expressions=quantity, - context=p, - polyline_points=1, - report_category="Fields", - plot_type="Data Table", - plot_name=quantity, - ) - m2d.post.export_report_to_csv( - project_dir=temp_folder.name, - plot_name=quantity, - ) - -# ## Release AEDT - -m2d.save_project() -m2d.release_desktop() -# Wait 3 seconds to allow AEDT to shut down before cleaning the temporary directory. -time.sleep(3) - -# ## Clean up -# -# All project files are saved in the folder ``temp_folder.name``. -# If you've run this example as a Jupyter notebook, you -# can retrieve those project files. The following cell -# removes all temporary files, including the project folder. - -temp_folder.cleanup() diff --git a/examples/low_frequency/magnetic/magneto_motive_line.py b/examples/low_frequency/magnetic/magneto_motive_line.py index 093020798..c340ba5be 100644 --- a/examples/low_frequency/magnetic/magneto_motive_line.py +++ b/examples/low_frequency/magnetic/magneto_motive_line.py @@ -4,16 +4,19 @@ # the magnetomotive force along a line that changes position. # It shows how to leverage the PyAEDT advanced fields calculator # to insert a custom formula, which in this case is the integral -# of the H field along a line. It computes the field for each position -# with a parametric sweep. -# +# of the H field along a line. +# The example shows two options to achieve the intent. +# The first one creates many lines as to simulate a contour that changes position. +# The integral of the H field is computed for each line. +# The second option creates one parametric polyline and then uses a parametric sweep to change its position. +# The integral of the H field is computed for each position. + # Keywords: **Maxwell 2D**, **magnetomotive force**. # ## Perform imports and define constants # # Perform required imports. -import os import tempfile import time @@ -55,6 +58,59 @@ design="Maxwell2DDesign1", ) +# # First option + +# ## Create a polyline +# +# Create a polyline, specifying its ends. + +poly = m2d.modeler.create_polyline(points=[[10, -10, 0], [10, 10, 0]], name="polyline") + +# Duplicate the polyline along a vector. + +polys = [poly.name] +polys.extend(poly.duplicate_along_line(vector=[-0.5, 0, 0], clones=10)) + +# ## Compute magnetomotive force along each line +# +# Create and add a new formula to add in the PyAEDT advanced fields calculator. +# Create the fields report object and get field data. +# Create a data table report for the H field along each line and export it to a .csv file. + +quantities = [] +for p in polys: + quantity = "H_field_{}".format(p) + quantities.append(quantity) + my_expression = { + "name": quantity, + "description": "Magnetomotive force along a line", + "design_type": ["Maxwell 2D", "Maxwell 3D"], + "fields_type": ["Fields"], + "primary_sweep": "distance", + "assignment": p, + "assignment_type": ["Line"], + "operations": [ + "Fundamental_Quantity('H')", + "Operation('Tangent')", + "Operation('Dot')", + "EnterLine('assignment')", + "Operation('LineValue')", + "Operation('Integrate')", + ], + "report": ["Data Table"], + } + m2d.post.fields_calculator.add_expression(my_expression, p) + report = m2d.post.create_report( + expressions=quantity, + context=p, + polyline_points=1, + report_category="Fields", + plot_type="Data Table", + plot_name=quantity, + ) + +# # Second option + # ## Create a design variable # # Parametrize the polyline x position. @@ -63,17 +119,12 @@ # ## Create polyline # -# Create a polyline, specifying its points. +# Create a parametrized polyline, specifying its ends. poly = m2d.modeler.create_polyline( - points=[["xl", -10, 0], ["xl", 10, 0]], name="polyline" + points=[["xl", -10, 0], ["xl", 10, 0]], name="polyline_sweep" ) -# ## Plot model - -model = m2d.plot(show=False) -model.plot(os.path.join(temp_folder.name, "Image.jpg")) - # ## Add parametric sweep # # Add a parametric sweep where the parameter to sweep is ``xl``. @@ -91,9 +142,9 @@ # # Create and add a new formula to add in the PyAEDT advanced fields calculator. -quantity = "H_field_{}".format(poly.name) +quantity_sweep = "H_field_{}".format(poly.name) my_expression = { - "name": quantity, + "name": quantity_sweep, "description": "Magnetomotive force along a line", "design_type": ["Maxwell 2D", "Maxwell 3D"], "fields_type": ["Fields"], @@ -112,31 +163,47 @@ } m2d.post.fields_calculator.add_expression(my_expression, poly.name) -# ## Add parametric sweep calculation specifying the quantity (H). +# ## Add parametric sweep calculation specifying the quantity (H) and save fields. -param_sweep.add_calculation(calculation=quantity, report_type="Fields", ranges={}) - -# ## Analyze parametric sweep - -param_sweep.analyze(cores=NUM_CORES) +param_sweep.add_calculation(calculation=quantity_sweep, report_type="Fields", ranges={}) +param_sweep.props["ProdOptiSetupDataV2"]["SaveFields"] = True # ## Create data table report # # Create a data table report to display H for each polyline position. -# Afterwards, export results to a CSV file. -report = m2d.post.create_report( - expressions=quantity, +report_sweep = m2d.post.create_report( + expressions=quantity_sweep, report_category="Fields", plot_type="Data Table", - plot_name=quantity, + plot_name=quantity_sweep, primary_sweep_variable="xl", + variations={"xl": "All"}, ) + +# ## Analyze parametric sweep + +param_sweep.analyze(cores=NUM_CORES) + +# ## Export results +# +# Export results in a .csv file for the parametric sweep analysis (second option). + m2d.post.export_report_to_csv( project_dir=temp_folder.name, - plot_name=quantity, + plot_name=quantity_sweep, ) +# Export results in a .csv file for each polyline (first option). + +[ + m2d.post.export_report_to_csv( + project_dir=temp_folder.name, + plot_name=q, + ) + for q in quantities +] + # ## Release AEDT m2d.save_project() From 57c39a2d1b7e9e51185923e2a2c95259f622b636 Mon Sep 17 00:00:00 2001 From: Giulia Malinverno Date: Tue, 24 Sep 2024 16:41:25 +0200 Subject: [PATCH 2/3] add plot and update index.rst --- examples/aedt/maxwell_2d/index.rst | 1 - examples/low_frequency/magnetic/index.rst | 1 - examples/low_frequency/magnetic/magneto_motive_line.py | 6 ++++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/aedt/maxwell_2d/index.rst b/examples/aedt/maxwell_2d/index.rst index dab96d50b..38378af73 100644 --- a/examples/aedt/maxwell_2d/index.rst +++ b/examples/aedt/maxwell_2d/index.rst @@ -10,7 +10,6 @@ These examples use PyAEDT to show Maxwell 2D capabilities ../../low_frequency/general/electrostatic.py ../../low_frequency/general/external_circuit.py ../../low_frequency/general/resistance.py - ../../low_frequency/magnetic/magneto_motive_contour.py ../../low_frequency/magnetic/magneto_motive_line.py ../../low_frequency/magnetic/transient_winding.py ../../low_frequency/magnetic/lorentz_actuator.py diff --git a/examples/low_frequency/magnetic/index.rst b/examples/low_frequency/magnetic/index.rst index b7d2098aa..6929bdbaf 100644 --- a/examples/low_frequency/magnetic/index.rst +++ b/examples/low_frequency/magnetic/index.rst @@ -7,6 +7,5 @@ These examples use PyAEDT to show some magnetics applications. transient_winding.py choke.py - magneto_motive_contour.py magneto_motive_line.py lorentz_actuator.py diff --git a/examples/low_frequency/magnetic/magneto_motive_line.py b/examples/low_frequency/magnetic/magneto_motive_line.py index c340ba5be..8aa4c3b3c 100644 --- a/examples/low_frequency/magnetic/magneto_motive_line.py +++ b/examples/low_frequency/magnetic/magneto_motive_line.py @@ -17,6 +17,7 @@ # # Perform required imports. +import os import tempfile import time @@ -58,6 +59,11 @@ design="Maxwell2DDesign1", ) +# ## Plot model + +model = m2d.plot(show=False) +model.plot(os.path.join(temp_folder.name, "Image.jpg")) + # # First option # ## Create a polyline From 13ce9e2ec3fc8164354f97c364a9c9df7dca6cae Mon Sep 17 00:00:00 2001 From: Giulia Malinverno Date: Tue, 24 Sep 2024 17:53:37 +0200 Subject: [PATCH 3/3] improve expression --- .../magnetic/magneto_motive_line.py | 59 ++++++++----------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/examples/low_frequency/magnetic/magneto_motive_line.py b/examples/low_frequency/magnetic/magneto_motive_line.py index 8aa4c3b3c..dc6547de6 100644 --- a/examples/low_frequency/magnetic/magneto_motive_line.py +++ b/examples/low_frequency/magnetic/magneto_motive_line.py @@ -83,28 +83,31 @@ # Create the fields report object and get field data. # Create a data table report for the H field along each line and export it to a .csv file. +my_expression = { + "name": None, + "description": "Magnetomotive force along a line", + "design_type": ["Maxwell 2D", "Maxwell 3D"], + "fields_type": ["Fields"], + "primary_sweep": "distance", + "assignment": None, + "assignment_type": ["Line"], + "operations": [ + "Fundamental_Quantity('H')", + "Operation('Tangent')", + "Operation('Dot')", + "EnterLine('assignment')", + "Operation('LineValue')", + "Operation('Integrate')", + ], + "report": ["Data Table"], +} + quantities = [] for p in polys: quantity = "H_field_{}".format(p) quantities.append(quantity) - my_expression = { - "name": quantity, - "description": "Magnetomotive force along a line", - "design_type": ["Maxwell 2D", "Maxwell 3D"], - "fields_type": ["Fields"], - "primary_sweep": "distance", - "assignment": p, - "assignment_type": ["Line"], - "operations": [ - "Fundamental_Quantity('H')", - "Operation('Tangent')", - "Operation('Dot')", - "EnterLine('assignment')", - "Operation('LineValue')", - "Operation('Integrate')", - ], - "report": ["Data Table"], - } + my_expression["name"] = quantity + my_expression["assignment"] = quantity m2d.post.fields_calculator.add_expression(my_expression, p) report = m2d.post.create_report( expressions=quantity, @@ -149,24 +152,8 @@ # Create and add a new formula to add in the PyAEDT advanced fields calculator. quantity_sweep = "H_field_{}".format(poly.name) -my_expression = { - "name": quantity_sweep, - "description": "Magnetomotive force along a line", - "design_type": ["Maxwell 2D", "Maxwell 3D"], - "fields_type": ["Fields"], - "primary_sweep": "distance", - "assignment": poly.name, - "assignment_type": ["Line"], - "operations": [ - "Fundamental_Quantity('H')", - "Operation('Tangent')", - "Operation('Dot')", - "EnterLine('assignment')", - "Operation('LineValue')", - "Operation('Integrate')", - ], - "report": ["Data Table"], -} +my_expression["name"] = quantity_sweep +my_expression["assignment"] = poly.name m2d.post.fields_calculator.add_expression(my_expression, poly.name) # ## Add parametric sweep calculation specifying the quantity (H) and save fields.