# 3D Acoustic Analysis#

This example shows how to perform an acoustic analysis using PyMAPDL and `FLUID` elements.

## Launch PyMAPDL#

Launch PyMAPDL and load `matplotlib`.

```from matplotlib import pyplot as plt

from ansys.mapdl.core import launch_mapdl

mapdl = launch_mapdl()

mapdl.clear()
mapdl.prep7()
mapdl.units("SI")  # SI - International system (m, kg, s, K).
```
```SI UNITS SPECIFIED FOR INTERNAL
LENGTH        (l)  = METER (M)
MASS          (M)  = KILOGRAM (KG)
TIME          (t)  = SECOND (SEC)
TEMPERATURE   (T)  = KELVIN (K)
TOFFSET            = 0.0 KELVIN
CHARGE        (Q)  = COULOMB
FORCE         (f)  = NEWTON (N) (KG-M/SEC2)
HEAT               = JOULE (N-M)

PRESSURE           = PASCAL (NEWTON/M**2)
ENERGY        (W)  = JOULE (N-M)
POWER         (P)  = WATT (N-M/SEC)
CURRENT       (i)  = AMPERE (COULOMBS/SEC)
INDUCTANCE    (L)  = HENRY
MAGNETIC FLUX      = WEBER
RESISTANCE    (R)  = OHM
ELECTRIC POTENTIAL = VOLT

INPUT  UNITS ARE ALSO SET TO SI
```

## Element and Material Properties#

Define the `FLUID30` and `FLUID130` element types.

```mapdl.et(1, "FLUID30", kop1=2)

# Define the material properties
mapdl.mp("SONC", 1, 1500)  # sonc in m/s
mapdl.mp("DENS", 1, 1000)  # Density in kg/m3
print(mapdl.mplist())

# Define the real const
mapdl.r("1", "1e-6")  # Reference pressure for R set 1
print(mapdl.rlist())
```
```LIST MATERIALS        1 TO        1 BY        1
PROPERTY= ALL

MATERIAL NUMBER        1

TEMP        DENS
1000.000

TEMP        SONC
1500.000
*** MAPDL - ENGINEERING ANALYSIS SYSTEM  RELEASE                  22.2     ***
Ansys Mechanical Enterprise
00000000  VERSION=LINUX x64     13:10:19  JAN 18, 2023 CP=     45.642

LIST REAL SETS        1 TO        1 BY        1

REAL CONSTANT SET        1  ITEMS   1 TO   6
0.10000E-05   0.0000       0.0000       0.0000       0.0000       0.0000
```

## Geometry Definition#

Create a simple sphere.

```vnum = mapdl.sphere(rad1=0.5, rad2=1.0)
mapdl.vsbw("all")
mapdl.vplot(show_area_numbering=True)
```

## Geometry Meshing#

First select the material and elements.

```mapdl.type(itype=1)
mapdl.real(nset=1)
mapdl.mat(mat=1)
mapdl.mshape(1, "3D")
```
```PRODUCE ALL TETRAHEDRAL ELEMENTS IN 3D.

*** WARNING ***                         CP =      46.359   TIME= 13:10:20
Structural elements without mid nodes usually produce much more
accurate results in quad or brick shape.
```

Then choose the element size and perform the mesh.

```mapdl.esize(0.25)
mapdl.vmesh("all")
mapdl.eplot(show_node_numbering=False)
```

## Boundary Conditions#

Add surface boundary condition to the nodes using `Mapdl.sf()` and the option `SHLD` for Surface normal velocity or acceleration.

```mapdl.csys(2)
mapdl.asel("s", "loc", "x", 0.5)
mapdl.csys(0)
mapdl.nsla("S", 1)
mapdl.sf("all", "SHLD", 5)
mapdl.allsel()
```
```SELECT ALL ENTITIES OF TYPE= ALL  AND BELOW
```

## Solve the model#

