3434import os
3535from pathlib import Path
3636
37- from ansys .mechanical .core import App
37+ import ansys .mechanical .core as mech
3838from matplotlib import image as mpimg
3939from matplotlib import pyplot as plt
4040
6363# Start a PyMechanical app
6464# ------------------------
6565#
66- app = App (globals = globals ())
66+ app = mech .App ()
67+ app .update_globals (globals ())
6768print (app )
6869
6970
@@ -97,36 +98,12 @@ def display_image(image_name):
9798# Import geometry which is generated with pyansys-geometry
9899#
99100geometry_path = Path (OUTPUT_DIR , "pcb.pmdb" )
100-
101- print ("^" * 40 )
102- print ("Importing geometry from:" , geometry_path )
103- print ("^" * 40 )
104-
105- print ("^" * 40 )
106- print ("Importing string geometry from:" , str (geometry_path ))
107- print ("^" * 40 )
108-
109- try :
110- geometry_path = Path (OUTPUT_DIR , "pcb.pmdb" )
111- geometry_import_group = Model .GeometryImportGroup
112- geometry_import = geometry_import_group .AddGeometryImport ()
113- geometry_import_format = (
114- Ansys .Mechanical .DataModel .Enums .GeometryImportPreference .Format .Automatic
115- )
116- geometry_import_preferences = Ansys .ACT .Mechanical .Utilities .GeometryImportPreferences ()
117- geometry_import_preferences .ProcessNamedSelections = True
118- geometry_import_preferences .NamedSelectionKey = ""
119- print ("^" * 40 )
120- print ("starting import..." )
121- print ("^" * 40 )
122-
123- geometry_import .Import (str (geometry_path ), geometry_import_format , geometry_import_preferences )
124- print ("geometry imported successfully." )
125- except Exception as e :
126- print (f"An error occurred during geometry import: with path { geometry_path } , error: { e } " )
127- finally :
128- app .save_as (os .path .join (OUTPUT_DIR , "geo.mechdb" ), overwrite = True )
129-
101+ geometry_import_group = Model .GeometryImportGroup
102+ geometry_import = geometry_import_group .AddGeometryImport ()
103+ geometry_import_format = Ansys .Mechanical .DataModel .Enums .GeometryImportPreference .Format .Automatic
104+ geometry_import_preferences = Ansys .ACT .Mechanical .Utilities .GeometryImportPreferences ()
105+ geometry_import_preferences .ProcessNamedSelections = True
106+ geometry_import .Import (str (geometry_path ), geometry_import_format , geometry_import_preferences )
130107
131108# Plot geometry
132109if GRAPHICS_BOOL :
@@ -138,105 +115,113 @@ def display_image(image_name):
138115# -----------------------
139116#
140117
141- # ExtAPI.Application.ActiveUnitSystem = MechanicalUnitSystem.StandardMKS
142-
143- # # Create named selection for all bodies
144- # bodies = Model.Geometry.GetChildren(DataModelObjectCategory.Body, True)
145- # body_ids = [bd.GetGeoBody().Id for bd in bodies]
146- # selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
147- # selection.Ids = body_ids
148- # ns1 = Model.AddNamedSelection()
149- # ns1.Name = "all_bodies"
150- # ns1.Location = selection
151-
152- # # Create named selection for all except substrate
153- # substrate_id = [bd.GetGeoBody().Id for bd in bodies if bd.Name.endswith("substrate")]
154- # except_substrate_id = list(set(body_ids) - set(substrate_id))
155-
156- # selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
157- # selection.Ids = except_substrate_id
158- # ns2 = Model.AddNamedSelection()
159- # ns2.Name = "all_except_board"
160- # ns2.Location = selection
161-
162- # ###############################################################################
163- # # Meshing
164- # # -------
165- # #
166- # mesh = Model.Mesh
167- # mesh.GenerateMesh()
168-
169- # # Export mesh image
170- # ExtAPI.Graphics.Camera.SetFit()
171- # ExtAPI.Graphics.ExportImage(
172- # os.path.join(OUTPUT_DIR, "mesh.png"), image_export_format, settings_720p
173- # )
174-
175- # # Display the mesh
176- # if GRAPHICS_BOOL:
177- # display_image("mesh.png")
178-
179-
180- # ###############################################################################
181- # # Analysis
182- # # --------
183- # # Setup steady state thermal analysis
184-
185- # steady = Model.AddSteadyStateThermalAnalysis()
186- # transient = Model.AddTransientThermalAnalysis()
187-
188- # internal_heat_generation = steady.AddInternalHeatGeneration()
189- # NSall = ExtAPI.DataModel.Project.Model.NamedSelections.GetChildren[
190- # Ansys.ACT.Automation.Mechanical.NamedSelection
191- # ](True)
192- # ic6 = [i for i in NSall if i.Name == "ic-6"][0]
193- # internal_heat_generation.Location = ic6
194- # internal_heat_generation.Magnitude.Output.SetDiscreteValue(0, Quantity(5e7, "W m^-1 m^-1 m^-1"))
195-
196- # all_bodies = [i for i in NSall if i.Name == "all_bodies"][0]
197- # convection = steady.AddConvection()
198- # convection.Location = all_bodies
199- # convection.FilmCoefficient.Output.DiscreteValues = [Quantity("5[W m^-2 C^-1]")]
200-
201- # steady_solution = steady.Solution
202- # temperature_result = steady_solution.AddTemperature()
203- # steady_solution.Solve(True)
204-
205- # # Transient analysis setup
206- # initial_condition = transient.InitialConditions[0]
207- # initial_condition.InitialTemperature = InitialTemperatureType.NonUniform
208- # initial_condition.InitialEnvironment = steady
209-
210- # transient_analysis_settings = transient.AnalysisSettings
211- # transient_analysis_settings.StepEndTime = Quantity(200, "sec")
212-
213- # internal_heat_generation2 = transient.AddInternalHeatGeneration()
214-
215- # ic1 = [i for i in NSall if i.Name == "ic-1"][0]
216- # internal_heat_generation2.Location = ic1
217- # internal_heat_generation2.Magnitude.Output.SetDiscreteValue(0, Quantity(5e7, "W m^-1 m^-1 m^-1"))
218-
219- # ###############################################################################
220- # # Add result objects
221- # # ------------------
222- # #
223- # transient_solution = transient.Solution
224- # transient_temperature_result = transient_solution.AddTemperature()
225- # temperature_probe1 = transient_solution.AddTemperatureProbe()
226- # temperature_probe1.GeometryLocation = ic6
227- # temperature_probe2 = transient_solution.AddTemperatureProbe()
228- # temperature_probe2.GeometryLocation = ic1
229-
230- # ###############################################################################
231- # # Solve
232- # # -----
233- # #
234- # transient_solution.Solve(True)
118+ ExtAPI .Application .ActiveUnitSystem = MechanicalUnitSystem .StandardMKS
119+
120+ # Create named selection for all bodies
121+ bodies = Model .Geometry .GetChildren (DataModelObjectCategory .Body , True )
122+ body_ids = [bd .GetGeoBody ().Id for bd in bodies ]
123+ selection = ExtAPI .SelectionManager .CreateSelectionInfo (SelectionTypeEnum .GeometryEntities )
124+ selection .Ids = body_ids
125+ ns1 = Model .AddNamedSelection ()
126+ ns1 .Name = "all_bodies"
127+ ns1 .Location = selection
128+
129+ # Create named selection for all except substrate
130+ substrate_id = [bd .GetGeoBody ().Id for bd in bodies if bd .Name .endswith ("substrate" )]
131+ except_substrate_id = list (set (body_ids ) - set (substrate_id ))
132+
133+ selection = ExtAPI .SelectionManager .CreateSelectionInfo (SelectionTypeEnum .GeometryEntities )
134+ selection .Ids = except_substrate_id
135+ ns2 = Model .AddNamedSelection ()
136+ ns2 .Name = "all_except_board"
137+ ns2 .Location = selection
138+
139+ ###############################################################################
140+ # Meshing
141+ # -------
142+ #
143+ mesh = Model .Mesh
144+ mesh .GenerateMesh ()
145+
146+ # Export mesh image
147+ ExtAPI .Graphics .Camera .SetFit ()
148+ ExtAPI .Graphics .ExportImage (
149+ os .path .join (OUTPUT_DIR , "mesh.png" ), image_export_format , settings_720p
150+ )
151+
152+ # Display the mesh
153+ if GRAPHICS_BOOL :
154+ display_image ("mesh.png" )
155+
156+
157+ ###############################################################################
158+ # Analysis
159+ # --------
160+ # Setup steady state thermal analysis
161+
162+ steady = Model .AddSteadyStateThermalAnalysis ()
163+ transient = Model .AddTransientThermalAnalysis ()
164+
165+ internal_heat_generation = steady .AddInternalHeatGeneration ()
166+ NSall = ExtAPI .DataModel .Project .Model .NamedSelections .GetChildren [
167+ Ansys .ACT .Automation .Mechanical .NamedSelection
168+ ](True )
169+ ic6 = [i for i in NSall if i .Name == "ic-6" ][0 ]
170+ internal_heat_generation .Location = ic6
171+ internal_heat_generation .Magnitude .Output .SetDiscreteValue (0 , Quantity (5e7 , "W m^-1 m^-1 m^-1" ))
172+
173+ all_bodies = [i for i in NSall if i .Name == "all_bodies" ][0 ]
174+ convection = steady .AddConvection ()
175+ convection .Location = all_bodies
176+ convection .FilmCoefficient .Output .DiscreteValues = [Quantity ("5[W m^-2 C^-1]" )]
177+
178+ steady_solution = steady .Solution
179+ temperature_result = steady_solution .AddTemperature ()
180+ steady_solution .Solve (True )
181+
182+ # Transient analysis setup
183+ initial_condition = transient .InitialConditions [0 ]
184+ initial_condition .InitialTemperature = InitialTemperatureType .NonUniform
185+ initial_condition .InitialEnvironment = steady
186+
187+ transient_analysis_settings = transient .AnalysisSettings
188+ transient_analysis_settings .StepEndTime = Quantity (200 , "sec" )
189+
190+ internal_heat_generation2 = transient .AddInternalHeatGeneration ()
191+
192+ ic1 = [i for i in NSall if i .Name == "ic-1" ][0 ]
193+ internal_heat_generation2 .Location = ic1
194+ internal_heat_generation2 .Magnitude .Output .SetDiscreteValue (0 , Quantity (5e7 , "W m^-1 m^-1 m^-1" ))
195+
196+ ###############################################################################
197+ # Add result objects
198+ # ------------------
199+ #
200+ transient_solution = transient .Solution
201+ transient_temperature_result = transient_solution .AddTemperature ()
202+ temperature_probe1 = transient_solution .AddTemperatureProbe ()
203+ temperature_probe1 .GeometryLocation = ic6
204+ temperature_probe2 = transient_solution .AddTemperatureProbe ()
205+ temperature_probe2 .GeometryLocation = ic1
206+
207+ ###############################################################################
208+ # Solve
209+ # -----
210+ #
211+ transient_solution .Solve (True )
212+
213+ # TODO :Remove once completed
214+ # Export mesh image
215+ Tree .Activate ([transient_temperature_result ])
216+ ExtAPI .Graphics .Camera .SetFit ()
217+ ExtAPI .Graphics .ExportImage (
218+ os .path .join (OUTPUT_DIR , "temperature.png" ), image_export_format , settings_720p
219+ )
235220
236221###############################################################################
237222# Save files and close Mechanical
238223# -------------------------------
239224# Mechanical file (mechdb) contains results for each analysis
240225#
241- app .save_as (os .path .join (OUTPUT_DIR , "pcb.mechdb" ), overwrite = True )
226+ app .save (os .path .join (OUTPUT_DIR , "pcb.mechdb" ))
242227app .close ()
0 commit comments