Set Up, Solve, and Post-Process a Fluids Study Using AIM

This example demonstrates a basic fluids study using scripting in ANSYS AIM. After you extract the ScriptingGuideExamples.zip file, you will find the files for this example in the AIM_Fluids_Study directory.

ANSYS AIM Script The script for this example follows. Each line is numbered for reference in the discussion that follows.

1  	# encoding: utf-8
2  	SetScriptVersion(Version="16.0")
3  	Reset()
4  
5  	# Define working directory and CAD file
6  	workingDir = r"D:\ScriptingGuideExamples\AIM_Fluids_Study\"
7  	CADFile = "TrumpetValve.agdb"
8  	projectName = "sce-4-cfd.wbpj"
9  
10 	import os
11 	fullPath = os.path.join(workingDir, CADFile)
12 	savePath = os.path.join(workingDir, projectName)
13 
14 	with Transaction():
15 	    # Define Study system and access the container
16 	    system1 = GetSystem(Name="Study")
17 	    study1 = system1.GetContainer(ComponentName="Study")
18 
19 	    # Define the Tasks and Groups
20 	    importComponent1 = Study.CreateTask(Type="Import", System=system1)
21 	    meshingComponent1 = Study.CreateTask(Type="Meshing", System=system1)
22 
23 	    physicsDefinitionComponent1 = Study.CreateTask(Type="Physics Definition",System=system1)
24 	    solvePhysicsComponent1 = Study.CreateTask(Type="Solve Physics",System=system1)
25 	    # physicsDefinitionComponent1 and solvePhysicsComponent1 will be grouped together
26 
27 	    physicsSolutionGroup1 = Study.CreateGroup(Name="Physics Solution")
28 	    physicsSolutionGroup1.Add(Component=physicsDefinitionComponent1)
29 	    physicsSolutionGroup1.Add(Component=solvePhysicsComponent1)
30 	    
31 	    resultsEvaluationComponent1 = Study.CreateTask(Type="Results Evaluation", System=system1)
32 
33 	    # Define the Import 
34 	    import1 = importComponent1.GetTaskObject()
35 
36 	    # Define the Part Meshing properties
37 	    partMesh1 = meshingComponent1.GetTaskObject()
38 	    partMesh1.EngineeringIntent = "FluidFlow"
39 	    partMesh1.GlobalMeshSizingAttributes.Smoothing = "High"
40 
41 	    # Define the Solution properties
42 	    physicsDefinition1 = physicsDefinitionComponent1.GetTaskObject()
43 	    physicsDefinition1.CalculationType = "Static"
44 
45 	    # Define PhysicsRegion, SolverSettings, Material
46 	    physicsRegion1 = study1.CreateEntity(Type="PhysicsRegion",AssociatedObject=physicsDefinition1)
47 	    physicsRegion1.PhysicsType = "Fluid"
48 	    physicsRegion1.Location = "AllBodies()"
49 
50 	    solverSettings1 = study1.CreateEntity(Type="SolverSettings", Association=physicsDefinition1)
51 	    
52 	    material1 = study1.CreateEntity(Type="Material", AssociatedObject=physicsDefinition1)
53 	    material1.ImportEngineeringData(Name="Water")
54 
55 	    materialAssignment1 = study1.CreateEntity(Type="MaterialAssignment",AssociatedObject=physicsDefinition1)
56 	    materialAssignment1.Material = material1
57 	    materialAssignment1.Location = [physicsRegion1]
58 
59 	    # Define upstream-downstream links between the tasks
60 	    AddSourceToComponentInSystem(SourceComponent=importComponent1, TargetComponent=meshingComponent1)
61 	    AddSourceToComponentInSystem(SourceComponent=meshingComponent1, TargetComponent=physicsDefinitionComponent1)
62 	    AddSourceToComponentInSystem(SourceComponent=physicsDefinitionComponent1, TargetComponent=solvePhysicsComponent1)
63 	    AddSourceToComponentInSystem(SourceComponent=solvePhysicsComponent1, TargetComponent=resultsEvaluationComponent1)
64 	    
65 	    # Add a Transcript tab
66 	    transcript1 = study1.CreateEntity(Type="Transcript", Association=physicsDefinition1)
67 
68 	    # Define Results
69 	    results1 = resultsEvaluationComponent1.GetTaskObject()
70 
71 	with Transaction():
72 	    # Import the CAD 
73 	    Study.AddGeometryImportSourceOperation(ObjectReference=import1)
74 	    geometryImportSource1 = study1.GetGeometryImportSource(Name="GeometryImportSource 1")
75 	    geometryImportSource1.FilePath = fullPath
76 	    geometryImportSource1.GeometryPreferences.ImportNamedSelections = "True"
77 	    geometryImportSource1.GeometryPreferences.NamedSelectionKey = ""
78 	    
79 	    importComponent1.Update(AllDependencies=True)
80 
81 	# Refresh other cells
82 	meshingComponent1.Refresh()
83 	physicsDefinitionComponent1.Refresh()
84 	resultsEvaluationComponent1.Refresh()
85 
86 	# Define Mesh controls (boundary layers on wall)
87 	with Transaction():
88 	    meshControlLocalInflation1 = study1.CreateEntity(Type="MeshControlLocalInflation", AssociatedObject=partMesh1)
89 	    meshControlLocalInflation1.Location = 'GetSelectionSet("@wall")'
90 	    meshControlLocalInflation1.MaximumLayers = "5"
91 	    meshControlLocalInflation1.GrowthRate = "1.2"
92 
93 	# Define Solver Controls
94 	with Transaction():
95 	    numericalControls1 = study1.GetNumericalControls(Name="NumericalControls 1")
96 	    numericalControls1.SolutionControl.CouplingControl.PressureVelocityCoupling.Option = "SIMPLE"
97 	    numericalControls1.SolutionControl.ExplicitRelaxation["pressure"].ExplicitRelaxFactor = "0.4"
98 	    numericalControls1.SolutionControl.ExplicitRelaxation["velocity"].ExplicitRelaxFactor = "0.6"
99 
100	with Transaction():
101	    solutionControls1 = study1.GetSolutionControls(Name="SolutionControls 1")
102	    solutionControls1.CalculationControl.IterationControl.MaximumNumberOfIterations = "250"
103
104	# Define Boundary Conditions
105	with Transaction():
106	    inletBoundary1 = study1.CreateEntity(Type="InletBoundary", AssociatedObject=physicsRegion1)
107	    inletBoundary1.Location = 'GetSelectionSet("@inlet")'
108	    inletBoundary1.Flow.Velocity.Magnitude = "3.0 [m s^-1]"
109
110	with Transaction():
111	    outletBoundary1 = study1.CreateEntity(Type="OutletBoundary", AssociatedObject=physicsRegion1)
112	    outletBoundary1.Location = 'GetSelectionSet("@outlet")'
113	    outletBoundary1.Flow.Pressure.GaugeStaticPressure = "0 [Pa]"
114
115	with Transaction():
116	    wallBoundary1 = study1.CreateEntity(Type="WallBoundary", AssociatedObject=physicsRegion1)
117	    wallBoundary1.Location = 'GetSelectionSet("@wall")'
118
119	# Add post-processing items (contours, vectors and streamlines) on boundaries and cut-planes
120	with Transaction():
121	    contourResult1 = study1.CreateEntity(Type="ContourResult", AssociatedObject=results1)
122	    contourResult1.Variable = "Pressure"
123	    contourResult1.Location = 'GetSelectionSet("@wall")'
124	    contourResult1.DisplayText = "WallPressure"
125
126	with Transaction():
127	    vectorResult1 = study1.CreateEntity(Type="VectorResult", AssociatedObject=results1)
128	    vectorResult1.Variable = "Velocity"
129	    vectorResult1.Location = 'GetSelectionSet("@outlet")'
130	    vectorResult1.DisplayText = "OutletVelocity"
131
132	with Transaction():
133	    plane1 = study1.CreateEntity(Type="Plane", AssociatedObject=results1)
134	    plane1.DisplayText = "Z0"
135	    
136	with Transaction():
137	    plane2 = study1.CreateEntity(Type="Plane", AssociatedObject=results1)
138	    plane2.Transformation.RotationAngle = "90 [degree]"
139	    plane2.DisplayText = "Y0"
140
141	with Transaction():
142	    contourResult2 = study1.CreateEntity(Type="ContourResult", AssociatedObject=results1)
143	    contourResult2.Variable = "Pressure"
144	    contourResult2.Location = [plane2] # Y0
145	    contourResult2.DisplayText = "Y0Pressure"
146
147	with Transaction():
148	    vectorResult2 = study1.CreateEntity(Type="VectorResult", AssociatedObject=results1)
149	    vectorResult2.Variable = "Velocity"
150	    vectorResult2.Location = [plane1] # Z0
151	    vectorResult2.DisplayText = "Z0Velocity"
152
153	with Transaction():
154	    streamLine1 = study1.CreateEntity(Type="StreamLine",AssociatedObject=results1)
155	    streamLine1.Location = 'GetSelectionSet("@inlet")'
156	    streamLine1.DisplayText = "Streamlines_from_inlet"
157	    
158	# Generate the Mesh
159	meshingComponent1.Update(AllDependencies=True)
160
161	# Perform the Simulation
162	with Transaction():
163	    physicsDefinitionComponent1.Update(AllDependencies=True)
164	    solvePhysicsComponent1.Update(AllDependencies=True)
165
166	# Update the Results
167	resultsEvaluationComponent1.Update(AllDependencies=True)
168
169	# Save project to working directory
170	Save(FilePath=savePath, Overwrite=True)
171
172	# Exit from Workbench AIM
173