```mapdl.allsel()
mapdl.run("/SOLU")
mapdl.antype(3)
mapdl.harfrq(freqb=200, freqe=1000)
mapdl.autots("off")
mapdl.nsubst(40)
mapdl.kbc(0)

mapdl.outres("erase")  # Save less data in order to reduce the size of .rst file
mapdl.outres("all", "none")  # NOTE that other output like stresses is not saved
mapdl.outres("nsol", "all")  # Save pressure and displacement
mapdl.outres("misc", "all")  # For post processing calculations

mapdl.solve()
```
```*****  MAPDL SOLVE    COMMAND  *****

*** NOTE ***                            CP =      46.718   TIME= 13:10:21
There is no title defined for this analysis.

*** MAPDL - ENGINEERING ANALYSIS SYSTEM  RELEASE                  22.2     ***
Ansys Mechanical Enterprise
00000000  VERSION=LINUX x64     13:10:21  JAN 18, 2023 CP=     46.723

S O L U T I O N   O P T I O N S

PROBLEM DIMENSIONALITY. . . . . . . . . . . . .3-D
DEGREES OF FREEDOM. . . . . . UX   UY   UZ   PRES
ANALYSIS TYPE . . . . . . . . . . . . . . . . .HARMONIC
SOLUTION METHOD. . . . . . . . . . . . . . .AUTO
COMPLEX DISPLACEMENT PRINT OPTION . . . . . . .REAL AND IMAGINARY
GLOBALLY ASSEMBLED MATRIX . . . . . . . . . . .SYMMETRIC

*** WARNING ***                         CP =      46.726   TIME= 13:10:21
No constraints have been defined using the D command.

*** WARNING ***                         CP =      46.728   TIME= 13:10:21
Load amplitudes will be ramped over the frequency range.  Use the KBC,1
command to use a step change.

*** NOTE ***                            CP =      46.729   TIME= 13:10:21
The step data was checked and warning messages were found.
Please review output or errors file ( /file.err ) for these warning
messages.

*** NOTE ***                            CP =      46.729   TIME= 13:10:21
The Solution Control Option is only valid for single field structural,
single field thermal, single field diffusion, coupled
thermal-diffusion analyses and coupled-field analyses with structural
degrees of freedom.  The SOLCONTROL,ON command (if present) has been
de-activated.

L O A D   S T E P   O P T I O N S

LOAD STEP NUMBER. . . . . . . . . . . . . . . .     1
FREQUENCY RANGE . . . . . . . . . . . . . . . .  200.00     TO   1000.0
NUMBER OF SUBSTEPS. . . . . . . . . . . . . . .    40
STEP CHANGE BOUNDARY CONDITIONS . . . . . . . .   YES
PRINT OUTPUT CONTROLS . . . . . . . . . . . . .NO PRINTOUT
DATABASE OUTPUT CONTROLS
ITEM     FREQUENCY   COMPONENT
ALL       NONE
NSOL        ALL
FGRA        ALL
MISC        ALL

AUTO SELECTION OF VT FOR FREQUENCY SWEEP. . . .YES

**** CENTER OF MASS, MASS, AND MASS MOMENTS OF INERTIA ****

CALCULATIONS ASSUME ELEMENT MASS AT ELEMENT CENTROID

TOTAL MASS =   3583.5

MOM. OF INERTIA         MOM. OF INERTIA

XC = -0.48377E-03      IXX =    1530.          IXX =    1530.
YC = -0.24119E-03      IYY =    1535.          IYY =    1535.
ZC = -0.47659E-04      IZZ =    1526.          IZZ =    1526.
IXY =   0.1218          IXY =   0.1222
IYZ =   0.6319          IYZ =   0.6320
IZX =   0.6068          IZX =   0.6069

*** MASS SUMMARY BY ELEMENT TYPE ***

TYPE      MASS
1   3583.49

Range of element maximum matrix coefficients in global coordinates
Maximum = 0.221163386 at element 250.
Minimum = 4.117025805E-02 at element 106.

*** ELEMENT MATRIX FORMULATION TIMES
TYPE    NUMBER   ENAME      TOTAL CP  AVE CP

1      1606  FLUID30       0.391   0.000243
Time at end of element matrix formulation CP = 47.0598564.

*** ELEMENT RESULT CALCULATION TIMES
TYPE    NUMBER   ENAME      TOTAL CP  AVE CP

1      1606  FLUID30       0.365   0.000227

TYPE    NUMBER   ENAME      TOTAL CP  AVE CP

1      1606  FLUID30       0.034   0.000021
*** LOAD STEP     1   SUBSTEP     1  COMPLETED.  FREQUENCY=   220.000
*** LOAD STEP     1   SUBSTEP     2  COMPLETED.  FREQUENCY=   240.000
*** LOAD STEP     1   SUBSTEP     3  COMPLETED.  FREQUENCY=   260.000
*** LOAD STEP     1   SUBSTEP     4  COMPLETED.  FREQUENCY=   280.000
*** LOAD STEP     1   SUBSTEP     5  COMPLETED.  FREQUENCY=   300.000
*** LOAD STEP     1   SUBSTEP     6  COMPLETED.  FREQUENCY=   320.000
*** LOAD STEP     1   SUBSTEP     7  COMPLETED.  FREQUENCY=   340.000
*** LOAD STEP     1   SUBSTEP     8  COMPLETED.  FREQUENCY=   360.000
*** LOAD STEP     1   SUBSTEP     9  COMPLETED.  FREQUENCY=   380.000
*** LOAD STEP     1   SUBSTEP    10  COMPLETED.  FREQUENCY=   400.000
*** LOAD STEP     1   SUBSTEP    11  COMPLETED.  FREQUENCY=   420.000
*** LOAD STEP     1   SUBSTEP    12  COMPLETED.  FREQUENCY=   440.000
*** LOAD STEP     1   SUBSTEP    13  COMPLETED.  FREQUENCY=   460.000
*** LOAD STEP     1   SUBSTEP    14  COMPLETED.  FREQUENCY=   480.000
*** LOAD STEP     1   SUBSTEP    15  COMPLETED.  FREQUENCY=   500.000
*** LOAD STEP     1   SUBSTEP    16  COMPLETED.  FREQUENCY=   520.000
*** LOAD STEP     1   SUBSTEP    17  COMPLETED.  FREQUENCY=   540.000
*** LOAD STEP     1   SUBSTEP    18  COMPLETED.  FREQUENCY=   560.000
*** LOAD STEP     1   SUBSTEP    19  COMPLETED.  FREQUENCY=   580.000
*** LOAD STEP     1   SUBSTEP    20  COMPLETED.  FREQUENCY=   600.000
*** LOAD STEP     1   SUBSTEP    21  COMPLETED.  FREQUENCY=   620.000
*** LOAD STEP     1   SUBSTEP    22  COMPLETED.  FREQUENCY=   640.000
*** LOAD STEP     1   SUBSTEP    23  COMPLETED.  FREQUENCY=   660.000
*** LOAD STEP     1   SUBSTEP    24  COMPLETED.  FREQUENCY=   680.000
*** LOAD STEP     1   SUBSTEP    25  COMPLETED.  FREQUENCY=   700.000
*** LOAD STEP     1   SUBSTEP    26  COMPLETED.  FREQUENCY=   720.000
*** LOAD STEP     1   SUBSTEP    27  COMPLETED.  FREQUENCY=   740.000
*** LOAD STEP     1   SUBSTEP    28  COMPLETED.  FREQUENCY=   760.000
*** LOAD STEP     1   SUBSTEP    29  COMPLETED.  FREQUENCY=   780.000
*** LOAD STEP     1   SUBSTEP    30  COMPLETED.  FREQUENCY=   800.000
*** LOAD STEP     1   SUBSTEP    31  COMPLETED.  FREQUENCY=   820.000
*** LOAD STEP     1   SUBSTEP    32  COMPLETED.  FREQUENCY=   840.000
*** LOAD STEP     1   SUBSTEP    33  COMPLETED.  FREQUENCY=   860.000
*** LOAD STEP     1   SUBSTEP    34  COMPLETED.  FREQUENCY=   880.000
*** LOAD STEP     1   SUBSTEP    35  COMPLETED.  FREQUENCY=   900.000
*** LOAD STEP     1   SUBSTEP    36  COMPLETED.  FREQUENCY=   920.000
*** LOAD STEP     1   SUBSTEP    37  COMPLETED.  FREQUENCY=   940.000
*** LOAD STEP     1   SUBSTEP    38  COMPLETED.  FREQUENCY=   960.000
*** LOAD STEP     1   SUBSTEP    39  COMPLETED.  FREQUENCY=   980.000
*** LOAD STEP     1   SUBSTEP    40  COMPLETED.  FREQUENCY=   1000.00

*** MAPDL BINARY FILE STATISTICS
BUFFER SIZE USED= 16384
20.625 MB WRITTEN ON ELEMENT MATRIX FILE: file.emat
1.188 MB WRITTEN ON ELEMENT SAVED DATA FILE: file.esav
0.312 MB WRITTEN ON ASSEMBLED MATRIX FILE: file.full
34.875 MB WRITTEN ON RESULTS FILE: file.rst
```

