Note
Go to the end to download the full example code
Maxwell 3D: magnet DC analysis#
This example shows how you can use PyAEDT to create a Maxwell DC analysis, compute mass center, and move coordinate systems.
Perform required imports#
Perform required imports.
from pyaedt import Maxwell3d
from pyaedt import generate_unique_project_name
import os
Set non-graphical mode#
Set non-graphical mode. "PYAEDT_NON_GRAPHICAL"
is needed to generate
documentation only.
You can set non_graphical
either to True
or False
.
non_graphical = False
Launch AEDT#
Launch AEDT 2023 R1 in graphical mode.
m3d = Maxwell3d(projectname=generate_unique_project_name(),
specified_version="2023.1",
new_desktop_session=True,
non_graphical=non_graphical)
Set up Maxwell solution#
Set up the Maxwell solution to DC.
m3d.solution_type = m3d.SOLUTIONS.Maxwell3d.ElectroDCConduction
Create magnet#
Create a magnet.
magnet = m3d.modeler.create_box(position=[7, 4, 22], dimensions_list=[10, 5, 30], name="Magnet", matname="copper")
Create setup and assign voltage#
Create the setup and assign a voltage.
m3d.assign_voltage(magnet.faces, 0)
m3d.create_setup()
SetupName MySetupAuto with 0 Sweeps
Plot model#
Plot the model.
m3d.plot(show=False, export_path=os.path.join(m3d.working_directory, "Image.jpg"), plot_air_objects=True)