Discussion A description of each line follows.

Line 3

Reset() command starts a new session.

Line 6

The working directory is set, so that the script can load the geometry file from the specified location. When completing this example, this line will need to be modified to reflect your working location.

Line 10

The 'os' module used in this script provides capabilities for interacting with operating system services. Refer to the Python Standard Library documentation for details on all available modules.

Lines 14-63

This part defines a basic CFD analysis template. All the different tasks (e.g., geometry import, mesh generation, solution and results) are created and populated.

Lines 19-31

Different tasks (e.g., geometry import, mesh generation, solution, and results) are created.

Lines 36-39

Define some global properties for the mesh generation.

Lines 45-57

Define PhysicsRegion, SolverSettings, and Material for the solution.

Lines 59-63

Define links between the tasks, i.e., meshing task takes the geometry from the import task as input; solution task takes the mesh generated from the meshing task, etc.

Lines 65-66

Add a Transcript tab so that solver output information can be monitored.

Lines 71-84

The geometry file is imported (along all NamedSelections) and other tasks are refreshed as their upstream data is updated.

Lines 86-91

Define a boundary layers mesh control on the "wall" NamedSelection.

Lines 93-102

Define Solver Controls

Lines 104-117

Define Boundary Conditions.

Lines 105-108

3.0 m/s velocity magnitude for the "inlet" Named Selection.

Lines 110-113

0 Pa static pressure for the "outlet" Named Selection.

Lines 115-117

Wall for the "wall" Named Selection.

Lines 120-156

Add post-processing items (contours, vectors and streamlines) on boundaries and cut-planes.

Line 159

Generate the mesh.

Line 162-164

Perform the simulation.

Line 166-167

Evaluate all of the results.

Line 170

Save the project files.


Release 18.2 - © ANSYS, Inc. All rights reserved.