## Post1: Time step results#

Listing the results

```mapdl.post1()
print(mapdl.set("LIST"))
```
```*****  INDEX OF DATA SETS ON RESULTS FILE  *****

SET   TIME/FREQ    LOAD STEP   SUBSTEP  CUMULATIVE
1  220.00             1         1         1
2  220.00             1         1         1
3  240.00             1         2         2
4  240.00             1         2         2
5  260.00             1         3         3
6  260.00             1         3         3
7  280.00             1         4         4
8  280.00             1         4         4
9  300.00             1         5         5
10  300.00             1         5         5
11  320.00             1         6         6
12  320.00             1         6         6
13  340.00             1         7         7
14  340.00             1         7         7
15  360.00             1         8         8
16  360.00             1         8         8
17  380.00             1         9         9
18  380.00             1         9         9
19  400.00             1        10        10
20  400.00             1        10        10
21  420.00             1        11        11
22  420.00             1        11        11
23  440.00             1        12        12
24  440.00             1        12        12
25  460.00             1        13        13
26  460.00             1        13        13
27  480.00             1        14        14
28  480.00             1        14        14
29  500.00             1        15        15
30  500.00             1        15        15
31  520.00             1        16        16
32  520.00             1        16        16
33  540.00             1        17        17
34  540.00             1        17        17
35  560.00             1        18        18
36  560.00             1        18        18
37  580.00             1        19        19
38  580.00             1        19        19
39  600.00             1        20        20
40  600.00             1        20        20
41  620.00             1        21        21
42  620.00             1        21        21
43  640.00             1        22        22
44  640.00             1        22        22
45  660.00             1        23        23
46  660.00             1        23        23
47  680.00             1        24        24
48  680.00             1        24        24
49  700.00             1        25        25
50  700.00             1        25        25
51  720.00             1        26        26
52  720.00             1        26        26
53  740.00             1        27        27
54  740.00             1        27        27
55  760.00             1        28        28
56  760.00             1        28        28
57  780.00             1        29        29
58  780.00             1        29        29
59  800.00             1        30        30
60  800.00             1        30        30
61  820.00             1        31        31
62  820.00             1        31        31
63  840.00             1        32        32
64  840.00             1        32        32
65  860.00             1        33        33
66  860.00             1        33        33
67  880.00             1        34        34
68  880.00             1        34        34
69  900.00             1        35        35
70  900.00             1        35        35
71  920.00             1        36        36
72  920.00             1        36        36
73  940.00             1        37        37
74  940.00             1        37        37
75  960.00             1        38        38
76  960.00             1        38        38
77  980.00             1        39        39
78  980.00             1        39        39
79  1000.0             1        40        40
80  1000.0             1        40        40
```

## Post26: Time dependent results#

Getting results for specific nodes

```mapdl.post26()
freqs = mapdl.post_processing.time_values[::2]
node = 276

# Getting results
node_pressure = mapdl.nsol(3, node, "spl")
node_sound_pressure_level = mapdl.nsol(4, node, "SPLA")

# Plotting
fig, ax = plt.subplots(1, 2)

ax[0].plot(freqs, node_pressure)
ax[0].set_xlabel("Frequencies (Hz)")
ax[0].set_ylabel("Sound pressure level (Pa)")

ax[1].plot(freqs, node_sound_pressure_level, label="Nodal Sound Pressure")
ax[1].set_xlabel("Frequencies (Hz)")
ax[1].set_ylabel("A-weighted sound\npressure level (dBA)")

fig.suptitle(f"Node {node} Results")
fig.tight_layout()
fig.show()
```

Stop MAPDL

```mapdl.exit()
```

Total running time of the script: ( 0 minutes 10.826 seconds)

Gallery generated by Sphinx-Gallery