<pyaedt.generic.plot.ModelPlotter object at 0x000001BC25457F70>
Solve setup#
Solve the setup.
m3d.analyze()
True
Compute mass center#
Compute mass center using the fields calculator.
m3d.post.ofieldsreporter.EnterScalarFunc("X")
m3d.post.ofieldsreporter.EnterVol(magnet.name)
m3d.post.ofieldsreporter.CalcOp("Mean")
m3d.post.ofieldsreporter.AddNamedExpression("CM_X", "Fields")
m3d.post.ofieldsreporter.EnterScalarFunc("Y")
m3d.post.ofieldsreporter.EnterVol(magnet.name)
m3d.post.ofieldsreporter.CalcOp("Mean")
m3d.post.ofieldsreporter.AddNamedExpression("CM_Y", "Fields")
m3d.post.ofieldsreporter.EnterScalarFunc("Z")
m3d.post.ofieldsreporter.EnterVol(magnet.name)
m3d.post.ofieldsreporter.CalcOp("Mean")
m3d.post.ofieldsreporter.AddNamedExpression("CM_Z", "Fields")
m3d.post.ofieldsreporter.CalcStack("clear")
Get mass center#
Get mass center using the fields calculator.
File "C:\actions-runner\_work\_tool\Python\3.8.10\x64\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\actions-runner\_work\_tool\Python\3.8.10\x64\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\actions-runner\_work\pyaedt\pyaedt\testenv\Scripts\sphinx-build.exe\__main__.py", line 7, in <module>
sys.exit(main())
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\cmd\build.py", line 321, in main
return build_main(argv)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\cmd\build.py", line 280, in build_main
app = Sphinx(args.sourcedir, args.confdir, args.outputdir,
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\application.py", line 268, in __init__
self._init_builder()
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\application.py", line 339, in _init_builder
self.events.emit('builder-inited')
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\events.py", line 96, in emit
results.append(listener.handler(self.app, *args))
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_gallery.py", line 560, in generate_gallery_rst
) = generate_dir_rst(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 459, in generate_dir_rst
intro, title, cost = generate_file_rst(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 1075, in generate_file_rst
output_blocks, time_elapsed = execute_script(script_blocks,
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 993, in execute_script
output_blocks.append(execute_code_block(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 862, in execute_code_block
is_last_expr, mem_max = _exec_and_get_memory(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 723, in _exec_and_get_memory
mem_max, _ = gallery_conf['call_memory'](
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_gallery.py", line 213, in call_memory
return 0., func()
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 647, in __call__
exec(self.code, self.fake_main.__dict__)
File "C:\actions-runner\_work\pyaedt\pyaedt\examples\03-Maxwell\Maxwell_Magnet.py", line 95, in <module>
xval = m3d.post.get_scalar_field_value("CM_X", None)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\pyaedt\generic\general_methods.py", line 168, in wrapper
out = user_function(*args, **kwargs)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\pyaedt\modules\PostProcessor.py", line 2185, in get_scalar_field_value
self.ofieldsreporter.EnterQty(quantity_name)
RuntimeError: Error 0x80020009; Logs or message window might have more details
File "C:\actions-runner\_work\_tool\Python\3.8.10\x64\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\actions-runner\_work\_tool\Python\3.8.10\x64\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\actions-runner\_work\pyaedt\pyaedt\testenv\Scripts\sphinx-build.exe\__main__.py", line 7, in <module>
sys.exit(main())
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\cmd\build.py", line 321, in main
return build_main(argv)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\cmd\build.py", line 280, in build_main
app = Sphinx(args.sourcedir, args.confdir, args.outputdir,
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\application.py", line 268, in __init__
self._init_builder()
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\application.py", line 339, in _init_builder
self.events.emit('builder-inited')
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\events.py", line 96, in emit
results.append(listener.handler(self.app, *args))
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_gallery.py", line 560, in generate_gallery_rst
) = generate_dir_rst(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 459, in generate_dir_rst
intro, title, cost = generate_file_rst(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 1075, in generate_file_rst
output_blocks, time_elapsed = execute_script(script_blocks,
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 993, in execute_script
output_blocks.append(execute_code_block(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 862, in execute_code_block
is_last_expr, mem_max = _exec_and_get_memory(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 723, in _exec_and_get_memory
mem_max, _ = gallery_conf['call_memory'](
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_gallery.py", line 213, in call_memory
return 0., func()
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 647, in __call__
exec(self.code, self.fake_main.__dict__)
File "C:\actions-runner\_work\pyaedt\pyaedt\examples\03-Maxwell\Maxwell_Magnet.py", line 96, in <module>
yval = m3d.post.get_scalar_field_value("CM_Y", None)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\pyaedt\generic\general_methods.py", line 168, in wrapper
out = user_function(*args, **kwargs)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\pyaedt\modules\PostProcessor.py", line 2185, in get_scalar_field_value
self.ofieldsreporter.EnterQty(quantity_name)
RuntimeError: Error 0x80020009; Logs or message window might have more details
File "C:\actions-runner\_work\_tool\Python\3.8.10\x64\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\actions-runner\_work\_tool\Python\3.8.10\x64\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\actions-runner\_work\pyaedt\pyaedt\testenv\Scripts\sphinx-build.exe\__main__.py", line 7, in <module>
sys.exit(main())
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\cmd\build.py", line 321, in main
return build_main(argv)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\cmd\build.py", line 280, in build_main
app = Sphinx(args.sourcedir, args.confdir, args.outputdir,
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\application.py", line 268, in __init__
self._init_builder()
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\application.py", line 339, in _init_builder
self.events.emit('builder-inited')
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx\events.py", line 96, in emit
results.append(listener.handler(self.app, *args))
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_gallery.py", line 560, in generate_gallery_rst
) = generate_dir_rst(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 459, in generate_dir_rst
intro, title, cost = generate_file_rst(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 1075, in generate_file_rst
output_blocks, time_elapsed = execute_script(script_blocks,
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 993, in execute_script
output_blocks.append(execute_code_block(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 862, in execute_code_block
is_last_expr, mem_max = _exec_and_get_memory(
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 723, in _exec_and_get_memory
mem_max, _ = gallery_conf['call_memory'](
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_gallery.py", line 213, in call_memory
return 0., func()
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\sphinx_gallery\gen_rst.py", line 647, in __call__
exec(self.code, self.fake_main.__dict__)
File "C:\actions-runner\_work\pyaedt\pyaedt\examples\03-Maxwell\Maxwell_Magnet.py", line 97, in <module>
zval = m3d.post.get_scalar_field_value("CM_Z", None)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\pyaedt\generic\general_methods.py", line 168, in wrapper
out = user_function(*args, **kwargs)
File "c:\actions-runner\_work\pyaedt\pyaedt\testenv\lib\site-packages\pyaedt\modules\PostProcessor.py", line 2185, in get_scalar_field_value
self.ofieldsreporter.EnterQty(quantity_name)
RuntimeError: Error 0x80020009; Logs or message window might have more details
Create variables#
Create variables with mass center values.
Create coordinate system#
Create a parametric coordinate system.
cs1 = m3d.modeler.create_coordinate_system(
[magnet.name + "x", magnet.name + "y", magnet.name + "z"], reference_cs="Global", name=magnet.name + "CS"
)
Save and close#
Save the project and close AEDT.
m3d.save_project()
m3d.release_desktop(close_projects=True, close_desktop=True)
True
Total running time of the script: ( 1 minutes 3.167 seconds)