| name | ansys-simulation |
| description | Automate ANSYS Fluent CFD simulations via Python scripting and journal files |
| category | integrations |
| domain | fluids |
| complexity | advanced |
| dependencies | [] |
ANSYS Simulation Integration
Comprehensive guide for automating ANSYS Fluent CFD (Computational Fluid Dynamics) simulations using Python scripting, journal files, and the PyAnsys ecosystem.
Overview
ANSYS Fluent is a leading commercial CFD solver used for simulating fluid flow, heat transfer, and related phenomena in complex geometries. This skill covers automation approaches for:
- ANSYS Workbench: Unified platform for CAD integration, meshing, and workflow management
- ANSYS Fluent: Advanced CFD solver with extensive physics models
- PyAnsys: Python API ecosystem for programmatic control
- Journal Files: Text-based scripting for batch operations
ANSYS Workbench and Fluent Architecture
ANSYS Workbench
- Project-based workflow environment
- Integrates multiple ANSYS tools (Fluent, CFX, Mechanical, etc.)
- Supports parametric studies and design optimization
- Provides geometry and meshing tools (DesignModeler, SpaceClaim, Meshing)
ANSYS Fluent
- Finite volume-based CFD solver
- Extensive turbulence models (k-epsilon, k-omega SST, LES, DES)
- Multiphase flow capabilities
- Heat transfer and combustion modeling
- User-defined functions (UDFs) in C
- Text User Interface (TUI) for scripting
Licensing Requirements
Commercial Licensing
ANSYS products require commercial licenses managed through the ANSYS License Manager:
-
Fluent License Types:
- HPC licenses for parallel computing
- Solver licenses (increment-based)
- PrepPost licenses for pre/post-processing
-
License Server Setup:
export ANSYSLMD_LICENSE_FILE=1055@license-server.company.com
set ANSYSLMD_LICENSE_FILE=1055@license-server.company.com
-
License Checkout: Licenses are checked out when starting Fluent and returned on exit
-
HPC Licensing: Additional licenses required for parallel runs (typically cores/4)
Academic Licensing
- Student/teaching licenses available with limitations
- Typically limited mesh size and solver capabilities
- Separate license file required
Important Notes
- Always verify license availability before batch runs
- Consider license usage in automated workflows
- Use
-t flag to specify number of processors for HPC licensing
PyAnsys - Python API Introduction
PyAnsys provides a Pythonic interface to ANSYS products, enabling:
- Remote or local session management
- Programmatic control of simulation setup
- Data extraction and post-processing
- Integration with Python data science stack
Installation
pip install ansys-fluent-core
pip install ansys-mapdl-core
pip install ansys-dpf-core
pip install ansys-geometry
Basic PyFluent Usage
from ansys.fluent.core import launch_fluent
session = launch_fluent(precision='double', processor_count=4, mode='solver')
session.tui.file.read_case('case_file.cas')
session.tui.solve.iterate(100)
session.exit()
Key PyAnsys Components
- ansys-fluent-core: Main Fluent automation package
- ansys-fluent-visualization: Post-processing and visualization
- ansys-geometry: CAD operations and geometry manipulation
- ansys-meshing: Automated meshing workflows
Journal File Scripting
Journal files are text-based scripts containing TUI commands executed sequentially by Fluent.
Journal File Format
; Comments start with semicolon
; Commands follow TUI menu structure
/file/read-case case_file.cas
/solve/initialize/initialize-flow
/solve/iterate 1000
/file/write-data result.dat
/exit yes
Running Journal Files
fluent 3ddp -g -i journal.jou > fluent.log
Journal File Best Practices
- Use absolute paths for file I/O
- Include error checking where possible
- Add comments for maintainability
- Test interactively before batch execution
- Redirect output to log files for debugging
Common Workflow
1. Geometry Import
Via PyFluent:
from ansys.fluent.core import launch_fluent
solver = launch_fluent(precision='double', processor_count=4)
solver.tui.file.import_.cad_geometry(
'geometry.scdoc',
'yes'
)
Via Journal File:
/file/import/cad-geometry geometry.step yes
2. Meshing Setup
Mesh Generation Strategy:
- Surface mesh extraction from CAD
- Volume mesh generation (tetrahedral, hex, poly)
- Boundary layer refinement for wall-bounded flows
- Mesh quality checks
PyFluent Meshing:
from ansys.fluent.core import launch_fluent
meshing = launch_fluent(precision='double', processor_count=4, mode='meshing')
meshing.workflow.InitializeWorkflow(WorkflowType='Watertight Geometry')
meshing.meshing.GlobalSettings.set_state({
'MaxSize': 0.1,
'MinSize': 0.001,
'GrowthRate': 1.2
})
meshing.meshing.GenerateMesh()
solver = meshing.switch_to_solver()
Journal File Meshing:
/mesh/scale 0.001 0.001 0.001
/mesh/check
/mesh/repair-improve/repair
3. Physics Setup
Turbulence Models:
- k-epsilon: General purpose, wall functions
- k-omega SST: Better for adverse pressure gradients, boundary layers
- LES/DES: Large eddy simulation for transient, high-fidelity
Material Properties:
- Fluid properties (density, viscosity, thermal conductivity)
- Solid properties for conjugate heat transfer
PyFluent Physics Setup:
solver.tui.define.models.energy('yes')
solver.tui.define.models.viscous.kw_sst('yes')
solver.tui.define.materials.change_create(
'water-liquid',
'water',
'yes',
'constant',
998.2,
'yes',
'constant',
0.001003
)
solver.tui.define.boundary_conditions.fluid(
'fluid-zone',
'yes',
'water',
'no',
'no',
'no',
'no',
0,
'no',
0,
'no',
0,
'no',
'no'
)
Journal File Physics:
/define/models/energy yes no no no no
/define/models/viscous kw-sst yes
/define/materials/change-create air air yes constant 1.225 yes constant 1.7894e-05
4. Boundary Conditions
Common BC Types:
- Velocity inlet
- Pressure outlet
- Wall (stationary, moving, rotating)
- Symmetry
- Periodic
PyFluent BC Setup:
solver.tui.define.boundary_conditions.velocity_inlet(
'inlet',
'yes',
'no',
'yes',
'yes',
'no',
10,
'no',
0,
'no',
0,
'no',
300,
'no'
)
solver.tui.define.boundary_conditions.pressure_outlet(
'outlet',
'yes',
'no',
0,
'no',
300,
'no',
'yes'
)
solver.tui.define.boundary_conditions.wall(
'wall',
'yes',
'no',
'no',
0,
'no',
0,
'no',
'yes',
'no',
0
)
5. Solver Settings
Solution Methods:
- Pressure-velocity coupling (SIMPLE, SIMPLEC, PISO, Coupled)
- Discretization schemes (first-order, second-order upwind, QUICK)
- Gradient evaluation (least squares, Green-Gauss)
PyFluent Solver Configuration:
solver.tui.solve.set.p_v_coupling(24)
solver.tui.solve.set.discretization_scheme.pressure(12)
solver.tui.solve.set.discretization_scheme.mom(1)
solver.tui.solve.set.discretization_scheme.k(1)
solver.tui.solve.set.discretization_scheme.omega(1)
solver.tui.solve.set.under_relaxation.pressure(0.3)
solver.tui.solve.set.under_relaxation.mom(0.7)
solver.tui.solve.initialize.initialize_flow()
solver.tui.solve.iterate(1000)
Convergence Criteria:
solver.tui.solve.monitors.residual.convergence_criteria(
'1e-6',
'1e-6',
'1e-6',
'1e-6',
'1e-6',
'1e-6',
'1e-6'
)
6. Post-Processing
Data Extraction:
solver.tui.surface.iso_surface(
'pressure',
'iso-surface-1',
'fluid-zone',
'()',
101325
)
solver.tui.file.export.ascii(
'surface-data.csv',
'iso-surface-1',
'()',
'yes',
'pressure',
'velocity-magnitude',
'()'
)
solver.tui.report.forces.wall_forces(
'yes',
'wall-zone',
'()',
'yes',
'no',
'yes'
)
Visualization:
solver.tui.display.set.contours.filled_contours('yes')
solver.tui.display.contour(
'pressure',
'pressure',
0,
0,
'surface-1',
'()'
)
solver.tui.display.save_picture('pressure_contour.png')
Batch Mode Execution
Linux/Unix Batch Execution
#!/bin/bash
export ANSYSLMD_LICENSE_FILE=1055@license-server.com
NPROCS=8
fluent 3ddp -g -t${NPROCS} -i simulation.jou > fluent_${NPROCS}cores.log 2>&1
if [ $? -eq 0 ]; then
echo "Simulation completed successfully"
else
echo "Simulation failed - check log file"
exit 1
fi
Windows Batch Execution
@echo off
REM batch_fluent.bat
set ANSYSLMD_LICENSE_FILE=1055@license-server.com
set NPROCS=8
"C:\Program Files\ANSYS Inc\v241\fluent\ntbin\win64\fluent.exe" 3ddp -g -t%NPROCS% -i simulation.jou > fluent.log 2>&1
if %ERRORLEVEL% EQU 0 (
echo Simulation completed successfully
) else (
echo Simulation failed - check log file
exit /b 1
)
Python-Based Batch Execution
import subprocess
import os
from pathlib import Path
def run_fluent_batch(journal_file, num_procs=4, log_file='fluent.log'):
"""
Run Fluent in batch mode with journal file.
Args:
journal_file: Path to journal file
num_procs: Number of processors
log_file: Output log file name
Returns:
bool: True if successful, False otherwise
"""
os.environ['ANSYSLMD_LICENSE_FILE'] = '1055@license-server.com'
cmd = [
'fluent',
'3ddp',
'-g',
f'-t{num_procs}',
'-i', journal_file
]
with open(log_file, 'w') as log:
result = subprocess.run(
cmd,
stdout=log,
stderr=subprocess.STDOUT,
text=True
)
return result.returncode == 0
if __name__ == '__main__':
success = run_fluent_batch(
journal_file='simulation.jou',
num_procs=8,
log_file='fluent_run.log'
)
if success:
print("Simulation completed successfully")
else:
print("Simulation failed - check log file")
Parametric Studies
import numpy as np
from ansys.fluent.core import launch_fluent
def parametric_study(velocities, case_file):
"""
Run parametric study varying inlet velocity.
Args:
velocities: List of inlet velocities to test
case_file: Base case file
"""
results = {}
for velocity in velocities:
print(f"Running simulation with velocity = {velocity} m/s")
solver = launch_fluent(precision='double', processor_count=4)
solver.tui.file.read_case(case_file)
solver.tui.define.boundary_conditions.velocity_inlet(
'inlet',
'yes', 'no', 'yes', 'yes', 'no',
velocity,
'no', 0, 'no', 0, 'no', 300, 'no'
)
solver.tui.solve.initialize.initialize_flow()
solver.tui.solve.iterate(500)
output_file = f'result_v{velocity}.cas.h5'
solver.tui.file.write_case_data(output_file)
solver.exit()
results[velocity] = output_file
return results
velocities = np.linspace(5, 25, 5)
results = parametric_study(velocities, 'base_case.cas.h5')
Best Practices
- License Management: Always check license availability before long batch runs
- Mesh Quality: Verify mesh quality before solving (skewness, aspect ratio)
- Convergence Monitoring: Set appropriate convergence criteria and monitor residuals
- File Management: Use clear naming conventions for parametric studies
- Error Handling: Implement robust error checking in automation scripts
- Documentation: Comment journal files and Python scripts thoroughly
- Version Control: Track journal files and scripts in version control
- Testing: Test automation workflows on small cases before production runs
- Resource Management: Monitor memory and disk usage for large simulations
- Reproducibility: Document all settings for reproducible results
Troubleshooting
Common Issues
- License checkout failures: Check license server connectivity and availability
- Mesh issues: Review mesh quality metrics and refine problematic regions
- Convergence problems: Adjust under-relaxation factors, check BC consistency
- Memory errors: Reduce mesh size or use more nodes in parallel
- File I/O errors: Verify file paths and permissions
Debugging Tips
- Run interactively first to validate workflow
- Check transcript files for TUI command syntax
- Use
-print flag in journal files for debugging
- Monitor residual plots for convergence behavior
- Review warning and error messages in log files
Additional Resources
- ANSYS Documentation: Help system within ANSYS products
- PyAnsys Documentation: https://docs.pyansys.com
- ANSYS Learning Hub: Online training and tutorials
- ANSYS Community: User forums and knowledge base
- Fluent User's Guide: Comprehensive theory and usage manual
- Fluent TUI Reference: Complete TUI command listing