1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "metadata" : {},
6+ "source" : [
7+ " \n\n # Mechanical - Thermal analysis\n\n This examples performs meshing, steady-state and transient thermal analysis of PCB.\n Objective of this example is to study or examine resulting temperatures caused by\n the heat developed in chips.\n "
8+ ]
9+ },
10+ {
11+ "cell_type" : " code" ,
12+ "execution_count" : null ,
13+ "metadata" : {
14+ "collapsed" : false
15+ },
16+ "outputs" : [],
17+ "source" : [
18+ " import os\n from pathlib import Path\n\n import ansys.mechanical.core as mech\n from matplotlib import image as mpimg\n from matplotlib import pyplot as plt"
19+ ]
20+ },
21+ {
22+ "cell_type" : " markdown" ,
23+ "metadata" : {},
24+ "source" : [
25+ " ## Parameters for the script\n The following parameters are used to control the script execution. You can\n modify these parameters to suit your needs.\n\n\n "
26+ ]
27+ },
28+ {
29+ "cell_type" : " code" ,
30+ "execution_count" : null ,
31+ "metadata" : {
32+ "collapsed" : false
33+ },
34+ "outputs" : [],
35+ "source" : [
36+ " GRAPHICS_BOOL = False # Set to True to display the graphics\n OUTPUT_DIR = Path(Path(__file__).parent, \" outputs\" ) # Output directory"
37+ ]
38+ },
39+ {
40+ "cell_type" : " markdown" ,
41+ "metadata" : {},
42+ "source" : [
43+ " ## Start a PyMechanical app\n\n\n "
44+ ]
45+ },
46+ {
47+ "cell_type" : " code" ,
48+ "execution_count" : null ,
49+ "metadata" : {
50+ "collapsed" : false
51+ },
52+ "outputs" : [],
53+ "source" : [
54+ " app = mech.App()\n app.update_globals(globals())\n print(app)\n\n\n def display_image(image_name):\n plt.figure(figsize=(16, 9))\n plt.imshow(mpimg.imread(os.path.join(OUTPUT_DIR, image_name)))\n plt.xticks([])\n plt.yticks([])\n plt.axis(\" off\" )\n plt.show()"
55+ ]
56+ },
57+ {
58+ "cell_type" : " markdown" ,
59+ "metadata" : {},
60+ "source" : [
61+ " ## Configure graphics for image export\n\n\n "
62+ ]
63+ },
64+ {
65+ "cell_type" : " code" ,
66+ "execution_count" : null ,
67+ "metadata" : {
68+ "collapsed" : false
69+ },
70+ "outputs" : [],
71+ "source" : [
72+ " ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)\n ExtAPI.Graphics.Camera.SetFit()\n image_export_format = GraphicsImageExportFormat.PNG\n settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()\n settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution\n settings_720p.Background = GraphicsBackgroundType.White\n settings_720p.Width = 1280\n settings_720p.Height = 720\n settings_720p.CurrentGraphicsDisplay = False"
73+ ]
74+ },
75+ {
76+ "cell_type" : " markdown" ,
77+ "metadata" : {},
78+ "source" : [
79+ " ## Import geometry\n Import geometry which is generated with pyansys-geometry\n\n\n "
80+ ]
81+ },
82+ {
83+ "cell_type" : " code" ,
84+ "execution_count" : null ,
85+ "metadata" : {
86+ "collapsed" : false
87+ },
88+ "outputs" : [],
89+ "source" : [
90+ " geometry_path = Path(OUTPUT_DIR, \" pcb.pmdb\" )\n geometry_import_group = Model.GeometryImportGroup\n geometry_import = geometry_import_group.AddGeometryImport()\n geometry_import_format = Ansys.Mechanical.DataModel.Enums.GeometryImportPreference.Format.Automatic\n geometry_import_preferences = Ansys.ACT.Mechanical.Utilities.GeometryImportPreferences()\n geometry_import_preferences.ProcessNamedSelections = True\n geometry_import.Import(str(geometry_path), geometry_import_format, geometry_import_preferences)\n\n # Plot geometry\n if GRAPHICS_BOOL:\n app.plot()"
91+ ]
92+ },
93+ {
94+ "cell_type" : " markdown" ,
95+ "metadata" : {},
96+ "source" : [
97+ " ## Create named selections\n\n\n "
98+ ]
99+ },
100+ {
101+ "cell_type" : " code" ,
102+ "execution_count" : null ,
103+ "metadata" : {
104+ "collapsed" : false
105+ },
106+ "outputs" : [],
107+ "source" : [
108+ " ExtAPI.Application.ActiveUnitSystem = MechanicalUnitSystem.StandardMKS\n\n # Create named selection for all bodies\n bodies = Model.Geometry.GetChildren(DataModelObjectCategory.Body, True)\n body_ids = [bd.GetGeoBody().Id for bd in bodies]\n selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)\n selection.Ids = body_ids\n ns1 = Model.AddNamedSelection()\n ns1.Name = \" all_bodies\"\n ns1.Location = selection\n\n # Create named selection for all except substrate\n substrate_id = [bd.GetGeoBody().Id for bd in bodies if bd.Name.endswith(\" substrate\" )]\n except_substrate_id = list(set(body_ids) - set(substrate_id))\n\n selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)\n selection.Ids = except_substrate_id\n ns2 = Model.AddNamedSelection()\n ns2.Name = \" all_except_board\"\n ns2.Location = selection"
109+ ]
110+ },
111+ {
112+ "cell_type" : " markdown" ,
113+ "metadata" : {},
114+ "source" : [
115+ " ## Meshing\n\n\n "
116+ ]
117+ },
118+ {
119+ "cell_type" : " code" ,
120+ "execution_count" : null ,
121+ "metadata" : {
122+ "collapsed" : false
123+ },
124+ "outputs" : [],
125+ "source" : [
126+ " mesh = Model.Mesh\n mesh.GenerateMesh()\n\n # Export mesh image\n ExtAPI.Graphics.Camera.SetFit()\n ExtAPI.Graphics.ExportImage(\n os.path.join(OUTPUT_DIR, \" mesh.png\" ), image_export_format, settings_720p\n )\n\n # Display the mesh\n if GRAPHICS_BOOL:\n display_image(\" mesh.png\" )"
127+ ]
128+ },
129+ {
130+ "cell_type" : " markdown" ,
131+ "metadata" : {},
132+ "source" : [
133+ " ## Analysis\n Setup steady state thermal analysis\n\n "
134+ ]
135+ },
136+ {
137+ "cell_type" : " code" ,
138+ "execution_count" : null ,
139+ "metadata" : {
140+ "collapsed" : false
141+ },
142+ "outputs" : [],
143+ "source" : [
144+ "steady = Model.AddSteadyStateThermalAnalysis()\ntransient = Model.AddTransientThermalAnalysis()\n\ninternal_heat_generation = steady.AddInternalHeatGeneration()\nNSall = ExtAPI.DataModel.Project.Model.NamedSelections.GetChildren[\n Ansys.ACT.Automation.Mechanical.NamedSelection\n](True)\nic6 = [i for i in NSall if i.Name == \"ic-6\"][0]\ninternal_heat_generation.Location = ic6\ninternal_heat_generation.Magnitude.Output.SetDiscreteValue(0, Quantity(5e7, \"W m^-1 m^-1 m^-1\"))\n\nall_bodies = [i for i in NSall if i.Name == \"all_bodies\"][0]\nconvection = steady.AddConvection()\nconvection.Location = all_bodies\nconvection.FilmCoefficient.Output.DiscreteValues = [Quantity(\"5[W m^-2 C^-1]\")]\n\nsteady_solution = steady.Solution\ntemperature_result = steady_solution.AddTemperature()\nsteady_solution.Solve(True)\n\n# Transient analysis setup\ninitial_condition = transient.InitialConditions[0]\ninitial_condition.InitialTemperature = InitialTemperatureType.NonUniform\ninitial_condition.InitialEnvironment = steady\n\ntransient_analysis_settings = transient.AnalysisSettings\ntransient_analysis_settings.StepEndTime = Quantity(200, \"sec\")\n\ninternal_heat_generation2 = transient.AddInternalHeatGeneration()\n\nic1 = [i for i in NSall if i.Name == \"ic-1\"][0]\ninternal_heat_generation2.Location = ic1\ninternal_heat_generation2.Magnitude.Output.SetDiscreteValue(0, Quantity(5e7, \"W m^-1 m^-1 m^-1\"))"
145+ ]
146+ },
147+ {
148+ "cell_type" : " markdown" ,
149+ "metadata" : {},
150+ "source" : [
151+ " ## Add result objects\n\n\n "
152+ ]
153+ },
154+ {
155+ "cell_type" : " code" ,
156+ "execution_count" : null ,
157+ "metadata" : {
158+ "collapsed" : false
159+ },
160+ "outputs" : [],
161+ "source" : [
162+ " transient_solution = transient.Solution\n transient_temperature_result = transient_solution.AddTemperature()\n temperature_probe1 = transient_solution.AddTemperatureProbe()\n temperature_probe1.GeometryLocation = ic6\n temperature_probe2 = transient_solution.AddTemperatureProbe()\n temperature_probe2.GeometryLocation = ic1"
163+ ]
164+ },
165+ {
166+ "cell_type" : " markdown" ,
167+ "metadata" : {},
168+ "source" : [
169+ " ## Solve\n\n\n "
170+ ]
171+ },
172+ {
173+ "cell_type" : " code" ,
174+ "execution_count" : null ,
175+ "metadata" : {
176+ "collapsed" : false
177+ },
178+ "outputs" : [],
179+ "source" : [
180+ " transient_solution.Solve(True)"
181+ ]
182+ },
183+ {
184+ "cell_type" : " markdown" ,
185+ "metadata" : {},
186+ "source" : [
187+ " ## Save files and close Mechanical\n Mechanical file (mechdb) contains results for each analysis\n\n\n "
188+ ]
189+ },
190+ {
191+ "cell_type" : " code" ,
192+ "execution_count" : null ,
193+ "metadata" : {
194+ "collapsed" : false
195+ },
196+ "outputs" : [],
197+ "source" : [
198+ " app.save(os.path.join(OUTPUT_DIR, \" pcb.mechdb\" ))\n project_directory = ExtAPI.DataModel.Project.ProjectDirectory\n app.exit()"
199+ ]
200+ }
201+ ],
202+ "metadata" : {
203+ "kernelspec" : {
204+ "display_name" : " Python 3" ,
205+ "language" : " python" ,
206+ "name" : " python3"
207+ },
208+ "language_info" : {
209+ "codemirror_mode" : {
210+ "name" : " ipython" ,
211+ "version" : 3
212+ },
213+ "file_extension" : " .py" ,
214+ "mimetype" : " text/x-python" ,
215+ "name" : " python" ,
216+ "nbconvert_exporter" : " python" ,
217+ "pygments_lexer" : " ipython3" ,
218+ "version" : " 3.12.9"
219+ }
220+ },
221+ "nbformat" : 4 ,
222+ "nbformat_minor" : 0
223+ }
0 commit comments